aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Simon Ser <contact@emersion.fr>2019-06-09 21:17:28 +0300
committerLibravatar Drew DeVault <sir@cmpwn.com>2019-06-09 15:20:14 -0400
commit2bccb387d81298ffd3b88a193deb3e32c0b6c494 (patch)
treeeda04f72b85e4b3251fc18336b925c67a9678b7c
parentipc: add xkb_layout_names and xkb_active_layout_index (diff)
downloadsway-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.
-rw-r--r--include/sway/commands.h1
-rw-r--r--sway/commands/input.c4
-rw-r--r--sway/commands/input/xkb_switch_layout.c51
-rw-r--r--sway/meson.build1
4 files changed, 56 insertions, 1 deletions
diff --git a/include/sway/commands.h b/include/sway/commands.h
index 927296b0..e0cd94d1 100644
--- a/include/sway/commands.h
+++ b/include/sway/commands.h
@@ -260,6 +260,7 @@ sway_cmd input_cmd_xkb_model;
260sway_cmd input_cmd_xkb_numlock; 260sway_cmd input_cmd_xkb_numlock;
261sway_cmd input_cmd_xkb_options; 261sway_cmd input_cmd_xkb_options;
262sway_cmd input_cmd_xkb_rules; 262sway_cmd input_cmd_xkb_rules;
263sway_cmd input_cmd_xkb_switch_layout;
263sway_cmd input_cmd_xkb_variant; 264sway_cmd input_cmd_xkb_variant;
264 265
265sway_cmd output_cmd_background; 266sway_cmd output_cmd_background;
diff --git a/sway/commands/input.c b/sway/commands/input.c
index d95c4baf..08af3cb2 100644
--- a/sway/commands/input.c
+++ b/sway/commands/input.c
@@ -31,6 +31,7 @@ static struct cmd_handler input_handlers[] = {
31 { "xkb_model", input_cmd_xkb_model }, 31 { "xkb_model", input_cmd_xkb_model },
32 { "xkb_options", input_cmd_xkb_options }, 32 { "xkb_options", input_cmd_xkb_options },
33 { "xkb_rules", input_cmd_xkb_rules }, 33 { "xkb_rules", input_cmd_xkb_rules },
34 { "xkb_switch_layout", input_cmd_xkb_switch_layout },
34 { "xkb_variant", input_cmd_xkb_variant }, 35 { "xkb_variant", input_cmd_xkb_variant },
35}; 36};
36 37
@@ -86,7 +87,8 @@ struct cmd_results *cmd_input(int argc, char **argv) {
86 input_handlers, sizeof(input_handlers)); 87 input_handlers, sizeof(input_handlers));
87 } 88 }
88 89
89 if (!res || res->status == CMD_SUCCESS) { 90 if ((!res || res->status == CMD_SUCCESS) &&
91 strcmp(argv[1], "xkb_switch_layout") != 0) {
90 char *error = NULL; 92 char *error = NULL;
91 struct input_config *ic = 93 struct input_config *ic =
92 store_input_config(config->handler_context.input_config, &error); 94 store_input_config(config->handler_context.input_config, &error);
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
7static 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
16struct 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}
diff --git a/sway/meson.build b/sway/meson.build
index 262d7057..99dab7e7 100644
--- a/sway/meson.build
+++ b/sway/meson.build
@@ -164,6 +164,7 @@ sway_sources = files(
164 'commands/input/xkb_numlock.c', 164 'commands/input/xkb_numlock.c',
165 'commands/input/xkb_options.c', 165 'commands/input/xkb_options.c',
166 'commands/input/xkb_rules.c', 166 'commands/input/xkb_rules.c',
167 'commands/input/xkb_switch_layout.c',
167 'commands/input/xkb_variant.c', 168 'commands/input/xkb_variant.c',
168 169
169 'commands/output/background.c', 170 'commands/output/background.c',