1 /* src/client/misc.c */
4 #include <ncurses.h> /* delwin() */
5 #include <stddef.h> /* NULL */
6 #include <stdint.h> /* uint8_t, uint32_t */
7 #include <stdio.h> /* FILE, sprintf() */
8 #include <stdlib.h> /* free(), exit() */
9 #include <string.h> /* memcpy(), strlen() */
10 #include <unistd.h> /* global optarg, getopt() */
11 #include "../common/err_try_fgets.h" /* reset_err_try_fgets_counter() */
12 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_sizes(),
13 * try_fclose_unlink_rename(),
15 #include "../common/rexit.h" /* exit_err() */
16 #include "../common/try_malloc.h" /* try_malloc() */
17 #include "cleanup.h" /* set_cleanup_flag() */
18 #include "keybindings.h" /* free_keybindings(), read_keybindings_from_file(),
19 * write_keybindings_to_file()
21 #include "map.h" /* map_center() */
22 #include "windows.h" /* free_winDB(), make_v_screen_and_init_win_sizes(),
23 * read_winconf_from_file(), write_winconf_of_id_to_file(),
26 #include "world.h" /* global world */
30 extern void obey_argv(int argc, char * argv[])
33 while (-1 != (opt = getopt(argc, argv, "i:")))
37 world.path_interface = optarg;
48 extern void save_interface_conf()
50 char * f_name = "save_interface_conf()";
51 char * path = world.path_interface;
52 char path_tmp[strlen(path) + 4 + 1];
53 sprintf(path_tmp, "%s_tmp", path);
54 FILE * file = try_fopen(path_tmp, "w", f_name);
55 write_keybindings_to_file(file, &world.kb_global);
56 write_keybindings_to_file(file, &world.kb_wingeom);
57 write_keybindings_to_file(file, &world.kb_winkeys);
58 write_order_wins_visible_active(file);
60 for (i = 0; i < strlen(world.winDB.ids); i++)
62 write_winconf_of_id_to_file(file, world.winDB.ids[i]);
64 try_fclose_unlink_rename(file, path_tmp, path, f_name);
69 extern void load_interface_conf()
71 char * f_name = "load_interface_conf()";
73 /* Read keybindings and WincConf DB from interface config file. */
74 reset_err_try_fgets_counter();
75 FILE * file = try_fopen(world.path_interface, "r", f_name);
76 uint32_t linemax = textfile_sizes(file, NULL);
77 char line[linemax + 1];
78 read_keybindings_from_file(line, linemax, file, &world.kb_global);
79 read_keybindings_from_file(line, linemax, file, &world.kb_wingeom);
80 read_keybindings_from_file(line, linemax, file, &world.kb_winkeys);
83 read_order_wins_visible_active(line, linemax, file, &order_tmp, &active_tmp);
84 while (read_winconf_from_file(line, linemax, file));
85 try_fclose(file, f_name);
87 /* Check that windows of all legal IDs have been initalized. The validity of
88 * this test relies on read_winconf_from_file() failing on duplicates. Only
89 * on success initialize the windows as visible, to enable safe cleaning up.
91 char * err = "Failed to initialize all expected windows.";
92 exit_err(strlen(world.winDB.legal_ids) != strlen(world.winDB.ids), err);
93 world.winDB.active = active_tmp;
94 world.winDB.order = order_tmp;
96 /* Build windows as defined by read interface data and toggle them on. */
97 make_v_screen_and_init_win_sizes();
98 char tmp_active = world.winDB.active;
99 char tmp_order[strlen(world.winDB.order) + 1];
100 sprintf(tmp_order, "%s", world.winDB.order);
101 world.winDB.order[0] = '\0';
103 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
104 world.winDB.active = tmp_active;
106 /* So that the interface config data and the window structs get freed. */
107 set_cleanup_flag(CLEANUP_INTERFACE);
112 extern void unload_interface_conf()
114 free_keybindings(world.kb_global.kbs);
115 free_keybindings(world.kb_wingeom.kbs);
116 free_keybindings(world.kb_winkeys.kbs);
117 while ('\0' != world.winDB.active)
119 toggle_window(world.winDB.active);
122 delwin(world.winDB.v_screen);
127 extern void reload_interface_conf()
129 unload_interface_conf();
130 load_interface_conf();
132 world.winDB.v_screen_offset = 0;
137 extern void nav_inventory(char dir)
141 world.player_inventory_select = world.player_inventory_select
142 - (world.player_inventory_select > 0);
147 for (i = 0; '\0' != world.player_inventory[i]; i++)
149 n_elems = n_elems + ('\n' == world.player_inventory[i]);
151 world.player_inventory_select = world.player_inventory_select
152 + (world.player_inventory_select < n_elems);
157 extern void array_append(uint32_t old_n, size_t region_size, void * new_region,
158 void ** ptr_old_array)
160 char * f_name = "array_append()";
161 uint32_t old_size = old_n * region_size;
162 uint32_t new_size = old_size + region_size;
163 void * new_array = try_malloc(new_size, f_name);
164 memcpy(new_array, * ptr_old_array, old_size);
165 memcpy(new_array + (old_n * region_size), new_region, region_size);
166 free(* ptr_old_array);
167 * ptr_old_array = new_array;