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