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