1 /* src/client/interface_conf.c */
3 #include "interface_conf.h"
4 #include <ncurses.h> /* delwin() */
5 #include <stdint.h> /* uint8_t, uint32_t */
6 #include <stdio.h> /* FILE, sprintf() */
7 #include <stdlib.h> /* free(), exit() */
8 #include <string.h> /* strlen() */
9 #include <unistd.h> /* global optarg, getopt() */
10 #include "../common/err_try_fgets.h" /* reset_err_try_fgets_counter() */
11 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_width(),
12 * try_fclose_unlink_rename(),
14 #include "../common/rexit.h" /* exit_err() */
15 #include "cleanup.h" /* set_cleanup_flag() */
16 #include "keybindings.h" /* read_keybindings_from_file(),
17 * write_keybindings_to_file()
19 #include "map.h" /* map_center() */
20 #include "wincontrol.h" /* toggle_window() */
21 #include "windows.h" /* free_winDB(), make_v_screen_and_init_win_sizes(),
22 * read_winconf_from_file(), write_winconf_of_id_to_file(),
24 #include "world.h" /* global world */
28 extern void obey_argv(int argc, char * argv[])
31 while (-1 != (opt = getopt(argc, argv, "i:")))
35 world.path_interface = optarg;
46 extern void save_interface_conf()
48 char * f_name = "save_interface_conf()";
49 char * path = world.path_interface;
50 char path_tmp[strlen(path) + 4 + 1];
51 sprintf(path_tmp, "%s_tmp", path);
52 FILE * file = try_fopen(path_tmp, "w", f_name);
53 write_keybindings_to_file(file, &world.kb_global);
54 write_keybindings_to_file(file, &world.kb_wingeom);
55 write_keybindings_to_file(file, &world.kb_winkeys);
56 write_order_wins_visible_active(file);
58 for (i = 0; i < strlen(world.winDB.ids); i++)
60 write_winconf_of_id_to_file(file, world.winDB.ids[i]);
62 try_fclose_unlink_rename(file, path_tmp, path, f_name);
67 extern void load_interface_conf()
69 char * f_name = "load_interface_conf()";
71 /* Read keybindings and WincConf DB from interface config file. */
72 reset_err_try_fgets_counter();
73 FILE * file = try_fopen(world.path_interface, "r", f_name);
74 uint32_t linemax = textfile_width(file);
75 char line[linemax + 1];
76 read_keybindings_from_file(line, linemax, file, &world.kb_global);
77 read_keybindings_from_file(line, linemax, file, &world.kb_wingeom);
78 read_keybindings_from_file(line, linemax, file, &world.kb_winkeys);
81 read_order_wins_visible_active(line, linemax, file, &order_tmp,&active_tmp);
82 while (read_winconf_from_file(line, linemax, file));
83 try_fclose(file, f_name);
85 /* Check that windows of all legal IDs have been initalized. The validity of
86 * this test relies on read_winconf_from_file() failing on duplicates. Only
87 * on success initialize the windows as visible, to enable safe cleaning up.
89 char * err = "Failed to initialize all expected windows.";
90 exit_err(strlen(world.winDB.legal_ids) != strlen(world.winDB.ids), err);
91 world.winDB.active = active_tmp;
92 world.winDB.order = order_tmp;
94 /* Build windows as defined by read interface data and toggle them on. */
95 make_v_screen_and_init_win_sizes();
96 char tmp_active = world.winDB.active;
97 char tmp_order[strlen(world.winDB.order) + 1];
98 sprintf(tmp_order, "%s", world.winDB.order);
99 world.winDB.order[0] = '\0';
101 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
102 world.winDB.active = tmp_active;
104 /* So that the interface config data and the window structs get freed. */
105 set_cleanup_flag(CLEANUP_INTERFACE);
110 extern void unload_interface_conf()
112 free(world.kb_global.kbs);
113 world.kb_global.kbs = NULL;
114 free(world.kb_wingeom.kbs);
115 world.kb_wingeom.kbs = NULL;
116 free(world.kb_winkeys.kbs);
117 world.kb_winkeys.kbs = NULL;
118 while ('\0' != world.winDB.active)
120 toggle_window(world.winDB.active);
123 delwin(world.winDB.v_screen);
128 extern void reload_interface_conf()
130 unload_interface_conf();
131 load_interface_conf();
133 world.winDB.v_screen_offset = 0;