source: keybinding.c @ de8945b

release-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since de8945b was 12bc46a, checked in by Anders Kaseorg <andersk@mit.edu>, 15 years ago
Add const qualifiers for owl_keyhandler *. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[7d4fbcd]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 */
[e19eb97]14int owl_keybinding_init(owl_keybinding *kb, const char *keyseq, const char *command, void (*function_fn)(void), const char *desc)
[cf83b7a]15{
[7d4fbcd]16  char **ktokens;
17  int    nktokens, i;
18 
[176d3443]19  owl_function_debugmsg("owl_keybinding_init: creating binding for <%s> with desc: <%s>", keyseq, desc);
[7d4fbcd]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  }
[e1b136bf]34  kb->keys = owl_malloc(nktokens*sizeof(int));
[7d4fbcd]35  for (i=0; i<nktokens; i++) {
[e1b136bf]36    kb->keys[i] = owl_keypress_fromstring(ktokens[i]);
37    if (kb->keys[i] == ERR) { 
[7d4fbcd]38      atokenize_free(ktokens, nktokens);
[e1b136bf]39      owl_free(kb->keys);
[7d4fbcd]40      return(-1);
41    }
42  }
[e1b136bf]43  kb->len = nktokens;
[7d4fbcd]44
[1716fed]45  atokenize_free(ktokens, nktokens);
46
[7d4fbcd]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{
[e1b136bf]57  if (kb->keys) owl_free(kb->keys);
[7d4fbcd]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 */
[f1d7d0f]70void owl_keybinding_execute(const owl_keybinding *kb, int j)
[cf83b7a]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 */
[e1b136bf]80int owl_keybinding_stack_tostring(int *j, int len, char *buff, int bufflen)
[cf83b7a]81{
[7d4fbcd]82  char *pos = buff;
83  int   rem = bufflen;
84  int   i, n;
85
[e1b136bf]86  for (i=0; i < len; i++) {
[7d4fbcd]87    owl_keypress_tostring(j[i], 0, pos, rem-1);
[e1b136bf]88    if (i < len - 1) strcat(pos, " ");
[7d4fbcd]89    n = strlen(pos);
90    pos += n;
91    rem -= n;
92  }
93  return 0;
94}
95
96/* returns 0 on success */
[f1d7d0f]97int owl_keybinding_tostring(const owl_keybinding *kb, char *buff, int bufflen)
[cf83b7a]98{
[e1b136bf]99  return owl_keybinding_stack_tostring(kb->keys, kb->len, buff, bufflen);
[7d4fbcd]100}
101
[f1d7d0f]102const char *owl_keybinding_get_desc(const owl_keybinding *kb)
[cf83b7a]103{
[7d4fbcd]104  return kb->desc;
105}
106
107/* returns 0 on no match, 1 on subset match, and 2 on complete match */
[12bc46a]108int owl_keybinding_match(const owl_keybinding *kb, const owl_keyhandler *kh)
[cf83b7a]109{
[e1b136bf]110  int i;
111  for(i = 0; i <= kh->kpstackpos && i < kb->len; i++) {
112    if(kb->keys[i] != kh->kpstack[i])
[7d4fbcd]113      return 0;
114  }
[e1b136bf]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 */
[7d4fbcd]119    return 2;
[e1b136bf]120  } else if(kb->len > kh->kpstackpos + 1) {
[7d4fbcd]121    return 1;
122  }
[e1b136bf]123
124  return 0;
[7d4fbcd]125}
126
127/* returns 1 if keypress sequence is the same */
[f1d7d0f]128int owl_keybinding_equal(const owl_keybinding *kb1, const owl_keybinding *kb2)
[cf83b7a]129{
[e1b136bf]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;
[7d4fbcd]137  }
[e1b136bf]138
139  return 1;
[7d4fbcd]140}
Note: See TracBrowser for help on using the repository browser.