Skip to content

Commit

Permalink
Implement input_cmd_xkb_file (swaywm#3999)
Browse files Browse the repository at this point in the history
Adds a new commend "xkb_file", which constructs the internal
xkb_keymap from a xkb file rather than an RMLVO configuration.
This allows greater flexibility when specifying xkb configurations.
An xkb file can be dumped with the xkbcomp program.
  • Loading branch information
edyounis committed Jul 14, 2019
1 parent c312a10 commit d1b1178
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 9 deletions.
1 change: 1 addition & 0 deletions include/sway/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ sway_cmd input_cmd_scroll_method;
sway_cmd input_cmd_tap;
sway_cmd input_cmd_tap_button_map;
sway_cmd input_cmd_xkb_capslock;
sway_cmd input_cmd_xkb_file;
sway_cmd input_cmd_xkb_layout;
sway_cmd input_cmd_xkb_model;
sway_cmd input_cmd_xkb_numlock;
Expand Down
1 change: 1 addition & 0 deletions include/sway/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ struct input_config {
char *xkb_options;
char *xkb_rules;
char *xkb_variant;
char *xkb_file;

int xkb_numlock;
int xkb_capslock;
Expand Down
5 changes: 3 additions & 2 deletions sway/commands/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static struct cmd_handler input_handlers[] = {
{ "scroll_method", input_cmd_scroll_method },
{ "tap", input_cmd_tap },
{ "tap_button_map", input_cmd_tap_button_map },
{ "xkb_file", input_cmd_xkb_file },
{ "xkb_layout", input_cmd_xkb_layout },
{ "xkb_model", input_cmd_xkb_model },
{ "xkb_options", input_cmd_xkb_options },
Expand All @@ -48,8 +49,8 @@ static struct cmd_handler input_config_handlers[] = {
static void retranslate_keysyms(struct input_config *input_config) {
for (int i = 0; i < config->input_configs->length; ++i) {
struct input_config *ic = config->input_configs->items[i];
if (ic->xkb_layout) {
// this is the first config with xkb_layout
if (ic->xkb_layout || ic->xkb_file) {
// this is the first config with xkb_layout or xkb_file
if (ic->identifier == input_config->identifier) {
translate_keysyms(ic);
}
Expand Down
26 changes: 26 additions & 0 deletions sway/commands/input/xkb_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#define _POSIX_C_SOURCE 200809L
#include "sway/config.h"
#include "sway/commands.h"
#include "sway/input/input-manager.h"
#include "log.h"

struct cmd_results *input_cmd_xkb_file(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "xkb_file", 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 (strcmp(argv[0], "-") == 0) {
ic->xkb_file = NULL;
} else {
ic->xkb_file = strdup(argv[0]);
}

sway_log(SWAY_DEBUG, "set-xkb_file for config: %s file: %s",
ic->identifier, ic->xkb_file);
return cmd_results_new(CMD_SUCCESS, NULL);
}
5 changes: 5 additions & 0 deletions sway/config/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ void merge_input_config(struct input_config *dst, struct input_config *src) {
if (src->tap_button_map != INT_MIN) {
dst->tap_button_map = src->tap_button_map;
}
if (src->xkb_file) {
free(dst->xkb_file);
dst->xkb_file = strdup(src->xkb_file);
}
if (src->xkb_layout) {
free(dst->xkb_layout);
dst->xkb_layout = strdup(src->xkb_layout);
Expand Down Expand Up @@ -307,6 +311,7 @@ void free_input_config(struct input_config *ic) {
return;
}
free(ic->identifier);
free(ic->xkb_file);
free(ic->xkb_layout);
free(ic->xkb_model);
free(ic->xkb_options);
Expand Down
30 changes: 23 additions & 7 deletions sway/input/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,20 +534,36 @@ static void handle_xkb_context_log(struct xkb_context *context,

struct xkb_keymap *sway_keyboard_compile_keymap(struct input_config *ic,
char **error) {
struct xkb_rule_names rules = {0};
if (ic) {
input_config_fill_rule_names(ic, &rules);
}

struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (!sway_assert(context, "cannot create XKB context")) {
return NULL;
}
xkb_context_set_user_data(context, error);
xkb_context_set_log_fn(context, handle_xkb_context_log);

struct xkb_keymap *keymap =
xkb_keymap_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS);
struct xkb_keymap *keymap = NULL;

if (ic && ic->xkb_file) {
FILE *keymap_file = fopen(ic->xkb_file, "r");
if (!keymap_file) {
sway_log_errno(SWAY_ERROR, "cannot open XKB file");
}

keymap = xkb_keymap_new_from_file(context, keymap_file,
XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);

if (!fclose(keymap_file)) {
sway_log_errno(SWAY_ERROR, "cannot close XKB file");
}
} else {
struct xkb_rule_names rules = {0};
if (ic) {
input_config_fill_rule_names(ic, &rules);
}
keymap = xkb_keymap_new_from_names(context, &rules,
XKB_KEYMAP_COMPILE_NO_FLAGS);
}

xkb_context_set_user_data(context, NULL);
xkb_context_unref(context);
return keymap;
Expand Down
1 change: 1 addition & 0 deletions sway/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ sway_sources = files(
'commands/input/tap.c',
'commands/input/tap_button_map.c',
'commands/input/xkb_capslock.c',
'commands/input/xkb_file.c',
'commands/input/xkb_layout.c',
'commands/input/xkb_model.c',
'commands/input/xkb_numlock.c',
Expand Down
5 changes: 5 additions & 0 deletions sway/sway-input.5.scd
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ on top of the existing device configurations.
For more information on these xkb configuration options, see
*xkeyboard-config*(7).

*input* <identifier> xkb_file <file_name>
Sets all xkb configurations from a complete .xkb file. This file can be
dumped from _xkbcomp $DISPLAY keymap.xkb_. This setting overrides
xkb_layout, xkb_model, xkb_options, xkb_rules, and xkb_variant settings.

*input* <identifier> xkb_layout <layout_name>
Sets the layout of the keyboard like _us_ or _de_.

Expand Down

0 comments on commit d1b1178

Please sign in to comment.