1 /* src/client/interface_conf.c
3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
8 #define _POSIX_C_SOURCE 200809L /* getopt(), optarg, strdup(), snprintf() */
9 #include "interface_conf.h"
10 #include <ncurses.h> /* delwin() */
11 #include <stddef.h> /* NULL, size_t */
12 #include <stdint.h> /* UINT8_MAX, uint8_t, uint32_t */
13 #include <stdlib.h> /* EXIT_SUCCESS, atoi(), exit(), free() */
14 #include <stdio.h> /* FILE, sprintf() */
15 #include <string.h> /* strchr(), strcmp(), strdup(), strlen() */
16 #include <unistd.h> /* optarg, getopt() */
17 #include "../common/parse_file.h" /* token_from_line(),parsetset_singlechar() */
18 #include "../common/readwrite.h" /* atomic_write_start(), atomic_write_finish(),
19 * detect_atomic_leftover(), try_fwrite()
21 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
22 #include "../common/try_malloc.h" /* try_malloc() */
23 #include "array_append.h" /* array_append() */
24 #include "cleanup.h" /* set_cleanup_flag() */
25 #include "command_db.h" /* get_command() */
26 #include "keybindings.h" /* KeyBinding, KeyBindingDB, get_command_to_keycode()*/
27 #include "parse.h" /* EDIT_STARTED, parse_file(), parse_flagval(),
28 * parse_and_reduce_to_readyflag(), parse_id_uniq()
29 * parsetest_defcontext(), parse_unknown_arg(),
30 * parsetest_too_many_values(), parse_init_entry()
32 #include "wincontrol.h" /* toggle_window() */
33 #include "windows.h" /* Win, free_winDB(), make_v_screen_and_init_win_sizes() */
34 #include "world.h" /* global world */
38 /* Editing state flags set / checked in tokens_into_entries(). */
46 READY_WIN = NAME_SET | WIDTH_SET | HEIGHT_SET | BREAK_SET,
53 /* Used in load_interface_conf() to temporarily store designated values for
54 * world.winDB.order and world.winDB.active. If those were set right away
55 * without all windows having already been initialized, orderly exits on error
56 * would be impossible, for windows are cleaned out by working toggling them off
57 * piece by peice following these values in unload_interfac_conf().
59 static char * tmp_order;
60 static char tmp_active;
64 /* Write into "file" keybindings in "kbdb" in config file format. */
65 static void write_keybindings(FILE * file, struct KeyBindingDB * kbdb);
67 /* Write into file "val" interpreted as pointing either to string (type = 's'),
68 * int16 ('i') or char ('c'), prefixed with "prefix" and put into single quotes
71 static void write_def(FILE * file, char * prefix, uint8_t quotes, char * val,
74 /* Read in "token0" and "token1" as interface config definition lines, apply
75 * definitions to WinDB, KeyBindingsDBs and tmp_order/tmp_active. If argument is
76 * "KEY", get third token via token_from_line() for complete keybinding.
78 static void tokens_into_entries(char * token0, char * token1);
80 /* If "win" points to non-NULL Win struct, append to it to WinDB.wins array and
81 * add its ID to world.winDB.ids.
83 static void write_if_win(struct Win ** win);
85 /* Start block setting data for window "token1" id if "token0" == "str_win".*/
86 static uint8_t start_win(char * token0, char * token1, char * str_win,
87 uint8_t * win_flags, struct Win ** win);
89 /* Start block setting window order and focus if "token0" = "str_ord".
90 * "tmp_order" stores the designated world.winDB.order string (only later to
91 * be realized in load_interface_conf() when all windows have been initialized),
92 * as does "tmp_active" for the designated world.winDB.active char, defaulting
93 * to the first char in the window order string if not otherwise specified.
95 static uint8_t start_ord(char * token0, char * token1, char * str_ord,
98 /* Start block setting non-window-specific keybindings if "token0" == "str_kbd".
99 * "token1" must be "global", "wingeom" or "winkeys" to set KeyBindingDB to edit
100 * ("kbdb") to world.kb_global, world.kb_wingeom or world.kb_winkeys.
102 static uint8_t start_kbd(char * token0, char * token1, char * str_kbd,
103 uint8_t * kbd_flags, struct KeyBindingDB ** kbdb);
105 /* Helper to tokens_into_entries(), sets specific entry member data. "token0"
106 * and the state of flags determine what entry member to edit; "win" and "kbdb"
107 * are entries to write "token1" data into (as is the global "tmp_active"). If
108 * "token0" is "str_key", a keybinding is defined and "token2" is read/must not
109 * be NULL. In that case, the line read is checked against having a 4th token.
111 static uint8_t set_members(char * token0, char * token1, uint8_t * win_flags,
112 uint8_t * ord_flags,uint8_t kbd_flags,char * str_key,
113 struct Win * win, struct KeyBindingDB * kbdb);
115 /* Add keybinding defined in "token1" as keycode and "token2" as command to
116 * "kbdb" if "flags" are set to EDIT_STARTED.
118 static void set_keybindings(char * token1, uint8_t flags,
119 struct KeyBindingDB * kbdb);
123 static void write_keybindings(FILE * file, struct KeyBindingDB * kbdb)
128 for (i_kb = 0; i_kb < kbdb->n_of_kbs; i_kb++)
130 size_t size = strlen(tok0) + strlen(sep) + 3 + strlen(sep)
131 + strlen(kbdb->kbs[i_kb].command->dsc_short) + 1 + 1;
132 char * line = try_malloc(size, __func__);
133 int test = snprintf(line, size, "%s%s%d%s%s\n",
134 tok0, sep, kbdb->kbs[i_kb].keycode, sep,
135 kbdb->kbs[i_kb].command->dsc_short);
136 exit_trouble(test < 0, __func__, "snprintf");
137 try_fwrite(line, sizeof(char), strlen(line), file, __func__);
144 static void write_def(FILE * file, char * prefix, uint8_t quotes, char * val,
147 char * val_str = NULL;
148 int test_val_str = 1;
151 val_str = strdup(val);
155 size_t size_val_str = 6 + 1;
156 val_str = try_malloc(size_val_str, __func__);
157 test_val_str = snprintf(val_str, size_val_str, "%d", (int16_t) *val);
159 else if ('c' == type)
161 size_t size_val_str = 1 + 1;
162 val_str = try_malloc(size_val_str, __func__);
163 test_val_str = snprintf(val_str, size_val_str, "%c", * val);
165 exit_trouble(test_val_str < 0, __func__, "snprintf");
166 char * quote = quotes ? "'": "";
168 size_t size = strlen(prefix) + strlen(val_str) + (2 * strlen(quote))
170 char * line = try_malloc(size, __func__);
171 int test = snprintf(line, size, "%s%s%s%s%s",
172 prefix, quote, val_str, quote, affix);
173 exit_trouble(test < 0, __func__, "snprintf");
175 try_fwrite(line, sizeof(char), strlen(line), file, __func__);
181 static void tokens_into_entries(char * token0, char * token1)
183 char * str_win = "WINDOW";
184 char * str_ord = "WIN_ORDER";
185 char * str_kbd = "KEYBINDINGS";
186 char * str_key = "KEY";
187 static uint8_t win_flags = READY_WIN;
188 static uint8_t ord_flags = READY_ORD;
189 static uint8_t kbd_flags = READY_KBD;
190 static struct Win * win = NULL;
191 static struct KeyBindingDB * kbdb = NULL;
192 if (!token0 || !strcmp(token0, str_win) || !strcmp(token0, str_ord)
193 || !strcmp(token0, str_kbd))
195 parse_and_reduce_to_readyflag(&win_flags, READY_WIN);
196 parse_and_reduce_to_readyflag(&ord_flags, READY_ORD);
197 parse_and_reduce_to_readyflag(&kbd_flags, READY_KBD);
202 if (strcmp(token0, str_key))
204 parsetest_too_many_values();
206 if (!( start_win(token0, token1, str_win, &win_flags, &win)
207 || start_ord(token0, token1, str_ord, &ord_flags)
208 || start_kbd(token0, token1, str_kbd, &kbd_flags, &kbdb)
209 || set_members(token0, token1, &win_flags, &ord_flags, kbd_flags,
210 str_key, win, kbdb)))
219 static void write_if_win(struct Win ** win)
223 (*win)->target_height_type = (0 >= (*win)->target_height);
224 (*win)->target_width_type = (0 >= (*win)->target_width);;
225 size_t old_ids_size = strlen(world.winDB.ids);
226 size_t new_size = old_ids_size + 1 + 1;
227 char * new_ids = try_malloc(new_size, __func__);
228 int test = snprintf(new_ids,new_size,"%s%c",world.winDB.ids,(*win)->id);
229 exit_trouble(test < 0, __func__, "snprintf");
230 free(world.winDB.ids);
231 world.winDB.ids = new_ids;
232 array_append(old_ids_size, sizeof(struct Win), *win,
233 (void **) &world.winDB.wins);
241 static uint8_t start_win(char * token0, char * token1, char * str_win,
242 uint8_t * win_flags, struct Win ** win)
244 if (strcmp(token0, str_win))
248 *win = (struct Win *) parse_init_entry(win_flags, sizeof(struct Win));
249 parsetest_singlechar(token1);
250 parse_id_uniq(world.winDB.ids && strchr(world.winDB.ids,token1[0]));
251 (*win)->id = token1[0];
257 static uint8_t start_ord(char * token0, char * token1, char * str_ord,
260 if (strcmp(token0, str_ord))
264 *ord_flags = EDIT_STARTED;
266 for (; i < strlen(token1); i++)
268 err_line(!strchr(world.winDB.legal_ids, token1[i]), "Illegal ID(s).");
271 tmp_order = strdup(token1);
272 if (0 < strlen(tmp_order))
274 tmp_active = tmp_order[0];
281 static uint8_t start_kbd(char * token0, char * token1, char * str_kbd,
282 uint8_t * kbd_flags, struct KeyBindingDB ** kbdb)
284 if (strcmp(token0, str_kbd))
288 *kbd_flags = EDIT_STARTED;
289 if (!strcmp(token1, "global"))
291 *kbdb = &world.kb_global;
293 else if (!strcmp(token1, "wingeom"))
295 *kbdb = &world.kb_wingeom;
297 else if (!strcmp(token1, "winkeys"))
299 *kbdb = &world.kb_winkeys;
303 err_line(1, "Value must be 'global', 'wingeom' or 'winkeys'.");
310 static uint8_t set_members(char * token0, char * token1, uint8_t * win_flags,
311 uint8_t * ord_flags,uint8_t kbd_flags,char * str_key,
312 struct Win * win, struct KeyBindingDB * kbdb)
314 if ( parse_flagval(token0, token1, "NAME", win_flags,
315 NAME_SET, 's', (char *) &win->title)
316 || parse_flagval(token0, token1, "WIDTH", win_flags,
317 WIDTH_SET, 'i', (char *) &win->target_width)
318 || parse_flagval(token0, token1, "HEIGHT", win_flags,
319 HEIGHT_SET, 'i', (char *) &win->target_height));
320 else if (parse_flagval(token0, token1, "BREAK", win_flags,
321 BREAK_SET, '8', (char *) &win->linebreak))
323 err_line(2 < win->linebreak, "Value must be 0, 1 or 2.");
325 else if (parse_flagval(token0, token1, "WIN_FOCUS", ord_flags,
326 FOCUS_SET, 'c', &tmp_active))
328 char * err_null = "Value not empty as it should be.";
329 char * err_outside = "ID not found in WIN_ORDER ID series.";
330 err_line(!strlen(tmp_order) && tmp_active, err_null);
331 err_line(!strchr(tmp_order, tmp_active), err_outside);
333 else if (!strcmp(token0, str_key))
335 if (*win_flags & EDIT_STARTED)
337 set_keybindings(token1, *win_flags, &win->kb);
341 set_keybindings(token1, kbd_flags, kbdb);
353 static void set_keybindings(char * token1, uint8_t flags,
354 struct KeyBindingDB * kbdb)
356 char * token2 = token_from_line(NULL);
357 err_line(!token2, "No binding to key given.");
358 parsetest_too_many_values();
359 char * err_code = "Invalid keycode. Must be >= 0 and < 1000.";
360 parsetest_defcontext(flags);
361 char * err_many = "No more than 255 keybindings allowed in one section.";
362 err_line(UINT8_MAX == kbdb->n_of_kbs, err_many);
363 struct KeyBinding kb;
364 uint8_t test = strlen(token1);
365 err_line(!test, err_code);
367 for (i = 0; '\0' != token1[i]; i++)
369 test= i > 2 || '0' > token1[i] || '9' < token1[i];
370 err_line(test, err_code);
372 kb.keycode = atoi(token1);
373 char * err_uniq = "Binding to key already defined.";
374 err_line(!(!get_command_to_keycode(kbdb, kb.keycode)), err_uniq);
375 kb.command = get_command(token2);
376 err_line(!(kb.command), "No such command in command DB.");
377 array_append(kbdb->n_of_kbs, sizeof(struct KeyBinding), (void *) &kb,
384 extern void obey_argv(int argc, char * argv[])
387 while (-1 != (opt = getopt(argc, argv, "i:")))
391 world.path_interface = optarg;
402 extern void save_interface_conf()
405 FILE * file = atomic_write_start(world.path_interface, &path_tmp);
406 char * str_keybs = "\nKEYBINDINGS ";
407 write_def(file, str_keybs, 1, "global", 's');
408 write_keybindings(file, &world.kb_global);
409 write_def(file, str_keybs, 1, "wingeom", 's');
410 write_keybindings(file, &world.kb_wingeom);
411 write_def(file, str_keybs, 1, "winkeys", 's');
412 write_keybindings(file, &world.kb_winkeys);
413 write_def(file, "\nWIN_ORDER ", 1, world.winDB.order, 's');
414 if (world.winDB.active)
416 write_def(file, "WIN_FOCUS ", 1, &world.winDB.active, 'c');
419 for (i = 0; i < strlen(world.winDB.ids); i++)
421 write_def(file, "\nWINDOW ", 0, &world.winDB.ids[i], 'c');
422 struct Win * win = get_win_by_id(world.winDB.ids[i]);
423 write_def(file, "NAME ", 1, win->title, 's');
424 write_def(file, "BREAK ", 0, (char *) &win->linebreak, 'i');
425 write_def(file, "WIDTH ", 0, (char *) &win->target_width, 'i');
426 write_def(file, "HEIGHT ", 0, (char *) &win->target_height, 'i');
427 write_keybindings(file, &win->kb);
429 atomic_write_finish(file, world.path_interface, path_tmp);
434 extern void load_interface_conf()
436 world.winDB.ids = try_malloc(1, __func__);
437 world.winDB.ids[0] = '\0';
438 world.winDB.wins = NULL;
439 tmp_order = try_malloc(1, __func__);
442 detect_atomic_leftover(world.path_interface);
443 parse_file(world.path_interface, tokens_into_entries);
444 char * err = "Not all expected windows defined in config file.";
445 exit_err(strlen(world.winDB.legal_ids) != strlen(world.winDB.ids), err);
446 make_v_screen_and_init_win_sizes();
447 world.winDB.order = try_malloc(1, __func__);
448 world.winDB.order[0] = '\0';
450 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
451 world.winDB.active = tmp_active;
453 set_cleanup_flag(CLEANUP_INTERFACE);
458 extern void unload_interface_conf()
460 free(world.kb_global.kbs);
461 world.kb_global.kbs = NULL;
462 world.kb_global.n_of_kbs = 0;
463 free(world.kb_wingeom.kbs);
464 world.kb_wingeom.kbs = NULL;
465 world.kb_wingeom.n_of_kbs = 0;
466 free(world.kb_winkeys.kbs);
467 world.kb_winkeys.kbs = NULL;
468 world.kb_winkeys.n_of_kbs = 0;
469 while ('\0' != world.winDB.active)
471 toggle_window(world.winDB.active);
474 delwin(world.winDB.v_screen);
479 extern void reload_interface_conf()
481 unload_interface_conf();
482 load_interface_conf();
483 world.winDB.v_screen_offset = 0;