1 /* src/client/misc.c */
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> /* memcpy(), 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 "../common/try_malloc.h" /* try_malloc() */
16 #include "cleanup.h" /* set_cleanup_flag() */
17 #include "keybindings.h" /* read_keybindings_from_file(),
18 * write_keybindings_to_file()
20 #include "map.h" /* map_center() */
21 #include "wincontrol.h" /* toggle_window() */
22 #include "windows.h" /* free_winDB(), make_v_screen_and_init_win_sizes(),
23 * read_winconf_from_file(), write_winconf_of_id_to_file(),
25 #include "world.h" /* global world */
29 extern void obey_argv(int argc, char * argv[])
32 while (-1 != (opt = getopt(argc, argv, "i:")))
36 world.path_interface = optarg;
47 extern void save_interface_conf()
49 char * f_name = "save_interface_conf()";
50 char * path = world.path_interface;
51 char path_tmp[strlen(path) + 4 + 1];
52 sprintf(path_tmp, "%s_tmp", path);
53 FILE * file = try_fopen(path_tmp, "w", f_name);
54 write_keybindings_to_file(file, &world.kb_global);
55 write_keybindings_to_file(file, &world.kb_wingeom);
56 write_keybindings_to_file(file, &world.kb_winkeys);
57 write_order_wins_visible_active(file);
59 for (i = 0; i < strlen(world.winDB.ids); i++)
61 write_winconf_of_id_to_file(file, world.winDB.ids[i]);
63 try_fclose_unlink_rename(file, path_tmp, path, f_name);
68 extern void load_interface_conf()
70 char * f_name = "load_interface_conf()";
72 /* Read keybindings and WincConf DB from interface config file. */
73 reset_err_try_fgets_counter();
74 FILE * file = try_fopen(world.path_interface, "r", f_name);
75 uint32_t linemax = textfile_width(file);
76 char line[linemax + 1];
77 read_keybindings_from_file(line, linemax, file, &world.kb_global);
78 read_keybindings_from_file(line, linemax, file, &world.kb_wingeom);
79 read_keybindings_from_file(line, linemax, file, &world.kb_winkeys);
82 read_order_wins_visible_active(line, linemax, file, &order_tmp,&active_tmp);
83 while (read_winconf_from_file(line, linemax, file));
84 try_fclose(file, f_name);
86 /* Check that windows of all legal IDs have been initalized. The validity of
87 * this test relies on read_winconf_from_file() failing on duplicates. Only
88 * on success initialize the windows as visible, to enable safe cleaning up.
90 char * err = "Failed to initialize all expected windows.";
91 exit_err(strlen(world.winDB.legal_ids) != strlen(world.winDB.ids), err);
92 world.winDB.active = active_tmp;
93 world.winDB.order = order_tmp;
95 /* Build windows as defined by read interface data and toggle them on. */
96 make_v_screen_and_init_win_sizes();
97 char tmp_active = world.winDB.active;
98 char tmp_order[strlen(world.winDB.order) + 1];
99 sprintf(tmp_order, "%s", world.winDB.order);
100 world.winDB.order[0] = '\0';
102 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
103 world.winDB.active = tmp_active;
105 /* So that the interface config data and the window structs get freed. */
106 set_cleanup_flag(CLEANUP_INTERFACE);
111 extern void unload_interface_conf()
113 free(world.kb_global.kbs);
114 world.kb_global.kbs = NULL;
115 free(world.kb_wingeom.kbs);
116 world.kb_wingeom.kbs = NULL;
117 free(world.kb_winkeys.kbs);
118 world.kb_winkeys.kbs = NULL;
119 while ('\0' != world.winDB.active)
121 toggle_window(world.winDB.active);
124 delwin(world.winDB.v_screen);
129 extern void reload_interface_conf()
131 unload_interface_conf();
132 load_interface_conf();
134 world.winDB.v_screen_offset = 0;
139 extern void nav_inventory(char dir)
143 world.player_inventory_select = world.player_inventory_select
144 - (world.player_inventory_select > 0);
149 for (i = 0; '\0' != world.player_inventory[i]; i++)
151 n_elems = n_elems + ('\n' == world.player_inventory[i]);
153 world.player_inventory_select = world.player_inventory_select
154 + (world.player_inventory_select < n_elems);
159 extern void array_append(uint32_t old_n, size_t region_size, void * new_region,
160 void ** ptr_old_array)
162 char * f_name = "array_append()";
163 uint32_t old_size = old_n * region_size;
164 uint32_t new_size = old_size + region_size;
165 char * new_array = try_malloc(new_size, f_name);
166 memcpy(new_array, * ptr_old_array, old_size);
167 memcpy(new_array + (old_n * region_size), new_region, region_size);
168 free(* ptr_old_array);
169 * ptr_old_array = new_array;