aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2015-11-27 10:39:18 -0500
committerLibravatar Drew DeVault <sir@cmpwn.com>2015-11-27 10:39:18 -0500
commitf05b6cd55c63876cf0e5ee7f4f106a26a46f8b66 (patch)
tree8989b48bbabf43a78173234802acf7ab8ce0f61e
parentAdd command line to swaygrab (diff)
downloadsway-f05b6cd55c63876cf0e5ee7f4f106a26a46f8b66.tar.gz
sway-f05b6cd55c63876cf0e5ee7f4f106a26a46f8b66.tar.zst
sway-f05b6cd55c63876cf0e5ee7f4f106a26a46f8b66.zip
Implement swaygrab for still images
-rw-r--r--sway.1.txt2
-rw-r--r--swaygrab.1.txt9
-rw-r--r--swaygrab/main.c56
3 files changed, 60 insertions, 7 deletions
diff --git a/sway.1.txt b/sway.1.txt
index f8d01ec3..c0715841 100644
--- a/sway.1.txt
+++ b/sway.1.txt
@@ -81,4 +81,4 @@ source contributors. For more information about sway development, see
81See Also 81See Also
82-------- 82--------
83 83
84**sway**(5) 84**sway**(5) **swaymsg**(1) **swaygrab**(1)
diff --git a/swaygrab.1.txt b/swaygrab.1.txt
index 68bea3e8..54a1c37a 100644
--- a/swaygrab.1.txt
+++ b/swaygrab.1.txt
@@ -31,6 +31,15 @@ Options
31 Use the specified socket path. Otherwise, swaymsg will ask sway where the 31 Use the specified socket path. Otherwise, swaymsg will ask sway where the
32 socket is (which is the value of $SWAYSOCK, then of $I3SOCK). 32 socket is (which is the value of $SWAYSOCK, then of $I3SOCK).
33 33
34Examples
35--------
36
37swaygrab HDMI-A-1 output.png::
38 Grab the contents of HDMI-A-1 and write to output.png.
39
40swaygrab -c HDMI-A-1 output.webm::
41 Capture a webm of HDMI-A-1.
42
34Authors 43Authors
35------- 44-------
36 45
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
10int main(int argc, const char **argv) { 14int 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
24void 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
48int 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}