aboutsummaryrefslogtreecommitdiff
path: root/swaygrab/json.c
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2016-09-19 06:53:29 +0900
committerGitHub <noreply@github.com>2016-09-19 06:53:29 +0900
commitd98c26d0edbab66042ff3da0348d339fd857f99d (patch)
tree78146ff883ee17c5f56610cce67ae658a2d2412d /swaygrab/json.c
parent043640820f4fdb7ea00bde6676b3b059f9b05c42 (diff)
parent0516dba3f68b47c0c49aa236bbba7c3c6b0b8dfd (diff)
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.c123
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 @@
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "ipc-client.h"
+#include "swaygrab/json.h"
+
+static json_object *tree;
+
+void init_json_tree(int socketfd) {
+ uint32_t len = 0;
+ char *res = ipc_single_command(socketfd, IPC_GET_TREE, NULL, &len);
+ tree = json_tokener_parse(res);
+}
+
+void free_json_tree() {
+ json_object_put(tree);
+}
+
+static bool is_focused(json_object *c) {
+ json_object *focused;
+ json_object_object_get_ex(c, "focused", &focused);
+ return json_object_get_boolean(focused);
+}
+
+static json_object *get_focused_container_r(json_object *c) {
+ json_object *name;
+ json_object_object_get_ex(c, "name", &name);
+ if (is_focused(c)) {
+ return c;
+ } else {
+ json_object *nodes, *node, *child;
+ json_object_object_get_ex(c, "nodes", &nodes);
+ int i;
+ for (i = 0; i < json_object_array_length(nodes); i++) {
+ node = json_object_array_get_idx(nodes, i);
+
+ if ((child = get_focused_container_r(node))) {
+ return child;
+ }
+ }
+
+ json_object_object_get_ex(c, "floating_nodes", &nodes);
+ for (i = 0; i < json_object_array_length(nodes); i++) {
+ node = json_object_array_get_idx(nodes, i);
+
+ if ((child = get_focused_container_r(node))) {
+ return child;
+ }
+ }
+
+ }
+
+ return NULL;
+}
+
+json_object *get_focused_container() {
+ return get_focused_container_r(tree);
+}
+
+char *get_focused_output() {
+ json_object *outputs, *output, *name;
+ json_object_object_get_ex(tree, "nodes", &outputs);
+
+ for (int i = 0; i < json_object_array_length(outputs); i++) {
+ output = json_object_array_get_idx(outputs, i);
+
+ if (get_focused_container_r(output)) {
+ json_object_object_get_ex(output, "name", &name);
+ return strdup(json_object_get_string(name));
+ }
+ }
+
+ return NULL;
+}
+
+char *create_payload(const char *output, struct wlc_geometry *g) {
+ char *payload_str = malloc(256);
+ json_object *payload = json_object_new_object();
+
+ json_object_object_add(payload, "output", json_object_new_string(output));
+ json_object_object_add(payload, "x", json_object_new_int(g->origin.x));
+ json_object_object_add(payload, "y", json_object_new_int(g->origin.y));
+ json_object_object_add(payload, "w", json_object_new_int(g->size.w));
+ json_object_object_add(payload, "h", json_object_new_int(g->size.h));
+
+ snprintf(payload_str, 256, "%s", json_object_to_json_string(payload));
+ return strdup(payload_str);
+}
+
+struct wlc_geometry *get_container_geometry(json_object *container) {
+ struct wlc_geometry *geo = malloc(sizeof(struct wlc_geometry));
+ json_object *rect, *x, *y, *w, *h;
+
+ json_object_object_get_ex(container, "rect", &rect);
+ json_object_object_get_ex(rect, "x", &x);
+ json_object_object_get_ex(rect, "y", &y);
+ json_object_object_get_ex(rect, "width", &w);
+ json_object_object_get_ex(rect, "height", &h);
+
+ geo->origin.x = json_object_get_int(x);
+ geo->origin.y = json_object_get_int(y);
+ geo->size.w = json_object_get_int(w);
+ geo->size.h = json_object_get_int(h);
+
+ return geo;
+}
+
+json_object *get_output_container(const char *output) {
+ json_object *outputs, *json_output, *name;
+ json_object_object_get_ex(tree, "nodes", &outputs);
+
+ for (int i = 0; i < json_object_array_length(outputs); i++) {
+ json_output = json_object_array_get_idx(outputs, i);
+ json_object_object_get_ex(json_output, "name", &name);
+
+ if (strcmp(json_object_get_string(name), output) == 0) {
+ return json_output;
+ }
+ }
+
+ return NULL;
+}