diff options
Diffstat (limited to 'swaygrab')
-rw-r--r-- | swaygrab/main.c | 38 | ||||
-rw-r--r-- | swaygrab/swaygrab.1.txt | 2 |
2 files changed, 28 insertions, 12 deletions
diff --git a/swaygrab/main.c b/swaygrab/main.c index c437653d..1b699bb9 100644 --- a/swaygrab/main.c +++ b/swaygrab/main.c @@ -9,6 +9,7 @@ #include <stdint.h> #include <math.h> #include <time.h> +#include <sys/wait.h> #include <json-c/json.h> #include "log.h" #include "ipc-client.h" @@ -47,17 +48,32 @@ void grab_and_apply_magick(const char *file, const char *payload, return; } - const char *fmt = "convert -depth 8 -size %dx%d+0 rgba:- -flip %s"; - char *cmd = malloc(strlen(fmt) - 6 /*args*/ - + numlen(width) + numlen(height) + strlen(file) + 1); - sprintf(cmd, fmt, width, height, file); + char size[10 + 1 + 10 + 2 + 1]; // int32_t are max 10 digits + sprintf(size, "%dx%d+0", width, height); - FILE *f = popen(cmd, "w"); - fwrite(pixels, 1, len, f); - fflush(f); - fclose(f); - free(pixels - 9); - free(cmd); + pid_t child; + int fd[2]; + pipe(fd); + + if ((child = fork()) < 0) { + sway_log(L_ERROR, "Swaygrab failed to fork."); + exit(EXIT_FAILURE); + } else if (child != 0) { + close(fd[0]); + write(fd[1], pixels, len); + close(fd[1]); + free(pixels - 9); + waitpid(child, NULL, 0); + } else { + close(fd[1]); + if (dup2(fd[0], 0) != 0) { + sway_log(L_ERROR, "Could not fdup the pipe"); + } + close(fd[0]); + execlp("convert", "convert", "-depth", "8", "-size", size, "rgba:-", "-flip", file, NULL); + sway_log(L_ERROR, "Swaygrab could not run convert."); + exit(EXIT_FAILURE); + } } void grab_and_apply_movie_magic(const char *file, const char *payload, @@ -93,7 +109,7 @@ void grab_and_apply_movie_magic(const char *file, const char *payload, "-video_size %dx%d -pixel_format argb " "-i pipe:0 -r %d -vf vflip %s"; char *cmd = malloc(strlen(fmt) - 8 /*args*/ - + strlen(ffmpeg_opts) + numlen(width) + numlen(height) + + strlen(ffmpeg_opts) + numlen(width) + numlen(height) + numlen(framerate) * 2 + strlen(file) + 1); sprintf(cmd, fmt, ffmpeg_opts, framerate, width, height, framerate, file); diff --git a/swaygrab/swaygrab.1.txt b/swaygrab/swaygrab.1.txt index 1da56db8..8faf43f5 100644 --- a/swaygrab/swaygrab.1.txt +++ b/swaygrab/swaygrab.1.txt @@ -73,4 +73,4 @@ Authors Maintained by Drew DeVault <sir@cmpwn.com>, who is assisted by other open source contributors. For more information about sway development, see -<https://github.com/SirCmpwn/sway>. +<https://github.com/swaywm/sway>. |