diff options
Diffstat (limited to 'swaymsg/main.c')
-rw-r--r-- | swaymsg/main.c | 82 |
1 files changed, 66 insertions, 16 deletions
diff --git a/swaymsg/main.c b/swaymsg/main.c index 243b5fdc..e5eee631 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c | |||
@@ -1,4 +1,4 @@ | |||
1 | #define _XOPEN_SOURCE 500 | 1 | #define _POSIX_C_SOURCE 200809L |
2 | #include <stdio.h> | 2 | #include <stdio.h> |
3 | #include <stdlib.h> | 3 | #include <stdlib.h> |
4 | #include <string.h> | 4 | #include <string.h> |
@@ -12,7 +12,6 @@ | |||
12 | #include <json-c/json.h> | 12 | #include <json-c/json.h> |
13 | #include "stringop.h" | 13 | #include "stringop.h" |
14 | #include "ipc-client.h" | 14 | #include "ipc-client.h" |
15 | #include "readline.h" | ||
16 | #include "log.h" | 15 | #include "log.h" |
17 | 16 | ||
18 | void sway_terminate(int exit_code) { | 17 | void sway_terminate(int exit_code) { |
@@ -32,6 +31,9 @@ static bool success_object(json_object *result) { | |||
32 | // Iterate results array and return false if any of them failed | 31 | // Iterate results array and return false if any of them failed |
33 | static bool success(json_object *r, bool fallback) { | 32 | static bool success(json_object *r, bool fallback) { |
34 | if (!json_object_is_type(r, json_type_array)) { | 33 | if (!json_object_is_type(r, json_type_array)) { |
34 | if (json_object_is_type(r, json_type_object)) { | ||
35 | return success_object(r); | ||
36 | } | ||
35 | return fallback; | 37 | return fallback; |
36 | } | 38 | } |
37 | 39 | ||
@@ -111,7 +113,7 @@ static const char *pretty_type_name(const char *name) { | |||
111 | } | 113 | } |
112 | 114 | ||
113 | static void pretty_print_input(json_object *i) { | 115 | static void pretty_print_input(json_object *i) { |
114 | json_object *id, *name, *type, *product, *vendor, *kbdlayout; | 116 | json_object *id, *name, *type, *product, *vendor, *kbdlayout, *events; |
115 | json_object_object_get_ex(i, "identifier", &id); | 117 | json_object_object_get_ex(i, "identifier", &id); |
116 | json_object_object_get_ex(i, "name", &name); | 118 | json_object_object_get_ex(i, "name", &name); |
117 | json_object_object_get_ex(i, "type", &type); | 119 | json_object_object_get_ex(i, "type", &type); |
@@ -137,6 +139,10 @@ static void pretty_print_input(json_object *i) { | |||
137 | json_object_get_string(kbdlayout)); | 139 | json_object_get_string(kbdlayout)); |
138 | } | 140 | } |
139 | 141 | ||
142 | if (json_object_object_get_ex(i, "libinput_send_events", &events)) { | ||
143 | printf(" Libinput Send Events: %s\n", json_object_get_string(events)); | ||
144 | } | ||
145 | |||
140 | printf("\n"); | 146 | printf("\n"); |
141 | } | 147 | } |
142 | 148 | ||
@@ -305,8 +311,9 @@ static void pretty_print(int type, json_object *resp) { | |||
305 | } | 311 | } |
306 | 312 | ||
307 | int main(int argc, char **argv) { | 313 | int main(int argc, char **argv) { |
308 | static int quiet = 0; | 314 | static bool quiet = false; |
309 | static int raw = 0; | 315 | static bool raw = false; |
316 | static bool monitor = false; | ||
310 | char *socket_path = NULL; | 317 | char *socket_path = NULL; |
311 | char *cmdtype = NULL; | 318 | char *cmdtype = NULL; |
312 | 319 | ||
@@ -314,6 +321,7 @@ int main(int argc, char **argv) { | |||
314 | 321 | ||
315 | static struct option long_options[] = { | 322 | static struct option long_options[] = { |
316 | {"help", no_argument, NULL, 'h'}, | 323 | {"help", no_argument, NULL, 'h'}, |
324 | {"monitor", no_argument, NULL, 'm'}, | ||
317 | {"quiet", no_argument, NULL, 'q'}, | 325 | {"quiet", no_argument, NULL, 'q'}, |
318 | {"raw", no_argument, NULL, 'r'}, | 326 | {"raw", no_argument, NULL, 'r'}, |
319 | {"socket", required_argument, NULL, 's'}, | 327 | {"socket", required_argument, NULL, 's'}, |
@@ -326,6 +334,7 @@ int main(int argc, char **argv) { | |||
326 | "Usage: swaymsg [options] [message]\n" | 334 | "Usage: swaymsg [options] [message]\n" |
327 | "\n" | 335 | "\n" |
328 | " -h, --help Show help message and quit.\n" | 336 | " -h, --help Show help message and quit.\n" |
337 | " -m, --monitor Monitor until killed (-t SUBSCRIBE only)\n" | ||
329 | " -q, --quiet Be quiet.\n" | 338 | " -q, --quiet Be quiet.\n" |
330 | " -r, --raw Use raw output even if using a tty\n" | 339 | " -r, --raw Use raw output even if using a tty\n" |
331 | " -s, --socket <socket> Use the specified socket.\n" | 340 | " -s, --socket <socket> Use the specified socket.\n" |
@@ -337,16 +346,19 @@ int main(int argc, char **argv) { | |||
337 | int c; | 346 | int c; |
338 | while (1) { | 347 | while (1) { |
339 | int option_index = 0; | 348 | int option_index = 0; |
340 | c = getopt_long(argc, argv, "hqrs:t:v", long_options, &option_index); | 349 | c = getopt_long(argc, argv, "hmqrs:t:v", long_options, &option_index); |
341 | if (c == -1) { | 350 | if (c == -1) { |
342 | break; | 351 | break; |
343 | } | 352 | } |
344 | switch (c) { | 353 | switch (c) { |
354 | case 'm': // Monitor | ||
355 | monitor = true; | ||
356 | break; | ||
345 | case 'q': // Quiet | 357 | case 'q': // Quiet |
346 | quiet = 1; | 358 | quiet = true; |
347 | break; | 359 | break; |
348 | case 'r': // Raw | 360 | case 'r': // Raw |
349 | raw = 1; | 361 | raw = true; |
350 | break; | 362 | break; |
351 | case 's': // Socket | 363 | case 's': // Socket |
352 | socket_path = strdup(optarg); | 364 | socket_path = strdup(optarg); |
@@ -400,12 +412,20 @@ int main(int argc, char **argv) { | |||
400 | type = IPC_GET_CONFIG; | 412 | type = IPC_GET_CONFIG; |
401 | } else if (strcasecmp(cmdtype, "send_tick") == 0) { | 413 | } else if (strcasecmp(cmdtype, "send_tick") == 0) { |
402 | type = IPC_SEND_TICK; | 414 | type = IPC_SEND_TICK; |
415 | } else if (strcasecmp(cmdtype, "subscribe") == 0) { | ||
416 | type = IPC_SUBSCRIBE; | ||
403 | } else { | 417 | } else { |
404 | sway_abort("Unknown message type %s", cmdtype); | 418 | sway_abort("Unknown message type %s", cmdtype); |
405 | } | 419 | } |
406 | 420 | ||
407 | free(cmdtype); | 421 | free(cmdtype); |
408 | 422 | ||
423 | if (monitor && type != IPC_SUBSCRIBE) { | ||
424 | wlr_log(WLR_ERROR, "Monitor can only be used with -t SUBSCRIBE"); | ||
425 | free(socket_path); | ||
426 | return 1; | ||
427 | } | ||
428 | |||
409 | char *command = NULL; | 429 | char *command = NULL; |
410 | if (optind < argc) { | 430 | if (optind < argc) { |
411 | command = join_args(argv + optind, argc - optind); | 431 | command = join_args(argv + optind, argc - optind); |
@@ -422,26 +442,56 @@ int main(int argc, char **argv) { | |||
422 | json_object *obj = json_tokener_parse(resp); | 442 | json_object *obj = json_tokener_parse(resp); |
423 | 443 | ||
424 | if (obj == NULL) { | 444 | if (obj == NULL) { |
425 | fprintf(stderr, "ERROR: Could not parse json response from ipc. This is a bug in sway."); | 445 | fprintf(stderr, "ERROR: Could not parse json response from ipc. " |
446 | "This is a bug in sway."); | ||
426 | printf("%s\n", resp); | 447 | printf("%s\n", resp); |
427 | ret = 1; | 448 | ret = 1; |
428 | } else { | 449 | } else { |
429 | if (!success(obj, true)) { | 450 | if (!success(obj, true)) { |
430 | ret = 1; | 451 | ret = 1; |
431 | } | 452 | } |
432 | if (raw) { | 453 | if (type != IPC_SUBSCRIBE || ret != 0) { |
433 | printf("%s\n", json_object_to_json_string_ext(obj, | 454 | if (raw) { |
434 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); | 455 | printf("%s\n", json_object_to_json_string_ext(obj, |
435 | } else { | 456 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); |
436 | pretty_print(type, obj); | 457 | } else { |
458 | pretty_print(type, obj); | ||
459 | } | ||
437 | } | 460 | } |
438 | json_object_put(obj); | 461 | json_object_put(obj); |
439 | } | 462 | } |
440 | } | 463 | } |
441 | close(socketfd); | ||
442 | |||
443 | free(command); | 464 | free(command); |
444 | free(resp); | 465 | free(resp); |
466 | |||
467 | if (type == IPC_SUBSCRIBE && ret == 0) { | ||
468 | do { | ||
469 | struct ipc_response *reply = ipc_recv_response(socketfd); | ||
470 | if (!reply) { | ||
471 | break; | ||
472 | } | ||
473 | |||
474 | json_object *obj = json_tokener_parse(reply->payload); | ||
475 | if (obj == NULL) { | ||
476 | fprintf(stderr, "ERROR: Could not parse json response from ipc" | ||
477 | ". This is a bug in sway."); | ||
478 | ret = 1; | ||
479 | break; | ||
480 | } else { | ||
481 | if (raw) { | ||
482 | printf("%s\n", json_object_to_json_string(obj)); | ||
483 | } else { | ||
484 | printf("%s\n", json_object_to_json_string_ext(obj, | ||
485 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); | ||
486 | } | ||
487 | json_object_put(obj); | ||
488 | } | ||
489 | |||
490 | free_ipc_response(reply); | ||
491 | } while (monitor); | ||
492 | } | ||
493 | |||
494 | close(socketfd); | ||
445 | free(socket_path); | 495 | free(socket_path); |
446 | return ret; | 496 | return ret; |
447 | } | 497 | } |