source: keybinding.c @ d4ecc78

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