source: keybinding.c @ 6a20996

release-1.10release-1.9
Last change on this file since 6a20996 was f271129, checked in by Jason Gross <jgross@mit.edu>, 13 years ago
Fix up headers The additions to owl.h and some of the removals were done by Alejandro Sedeño <asedeno@mit.edu> in commit 77a0258b3919468fc9d7f7602588ac427ab36e6c. Notes: * I think owl.c lost the need for sys/time.h when we punted select() in favor of glib's main loop. * We don't actually need to include things like stdarg.h, stdio.h, glib/gstdio.h, glib-object.h. I think they get indirectly included via owl.h and/or glib.h. They're left in (or added in to) the files that use functions/types from them. * I'm not entirely sure what sys/socket.h is doing in message.c. It is there from the initial commit. I suspect it might have had something to do with the call to getnameinfo. message.c compiles without it, but http://pubs.opengroup.org/onlinepubs/009695399/functions/getnameinfo.html suggests that we're supposed to include it? *shrugs* I'm leaving it in, for now. (Rather, I'll leave one copy of the #include in.)
  • Property mode set to 100644
File size: 3.5 KB
Line 
1#include "owl.h"
2
3/*
4 * TODO: Idea for allowing functions to be user-specified ---
5 *      Have function have a context bitmask that says where it
6 *      can be used, and have keymaps also have one, and compare
7 *      the two when setting.
8 *     
9 */
10
11static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq);
12
13/* sets up a new keybinding for a command */
14CALLER_OWN owl_keybinding *owl_keybinding_new(const char *keyseq, const char *command, void (*function_fn)(void), const char *desc)
15{
16  owl_keybinding *kb = g_new(owl_keybinding, 1);
17
18  owl_function_debugmsg("owl_keybinding_init: creating binding for <%s> with desc: <%s>", keyseq, desc);
19  if (command && function_fn) {
20    g_free(kb);
21    return NULL;
22  } else 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    kb->type = OWL_KEYBINDING_NOOP;
28  }
29
30  if (owl_keybinding_make_keys(kb, keyseq) != 0) {
31    g_free(kb);
32    return NULL;
33  }
34
35  kb->command = g_strdup(command);
36  kb->function_fn = function_fn;
37  kb->desc = g_strdup(desc);
38  return kb;
39}
40
41static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq)
42{
43  char **ktokens;
44  int    nktokens, i;
45
46  ktokens = g_strsplit_set(keyseq, " ", 0);
47  nktokens = g_strv_length(ktokens);
48  if (nktokens < 1 || nktokens > OWL_KEYMAP_MAXSTACK) {
49    g_strfreev(ktokens);
50    return(-1);
51  }
52  kb->keys = g_new(int, nktokens);
53  for (i=0; i<nktokens; i++) {
54    kb->keys[i] = owl_keypress_fromstring(ktokens[i]);
55    if (kb->keys[i] == ERR) {
56      g_strfreev(ktokens);
57      g_free(kb->keys);
58      return(-1);
59    }
60  }
61  kb->len = nktokens;
62  g_strfreev(ktokens);
63  return(0);
64}
65
66/* Releases data associated with a keybinding, and the kb itself */
67void owl_keybinding_delete(owl_keybinding *kb)
68{
69  g_free(kb->keys);
70  g_free(kb->desc);
71  g_free(kb->command);
72  g_free(kb);
73}
74
75/* executes a keybinding */
76void owl_keybinding_execute(const owl_keybinding *kb, int j)
77{
78  if (kb->type == OWL_KEYBINDING_COMMAND && kb->command) {
79    owl_function_command_norv(kb->command);
80  } else if (kb->type == OWL_KEYBINDING_FUNCTION && kb->function_fn) {
81    kb->function_fn();
82  }
83}
84
85CALLER_OWN char *owl_keybinding_stack_tostring(int *j, int len)
86{
87  GString *string;
88  int  i;
89
90  string = g_string_new("");
91  for (i = 0; i < len; i++) {
92    char *keypress = owl_keypress_tostring(j[i], 0);
93    g_string_append(string, keypress ? keypress : "INVALID");
94    g_free(keypress);
95    if (i < len - 1) g_string_append_c(string, ' ');
96  }
97  return g_string_free(string, false);
98}
99
100CALLER_OWN char *owl_keybinding_tostring(const owl_keybinding *kb)
101{
102  return owl_keybinding_stack_tostring(kb->keys, kb->len);
103}
104
105const char *owl_keybinding_get_desc(const owl_keybinding *kb)
106{
107  return kb->desc;
108}
109
110/* returns 0 on no match, 1 on subset match, and 2 on complete match */
111int owl_keybinding_match(const owl_keybinding *kb, const owl_keyhandler *kh)
112{
113  int i;
114  for(i = 0; i <= kh->kpstackpos && i < kb->len; i++) {
115    if(kb->keys[i] != kh->kpstack[i])
116      return 0;
117  }
118
119  /* If we've made it to this point, then they match as far as they are. */
120  if(kb->len == kh->kpstackpos + 1) {
121    /* Equal length */
122    return 2;
123  } else if(kb->len > kh->kpstackpos + 1) {
124    return 1;
125  }
126
127  return 0;
128}
129
130/* returns 1 if keypress sequence is the same */
131int owl_keybinding_equal(const owl_keybinding *kb1, const owl_keybinding *kb2)
132{
133  int i;
134
135  if(kb1->len != kb2->len) return 0;
136
137  for(i = 0; i < kb1->len; i++) {
138    if(kb1->keys[i] != kb2->keys[i])
139      return 0;
140  }
141
142  return 1;
143}
Note: See TracBrowser for help on using the repository browser.