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