aboutsummaryrefslogtreecommitdiff
path: root/src/binds.c
blob: e0426e615595807358b5ae874d40fc2f9b29f032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#include "binds.h"
#include "list.h"

#include <stdbool.h>

struct bind_node {
  char *key; /* input key to reach this node */
  char *command; /* command to run for this node, or NULL if not leaf node */
  struct list *suffixes; /* list of bind_node* suffixes, or NULL if leaf node */
};

struct imv_binds {
  struct bind_node bind_tree;
  struct list *keys;
};

static int compare_node_key(const void* item, const void *key)
{
  const struct bind_node *node = item;
  const char *keystr = key;
  return strcmp(node->key, keystr);
}

static void init_bind_node(struct bind_node *bn)
{
  bn->key = NULL;
  bn->command = NULL;
  bn->suffixes = list_create();
}

static void destroy_bind_node(struct bind_node *bn)
{
  for(size_t i = 0; i < bn->suffixes->len; ++i) {
    destroy_bind_node(bn->suffixes->items[i]);
  }
  free(bn->key);
  free(bn->command);
  list_deep_free(bn->suffixes);
}

struct imv_binds *imv_binds_create(void)
{
  struct imv_binds *binds = malloc(sizeof(struct imv_binds));
  init_bind_node(&binds->bind_tree);
  binds->keys = list_create();
  return binds;
}

void imv_binds_free(struct imv_binds *binds)
{
  destroy_bind_node(&binds->bind_tree);
  free(binds);
}

enum bind_result imv_binds_add(struct imv_binds *binds, const struct list *keys, const char *command)
{
  if(!command) {
    return BIND_INVALID_COMMAND;
  }

  if(!keys) {
    return BIND_INVALID_KEYS;
  }

  /* Prepare our return code */
  int result = BIND_SUCCESS;

  /* Traverse the trie */
  struct bind_node *node = &binds->bind_tree;
  for(size_t i = 0; i < keys->len; ++i) {
    /* If we've reached a node that already has a command, there's a conflict */
    if(node->command) {
      result = BIND_INVALID_COMMAND;
      break;
    }

    /* Find / create a child with the current key */
    struct bind_node *next_node = NULL;
    int child_index = list_find(node->suffixes, &compare_node_key, keys->items[i]);
    if(child_index == -1) {
      /* Create our new node */
      next_node = malloc(sizeof(struct bind_node));
      init_bind_node(next_node);
      next_node->key = strdup(keys->items[i]);
      list_append(node->suffixes, next_node);
    } else {
      next_node = node->suffixes->items[child_index];
    }

    /* We've now found the correct node for this key */

    /* Check if the node has a command */
    if(next_node->command) {
      if(i + 1 < keys->len) {
        /* If we're not at the end, it's a conflict */
        result = BIND_CONFLICTS;
        break;
      } else {
        /* Otherwise we just need to overwrite the existing bind. */
        free(next_node->command);
        next_node->command = strdup(command);
        result = BIND_SUCCESS;
        break;
      }
    }

    if(i + 1 == keys->len) {
      /* this is the last key part, try to insert command */
      /* but first, make sure there's no children */
      if(next_node->suffixes->len > 0) {
        result = BIND_CONFLICTS;
      } else {
        next_node->command = strdup(command);
      }
    } else {
      /* Otherwise, move down the trie */
      node = next_node;
    }
  }

  return result;
}

void imv_binds_clear(struct imv_binds *binds)
{
  destroy_bind_node(&binds->bind_tree);
  init_bind_node(&binds->bind_tree);
}

enum lookup_result {
  LOOKUP_PARTIAL,
  LOOKUP_INVALID,
  LOOKUP_MATCH,
};

static enum lookup_result bind_lookup(struct bind_node *node, struct list *keys, const char **out_str)
{
  for(size_t part = 0; part < keys->len; ++part) {
    const char* cur_key = keys->items[part];
    int found = 0;
    for(size_t i = 0; i < node->suffixes->len; ++i) {
      struct bind_node* cur_node = node->suffixes->items[i];
      if(strcmp(cur_node->key, cur_key) == 0) {
        node = node->suffixes->items[i];
        found = 1;
        break;
      }
    }
    if(!found) {
      return LOOKUP_INVALID;
    }
  }

  if(node->command) {
    *out_str = node->command;
    return LOOKUP_MATCH;
  }
  return LOOKUP_PARTIAL;
}

static int print_event(char *buf, size_t len, const SDL_Event *event)
{
  /* only accept keydown events */
  if(event->type != SDL_KEYDOWN) {
    buf[0] = 0;
    return 0;
  }

  const SDL_KeyboardEvent *kevent = &event->key;

  /* filter out modifier keys */
  switch(kevent->keysym.sym) {
    case SDLK_LCTRL:
    case SDLK_RCTRL:
    case SDLK_LALT:
    case SDLK_RALT:
    case SDLK_LSHIFT:
    case SDLK_RSHIFT:
      return 0;
  }

  /* Build prefix first: */
  char prefix[16] = {0};
  snprintf(prefix, sizeof prefix, "%s%s%s",
      SDL_GetModState() & KMOD_CTRL ? "Ctrl+" : "",
      SDL_GetModState() & KMOD_ALT ? "Meta+" : "",
      SDL_GetModState() & KMOD_SHIFT ? "Shift+" : "");

  /* Try plain old character input */
  const char *keyname = SDL_GetKeyName(kevent->keysym.sym);

  /* Because '<' and '>' have special meaning in our syntax, and '=', '[', and
   * ']' are restricted within ini files, we rename these.  */
  if(!strcmp(keyname, "<")) {
    keyname = "Less";
  } else if(!strcmp(keyname, ">")) {
    keyname = "Greater";
  } else if(!strcmp(keyname, "=")) {
    keyname = "Equals";
  } else if(!strcmp(keyname, "[")) {
    keyname = "LeftSquareBracket";
  } else if(!strcmp(keyname, "]")) {
    keyname = "RightSquareBracket";
  }

  return snprintf(buf, len, "%s%s", prefix, keyname);
}

void imv_bind_clear_input(struct imv_binds *binds)
{
  list_deep_free(binds->keys);
  binds->keys = list_create();
}

const char *imv_bind_handle_event(struct imv_binds *binds, const SDL_Event *event)
{
  if(event->key.keysym.sym == SDLK_ESCAPE) {
    imv_bind_clear_input(binds);
    return NULL;
  }

  char buffer[128];
  print_event(buffer, sizeof(buffer), event);
  list_append(binds->keys, strdup(buffer));

  const char *command = NULL;
  enum lookup_result result = bind_lookup(&binds->bind_tree, binds->keys, &command);
  if(result == LOOKUP_PARTIAL) {
    return NULL;
  } else if(result == LOOKUP_MATCH) {
    imv_bind_clear_input(binds);
    return command;
  } else if(result == LOOKUP_INVALID) {
    imv_bind_clear_input(binds);
    return NULL;
  }

  /* Should not happen */
  imv_bind_clear_input(binds);
  return NULL;
}

struct list *imv_bind_parse_keys(const char *keys)
{
  struct list *list = list_create();

  /* Iterate through the string, breaking it into its parts */
  while(*keys) {

    if(*keys == '<') {
      /* Keyname block, need to extract the name, and convert it */
      const char *end = keys;
      while(*end && *end != '>') {
        ++end;
      }
      if(*end == '>') {
        /* We've got a <stuff> block. Check if it's a valid special key name. */
        const size_t key_len = end - keys;
        char *key = malloc(key_len);
        memcpy(key, keys + 1 /* skip the '<' */, key_len - 1);
        key[key_len - 1] = 0;
        list_append(list, key);
        keys = end + 1;
      } else {
        /* A <key> block that didn't have a closing '<'. Abort. */
        list_deep_free(list);
        return NULL;
      }
    } else {
      /* Just a regular character */
      char *item = malloc(2);
      item[0] = *keys;
      item[1] = 0;
      list_append(list, item);
      ++keys;
    }
  }

  return list;
}

size_t imv_bind_print_keylist(const struct list *keys, char *buf, size_t len)
{
  size_t printed = 0;

  /* iterate through all the keys, wrapping them in '<' and '>' if needed */
  for(size_t i = 0; i < keys->len; ++i) {
    const char *key = keys->items[i];
    const char *format = strlen(key) > 1 ? "<%s>" : "%s";
    printed += snprintf(buf, len - printed, format, key);
  }
  return printed;
}