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