source: keybinding.c @ 7d4fbcd

barnowl_perlaimdebianowlrelease-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 7d4fbcd was 7d4fbcd, checked in by James M. Kretchmar <kretch@mit.edu>, 18 years ago
Initial check in
  • Property mode set to 100644
File size: 3.2 KB
Line 
1#include <ctype.h>
2#include <string.h>
3#include "owl.h"
4
5
6/*
7 * TODO: Idea for allowing functions to be user-specified ---
8 *      Have function have a context bitmask that says where it
9 *      can be used, and have keymaps also have one, and compare
10 *      the two when setting.
11 *     
12 */
13
14/* sets up a new keybinding for a command */
15int owl_keybinding_init(owl_keybinding *kb, char *keyseq, char *command, void (*function_fn)(void), char *desc) {
16  char **ktokens;
17  int    nktokens, i;
18 
19  owl_function_debugmsg("creating binding for <%s> with desc: <%s>\n", 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->j = owl_malloc((nktokens+1)*sizeof(int));
35  for (i=0; i<nktokens; i++) {
36    kb->j[i] = owl_keypress_fromstring(ktokens[i]);
37    if (kb->j[i] == ERR) { 
38      atokenize_free(ktokens, nktokens);
39      owl_free(kb->j);
40      return(-1);
41    }
42  }
43  kb->j[i] = 0;
44
45  if (command) kb->command = owl_strdup(command);
46  kb->function_fn = function_fn;
47  if (desc) kb->desc = owl_strdup(desc);
48  else kb->desc = NULL;
49  return(0);
50}
51
52/* Releases data associated with a keybinding */
53void owl_keybinding_free(owl_keybinding *kb) {
54  if (kb->j) owl_free(kb->j);
55  if (kb->desc) owl_free(kb->desc);
56  if (kb->command) owl_free(kb->command);
57}
58
59/* Releases data associated with a keybinding, and the kb itself */
60void owl_keybinding_free_all(owl_keybinding *kb) {
61  owl_keybinding_free(kb);
62  owl_free(kb);
63}
64
65/* executes a keybinding */
66void owl_keybinding_execute(owl_keybinding *kb, int j) {
67  if (kb->type == OWL_KEYBINDING_COMMAND && kb->command) {
68    owl_function_command_norv(kb->command);
69  } else if (kb->type == OWL_KEYBINDING_FUNCTION && kb->function_fn) {
70    kb->function_fn();
71  }
72}
73
74/* returns 0 on success */
75int owl_keybinding_stack_tostring(int *j, char *buff, int bufflen) {
76  char *pos = buff;
77  int   rem = bufflen;
78  int   i, n;
79
80  for (i=0; j[i]; i++) {
81    owl_keypress_tostring(j[i], 0, pos, rem-1);
82    if (j[i+1]) strcat(pos, " ");
83    n = strlen(pos);
84    pos += n;
85    rem -= n;
86  }
87  return 0;
88}
89
90/* returns 0 on success */
91int owl_keybinding_tostring(owl_keybinding *kb, char *buff, int bufflen) {
92  return owl_keybinding_stack_tostring(kb->j, buff, bufflen);
93}
94
95char *owl_keybinding_get_desc(owl_keybinding *kb) {
96  return kb->desc;
97}
98
99/* returns 0 on no match, 1 on subset match, and 2 on complete match */
100int owl_keybinding_match(owl_keybinding *kb, int *kpstack) {
101  int *kbstack = kb->j;
102 
103  while (*kbstack && *kpstack) {
104    if (*kbstack != *kpstack) {
105      return 0;
106    }
107    kbstack++; kpstack++;
108  }
109  if (!*kpstack && !*kbstack) {
110    return 2;
111  } else if (!*kpstack) {
112    return 1;
113  } else {
114    return 0;
115  }
116}
117
118/* returns 1 if keypress sequence is the same */
119int owl_keybinding_equal(owl_keybinding *kb1, owl_keybinding *kb2) {
120  int *j1 = kb1->j;
121  int *j2 = kb2->j;
122  while (*j1 && *j2) {
123    if (*(j1++) != *(j2++))  return(0);
124  }
125  if (*j1 != *j2) return(0);
126  return(1);
127}
Note: See TracBrowser for help on using the repository browser.