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