aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Tony Crisci <tony@dubstepdish.com>2017-12-18 14:27:38 -0500
committerLibravatar Tony Crisci <tony@dubstepdish.com>2017-12-18 14:27:38 -0500
commitf2985000f364693fbeb832df1c4fd468c608e40f (patch)
treef84600bdc02e4239fd93f203eb020a90fc7a5f48
parentMerge branch 'wlroots' into feature/input (diff)
downloadsway-f2985000f364693fbeb832df1c4fd468c608e40f.tar.gz
sway-f2985000f364693fbeb832df1c4fd468c608e40f.tar.zst
sway-f2985000f364693fbeb832df1c4fd468c608e40f.zip
ipc get_inputs
-rw-r--r--include/sway/ipc-json.h2
-rw-r--r--sway/ipc-json.c37
-rw-r--r--sway/ipc-server.c14
-rw-r--r--swaymsg/main.c72
4 files changed, 86 insertions, 39 deletions
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 @@
2#define _SWAY_IPC_JSON_H 2#define _SWAY_IPC_JSON_H
3#include <json-c/json.h> 3#include <json-c/json.h>
4#include "sway/container.h" 4#include "sway/container.h"
5#include "sway/input/input-manager.h"
5 6
6json_object *ipc_json_get_version(); 7json_object *ipc_json_get_version();
7 8
8json_object *ipc_json_describe_container(swayc_t *c); 9json_object *ipc_json_describe_container(swayc_t *c);
9json_object *ipc_json_describe_container_recursive(swayc_t *c); 10json_object *ipc_json_describe_container_recursive(swayc_t *c);
11json_object *ipc_json_describe_input(struct sway_input_device *device);
10 12
11#endif 13#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) {
141 141
142 return object; 142 return object;
143} 143}
144
145static const char *describe_device_type(struct sway_input_device *device) {
146 switch (device->wlr_device->type) {
147 case WLR_INPUT_DEVICE_POINTER:
148 return "pointer";
149 case WLR_INPUT_DEVICE_KEYBOARD:
150 return "keyboard";
151 case WLR_INPUT_DEVICE_TOUCH:
152 return "touch";
153 case WLR_INPUT_DEVICE_TABLET_TOOL:
154 return "tablet_tool";
155 case WLR_INPUT_DEVICE_TABLET_PAD:
156 return "tablet_pad";
157 }
158 return "unknown";
159}
160
161json_object *ipc_json_describe_input(struct sway_input_device *device) {
162 if (!(sway_assert(device, "Device must not be null"))) {
163 return NULL;
164 }
165
166 json_object *object = json_object_new_object();
167
168 json_object_object_add(object, "identifier",
169 json_object_new_string(device->identifier));
170 json_object_object_add(object, "name",
171 json_object_new_string(device->wlr_device->name));
172 json_object_object_add(object, "vendor",
173 json_object_new_int(device->wlr_device->vendor));
174 json_object_object_add(object, "product",
175 json_object_new_int(device->wlr_device->product));
176 json_object_object_add(object, "type",
177 json_object_new_string(describe_device_type(device)));
178
179 return object;
180}
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 @@
20#include "sway/ipc-json.h" 20#include "sway/ipc-json.h"
21#include "sway/ipc-server.h" 21#include "sway/ipc-server.h"
22#include "sway/server.h" 22#include "sway/server.h"
23#include "sway/input/input-manager.h"
23#include "list.h" 24#include "list.h"
24#include "log.h" 25#include "log.h"
25 26
@@ -359,6 +360,19 @@ void ipc_client_handle_command(struct ipc_client *client) {
359 goto exit_cleanup; 360 goto exit_cleanup;
360 } 361 }
361 362
363 case IPC_GET_INPUTS:
364 {
365 json_object *inputs = json_object_new_array();
366 struct sway_input_device *device = NULL;
367 wl_list_for_each(device, &input_manager->devices, link) {
368 json_object_array_add(inputs, ipc_json_describe_input(device));
369 }
370 const char *json_string = json_object_to_json_string(inputs);
371 ipc_send_reply(client, json_string, (uint32_t)strlen(json_string));
372 json_object_put(inputs); // free
373 goto exit_cleanup;
374 }
375
362 case IPC_GET_TREE: 376 case IPC_GET_TREE:
363 { 377 {
364 json_object *tree = 378 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) {
61 ); 61 );
62} 62}
63 63
64static void pretty_print_input(json_object *i) { 64static const char *pretty_type_name(const char *name) {
65 json_object *id, *name, *size, *caps; 65 // TODO these constants probably belong in the common lib
66 json_object_object_get_ex(i, "identifier", &id);
67 json_object_object_get_ex(i, "name", &name);
68 json_object_object_get_ex(i, "size", &size);
69 json_object_object_get_ex(i, "capabilities", &caps);
70
71 printf( "Input device %s\n Type: ", json_object_get_string(name));
72
73 struct { 66 struct {
74 const char *a; 67 const char *a;
75 const char *b; 68 const char *b;
76 } cap_names[] = { 69 } type_names[] = {
77 { "keyboard", "Keyboard" }, 70 { "keyboard", "Keyboard" },
78 { "pointer", "Mouse" }, 71 { "pointer", "Mouse" },
79 { "touch", "Touch" },
80 { "tablet_tool", "Tablet tool" },
81 { "tablet_pad", "Tablet pad" }, 72 { "tablet_pad", "Tablet pad" },
82 { "gesture", "Gesture" }, 73 { "tablet_tool", "Tablet tool" },
83 { "switch", "Switch" }, 74 { "touch", "Touch" },
84 }; 75 };
85 76
86 size_t len = json_object_array_length(caps); 77 for (size_t i = 0; i < sizeof(type_names) / sizeof(type_names[0]); ++i) {
87 if (len == 0) { 78 if (strcmp(type_names[i].a, name) == 0) {
88 printf("Unknown"); 79 return type_names[i].b;
89 }
90
91 json_object *cap;
92 for (size_t i = 0; i < len; ++i) {
93 cap = json_object_array_get_idx(caps, i);
94 const char *cap_s = json_object_get_string(cap);
95 const char *_name = NULL;
96 for (size_t j = 0; j < sizeof(cap_names) / sizeof(cap_names[0]); ++i) {
97 if (strcmp(cap_names[i].a, cap_s) == 0) {
98 _name = cap_names[i].b;
99 break;
100 }
101 } 80 }
102 printf("%s%s", _name ? _name : cap_s, len > 1 && i != len - 1 ? ", " : "");
103 }
104 printf("\n Sway ID: %s\n", json_object_get_string(id));
105 if (size) {
106 json_object *width, *height;
107 json_object_object_get_ex(size, "width", &width);
108 json_object_object_get_ex(size, "height", &height);
109 printf(" Size: %lfmm x %lfmm\n",
110 json_object_get_double(width), json_object_get_double(height));
111 } 81 }
112 printf("\n"); 82
83 return name;
84}
85
86static void pretty_print_input(json_object *i) {
87 json_object *id, *name, *type, *product, *vendor;
88 json_object_object_get_ex(i, "identifier", &id);
89 json_object_object_get_ex(i, "name", &name);
90 json_object_object_get_ex(i, "type", &type);
91 json_object_object_get_ex(i, "product", &product);
92 json_object_object_get_ex(i, "vendor", &vendor);
93
94 const char *fmt =
95 "Input device: %s\n"
96 " Type: %s\n"
97 " Identifier: %s\n"
98 " Product ID: %d\n"
99 " Vendor ID: %d\n\n";
100
101
102 printf(fmt, json_object_get_string(name),
103 pretty_type_name(json_object_get_string(type)),
104 json_object_get_string(id),
105 json_object_get_int(product),
106 json_object_get_int(vendor));
113} 107}
114 108
115static void pretty_print_output(json_object *o) { 109static void pretty_print_output(json_object *o) {