diff --git a/include/sway/ipc-json.h b/include/sway/ipc-json.h index 9435b664..eef5a018 100644 --- a/include/sway/ipc-json.h +++ b/include/sway/ipc-json.h @@ -2,10 +2,12 @@ #define _SWAY_IPC_JSON_H #include #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 diff --git a/sway/ipc-json.c b/sway/ipc-json.c index 09a32c1b..bab9a201 100644 --- a/sway/ipc-json.c +++ b/sway/ipc-json.c @@ -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; +} diff --git a/sway/ipc-server.c b/sway/ipc-server.c index b7cd2d76..046e40a8 100644 --- a/sway/ipc-server.c +++ b/sway/ipc-server.c @@ -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 = diff --git a/swaymsg/main.c b/swaymsg/main.c index 2f9cfb14..18f17a59 100644 --- a/swaymsg/main.c +++ b/swaymsg/main.c @@ -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) {