source: keybinding.c @ 2527615

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 2527615 was 1aee7d9, checked in by Erik Nygren <nygren@mit.edu>, 22 years ago
* Added RCS Id strings to all files. * 'show keymaps' shows details of all keymaps after summary list.
  • Property mode set to 100644
File size: 3.2 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  char **ktokens;
18  int    nktokens, i;
19 
20  owl_function_debugmsg("creating binding for <%s> with desc: <%s>\n", keyseq, desc);
21  if (command && !function_fn) {
22    kb->type = OWL_KEYBINDING_COMMAND;
23  } else if (!command && function_fn) {
24    kb->type = OWL_KEYBINDING_FUNCTION;
25  } else {
26    return(-1);
27  }
28
29  ktokens = atokenize(keyseq, " ", &nktokens);
30  if (!ktokens) return(-1);
31  if (nktokens > OWL_KEYMAP_MAXSTACK) {
32    atokenize_free(ktokens, nktokens);
33    return(-1);
34  }
35  kb->j = owl_malloc((nktokens+1)*sizeof(int));
36  for (i=0; i<nktokens; i++) {
37    kb->j[i] = owl_keypress_fromstring(ktokens[i]);
38    if (kb->j[i] == ERR) { 
39      atokenize_free(ktokens, nktokens);
40      owl_free(kb->j);
41      return(-1);
42    }
43  }
44  kb->j[i] = 0;
45
46  if (command) kb->command = owl_strdup(command);
47  kb->function_fn = function_fn;
48  if (desc) kb->desc = owl_strdup(desc);
49  else kb->desc = NULL;
50  return(0);
51}
52
53/* Releases data associated with a keybinding */
54void owl_keybinding_free(owl_keybinding *kb) {
55  if (kb->j) owl_free(kb->j);
56  if (kb->desc) owl_free(kb->desc);
57  if (kb->command) owl_free(kb->command);
58}
59
60/* Releases data associated with a keybinding, and the kb itself */
61void owl_keybinding_free_all(owl_keybinding *kb) {
62  owl_keybinding_free(kb);
63  owl_free(kb);
64}
65
66/* executes a keybinding */
67void owl_keybinding_execute(owl_keybinding *kb, int j) {
68  if (kb->type == OWL_KEYBINDING_COMMAND && kb->command) {
69    owl_function_command_norv(kb->command);
70  } else if (kb->type == OWL_KEYBINDING_FUNCTION && kb->function_fn) {
71    kb->function_fn();
72  }
73}
74
75/* returns 0 on success */
76int owl_keybinding_stack_tostring(int *j, char *buff, int bufflen) {
77  char *pos = buff;
78  int   rem = bufflen;
79  int   i, n;
80
81  for (i=0; j[i]; i++) {
82    owl_keypress_tostring(j[i], 0, pos, rem-1);
83    if (j[i+1]) strcat(pos, " ");
84    n = strlen(pos);
85    pos += n;
86    rem -= n;
87  }
88  return 0;
89}
90
91/* returns 0 on success */
92int owl_keybinding_tostring(owl_keybinding *kb, char *buff, int bufflen) {
93  return owl_keybinding_stack_tostring(kb->j, buff, bufflen);
94}
95
96char *owl_keybinding_get_desc(owl_keybinding *kb) {
97  return kb->desc;
98}
99
100/* returns 0 on no match, 1 on subset match, and 2 on complete match */
101int owl_keybinding_match(owl_keybinding *kb, int *kpstack) {
102  int *kbstack = kb->j;
103 
104  while (*kbstack && *kpstack) {
105    if (*kbstack != *kpstack) {
106      return 0;
107    }
108    kbstack++; kpstack++;
109  }
110  if (!*kpstack && !*kbstack) {
111    return 2;
112  } else if (!*kpstack) {
113    return 1;
114  } else {
115    return 0;
116  }
117}
118
119/* returns 1 if keypress sequence is the same */
120int owl_keybinding_equal(owl_keybinding *kb1, owl_keybinding *kb2) {
121  int *j1 = kb1->j;
122  int *j2 = kb2->j;
123  while (*j1 && *j2) {
124    if (*(j1++) != *(j2++))  return(0);
125  }
126  if (*j1 != *j2) return(0);
127  return(1);
128}
Note: See TracBrowser for help on using the repository browser.