source: keybinding.c @ 4692b70

barnowl_perlaimdebianowlrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 4692b70 was 176d3443, checked in by James M. Kretchmar <kretch@mit.edu>, 20 years ago
Deal gracefully with being resized as small as 1x1 [BZ 3]
  • Property mode set to 100644
File size: 3.3 KB
RevLine 
[7d4fbcd]1#include <ctype.h>
2#include <string.h>
3#include "owl.h"
4
[1aee7d9]5static const char fileIdent[] = "$Id$";
[7d4fbcd]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 */
[cf83b7a]16int owl_keybinding_init(owl_keybinding *kb, char *keyseq, char *command, void (*function_fn)(void), char *desc)
17{
[7d4fbcd]18  char **ktokens;
19  int    nktokens, i;
20 
[176d3443]21  owl_function_debugmsg("owl_keybinding_init: creating binding for <%s> with desc: <%s>", keyseq, desc);
[7d4fbcd]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  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 */
[cf83b7a]55void owl_keybinding_free(owl_keybinding *kb)
56{
[7d4fbcd]57  if (kb->j) owl_free(kb->j);
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 */
[cf83b7a]63void owl_keybinding_free_all(owl_keybinding *kb)
64{
[7d4fbcd]65  owl_keybinding_free(kb);
66  owl_free(kb);
67}
68
69/* executes a keybinding */
[cf83b7a]70void owl_keybinding_execute(owl_keybinding *kb, int j)
71{
[7d4fbcd]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 */
[cf83b7a]80int owl_keybinding_stack_tostring(int *j, char *buff, int bufflen)
81{
[7d4fbcd]82  char *pos = buff;
83  int   rem = bufflen;
84  int   i, n;
85
86  for (i=0; j[i]; i++) {
87    owl_keypress_tostring(j[i], 0, pos, rem-1);
88    if (j[i+1]) strcat(pos, " ");
89    n = strlen(pos);
90    pos += n;
91    rem -= n;
92  }
93  return 0;
94}
95
96/* returns 0 on success */
[cf83b7a]97int owl_keybinding_tostring(owl_keybinding *kb, char *buff, int bufflen)
98{
[7d4fbcd]99  return owl_keybinding_stack_tostring(kb->j, buff, bufflen);
100}
101
[cf83b7a]102char *owl_keybinding_get_desc(owl_keybinding *kb)
103{
[7d4fbcd]104  return kb->desc;
105}
106
107/* returns 0 on no match, 1 on subset match, and 2 on complete match */
[cf83b7a]108int owl_keybinding_match(owl_keybinding *kb, int *kpstack)
109{
[7d4fbcd]110  int *kbstack = kb->j;
111 
112  while (*kbstack && *kpstack) {
113    if (*kbstack != *kpstack) {
114      return 0;
115    }
116    kbstack++; kpstack++;
117  }
118  if (!*kpstack && !*kbstack) {
119    return 2;
120  } else if (!*kpstack) {
121    return 1;
122  } else {
123    return 0;
124  }
125}
126
127/* returns 1 if keypress sequence is the same */
[cf83b7a]128int owl_keybinding_equal(owl_keybinding *kb1, owl_keybinding *kb2)
129{
[7d4fbcd]130  int *j1 = kb1->j;
131  int *j2 = kb2->j;
132  while (*j1 && *j2) {
133    if (*(j1++) != *(j2++))  return(0);
134  }
135  if (*j1 != *j2) return(0);
136  return(1);
137}
Note: See TracBrowser for help on using the repository browser.