diff options
author | Drew DeVault <sir@cmpwn.com> | 2016-09-19 06:53:29 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-19 06:53:29 +0900 |
commit | d98c26d0edbab66042ff3da0348d339fd857f99d (patch) | |
tree | 78146ff883ee17c5f56610cce67ae658a2d2412d /swaygrab/json.c | |
parent | Merge pull request #907 from zandrmartin/fix-json-tree-focus-property (diff) | |
parent | implement "focused container" feature for swaygrab (diff) | |
download | sway-d98c26d0edbab66042ff3da0348d339fd857f99d.tar.gz sway-d98c26d0edbab66042ff3da0348d339fd857f99d.tar.zst sway-d98c26d0edbab66042ff3da0348d339fd857f99d.zip |
Merge pull request #909 from zandrmartin/grab-focused
implement "focused container" option for swaygrab
Diffstat (limited to 'swaygrab/json.c')
-rw-r--r-- | swaygrab/json.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/swaygrab/json.c b/swaygrab/json.c new file mode 100644 index 00000000..7cd73cbe --- /dev/null +++ b/swaygrab/json.c | |||
@@ -0,0 +1,123 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <stdbool.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <unistd.h> | ||
5 | #include "ipc-client.h" | ||
6 | #include "swaygrab/json.h" | ||
7 | |||
8 | static json_object *tree; | ||
9 | |||
10 | void init_json_tree(int socketfd) { | ||
11 | uint32_t len = 0; | ||
12 | char *res = ipc_single_command(socketfd, IPC_GET_TREE, NULL, &len); | ||
13 | tree = json_tokener_parse(res); | ||
14 | } | ||
15 | |||
16 | void free_json_tree() { | ||
17 | json_object_put(tree); | ||
18 | } | ||
19 | |||
20 | static bool is_focused(json_object *c) { | ||
21 | json_object *focused; | ||
22 | json_object_object_get_ex(c, "focused", &focused); | ||
23 | return json_object_get_boolean(focused); | ||
24 | } | ||
25 | |||
26 | static json_object *get_focused_container_r(json_object *c) { | ||
27 | json_object *name; | ||
28 | json_object_object_get_ex(c, "name", &name); | ||
29 | if (is_focused(c)) { | ||
30 | return c; | ||
31 | } else { | ||
32 | json_object *nodes, *node, *child; | ||
33 | json_object_object_get_ex(c, "nodes", &nodes); | ||
34 | int i; | ||
35 | for (i = 0; i < json_object_array_length(nodes); i++) { | ||
36 | node = json_object_array_get_idx(nodes, i); | ||
37 | |||
38 | if ((child = get_focused_container_r(node))) { | ||
39 | return child; | ||
40 | } | ||
41 | } | ||
42 | |||
43 | json_object_object_get_ex(c, "floating_nodes", &nodes); | ||
44 | for (i = 0; i < json_object_array_length(nodes); i++) { | ||
45 | node = json_object_array_get_idx(nodes, i); | ||
46 | |||
47 | if ((child = get_focused_container_r(node))) { | ||
48 | return child; | ||
49 | } | ||
50 | } | ||
51 | |||
52 | } | ||
53 | |||
54 | return NULL; | ||
55 | } | ||
56 | |||
57 | json_object *get_focused_container() { | ||
58 | return get_focused_container_r(tree); | ||
59 | } | ||
60 | |||
61 | char *get_focused_output() { | ||
62 | json_object *outputs, *output, *name; | ||
63 | json_object_object_get_ex(tree, "nodes", &outputs); | ||
64 | |||
65 | for (int i = 0; i < json_object_array_length(outputs); i++) { | ||
66 | output = json_object_array_get_idx(outputs, i); | ||
67 | |||
68 | if (get_focused_container_r(output)) { | ||
69 | json_object_object_get_ex(output, "name", &name); | ||
70 | return strdup(json_object_get_string(name)); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | return NULL; | ||
75 | } | ||
76 | |||
77 | char *create_payload(const char *output, struct wlc_geometry *g) { | ||
78 | char *payload_str = malloc(256); | ||
79 | json_object *payload = json_object_new_object(); | ||
80 | |||
81 | json_object_object_add(payload, "output", json_object_new_string(output)); | ||
82 | json_object_object_add(payload, "x", json_object_new_int(g->origin.x)); | ||
83 | json_object_object_add(payload, "y", json_object_new_int(g->origin.y)); | ||
84 | json_object_object_add(payload, "w", json_object_new_int(g->size.w)); | ||
85 | json_object_object_add(payload, "h", json_object_new_int(g->size.h)); | ||
86 | |||
87 | snprintf(payload_str, 256, "%s", json_object_to_json_string(payload)); | ||
88 | return strdup(payload_str); | ||
89 | } | ||
90 | |||
91 | struct wlc_geometry *get_container_geometry(json_object *container) { | ||
92 | struct wlc_geometry *geo = malloc(sizeof(struct wlc_geometry)); | ||
93 | json_object *rect, *x, *y, *w, *h; | ||
94 | |||
95 | json_object_object_get_ex(container, "rect", &rect); | ||
96 | json_object_object_get_ex(rect, "x", &x); | ||
97 | json_object_object_get_ex(rect, "y", &y); | ||
98 | json_object_object_get_ex(rect, "width", &w); | ||
99 | json_object_object_get_ex(rect, "height", &h); | ||
100 | |||
101 | geo->origin.x = json_object_get_int(x); | ||
102 | geo->origin.y = json_object_get_int(y); | ||
103 | geo->size.w = json_object_get_int(w); | ||
104 | geo->size.h = json_object_get_int(h); | ||
105 | |||
106 | return geo; | ||
107 | } | ||
108 | |||
109 | json_object *get_output_container(const char *output) { | ||
110 | json_object *outputs, *json_output, *name; | ||
111 | json_object_object_get_ex(tree, "nodes", &outputs); | ||
112 | |||
113 | for (int i = 0; i < json_object_array_length(outputs); i++) { | ||
114 | json_output = json_object_array_get_idx(outputs, i); | ||
115 | json_object_object_get_ex(json_output, "name", &name); | ||
116 | |||
117 | if (strcmp(json_object_get_string(name), output) == 0) { | ||
118 | return json_output; | ||
119 | } | ||
120 | } | ||
121 | |||
122 | return NULL; | ||
123 | } | ||