Implement bindsym --to-code

* `bindsym --to-code` enables keysym to keycode translation.
* If there are no `xkb_layout` commands in the config file, the translation
uses the XKB_DEFAULT_LAYOUT value.
* It there is one or more `xkb_layout` command, the translation uses
the first one.
* If the translation is unsuccessful, a message is logged and the binding
is stored as BINDING_KEYSYM.
* The binding keysyms are stored and re-translated when a change in the input
configuration may affect the translated bindings.
This commit is contained in:
Konstantin Pospelov 2019-02-17 19:08:22 +03:00 committed by Simon Ser
parent bd32b300fb
commit a09c144b8b
4 changed files with 274 additions and 26 deletions

View file

@ -24,7 +24,6 @@ struct sway_variable {
char *value; char *value;
}; };
enum binding_input_type { enum binding_input_type {
BINDING_KEYCODE, BINDING_KEYCODE,
BINDING_KEYSYM, BINDING_KEYSYM,
@ -39,6 +38,7 @@ enum binding_flags {
BINDING_BORDER=4, // mouse only; trigger on container border BINDING_BORDER=4, // mouse only; trigger on container border
BINDING_CONTENTS=8, // mouse only; trigger on container contents BINDING_CONTENTS=8, // mouse only; trigger on container contents
BINDING_TITLEBAR=16, // mouse only; trigger on container titlebar BINDING_TITLEBAR=16, // mouse only; trigger on container titlebar
BINDING_CODE=32, // keyboard only; convert keysyms into keycodes
}; };
/** /**
@ -50,6 +50,7 @@ struct sway_binding {
char *input; char *input;
uint32_t flags; uint32_t flags;
list_t *keys; // sorted in ascending order list_t *keys; // sorted in ascending order
list_t *syms; // sorted in ascending order; NULL if BINDING_CODE is not set
uint32_t modifiers; uint32_t modifiers;
char *command; char *command;
}; };
@ -406,6 +407,14 @@ enum alignment {
ALIGN_RIGHT ALIGN_RIGHT
}; };
/**
* The keysym to keycode translation.
*/
struct keysym_translation_data {
struct xkb_keymap *xkb_keymap;
struct xkb_state *xkb_state;
};
/** /**
* The configuration struct. The result of loading a config file. * The configuration struct. The result of loading a config file.
*/ */
@ -508,6 +517,9 @@ struct sway_config {
list_t *feature_policies; list_t *feature_policies;
list_t *ipc_policies; list_t *ipc_policies;
// The keysym to keycode translation
struct keysym_translation_data keysym_translation;
// Context for command handlers // Context for command handlers
struct { struct {
struct input_config *input_config; struct input_config *input_config;
@ -617,12 +629,6 @@ bool spawn_swaybg(void);
int workspace_output_cmp_workspace(const void *a, const void *b); int workspace_output_cmp_workspace(const void *a, const void *b);
int sway_binding_cmp(const void *a, const void *b);
int sway_binding_cmp_qsort(const void *a, const void *b);
int sway_binding_cmp_keys(const void *a, const void *b);
void free_sway_binding(struct sway_binding *sb); void free_sway_binding(struct sway_binding *sb);
void free_switch_binding(struct sway_switch_binding *binding); void free_switch_binding(struct sway_switch_binding *binding);
@ -651,6 +657,16 @@ void free_workspace_config(struct workspace_config *wsc);
*/ */
void config_update_font_height(bool recalculate); void config_update_font_height(bool recalculate);
/**
* Convert bindsym into bindcode using the first configured layout.
* Return false in case the conversion is unsuccessful.
*/
bool translate_binding(struct sway_binding *binding);
void translate_keysyms(const char *layout);
void binding_add_translated(struct sway_binding *binding, list_t *bindings);
/* Global config singleton. */ /* Global config singleton. */
extern struct sway_config *config; extern struct sway_config *config;

View file

@ -24,6 +24,7 @@ void free_sway_binding(struct sway_binding *binding) {
} }
list_free_items_and_destroy(binding->keys); list_free_items_and_destroy(binding->keys);
list_free_items_and_destroy(binding->syms);
free(binding->input); free(binding->input);
free(binding->command); free(binding->command);
free(binding); free(binding);
@ -249,14 +250,30 @@ static struct cmd_results *switch_binding_remove(
switchcombo); switchcombo);
} }
static struct cmd_results *binding_add(struct sway_binding *binding, /**
list_t *mode_bindings, const char *bindtype, * Insert or update the binding.
const char *keycombo, bool warn) { * Return the binding which has been replaced or NULL.
// overwrite the binding if it already exists */
bool overwritten = false; static struct sway_binding *binding_upsert(struct sway_binding *binding,
list_t *mode_bindings) {
for (int i = 0; i < mode_bindings->length; ++i) { for (int i = 0; i < mode_bindings->length; ++i) {
struct sway_binding *config_binding = mode_bindings->items[i]; struct sway_binding *config_binding = mode_bindings->items[i];
if (binding_key_compare(binding, config_binding)) { if (binding_key_compare(binding, config_binding)) {
mode_bindings->items[i] = binding;
return config_binding;
}
}
list_add(mode_bindings, binding);
return NULL;
}
static struct cmd_results *binding_add(struct sway_binding *binding,
list_t *mode_bindings, const char *bindtype,
const char *keycombo, bool warn) {
struct sway_binding *config_binding = binding_upsert(binding, mode_bindings);
if (config_binding) {
sway_log(SWAY_INFO, "Overwriting binding '%s' for device '%s' " sway_log(SWAY_INFO, "Overwriting binding '%s' for device '%s' "
"to `%s` from `%s`", keycombo, binding->input, "to `%s` from `%s`", keycombo, binding->input,
binding->command, config_binding->command); binding->command, config_binding->command);
@ -267,13 +284,7 @@ static struct cmd_results *binding_add(struct sway_binding *binding,
config_binding->command); config_binding->command);
} }
free_sway_binding(config_binding); free_sway_binding(config_binding);
mode_bindings->items[i] = binding; } else {
overwritten = true;
}
}
if (!overwritten) {
list_add(mode_bindings, binding);
sway_log(SWAY_DEBUG, "%s - Bound %s to command `%s` for device '%s'", sway_log(SWAY_DEBUG, "%s - Bound %s to command `%s` for device '%s'",
bindtype, keycombo, binding->command, binding->input); bindtype, keycombo, binding->command, binding->input);
} }
@ -329,7 +340,6 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
bool exclude_titlebar = false; bool exclude_titlebar = false;
bool warn = true; bool warn = true;
// Handle --release and --locked
while (argc > 0) { while (argc > 0) {
if (strcmp("--release", argv[0]) == 0) { if (strcmp("--release", argv[0]) == 0) {
binding->flags |= BINDING_RELEASE; binding->flags |= BINDING_RELEASE;
@ -339,6 +349,10 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
binding->flags |= BINDING_BORDER | BINDING_CONTENTS | BINDING_TITLEBAR; binding->flags |= BINDING_BORDER | BINDING_CONTENTS | BINDING_TITLEBAR;
} else if (strcmp("--border", argv[0]) == 0) { } else if (strcmp("--border", argv[0]) == 0) {
binding->flags |= BINDING_BORDER; binding->flags |= BINDING_BORDER;
} else if (strcmp("--to-code", argv[0]) == 0) {
if (!bindcode) {
binding->flags |= BINDING_CODE;
}
} else if (strcmp("--exclude-titlebar", argv[0]) == 0) { } else if (strcmp("--exclude-titlebar", argv[0]) == 0) {
exclude_titlebar = true; exclude_titlebar = true;
} else if (strncmp("--input-device=", argv[0], } else if (strncmp("--input-device=", argv[0],
@ -410,6 +424,12 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
// sort ascending // sort ascending
list_qsort(binding->keys, key_qsort_cmp); list_qsort(binding->keys, key_qsort_cmp);
// translate keysyms into keycodes
if (!translate_binding(binding)) {
sway_log(SWAY_INFO,
"Unable to translate bindsym into bindcode: %s", argv[0]);
}
list_t *mode_bindings; list_t *mode_bindings;
if (binding->type == BINDING_KEYCODE) { if (binding->type == BINDING_KEYCODE) {
mode_bindings = config->current_mode->keycode_bindings; mode_bindings = config->current_mode->keycode_bindings;
@ -566,3 +586,114 @@ void seat_execute_command(struct sway_seat *seat, struct sway_binding *binding)
ipc_event_binding(binding); ipc_event_binding(binding);
} }
} }
/**
* The last found keycode associated with the keysym
* and the total count of matches.
*/
struct keycode_matches {
xkb_keysym_t keysym;
xkb_keycode_t keycode;
int count;
};
/**
* Iterate through keycodes in the keymap to find ones matching
* the specified keysym.
*/
static void find_keycode(struct xkb_keymap *keymap,
xkb_keycode_t keycode, void *data) {
xkb_keysym_t keysym = xkb_state_key_get_one_sym(
config->keysym_translation.xkb_state, keycode);
if (keysym == XKB_KEY_NoSymbol) {
return;
}
struct keycode_matches *matches = data;
if (matches->keysym == keysym) {
matches->keycode = keycode;
matches->count++;
}
}
/**
* Return the keycode for the specified keysym.
*/
static struct keycode_matches get_keycode_for_keysym(xkb_keysym_t keysym) {
struct keycode_matches matches = {
.keysym = keysym,
.keycode = XKB_KEYCODE_INVALID,
.count = 0,
};
xkb_keymap_key_for_each(config->keysym_translation.xkb_keymap,
find_keycode, &matches);
return matches;
}
bool translate_binding(struct sway_binding *binding) {
if ((binding->flags & BINDING_CODE) == 0) {
return true;
}
switch (binding->type) {
// a bindsym to translate
case BINDING_KEYSYM:
binding->syms = binding->keys;
binding->keys = create_list();
break;
// a bindsym to re-translate
case BINDING_KEYCODE:
list_free_items_and_destroy(binding->keys);
binding->keys = create_list();
break;
default:
return true;
}
for (int i = 0; i < binding->syms->length; ++i) {
xkb_keysym_t *keysym = binding->syms->items[i];
struct keycode_matches matches = get_keycode_for_keysym(*keysym);
if (matches.count != 1) {
sway_log(SWAY_INFO, "Unable to convert keysym %d into"
" a single keycode (found %d matches)",
*keysym, matches.count);
goto error;
}
xkb_keycode_t *keycode = malloc(sizeof(xkb_keycode_t));
if (!keycode) {
sway_log(SWAY_ERROR, "Unable to allocate memory for a keycode");
goto error;
}
*keycode = matches.keycode;
list_add(binding->keys, keycode);
}
list_qsort(binding->keys, key_qsort_cmp);
binding->type = BINDING_KEYCODE;
return true;
error:
list_free_items_and_destroy(binding->keys);
binding->type = BINDING_KEYSYM;
binding->keys = binding->syms;
binding->syms = NULL;
return false;
}
void binding_add_translated(struct sway_binding *binding,
list_t *mode_bindings) {
struct sway_binding *config_binding =
binding_upsert(binding, mode_bindings);
if (config_binding) {
sway_log(SWAY_INFO, "Overwriting binding for device '%s' "
"to `%s` from `%s`", binding->input,
binding->command, config_binding->command);
free_sway_binding(config_binding);
}
}

View file

@ -39,6 +39,30 @@ static struct cmd_handler input_config_handlers[] = {
{ "xkb_numlock", input_cmd_xkb_numlock }, { "xkb_numlock", input_cmd_xkb_numlock },
}; };
/**
* Re-translate keysyms if a change in the input config could affect them.
*/
static void retranslate_keysyms(struct input_config *input_config) {
bool matched = false;
for (int i = 0; i < config->input_configs->length; ++i) {
struct input_config *ic = config->input_configs->items[i];
matched |= ic->identifier == input_config->identifier;
// the first configured xkb_layout
if (ic->xkb_layout) {
if (matched) {
translate_keysyms(ic->xkb_layout);
}
// nothing has changed
return;
}
}
// no xkb_layout has been set, restore the default
translate_keysyms(getenv("XKB_DEFAULT_LAYOUT"));
}
struct cmd_results *cmd_input(int argc, char **argv) { struct cmd_results *cmd_input(int argc, char **argv) {
struct cmd_results *error = NULL; struct cmd_results *error = NULL;
if ((error = checkarg(argc, "input", EXPECTED_AT_LEAST, 2))) { if ((error = checkarg(argc, "input", EXPECTED_AT_LEAST, 2))) {
@ -73,6 +97,7 @@ struct cmd_results *cmd_input(int argc, char **argv) {
store_input_config(config->handler_context.input_config); store_input_config(config->handler_context.input_config);
input_manager_apply_input_config(ic); input_manager_apply_input_config(ic);
retranslate_keysyms(ic);
} else { } else {
free_input_config(config->handler_context.input_config); free_input_config(config->handler_context.input_config);
} }

View file

@ -33,6 +33,31 @@
struct sway_config *config = NULL; struct sway_config *config = NULL;
static struct keysym_translation_data new_keysym_translation_data(
const char *layout) {
struct xkb_rule_names rules = {
.layout = layout,
};
struct xkb_keymap *xkb_keymap = xkb_keymap_new_from_names(
xkb_context_new(XKB_CONTEXT_NO_FLAGS),
&rules,
XKB_KEYMAP_COMPILE_NO_FLAGS);
struct keysym_translation_data result = {
.xkb_keymap = xkb_keymap,
.xkb_state = xkb_state_new(xkb_keymap),
};
return result;
}
static void free_keysym_translation_data(
struct keysym_translation_data config) {
xkb_state_unref(config.xkb_state);
xkb_keymap_unref(config.xkb_keymap);
}
static void free_mode(struct sway_mode *mode) { static void free_mode(struct sway_mode *mode) {
if (!mode) { if (!mode) {
return; return;
@ -146,6 +171,7 @@ void free_config(struct sway_config *config) {
free(config->swaynag_command); free(config->swaynag_command);
free((char *)config->current_config_path); free((char *)config->current_config_path);
free((char *)config->current_config); free((char *)config->current_config);
free_keysym_translation_data(config->keysym_translation);
free(config); free(config);
} }
@ -317,6 +343,9 @@ static void config_defaults(struct sway_config *config) {
if (!(config->feature_policies = create_list())) goto cleanup; if (!(config->feature_policies = create_list())) goto cleanup;
if (!(config->ipc_policies = create_list())) goto cleanup; if (!(config->ipc_policies = create_list())) goto cleanup;
// The keysym to keycode translation
config->keysym_translation = new_keysym_translation_data(getenv("XKB_DEFAULT_LAYOUT"));
return; return;
cleanup: cleanup:
sway_abort("Unable to allocate config structures"); sway_abort("Unable to allocate config structures");
@ -937,3 +966,50 @@ void config_update_font_height(bool recalculate) {
arrange_root(); arrange_root();
} }
} }
static void translate_binding_list(list_t *bindings, list_t *bindsyms,
list_t *bindcodes) {
for (int i = 0; i < bindings->length; ++i) {
struct sway_binding *binding = bindings->items[i];
translate_binding(binding);
list_t *bindings;
switch (binding->type) {
case BINDING_KEYSYM:
bindings = bindsyms;
break;
case BINDING_KEYCODE:
bindings = bindcodes;
break;
default:
sway_assert(false, "unexpected translated binding type: %d",
binding->type);
break;
}
binding_add_translated(binding, bindings);
}
}
void translate_keysyms(const char *layout) {
free_keysym_translation_data(config->keysym_translation);
config->keysym_translation = new_keysym_translation_data(layout);
for (int i = 0; i < config->modes->length; ++i) {
struct sway_mode *mode = config->modes->items[i];
list_t *bindsyms = create_list();
list_t *bindcodes = create_list();
translate_binding_list(mode->keysym_bindings, bindsyms, bindcodes);
translate_binding_list(mode->keycode_bindings, bindsyms, bindcodes);
list_free(mode->keysym_bindings);
list_free(mode->keycode_bindings);
mode->keysym_bindings = bindsyms;
mode->keycode_bindings = bindcodes;
}
sway_log(SWAY_DEBUG, "Translated keysyms for layout %s", layout);
}