aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/sway/ipc-json.h3
-rw-r--r--sway/ipc-json.c91
-rw-r--r--sway/ipc-server.c10
3 files changed, 104 insertions, 0 deletions
diff --git a/include/sway/ipc-json.h b/include/sway/ipc-json.h
index 5bfddcba..9986c399 100644
--- a/include/sway/ipc-json.h
+++ b/include/sway/ipc-json.h
@@ -1,7 +1,10 @@
1#ifndef _SWAY_IPC_JSON_H 1#ifndef _SWAY_IPC_JSON_H
2#define _SWAY_IPC_JSON_H 2#define _SWAY_IPC_JSON_H
3#include <json-c/json.h> 3#include <json-c/json.h>
4#include "sway/container.h"
4 5
5json_object *ipc_json_get_version(); 6json_object *ipc_json_get_version();
6 7
8json_object *ipc_json_describe_container_recursive(swayc_t *c);
9
7#endif 10#endif
diff --git a/sway/ipc-json.c b/sway/ipc-json.c
index 6fcb91fa..2e774a96 100644
--- a/sway/ipc-json.c
+++ b/sway/ipc-json.c
@@ -1,6 +1,11 @@
1#include <json-c/json.h> 1#include <json-c/json.h>
2#include <stdio.h> 2#include <stdio.h>
3#include <ctype.h>
4#include "log.h"
3#include "sway/ipc-json.h" 5#include "sway/ipc-json.h"
6#include "sway/container.h"
7#include <wlr/types/wlr_box.h>
8#include <wlr/types/wlr_output.h>
4 9
5json_object *ipc_json_get_version() { 10json_object *ipc_json_get_version() {
6 int major = 0, minor = 0, patch = 0; 11 int major = 0, minor = 0, patch = 0;
@@ -16,3 +21,89 @@ json_object *ipc_json_get_version() {
16 21
17 return version; 22 return version;
18} 23}
24
25static json_object *ipc_json_create_rect(swayc_t *c) {
26 json_object *rect = json_object_new_object();
27
28 json_object_object_add(rect, "x", json_object_new_int((int32_t)c->x));
29 json_object_object_add(rect, "y", json_object_new_int((int32_t)c->y));
30 json_object_object_add(rect, "width", json_object_new_int((int32_t)c->width));
31 json_object_object_add(rect, "height", json_object_new_int((int32_t)c->height));
32
33 return rect;
34}
35
36static void ipc_json_describe_root(swayc_t *root, json_object *object) {
37 json_object_object_add(object, "type", json_object_new_string("root"));
38 json_object_object_add(object, "layout", json_object_new_string("splith"));
39}
40
41static void ipc_json_describe_output(swayc_t *output, json_object *object) {
42 json_object_object_add(object, "type", json_object_new_string("output"));
43 json_object_object_add(object, "current_workspace",
44 (output->focused) ? json_object_new_string(output->focused->name) : NULL);
45}
46
47static void ipc_json_describe_workspace(swayc_t *workspace, json_object *object) {
48 int num = (isdigit(workspace->name[0])) ? atoi(workspace->name) : -1;
49
50 json_object_object_add(object, "num", json_object_new_int(num));
51 json_object_object_add(object, "output", (workspace->parent) ? json_object_new_string(workspace->parent->name) : NULL);
52 json_object_object_add(object, "type", json_object_new_string("workspace"));
53}
54
55static void ipc_json_describe_view(swayc_t *c, json_object *object) {
56 json_object_object_add(object, "name", (c->name) ? json_object_new_string(c->name) : NULL);
57}
58
59json_object *ipc_json_describe_container(swayc_t *c) {
60 if (!(sway_assert(c, "Container must not be null."))) {
61 return NULL;
62 }
63
64 json_object *object = json_object_new_object();
65
66 json_object_object_add(object, "id", json_object_new_int((int)c->id));
67 json_object_object_add(object, "name", (c->name) ? json_object_new_string(c->name) : NULL);
68 json_object_object_add(object, "rect", ipc_json_create_rect(c));
69
70 switch (c->type) {
71 case C_ROOT:
72 ipc_json_describe_root(c, object);
73 break;
74
75 case C_OUTPUT:
76 ipc_json_describe_output(c, object);
77 break;
78
79 case C_CONTAINER:
80 case C_VIEW:
81 ipc_json_describe_view(c, object);
82 break;
83
84 case C_WORKSPACE:
85 ipc_json_describe_workspace(c, object);
86 break;
87
88 case C_TYPES:
89 default:
90 break;
91 }
92
93 return object;
94}
95
96json_object *ipc_json_describe_container_recursive(swayc_t *c) {
97 json_object *object = ipc_json_describe_container(c);
98 int i;
99
100 json_object *children = json_object_new_array();
101 if (c->type != C_VIEW && c->children) {
102 for (i = 0; i < c->children->length; ++i) {
103 json_object_array_add(children, ipc_json_describe_container_recursive(c->children->items[i]));
104 }
105 }
106 json_object_object_add(object, "nodes", children);
107
108 return object;
109}
diff --git a/sway/ipc-server.c b/sway/ipc-server.c
index f3a6469b..71f8dddd 100644
--- a/sway/ipc-server.c
+++ b/sway/ipc-server.c
@@ -343,6 +343,16 @@ void ipc_client_handle_command(struct ipc_client *client) {
343 goto exit_cleanup; 343 goto exit_cleanup;
344 } 344 }
345 345
346 case IPC_GET_TREE:
347 {
348 json_object *tree =
349 ipc_json_describe_container_recursive(&root_container);
350 const char *json_string = json_object_to_json_string(tree);
351 ipc_send_reply(client, json_string, (uint32_t) strlen(json_string));
352 json_object_put(tree);
353 goto exit_cleanup;
354 }
355
346 case IPC_GET_VERSION: 356 case IPC_GET_VERSION:
347 { 357 {
348 json_object *version = ipc_json_get_version(); 358 json_object *version = ipc_json_get_version();