diff options
Diffstat (limited to 'swaymsg/main.c')
-rw-r--r-- | swaymsg/main.c | 115 |
1 files changed, 90 insertions, 25 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); |