swayfx/sway/ipc-json.c

201 lines
6.8 KiB
C
Raw Normal View History

2017-11-23 13:37:07 +11:00
#include <json-c/json.h>
#include <stdio.h>
2017-12-04 00:33:52 +11:00
#include <ctype.h>
#include "log.h"
2017-11-23 13:37:07 +11:00
#include "sway/ipc-json.h"
#include "sway/tree/container.h"
2017-12-19 00:06:03 +11:00
#include "sway/output.h"
2018-02-27 09:40:38 +11:00
#include "sway/input/input-manager.h"
#include "sway/input/seat.h"
2017-12-04 00:33:52 +11:00
#include <wlr/types/wlr_box.h>
#include <wlr/types/wlr_output.h>
2017-11-23 13:37:07 +11:00
json_object *ipc_json_get_version() {
int major = 0, minor = 0, patch = 0;
json_object *version = json_object_new_object();
sscanf(SWAY_VERSION, "%u.%u.%u", &major, &minor, &patch);
json_object_object_add(version, "human_readable", json_object_new_string(SWAY_VERSION));
json_object_object_add(version, "variant", json_object_new_string("sway"));
json_object_object_add(version, "major", json_object_new_int(major));
json_object_object_add(version, "minor", json_object_new_int(minor));
json_object_object_add(version, "patch", json_object_new_int(patch));
return version;
}
2017-12-04 00:33:52 +11:00
2018-03-30 07:17:55 +11:00
static json_object *ipc_json_create_rect(struct sway_container *c) {
2017-12-04 00:33:52 +11:00
json_object *rect = json_object_new_object();
json_object_object_add(rect, "x", json_object_new_int((int32_t)c->x));
json_object_object_add(rect, "y", json_object_new_int((int32_t)c->y));
json_object_object_add(rect, "width", json_object_new_int((int32_t)c->width));
json_object_object_add(rect, "height", json_object_new_int((int32_t)c->height));
return rect;
}
2018-03-30 07:17:55 +11:00
static void ipc_json_describe_root(struct sway_container *root, json_object *object) {
2017-12-04 00:33:52 +11:00
json_object_object_add(object, "type", json_object_new_string("root"));
json_object_object_add(object, "layout", json_object_new_string("splith"));
}
2017-12-19 00:06:03 +11:00
static const char *ipc_json_get_output_transform(enum wl_output_transform transform) {
switch (transform) {
case WL_OUTPUT_TRANSFORM_NORMAL:
return "normal";
case WL_OUTPUT_TRANSFORM_90:
return "90";
case WL_OUTPUT_TRANSFORM_180:
return "180";
case WL_OUTPUT_TRANSFORM_270:
return "270";
case WL_OUTPUT_TRANSFORM_FLIPPED:
return "flipped";
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
return "flipped-90";
case WL_OUTPUT_TRANSFORM_FLIPPED_180:
return "flipped-180";
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
return "flipped-270";
}
return NULL;
}
2018-03-30 07:17:55 +11:00
static void ipc_json_describe_output(struct sway_container *container, json_object *object) {
2017-12-19 00:06:03 +11:00
struct wlr_output *wlr_output = container->sway_output->wlr_output;
2017-12-04 00:33:52 +11:00
json_object_object_add(object, "type", json_object_new_string("output"));
2017-12-19 00:06:03 +11:00
json_object_object_add(object, "active", json_object_new_boolean(true));
json_object_object_add(object, "primary", json_object_new_boolean(false));
json_object_object_add(object, "layout", json_object_new_string("output"));
json_object_object_add(object, "make", json_object_new_string(wlr_output->make));
json_object_object_add(object, "model", json_object_new_string(wlr_output->model));
json_object_object_add(object, "serial", json_object_new_string(wlr_output->serial));
json_object_object_add(object, "scale", json_object_new_double(wlr_output->scale));
json_object_object_add(object, "refresh", json_object_new_int(wlr_output->refresh));
json_object_object_add(object, "transform",
json_object_new_string(ipc_json_get_output_transform(wlr_output->transform)));
2018-02-11 11:53:50 +11:00
// TODO WLR need to set "current_workspace" to the currently focused
// workspace in a way that makes sense with multiseat
2018-03-12 23:48:42 +11:00
json_object *modes_array = json_object_new_array();
struct wlr_output_mode *mode;
wl_list_for_each(mode, &wlr_output->modes, link) {
json_object *mode_object = json_object_new_object();
json_object_object_add(mode_object, "width",
json_object_new_int(mode->width));
json_object_object_add(mode_object, "height",
json_object_new_int(mode->height));
json_object_object_add(mode_object, "refresh",
json_object_new_int(mode->refresh));
json_object_array_add(modes_array, mode_object);
}
json_object_object_add(object, "modes", modes_array);
2017-12-04 00:33:52 +11:00
}
2018-03-30 07:17:55 +11:00
static void ipc_json_describe_workspace(struct sway_container *workspace, json_object *object) {
2017-12-04 00:33:52 +11:00
int num = (isdigit(workspace->name[0])) ? atoi(workspace->name) : -1;
json_object_object_add(object, "num", json_object_new_int(num));
json_object_object_add(object, "output", (workspace->parent) ? json_object_new_string(workspace->parent->name) : NULL);
json_object_object_add(object, "type", json_object_new_string("workspace"));
}
2018-03-30 07:17:55 +11:00
static void ipc_json_describe_view(struct sway_container *c, json_object *object) {
2017-12-04 00:33:52 +11:00
json_object_object_add(object, "name", (c->name) ? json_object_new_string(c->name) : NULL);
}
2018-03-30 07:17:55 +11:00
json_object *ipc_json_describe_container(struct sway_container *c) {
2017-12-04 00:33:52 +11:00
if (!(sway_assert(c, "Container must not be null."))) {
return NULL;
}
2018-02-27 09:40:38 +11:00
struct sway_seat *seat = sway_input_manager_get_default_seat(input_manager);
bool focused = sway_seat_get_focus(seat) == c;
2017-12-04 00:33:52 +11:00
json_object *object = json_object_new_object();
json_object_object_add(object, "id", json_object_new_int((int)c->id));
json_object_object_add(object, "name", (c->name) ? json_object_new_string(c->name) : NULL);
json_object_object_add(object, "rect", ipc_json_create_rect(c));
2018-02-27 09:40:38 +11:00
json_object_object_add(object, "focused", json_object_new_boolean(focused));
2017-12-04 00:33:52 +11:00
switch (c->type) {
case C_ROOT:
ipc_json_describe_root(c, object);
break;
case C_OUTPUT:
ipc_json_describe_output(c, object);
break;
case C_CONTAINER:
case C_VIEW:
ipc_json_describe_view(c, object);
break;
case C_WORKSPACE:
ipc_json_describe_workspace(c, object);
break;
case C_TYPES:
default:
break;
}
return object;
}
2018-03-30 07:17:55 +11:00
json_object *ipc_json_describe_container_recursive(struct sway_container *c) {
2017-12-04 00:33:52 +11:00
json_object *object = ipc_json_describe_container(c);
int i;
json_object *children = json_object_new_array();
if (c->type != C_VIEW && c->children) {
for (i = 0; i < c->children->length; ++i) {
json_object_array_add(children, ipc_json_describe_container_recursive(c->children->items[i]));
}
}
json_object_object_add(object, "nodes", children);
return object;
}
2017-12-19 06:27:38 +11:00
static const char *describe_device_type(struct sway_input_device *device) {
switch (device->wlr_device->type) {
case WLR_INPUT_DEVICE_POINTER:
return "pointer";
case WLR_INPUT_DEVICE_KEYBOARD:
return "keyboard";
case WLR_INPUT_DEVICE_TOUCH:
return "touch";
case WLR_INPUT_DEVICE_TABLET_TOOL:
return "tablet_tool";
case WLR_INPUT_DEVICE_TABLET_PAD:
return "tablet_pad";
}
return "unknown";
}
json_object *ipc_json_describe_input(struct sway_input_device *device) {
if (!(sway_assert(device, "Device must not be null"))) {
return NULL;
}
json_object *object = json_object_new_object();
json_object_object_add(object, "identifier",
json_object_new_string(device->identifier));
json_object_object_add(object, "name",
json_object_new_string(device->wlr_device->name));
json_object_object_add(object, "vendor",
json_object_new_int(device->wlr_device->vendor));
json_object_object_add(object, "product",
json_object_new_int(device->wlr_device->product));
json_object_object_add(object, "type",
json_object_new_string(describe_device_type(device)));
return object;
}