diff options
author | Simon Ser <contact@emersion.fr> | 2019-06-09 21:17:28 +0300 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2019-06-09 15:20:14 -0400 |
commit | 2bccb387d81298ffd3b88a193deb3e32c0b6c494 (patch) | |
tree | eda04f72b85e4b3251fc18336b925c67a9678b7c /sway/commands/input/xkb_switch_layout.c | |
parent | ipc: add xkb_layout_names and xkb_active_layout_index (diff) | |
download | sway-2bccb387d81298ffd3b88a193deb3e32c0b6c494.tar.gz sway-2bccb387d81298ffd3b88a193deb3e32c0b6c494.tar.zst sway-2bccb387d81298ffd3b88a193deb3e32c0b6c494.zip |
Add a new xkb_switch_layout command
This allows users to programatically change the active layout.
Diffstat (limited to 'sway/commands/input/xkb_switch_layout.c')
-rw-r--r-- | sway/commands/input/xkb_switch_layout.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/sway/commands/input/xkb_switch_layout.c b/sway/commands/input/xkb_switch_layout.c new file mode 100644 index 00000000..fdf21452 --- /dev/null +++ b/sway/commands/input/xkb_switch_layout.c | |||
@@ -0,0 +1,51 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | ||
2 | #include "sway/config.h" | ||
3 | #include "sway/commands.h" | ||
4 | #include "sway/input/input-manager.h" | ||
5 | #include "log.h" | ||
6 | |||
7 | static void switch_layout(struct wlr_keyboard *kbd, xkb_layout_index_t idx) { | ||
8 | xkb_layout_index_t num_layouts = xkb_keymap_num_layouts(kbd->keymap); | ||
9 | if (idx >= num_layouts) { | ||
10 | return; | ||
11 | } | ||
12 | wlr_keyboard_notify_modifiers(kbd, kbd->modifiers.depressed, | ||
13 | kbd->modifiers.latched, kbd->modifiers.locked, idx); | ||
14 | } | ||
15 | |||
16 | struct cmd_results *input_cmd_xkb_switch_layout(int argc, char **argv) { | ||
17 | struct cmd_results *error = NULL; | ||
18 | if ((error = checkarg(argc, "xkb_switch_layout", EXPECTED_EQUAL_TO, 1))) { | ||
19 | return error; | ||
20 | } | ||
21 | struct input_config *ic = config->handler_context.input_config; | ||
22 | if (!ic) { | ||
23 | return cmd_results_new(CMD_FAILURE, "No input device defined."); | ||
24 | } | ||
25 | |||
26 | if (config->reading || !config->active) { | ||
27 | return cmd_results_new(CMD_DEFER, NULL); | ||
28 | } | ||
29 | |||
30 | const char *layout_str = argv[0]; | ||
31 | |||
32 | char *end; | ||
33 | int layout = strtol(layout_str, &end, 10); | ||
34 | if (layout_str[0] == '\0' || end[0] != '\0' || layout < 0) { | ||
35 | return cmd_results_new(CMD_FAILURE, "Invalid layout index."); | ||
36 | } | ||
37 | |||
38 | struct sway_input_device *dev; | ||
39 | wl_list_for_each(dev, &server.input->devices, link) { | ||
40 | if (strcmp(ic->identifier, "*") != 0 && | ||
41 | strcmp(ic->identifier, dev->identifier) != 0) { | ||
42 | continue; | ||
43 | } | ||
44 | if (dev->wlr_device->type != WLR_INPUT_DEVICE_KEYBOARD) { | ||
45 | continue; | ||
46 | } | ||
47 | switch_layout(dev->wlr_device->keyboard, layout); | ||
48 | } | ||
49 | |||
50 | return cmd_results_new(CMD_SUCCESS, NULL); | ||
51 | } | ||