aboutsummaryrefslogtreecommitdiff
path: root/sway/commands
diff options
context:
space:
mode:
Diffstat (limited to 'sway/commands')
-rw-r--r--sway/commands/input.c4
-rw-r--r--sway/commands/input/xkb_switch_layout.c51
2 files changed, 54 insertions, 1 deletions
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[] = {
{ "xkb_model", input_cmd_xkb_model },
{ "xkb_options", input_cmd_xkb_options },
{ "xkb_rules", input_cmd_xkb_rules },
+ { "xkb_switch_layout", input_cmd_xkb_switch_layout },
{ "xkb_variant", input_cmd_xkb_variant },
};
@@ -86,7 +87,8 @@ struct cmd_results *cmd_input(int argc, char **argv) {
input_handlers, sizeof(input_handlers));
}
- if (!res || res->status == CMD_SUCCESS) {
+ if ((!res || res->status == CMD_SUCCESS) &&
+ strcmp(argv[1], "xkb_switch_layout") != 0) {
char *error = NULL;
struct input_config *ic =
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 @@
+#define _POSIX_C_SOURCE 200809L
+#include "sway/config.h"
+#include "sway/commands.h"
+#include "sway/input/input-manager.h"
+#include "log.h"
+
+static void switch_layout(struct wlr_keyboard *kbd, xkb_layout_index_t idx) {
+ xkb_layout_index_t num_layouts = xkb_keymap_num_layouts(kbd->keymap);
+ if (idx >= num_layouts) {
+ return;
+ }
+ wlr_keyboard_notify_modifiers(kbd, kbd->modifiers.depressed,
+ kbd->modifiers.latched, kbd->modifiers.locked, idx);
+}
+
+struct cmd_results *input_cmd_xkb_switch_layout(int argc, char **argv) {
+ struct cmd_results *error = NULL;
+ if ((error = checkarg(argc, "xkb_switch_layout", EXPECTED_EQUAL_TO, 1))) {
+ return error;
+ }
+ struct input_config *ic = config->handler_context.input_config;
+ if (!ic) {
+ return cmd_results_new(CMD_FAILURE, "No input device defined.");
+ }
+
+ if (config->reading || !config->active) {
+ return cmd_results_new(CMD_DEFER, NULL);
+ }
+
+ const char *layout_str = argv[0];
+
+ char *end;
+ int layout = strtol(layout_str, &end, 10);
+ if (layout_str[0] == '\0' || end[0] != '\0' || layout < 0) {
+ return cmd_results_new(CMD_FAILURE, "Invalid layout index.");
+ }
+
+ struct sway_input_device *dev;
+ wl_list_for_each(dev, &server.input->devices, link) {
+ if (strcmp(ic->identifier, "*") != 0 &&
+ strcmp(ic->identifier, dev->identifier) != 0) {
+ continue;
+ }
+ if (dev->wlr_device->type != WLR_INPUT_DEVICE_KEYBOARD) {
+ continue;
+ }
+ switch_layout(dev->wlr_device->keyboard, layout);
+ }
+
+ return cmd_results_new(CMD_SUCCESS, NULL);
+}