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