ipc get_inputs

This commit is contained in:
Tony Crisci 2017-12-18 14:27:38 -05:00
parent a949d7de5a
commit f2985000f3
4 changed files with 86 additions and 39 deletions

View file

@ -2,10 +2,12 @@
#define _SWAY_IPC_JSON_H
#include <json-c/json.h>
#include "sway/container.h"
#include "sway/input/input-manager.h"
json_object *ipc_json_get_version();
json_object *ipc_json_describe_container(swayc_t *c);
json_object *ipc_json_describe_container_recursive(swayc_t *c);
json_object *ipc_json_describe_input(struct sway_input_device *device);
#endif

View file

@ -141,3 +141,40 @@ json_object *ipc_json_describe_container_recursive(swayc_t *c) {
return object;
}
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;
}

View file

@ -20,6 +20,7 @@
#include "sway/ipc-json.h"
#include "sway/ipc-server.h"
#include "sway/server.h"
#include "sway/input/input-manager.h"
#include "list.h"
#include "log.h"
@ -359,6 +360,19 @@ void ipc_client_handle_command(struct ipc_client *client) {
goto exit_cleanup;
}
case IPC_GET_INPUTS:
{
json_object *inputs = json_object_new_array();
struct sway_input_device *device = NULL;
wl_list_for_each(device, &input_manager->devices, link) {
json_object_array_add(inputs, ipc_json_describe_input(device));
}
const char *json_string = json_object_to_json_string(inputs);
ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
json_object_put(inputs); // free
goto exit_cleanup;
}
case IPC_GET_TREE:
{
json_object *tree =

View file

@ -61,55 +61,49 @@ static void pretty_print_workspace(json_object *w) {
);
}
static void pretty_print_input(json_object *i) {
json_object *id, *name, *size, *caps;
json_object_object_get_ex(i, "identifier", &id);
json_object_object_get_ex(i, "name", &name);
json_object_object_get_ex(i, "size", &size);
json_object_object_get_ex(i, "capabilities", &caps);
printf( "Input device %s\n Type: ", json_object_get_string(name));
static const char *pretty_type_name(const char *name) {
// TODO these constants probably belong in the common lib
struct {
const char *a;
const char *b;
} cap_names[] = {
} type_names[] = {
{ "keyboard", "Keyboard" },
{ "pointer", "Mouse" },
{ "touch", "Touch" },
{ "tablet_tool", "Tablet tool" },
{ "tablet_pad", "Tablet pad" },
{ "gesture", "Gesture" },
{ "switch", "Switch" },
{ "tablet_tool", "Tablet tool" },
{ "touch", "Touch" },
};
size_t len = json_object_array_length(caps);
if (len == 0) {
printf("Unknown");
for (size_t i = 0; i < sizeof(type_names) / sizeof(type_names[0]); ++i) {
if (strcmp(type_names[i].a, name) == 0) {
return type_names[i].b;
}
}
json_object *cap;
for (size_t i = 0; i < len; ++i) {
cap = json_object_array_get_idx(caps, i);
const char *cap_s = json_object_get_string(cap);
const char *_name = NULL;
for (size_t j = 0; j < sizeof(cap_names) / sizeof(cap_names[0]); ++i) {
if (strcmp(cap_names[i].a, cap_s) == 0) {
_name = cap_names[i].b;
break;
}
}
printf("%s%s", _name ? _name : cap_s, len > 1 && i != len - 1 ? ", " : "");
}
printf("\n Sway ID: %s\n", json_object_get_string(id));
if (size) {
json_object *width, *height;
json_object_object_get_ex(size, "width", &width);
json_object_object_get_ex(size, "height", &height);
printf(" Size: %lfmm x %lfmm\n",
json_object_get_double(width), json_object_get_double(height));
}
printf("\n");
return name;
}
static void pretty_print_input(json_object *i) {
json_object *id, *name, *type, *product, *vendor;
json_object_object_get_ex(i, "identifier", &id);
json_object_object_get_ex(i, "name", &name);
json_object_object_get_ex(i, "type", &type);
json_object_object_get_ex(i, "product", &product);
json_object_object_get_ex(i, "vendor", &vendor);
const char *fmt =
"Input device: %s\n"
" Type: %s\n"
" Identifier: %s\n"
" Product ID: %d\n"
" Vendor ID: %d\n\n";
printf(fmt, json_object_get_string(name),
pretty_type_name(json_object_get_string(type)),
json_object_get_string(id),
json_object_get_int(product),
json_object_get_int(vendor));
}
static void pretty_print_output(json_object *o) {