source: keybinding.c @ b13daa0

release-1.10release-1.8release-1.9
Last change on this file since b13daa0 was b13daa0, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
Make owl_keybinding new/delete instead of init/cleanup Valgrind is deeply disappointed in this code... Hide owl_keybinding_make_keys again, so we don't construct half-baked owl_keybinding instances. Instead allow for creating a dummy keybinding. Also, initialize all fields of owl_keybinding, or freeing it afterwards explodes. Also fix a memory leak in unbindkey.
  • Property mode set to 100644
File size: 3.6 KB
Line 
1#include <ctype.h>
2#include <string.h>
3#include "owl.h"
4
5/*
6 * TODO: Idea for allowing functions to be user-specified ---
7 *      Have function have a context bitmask that says where it
8 *      can be used, and have keymaps also have one, and compare
9 *      the two when setting.
10 *     
11 */
12
13static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq);
14
15/* sets up a new keybinding for a command */
16owl_keybinding *owl_keybinding_new(const char *keyseq, const char *command, void (*function_fn)(void), const char *desc)
17{
18  owl_keybinding *kb = g_new(owl_keybinding, 1);
19
20  owl_function_debugmsg("owl_keybinding_init: creating binding for <%s> with desc: <%s>", keyseq, desc);
21  if (command && function_fn) {
22    g_free(kb);
23    return NULL;
24  } else if (command && !function_fn) {
25    kb->type = OWL_KEYBINDING_COMMAND;
26  } else if (!command && function_fn) {
27    kb->type = OWL_KEYBINDING_FUNCTION;
28  } else {
29    kb->type = OWL_KEYBINDING_NOOP;
30  }
31
32  if (owl_keybinding_make_keys(kb, keyseq) != 0) {
33    g_free(kb);
34    return NULL;
35  }
36
37  kb->command = g_strdup(command);
38  kb->function_fn = function_fn;
39  kb->desc = g_strdup(desc);
40  return kb;
41}
42
43static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq)
44{
45  char **ktokens;
46  int    nktokens, i;
47
48  ktokens = g_strsplit_set(keyseq, " ", 0);
49  nktokens = g_strv_length(ktokens);
50  if (nktokens < 1 || nktokens > OWL_KEYMAP_MAXSTACK) {
51    g_strfreev(ktokens);
52    return(-1);
53  }
54  kb->keys = g_new(int, nktokens);
55  for (i=0; i<nktokens; i++) {
56    kb->keys[i] = owl_keypress_fromstring(ktokens[i]);
57    if (kb->keys[i] == ERR) {
58      g_strfreev(ktokens);
59      g_free(kb->keys);
60      return(-1);
61    }
62  }
63  kb->len = nktokens;
64  g_strfreev(ktokens);
65  return(0);
66}
67
68/* Releases data associated with a keybinding, and the kb itself */
69void owl_keybinding_delete(owl_keybinding *kb)
70{
71  if (kb->keys) g_free(kb->keys);
72  if (kb->desc) g_free(kb->desc);
73  if (kb->command) g_free(kb->command);
74  g_free(kb);
75}
76
77/* executes a keybinding */
78void owl_keybinding_execute(const owl_keybinding *kb, int j)
79{
80  if (kb->type == OWL_KEYBINDING_COMMAND && kb->command) {
81    owl_function_command_norv(kb->command);
82  } else if (kb->type == OWL_KEYBINDING_FUNCTION && kb->function_fn) {
83    kb->function_fn();
84  }
85}
86
87/* returns 0 on success */
88int owl_keybinding_stack_tostring(int *j, int len, char *buff, int bufflen)
89{
90  char *pos = buff;
91  int   rem = bufflen;
92  int   i, n;
93
94  for (i=0; i < len; i++) {
95    owl_keypress_tostring(j[i], 0, pos, rem-1);
96    if (i < len - 1) strcat(pos, " ");
97    n = strlen(pos);
98    pos += n;
99    rem -= n;
100  }
101  return 0;
102}
103
104/* returns 0 on success */
105int owl_keybinding_tostring(const owl_keybinding *kb, char *buff, int bufflen)
106{
107  return owl_keybinding_stack_tostring(kb->keys, kb->len, buff, bufflen);
108}
109
110const char *owl_keybinding_get_desc(const owl_keybinding *kb)
111{
112  return kb->desc;
113}
114
115/* returns 0 on no match, 1 on subset match, and 2 on complete match */
116int owl_keybinding_match(const owl_keybinding *kb, const owl_keyhandler *kh)
117{
118  int i;
119  for(i = 0; i <= kh->kpstackpos && i < kb->len; i++) {
120    if(kb->keys[i] != kh->kpstack[i])
121      return 0;
122  }
123
124  /* If we've made it to this point, then they match as far as they are. */
125  if(kb->len == kh->kpstackpos + 1) {
126    /* Equal length */
127    return 2;
128  } else if(kb->len > kh->kpstackpos + 1) {
129    return 1;
130  }
131
132  return 0;
133}
134
135/* returns 1 if keypress sequence is the same */
136int owl_keybinding_equal(const owl_keybinding *kb1, const owl_keybinding *kb2)
137{
138  int i;
139
140  if(kb1->len != kb2->len) return 0;
141
142  for(i = 0; i < kb1->len; i++) {
143    if(kb1->keys[i] != kb2->keys[i])
144      return 0;
145  }
146
147  return 1;
148}
Note: See TracBrowser for help on using the repository browser.