aboutsummaryrefslogtreecommitdiffstats
path: root/swaygrab/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaygrab/main.c')
-rw-r--r--swaygrab/main.c38
1 files changed, 27 insertions, 11 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 @@
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,32 @@ 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[0]);
63 write(fd[1], pixels, len);
64 close(fd[1]);
65 free(pixels - 9);
66 waitpid(child, NULL, 0);
67 } else {
68 close(fd[1]);
69 if (dup2(fd[0], 0) != 0) {
70 sway_log(L_ERROR, "Could not fdup the pipe");
71 }
72 close(fd[0]);
73 execlp("convert", "convert", "-depth", "8", "-size", size, "rgba:-", "-flip", file, NULL);
74 sway_log(L_ERROR, "Swaygrab could not run convert.");
75 exit(EXIT_FAILURE);
76 }
61} 77}
62 78
63void grab_and_apply_movie_magic(const char *file, const char *payload, 79void 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,
93 "-video_size %dx%d -pixel_format argb " 109 "-video_size %dx%d -pixel_format argb "
94 "-i pipe:0 -r %d -vf vflip %s"; 110 "-i pipe:0 -r %d -vf vflip %s";
95 char *cmd = malloc(strlen(fmt) - 8 /*args*/ 111 char *cmd = malloc(strlen(fmt) - 8 /*args*/
96 + strlen(ffmpeg_opts) + numlen(width) + numlen(height) 112 + strlen(ffmpeg_opts) + numlen(width) + numlen(height)
97 + numlen(framerate) * 2 + strlen(file) + 1); 113 + numlen(framerate) * 2 + strlen(file) + 1);
98 sprintf(cmd, fmt, ffmpeg_opts, framerate, width, height, framerate, file); 114 sprintf(cmd, fmt, ffmpeg_opts, framerate, width, height, framerate, file);
99 115