diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-05-12 09:27:24 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-12 09:27:24 -0400 |
commit | 867190e0828a139fa4529cde60d033a9389df42c (patch) | |
tree | c986cca12ecdb3e3ac4d94d38205028788315889 /swaygrab/json.c | |
parent | Merge pull request #1959 from RyanDwyer/ipc-get-seats (diff) | |
parent | Merge remote-tracking branch 'origin/master' into scdoc (diff) | |
download | sway-867190e0828a139fa4529cde60d033a9389df42c.tar.gz sway-867190e0828a139fa4529cde60d033a9389df42c.tar.zst sway-867190e0828a139fa4529cde60d033a9389df42c.zip |
Merge pull request #1958 from swaywm/scdoc
Port man pages to scdoc
Diffstat (limited to 'swaygrab/json.c')
-rw-r--r-- | swaygrab/json.c | 144 |
1 files changed, 0 insertions, 144 deletions
diff --git a/swaygrab/json.c b/swaygrab/json.c deleted file mode 100644 index 286085c3..00000000 --- a/swaygrab/json.c +++ /dev/null | |||
@@ -1,144 +0,0 @@ | |||
1 | #define _XOPEN_SOURCE 700 | ||
2 | #include <string.h> | ||
3 | #include <stdio.h> | ||
4 | #include <stdbool.h> | ||
5 | #include <stdlib.h> | ||
6 | #include <unistd.h> | ||
7 | #include "log.h" | ||
8 | #include "ipc-client.h" | ||
9 | #include "swaygrab/json.h" | ||
10 | |||
11 | static json_object *tree; | ||
12 | |||
13 | void init_json_tree(int socketfd) { | ||
14 | uint32_t len = 0; | ||
15 | char *res = ipc_single_command(socketfd, IPC_GET_TREE, NULL, &len); | ||
16 | struct json_tokener *tok = json_tokener_new_ex(256); | ||
17 | if (!tok) { | ||
18 | sway_abort("Unable to get json tokener."); | ||
19 | } | ||
20 | tree = json_tokener_parse_ex(tok, res, len); | ||
21 | if (!tree || tok->err != json_tokener_success) { | ||
22 | sway_abort("Unable to parse IPC response as JSON: %s", json_tokener_error_desc(tok->err)); | ||
23 | } | ||
24 | json_object *success; | ||
25 | json_object_object_get_ex(tree, "success", &success); | ||
26 | if (success && !json_object_get_boolean(success)) { | ||
27 | json_object *error; | ||
28 | json_object_object_get_ex(tree, "error", &error); | ||
29 | sway_abort("IPC request failed: %s", json_object_get_string(error)); | ||
30 | } | ||
31 | json_object_put(success); | ||
32 | json_tokener_free(tok); | ||
33 | } | ||
34 | |||
35 | void free_json_tree() { | ||
36 | json_object_put(tree); | ||
37 | } | ||
38 | |||
39 | static bool is_focused(json_object *c) { | ||
40 | json_object *focused; | ||
41 | json_object_object_get_ex(c, "focused", &focused); | ||
42 | return json_object_get_boolean(focused); | ||
43 | } | ||
44 | |||
45 | static json_object *get_focused_container_r(json_object *c) { | ||
46 | json_object *name; | ||
47 | json_object_object_get_ex(c, "name", &name); | ||
48 | if (is_focused(c)) { | ||
49 | return c; | ||
50 | } else { | ||
51 | json_object *nodes, *node, *child; | ||
52 | json_object_object_get_ex(c, "nodes", &nodes); | ||
53 | int i; | ||
54 | for (i = 0; i < json_object_array_length(nodes); i++) { | ||
55 | node = json_object_array_get_idx(nodes, i); | ||
56 | |||
57 | if ((child = get_focused_container_r(node))) { | ||
58 | return child; | ||
59 | } | ||
60 | } | ||
61 | |||
62 | json_object_object_get_ex(c, "floating_nodes", &nodes); | ||
63 | for (i = 0; i < json_object_array_length(nodes); i++) { | ||
64 | node = json_object_array_get_idx(nodes, i); | ||
65 | |||
66 | if ((child = get_focused_container_r(node))) { | ||
67 | return child; | ||
68 | } | ||
69 | } | ||
70 | |||
71 | } | ||
72 | |||
73 | return NULL; | ||
74 | } | ||
75 | |||
76 | json_object *get_focused_container() { | ||
77 | return get_focused_container_r(tree); | ||
78 | } | ||
79 | |||
80 | char *get_focused_output() { | ||
81 | json_object *outputs, *output, *name; | ||
82 | json_object_object_get_ex(tree, "nodes", &outputs); | ||
83 | if (!outputs) { | ||
84 | sway_abort("Unabled to get focused output. No nodes in tree."); | ||
85 | } | ||
86 | for (int i = 0; i < json_object_array_length(outputs); i++) { | ||
87 | output = json_object_array_get_idx(outputs, i); | ||
88 | |||
89 | if (get_focused_container_r(output)) { | ||
90 | json_object_object_get_ex(output, "name", &name); | ||
91 | return strdup(json_object_get_string(name)); | ||
92 | } | ||
93 | } | ||
94 | |||
95 | return NULL; | ||
96 | } | ||
97 | |||
98 | char *create_payload(const char *output, struct wlc_geometry *g) { | ||
99 | char *payload_str = malloc(256); | ||
100 | json_object *payload = json_object_new_object(); | ||
101 | |||
102 | json_object_object_add(payload, "output", json_object_new_string(output)); | ||
103 | json_object_object_add(payload, "x", json_object_new_int(g->origin.x)); | ||
104 | json_object_object_add(payload, "y", json_object_new_int(g->origin.y)); | ||
105 | json_object_object_add(payload, "w", json_object_new_int(g->size.w)); | ||
106 | json_object_object_add(payload, "h", json_object_new_int(g->size.h)); | ||
107 | |||
108 | snprintf(payload_str, 256, "%s", json_object_to_json_string(payload)); | ||
109 | return strdup(payload_str); | ||
110 | } | ||
111 | |||
112 | struct wlc_geometry *get_container_geometry(json_object *container) { | ||
113 | struct wlc_geometry *geo = malloc(sizeof(struct wlc_geometry)); | ||
114 | json_object *rect, *x, *y, *w, *h; | ||
115 | |||
116 | json_object_object_get_ex(container, "rect", &rect); | ||
117 | json_object_object_get_ex(rect, "x", &x); | ||
118 | json_object_object_get_ex(rect, "y", &y); | ||
119 | json_object_object_get_ex(rect, "width", &w); | ||
120 | json_object_object_get_ex(rect, "height", &h); | ||
121 | |||
122 | geo->origin.x = json_object_get_int(x); | ||
123 | geo->origin.y = json_object_get_int(y); | ||
124 | geo->size.w = json_object_get_int(w); | ||
125 | geo->size.h = json_object_get_int(h); | ||
126 | |||
127 | return geo; | ||
128 | } | ||
129 | |||
130 | json_object *get_output_container(const char *output) { | ||
131 | json_object *outputs, *json_output, *name; | ||
132 | json_object_object_get_ex(tree, "nodes", &outputs); | ||
133 | |||
134 | for (int i = 0; i < json_object_array_length(outputs); i++) { | ||
135 | json_output = json_object_array_get_idx(outputs, i); | ||
136 | json_object_object_get_ex(json_output, "name", &name); | ||
137 | |||
138 | if (strcmp(json_object_get_string(name), output) == 0) { | ||
139 | return json_output; | ||
140 | } | ||
141 | } | ||
142 | |||
143 | return NULL; | ||
144 | } | ||