aboutsummaryrefslogtreecommitdiffstats
path: root/swaygrab
diff options
context:
space:
mode:
authorLibravatar Clar Charr <clar@charr.xyz>2017-06-20 16:03:40 -0400
committerLibravatar Clar Charr <clar@charr.xyz>2017-08-27 14:12:52 -0400
commitfab57205765e35a6071279bdbd43eb304f958757 (patch)
tree1188483c734bbd7a3dadda21792a24669f5e69cc /swaygrab
parentUpdate IRC channel in CONTRIBUTING.md (diff)
downloadsway-fab57205765e35a6071279bdbd43eb304f958757.tar.gz
sway-fab57205765e35a6071279bdbd43eb304f958757.tar.zst
sway-fab57205765e35a6071279bdbd43eb304f958757.zip
Use fork in swaygrab instead of popen.
Diffstat (limited to 'swaygrab')
-rw-r--r--swaygrab/main.c31
1 files changed, 21 insertions, 10 deletions
diff --git a/swaygrab/main.c b/swaygrab/main.c
index c437653d..6e851899 100644
--- a/swaygrab/main.c
+++ b/swaygrab/main.c
@@ -9,6 +9,7 @@
9#include <stdint.h> 9#include <stdint.h>
10#include <math.h> 10#include <math.h>
11#include <time.h> 11#include <time.h>
12#include <sys/wait.h>
12#include <json-c/json.h> 13#include <json-c/json.h>
13#include "log.h" 14#include "log.h"
14#include "ipc-client.h" 15#include "ipc-client.h"
@@ -47,17 +48,27 @@ void grab_and_apply_magick(const char *file, const char *payload,
47 return; 48 return;
48 } 49 }
49 50
50 const char *fmt = "convert -depth 8 -size %dx%d+0 rgba:- -flip %s"; 51 char size[10 + 1 + 10 + 2 + 1]; // int32_t are max 10 digits
51 char *cmd = malloc(strlen(fmt) - 6 /*args*/ 52 sprintf(size, "%dx%d+0", width, height);
52 + numlen(width) + numlen(height) + strlen(file) + 1);
53 sprintf(cmd, fmt, width, height, file);
54 53
55 FILE *f = popen(cmd, "w"); 54 pid_t child;
56 fwrite(pixels, 1, len, f); 55 int fd[2];
57 fflush(f); 56 pipe(fd);
58 fclose(f); 57
59 free(pixels - 9); 58 if ((child = fork()) < 0) {
60 free(cmd); 59 sway_log(L_ERROR, "Swaygrab failed to fork.");
60 exit(EXIT_FAILURE);
61 } else if (child == 0) {
62 close(fd[1]);
63 write(fd[0], pixels, len);
64 free(pixels - 9);
65 waitpid(child, NULL, 0);
66 } else {
67 close(fd[0]);
68 execlp("convert", "-depth", "8", "-size", size, "rgba:-", "-flip", file, NULL);
69 sway_log(L_ERROR, "Swaygrab could not run convert.");
70 exit(EXIT_FAILURE);
71 }
61} 72}
62 73
63void grab_and_apply_movie_magic(const char *file, const char *payload, 74void grab_and_apply_movie_magic(const char *file, const char *payload,