source: keybinding.c @ b13daa0

release-1.10release-1.8release-1.9
Last change on this file since b13daa0 was b13daa0, checked in by David Benjamin <davidben@mit.edu>, 13 years ago
Make owl_keybinding new/delete instead of init/cleanup Valgrind is deeply disappointed in this code... Hide owl_keybinding_make_keys again, so we don't construct half-baked owl_keybinding instances. Instead allow for creating a dummy keybinding. Also, initialize all fields of owl_keybinding, or freeing it afterwards explodes. Also fix a memory leak in unbindkey.
  • Property mode set to 100644
File size: 3.6 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
[b13daa0]13static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq);
14
[7d4fbcd]15/* sets up a new keybinding for a command */
[b13daa0]16owl_keybinding *owl_keybinding_new(const char *keyseq, const char *command, void (*function_fn)(void), const char *desc)
[cf83b7a]17{
[b13daa0]18  owl_keybinding *kb = g_new(owl_keybinding, 1);
19
[176d3443]20  owl_function_debugmsg("owl_keybinding_init: creating binding for <%s> with desc: <%s>", keyseq, desc);
[b13daa0]21  if (command && function_fn) {
22    g_free(kb);
23    return NULL;
24  } else if (command && !function_fn) {
[7d4fbcd]25    kb->type = OWL_KEYBINDING_COMMAND;
26  } else if (!command && function_fn) {
27    kb->type = OWL_KEYBINDING_FUNCTION;
28  } else {
[b13daa0]29    kb->type = OWL_KEYBINDING_NOOP;
[7d4fbcd]30  }
31
[5643f99]32  if (owl_keybinding_make_keys(kb, keyseq) != 0) {
[b13daa0]33    g_free(kb);
34    return NULL;
[5643f99]35  }
36
[b13daa0]37  kb->command = g_strdup(command);
[5643f99]38  kb->function_fn = function_fn;
[b13daa0]39  kb->desc = g_strdup(desc);
40  return kb;
[5643f99]41}
42
[b13daa0]43static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq)
[5643f99]44{
45  char **ktokens;
46  int    nktokens, i;
47
[d275eb2]48  ktokens = g_strsplit_set(keyseq, " ", 0);
49  nktokens = g_strv_length(ktokens);
50  if (nktokens < 1 || nktokens > OWL_KEYMAP_MAXSTACK) {
51    g_strfreev(ktokens);
[7d4fbcd]52    return(-1);
53  }
[96828e4]54  kb->keys = g_new(int, nktokens);
[8a921b5]55  for (i=0; i<nktokens; i++) {
[e1b136bf]56    kb->keys[i] = owl_keypress_fromstring(ktokens[i]);
[d275eb2]57    if (kb->keys[i] == ERR) {
58      g_strfreev(ktokens);
[ddbbcffa]59      g_free(kb->keys);
[7d4fbcd]60      return(-1);
61    }
62  }
[e1b136bf]63  kb->len = nktokens;
[d275eb2]64  g_strfreev(ktokens);
[7d4fbcd]65  return(0);
66}
67
[b13daa0]68/* Releases data associated with a keybinding, and the kb itself */
69void owl_keybinding_delete(owl_keybinding *kb)
[cf83b7a]70{
[ddbbcffa]71  if (kb->keys) g_free(kb->keys);
72  if (kb->desc) g_free(kb->desc);
73  if (kb->command) g_free(kb->command);
74  g_free(kb);
[7d4fbcd]75}
76
77/* executes a keybinding */
[f1d7d0f]78void owl_keybinding_execute(const owl_keybinding *kb, int j)
[cf83b7a]79{
[7d4fbcd]80  if (kb->type == OWL_KEYBINDING_COMMAND && kb->command) {
81    owl_function_command_norv(kb->command);
82  } else if (kb->type == OWL_KEYBINDING_FUNCTION && kb->function_fn) {
83    kb->function_fn();
84  }
85}
86
87/* returns 0 on success */
[e1b136bf]88int owl_keybinding_stack_tostring(int *j, int len, char *buff, int bufflen)
[cf83b7a]89{
[7d4fbcd]90  char *pos = buff;
91  int   rem = bufflen;
92  int   i, n;
93
[e1b136bf]94  for (i=0; i < len; i++) {
[7d4fbcd]95    owl_keypress_tostring(j[i], 0, pos, rem-1);
[e1b136bf]96    if (i < len - 1) strcat(pos, " ");
[7d4fbcd]97    n = strlen(pos);
98    pos += n;
99    rem -= n;
100  }
101  return 0;
102}
103
104/* returns 0 on success */
[f1d7d0f]105int owl_keybinding_tostring(const owl_keybinding *kb, char *buff, int bufflen)
[cf83b7a]106{
[e1b136bf]107  return owl_keybinding_stack_tostring(kb->keys, kb->len, buff, bufflen);
[7d4fbcd]108}
109
[f1d7d0f]110const char *owl_keybinding_get_desc(const owl_keybinding *kb)
[cf83b7a]111{
[7d4fbcd]112  return kb->desc;
113}
114
115/* returns 0 on no match, 1 on subset match, and 2 on complete match */
[12bc46a]116int owl_keybinding_match(const owl_keybinding *kb, const owl_keyhandler *kh)
[cf83b7a]117{
[e1b136bf]118  int i;
119  for(i = 0; i <= kh->kpstackpos && i < kb->len; i++) {
120    if(kb->keys[i] != kh->kpstack[i])
[7d4fbcd]121      return 0;
122  }
[e1b136bf]123
124  /* If we've made it to this point, then they match as far as they are. */
125  if(kb->len == kh->kpstackpos + 1) {
126    /* Equal length */
[7d4fbcd]127    return 2;
[e1b136bf]128  } else if(kb->len > kh->kpstackpos + 1) {
[7d4fbcd]129    return 1;
130  }
[e1b136bf]131
132  return 0;
[7d4fbcd]133}
134
135/* returns 1 if keypress sequence is the same */
[f1d7d0f]136int owl_keybinding_equal(const owl_keybinding *kb1, const owl_keybinding *kb2)
[cf83b7a]137{
[e1b136bf]138  int i;
139
140  if(kb1->len != kb2->len) return 0;
141
142  for(i = 0; i < kb1->len; i++) {
143    if(kb1->keys[i] != kb2->keys[i])
144      return 0;
[7d4fbcd]145  }
[e1b136bf]146
147  return 1;
[7d4fbcd]148}
Note: See TracBrowser for help on using the repository browser.