diff options
-rw-r--r-- | sway/commands.c | 31 | ||||
-rw-r--r-- | sway/config.c | 28 | ||||
-rw-r--r-- | sway/config.h | 3 | ||||
-rw-r--r-- | sway/stringop.c | 16 | ||||
-rw-r--r-- | sway/stringop.h | 1 |
5 files changed, 75 insertions, 4 deletions
diff --git a/sway/commands.c b/sway/commands.c index cb24f796..3f286c3c 100644 --- a/sway/commands.c +++ b/sway/commands.c | |||
@@ -1,3 +1,5 @@ | |||
1 | #include <xkbcommon/xkbcommon.h> | ||
2 | #include <xkbcommon/xkbcommon-names.h> | ||
1 | #include <stdio.h> | 3 | #include <stdio.h> |
2 | #include <stdlib.h> | 4 | #include <stdlib.h> |
3 | #include <string.h> | 5 | #include <string.h> |
@@ -20,17 +22,40 @@ int cmd_set(struct sway_config *config, int argc, char **argv) { | |||
20 | } | 22 | } |
21 | 23 | ||
22 | int cmd_bindsym(struct sway_config *config, int argc, char **argv) { | 24 | int cmd_bindsym(struct sway_config *config, int argc, char **argv) { |
23 | if (argc != 2) { | 25 | if (argc < 2) { |
24 | fprintf(stderr, "Invalid bindsym command (expected 2 arguments, got %d)\n", argc); | 26 | fprintf(stderr, "Invalid bindsym command (expected 2 arguments, got %d)\n", argc); |
25 | return 1; | 27 | return 1; |
26 | } | 28 | } |
27 | // TODO: parse out keybindings | 29 | argv[0] = do_var_replacement(config, argv[0]); |
30 | |||
31 | struct sway_binding *binding = malloc(sizeof(struct sway_binding)); | ||
32 | binding->keys = create_list(); | ||
33 | binding->modifiers = 0; | ||
34 | binding->command = join_args(argv + 1, argc - 1); | ||
35 | |||
36 | list_t *split = split_string(argv[0], "+"); | ||
37 | int i; | ||
38 | for (i = 0; i < split->length; ++i) { | ||
39 | // TODO: Parse modifier keys | ||
40 | xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE); | ||
41 | if (!sym) { | ||
42 | fprintf(stderr, "bindsym - unknown key '%s'\n", (char *)split->items[i]); | ||
43 | // Ignore for now, we need to deal with modifier keys | ||
44 | // return 1; | ||
45 | } | ||
46 | list_add(binding->keys, split->items[i]); | ||
47 | } | ||
48 | list_free(split); | ||
49 | |||
50 | list_add(config->current_mode->bindings, binding); | ||
51 | |||
52 | fprintf(stderr, "bindsym - Bound %s to command %s\n", argv[0], binding->command); | ||
28 | return 0; | 53 | return 0; |
29 | } | 54 | } |
30 | 55 | ||
31 | /* Keep alphabetized */ | 56 | /* Keep alphabetized */ |
32 | struct cmd_handler handlers[] = { | 57 | struct cmd_handler handlers[] = { |
33 | { "bindsym", cmd_bindsym } | 58 | { "bindsym", cmd_bindsym }, |
34 | { "set", cmd_set } | 59 | { "set", cmd_set } |
35 | }; | 60 | }; |
36 | 61 | ||
diff --git a/sway/config.c b/sway/config.c index 7242be8d..e98246ff 100644 --- a/sway/config.c +++ b/sway/config.c | |||
@@ -50,3 +50,31 @@ _continue: | |||
50 | 50 | ||
51 | return config; | 51 | return config; |
52 | } | 52 | } |
53 | |||
54 | char *do_var_replacement(struct sway_config *config, char *str) { | ||
55 | // TODO: Handle escaping $ and using $ in string literals | ||
56 | int i; | ||
57 | for (i = 0; str[i]; ++i) { | ||
58 | if (str[i] == '$') { | ||
59 | // Try for match (note: this could be faster) | ||
60 | int j; | ||
61 | for (j = 0; j < config->symbols->length; ++j) { | ||
62 | struct sway_variable *var = config->symbols->items[j]; | ||
63 | if (strstr(str + i, var->name) == str + i) { | ||
64 | // Match, do replacement | ||
65 | char *new_string = malloc( | ||
66 | strlen(str) - | ||
67 | strlen(var->name) + | ||
68 | strlen(var->value) + 1); | ||
69 | strncpy(new_string, str, i); | ||
70 | new_string[i] = 0; | ||
71 | strcat(new_string, var->value); | ||
72 | strcat(new_string, str + i + strlen(var->name)); | ||
73 | free(str); | ||
74 | str = new_string; | ||
75 | } | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | return str; | ||
80 | } | ||
diff --git a/sway/config.h b/sway/config.h index b2475871..bcf65ec8 100644 --- a/sway/config.h +++ b/sway/config.h | |||
@@ -12,7 +12,7 @@ struct sway_variable { | |||
12 | 12 | ||
13 | struct sway_binding { | 13 | struct sway_binding { |
14 | list_t *keys; | 14 | list_t *keys; |
15 | struct wlc_modifiers modifiers; | 15 | uint32_t modifiers; |
16 | char *command; | 16 | char *command; |
17 | }; | 17 | }; |
18 | 18 | ||
@@ -28,5 +28,6 @@ struct sway_config { | |||
28 | }; | 28 | }; |
29 | 29 | ||
30 | struct sway_config *read_config(FILE *file); | 30 | struct sway_config *read_config(FILE *file); |
31 | char *do_var_replacement(struct sway_config *config, char *str); | ||
31 | 32 | ||
32 | #endif | 33 | #endif |
diff --git a/sway/stringop.c b/sway/stringop.c index 4b05e657..bbc0bcdf 100644 --- a/sway/stringop.c +++ b/sway/stringop.c | |||
@@ -148,3 +148,19 @@ int unescape_string(char *string) { | |||
148 | } | 148 | } |
149 | return len; | 149 | return len; |
150 | } | 150 | } |
151 | |||
152 | char *join_args(char **argv, int argc) { | ||
153 | int len = 0, i; | ||
154 | for (i = 0; i < argc; ++i) { | ||
155 | len += strlen(argv[i]) + 1; | ||
156 | } | ||
157 | char *res = malloc(len); | ||
158 | len = 0; | ||
159 | for (i = 0; i < argc; ++i) { | ||
160 | strcpy(res + len, argv[i]); | ||
161 | len += strlen(argv[i]); | ||
162 | res[len++] = ' '; | ||
163 | } | ||
164 | res[len - 1] = '\0'; | ||
165 | return res; | ||
166 | } | ||
diff --git a/sway/stringop.h b/sway/stringop.h index 74f34ebf..a5346829 100644 --- a/sway/stringop.h +++ b/sway/stringop.h | |||
@@ -9,5 +9,6 @@ void free_flat_list(list_t *list); | |||
9 | char *code_strchr(const char *string, char delimiter); | 9 | char *code_strchr(const char *string, char delimiter); |
10 | char *code_strstr(const char *haystack, const char *needle); | 10 | char *code_strstr(const char *haystack, const char *needle); |
11 | int unescape_string(char *string); | 11 | int unescape_string(char *string); |
12 | char *join_args(char **argv, int argc); | ||
12 | 13 | ||
13 | #endif | 14 | #endif |