aboutsummaryrefslogtreecommitdiffstats
path: root/sway/ipc-json.c
diff options
context:
space:
mode:
authorLibravatar Drew DeVault <sir@cmpwn.com>2017-04-03 07:27:25 -0400
committerLibravatar Drew DeVault <sir@cmpwn.com>2017-04-03 07:27:25 -0400
commit60ce81e06adc9ea133e8cfd030465e94295a95ff (patch)
treeeb34022e36dd9277b40bed5bb4def27d5a1de5e0 /sway/ipc-json.c
parentUpdate README.md (diff)
downloadsway-60ce81e06adc9ea133e8cfd030465e94295a95ff.tar.gz
sway-60ce81e06adc9ea133e8cfd030465e94295a95ff.tar.zst
sway-60ce81e06adc9ea133e8cfd030465e94295a95ff.zip
Add pretty printing to swaymsg
If stdout is a tty, it will pretty print unless -r (--raw) is given. Sample outputs: ``` ~/s/s/build > ./bin/swaymsg fullscreen toggle Error: Permission denied for fullscreen toggle via IPC ~/s/s/build > ./bin/swaymsg -t get_workspaces Workspace 3:三 Output: DVI-I-1 Layout: splith Workspace 1:一 (off-screen) Output: HDMI-A-1 Layout: splith Workspace 5:五 (focused) Output: HDMI-A-1 Layout: splith ~/s/s/build > ./bin/swaymsg -t get_inputs Input device Metadot - Das Keyboard Das Keyboard Type: Keyboard Sway ID: 9456:320:Metadot_-_Das_Keyboard_Das_Keyb Input device Wacom Intuos S 2 Pen Type: Tablet tool Sway ID: 1386:827:Wacom_Intuos_S_2 Input device Wacom Intuos S 2 Pad Type: Tablet pad Sway ID: 1386:827:Wacom_Intuos_S_2 Input device Logitech Gaming Mouse G502 Type: Keyboard, Mouse Sway ID: 1133:49277:Logitech_Gaming_Mous ~/s/s/build > ./bin/swaymsg -t get_outputs Output DVI-I-1 Geometry: 1920x1080 @ 3840,0 Scale factor: 1x Workspace: 3:三 Output DVI-D-1 Geometry: 1920x1080 @ 0,0 Scale factor: 1x Workspace: 4:四 Output HDMI-A-1 Geometry: 1920x1080 @ 1920,0 Scale factor: 1x Workspace: 5:五 ```
Diffstat (limited to 'sway/ipc-json.c')
-rw-r--r--sway/ipc-json.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/sway/ipc-json.c b/sway/ipc-json.c
index 6bd5204c..fba489d7 100644
--- a/sway/ipc-json.c
+++ b/sway/ipc-json.c
@@ -2,7 +2,9 @@
2#include <ctype.h> 2#include <ctype.h>
3#include <string.h> 3#include <string.h>
4#include <stdint.h> 4#include <stdint.h>
5#include <libinput.h>
5#include "sway/container.h" 6#include "sway/container.h"
7#include "sway/input.h"
6#include "sway/ipc-json.h" 8#include "sway/ipc-json.h"
7#include "util.h" 9#include "util.h"
8 10
@@ -242,6 +244,65 @@ json_object *ipc_json_describe_container(swayc_t *c) {
242 return object; 244 return object;
243} 245}
244 246
247json_object *ipc_json_describe_input(struct libinput_device *device) {
248 char* identifier = libinput_dev_unique_id(device);
249 int vendor = libinput_device_get_id_vendor(device);
250 int product = libinput_device_get_id_product(device);
251 const char *name = libinput_device_get_name(device);
252 double width = -1, height = -1;
253 int has_size = libinput_device_get_size(device, &width, &height);
254
255 json_object *device_object = json_object_new_object();
256 json_object_object_add(device_object,"identifier",
257 identifier ? json_object_new_string(identifier) : NULL);
258 json_object_object_add(device_object,
259 "vendor", json_object_new_int(vendor));
260 json_object_object_add(device_object,
261 "product", json_object_new_int(product));
262 json_object_object_add(device_object,
263 "name", json_object_new_string(name));
264 if (has_size == 0) {
265 json_object *size_object = json_object_new_object();
266 json_object_object_add(size_object,
267 "width", json_object_new_double(width));
268 json_object_object_add(size_object,
269 "height", json_object_new_double(height));
270 } else {
271 json_object_object_add(device_object, "size", NULL);
272 }
273
274 struct {
275 enum libinput_device_capability cap;
276 const char *name;
277 // If anyone feels like implementing device-specific IPC output,
278 // be my guest
279 json_object *(*describe)(struct libinput_device *);
280 } caps[] = {
281 { LIBINPUT_DEVICE_CAP_KEYBOARD, "keyboard", NULL },
282 { LIBINPUT_DEVICE_CAP_POINTER, "pointer", NULL },
283 { LIBINPUT_DEVICE_CAP_TOUCH, "touch", NULL },
284 { LIBINPUT_DEVICE_CAP_TABLET_TOOL, "tablet_tool", NULL },
285 { LIBINPUT_DEVICE_CAP_TABLET_PAD, "tablet_pad", NULL },
286 { LIBINPUT_DEVICE_CAP_GESTURE, "gesture", NULL },
287 { LIBINPUT_DEVICE_CAP_SWITCH, "switch", NULL }
288 };
289
290 json_object *_caps = json_object_new_array();
291 for (size_t i = 0; i < sizeof(caps) / sizeof(caps[0]); ++i) {
292 if (libinput_device_has_capability(device, caps[i].cap)) {
293 json_object_array_add(_caps, json_object_new_string(caps[i].name));
294 if (caps[i].describe) {
295 json_object *desc = caps[i].describe(device);
296 json_object_object_add(device_object, caps[i].name, desc);
297 }
298 }
299 }
300 json_object_object_add(device_object, "capabilities", _caps);
301
302 free(identifier);
303 return device_object;
304}
305
245json_object *ipc_json_get_version() { 306json_object *ipc_json_get_version() {
246 json_object *version = json_object_new_object(); 307 json_object *version = json_object_new_object();
247 308