summaryrefslogtreecommitdiffstats
path: root/sway/ipc-server.c
diff options
context:
space:
mode:
authorLibravatar Zandr Martin <zandrmartin@gmail.com>2016-07-04 13:34:44 -0500
committerLibravatar Zandr Martin <zandrmartin@gmail.com>2016-07-04 13:34:44 -0500
commit15a324b0d3c0d15405286794d2c8b88b764835cf (patch)
treeb2c2cf4c7e7d509b515fd6b93aa34f959fb666e1 /sway/ipc-server.c
parentSpawn windows as floating if they have a parent (diff)
downloadsway-15a324b0d3c0d15405286794d2c8b88b764835cf.tar.gz
sway-15a324b0d3c0d15405286794d2c8b88b764835cf.tar.zst
sway-15a324b0d3c0d15405286794d2c8b88b764835cf.zip
implement `get_tree` command
Diffstat (limited to 'sway/ipc-server.c')
-rw-r--r--sway/ipc-server.c168
1 files changed, 17 insertions, 151 deletions
diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index 484d7e51..0729bfd5 100644
--- a/sway/ipc-server.c
+++ b/sway/ipc-server.c
@@ -10,10 +10,10 @@
10#include <stdlib.h> 10#include <stdlib.h>
11#include <sys/ioctl.h> 11#include <sys/ioctl.h>
12#include <fcntl.h> 12#include <fcntl.h>
13#include <ctype.h>
14#include <json-c/json.h> 13#include <json-c/json.h>
15#include <list.h> 14#include <list.h>
16#include <libinput.h> 15#include <libinput.h>
16#include "ipc-json.h"
17#include "ipc-server.h" 17#include "ipc-server.h"
18#include "log.h" 18#include "log.h"
19#include "config.h" 19#include "config.h"
@@ -53,7 +53,6 @@ void ipc_client_handle_command(struct ipc_client *client);
53bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length); 53bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t payload_length);
54void ipc_get_workspaces_callback(swayc_t *workspace, void *data); 54void ipc_get_workspaces_callback(swayc_t *workspace, void *data);
55void ipc_get_outputs_callback(swayc_t *container, void *data); 55void ipc_get_outputs_callback(swayc_t *container, void *data);
56json_object *ipc_json_describe_bar_config(struct bar_config *bar);
57 56
58void ipc_init(void) { 57void ipc_init(void) {
59 ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0); 58 ipc_socket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
@@ -399,33 +398,21 @@ void ipc_client_handle_command(struct ipc_client *client) {
399 goto exit_cleanup; 398 goto exit_cleanup;
400 } 399 }
401 400
401 case IPC_GET_TREE:
402 {
403 json_object *tree = ipc_json_describe_container_recursive(&root_container);
404 const char *json_string = json_object_to_json_string(tree);
405 ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
406 json_object_put(tree);
407 goto exit_cleanup;
408 }
409
402 case IPC_GET_VERSION: 410 case IPC_GET_VERSION:
403 { 411 {
404#if defined SWAY_GIT_VERSION && defined SWAY_GIT_BRANCH && defined SWAY_VERSION_DATE 412 json_object *version = ipc_json_get_version();
405 char *full_version = calloc(strlen(SWAY_GIT_VERSION) + strlen(SWAY_GIT_BRANCH) + strlen(SWAY_VERSION_DATE) + 20, 1); 413 const char *json_string = json_object_to_json_string(version);
406 strcat(full_version, SWAY_GIT_VERSION);
407 strcat(full_version, " (");
408 strcat(full_version, SWAY_VERSION_DATE);
409 strcat(full_version, ", branch \"");
410 strcat(full_version, SWAY_GIT_BRANCH);
411 strcat(full_version, "\")");
412 json_object *json = json_object_new_object();
413 json_object_object_add(json, "human_readable", json_object_new_string(full_version));
414 json_object_object_add(json, "variant", json_object_new_string("sway"));
415 // Todo once we actually release a version
416 json_object_object_add(json, "major", json_object_new_int(0));
417 json_object_object_add(json, "minor", json_object_new_int(0));
418 json_object_object_add(json, "patch", json_object_new_int(1));
419#else
420 json_object_object_add(json, "human_readable", json_object_new_string("version not found"));
421 json_object_object_add(json, "major", json_object_new_int(0));
422 json_object_object_add(json, "minor", json_object_new_int(0));
423 json_object_object_add(json, "patch", json_object_new_int(0));
424#endif
425 const char *json_string = json_object_to_json_string(json);
426 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string)); 414 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
427 json_object_put(json); // free 415 json_object_put(version); // free
428 free(full_version);
429 goto exit_cleanup; 416 goto exit_cleanup;
430 } 417 }
431 418
@@ -518,137 +505,16 @@ bool ipc_send_reply(struct ipc_client *client, const char *payload, uint32_t pay
518 return true; 505 return true;
519} 506}
520 507
521json_object *ipc_json_describe_workspace(swayc_t *workspace) {
522 if (!sway_assert(workspace, "Workspace must not be NULL")) {
523 return NULL;
524 }
525
526 int num = isdigit(workspace->name[0]) ? atoi(workspace->name) : -1;
527 json_object *object = json_object_new_object();
528 json_object *rect = json_object_new_object();
529 json_object_object_add(rect, "x", json_object_new_int((int32_t) workspace->x));
530 json_object_object_add(rect, "y", json_object_new_int((int32_t) workspace->y));
531 json_object_object_add(rect, "width", json_object_new_int((int32_t) workspace->width));
532 json_object_object_add(rect, "height", json_object_new_int((int32_t) workspace->height));
533
534 json_object_object_add(object, "num", json_object_new_int(num));
535 json_object_object_add(object, "name", json_object_new_string(workspace->name));
536 json_object_object_add(object, "visible", json_object_new_boolean(workspace->visible));
537 bool focused = root_container.focused == workspace->parent && workspace->parent->focused == workspace;
538 json_object_object_add(object, "focused", json_object_new_boolean(focused));
539 json_object_object_add(object, "rect", rect);
540 json_object_object_add(object, "output", json_object_new_string(workspace->parent ? workspace->parent->name : "null"));
541 json_object_object_add(object, "urgent", json_object_new_boolean(false));
542
543 return object;
544}
545
546void ipc_get_workspaces_callback(swayc_t *workspace, void *data) { 508void ipc_get_workspaces_callback(swayc_t *workspace, void *data) {
547 if (workspace->type == C_WORKSPACE) { 509 if (workspace->type == C_WORKSPACE) {
548 json_object_array_add((json_object *)data, ipc_json_describe_workspace(workspace)); 510 json_object_array_add((json_object *)data, ipc_json_describe_container(workspace));
549 } 511 }
550} 512}
551 513
552json_object *ipc_json_describe_output(swayc_t *output) {
553 json_object *object = json_object_new_object();
554 json_object *rect = json_object_new_object();
555 json_object_object_add(rect, "x", json_object_new_int((int32_t) output->x));
556 json_object_object_add(rect, "y", json_object_new_int((int32_t) output->y));
557 json_object_object_add(rect, "width", json_object_new_int((int32_t) output->width));
558 json_object_object_add(rect, "height", json_object_new_int((int32_t) output->height));
559
560 json_object_object_add(object, "name", json_object_new_string(output->name));
561 json_object_object_add(object, "active", json_object_new_boolean(true));
562 json_object_object_add(object, "primary", json_object_new_boolean(false));
563 json_object_object_add(object, "rect", rect);
564 json_object_object_add(object, "current_workspace",
565 output->focused ? json_object_new_string(output->focused->name) : NULL);
566
567 return object;
568}
569
570void ipc_get_outputs_callback(swayc_t *container, void *data) { 514void ipc_get_outputs_callback(swayc_t *container, void *data) {
571 if (container->type == C_OUTPUT) { 515 if (container->type == C_OUTPUT) {
572 json_object_array_add((json_object *)data, ipc_json_describe_output(container)); 516 json_object_array_add((json_object *)data, ipc_json_describe_container(container));
573 }
574}
575
576json_object *ipc_json_describe_bar_config(struct bar_config *bar) {
577 if (!sway_assert(bar, "Bar must not be NULL")) {
578 return NULL;
579 }
580
581 json_object *json = json_object_new_object();
582 json_object_object_add(json, "id", json_object_new_string(bar->id));
583 json_object_object_add(json, "tray_output", NULL);
584 json_object_object_add(json, "mode", json_object_new_string(bar->mode));
585 json_object_object_add(json, "hidden_state", json_object_new_string(bar->hidden_state));
586 json_object_object_add(json, "modifier", json_object_new_string(get_modifier_name_by_mask(bar->modifier)));
587 switch (bar->position) {
588 case DESKTOP_SHELL_PANEL_POSITION_TOP:
589 json_object_object_add(json, "position", json_object_new_string("top"));
590 break;
591 case DESKTOP_SHELL_PANEL_POSITION_BOTTOM:
592 json_object_object_add(json, "position", json_object_new_string("bottom"));
593 break;
594 case DESKTOP_SHELL_PANEL_POSITION_LEFT:
595 json_object_object_add(json, "position", json_object_new_string("left"));
596 break;
597 case DESKTOP_SHELL_PANEL_POSITION_RIGHT:
598 json_object_object_add(json, "position", json_object_new_string("right"));
599 break;
600 }
601 json_object_object_add(json, "status_command", json_object_new_string(bar->status_command));
602 json_object_object_add(json, "font", json_object_new_string(bar->font ? bar->font : config->font));
603 if (bar->separator_symbol) {
604 json_object_object_add(json, "separator_symbol", json_object_new_string(bar->separator_symbol));
605 }
606 json_object_object_add(json, "bar_height", json_object_new_int(bar->height));
607 json_object_object_add(json, "workspace_buttons", json_object_new_boolean(bar->workspace_buttons));
608 json_object_object_add(json, "strip_workspace_numbers", json_object_new_boolean(bar->strip_workspace_numbers));
609 json_object_object_add(json, "binding_mode_indicator", json_object_new_boolean(bar->binding_mode_indicator));
610 json_object_object_add(json, "verbose", json_object_new_boolean(bar->verbose));
611 json_object_object_add(json, "pango_markup", json_object_new_boolean(bar->pango_markup));
612
613 json_object *colors = json_object_new_object();
614 json_object_object_add(colors, "background", json_object_new_string(bar->colors.background));
615 json_object_object_add(colors, "statusline", json_object_new_string(bar->colors.statusline));
616 json_object_object_add(colors, "separator", json_object_new_string(bar->colors.separator));
617
618 json_object_object_add(colors, "focused_workspace_border", json_object_new_string(bar->colors.focused_workspace_border));
619 json_object_object_add(colors, "focused_workspace_bg", json_object_new_string(bar->colors.focused_workspace_bg));
620 json_object_object_add(colors, "focused_workspace_text", json_object_new_string(bar->colors.focused_workspace_text));
621
622 json_object_object_add(colors, "inactive_workspace_border", json_object_new_string(bar->colors.inactive_workspace_border));
623 json_object_object_add(colors, "inactive_workspace_bg", json_object_new_string(bar->colors.inactive_workspace_bg));
624 json_object_object_add(colors, "inactive_workspace_text", json_object_new_string(bar->colors.inactive_workspace_text));
625
626 json_object_object_add(colors, "active_workspace_border", json_object_new_string(bar->colors.active_workspace_border));
627 json_object_object_add(colors, "active_workspace_bg", json_object_new_string(bar->colors.active_workspace_bg));
628 json_object_object_add(colors, "active_workspace_text", json_object_new_string(bar->colors.active_workspace_text));
629
630 json_object_object_add(colors, "urgent_workspace_border", json_object_new_string(bar->colors.urgent_workspace_border));
631 json_object_object_add(colors, "urgent_workspace_bg", json_object_new_string(bar->colors.urgent_workspace_bg));
632 json_object_object_add(colors, "urgent_workspace_text", json_object_new_string(bar->colors.urgent_workspace_text));
633
634 json_object_object_add(colors, "binding_mode_border", json_object_new_string(bar->colors.binding_mode_border));
635 json_object_object_add(colors, "binding_mode_bg", json_object_new_string(bar->colors.binding_mode_bg));
636 json_object_object_add(colors, "binding_mode_text", json_object_new_string(bar->colors.binding_mode_text));
637
638 json_object_object_add(json, "colors", colors);
639
640 // Add outputs if defined
641 if (bar->outputs && bar->outputs->length > 0) {
642 json_object *outputs = json_object_new_array();
643 int i;
644 for (i = 0; i < bar->outputs->length; ++i) {
645 const char *name = bar->outputs->items[i];
646 json_object_array_add(outputs, json_object_new_string(name));
647 }
648 json_object_object_add(json, "outputs", outputs);
649 } 517 }
650
651 return json;
652} 518}
653 519
654void ipc_send_event(const char *json_string, enum ipc_command_type event) { 520void ipc_send_event(const char *json_string, enum ipc_command_type event) {
@@ -672,14 +538,14 @@ void ipc_event_workspace(swayc_t *old, swayc_t *new, const char *change) {
672 json_object_object_add(obj, "change", json_object_new_string(change)); 538 json_object_object_add(obj, "change", json_object_new_string(change));
673 if (strcmp("focus", change) == 0) { 539 if (strcmp("focus", change) == 0) {
674 if (old) { 540 if (old) {
675 json_object_object_add(obj, "old", ipc_json_describe_workspace(old)); 541 json_object_object_add(obj, "old", ipc_json_describe_container(old));
676 } else { 542 } else {
677 json_object_object_add(obj, "old", NULL); 543 json_object_object_add(obj, "old", NULL);
678 } 544 }
679 } 545 }
680 546
681 if (new) { 547 if (new) {
682 json_object_object_add(obj, "current", ipc_json_describe_workspace(new)); 548 json_object_object_add(obj, "current", ipc_json_describe_container(new));
683 } else { 549 } else {
684 json_object_object_add(obj, "current", NULL); 550 json_object_object_add(obj, "current", NULL);
685 } 551 }