diff options
author | Drew DeVault <sir@cmpwn.com> | 2015-11-27 10:39:18 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2015-11-27 10:39:18 -0500 |
commit | f05b6cd55c63876cf0e5ee7f4f106a26a46f8b66 (patch) | |
tree | 8989b48bbabf43a78173234802acf7ab8ce0f61e /swaygrab | |
parent | Add command line to swaygrab (diff) | |
download | sway-f05b6cd55c63876cf0e5ee7f4f106a26a46f8b66.tar.gz sway-f05b6cd55c63876cf0e5ee7f4f106a26a46f8b66.tar.zst sway-f05b6cd55c63876cf0e5ee7f4f106a26a46f8b66.zip |
Implement swaygrab for still images
Diffstat (limited to 'swaygrab')
-rw-r--r-- | swaygrab/main.c | 56 |
1 files changed, 50 insertions, 6 deletions
diff --git a/swaygrab/main.c b/swaygrab/main.c index bd64bd93..1edda490 100644 --- a/swaygrab/main.c +++ b/swaygrab/main.c | |||
@@ -1,5 +1,9 @@ | |||
1 | #include <stdio.h> | 1 | #include <stdio.h> |
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | #include <string.h> | ||
4 | #include <getopt.h> | ||
5 | #include <unistd.h> | ||
6 | #include <math.h> | ||
3 | #include "log.h" | 7 | #include "log.h" |
4 | #include "ipc-client.h" | 8 | #include "ipc-client.h" |
5 | 9 | ||
@@ -7,8 +11,42 @@ void sway_terminate(void) { | |||
7 | exit(1); | 11 | exit(1); |
8 | } | 12 | } |
9 | 13 | ||
10 | int main(int argc, const char **argv) { | 14 | int numlen(int n) { |
11 | int capture; | 15 | if (n >= 1000000) return 7; |
16 | if (n >= 100000) return 6; | ||
17 | if (n >= 10000) return 5; | ||
18 | if (n >= 1000) return 4; | ||
19 | if (n >= 100) return 3; | ||
20 | if (n >= 10) return 2; | ||
21 | return 1; | ||
22 | } | ||
23 | |||
24 | void grab_and_apply_magick(const char *file, const char *output, int socketfd) { | ||
25 | uint32_t len = strlen(output); | ||
26 | char *pixels = ipc_single_command(socketfd, | ||
27 | IPC_SWAY_GET_PIXELS, output, &len); | ||
28 | uint32_t *u32pixels = (uint32_t *)(pixels + 1); | ||
29 | uint32_t width = u32pixels[0]; | ||
30 | uint32_t height = u32pixels[1]; | ||
31 | pixels += 9; | ||
32 | |||
33 | if (width == 0 || height == 0) { | ||
34 | sway_abort("Unknown output %s.", output); | ||
35 | } | ||
36 | |||
37 | const char *fmt = "convert -depth 8 -size %dx%d+0 rgba:- -flip %s"; | ||
38 | char *cmd = malloc(strlen(fmt) - 6 /*args*/ | ||
39 | + numlen(width) + numlen(height) + strlen(file) + 1); | ||
40 | sprintf(cmd, fmt, width, height, file); | ||
41 | |||
42 | FILE *f = popen(cmd, "w"); | ||
43 | fwrite(pixels, 1, len, f); | ||
44 | fflush(f); | ||
45 | fclose(f); | ||
46 | } | ||
47 | |||
48 | int main(int argc, char **argv) { | ||
49 | static int capture = 0; | ||
12 | char *socket_path = NULL; | 50 | char *socket_path = NULL; |
13 | 51 | ||
14 | init_log(L_INFO); | 52 | init_log(L_INFO); |
@@ -51,15 +89,21 @@ int main(int argc, const char **argv) { | |||
51 | } | 89 | } |
52 | } | 90 | } |
53 | 91 | ||
54 | if (optind >= argc) { | 92 | if (optind >= argc - 1) { |
55 | sway_abort("Expected output file on command line. See `man swaygrab`"); | 93 | sway_abort("Expected output and file on command line. See `man swaygrab`"); |
56 | } | 94 | } |
57 | 95 | ||
58 | char *out = argv[optind]; | 96 | char *file = argv[optind + 1]; |
97 | char *output = argv[optind]; | ||
59 | int socketfd = ipc_open_socket(socket_path); | 98 | int socketfd = ipc_open_socket(socket_path); |
60 | free(socket_path); | 99 | free(socket_path); |
61 | 100 | ||
101 | if (!capture) { | ||
102 | grab_and_apply_magick(file, output, socketfd); | ||
103 | } else { | ||
104 | sway_abort("Capture is not yet supported"); | ||
105 | } | ||
106 | |||
62 | close(socketfd); | 107 | close(socketfd); |
63 | free(out); | ||
64 | return 0; | 108 | return 0; |
65 | } | 109 | } |