home · contact · privacy
Removed redundant calls for zeroing of memory, especially where replaceable with...
[plomrogue] / src / client / keybindings.c
1 /* src/client/keybindings.c */
2
3 #include "keybindings.h"
4 #include <ncurses.h> /* keycode defines, cbreak(), halfdelay(), getch() */
5 #include <stddef.h> /* NULL */
6 #include <stdio.h> /* FILE, sprintf(), snprintf() */
7 #include <stdint.h> /* uint8_t, uint16_t, uint32_t */
8 #include <stdlib.h> /* free(), atoi() */
9 #include <string.h> /* strlen(), strchr(), strcmp() */
10 #include "../common/readwrite.h" /* textfile_sizes(), try_fgets(),try_fwrite()*/
11 #include "../common/try_malloc.h" /* try_malloc() */
12 #include "windows.h" /* draw_all_wins() */
13 #include "world.h" /* global world */
14
15
16
17 /* Return "n"-th keybinding in keybindings chain from "kb_p" on. */
18 static struct KeyBinding * get_keyb_of_n(struct KeyBinding * kb_p, uint16_t n);
19
20 /* Return number of keybindings in keybindings chain from "kb_p" on. */
21 static uint16_t get_n_of_keybs(struct KeyBinding * kb_p);
22
23 /* Return pointer to global keybindings or to keybindings for wingeometry config
24  * (c = "g") or winkeys config (c = "k") or active window's keybindings ("w").
25  */
26 static struct KeyBindingDB * char_selected_kb_db(char c);
27
28 /* If "keycode_given" equals "keycode_match", copy "keyname_match" to "keyname"
29  * and return 1; otherwise return 0.
30  */
31 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
32                            uint16_t keycode_match, char * keyname_match);
33
34
35
36 static struct KeyBinding * get_keyb_of_n(struct KeyBinding * kb_p, uint16_t n)
37 {
38     uint16_t i = 0;
39     while (1)
40     {
41         if (n == i)
42         {
43             break;
44         }
45         i++;
46         kb_p = kb_p->next;
47     }
48     return kb_p;
49 }
50
51
52
53 static uint16_t get_n_of_keybs(struct KeyBinding * kb_p)
54 {
55     uint16_t i = 0;
56     while (1)
57     {
58         if (0 == kb_p)
59         {
60             break;
61         }
62         i++;
63         kb_p = kb_p->next;
64     }
65     return i;
66 }
67
68
69
70 static struct KeyBindingDB * char_selected_kb_db(char c)
71 {
72     struct KeyBindingDB * kbd;
73     kbd = &world.kb_global;
74     if      ('g' == c)
75     {
76         kbd = &world.kb_wingeom;
77     }
78     else if ('k' == c)
79     {
80         kbd = &world.kb_winkeys;
81     }
82     else if ('w' == c)
83     {
84         struct Win * w = get_win_by_id(world.windb.active);
85         kbd = &w->kb;
86     }
87     return kbd;
88 }
89
90
91
92 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
93                            uint16_t keycode_match, char * keyname_match)
94 {
95     if (keycode_given == keycode_match)
96     {
97         sprintf(keyname, keyname_match);
98         return 1;
99     }
100     return 0;
101 }
102
103
104
105 extern struct Command * get_command_to_keycode(struct KeyBinding * kb_p,
106                                                uint16_t key)
107 {
108     while (0 != kb_p)
109     {
110         if (key == kb_p->key)
111         {
112             return kb_p->command;
113         }
114         kb_p = kb_p->next;
115     }
116     return NULL;
117 }
118
119
120
121 extern char * get_keyname_to_keycode(uint16_t keycode)
122 {
123     char * f_name = "get_name_to_keycode()";
124     char * keyname = try_malloc(15, f_name);                /* FIXME: Why 15? */
125     if (32 < keycode && keycode < 127)
126     {
127         sprintf(keyname, "%c", keycode);
128     }
129     else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
130     {
131         uint16_t f = keycode - KEY_F0;
132         sprintf(keyname, "F%d", f);
133     }
134     else if (   try_keycode(keycode, keyname, 9, "TAB")
135              || try_keycode(keycode, keyname, 10, "RETURN")
136              || try_keycode(keycode, keyname, 27, "ESCAPE")
137              || try_keycode(keycode, keyname, 32, "SPACE")
138              || try_keycode(keycode, keyname, KEY_UP, "UP")
139              || try_keycode(keycode, keyname, KEY_DOWN, "DOWN")
140              || try_keycode(keycode, keyname, KEY_LEFT, "LEFT")
141              || try_keycode(keycode, keyname, KEY_RIGHT, "RIGHT")
142              || try_keycode(keycode, keyname, KEY_HOME, "HOME")
143              || try_keycode(keycode, keyname, KEY_BACKSPACE, "BACKSPACE")
144              || try_keycode(keycode, keyname, KEY_DC, "DELETE")
145              || try_keycode(keycode, keyname, KEY_IC, "INSERT")
146              || try_keycode(keycode, keyname, KEY_NPAGE, "NEXT PAGE")
147              || try_keycode(keycode, keyname, KEY_PPAGE, "PREV PAGE")
148              || try_keycode(keycode, keyname, KEY_END, "END"))
149     {
150         ;
151     }
152     else
153     {
154         sprintf(keyname, "(unknown)");
155     }
156     return keyname;
157 }
158
159
160
161 extern void write_keybindings_to_file(FILE * file, struct KeyBindingDB * kbd,
162                                       char * delim)
163 {
164     char * f_name = "write_keybindings_to_file()";
165     uint16_t linemax = 0;
166     struct KeyBinding * kb_p = kbd->kbs;
167     while (0 != kb_p)
168     {
169         if (strlen(kb_p->command->dsc_short) > linemax)
170         {
171             linemax = strlen(kb_p->command->dsc_short);
172         }
173         kb_p = kb_p->next;
174     }
175     linemax = linemax + 6;            /* + 6 = + 3 digits + ' ' + '\n' + '\0' */
176     char line[linemax];
177     kb_p = kbd->kbs;
178     while (0 != kb_p)
179     {
180         sprintf(line, "%d %s\n", kb_p->key, kb_p->command->dsc_short);
181         try_fwrite(line, sizeof(char), strlen(line), file, f_name);
182         kb_p = kb_p->next;
183     }
184     try_fwrite(delim, strlen(delim), 1, file, f_name);
185 }
186
187
188
189 extern void read_keybindings_from_file(char * line, uint32_t linemax,
190                                        FILE * file, struct KeyBindingDB * kbd)
191 {
192     char * f_name = "read_keybindings_from_file()";
193     char * cmdptr;
194     struct KeyBinding ** loc_last_ptr = &kbd->kbs;
195     * loc_last_ptr = 0;
196     while (try_fgets(line, linemax + 1, file, f_name))
197     {
198         if (!strcmp("%\n", line))
199         {
200             break;
201         }
202         * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), f_name);
203         struct KeyBinding * kb_p = * loc_last_ptr;
204         kb_p->next = 0;
205         kb_p->key = atoi(line);
206         cmdptr = strchr(line, ' ') + 1;
207         cmdptr[strlen(cmdptr) - 1] = '\0';
208         kb_p->command = get_command(cmdptr);
209         loc_last_ptr = & kb_p->next;
210     }
211 }
212
213
214
215 extern void free_keybindings(struct KeyBinding * kb_start)
216 {
217     if (0 == kb_start)
218     {
219         return;
220     }
221     struct KeyBinding * kb_p = kb_start->next;
222     if (0 != kb_p)
223     {
224         free_keybindings(kb_p);
225     }
226     free(kb_start);
227 }
228
229
230
231 extern void mod_selected_keyb(char kb_c)
232 {
233     struct KeyBindingDB * kbd = char_selected_kb_db(kb_c);
234     kbd->edit = 1;
235     draw_all_wins();
236     cbreak();
237     int key = getch();
238     halfdelay(world.halfdelay);
239     if (key < 1000)
240     {
241         struct KeyBinding * kb_p = get_keyb_of_n(kbd->kbs, kbd->select);
242         kb_p->key = key;
243     }
244     kbd->edit = 0;
245 }
246
247
248
249 extern void move_keyb_selection(char kb_c, char dir)
250 {
251     struct KeyBindingDB * kbd = char_selected_kb_db(kb_c);
252     if      ('u' == dir && kbd->select > 0)
253     {
254         kbd->select--;
255     }
256     else if ('d' == dir && kbd->select < get_n_of_keybs(kbd->kbs) - 1)
257     {
258         kbd->select++;
259     }
260 }