aboutsummaryrefslogtreecommitdiffstats
path: root/swaygrab
diff options
context:
space:
mode:
authorLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2015-12-17 16:01:36 +0100
committerLibravatar Mikkel Oscar Lyderik <mikkeloscar@gmail.com>2015-12-17 16:09:03 +0100
commitd49282b2c37f0e3647d5300e97732e61602a3094 (patch)
treef9c2b50489957a8df63bfbf7dd65a4f5ad343738 /swaygrab
parent[swaylock] Stupid implementation of password entry (diff)
downloadsway-d49282b2c37f0e3647d5300e97732e61602a3094.tar.gz
sway-d49282b2c37f0e3647d5300e97732e61602a3094.tar.zst
sway-d49282b2c37f0e3647d5300e97732e61602a3094.zip
swaygrab: make focused output default
This makes swaygrab use the currently focused output as source if no other output is defined with the `-o, --output <output>` option.
Diffstat (limited to 'swaygrab')
-rw-r--r--swaygrab/CMakeLists.txt5
-rw-r--r--swaygrab/main.c50
2 files changed, 47 insertions, 8 deletions
diff --git a/swaygrab/CMakeLists.txt b/swaygrab/CMakeLists.txt
index f664c01d..9035ac8b 100644
--- a/swaygrab/CMakeLists.txt
+++ b/swaygrab/CMakeLists.txt
@@ -1,9 +1,14 @@
1include_directories(
2 ${JSONC_INCLUDE_DIRS}
3)
4
1add_executable(swaygrab 5add_executable(swaygrab
2 main.c 6 main.c
3) 7)
4 8
5target_link_libraries(swaygrab 9target_link_libraries(swaygrab
6 sway-common 10 sway-common
11 ${JSONC_LIBRARIES}
7 rt 12 rt
8) 13)
9 14
diff --git a/swaygrab/main.c b/swaygrab/main.c
index 2c6cf2dd..671b8737 100644
--- a/swaygrab/main.c
+++ b/swaygrab/main.c
@@ -6,6 +6,7 @@
6#include <stdint.h> 6#include <stdint.h>
7#include <math.h> 7#include <math.h>
8#include <time.h> 8#include <time.h>
9#include <json-c/json.h>
9#include "log.h" 10#include "log.h"
10#include "ipc-client.h" 11#include "ipc-client.h"
11#include "util.h" 12#include "util.h"
@@ -110,9 +111,34 @@ void grab_and_apply_movie_magic(const char *file, const char *output,
110 free(cmd); 111 free(cmd);
111} 112}
112 113
114char *get_focused_output(int socketfd) {
115 uint32_t len = 0;
116 char *res = ipc_single_command(socketfd, IPC_GET_WORKSPACES, NULL, &len);
117 json_object *workspaces = json_tokener_parse(res);
118
119 int length = json_object_array_length(workspaces);
120 json_object *workspace, *focused, *json_output;
121 char *output = NULL;
122 int i;
123 for (i = 0; i < length; ++i) {
124 workspace = json_object_array_get_idx(workspaces, i);
125 json_object_object_get_ex(workspace, "focused", &focused);
126 if (json_object_get_boolean(focused) == TRUE) {
127 json_object_object_get_ex(workspace, "output", &json_output);
128 output = strdup(json_object_get_string(json_output));
129 break;
130 }
131 }
132
133 json_object_put(workspaces);
134 free(res);
135 return output;
136}
137
113int main(int argc, char **argv) { 138int main(int argc, char **argv) {
114 static int capture = 0, raw = 0; 139 static int capture = 0, raw = 0;
115 char *socket_path = NULL; 140 char *socket_path = NULL;
141 char *output = NULL;
116 int framerate = 30; 142 int framerate = 30;
117 143
118 init_log(L_INFO); 144 init_log(L_INFO);
@@ -120,6 +146,7 @@ int main(int argc, char **argv) {
120 static struct option long_options[] = { 146 static struct option long_options[] = {
121 {"help", no_argument, NULL, 'h'}, 147 {"help", no_argument, NULL, 'h'},
122 {"capture", no_argument, NULL, 'c'}, 148 {"capture", no_argument, NULL, 'c'},
149 {"output", required_argument, NULL, 'o'},
123 {"version", no_argument, NULL, 'v'}, 150 {"version", no_argument, NULL, 'v'},
124 {"socket", required_argument, NULL, 's'}, 151 {"socket", required_argument, NULL, 's'},
125 {"raw", no_argument, NULL, 'r'}, 152 {"raw", no_argument, NULL, 'r'},
@@ -128,10 +155,11 @@ int main(int argc, char **argv) {
128 }; 155 };
129 156
130 const char *usage = 157 const char *usage =
131 "Usage: swaygrab [options] <output> [file]\n" 158 "Usage: swaygrab [options] [file]\n"
132 "\n" 159 "\n"
133 " -h, --help Show help message and quit.\n" 160 " -h, --help Show help message and quit.\n"
134 " -c, --capture Capture video.\n" 161 " -c, --capture Capture video.\n"
162 " -o, --output <output> Output source.\n"
135 " -v, --version Show the version number and quit.\n" 163 " -v, --version Show the version number and quit.\n"
136 " -s, --socket <socket> Use the specified socket.\n" 164 " -s, --socket <socket> Use the specified socket.\n"
137 " -R, --rate <rate> Specify framerate (default: 30)\n" 165 " -R, --rate <rate> Specify framerate (default: 30)\n"
@@ -140,7 +168,7 @@ int main(int argc, char **argv) {
140 int c; 168 int c;
141 while (1) { 169 while (1) {
142 int option_index = 0; 170 int option_index = 0;
143 c = getopt_long(argc, argv, "hcvs:r", long_options, &option_index); 171 c = getopt_long(argc, argv, "hco:vs:r", long_options, &option_index);
144 if (c == -1) { 172 if (c == -1) {
145 break; 173 break;
146 } 174 }
@@ -151,6 +179,9 @@ int main(int argc, char **argv) {
151 case 'r': 179 case 'r':
152 raw = 1; 180 raw = 1;
153 break; 181 break;
182 case 'o': // output
183 output = strdup(optarg);
184 break;
154 case 'c': 185 case 'c':
155 capture = 1; 186 capture = 1;
156 break; 187 break;
@@ -178,29 +209,32 @@ int main(int argc, char **argv) {
178 } 209 }
179 } 210 }
180 211
181 char *file = NULL, *output = NULL; 212 char *file = NULL;
182 if (raw) { 213 if (raw) {
183 if (optind >= argc) { 214 if (optind >= argc + 1) {
184 sway_abort("Invalid usage. See `man swaygrab` %d %d", argc, optind); 215 sway_abort("Invalid usage. See `man swaygrab` %d %d", argc, optind);
185 } 216 }
186 output = argv[optind];
187 } else { 217 } else {
188 if (optind >= argc - 1) { 218 if (optind >= argc) {
189 sway_abort("Invalid usage. See `man swaygrab`"); 219 sway_abort("Invalid usage. See `man swaygrab`");
190 } 220 }
191 file = argv[optind + 1]; 221 file = argv[optind];
192 output = argv[optind];
193 } 222 }
194 223
195 int socketfd = ipc_open_socket(socket_path); 224 int socketfd = ipc_open_socket(socket_path);
196 free(socket_path); 225 free(socket_path);
197 226
227 if (!output) {
228 output = get_focused_output(socketfd);
229 }
230
198 if (!capture) { 231 if (!capture) {
199 grab_and_apply_magick(file, output, socketfd, raw); 232 grab_and_apply_magick(file, output, socketfd, raw);
200 } else { 233 } else {
201 grab_and_apply_movie_magic(file, output, socketfd, raw, framerate); 234 grab_and_apply_movie_magic(file, output, socketfd, raw, framerate);
202 } 235 }
203 236
237 free(output);
204 close(socketfd); 238 close(socketfd);
205 return 0; 239 return 0;
206} 240}