source: keybinding.c @ 6772d19

release-1.6release-1.7release-1.8release-1.9
Last change on this file since 6772d19 was 8a921b5, checked in by Leonid Grinberg <leonidg@mit.edu>, 12 years ago
Add new command unbindkey for removing keybindings
  • 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
13/* sets up a new keybinding for a command */
14int owl_keybinding_init(owl_keybinding *kb, const char *keyseq, const char *command, void (*function_fn)(void), const char *desc)
15{
16  owl_function_debugmsg("owl_keybinding_init: creating binding for <%s> with desc: <%s>", keyseq, desc);
17  if (command && !function_fn) {
18    kb->type = OWL_KEYBINDING_COMMAND;
19  } else if (!command && function_fn) {
20    kb->type = OWL_KEYBINDING_FUNCTION;
21  } else {
22    return(-1);
23  }
24
25  if (owl_keybinding_make_keys(kb, keyseq) != 0) {
26    return(-1);
27  }
28
29  if (command) kb->command = owl_strdup(command);
30  kb->function_fn = function_fn;
31  if (desc) kb->desc = owl_strdup(desc);
32  else kb->desc = NULL;
33  return(0);
34}
35
36int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq)
37{
38  char **ktokens;
39  int    nktokens, i;
40
41  ktokens = atokenize(keyseq, " ", &nktokens);
42  if (!ktokens) return(-1);
43  if (nktokens > OWL_KEYMAP_MAXSTACK) {
44    atokenize_delete(ktokens, nktokens);
45    return(-1);
46  }
47  kb->keys = owl_malloc(nktokens*sizeof(int));
48  for (i=0; i<nktokens; i++) {
49    kb->keys[i] = owl_keypress_fromstring(ktokens[i]);
50    if (kb->keys[i] == ERR) { 
51      atokenize_delete(ktokens, nktokens);
52      owl_free(kb->keys);
53      return(-1);
54    }
55  }
56  kb->len = nktokens;
57  atokenize_delete(ktokens, nktokens);
58  return(0);
59}
60
61/* Releases data associated with a keybinding */
62void owl_keybinding_cleanup(owl_keybinding *kb)
63{
64  if (kb->keys) owl_free(kb->keys);
65  if (kb->desc) owl_free(kb->desc);
66  if (kb->command) owl_free(kb->command);
67}
68
69/* Releases data associated with a keybinding, and the kb itself */
70void owl_keybinding_delete(owl_keybinding *kb)
71{
72  owl_keybinding_cleanup(kb);
73  owl_free(kb);
74}
75
76/* executes a keybinding */
77void owl_keybinding_execute(const owl_keybinding *kb, int j)
78{
79  if (kb->type == OWL_KEYBINDING_COMMAND && kb->command) {
80    owl_function_command_norv(kb->command);
81  } else if (kb->type == OWL_KEYBINDING_FUNCTION && kb->function_fn) {
82    kb->function_fn();
83  }
84}
85
86/* returns 0 on success */
87int owl_keybinding_stack_tostring(int *j, int len, char *buff, int bufflen)
88{
89  char *pos = buff;
90  int   rem = bufflen;
91  int   i, n;
92
93  for (i=0; i < len; i++) {
94    owl_keypress_tostring(j[i], 0, pos, rem-1);
95    if (i < len - 1) strcat(pos, " ");
96    n = strlen(pos);
97    pos += n;
98    rem -= n;
99  }
100  return 0;
101}
102
103/* returns 0 on success */
104int owl_keybinding_tostring(const owl_keybinding *kb, char *buff, int bufflen)
105{
106  return owl_keybinding_stack_tostring(kb->keys, kb->len, buff, bufflen);
107}
108
109const char *owl_keybinding_get_desc(const owl_keybinding *kb)
110{
111  return kb->desc;
112}
113
114/* returns 0 on no match, 1 on subset match, and 2 on complete match */
115int owl_keybinding_match(const owl_keybinding *kb, const owl_keyhandler *kh)
116{
117  int i;
118  for(i = 0; i <= kh->kpstackpos && i < kb->len; i++) {
119    if(kb->keys[i] != kh->kpstack[i])
120      return 0;
121  }
122
123  /* If we've made it to this point, then they match as far as they are. */
124  if(kb->len == kh->kpstackpos + 1) {
125    /* Equal length */
126    return 2;
127  } else if(kb->len > kh->kpstackpos + 1) {
128    return 1;
129  }
130
131  return 0;
132}
133
134/* returns 1 if keypress sequence is the same */
135int owl_keybinding_equal(const owl_keybinding *kb1, const owl_keybinding *kb2)
136{
137  int i;
138
139  if(kb1->len != kb2->len) return 0;
140
141  for(i = 0; i < kb1->len; i++) {
142    if(kb1->keys[i] != kb2->keys[i])
143      return 0;
144  }
145
146  return 1;
147}
Note: See TracBrowser for help on using the repository browser.