aboutsummaryrefslogtreecommitdiffstats
path: root/sway/ipc-json.c
diff options
context:
space:
mode:
authorLibravatar Tony Crisci <tony@dubstepdish.com>2017-12-03 08:33:52 -0500
committerLibravatar Tony Crisci <tony@dubstepdish.com>2017-12-03 08:33:52 -0500
commit421f49fe034cc8a48348ba57af8fdf3f98adb62c (patch)
tree4fafbee0256eff1e0e9baa283ae1c8c9342e15ff /sway/ipc-json.c
parentMerge pull request #1487 from acrisci/feature/log-swaysock (diff)
downloadsway-421f49fe034cc8a48348ba57af8fdf3f98adb62c.tar.gz
sway-421f49fe034cc8a48348ba57af8fdf3f98adb62c.tar.zst
sway-421f49fe034cc8a48348ba57af8fdf3f98adb62c.zip
basic get_tree
Diffstat (limited to 'sway/ipc-json.c')
-rw-r--r--sway/ipc-json.c91
1 files changed, 91 insertions, 0 deletions
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}