2018-11-25 22:12:48 +11:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2017-12-17 03:25:59 +11:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <float.h>
|
|
|
|
#include "sway/config.h"
|
2019-06-06 04:16:37 +10:00
|
|
|
#include "sway/input/keyboard.h"
|
2017-12-17 03:25:59 +11:00
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
struct input_config *new_input_config(const char* identifier) {
|
|
|
|
struct input_config *input = calloc(1, sizeof(struct input_config));
|
|
|
|
if (!input) {
|
2019-01-21 05:51:12 +11:00
|
|
|
sway_log(SWAY_DEBUG, "Unable to allocate input config");
|
2017-12-17 03:25:59 +11:00
|
|
|
return NULL;
|
|
|
|
}
|
2019-01-21 05:51:12 +11:00
|
|
|
sway_log(SWAY_DEBUG, "new_input_config(%s)", identifier);
|
2017-12-17 03:25:59 +11:00
|
|
|
if (!(input->identifier = strdup(identifier))) {
|
|
|
|
free(input);
|
2019-01-21 05:51:12 +11:00
|
|
|
sway_log(SWAY_DEBUG, "Unable to allocate input config");
|
2017-12-17 03:25:59 +11:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-26 13:05:49 +11:00
|
|
|
input->input_type = NULL;
|
2017-12-17 03:25:59 +11:00
|
|
|
input->tap = INT_MIN;
|
2018-07-14 15:01:47 +10:00
|
|
|
input->tap_button_map = INT_MIN;
|
2018-09-29 19:49:41 +10:00
|
|
|
input->drag = INT_MIN;
|
2017-12-17 03:25:59 +11:00
|
|
|
input->drag_lock = INT_MIN;
|
|
|
|
input->dwt = INT_MIN;
|
|
|
|
input->send_events = INT_MIN;
|
|
|
|
input->click_method = INT_MIN;
|
|
|
|
input->middle_emulation = INT_MIN;
|
|
|
|
input->natural_scroll = INT_MIN;
|
|
|
|
input->accel_profile = INT_MIN;
|
|
|
|
input->pointer_accel = FLT_MIN;
|
2018-11-18 06:31:33 +11:00
|
|
|
input->scroll_factor = FLT_MIN;
|
2018-07-12 06:03:06 +10:00
|
|
|
input->scroll_button = INT_MIN;
|
2018-07-12 20:08:53 +10:00
|
|
|
input->scroll_method = INT_MIN;
|
2017-12-17 03:25:59 +11:00
|
|
|
input->left_handed = INT_MIN;
|
2018-04-18 23:19:23 +10:00
|
|
|
input->repeat_delay = INT_MIN;
|
|
|
|
input->repeat_rate = INT_MIN;
|
2018-07-23 05:16:19 +10:00
|
|
|
input->xkb_numlock = INT_MIN;
|
|
|
|
input->xkb_capslock = INT_MIN;
|
2019-07-13 12:04:29 +10:00
|
|
|
input->xkb_file_is_set = false;
|
2017-12-17 03:25:59 +11:00
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
|
|
|
void merge_input_config(struct input_config *dst, struct input_config *src) {
|
|
|
|
if (src->accel_profile != INT_MIN) {
|
|
|
|
dst->accel_profile = src->accel_profile;
|
|
|
|
}
|
|
|
|
if (src->click_method != INT_MIN) {
|
|
|
|
dst->click_method = src->click_method;
|
|
|
|
}
|
2018-09-29 19:49:41 +10:00
|
|
|
if (src->drag != INT_MIN) {
|
|
|
|
dst->drag = src->drag;
|
|
|
|
}
|
2017-12-17 03:25:59 +11:00
|
|
|
if (src->drag_lock != INT_MIN) {
|
|
|
|
dst->drag_lock = src->drag_lock;
|
|
|
|
}
|
|
|
|
if (src->dwt != INT_MIN) {
|
|
|
|
dst->dwt = src->dwt;
|
|
|
|
}
|
2018-09-27 18:25:32 +10:00
|
|
|
if (src->left_handed != INT_MIN) {
|
|
|
|
dst->left_handed = src->left_handed;
|
|
|
|
}
|
2017-12-17 03:25:59 +11:00
|
|
|
if (src->middle_emulation != INT_MIN) {
|
|
|
|
dst->middle_emulation = src->middle_emulation;
|
|
|
|
}
|
|
|
|
if (src->natural_scroll != INT_MIN) {
|
|
|
|
dst->natural_scroll = src->natural_scroll;
|
|
|
|
}
|
|
|
|
if (src->pointer_accel != FLT_MIN) {
|
|
|
|
dst->pointer_accel = src->pointer_accel;
|
|
|
|
}
|
2018-11-18 06:31:33 +11:00
|
|
|
if (src->scroll_factor != FLT_MIN) {
|
|
|
|
dst->scroll_factor = src->scroll_factor;
|
|
|
|
}
|
2018-04-18 23:19:23 +10:00
|
|
|
if (src->repeat_delay != INT_MIN) {
|
|
|
|
dst->repeat_delay = src->repeat_delay;
|
|
|
|
}
|
|
|
|
if (src->repeat_rate != INT_MIN) {
|
|
|
|
dst->repeat_rate = src->repeat_rate;
|
|
|
|
}
|
2017-12-17 03:25:59 +11:00
|
|
|
if (src->scroll_method != INT_MIN) {
|
|
|
|
dst->scroll_method = src->scroll_method;
|
|
|
|
}
|
2018-07-12 06:03:06 +10:00
|
|
|
if (src->scroll_button != INT_MIN) {
|
2018-07-12 20:08:53 +10:00
|
|
|
dst->scroll_button = src->scroll_button;
|
2018-07-12 06:03:06 +10:00
|
|
|
}
|
2017-12-17 03:25:59 +11:00
|
|
|
if (src->send_events != INT_MIN) {
|
|
|
|
dst->send_events = src->send_events;
|
|
|
|
}
|
|
|
|
if (src->tap != INT_MIN) {
|
|
|
|
dst->tap = src->tap;
|
|
|
|
}
|
2018-07-14 15:01:47 +10:00
|
|
|
if (src->tap_button_map != INT_MIN) {
|
|
|
|
dst->tap_button_map = src->tap_button_map;
|
|
|
|
}
|
2019-07-13 12:04:29 +10:00
|
|
|
if (src->xkb_file_is_set) {
|
|
|
|
free(dst->xkb_file);
|
|
|
|
dst->xkb_file = src->xkb_file ? strdup(src->xkb_file) : NULL;
|
|
|
|
dst->xkb_file_is_set = dst->xkb_file != NULL;
|
|
|
|
}
|
2017-12-17 03:25:59 +11:00
|
|
|
if (src->xkb_layout) {
|
|
|
|
free(dst->xkb_layout);
|
|
|
|
dst->xkb_layout = strdup(src->xkb_layout);
|
|
|
|
}
|
|
|
|
if (src->xkb_model) {
|
|
|
|
free(dst->xkb_model);
|
|
|
|
dst->xkb_model = strdup(src->xkb_model);
|
|
|
|
}
|
|
|
|
if (src->xkb_options) {
|
|
|
|
free(dst->xkb_options);
|
|
|
|
dst->xkb_options = strdup(src->xkb_options);
|
|
|
|
}
|
|
|
|
if (src->xkb_rules) {
|
|
|
|
free(dst->xkb_rules);
|
|
|
|
dst->xkb_rules = strdup(src->xkb_rules);
|
|
|
|
}
|
|
|
|
if (src->xkb_variant) {
|
|
|
|
free(dst->xkb_variant);
|
|
|
|
dst->xkb_variant = strdup(src->xkb_variant);
|
|
|
|
}
|
2018-07-23 05:16:19 +10:00
|
|
|
if (src->xkb_numlock != INT_MIN) {
|
|
|
|
dst->xkb_numlock = src->xkb_numlock;
|
|
|
|
}
|
|
|
|
if (src->xkb_capslock != INT_MIN) {
|
|
|
|
dst->xkb_capslock = src->xkb_capslock;
|
|
|
|
}
|
2018-04-25 04:39:29 +10:00
|
|
|
if (src->mapped_from_region) {
|
|
|
|
free(dst->mapped_from_region);
|
|
|
|
dst->mapped_from_region =
|
|
|
|
malloc(sizeof(struct input_config_mapped_from_region));
|
|
|
|
memcpy(dst->mapped_from_region, src->mapped_from_region,
|
|
|
|
sizeof(struct input_config_mapped_from_region));
|
|
|
|
}
|
2019-11-02 04:37:29 +11:00
|
|
|
if (src->mapped_to) {
|
|
|
|
dst->mapped_to = src->mapped_to;
|
|
|
|
}
|
2018-04-25 04:39:29 +10:00
|
|
|
if (src->mapped_to_output) {
|
|
|
|
free(dst->mapped_to_output);
|
|
|
|
dst->mapped_to_output = strdup(src->mapped_to_output);
|
2018-04-09 04:15:13 +10:00
|
|
|
}
|
2019-10-29 11:26:00 +11:00
|
|
|
if (src->mapped_to_region) {
|
|
|
|
free(dst->mapped_to_region);
|
|
|
|
dst->mapped_to_region =
|
|
|
|
malloc(sizeof(struct wlr_box));
|
|
|
|
memcpy(dst->mapped_to_region, src->mapped_to_region,
|
|
|
|
sizeof(struct wlr_box));
|
|
|
|
}
|
2019-06-17 19:38:41 +10:00
|
|
|
if (src->calibration_matrix.configured) {
|
|
|
|
dst->calibration_matrix.configured = src->calibration_matrix.configured;
|
|
|
|
memcpy(dst->calibration_matrix.matrix, src->calibration_matrix.matrix,
|
|
|
|
sizeof(src->calibration_matrix.matrix));
|
|
|
|
}
|
2017-12-17 03:25:59 +11:00
|
|
|
}
|
|
|
|
|
2019-06-06 04:16:37 +10:00
|
|
|
static bool validate_xkb_merge(struct input_config *dest,
|
|
|
|
struct input_config *src, char **xkb_error) {
|
|
|
|
struct input_config *temp = new_input_config("temp");
|
|
|
|
if (dest) {
|
|
|
|
merge_input_config(temp, dest);
|
|
|
|
}
|
|
|
|
merge_input_config(temp, src);
|
|
|
|
|
|
|
|
struct xkb_keymap *keymap = sway_keyboard_compile_keymap(temp, xkb_error);
|
|
|
|
free_input_config(temp);
|
|
|
|
if (!keymap) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
xkb_keymap_unref(keymap);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool validate_wildcard_on_all(struct input_config *wildcard,
|
|
|
|
char **error) {
|
2018-09-24 09:56:52 +10:00
|
|
|
for (int i = 0; i < config->input_configs->length; i++) {
|
|
|
|
struct input_config *ic = config->input_configs->items[i];
|
|
|
|
if (strcmp(wildcard->identifier, ic->identifier) != 0) {
|
2019-06-06 04:16:37 +10:00
|
|
|
sway_log(SWAY_DEBUG, "Validating xkb merge of * on %s",
|
|
|
|
ic->identifier);
|
|
|
|
if (!validate_xkb_merge(ic, wildcard, error)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-24 09:56:52 +10:00
|
|
|
}
|
|
|
|
}
|
2019-03-26 13:05:49 +11:00
|
|
|
|
|
|
|
for (int i = 0; i < config->input_type_configs->length; i++) {
|
|
|
|
struct input_config *ic = config->input_type_configs->items[i];
|
2019-06-06 04:16:37 +10:00
|
|
|
sway_log(SWAY_DEBUG, "Validating xkb merge of * config on %s",
|
|
|
|
ic->identifier);
|
|
|
|
if (!validate_xkb_merge(ic, wildcard, error)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void merge_wildcard_on_all(struct input_config *wildcard) {
|
|
|
|
for (int i = 0; i < config->input_configs->length; i++) {
|
|
|
|
struct input_config *ic = config->input_configs->items[i];
|
2019-03-26 13:05:49 +11:00
|
|
|
if (strcmp(wildcard->identifier, ic->identifier) != 0) {
|
|
|
|
sway_log(SWAY_DEBUG, "Merging input * config on %s", ic->identifier);
|
|
|
|
merge_input_config(ic, wildcard);
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 04:16:37 +10:00
|
|
|
|
|
|
|
for (int i = 0; i < config->input_type_configs->length; i++) {
|
|
|
|
struct input_config *ic = config->input_type_configs->items[i];
|
|
|
|
sway_log(SWAY_DEBUG, "Merging input * config on %s", ic->identifier);
|
|
|
|
merge_input_config(ic, wildcard);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool validate_type_on_existing(struct input_config *type_wildcard,
|
|
|
|
char **error) {
|
|
|
|
for (int i = 0; i < config->input_configs->length; i++) {
|
|
|
|
struct input_config *ic = config->input_configs->items[i];
|
|
|
|
if (ic->input_type == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(ic->input_type, type_wildcard->identifier + 5) == 0) {
|
|
|
|
sway_log(SWAY_DEBUG, "Validating merge of %s on %s",
|
|
|
|
type_wildcard->identifier, ic->identifier);
|
|
|
|
if (!validate_xkb_merge(ic, type_wildcard, error)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2019-03-26 13:05:49 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
static void merge_type_on_existing(struct input_config *type_wildcard) {
|
|
|
|
for (int i = 0; i < config->input_configs->length; i++) {
|
|
|
|
struct input_config *ic = config->input_configs->items[i];
|
|
|
|
if (ic->input_type == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(ic->input_type, type_wildcard->identifier + 5) == 0) {
|
|
|
|
sway_log(SWAY_DEBUG, "Merging %s top of %s",
|
|
|
|
type_wildcard->identifier,
|
|
|
|
ic->identifier);
|
|
|
|
merge_input_config(ic, type_wildcard);
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 09:56:52 +10:00
|
|
|
}
|
|
|
|
|
2019-06-06 04:16:37 +10:00
|
|
|
struct input_config *store_input_config(struct input_config *ic,
|
|
|
|
char **error) {
|
2018-09-24 09:56:52 +10:00
|
|
|
bool wildcard = strcmp(ic->identifier, "*") == 0;
|
2019-06-06 04:16:37 +10:00
|
|
|
if (wildcard && error && !validate_wildcard_on_all(ic, error)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool type = strncmp(ic->identifier, "type:", strlen("type:")) == 0;
|
|
|
|
if (type && error && !validate_type_on_existing(ic, error)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_t *config_list = type ? config->input_type_configs
|
|
|
|
: config->input_configs;
|
|
|
|
|
|
|
|
struct input_config *current = NULL;
|
|
|
|
bool new_current = false;
|
|
|
|
|
|
|
|
int i = list_seq_find(config_list, input_identifier_cmp, ic->identifier);
|
|
|
|
if (i >= 0) {
|
|
|
|
current = config_list->items[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
i = list_seq_find(config->input_configs, input_identifier_cmp, "*");
|
|
|
|
if (!current && i >= 0) {
|
|
|
|
current = new_input_config(ic->identifier);
|
|
|
|
merge_input_config(current, config->input_configs->items[i]);
|
|
|
|
new_current = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error && !validate_xkb_merge(current, ic, error)) {
|
|
|
|
if (new_current) {
|
|
|
|
free_input_config(current);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-24 09:56:52 +10:00
|
|
|
if (wildcard) {
|
|
|
|
merge_wildcard_on_all(ic);
|
2018-01-18 01:49:02 +11:00
|
|
|
}
|
2018-09-24 09:56:52 +10:00
|
|
|
|
2019-06-06 04:16:37 +10:00
|
|
|
if (type) {
|
2019-03-26 13:05:49 +11:00
|
|
|
merge_type_on_existing(ic);
|
|
|
|
}
|
|
|
|
|
2019-06-06 04:16:37 +10:00
|
|
|
if (current) {
|
2018-09-24 09:56:52 +10:00
|
|
|
merge_input_config(current, ic);
|
|
|
|
free_input_config(ic);
|
|
|
|
ic = current;
|
2019-06-06 04:16:37 +10:00
|
|
|
}
|
|
|
|
|
2019-07-13 12:04:29 +10:00
|
|
|
ic->xkb_file_is_set = ic->xkb_file != NULL;
|
|
|
|
|
2019-06-06 04:16:37 +10:00
|
|
|
if (!current || new_current) {
|
2019-03-26 13:05:49 +11:00
|
|
|
list_add(config_list, ic);
|
2018-09-24 09:56:52 +10:00
|
|
|
}
|
|
|
|
|
2019-01-21 05:51:12 +11:00
|
|
|
sway_log(SWAY_DEBUG, "Config stored for input %s", ic->identifier);
|
2018-09-24 09:56:52 +10:00
|
|
|
|
|
|
|
return ic;
|
2018-01-18 01:49:02 +11:00
|
|
|
}
|
|
|
|
|
2019-03-06 06:38:26 +11:00
|
|
|
void input_config_fill_rule_names(struct input_config *ic,
|
|
|
|
struct xkb_rule_names *rules) {
|
|
|
|
rules->layout = ic->xkb_layout;
|
|
|
|
rules->model = ic->xkb_model;
|
|
|
|
rules->options = ic->xkb_options;
|
|
|
|
rules->rules = ic->xkb_rules;
|
|
|
|
rules->variant = ic->xkb_variant;
|
2019-02-20 22:54:59 +11:00
|
|
|
}
|
|
|
|
|
2017-12-17 03:25:59 +11:00
|
|
|
void free_input_config(struct input_config *ic) {
|
|
|
|
if (!ic) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(ic->identifier);
|
2019-07-13 12:04:29 +10:00
|
|
|
free(ic->xkb_file);
|
2018-09-19 21:50:19 +10:00
|
|
|
free(ic->xkb_layout);
|
|
|
|
free(ic->xkb_model);
|
|
|
|
free(ic->xkb_options);
|
|
|
|
free(ic->xkb_rules);
|
|
|
|
free(ic->xkb_variant);
|
|
|
|
free(ic->mapped_from_region);
|
|
|
|
free(ic->mapped_to_output);
|
2019-10-29 11:26:00 +11:00
|
|
|
free(ic->mapped_to_region);
|
2017-12-17 03:25:59 +11:00
|
|
|
free(ic);
|
|
|
|
}
|
|
|
|
|
|
|
|
int input_identifier_cmp(const void *item, const void *data) {
|
|
|
|
const struct input_config *ic = item;
|
|
|
|
const char *identifier = data;
|
|
|
|
return strcmp(ic->identifier, identifier);
|
|
|
|
}
|