source: keybinding.c @ 4724f1a

release-1.10release-1.9
Last change on this file since 4724f1a 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
RevLine 
[7d4fbcd]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
[b13daa0]11static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq);
12
[7d4fbcd]13/* sets up a new keybinding for a command */
[6829afc]14CALLER_OWN owl_keybinding *owl_keybinding_new(const char *keyseq, const char *command, void (*function_fn)(void), const char *desc)
[cf83b7a]15{
[b13daa0]16  owl_keybinding *kb = g_new(owl_keybinding, 1);
17
[176d3443]18  owl_function_debugmsg("owl_keybinding_init: creating binding for <%s> with desc: <%s>", keyseq, desc);
[b13daa0]19  if (command && function_fn) {
20    g_free(kb);
21    return NULL;
22  } else if (command && !function_fn) {
[7d4fbcd]23    kb->type = OWL_KEYBINDING_COMMAND;
24  } else if (!command && function_fn) {
25    kb->type = OWL_KEYBINDING_FUNCTION;
26  } else {
[b13daa0]27    kb->type = OWL_KEYBINDING_NOOP;
[7d4fbcd]28  }
29
[5643f99]30  if (owl_keybinding_make_keys(kb, keyseq) != 0) {
[b13daa0]31    g_free(kb);
32    return NULL;
[5643f99]33  }
34
[b13daa0]35  kb->command = g_strdup(command);
[5643f99]36  kb->function_fn = function_fn;
[b13daa0]37  kb->desc = g_strdup(desc);
38  return kb;
[5643f99]39}
40
[b13daa0]41static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq)
[5643f99]42{
43  char **ktokens;
44  int    nktokens, i;
45
[d275eb2]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);
[7d4fbcd]50    return(-1);
51  }
[96828e4]52  kb->keys = g_new(int, nktokens);
[8a921b5]53  for (i=0; i<nktokens; i++) {
[e1b136bf]54    kb->keys[i] = owl_keypress_fromstring(ktokens[i]);
[d275eb2]55    if (kb->keys[i] == ERR) {
56      g_strfreev(ktokens);
[ddbbcffa]57      g_free(kb->keys);
[7d4fbcd]58      return(-1);
59    }
60  }
[e1b136bf]61  kb->len = nktokens;
[d275eb2]62  g_strfreev(ktokens);
[7d4fbcd]63  return(0);
64}
65
[b13daa0]66/* Releases data associated with a keybinding, and the kb itself */
67void owl_keybinding_delete(owl_keybinding *kb)
[cf83b7a]68{
[3b8a563]69  g_free(kb->keys);
70  g_free(kb->desc);
71  g_free(kb->command);
[ddbbcffa]72  g_free(kb);
[7d4fbcd]73}
74
75/* executes a keybinding */
[f1d7d0f]76void owl_keybinding_execute(const owl_keybinding *kb, int j)
[cf83b7a]77{
[7d4fbcd]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
[6829afc]85CALLER_OWN char *owl_keybinding_stack_tostring(int *j, int len)
[cf83b7a]86{
[45e2c95]87  GString *string;
88  int  i;
89
90  string = g_string_new("");
91  for (i = 0; i < len; i++) {
[d07af84]92    char *keypress = owl_keypress_tostring(j[i], 0);
93    g_string_append(string, keypress ? keypress : "INVALID");
94    g_free(keypress);
[45e2c95]95    if (i < len - 1) g_string_append_c(string, ' ');
[7d4fbcd]96  }
[45e2c95]97  return g_string_free(string, false);
[7d4fbcd]98}
99
[6829afc]100CALLER_OWN char *owl_keybinding_tostring(const owl_keybinding *kb)
[cf83b7a]101{
[45e2c95]102  return owl_keybinding_stack_tostring(kb->keys, kb->len);
[7d4fbcd]103}
104
[f1d7d0f]105const char *owl_keybinding_get_desc(const owl_keybinding *kb)
[cf83b7a]106{
[7d4fbcd]107  return kb->desc;
108}
109
110/* returns 0 on no match, 1 on subset match, and 2 on complete match */
[12bc46a]111int owl_keybinding_match(const owl_keybinding *kb, const owl_keyhandler *kh)
[cf83b7a]112{
[e1b136bf]113  int i;
114  for(i = 0; i <= kh->kpstackpos && i < kb->len; i++) {
115    if(kb->keys[i] != kh->kpstack[i])
[7d4fbcd]116      return 0;
117  }
[e1b136bf]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 */
[7d4fbcd]122    return 2;
[e1b136bf]123  } else if(kb->len > kh->kpstackpos + 1) {
[7d4fbcd]124    return 1;
125  }
[e1b136bf]126
127  return 0;
[7d4fbcd]128}
129
130/* returns 1 if keypress sequence is the same */
[f1d7d0f]131int owl_keybinding_equal(const owl_keybinding *kb1, const owl_keybinding *kb2)
[cf83b7a]132{
[e1b136bf]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;
[7d4fbcd]140  }
[e1b136bf]141
142  return 1;
[7d4fbcd]143}
Note: See TracBrowser for help on using the repository browser.