aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Ryan Walklin <ryan@testtoast.com>2019-03-20 14:47:29 +1100
committerLibravatar Brian Ashworth <bosrsf04@gmail.com>2019-03-19 23:58:47 -0400
commitbdb402404cd6d54242b0b1dc2360cfc5679e52f2 (patch)
tree5a355e025c24b3de0bc69db4b8cc9d002bbd1167
parentClean up focus follows mouse logic (diff)
downloadsway-bdb402404cd6d54242b0b1dc2360cfc5679e52f2.tar.gz
sway-bdb402404cd6d54242b0b1dc2360cfc5679e52f2.tar.zst
sway-bdb402404cd6d54242b0b1dc2360cfc5679e52f2.zip
Support WLR_INPUT_DEVICE_SWITCH in sway
This commit adds support for laptop lid and tablet mode switches as provided by evdev/libinput and handled by wlroots. Adds a new bindswitch command with syntax: bindswitch <switch>:<state> <command> Where <switch> is one of: tablet for WLR_SWITCH_TYPE_TABLET_MODE lid for WLR_SWITCH_TYPE_LID <state> is one of: on for WLR_SWITCH_STATE_ON off for WLR_SWITCH_STATE_OFF toggle for WLR_SWITCH_STATE_TOGGLE (Note that WLR_SWITCH_STATE_TOGGLE doesn't map to libinput and will trigger at both on and off events)
-rw-r--r--include/sway/commands.h1
-rw-r--r--include/sway/config.h15
-rw-r--r--include/sway/input/seat.h1
-rw-r--r--include/sway/input/switch.h19
-rw-r--r--sway/commands.c2
-rw-r--r--sway/commands/bind.c118
-rw-r--r--sway/commands/mode.c2
-rw-r--r--sway/config.c7
-rw-r--r--sway/input/input-manager.c47
-rw-r--r--sway/input/seat.c18
-rw-r--r--sway/input/switch.c85
-rw-r--r--sway/ipc-json.c4
-rw-r--r--sway/meson.build1
-rw-r--r--sway/sway.5.scd28
14 files changed, 339 insertions, 9 deletions
diff --git a/include/sway/commands.h b/include/sway/commands.h
index 764821a0..1c147c5a 100644
--- a/include/sway/commands.h
+++ b/include/sway/commands.h
@@ -101,6 +101,7 @@ struct sway_container *container_find_resize_parent(struct sway_container *con,
101sway_cmd cmd_assign; 101sway_cmd cmd_assign;
102sway_cmd cmd_bar; 102sway_cmd cmd_bar;
103sway_cmd cmd_bindcode; 103sway_cmd cmd_bindcode;
104sway_cmd cmd_bindswitch;
104sway_cmd cmd_bindsym; 105sway_cmd cmd_bindsym;
105sway_cmd cmd_border; 106sway_cmd cmd_border;
106sway_cmd cmd_client_noop; 107sway_cmd cmd_client_noop;
diff --git a/include/sway/config.h b/include/sway/config.h
index 7c544541..8970696c 100644
--- a/include/sway/config.h
+++ b/include/sway/config.h
@@ -4,6 +4,7 @@
4#include <stdint.h> 4#include <stdint.h>
5#include <string.h> 5#include <string.h>
6#include <time.h> 6#include <time.h>
7#include <wlr/interfaces/wlr_switch.h>
7#include <wlr/types/wlr_box.h> 8#include <wlr/types/wlr_box.h>
8#include <xkbcommon/xkbcommon.h> 9#include <xkbcommon/xkbcommon.h>
9#include "../include/config.h" 10#include "../include/config.h"
@@ -29,6 +30,7 @@ enum binding_input_type {
29 BINDING_KEYSYM, 30 BINDING_KEYSYM,
30 BINDING_MOUSECODE, 31 BINDING_MOUSECODE,
31 BINDING_MOUSESYM, 32 BINDING_MOUSESYM,
33 BINDING_SWITCH
32}; 34};
33 35
34enum binding_flags { 36enum binding_flags {
@@ -61,6 +63,16 @@ struct sway_mouse_binding {
61}; 63};
62 64
63/** 65/**
66 * A laptop switch binding and an associated command.
67 */
68struct sway_switch_binding {
69 enum wlr_switch_type type;
70 enum wlr_switch_state state;
71 uint32_t flags;
72 char *command;
73};
74
75/**
64 * Focus on window activation. 76 * Focus on window activation.
65 */ 77 */
66enum sway_fowa { 78enum sway_fowa {
@@ -78,6 +90,7 @@ struct sway_mode {
78 list_t *keysym_bindings; 90 list_t *keysym_bindings;
79 list_t *keycode_bindings; 91 list_t *keycode_bindings;
80 list_t *mouse_bindings; 92 list_t *mouse_bindings;
93 list_t *switch_bindings;
81 bool pango; 94 bool pango;
82}; 95};
83 96
@@ -603,6 +616,8 @@ int sway_binding_cmp_keys(const void *a, const void *b);
603 616
604void free_sway_binding(struct sway_binding *sb); 617void free_sway_binding(struct sway_binding *sb);
605 618
619void free_switch_binding(struct sway_switch_binding *binding);
620
606void seat_execute_command(struct sway_seat *seat, struct sway_binding *binding); 621void seat_execute_command(struct sway_seat *seat, struct sway_binding *binding);
607 622
608void load_swaybar(struct bar_config *bar); 623void load_swaybar(struct bar_config *bar);
diff --git a/include/sway/input/seat.h b/include/sway/input/seat.h
index a5361e8c..f2af1066 100644
--- a/include/sway/input/seat.h
+++ b/include/sway/input/seat.h
@@ -27,6 +27,7 @@ struct sway_seat_device {
27 struct sway_seat *sway_seat; 27 struct sway_seat *sway_seat;
28 struct sway_input_device *input_device; 28 struct sway_input_device *input_device;
29 struct sway_keyboard *keyboard; 29 struct sway_keyboard *keyboard;
30 struct sway_switch *switch_device;
30 struct wl_list link; // sway_seat::devices 31 struct wl_list link; // sway_seat::devices
31}; 32};
32 33
diff --git a/include/sway/input/switch.h b/include/sway/input/switch.h
new file mode 100644
index 00000000..19bb1e77
--- /dev/null
+++ b/include/sway/input/switch.h
@@ -0,0 +1,19 @@
1#ifndef _SWAY_INPUT_SWITCH_H
2#define _SWAY_INPUT_SWITCH_H
3
4#include "sway/input/seat.h"
5
6struct sway_switch {
7 struct sway_seat_device *seat_device;
8
9 struct wl_listener switch_toggle;
10};
11
12struct sway_switch *sway_switch_create(struct sway_seat *seat,
13 struct sway_seat_device *device);
14
15void sway_switch_configure(struct sway_switch *sway_switch);
16
17void sway_switch_destroy(struct sway_switch *sway_switch);
18
19#endif
diff --git a/sway/commands.c b/sway/commands.c
index cb32169f..18b95c73 100644
--- a/sway/commands.c
+++ b/sway/commands.c
@@ -47,6 +47,7 @@ static struct cmd_handler handlers[] = {
47 { "assign", cmd_assign }, 47 { "assign", cmd_assign },
48 { "bar", cmd_bar }, 48 { "bar", cmd_bar },
49 { "bindcode", cmd_bindcode }, 49 { "bindcode", cmd_bindcode },
50 { "bindswitch", cmd_bindswitch },
50 { "bindsym", cmd_bindsym }, 51 { "bindsym", cmd_bindsym },
51 { "client.background", cmd_client_noop }, 52 { "client.background", cmd_client_noop },
52 { "client.focused", cmd_client_focused }, 53 { "client.focused", cmd_client_focused },
@@ -386,6 +387,7 @@ struct cmd_results *config_command(char *exec, char **new_block) {
386 && handler->handle != cmd_mode 387 && handler->handle != cmd_mode
387 && handler->handle != cmd_bindsym 388 && handler->handle != cmd_bindsym
388 && handler->handle != cmd_bindcode 389 && handler->handle != cmd_bindcode
390 && handler->handle != cmd_bindswitch
389 && handler->handle != cmd_set 391 && handler->handle != cmd_set
390 && handler->handle != cmd_for_window 392 && handler->handle != cmd_for_window
391 && (*argv[i] == '\"' || *argv[i] == '\'')) { 393 && (*argv[i] == '\"' || *argv[i] == '\'')) {
diff --git a/sway/commands/bind.c b/sway/commands/bind.c
index 172e6b8a..b3d391da 100644
--- a/sway/commands/bind.c
+++ b/sway/commands/bind.c
@@ -29,6 +29,28 @@ void free_sway_binding(struct sway_binding *binding) {
29 free(binding); 29 free(binding);
30} 30}
31 31
32void free_switch_binding(struct sway_switch_binding *binding) {
33 if (!binding) {
34 return;
35 }
36 free(binding->command);
37 free(binding);
38}
39
40/**
41 * Returns true if the bindings have the same switch type and state combinations.
42 */
43static bool binding_switch_compare(struct sway_switch_binding *binding_a,
44 struct sway_switch_binding *binding_b) {
45 if (binding_a->type != binding_b->type) {
46 return false;
47 }
48 if (binding_a->state != binding_b->state) {
49 return false;
50 }
51 return true;
52}
53
32/** 54/**
33 * Returns true if the bindings have the same key and modifier combinations. 55 * Returns true if the bindings have the same key and modifier combinations.
34 * Note that keyboard layout is not considered, so the bindings might actually 56 * Note that keyboard layout is not considered, so the bindings might actually
@@ -325,6 +347,102 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) {
325 return cmd_bindsym_or_bindcode(argc, argv, true); 347 return cmd_bindsym_or_bindcode(argc, argv, true);
326} 348}
327 349
350struct cmd_results *cmd_bindswitch(int argc, char **argv) {
351
352 struct cmd_results *error = NULL;
353 if ((error = checkarg(argc, "bindswitch", EXPECTED_AT_LEAST, 2))) {
354 return error;
355 }
356 struct sway_switch_binding *binding = calloc(1, sizeof(struct sway_switch_binding));
357 if (!binding) {
358 return cmd_results_new(CMD_FAILURE, "Unable to allocate binding");
359 }
360
361 bool warn = true;
362
363 // Handle flags
364 while (argc > 0) {
365 if (strcmp("--locked", argv[0]) == 0) {
366 binding->flags |= BINDING_LOCKED;
367 } else if (strcmp("--no-warn", argv[0]) == 0) {
368 warn = false;
369 } else {
370 break;
371 }
372 argv++;
373 argc--;
374 }
375
376 if (argc < 2) {
377 free(binding);
378 return cmd_results_new(CMD_FAILURE,
379 "Invalid bindswitch command (expected at least 2 "
380 "non-option arguments, got %d)", argc);
381 }
382 binding->command = join_args(argv + 1, argc - 1);
383
384 list_t *split = split_string(argv[0], ":");
385 if (split->length != 2) {
386 free_switch_binding(binding);
387 return cmd_results_new(CMD_FAILURE,
388 "Invalid bindswitch command (expected two arguments with "
389 "format <switch>:<state> <action>, got %d)", argc);
390 }
391 if (strcmp(split->items[0], "tablet") == 0) {
392 binding->type = WLR_SWITCH_TYPE_TABLET_MODE;
393 } else if (strcmp(split->items[0], "lid") == 0) {
394 binding->type = WLR_SWITCH_TYPE_LID;
395 } else {
396 free_switch_binding(binding);
397 return cmd_results_new(CMD_FAILURE,
398 "Invalid bindswitch command (expected switch binding: "
399 "unknown switch %s)", split->items[0]);
400 }
401 if (strcmp(split->items[1], "on") == 0) {
402 binding->state = WLR_SWITCH_STATE_ON;
403 } else if (strcmp(split->items[1], "off") == 0) {
404 binding->state = WLR_SWITCH_STATE_OFF;
405 } else if (strcmp(split->items[1], "toggle") == 0) {
406 binding->state = WLR_SWITCH_STATE_TOGGLE;
407 } else {
408 free_switch_binding(binding);
409 return cmd_results_new(CMD_FAILURE,
410 "Invalid bindswitch command "
411 "(expected switch state: unknown state %d)",
412 split->items[0]);
413 }
414 list_free_items_and_destroy(split);
415
416 list_t *mode_bindings = config->current_mode->switch_bindings;
417
418 // overwrite the binding if it already exists
419 bool overwritten = false;
420 for (int i = 0; i < mode_bindings->length; ++i) {
421 struct sway_switch_binding *config_binding = mode_bindings->items[i];
422 if (binding_switch_compare(binding, config_binding)) {
423 sway_log(SWAY_INFO, "Overwriting binding '%s' from `%s` to `%s`",
424 argv[0], binding->command, config_binding->command);
425 if (warn) {
426 config_add_swaynag_warning("Overwriting binding"
427 "'%s' to `%s` from `%s`",
428 argv[0], binding->command,
429 config_binding->command);
430 }
431 free_switch_binding(config_binding);
432 mode_bindings->items[i] = binding;
433 overwritten = true;
434 }
435 }
436
437 if (!overwritten) {
438 list_add(mode_bindings, binding);
439 }
440
441 sway_log(SWAY_DEBUG, "bindswitch - Bound %s to command `%s`",
442 argv[0], binding->command);
443 return cmd_results_new(CMD_SUCCESS, NULL);
444}
445
328/** 446/**
329 * Execute the command associated to a binding 447 * Execute the command associated to a binding
330 */ 448 */
diff --git a/sway/commands/mode.c b/sway/commands/mode.c
index d424eb1c..3e0ee80c 100644
--- a/sway/commands/mode.c
+++ b/sway/commands/mode.c
@@ -12,6 +12,7 @@
12// Must be in order for the bsearch 12// Must be in order for the bsearch
13static struct cmd_handler mode_handlers[] = { 13static struct cmd_handler mode_handlers[] = {
14 { "bindcode", cmd_bindcode }, 14 { "bindcode", cmd_bindcode },
15 { "bindswitch", cmd_bindswitch },
15 { "bindsym", cmd_bindsym } 16 { "bindsym", cmd_bindsym }
16}; 17};
17 18
@@ -54,6 +55,7 @@ struct cmd_results *cmd_mode(int argc, char **argv) {
54 mode->keysym_bindings = create_list(); 55 mode->keysym_bindings = create_list();
55 mode->keycode_bindings = create_list(); 56 mode->keycode_bindings = create_list();
56 mode->mouse_bindings = create_list(); 57 mode->mouse_bindings = create_list();
58 mode->switch_bindings = create_list();
57 mode->pango = pango; 59 mode->pango = pango;
58 list_add(config->modes, mode); 60 list_add(config->modes, mode);
59 } 61 }
diff --git a/sway/config.c b/sway/config.c
index 48bbd1ea..7104f55d 100644
--- a/sway/config.c
+++ b/sway/config.c
@@ -56,6 +56,12 @@ static void free_mode(struct sway_mode *mode) {
56 } 56 }
57 list_free(mode->mouse_bindings); 57 list_free(mode->mouse_bindings);
58 } 58 }
59 if (mode->switch_bindings) {
60 for (int i = 0; i < mode->switch_bindings->length; i++) {
61 free_switch_binding(mode->switch_bindings->items[i]);
62 }
63 list_free(mode->switch_bindings);
64 }
59 free(mode); 65 free(mode);
60} 66}
61 67
@@ -195,6 +201,7 @@ static void config_defaults(struct sway_config *config) {
195 if (!(config->current_mode->keysym_bindings = create_list())) goto cleanup; 201 if (!(config->current_mode->keysym_bindings = create_list())) goto cleanup;
196 if (!(config->current_mode->keycode_bindings = create_list())) goto cleanup; 202 if (!(config->current_mode->keycode_bindings = create_list())) goto cleanup;
197 if (!(config->current_mode->mouse_bindings = create_list())) goto cleanup; 203 if (!(config->current_mode->mouse_bindings = create_list())) goto cleanup;
204 if (!(config->current_mode->switch_bindings = create_list())) goto cleanup;
198 list_add(config->modes, config->current_mode); 205 list_add(config->modes, config->current_mode);
199 206
200 config->floating_mod = 0; 207 config->floating_mod = 0;
diff --git a/sway/input/input-manager.c b/sway/input/input-manager.c
index f99fc395..adb36af9 100644
--- a/sway/input/input-manager.c
+++ b/sway/input/input-manager.c
@@ -155,6 +155,47 @@ static void input_manager_libinput_reset_keyboard(
155 libinput_device, send_events)); 155 libinput_device, send_events));
156} 156}
157 157
158static void input_manager_libinput_config_switch(
159 struct sway_input_device *input_device) {
160 struct wlr_input_device *wlr_device = input_device->wlr_device;
161 struct input_config *ic = input_device_get_config(input_device);
162 struct libinput_device *libinput_device;
163
164 if (!ic || !wlr_input_device_is_libinput(wlr_device)) {
165 return;
166 }
167
168 libinput_device = wlr_libinput_get_device_handle(wlr_device);
169 sway_log(SWAY_DEBUG, "input_manager_libinput_config_switch(%s)",
170 ic->identifier);
171
172 if (ic->send_events != INT_MIN) {
173 sway_log(SWAY_DEBUG, "libinput_config_switch(%s) send_events_set_mode(%d)",
174 ic->identifier, ic->send_events);
175 log_libinput_config_status(libinput_device_config_send_events_set_mode(
176 libinput_device, ic->send_events));
177 }
178}
179
180static void input_manager_libinput_reset_switch(
181 struct sway_input_device *input_device) {
182 struct wlr_input_device *wlr_device = input_device->wlr_device;
183 struct libinput_device *libinput_device;
184
185 if (!wlr_input_device_is_libinput(wlr_device)) {
186 return;
187 }
188
189 libinput_device = wlr_libinput_get_device_handle(wlr_device);
190
191 uint32_t send_events =
192 libinput_device_config_send_events_get_default_mode(libinput_device);
193 sway_log(SWAY_DEBUG, "libinput_reset_switch(%s) send_events_set_mode(%d)",
194 input_device->identifier, send_events);
195 log_libinput_config_status(libinput_device_config_send_events_set_mode(
196 libinput_device, send_events));
197}
198
158static void input_manager_libinput_config_touch( 199static void input_manager_libinput_config_touch(
159 struct sway_input_device *input_device) { 200 struct sway_input_device *input_device) {
160 struct wlr_input_device *wlr_device = input_device->wlr_device; 201 struct wlr_input_device *wlr_device = input_device->wlr_device;
@@ -471,6 +512,8 @@ static void handle_new_input(struct wl_listener *listener, void *data) {
471 input_manager_libinput_config_pointer(input_device); 512 input_manager_libinput_config_pointer(input_device);
472 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) { 513 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) {
473 input_manager_libinput_config_keyboard(input_device); 514 input_manager_libinput_config_keyboard(input_device);
515 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_SWITCH) {
516 input_manager_libinput_config_switch(input_device);
474 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) { 517 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) {
475 input_manager_libinput_config_touch(input_device); 518 input_manager_libinput_config_touch(input_device);
476 } 519 }
@@ -624,6 +667,8 @@ void input_manager_apply_input_config(struct input_config *input_config) {
624 input_manager_libinput_config_pointer(input_device); 667 input_manager_libinput_config_pointer(input_device);
625 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) { 668 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) {
626 input_manager_libinput_config_keyboard(input_device); 669 input_manager_libinput_config_keyboard(input_device);
670 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_SWITCH) {
671 input_manager_libinput_config_switch(input_device);
627 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) { 672 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) {
628 input_manager_libinput_config_touch(input_device); 673 input_manager_libinput_config_touch(input_device);
629 } 674 }
@@ -642,6 +687,8 @@ void input_manager_reset_input(struct sway_input_device *input_device) {
642 input_manager_libinput_reset_pointer(input_device); 687 input_manager_libinput_reset_pointer(input_device);
643 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) { 688 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_KEYBOARD) {
644 input_manager_libinput_reset_keyboard(input_device); 689 input_manager_libinput_reset_keyboard(input_device);
690 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_SWITCH) {
691 input_manager_libinput_reset_switch(input_device);
645 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) { 692 } else if (input_device->wlr_device->type == WLR_INPUT_DEVICE_TOUCH) {
646 input_manager_libinput_reset_touch(input_device); 693 input_manager_libinput_reset_touch(input_device);
647 } 694 }
diff --git a/sway/input/seat.c b/sway/input/seat.c
index 7fc87ee4..d58ff9e6 100644
--- a/sway/input/seat.c
+++ b/sway/input/seat.c
@@ -15,6 +15,7 @@
15#include "sway/input/input-manager.h" 15#include "sway/input/input-manager.h"
16#include "sway/input/keyboard.h" 16#include "sway/input/keyboard.h"
17#include "sway/input/seat.h" 17#include "sway/input/seat.h"
18#include "sway/input/switch.h"
18#include "sway/ipc-server.h" 19#include "sway/ipc-server.h"
19#include "sway/layers.h" 20#include "sway/layers.h"
20#include "sway/output.h" 21#include "sway/output.h"
@@ -482,8 +483,8 @@ static void seat_update_capabilities(struct sway_seat *seat) {
482 case WLR_INPUT_DEVICE_TABLET_TOOL: 483 case WLR_INPUT_DEVICE_TABLET_TOOL:
483 caps |= WL_SEAT_CAPABILITY_POINTER; 484 caps |= WL_SEAT_CAPABILITY_POINTER;
484 break; 485 break;
485 case WLR_INPUT_DEVICE_TABLET_PAD:
486 case WLR_INPUT_DEVICE_SWITCH: 486 case WLR_INPUT_DEVICE_SWITCH:
487 case WLR_INPUT_DEVICE_TABLET_PAD:
487 break; 488 break;
488 } 489 }
489 } 490 }
@@ -570,6 +571,15 @@ static void seat_configure_keyboard(struct sway_seat *seat,
570 } 571 }
571} 572}
572 573
574static void seat_configure_switch(struct sway_seat *seat,
575 struct sway_seat_device *seat_device) {
576 if (!seat_device->switch_device) {
577 sway_switch_create(seat, seat_device);
578 }
579 seat_apply_input_config(seat, seat_device);
580 sway_switch_configure(seat_device->switch_device);
581}
582
573static void seat_configure_touch(struct sway_seat *seat, 583static void seat_configure_touch(struct sway_seat *seat,
574 struct sway_seat_device *sway_device) { 584 struct sway_seat_device *sway_device) {
575 wlr_cursor_attach_input_device(seat->cursor->cursor, 585 wlr_cursor_attach_input_device(seat->cursor->cursor,
@@ -611,6 +621,9 @@ void seat_configure_device(struct sway_seat *seat,
611 case WLR_INPUT_DEVICE_KEYBOARD: 621 case WLR_INPUT_DEVICE_KEYBOARD:
612 seat_configure_keyboard(seat, seat_device); 622 seat_configure_keyboard(seat, seat_device);
613 break; 623 break;
624 case WLR_INPUT_DEVICE_SWITCH:
625 seat_configure_switch(seat, seat_device);
626 break;
614 case WLR_INPUT_DEVICE_TOUCH: 627 case WLR_INPUT_DEVICE_TOUCH:
615 seat_configure_touch(seat, seat_device); 628 seat_configure_touch(seat, seat_device);
616 break; 629 break;
@@ -620,9 +633,6 @@ void seat_configure_device(struct sway_seat *seat,
620 case WLR_INPUT_DEVICE_TABLET_PAD: 633 case WLR_INPUT_DEVICE_TABLET_PAD:
621 sway_log(SWAY_DEBUG, "TODO: configure tablet pad"); 634 sway_log(SWAY_DEBUG, "TODO: configure tablet pad");
622 break; 635 break;
623 case WLR_INPUT_DEVICE_SWITCH:
624 sway_log(SWAY_DEBUG, "TODO: configure switch device");
625 break;
626 } 636 }
627} 637}
628 638
diff --git a/sway/input/switch.c b/sway/input/switch.c
new file mode 100644
index 00000000..d56e6525
--- /dev/null
+++ b/sway/input/switch.c
@@ -0,0 +1,85 @@
1#include "sway/config.h"
2#include "sway/desktop/transaction.h"
3#include "sway/input/switch.h"
4#include <wlr/types/wlr_idle.h>
5#include "log.h"
6
7struct sway_switch *sway_switch_create(struct sway_seat *seat,
8 struct sway_seat_device *device) {
9 struct sway_switch *switch_device =
10 calloc(1, sizeof(struct sway_switch));
11 if (!sway_assert(switch_device, "could not allocate switch")) {
12 return NULL;
13 }
14 device->switch_device = switch_device;
15 switch_device->seat_device = device;
16 wl_list_init(&switch_device->switch_toggle.link);
17 sway_log(SWAY_DEBUG, "Allocated switch for device");
18
19 return switch_device;
20}
21
22static void handle_switch_toggle(struct wl_listener *listener, void *data) {
23 struct sway_switch *sway_switch =
24 wl_container_of(listener, sway_switch, switch_toggle);
25 struct sway_seat* seat = sway_switch->seat_device->sway_seat;
26 struct wlr_seat *wlr_seat = seat->wlr_seat;
27 struct wlr_input_device *wlr_device =
28 sway_switch->seat_device->input_device->wlr_device;
29
30 wlr_idle_notify_activity(server.idle, wlr_seat);
31 bool input_inhibited = seat->exclusive_client != NULL;
32
33 char *device_identifier = input_device_get_identifier(wlr_device);
34
35 struct wlr_event_switch_toggle *event = data;
36 enum wlr_switch_type type = event->switch_type;
37 enum wlr_switch_state state = event->switch_state;
38 sway_log(SWAY_DEBUG, "%s: type %d state %d", device_identifier, type, state);
39
40 list_t *bindings = config->current_mode->switch_bindings;
41 for (int i = 0; i < bindings->length; ++i) {
42 struct sway_switch_binding *binding = bindings->items[i];
43 if (binding->type != type) {
44 continue;
45 }
46 if (binding->state != WLR_SWITCH_STATE_TOGGLE &&
47 binding->state != state) {
48 continue;
49 }
50 bool binding_locked = binding->flags & BINDING_LOCKED;
51 if (!binding_locked && input_inhibited) {
52 continue;
53 }
54
55 struct sway_binding *dummy_binding = calloc(1, sizeof(struct sway_binding));
56 dummy_binding->type = BINDING_SWITCH;
57 dummy_binding->flags = binding->flags;
58 dummy_binding->command = binding->command;
59
60 seat_execute_command(seat, dummy_binding);
61 free(dummy_binding);
62 }
63
64 transaction_commit_dirty();
65
66 free(device_identifier);
67}
68
69void sway_switch_configure(struct sway_switch *sway_switch) {
70 struct wlr_input_device *wlr_device =
71 sway_switch->seat_device->input_device->wlr_device;
72 wl_list_remove(&sway_switch->switch_toggle.link);
73 wl_signal_add(&wlr_device->switch_device->events.toggle,
74 &sway_switch->switch_toggle);
75 sway_switch->switch_toggle.notify = handle_switch_toggle;
76 sway_log(SWAY_DEBUG, "Configured switch for device");
77}
78
79void sway_switch_destroy(struct sway_switch *sway_switch) {
80 if (!sway_switch) {
81 return;
82 }
83 wl_list_remove(&sway_switch->switch_toggle.link);
84 free(sway_switch);
85}
diff --git a/sway/ipc-json.c b/sway/ipc-json.c
index f61e1a8c..c320d958 100644
--- a/sway/ipc-json.c
+++ b/sway/ipc-json.c
@@ -91,14 +91,14 @@ static const char *ipc_json_device_type_description(struct sway_input_device *de
91 return "pointer"; 91 return "pointer";
92 case WLR_INPUT_DEVICE_KEYBOARD: 92 case WLR_INPUT_DEVICE_KEYBOARD:
93 return "keyboard"; 93 return "keyboard";
94 case WLR_INPUT_DEVICE_SWITCH:
95 return "switch";
94 case WLR_INPUT_DEVICE_TOUCH: 96 case WLR_INPUT_DEVICE_TOUCH:
95 return "touch"; 97 return "touch";
96 case WLR_INPUT_DEVICE_TABLET_TOOL: 98 case WLR_INPUT_DEVICE_TABLET_TOOL:
97 return "tablet_tool"; 99 return "tablet_tool";
98 case WLR_INPUT_DEVICE_TABLET_PAD: 100 case WLR_INPUT_DEVICE_TABLET_PAD:
99 return "tablet_pad"; 101 return "tablet_pad";
100 case WLR_INPUT_DEVICE_SWITCH:
101 return "switch";
102 } 102 }
103 return "unknown"; 103 return "unknown";
104} 104}
diff --git a/sway/meson.build b/sway/meson.build
index 1138bc73..66876bdc 100644
--- a/sway/meson.build
+++ b/sway/meson.build
@@ -30,6 +30,7 @@ sway_sources = files(
30 'input/seatop_resize_tiling.c', 30 'input/seatop_resize_tiling.c',
31 'input/cursor.c', 31 'input/cursor.c',
32 'input/keyboard.c', 32 'input/keyboard.c',
33 'input/switch.c',
33 34
34 'config/bar.c', 35 'config/bar.c',
35 'config/output.c', 36 'config/output.c',
diff --git a/sway/sway.5.scd b/sway/sway.5.scd
index 989717cb..18fc28a3 100644
--- a/sway/sway.5.scd
+++ b/sway/sway.5.scd
@@ -341,6 +341,28 @@ runtime.
341 *bindcode* [--whole-window] [--border] [--exclude-titlebar] [--release] [--locked] [--input-device=<device>] [--no-warn] <code> <command> 341 *bindcode* [--whole-window] [--border] [--exclude-titlebar] [--release] [--locked] [--input-device=<device>] [--no-warn] <code> <command>
342 is also available for binding with key/button codes instead of key/button names. 342 is also available for binding with key/button codes instead of key/button names.
343 343
344*bindswitch* [--locked] [--no-warn] <switch>:<state> <command>
345 Binds <switch> to execute the sway command _command_ on state changes.
346 Supported switches are _lid_ (laptop lid) and _tablet_ (tablet mode)
347 switches. Valid values for _state_ are _on_, _off_ and _toggle. These
348 switches are on when the device lid is shut and when tablet mode is active
349 respectively. _toggle_ is also supported to run a command both when the
350 switch is toggled on or off.
351
352 Unless the flag _--locked_ is set, the command will not be run
353 when a screen locking program is active. By default, if you
354 overwrite a binding, swaynag will give you a warning. To silence this, use
355 the _--no-warn_ flag.
356
357 Example:
358```
359 # Show the virtual keyboard when tablet mode is entered.
360 bindswitch tablet:on busctl call --user sm.puri.OSK0 /sm/puri/OSK0 sm.puri.OSK0 SetVisible b true
361
362 # Log a message when the laptop lid is opened or closed.
363 bindswitch lid:toggle exec echo "Lid moved"
364```
365
344*client.<class>* <border> <background> <text> <indicator> <child_border> 366*client.<class>* <border> <background> <text> <indicator> <child_border>
345 Configures the color of window borders and title bars. All 5 colors are 367 Configures the color of window borders and title bars. All 5 colors are
346 required, with the exception of *client.background*, which requires exactly 368 required, with the exception of *client.background*, which requires exactly
@@ -551,9 +573,9 @@ The default colors are:
551 Switches to the specified mode. The default mode _default_. 573 Switches to the specified mode. The default mode _default_.
552 574
553*mode* [--pango_markup] <mode> <mode-subcommands...> 575*mode* [--pango_markup] <mode> <mode-subcommands...>
554 The only two valid _mode-subcommands..._ are *bindsym* and *bindcode*. 576 The only valid _mode-subcommands..._ are *bindsym*, *bindcode* and
555 If _--pango_markup_ is given, then _mode_ will be interpreted as pango 577 *bindswitch*. If _--pango_markup_ is given, then _mode_ will be interpreted
556 markup. 578 as pango markup.
557 579
558*mouse_warping* output|container|none 580*mouse_warping* output|container|none
559 If _output_ is specified, the mouse will be moved to new outputs as you 581 If _output_ is specified, the mouse will be moved to new outputs as you