home · contact · privacy
ffd71797595692a8806e43faafef9a5a2d917c20
[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 global */
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(uint16_t keycode)
35 {
36     char * f_name = "get_name_to_keycode()";
37     char * keyname = try_malloc(15, 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(char * path, struct KeyBiData * kbd)
151 {
152     char * f_name = "init_keybindings()";
153
154     FILE * file = try_fopen(path, "r", f_name);
155     uint16_t lines, linemax;
156     char * err = "textfile_sizes() in init_keybindings() returns error.";
157     exit_err(textfile_sizes(file, &linemax, &lines), err);
158
159     char command[linemax + 1];
160     char * cmdptr;
161     struct KeyBinding ** loc_last_ptr = &kbd->kbs;
162     * loc_last_ptr = 0;
163     while (fgets(command, linemax + 1, file))
164     {
165         if ('\n' == command[0] || 0 == command[0])
166         {
167             break;
168         }
169         * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), f_name);
170         struct KeyBinding * kb_p = * loc_last_ptr;
171         kb_p->next = 0;
172         kb_p->key = atoi(command);
173         cmdptr = strchr(command, ' ') + 1;
174         kb_p->name = try_malloc(strlen(cmdptr), f_name);
175         memcpy(kb_p->name, cmdptr, strlen(cmdptr) - 1);
176         kb_p->name[strlen(cmdptr) - 1] = '\0';
177         loc_last_ptr = & kb_p->next;
178     }
179
180     try_fclose(file, f_name);
181
182     kbd->edit = 0;
183     kbd->select = 0;
184 }
185
186
187
188 extern void save_keybindings(char * path, struct KeyBiData * kbd)
189 {
190     char * f_name = "save_keybindings()";
191
192     char path_tmp[strlen(path) + 4 + 1];
193     sprintf(path_tmp, "%s_tmp", path);
194     FILE * file = try_fopen(path_tmp, "w", f_name);
195
196     uint16_t linemax = 0;
197     struct KeyBinding * kb_p = kbd->kbs;
198     while (0 != kb_p)
199     {
200         if (strlen(kb_p->name) > linemax)
201         {
202             linemax = strlen(kb_p->name);
203         }
204         kb_p = kb_p->next;
205     }
206     linemax = linemax + 6;         /* + 6 = + 3 digits + whitespace + \n + \0 */
207
208     char line[linemax];
209     kb_p = kbd->kbs;
210     while (0 != kb_p)
211     {
212         snprintf(line, linemax, "%d %s\n", kb_p->key, kb_p->name);
213         try_fwrite(line, sizeof(char), strlen(line), file, f_name);
214         kb_p = kb_p->next;
215     }
216
217     try_fclose_unlink_rename(file, path_tmp, path, f_name);
218 }
219
220
221
222 extern void free_keybindings(struct KeyBinding * kb_start)
223 {
224     if (0 == kb_start)
225     {
226         return;
227     }
228     struct KeyBinding * kb_p = kb_start->next;
229     if (0 != kb_p)
230     {
231         free_keybindings(kb_p);
232     }
233     free(kb_start->name);
234     free(kb_start);
235 }
236
237
238
239 extern void mod_selected_keyb(struct KeyBiData * kbd)
240 {
241     kbd->edit = 1;
242     char * err = "Trouble with draw_all_wins() in mod_selected_keyb().";
243     exit_err(draw_all_wins(world.wmeta), err);
244     int key = getch();
245     if (key < 1000)
246     {
247         struct KeyBinding * kb_p = get_keyb_of_n(kbd->kbs, kbd->select);
248         kb_p->key = key;
249     }
250     kbd->edit = 0;
251 }
252
253
254
255 extern void move_keyb_mod_selection(struct KeyBiData * kbd, char dir)
256 {
257     if      ('u' == dir && kbd->select > 0)
258     {
259         kbd->select--;
260     }
261     else if ('d' == dir && kbd->select < get_n_of_keybs(kbd->kbs) - 1)
262     {
263         kbd->select++;
264     }
265 }