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.
This commit is contained in:
Mikkel Oscar Lyderik 2015-12-17 16:01:36 +01:00
parent 101a14faf8
commit d49282b2c3
2 changed files with 47 additions and 8 deletions

View file

@ -1,9 +1,14 @@
include_directories(
${JSONC_INCLUDE_DIRS}
)
add_executable(swaygrab
main.c
)
target_link_libraries(swaygrab
sway-common
${JSONC_LIBRARIES}
rt
)

View file

@ -6,6 +6,7 @@
#include <stdint.h>
#include <math.h>
#include <time.h>
#include <json-c/json.h>
#include "log.h"
#include "ipc-client.h"
#include "util.h"
@ -110,9 +111,34 @@ void grab_and_apply_movie_magic(const char *file, const char *output,
free(cmd);
}
char *get_focused_output(int socketfd) {
uint32_t len = 0;
char *res = ipc_single_command(socketfd, IPC_GET_WORKSPACES, NULL, &len);
json_object *workspaces = json_tokener_parse(res);
int length = json_object_array_length(workspaces);
json_object *workspace, *focused, *json_output;
char *output = NULL;
int i;
for (i = 0; i < length; ++i) {
workspace = json_object_array_get_idx(workspaces, i);
json_object_object_get_ex(workspace, "focused", &focused);
if (json_object_get_boolean(focused) == TRUE) {
json_object_object_get_ex(workspace, "output", &json_output);
output = strdup(json_object_get_string(json_output));
break;
}
}
json_object_put(workspaces);
free(res);
return output;
}
int main(int argc, char **argv) {
static int capture = 0, raw = 0;
char *socket_path = NULL;
char *output = NULL;
int framerate = 30;
init_log(L_INFO);
@ -120,6 +146,7 @@ int main(int argc, char **argv) {
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"capture", no_argument, NULL, 'c'},
{"output", required_argument, NULL, 'o'},
{"version", no_argument, NULL, 'v'},
{"socket", required_argument, NULL, 's'},
{"raw", no_argument, NULL, 'r'},
@ -128,10 +155,11 @@ int main(int argc, char **argv) {
};
const char *usage =
"Usage: swaygrab [options] <output> [file]\n"
"Usage: swaygrab [options] [file]\n"
"\n"
" -h, --help Show help message and quit.\n"
" -c, --capture Capture video.\n"
" -o, --output <output> Output source.\n"
" -v, --version Show the version number and quit.\n"
" -s, --socket <socket> Use the specified socket.\n"
" -R, --rate <rate> Specify framerate (default: 30)\n"
@ -140,7 +168,7 @@ int main(int argc, char **argv) {
int c;
while (1) {
int option_index = 0;
c = getopt_long(argc, argv, "hcvs:r", long_options, &option_index);
c = getopt_long(argc, argv, "hco:vs:r", long_options, &option_index);
if (c == -1) {
break;
}
@ -151,6 +179,9 @@ int main(int argc, char **argv) {
case 'r':
raw = 1;
break;
case 'o': // output
output = strdup(optarg);
break;
case 'c':
capture = 1;
break;
@ -178,29 +209,32 @@ int main(int argc, char **argv) {
}
}
char *file = NULL, *output = NULL;
char *file = NULL;
if (raw) {
if (optind >= argc) {
if (optind >= argc + 1) {
sway_abort("Invalid usage. See `man swaygrab` %d %d", argc, optind);
}
output = argv[optind];
} else {
if (optind >= argc - 1) {
if (optind >= argc) {
sway_abort("Invalid usage. See `man swaygrab`");
}
file = argv[optind + 1];
output = argv[optind];
file = argv[optind];
}
int socketfd = ipc_open_socket(socket_path);
free(socket_path);
if (!output) {
output = get_focused_output(socketfd);
}
if (!capture) {
grab_and_apply_magick(file, output, socketfd, raw);
} else {
grab_and_apply_movie_magic(file, output, socketfd, raw, framerate);
}
free(output);
close(socketfd);
return 0;
}