diff options
author | Drew DeVault <sir@cmpwn.com> | 2017-08-27 17:36:38 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-27 17:36:38 -0500 |
commit | 7553103e345c6fb65b18cf5bc18a60218f810b77 (patch) | |
tree | 1188483c734bbd7a3dadda21792a24669f5e69cc | |
parent | Update IRC channel in CONTRIBUTING.md (diff) | |
parent | Use fork in swaygrab instead of popen. (diff) | |
download | sway-7553103e345c6fb65b18cf5bc18a60218f810b77.tar.gz sway-7553103e345c6fb65b18cf5bc18a60218f810b77.tar.zst sway-7553103e345c6fb65b18cf5bc18a60218f810b77.zip |
Merge pull request #1247 from clarcharr/master
Convert a popen into a fork.
-rw-r--r-- | swaygrab/main.c | 31 |
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 | ||
63 | void grab_and_apply_movie_magic(const char *file, const char *payload, | 74 | void grab_and_apply_movie_magic(const char *file, const char *payload, |