diff options
Diffstat (limited to 'swaymsg')
-rw-r--r-- | swaymsg/main.c | 115 | ||||
-rw-r--r-- | swaymsg/swaymsg.1.scd | 20 |
2 files changed, 103 insertions, 32 deletions
diff --git a/swaymsg/main.c b/swaymsg/main.c index 60536e48..02bb12c6 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c | |||
@@ -1,4 +1,6 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | 1 | #define _POSIX_C_SOURCE 200809L |
2 | |||
3 | #include <limits.h> | ||
2 | #include <stdio.h> | 4 | #include <stdio.h> |
3 | #include <stdlib.h> | 5 | #include <stdlib.h> |
4 | #include <string.h> | 6 | #include <string.h> |
@@ -286,28 +288,74 @@ static void pretty_print_config(json_object *c) { | |||
286 | printf("%s\n", json_object_get_string(config)); | 288 | printf("%s\n", json_object_get_string(config)); |
287 | } | 289 | } |
288 | 290 | ||
289 | static void pretty_print(int type, json_object *resp) { | 291 | static void pretty_print_tree(json_object *obj, int indent) { |
290 | if (type != IPC_COMMAND && type != IPC_GET_WORKSPACES && | 292 | for (int i = 0; i < indent; i++) { |
291 | type != IPC_GET_INPUTS && type != IPC_GET_OUTPUTS && | 293 | printf(" "); |
292 | type != IPC_GET_VERSION && type != IPC_GET_SEATS && | ||
293 | type != IPC_GET_CONFIG && type != IPC_SEND_TICK) { | ||
294 | printf("%s\n", json_object_to_json_string_ext(resp, | ||
295 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); | ||
296 | return; | ||
297 | } | 294 | } |
298 | 295 | ||
299 | if (type == IPC_SEND_TICK) { | 296 | int id = json_object_get_int(json_object_object_get(obj, "id")); |
300 | return; | 297 | const char *name = json_object_get_string(json_object_object_get(obj, "name")); |
298 | const char *type = json_object_get_string(json_object_object_get(obj, "type")); | ||
299 | const char *shell = json_object_get_string(json_object_object_get(obj, "shell")); | ||
300 | |||
301 | printf("#%d: %s \"%s\"", id, type, name); | ||
302 | |||
303 | if (shell != NULL) { | ||
304 | int pid = json_object_get_int(json_object_object_get(obj, "pid")); | ||
305 | const char *app_id = json_object_get_string(json_object_object_get(obj, "app_id")); | ||
306 | json_object *window_props_obj = json_object_object_get(obj, "window_properties"); | ||
307 | const char *instance = json_object_get_string(json_object_object_get(window_props_obj, "instance")); | ||
308 | const char *class = json_object_get_string(json_object_object_get(window_props_obj, "class")); | ||
309 | int x11_id = json_object_get_int(json_object_object_get(obj, "window")); | ||
310 | |||
311 | printf(" (%s, pid: %d", shell, pid); | ||
312 | if (app_id != NULL) { | ||
313 | printf(", app_id: \"%s\"", app_id); | ||
314 | } | ||
315 | if (instance != NULL) { | ||
316 | printf(", instance: \"%s\"", instance); | ||
317 | } | ||
318 | if (class != NULL) { | ||
319 | printf(", class: \"%s\"", class); | ||
320 | } | ||
321 | if (x11_id != 0) { | ||
322 | printf(", X11 window: 0x%X", x11_id); | ||
323 | } | ||
324 | printf(")"); | ||
301 | } | 325 | } |
302 | 326 | ||
303 | if (type == IPC_GET_VERSION) { | 327 | printf("\n"); |
304 | pretty_print_version(resp); | 328 | |
305 | return; | 329 | json_object *nodes_obj = json_object_object_get(obj, "nodes"); |
330 | size_t len = json_object_array_length(nodes_obj); | ||
331 | for (size_t i = 0; i < len; i++) { | ||
332 | pretty_print_tree(json_object_array_get_idx(nodes_obj, i), indent + 1); | ||
306 | } | 333 | } |
334 | } | ||
307 | 335 | ||
308 | if (type == IPC_GET_CONFIG) { | 336 | static void pretty_print(int type, json_object *resp) { |
337 | switch (type) { | ||
338 | case IPC_SEND_TICK: | ||
339 | return; | ||
340 | case IPC_GET_VERSION: | ||
341 | pretty_print_version(resp); | ||
342 | return; | ||
343 | case IPC_GET_CONFIG: | ||
309 | pretty_print_config(resp); | 344 | pretty_print_config(resp); |
310 | return; | 345 | return; |
346 | case IPC_GET_TREE: | ||
347 | pretty_print_tree(resp, 0); | ||
348 | return; | ||
349 | case IPC_COMMAND: | ||
350 | case IPC_GET_WORKSPACES: | ||
351 | case IPC_GET_INPUTS: | ||
352 | case IPC_GET_OUTPUTS: | ||
353 | case IPC_GET_SEATS: | ||
354 | break; | ||
355 | default: | ||
356 | printf("%s\n", json_object_to_json_string_ext(resp, | ||
357 | JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_SPACED)); | ||
358 | return; | ||
311 | } | 359 | } |
312 | 360 | ||
313 | json_object *obj; | 361 | json_object *obj; |
@@ -343,7 +391,7 @@ int main(int argc, char **argv) { | |||
343 | 391 | ||
344 | sway_log_init(SWAY_INFO, NULL); | 392 | sway_log_init(SWAY_INFO, NULL); |
345 | 393 | ||
346 | static struct option long_options[] = { | 394 | static const struct option long_options[] = { |
347 | {"help", no_argument, NULL, 'h'}, | 395 | {"help", no_argument, NULL, 'h'}, |
348 | {"monitor", no_argument, NULL, 'm'}, | 396 | {"monitor", no_argument, NULL, 'm'}, |
349 | {"pretty", no_argument, NULL, 'p'}, | 397 | {"pretty", no_argument, NULL, 'p'}, |
@@ -480,12 +528,20 @@ int main(int argc, char **argv) { | |||
480 | char *resp = ipc_single_command(socketfd, type, command, &len); | 528 | char *resp = ipc_single_command(socketfd, type, command, &len); |
481 | 529 | ||
482 | // pretty print the json | 530 | // pretty print the json |
483 | json_object *obj = json_tokener_parse(resp); | 531 | json_tokener *tok = json_tokener_new_ex(JSON_MAX_DEPTH); |
484 | if (obj == NULL) { | 532 | if (tok == NULL) { |
533 | if (quiet) { | ||
534 | exit(EXIT_FAILURE); | ||
535 | } | ||
536 | sway_abort("failed allocating json_tokener"); | ||
537 | } | ||
538 | json_object *obj = json_tokener_parse_ex(tok, resp, -1); | ||
539 | enum json_tokener_error err = json_tokener_get_error(tok); | ||
540 | json_tokener_free(tok); | ||
541 | if (obj == NULL || err != json_tokener_success) { | ||
485 | if (!quiet) { | 542 | if (!quiet) { |
486 | fprintf(stderr, "ERROR: Could not parse json response from ipc. " | 543 | sway_log(SWAY_ERROR, "failed to parse payload as json: %s", |
487 | "This is a bug in sway."); | 544 | json_tokener_error_desc(err)); |
488 | printf("%s\n", resp); | ||
489 | } | 545 | } |
490 | ret = 1; | 546 | ret = 1; |
491 | } else { | 547 | } else { |
@@ -517,13 +573,22 @@ int main(int argc, char **argv) { | |||
517 | break; | 573 | break; |
518 | } | 574 | } |
519 | 575 | ||
520 | json_object *obj = json_tokener_parse(reply->payload); | 576 | json_tokener *tok = json_tokener_new_ex(JSON_MAX_DEPTH); |
521 | if (obj == NULL) { | 577 | if (tok == NULL) { |
578 | if (quiet) { | ||
579 | exit(EXIT_FAILURE); | ||
580 | } | ||
581 | sway_abort("failed allocating json_tokener"); | ||
582 | } | ||
583 | json_object *obj = json_tokener_parse_ex(tok, reply->payload, -1); | ||
584 | enum json_tokener_error err = json_tokener_get_error(tok); | ||
585 | json_tokener_free(tok); | ||
586 | if (obj == NULL || err != json_tokener_success) { | ||
522 | if (!quiet) { | 587 | if (!quiet) { |
523 | fprintf(stderr, "ERROR: Could not parse json response from" | 588 | sway_log(SWAY_ERROR, "failed to parse payload as json: %s", |
524 | " ipc. This is a bug in sway."); | 589 | json_tokener_error_desc(err)); |
525 | ret = 1; | ||
526 | } | 590 | } |
591 | ret = 1; | ||
527 | break; | 592 | break; |
528 | } else if (quiet) { | 593 | } else if (quiet) { |
529 | json_object_put(obj); | 594 | json_object_put(obj); |
diff --git a/swaymsg/swaymsg.1.scd b/swaymsg/swaymsg.1.scd index b69013b5..24a9d6c9 100644 --- a/swaymsg/swaymsg.1.scd +++ b/swaymsg/swaymsg.1.scd | |||
@@ -21,12 +21,13 @@ _swaymsg_ [options...] [message] | |||
21 | 21 | ||
22 | *-p, --pretty* | 22 | *-p, --pretty* |
23 | Use pretty output even when not using a tty. | 23 | Use pretty output even when not using a tty. |
24 | Not available for all message types. | ||
24 | 25 | ||
25 | *-q, --quiet* | 26 | *-q, --quiet* |
26 | Sends the IPC message but does not print the response from sway. | 27 | Sends the IPC message but does not print the response from sway. |
27 | 28 | ||
28 | *-r, --raw* | 29 | *-r, --raw* |
29 | Use raw output even if using a tty. | 30 | Use raw JSON output even if using a tty. |
30 | 31 | ||
31 | *-s, --socket* <path> | 32 | *-s, --socket* <path> |
32 | Use the specified socket path. Otherwise, swaymsg will ask sway where the | 33 | Use the specified socket path. Otherwise, swaymsg will ask sway where the |
@@ -46,6 +47,11 @@ _swaymsg_ [options...] [message] | |||
46 | 47 | ||
47 | See *sway*(5) for a list of commands. | 48 | See *sway*(5) for a list of commands. |
48 | 49 | ||
50 | _swaymsg_ can return pretty printed (standalone-default) or JSON-formatted | ||
51 | (*--raw*) output. For detailed documentation on the returned JSON-data of | ||
52 | each message type listed below, refer to *sway-ipc*(7). The JSON-format can | ||
53 | contain more information than the pretty print. | ||
54 | |||
49 | Tips: | 55 | Tips: |
50 | - Command expansion is performed twice: once by swaymsg, and again by sway. | 56 | - Command expansion is performed twice: once by swaymsg, and again by sway. |
51 | If you have quoted multi-word strings in your command, enclose the entire | 57 | If you have quoted multi-word strings in your command, enclose the entire |
@@ -60,20 +66,20 @@ _swaymsg_ [options...] [message] | |||
60 | _swaymsg -- mark --add test_ instead of _swaymsg mark --add test_. | 66 | _swaymsg -- mark --add test_ instead of _swaymsg mark --add test_. |
61 | 67 | ||
62 | *get\_workspaces* | 68 | *get\_workspaces* |
63 | Gets a JSON-encoded list of workspaces and their status. | 69 | Gets a list of workspaces and their status. |
64 | 70 | ||
65 | *get\_inputs* | 71 | *get\_inputs* |
66 | Gets a JSON-encoded list of current inputs. | 72 | Gets a list of current inputs. |
67 | 73 | ||
68 | *get\_outputs* | 74 | *get\_outputs* |
69 | Gets a JSON-encoded list of current outputs. | 75 | Gets a list of current outputs. |
70 | 76 | ||
71 | *get\_tree* | 77 | *get\_tree* |
72 | Gets a JSON-encoded layout tree of all open windows, containers, outputs, | 78 | Gets a JSON-encoded layout tree of all open windows, containers, outputs, |
73 | workspaces, and so on. | 79 | workspaces, and so on. |
74 | 80 | ||
75 | *get\_seats* | 81 | *get\_seats* |
76 | Gets a JSON-encoded list of all seats, | 82 | Gets a list of all seats, |
77 | its properties and all assigned devices. | 83 | its properties and all assigned devices. |
78 | 84 | ||
79 | *get\_marks* | 85 | *get\_marks* |
@@ -83,7 +89,7 @@ _swaymsg_ [options...] [message] | |||
83 | Get a JSON-encoded configuration for swaybar. | 89 | Get a JSON-encoded configuration for swaybar. |
84 | 90 | ||
85 | *get\_version* | 91 | *get\_version* |
86 | Get JSON-encoded version information for the running instance of sway. | 92 | Get version information for the running instance of sway. |
87 | 93 | ||
88 | *get\_binding\_modes* | 94 | *get\_binding\_modes* |
89 | Gets a JSON-encoded list of currently configured binding modes. | 95 | Gets a JSON-encoded list of currently configured binding modes. |
@@ -92,7 +98,7 @@ _swaymsg_ [options...] [message] | |||
92 | Gets JSON-encoded info about the current binding state. | 98 | Gets JSON-encoded info about the current binding state. |
93 | 99 | ||
94 | *get\_config* | 100 | *get\_config* |
95 | Gets a JSON-encoded copy of the current configuration. | 101 | Gets a copy of the current configuration. Doesn't expand includes. |
96 | 102 | ||
97 | *send\_tick* | 103 | *send\_tick* |
98 | Sends a tick event to all subscribed clients. | 104 | Sends a tick event to all subscribed clients. |