forked from aglasgall/barnowl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keybinding.c
143 lines (122 loc) · 3.61 KB
/
keybinding.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "owl.h"
/*
* TODO: Idea for allowing functions to be user-specified ---
* Have function have a context bitmask that says where it
* can be used, and have keymaps also have one, and compare
* the two when setting.
*
*/
static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq);
/* sets up a new keybinding for a command */
CALLER_OWN owl_keybinding *owl_keybinding_new(const char *keyseq, const char *command, void (*function_fn)(void), const char *desc)
{
owl_keybinding *kb = g_slice_new(owl_keybinding);
owl_function_debugmsg("owl_keybinding_init: creating binding for <%s> with desc: <%s>", keyseq, desc);
if (command && function_fn) {
g_slice_free(owl_keybinding, kb);
return NULL;
} else if (command && !function_fn) {
kb->type = OWL_KEYBINDING_COMMAND;
} else if (!command && function_fn) {
kb->type = OWL_KEYBINDING_FUNCTION;
} else {
kb->type = OWL_KEYBINDING_NOOP;
}
if (owl_keybinding_make_keys(kb, keyseq) != 0) {
g_slice_free(owl_keybinding, kb);
return NULL;
}
kb->command = g_strdup(command);
kb->function_fn = function_fn;
kb->desc = g_strdup(desc);
return kb;
}
static int owl_keybinding_make_keys(owl_keybinding *kb, const char *keyseq)
{
char **ktokens;
int nktokens, i;
ktokens = g_strsplit_set(keyseq, " ", 0);
nktokens = g_strv_length(ktokens);
if (nktokens < 1 || nktokens > OWL_KEYMAP_MAXSTACK) {
g_strfreev(ktokens);
return(-1);
}
kb->keys = g_new(int, nktokens);
for (i=0; i<nktokens; i++) {
kb->keys[i] = owl_keypress_fromstring(ktokens[i]);
if (kb->keys[i] == ERR) {
g_strfreev(ktokens);
g_free(kb->keys);
return(-1);
}
}
kb->len = nktokens;
g_strfreev(ktokens);
return(0);
}
/* Releases data associated with a keybinding, and the kb itself */
void owl_keybinding_delete(owl_keybinding *kb)
{
g_free(kb->keys);
g_free(kb->desc);
g_free(kb->command);
g_slice_free(owl_keybinding, kb);
}
/* executes a keybinding */
void owl_keybinding_execute(const owl_keybinding *kb, int j)
{
if (kb->type == OWL_KEYBINDING_COMMAND && kb->command) {
owl_function_command_norv(kb->command);
} else if (kb->type == OWL_KEYBINDING_FUNCTION && kb->function_fn) {
kb->function_fn();
}
}
CALLER_OWN char *owl_keybinding_stack_tostring(int *j, int len)
{
GString *string;
int i;
string = g_string_new("");
for (i = 0; i < len; i++) {
char *keypress = owl_keypress_tostring(j[i], 0);
g_string_append(string, keypress ? keypress : "INVALID");
g_free(keypress);
if (i < len - 1) g_string_append_c(string, ' ');
}
return g_string_free(string, false);
}
CALLER_OWN char *owl_keybinding_tostring(const owl_keybinding *kb)
{
return owl_keybinding_stack_tostring(kb->keys, kb->len);
}
const char *owl_keybinding_get_desc(const owl_keybinding *kb)
{
return kb->desc;
}
/* returns 0 on no match, 1 on subset match, and 2 on complete match */
int owl_keybinding_match(const owl_keybinding *kb, const owl_keyhandler *kh)
{
int i;
for(i = 0; i <= kh->kpstackpos && i < kb->len; i++) {
if(kb->keys[i] != kh->kpstack[i])
return 0;
}
/* If we've made it to this point, then they match as far as they are. */
if(kb->len == kh->kpstackpos + 1) {
/* Equal length */
return 2;
} else if(kb->len > kh->kpstackpos + 1) {
return 1;
}
return 0;
}
/* returns 1 if keypress sequence is the same */
int owl_keybinding_equal(const owl_keybinding *kb1, const owl_keybinding *kb2)
{
int i;
if(kb1->len != kb2->len) return 0;
for(i = 0; i < kb1->len; i++) {
if(kb1->keys[i] != kb2->keys[i])
return 0;
}
return 1;
}