Add a new xkb_switch_layout command

This allows users to programatically change the active layout.
master
Simon Ser 6 years ago committed by Drew DeVault
parent 84e4061bf4
commit 2bccb387d8

@ -260,6 +260,7 @@ sway_cmd input_cmd_xkb_model;
sway_cmd input_cmd_xkb_numlock; sway_cmd input_cmd_xkb_numlock;
sway_cmd input_cmd_xkb_options; sway_cmd input_cmd_xkb_options;
sway_cmd input_cmd_xkb_rules; sway_cmd input_cmd_xkb_rules;
sway_cmd input_cmd_xkb_switch_layout;
sway_cmd input_cmd_xkb_variant; sway_cmd input_cmd_xkb_variant;
sway_cmd output_cmd_background; sway_cmd output_cmd_background;

@ -31,6 +31,7 @@ static struct cmd_handler input_handlers[] = {
{ "xkb_model", input_cmd_xkb_model }, { "xkb_model", input_cmd_xkb_model },
{ "xkb_options", input_cmd_xkb_options }, { "xkb_options", input_cmd_xkb_options },
{ "xkb_rules", input_cmd_xkb_rules }, { "xkb_rules", input_cmd_xkb_rules },
{ "xkb_switch_layout", input_cmd_xkb_switch_layout },
{ "xkb_variant", input_cmd_xkb_variant }, { "xkb_variant", input_cmd_xkb_variant },
}; };
@ -86,7 +87,8 @@ struct cmd_results *cmd_input(int argc, char **argv) {
input_handlers, sizeof(input_handlers)); 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; char *error = NULL;
struct input_config *ic = struct input_config *ic =
store_input_config(config->handler_context.input_config, &error); store_input_config(config->handler_context.input_config, &error);

@ -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);
}

@ -164,6 +164,7 @@ sway_sources = files(
'commands/input/xkb_numlock.c', 'commands/input/xkb_numlock.c',
'commands/input/xkb_options.c', 'commands/input/xkb_options.c',
'commands/input/xkb_rules.c', 'commands/input/xkb_rules.c',
'commands/input/xkb_switch_layout.c',
'commands/input/xkb_variant.c', 'commands/input/xkb_variant.c',
'commands/output/background.c', 'commands/output/background.c',

Loading…
Cancel
Save