diff options
author | 2018-05-12 09:27:24 -0400 | |
---|---|---|
committer | 2018-05-12 09:27:24 -0400 | |
commit | 867190e0828a139fa4529cde60d033a9389df42c (patch) | |
tree | c986cca12ecdb3e3ac4d94d38205028788315889 /swaygrab | |
parent | Merge pull request #1959 from RyanDwyer/ipc-get-seats (diff) | |
parent | Merge remote-tracking branch 'origin/master' into scdoc (diff) | |
download | sway-867190e0828a139fa4529cde60d033a9389df42c.tar.gz sway-867190e0828a139fa4529cde60d033a9389df42c.tar.zst sway-867190e0828a139fa4529cde60d033a9389df42c.zip |
Merge pull request #1958 from swaywm/scdoc
Port man pages to scdoc
Diffstat (limited to 'swaygrab')
-rw-r--r-- | swaygrab/json.c | 144 | ||||
-rw-r--r-- | swaygrab/main.c | 298 | ||||
-rw-r--r-- | swaygrab/swaygrab.1.txt | 76 |
3 files changed, 0 insertions, 518 deletions
diff --git a/swaygrab/json.c b/swaygrab/json.c deleted file mode 100644 index 286085c3..00000000 --- a/swaygrab/json.c +++ /dev/null | |||
@@ -1,144 +0,0 @@ | |||
1 | #define _XOPEN_SOURCE 700 | ||
2 | #include <string.h> | ||
3 | #include <stdio.h> | ||
4 | #include <stdbool.h> | ||
5 | #include <stdlib.h> | ||
6 | #include <unistd.h> | ||
7 | #include "log.h" | ||
8 | #include "ipc-client.h" | ||
9 | #include "swaygrab/json.h" | ||
10 | |||
11 | static json_object *tree; | ||
12 | |||
13 | void init_json_tree(int socketfd) { | ||
14 | uint32_t len = 0; | ||
15 | char *res = ipc_single_command(socketfd, IPC_GET_TREE, NULL, &len); | ||
16 | struct json_tokener *tok = json_tokener_new_ex(256); | ||
17 | if (!tok) { | ||
18 | sway_abort("Unable to get json tokener."); | ||
19 | } | ||
20 | tree = json_tokener_parse_ex(tok, res, len); | ||
21 | if (!tree || tok->err != json_tokener_success) { | ||
22 | sway_abort("Unable to parse IPC response as JSON: %s", json_tokener_error_desc(tok->err)); | ||
23 | } | ||
24 | json_object *success; | ||
25 | json_object_object_get_ex(tree, "success", &success); | ||
26 | if (success && !json_object_get_boolean(success)) { | ||
27 | json_object *error; | ||
28 | json_object_object_get_ex(tree, "error", &error); | ||
29 | sway_abort("IPC request failed: %s", json_object_get_string(error)); | ||
30 | } | ||
31 | json_object_put(success); | ||
32 | json_tokener_free(tok); | ||
33 | } | ||
34 | |||
35 | void free_json_tree() { | ||
36 | json_object_put(tree); | ||
37 | } | ||
38 | |||
39 | static bool is_focused(json_object *c) { | ||
40 | json_object *focused; | ||
41 | json_object_object_get_ex(c, "focused", &focused); | ||
42 | return json_object_get_boolean(focused); | ||
43 | } | ||
44 | |||
45 | static json_object *get_focused_container_r(json_object *c) { | ||
46 | json_object *name; | ||
47 | json_object_object_get_ex(c, "name", &name); | ||
48 | if (is_focused(c)) { | ||
49 | return c; | ||
50 | } else { | ||
51 | json_object *nodes, *node, *child; | ||
52 | json_object_object_get_ex(c, "nodes", &nodes); | ||
53 | int i; | ||
54 | for (i = 0; i < json_object_array_length(nodes); i++) { | ||
55 | node = json_object_array_get_idx(nodes, i); | ||
56 | |||
57 | if ((child = get_focused_container_r(node))) { | ||
58 | return child; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | json_object_object_get_ex(c, "floating_nodes", &nodes); | ||
63 | for (i = 0; i < json_object_array_length(nodes); i++) { | ||
64 | node = json_object_array_get_idx(nodes, i); | ||
65 | |||
66 | if ((child = get_focused_container_r(node))) { | ||
67 | return child; | ||
68 | } | ||
69 | } | ||
70 | |||
71 | } | ||
72 | |||
73 | return NULL; | ||
74 | } | ||
75 | |||
76 | json_object *get_focused_container() { | ||
77 | return get_focused_container_r(tree); | ||
78 | } | ||
79 | |||
80 | char *get_focused_output() { | ||
81 | json_object *outputs, *output, *name; | ||
82 | json_object_object_get_ex(tree, "nodes", &outputs); | ||
83 | if (!outputs) { | ||
84 | sway_abort("Unabled to get focused output. No nodes in tree."); | ||
85 | } | ||
86 | for (int i = 0; i < json_object_array_length(outputs); i++) { | ||
87 | output = json_object_array_get_idx(outputs, i); | ||
88 | |||
89 | if (get_focused_container_r(output)) { | ||
90 | json_object_object_get_ex(output, "name", &name); | ||
91 | return strdup(json_object_get_string(name)); | ||
92 | } | ||
93 | } | ||
94 | |||
95 | return NULL; | ||
96 | } | ||
97 | |||
98 | char *create_payload(const char *output, struct wlc_geometry *g) { | ||
99 | char *payload_str = malloc(256); | ||
100 | json_object *payload = json_object_new_object(); | ||
101 | |||
102 | json_object_object_add(payload, "output", json_object_new_string(output)); | ||
103 | json_object_object_add(payload, "x", json_object_new_int(g->origin.x)); | ||
104 | json_object_object_add(payload, "y", json_object_new_int(g->origin.y)); | ||
105 | json_object_object_add(payload, "w", json_object_new_int(g->size.w)); | ||
106 | json_object_object_add(payload, "h", json_object_new_int(g->size.h)); | ||
107 | |||
108 | snprintf(payload_str, 256, "%s", json_object_to_json_string(payload)); | ||
109 | return strdup(payload_str); | ||
110 | } | ||
111 | |||
112 | struct wlc_geometry *get_container_geometry(json_object *container) { | ||
113 | struct wlc_geometry *geo = malloc(sizeof(struct wlc_geometry)); | ||
114 | json_object *rect, *x, *y, *w, *h; | ||
115 | |||
116 | json_object_object_get_ex(container, "rect", &rect); | ||
117 | json_object_object_get_ex(rect, "x", &x); | ||
118 | json_object_object_get_ex(rect, "y", &y); | ||
119 | json_object_object_get_ex(rect, "width", &w); | ||
120 | json_object_object_get_ex(rect, "height", &h); | ||
121 | |||
122 | geo->origin.x = json_object_get_int(x); | ||
123 | geo->origin.y = json_object_get_int(y); | ||
124 | geo->size.w = json_object_get_int(w); | ||
125 | geo->size.h = json_object_get_int(h); | ||
126 | |||
127 | return geo; | ||
128 | } | ||
129 | |||
130 | json_object *get_output_container(const char *output) { | ||
131 | json_object *outputs, *json_output, *name; | ||
132 | json_object_object_get_ex(tree, "nodes", &outputs); | ||
133 | |||
134 | for (int i = 0; i < json_object_array_length(outputs); i++) { | ||
135 | json_output = json_object_array_get_idx(outputs, i); | ||
136 | json_object_object_get_ex(json_output, "name", &name); | ||
137 | |||
138 | if (strcmp(json_object_get_string(name), output) == 0) { | ||
139 | return json_output; | ||
140 | } | ||
141 | } | ||
142 | |||
143 | return NULL; | ||
144 | } | ||
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 | |||
19 | void sway_terminate(int exit_code) { | ||
20 | exit(exit_code); | ||
21 | } | ||
22 | |||
23 | void 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 | |||
79 | void 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 | |||
152 | char *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 | |||
165 | int 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 | } | ||
diff --git a/swaygrab/swaygrab.1.txt b/swaygrab/swaygrab.1.txt deleted file mode 100644 index 8faf43f5..00000000 --- a/swaygrab/swaygrab.1.txt +++ /dev/null | |||
@@ -1,76 +0,0 @@ | |||
1 | ///// | ||
2 | vim:set ts=4 sw=4 tw=82 noet: | ||
3 | ///// | ||
4 | :quotes.~: | ||
5 | |||
6 | swaygrab (1) | ||
7 | ============ | ||
8 | |||
9 | Name | ||
10 | ---- | ||
11 | swaygrab - Grab image data from the current sway session. | ||
12 | |||
13 | Synopsis | ||
14 | -------- | ||
15 | 'swaygrab' [options] [file] | ||
16 | |||
17 | Grabs pixels from an output and writes them to _file_. The image will be passed to | ||
18 | ImageMagick convert for processing. | ||
19 | |||
20 | Options | ||
21 | ------- | ||
22 | |||
23 | *-h, --help*:: | ||
24 | Show help message and quit. | ||
25 | |||
26 | *-c, \--capture*:: | ||
27 | Captures multiple frames as video and passes them into ffmpeg. Continues until | ||
28 | you send SIGTERM (ctrl+c) to swaygrab. | ||
29 | |||
30 | *-o, \--output* <output>:: | ||
31 | Use the specified _output_. If no output is defined the currently focused | ||
32 | output in sway will be used. | ||
33 | |||
34 | *-v, \--version*:: | ||
35 | Print the version (of swaymsg) and quit. | ||
36 | |||
37 | *-s, --socket* <path>:: | ||
38 | Use the specified socket path. Otherwise, swaymsg will ask sway where the | ||
39 | socket is (which is the value of $SWAYSOCK, then of $I3SOCK). | ||
40 | |||
41 | *-R, --rate* <rate>:: | ||
42 | Specify a framerate (in frames per second). Used in combination with -c. | ||
43 | Default is 30. Must be an integer. | ||
44 | |||
45 | *-r, --raw*:: | ||
46 | Instead of invoking ImageMagick or ffmpeg, dump raw rgba data to stdout. | ||
47 | |||
48 | *-f, --focused*:: | ||
49 | Capture only the currently focused container. | ||
50 | |||
51 | Environment Variables | ||
52 | --------------------- | ||
53 | swaygrab reads the following environment variables. | ||
54 | |||
55 | *SWAYGRAB_FFMPEG_OPTS*:: | ||
56 | Pass additional arguments to FFmpeg when starting a capture. | ||
57 | |||
58 | Examples | ||
59 | -------- | ||
60 | |||
61 | swaygrab output.png:: | ||
62 | Grab the contents of currently focused output and write to output.png. | ||
63 | |||
64 | swaygrab -c -o HDMI-A-1 output.webm:: | ||
65 | Capture a webm of HDMI-A-1. | ||
66 | |||
67 | SWAYGRAB_FFMPEG_OPTS="-f alsa -i pulse" swaygrab -c:: | ||
68 | Capture the focused output and encode audio from the default recording | ||
69 | device. | ||
70 | |||
71 | Authors | ||
72 | ------- | ||
73 | |||
74 | Maintained by Drew DeVault <sir@cmpwn.com>, who is assisted by other open | ||
75 | source contributors. For more information about sway development, see | ||
76 | <https://github.com/swaywm/sway>. | ||