1 /* src/client/misc.c */
4 #include <stdlib.h> /* exit() */
5 #include <ncurses.h> /* delwin() */
6 #include <stddef.h> /* NULL */
7 #include <stdint.h> /* uint8_t, uint32_t */
8 #include <stdio.h> /* sprintf() */
9 #include <string.h> /* strlen() */
10 #include <unistd.h> /* global optarg, getopt() */
11 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_sizes(),
12 * try_fclose_unlink_rename(),
14 #include "cleanup.h" /* set_cleanup_flag() */
15 #include "keybindings.h" /* free_keybindings(), read_keybindings_from_file(),
16 * write_keybindings_to_file()
18 #include "map_window.h" /* map_center() */
19 #include "windows.h" /* for free_winDB(), make_v_screen_and_init_win_sizes(),
20 * read_winconf_from_file(), write_winconf_of_id_to_file(),
23 #include "world.h" /* global world */
25 #include "../common/try_malloc.h" /* try_malloc() */
28 extern void obey_argv(int argc, char * argv[])
31 while (-1 != (opt = getopt(argc, argv, "i:")))
35 world.path_interface_conf = optarg;
46 extern void save_interface_conf()
48 char * f_name = "save_interface_conf()";
49 char * path = world.path_interface_conf;
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);
54 write_keybindings_to_file(file, &world.kb_global, delim);
55 write_keybindings_to_file(file, &world.kb_wingeom, delim);
56 write_keybindings_to_file(file, &world.kb_winkeys, delim);
57 write_order_wins_visible_active(file, delim);
59 for (i = 0; i < strlen(world.winDB.ids); i++)
61 write_winconf_of_id_to_file(file, world.winDB.ids[i], delim);
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 FILE * file = try_fopen(world.path_interface_conf, "r", f_name);
74 uint32_t linemax = textfile_sizes(file, NULL);
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);
79 read_order_wins_visible_active(line, linemax, file);
80 while (read_winconf_from_file(line, linemax, file));
81 try_fclose(file, f_name);
83 /* Build windows as defined by read interface data and toggle them on. */
84 make_v_screen_and_init_win_sizes();
86 char tmp_active = world.winDB.active;
87 char tmp_order[strlen(world.winDB.order) + 1];
88 sprintf(tmp_order, "%s", world.winDB.order);
89 world.winDB.order[0] = '\0';
90 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
91 world.winDB.active = tmp_active;
93 /* So that the interface config data and the window structs get freed. */
94 set_cleanup_flag(CLEANUP_INTERFACE);
99 extern void unload_interface_conf()
101 free_keybindings(world.kb_global.kbs);
102 free_keybindings(world.kb_wingeom.kbs);
103 free_keybindings(world.kb_winkeys.kbs);
104 while ('\0' != world.winDB.active)
106 toggle_window(world.winDB.active);
109 delwin(world.winDB.v_screen);
114 extern void reload_interface_conf()
116 unload_interface_conf();
117 load_interface_conf();
123 extern void nav_inventory(char dir)
127 world.player_inventory_select = world.player_inventory_select
128 - (world.player_inventory_select > 0);
133 for (i = 0; '\0' != world.player_inventory[i]; i++)
135 n_elems = n_elems + ('\n' == world.player_inventory[i]);
137 world.player_inventory_select = world.player_inventory_select
138 + (world.player_inventory_select < n_elems);
143 extern void array_append(uint32_t old_n, size_t region_size, void * new_region,
144 void ** ptr_old_array)
146 char * f_name = "array_append()";
147 uint32_t old_size = old_n * region_size;
148 uint32_t new_size = old_size + region_size;
149 void * new_array = try_malloc(new_size, f_name);
150 memcpy(new_array, * ptr_old_array, old_size);
151 memcpy(new_array + (old_n * region_size), new_region, region_size);
152 free(* ptr_old_array);
153 * ptr_old_array = new_array;