summaryrefslogtreecommitdiffstats
path: root/swaygrab/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'swaygrab/main.c')
-rw-r--r--swaygrab/main.c298
1 files changed, 0 insertions, 298 deletions
diff --git a/swaygrab/main.c b/swaygrab/main.c
deleted file mode 100644
index 1b699bb9..00000000
--- a/swaygrab/main.c
+++ /dev/null
@@ -1,298 +0,0 @@
1#define _XOPEN_SOURCE 700
2#define _POSIX_C_SOURCE 199309L
3#include <stdio.h>
4#include <stdbool.h>
5#include <stdlib.h>
6#include <string.h>
7#include <getopt.h>
8#include <unistd.h>
9#include <stdint.h>
10#include <math.h>
11#include <time.h>
12#include <sys/wait.h>
13#include <json-c/json.h>
14#include "log.h"
15#include "ipc-client.h"
16#include "util.h"
17#include "swaygrab/json.h"
18
19void sway_terminate(int exit_code) {
20 exit(exit_code);
21}
22
23void grab_and_apply_magick(const char *file, const char *payload,
24 int socketfd, int raw) {
25 uint32_t len = strlen(payload);
26 char *pixels = ipc_single_command(socketfd,
27 IPC_SWAY_GET_PIXELS, payload, &len);
28 uint32_t *u32pixels = (uint32_t *)(pixels + 1);
29 uint32_t width = u32pixels[0];
30 uint32_t height = u32pixels[1];
31 len -= 9;
32 pixels += 9;
33
34 if (width == 0 || height == 0) {
35 // indicates geometry was clamped by WLC because it was outside of the output's area
36 json_object *obj = json_tokener_parse(payload);
37 json_object *output;
38 json_object_object_get_ex(obj, "output", &output);
39 const char *name = json_object_get_string(output);
40 json_object_put(obj);
41 sway_abort("Unknown output %s.", name);
42 }
43
44 if (raw) {
45 fwrite(pixels, 1, len, stdout);
46 fflush(stdout);
47 free(pixels - 9);
48 return;
49 }
50
51 char size[10 + 1 + 10 + 2 + 1]; // int32_t are max 10 digits
52 sprintf(size, "%dx%d+0", width, height);
53
54 pid_t child;
55 int fd[2];
56 pipe(fd);
57
58 if ((child = fork()) < 0) {
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 }
77}
78
79void grab_and_apply_movie_magic(const char *file, const char *payload,
80 int socketfd, int raw, int framerate) {
81 if (raw) {
82 sway_log(L_ERROR, "Raw capture data is not yet supported. Proceeding with ffmpeg normally.");
83 }
84
85 uint32_t len = strlen(payload);
86 char *pixels = ipc_single_command(socketfd,
87 IPC_SWAY_GET_PIXELS, payload, &len);
88 uint32_t *u32pixels = (uint32_t *)(pixels + 1);
89 uint32_t width = u32pixels[0];
90 uint32_t height = u32pixels[1];
91 pixels += 9;
92
93 if (width == 0 || height == 0) {
94 // indicates geometry was clamped by WLC because it was outside of the output's area
95 json_object *obj = json_tokener_parse(payload);
96 json_object *output;
97 json_object_object_get_ex(obj, "output", &output);
98 const char *name = json_object_get_string(output);
99 json_object_put(obj);
100 sway_abort("Unknown output %s.", name);
101 }
102
103 char *ffmpeg_opts = getenv("SWAYGRAB_FFMPEG_OPTS");
104 if(!ffmpeg_opts) {
105 ffmpeg_opts = "";
106 }
107
108 const char *fmt = "ffmpeg %s -f rawvideo -framerate %d "
109 "-video_size %dx%d -pixel_format argb "
110 "-i pipe:0 -r %d -vf vflip %s";
111 char *cmd = malloc(strlen(fmt) - 8 /*args*/
112 + strlen(ffmpeg_opts) + numlen(width) + numlen(height)
113 + numlen(framerate) * 2 + strlen(file) + 1);
114 sprintf(cmd, fmt, ffmpeg_opts, framerate, width, height, framerate, file);
115
116 long ns = (long)(1000000000 * (1.0 / framerate));
117 struct timespec start, finish, ts;
118 ts.tv_sec = 0;
119
120 FILE *f = popen(cmd, "w");
121 fwrite(pixels, 1, len, f);
122 free(pixels - 9);
123 int sleep = 0;
124 while (sleep != -1) {
125 clock_gettime(CLOCK_MONOTONIC, &start);
126 len = strlen(payload);
127 pixels = ipc_single_command(socketfd,
128 IPC_SWAY_GET_PIXELS, payload, &len);
129 pixels += 9;
130 len -= 9;
131
132 fwrite(pixels, 1, len, f);
133
134 free(pixels - 9);
135 clock_gettime(CLOCK_MONOTONIC, &finish);
136 ts.tv_nsec = ns;
137 double fts = (double)finish.tv_sec + 1.0e-9*finish.tv_nsec;
138 double sts = (double)start.tv_sec + 1.0e-9*start.tv_nsec;
139 long diff = (fts - sts) * 1000000000;
140 ts.tv_nsec = ns - diff;
141 if (ts.tv_nsec < 0) {
142 ts.tv_nsec = 0;
143 }
144 sleep = nanosleep(&ts, NULL);
145 }
146 fflush(f);
147
148 fclose(f);
149 free(cmd);
150}
151
152char *default_filename(const char *extension) {
153 int ext_len = strlen(extension);
154 int len = 28 + ext_len; // format: "2015-12-17-180040_swaygrab.ext"
155 char *filename = malloc(len * sizeof(char));
156 time_t t = time(NULL);
157
158 struct tm *lt = localtime(&t);
159 strftime(filename, len, "%Y-%m-%d-%H%M%S_swaygrab.", lt);
160 strncat(filename, extension, ext_len);
161
162 return filename;
163}
164
165int main(int argc, char **argv) {
166 static int capture = 0, raw = 0;
167 char *socket_path = NULL;
168 char *output = NULL;
169 int framerate = 30;
170 bool grab_focused = false;
171
172 init_log(L_INFO);
173
174 static struct option long_options[] = {
175 {"help", no_argument, NULL, 'h'},
176 {"capture", no_argument, NULL, 'c'},
177 {"output", required_argument, NULL, 'o'},
178 {"version", no_argument, NULL, 'v'},
179 {"socket", required_argument, NULL, 's'},
180 {"raw", no_argument, NULL, 'r'},
181 {"rate", required_argument, NULL, 'R'},
182 {"focused", no_argument, NULL, 'f'},
183 {0, 0, 0, 0}
184 };
185
186 const char *usage =
187 "Usage: swaygrab [options] [file]\n"
188 "\n"
189 " -h, --help Show help message and quit.\n"
190 " -c, --capture Capture video.\n"
191 " -o, --output <output> Output source.\n"
192 " -v, --version Show the version number and quit.\n"
193 " -s, --socket <socket> Use the specified socket.\n"
194 " -R, --rate <rate> Specify framerate (default: 30)\n"
195 " -r, --raw Write raw rgba data to stdout.\n"
196 " -f, --focused Grab the focused container.\n";
197
198 int c;
199 while (1) {
200 int option_index = 0;
201 c = getopt_long(argc, argv, "hco:vs:R:rf", long_options, &option_index);
202 if (c == -1) {
203 break;
204 }
205 switch (c) {
206 case 'f':
207 grab_focused = true;
208 break;
209 case 's': // Socket
210 socket_path = strdup(optarg);
211 break;
212 case 'r':
213 raw = 1;
214 break;
215 case 'o': // output
216 output = strdup(optarg);
217 break;
218 case 'c':
219 capture = 1;
220 break;
221 case 'R': // Frame rate
222 framerate = atoi(optarg);
223 break;
224 case 'v':
225 fprintf(stdout, "sway version " SWAY_VERSION "\n");
226 exit(EXIT_SUCCESS);
227 break;
228 default:
229 fprintf(stderr, "%s", usage);
230 exit(EXIT_FAILURE);
231 }
232 }
233
234 if (!socket_path) {
235 socket_path = get_socketpath();
236 if (!socket_path) {
237 sway_abort("Unable to retrieve socket path");
238 }
239 }
240
241 char *file = NULL;
242 if (raw) {
243 if (optind >= argc + 1) {
244 sway_abort("Invalid usage. See `man swaygrab` %d %d", argc, optind);
245 }
246 } else if (optind < argc) {
247 file = strdup(argv[optind]);
248 }
249
250 int socketfd = ipc_open_socket(socket_path);
251 free(socket_path);
252
253 init_json_tree(socketfd);
254
255 struct wlc_geometry *geo;
256
257 if (grab_focused) {
258 output = get_focused_output();
259 json_object *con = get_focused_container();
260 json_object *name;
261 json_object_object_get_ex(con, "name", &name);
262 geo = get_container_geometry(con);
263 free(con);
264 } else {
265 if (!output) {
266 output = get_focused_output();
267 }
268 geo = get_container_geometry(get_output_container(output));
269 // the geometry of the output in the get_tree response is relative to a global (0, 0).
270 // we need it to be relative to itself, so set origin to (0, 0) always.
271 geo->origin.x = 0;
272 geo->origin.y = 0;
273 }
274
275 const char *payload = create_payload(output, geo);
276
277 free(geo);
278
279 if (!file) {
280 if (!capture) {
281 file = default_filename("png");
282 } else {
283 file = default_filename("webm");
284 }
285 }
286
287 if (!capture) {
288 grab_and_apply_magick(file, payload, socketfd, raw);
289 } else {
290 grab_and_apply_movie_magic(file, payload, socketfd, raw, framerate);
291 }
292
293 free_json_tree();
294 free(output);
295 free(file);
296 close(socketfd);
297 return 0;
298}