diff options
author | Tony Crisci <tony@dubstepdish.com> | 2017-12-27 10:08:18 -0500 |
---|---|---|
committer | Tony Crisci <tony@dubstepdish.com> | 2017-12-27 12:31:06 -0500 |
commit | ba69f06695c24f98a05d138a53ba130108ebce6f (patch) | |
tree | e36dac0c2088492117fb46404cad3a5fb28dffbe | |
parent | Merge pull request #1535 from martinetd/libinput (diff) | |
download | sway-ba69f06695c24f98a05d138a53ba130108ebce6f.tar.gz sway-ba69f06695c24f98a05d138a53ba130108ebce6f.tar.zst sway-ba69f06695c24f98a05d138a53ba130108ebce6f.zip |
binding config
-rw-r--r-- | include/sway/config.h | 3 | ||||
-rw-r--r-- | sway/commands.c | 2 | ||||
-rw-r--r-- | sway/commands/bind.c | 173 | ||||
-rw-r--r-- | sway/config.c | 3 | ||||
-rw-r--r-- | sway/meson.build | 1 |
5 files changed, 180 insertions, 2 deletions
diff --git a/include/sway/config.h b/include/sway/config.h index 83ded720..7146623e 100644 --- a/include/sway/config.h +++ b/include/sway/config.h | |||
@@ -46,7 +46,8 @@ struct sway_mouse_binding { | |||
46 | */ | 46 | */ |
47 | struct sway_mode { | 47 | struct sway_mode { |
48 | char *name; | 48 | char *name; |
49 | list_t *bindings; | 49 | list_t *keysym_bindings; |
50 | list_t *keycode_bindings; | ||
50 | }; | 51 | }; |
51 | 52 | ||
52 | /** | 53 | /** |
diff --git a/sway/commands.c b/sway/commands.c index 34afb6a0..b0078a46 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -127,6 +127,8 @@ struct cmd_results *add_color(const char *name, char *buffer, const char *color) | |||
127 | 127 | ||
128 | /* Keep alphabetized */ | 128 | /* Keep alphabetized */ |
129 | static struct cmd_handler handlers[] = { | 129 | static struct cmd_handler handlers[] = { |
130 | { "bindcode", cmd_bindcode }, | ||
131 | { "bindsym", cmd_bindsym }, | ||
130 | { "exec", cmd_exec }, | 132 | { "exec", cmd_exec }, |
131 | { "exec_always", cmd_exec_always }, | 133 | { "exec_always", cmd_exec_always }, |
132 | { "exit", cmd_exit }, | 134 | { "exit", cmd_exit }, |
diff --git a/sway/commands/bind.c b/sway/commands/bind.c new file mode 100644 index 00000000..62aa535a --- /dev/null +++ b/sway/commands/bind.c | |||
@@ -0,0 +1,173 @@ | |||
1 | #ifdef __linux__ | ||
2 | #include <linux/input-event-codes.h> | ||
3 | #elif __FreeBSD__ | ||
4 | #include <dev/evdev/input-event-codes.h> | ||
5 | #endif | ||
6 | #include <xkbcommon/xkbcommon.h> | ||
7 | #include <xkbcommon/xkbcommon-names.h> | ||
8 | #include <strings.h> | ||
9 | #include "sway/commands.h" | ||
10 | #include "sway/config.h" | ||
11 | #include "list.h" | ||
12 | #include "log.h" | ||
13 | #include "stringop.h" | ||
14 | #include "util.h" | ||
15 | |||
16 | int binding_order = 0; | ||
17 | |||
18 | void free_sway_binding(struct sway_binding *binding) { | ||
19 | if (binding->keys) { | ||
20 | for (int i = 0; i < binding->keys->length; i++) { | ||
21 | free(binding->keys->items[i]); | ||
22 | } | ||
23 | list_free(binding->keys); | ||
24 | } | ||
25 | if (binding->command) { | ||
26 | free(binding->command); | ||
27 | } | ||
28 | free(binding); | ||
29 | } | ||
30 | |||
31 | struct cmd_results *cmd_bindsym(int argc, char **argv) { | ||
32 | struct cmd_results *error = NULL; | ||
33 | if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) { | ||
34 | return error; | ||
35 | } | ||
36 | |||
37 | struct sway_binding *binding = malloc(sizeof(struct sway_binding)); | ||
38 | if (!binding) { | ||
39 | return cmd_results_new(CMD_FAILURE, "bindsym", | ||
40 | "Unable to allocate binding"); | ||
41 | } | ||
42 | binding->keys = create_list(); | ||
43 | binding->modifiers = 0; | ||
44 | binding->release = false; | ||
45 | binding->bindcode = false; | ||
46 | |||
47 | // Handle --release | ||
48 | if (strcmp("--release", argv[0]) == 0) { | ||
49 | if (argc >= 3) { | ||
50 | binding->release = true; | ||
51 | argv++; | ||
52 | argc--; | ||
53 | } else { | ||
54 | free_sway_binding(binding); | ||
55 | return cmd_results_new(CMD_FAILURE, "bindsym", | ||
56 | "Invalid bindsym command " | ||
57 | "(expected more than 2 arguments, got %d)", argc); | ||
58 | } | ||
59 | } | ||
60 | |||
61 | binding->command = join_args(argv + 1, argc - 1); | ||
62 | |||
63 | list_t *split = split_string(argv[0], "+"); | ||
64 | for (int i = 0; i < split->length; ++i) { | ||
65 | // Check for a modifier key | ||
66 | uint32_t mod; | ||
67 | if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) { | ||
68 | binding->modifiers |= mod; | ||
69 | continue; | ||
70 | } | ||
71 | // Check for xkb key | ||
72 | xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], | ||
73 | XKB_KEYSYM_CASE_INSENSITIVE); | ||
74 | |||
75 | // Check for mouse binding | ||
76 | if (strncasecmp(split->items[i], "button", strlen("button")) == 0 && | ||
77 | strlen(split->items[i]) == strlen("button0")) { | ||
78 | sym = ((char *)split->items[i])[strlen("button")] - '1' + BTN_LEFT; | ||
79 | } | ||
80 | if (!sym) { | ||
81 | struct cmd_results *ret = cmd_results_new(CMD_INVALID, "bindsym", | ||
82 | "Unknown key '%s'", (char *)split->items[i]); | ||
83 | free_sway_binding(binding); | ||
84 | free_flat_list(split); | ||
85 | return ret; | ||
86 | } | ||
87 | xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t)); | ||
88 | if (!key) { | ||
89 | free_sway_binding(binding); | ||
90 | free_flat_list(split); | ||
91 | return cmd_results_new(CMD_FAILURE, "bindsym", | ||
92 | "Unable to allocate binding"); | ||
93 | } | ||
94 | *key = sym; | ||
95 | list_add(binding->keys, key); | ||
96 | } | ||
97 | free_flat_list(split); | ||
98 | |||
99 | struct sway_mode *mode = config->current_mode; | ||
100 | // TODO overwrite the binding if it already exists | ||
101 | binding->order = binding_order++; | ||
102 | list_add(mode->keysym_bindings, binding); | ||
103 | |||
104 | sway_log(L_DEBUG, "bindsym - Bound %s to command %s", | ||
105 | argv[0], binding->command); | ||
106 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
107 | } | ||
108 | |||
109 | struct cmd_results *cmd_bindcode(int argc, char **argv) { | ||
110 | struct cmd_results *error = NULL; | ||
111 | if ((error = checkarg(argc, "bindcode", EXPECTED_MORE_THAN, 1))) { | ||
112 | return error; | ||
113 | } | ||
114 | |||
115 | struct sway_binding *binding = malloc(sizeof(struct sway_binding)); | ||
116 | if (!binding) { | ||
117 | return cmd_results_new(CMD_FAILURE, "bindsym", | ||
118 | "Unable to allocate binding"); | ||
119 | } | ||
120 | binding->keys = create_list(); | ||
121 | binding->modifiers = 0; | ||
122 | binding->release = false; | ||
123 | binding->bindcode = true; | ||
124 | |||
125 | // Handle --release | ||
126 | if (strcmp("--release", argv[0]) == 0) { | ||
127 | if (argc >= 3) { | ||
128 | binding->release = true; | ||
129 | argv++; | ||
130 | argc--; | ||
131 | } else { | ||
132 | free_sway_binding(binding); | ||
133 | return cmd_results_new(CMD_FAILURE, "bindcode", | ||
134 | "Invalid bindcode command " | ||
135 | "(expected more than 2 arguments, got %d)", argc); | ||
136 | } | ||
137 | } | ||
138 | |||
139 | binding->command = join_args(argv + 1, argc - 1); | ||
140 | |||
141 | list_t *split = split_string(argv[0], "+"); | ||
142 | for (int i = 0; i < split->length; ++i) { | ||
143 | // Check for a modifier key | ||
144 | uint32_t mod; | ||
145 | if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) { | ||
146 | binding->modifiers |= mod; | ||
147 | continue; | ||
148 | } | ||
149 | // parse keycode | ||
150 | xkb_keycode_t keycode = (int)strtol(split->items[i], NULL, 10); | ||
151 | if (!xkb_keycode_is_legal_ext(keycode)) { | ||
152 | error = | ||
153 | cmd_results_new(CMD_INVALID, "bindcode", | ||
154 | "Invalid keycode '%s'", (char *)split->items[i]); | ||
155 | free_sway_binding(binding); | ||
156 | list_free(split); | ||
157 | return error; | ||
158 | } | ||
159 | xkb_keycode_t *key = malloc(sizeof(xkb_keycode_t)); | ||
160 | *key = keycode - 8; | ||
161 | list_add(binding->keys, key); | ||
162 | } | ||
163 | free_flat_list(split); | ||
164 | |||
165 | struct sway_mode *mode = config->current_mode; | ||
166 | // TODO overwrite binding if it already exists | ||
167 | binding->order = binding_order++; | ||
168 | list_add(mode->keycode_bindings, binding); | ||
169 | |||
170 | sway_log(L_DEBUG, "bindcode - Bound %s to command %s", | ||
171 | argv[0], binding->command); | ||
172 | return cmd_results_new(CMD_SUCCESS, NULL, NULL); | ||
173 | } | ||
diff --git a/sway/config.c b/sway/config.c index b591ae9e..312e0779 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -53,7 +53,8 @@ static void config_defaults(struct sway_config *config) { | |||
53 | goto cleanup; | 53 | goto cleanup; |
54 | if (!(config->current_mode->name = malloc(sizeof("default")))) goto cleanup; | 54 | if (!(config->current_mode->name = malloc(sizeof("default")))) goto cleanup; |
55 | strcpy(config->current_mode->name, "default"); | 55 | strcpy(config->current_mode->name, "default"); |
56 | if (!(config->current_mode->bindings = create_list())) goto cleanup; | 56 | if (!(config->current_mode->keysym_bindings = create_list())) goto cleanup; |
57 | if (!(config->current_mode->keycode_bindings = create_list())) goto cleanup; | ||
57 | list_add(config->modes, config->current_mode); | 58 | list_add(config->modes, config->current_mode); |
58 | 59 | ||
59 | config->floating_mod = 0; | 60 | config->floating_mod = 0; |
diff --git a/sway/meson.build b/sway/meson.build index fee2ddd2..01d5ef36 100644 --- a/sway/meson.build +++ b/sway/meson.build | |||
@@ -6,6 +6,7 @@ sway_sources = files( | |||
6 | 'input/seat.c', | 6 | 'input/seat.c', |
7 | 'input/cursor.c', | 7 | 'input/cursor.c', |
8 | 'input/keyboard.c', | 8 | 'input/keyboard.c', |
9 | 'commands/bind.c', | ||
9 | 'commands/exit.c', | 10 | 'commands/exit.c', |
10 | 'commands/exec.c', | 11 | 'commands/exec.c', |
11 | 'commands/exec_always.c', | 12 | 'commands/exec_always.c', |