source: keybinding.c @ 601733d

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