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 */
27 extern void obey_argv(int argc, char * argv[])
30 while (-1 != (opt = getopt(argc, argv, "i:")))
34 world.path_interface_conf = optarg;
45 extern void save_interface_conf()
47 char * f_name = "save_interface_conf()";
48 char * path = world.path_interface_conf;
49 char path_tmp[strlen(path) + 4 + 1];
50 sprintf(path_tmp, "%s_tmp", path);
51 FILE * file = try_fopen(path_tmp, "w", f_name);
53 write_keybindings_to_file(file, &world.kb_global, delim);
54 write_keybindings_to_file(file, &world.kb_wingeom, delim);
55 write_keybindings_to_file(file, &world.kb_winkeys, delim);
56 write_order_wins_visible_active(file, delim);
58 for (i = 0; i < strlen(world.windb.ids); i++)
60 write_winconf_of_id_to_file(file, world.windb.ids[i], delim);
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 FILE * file = try_fopen(world.path_interface_conf, "r", f_name);
73 uint32_t linemax = textfile_sizes(file, NULL);
74 char line[linemax + 1];
75 read_keybindings_from_file(line, linemax, file, &world.kb_global);
76 read_keybindings_from_file(line, linemax, file, &world.kb_wingeom);
77 read_keybindings_from_file(line, linemax, file, &world.kb_winkeys);
78 read_order_wins_visible_active(line, linemax, file);
79 while (read_winconf_from_file(line, linemax, file));
80 try_fclose(file, f_name);
82 /* Build windows as defined by read interface data and toggle them on. */
83 make_v_screen_and_init_win_sizes();
85 char tmp_active = world.windb.active;
86 char tmp_order[strlen(world.windb.order) + 1];
87 sprintf(tmp_order, "%s", world.windb.order);
88 world.windb.order[0] = '\0';
89 for (i = 0; i < strlen(tmp_order); toggle_window(tmp_order[i]), i++);
90 world.windb.active = tmp_active;
92 /* So that the interface config data and the window structs get freed. */
93 set_cleanup_flag(CLEANUP_INTERFACE);
98 extern void unload_interface_conf()
100 free_keybindings(world.kb_global.kbs);
101 world.kb_global.kbs = NULL;
102 free_keybindings(world.kb_wingeom.kbs);
103 world.kb_wingeom.kbs = NULL;
104 free_keybindings(world.kb_winkeys.kbs);
105 world.kb_winkeys.kbs = NULL;
106 while ('\0' != world.windb.active)
108 toggle_window(world.windb.active);
111 delwin(world.windb.v_screen);
116 extern void reload_interface_conf()
118 unload_interface_conf();
119 load_interface_conf();
125 extern void nav_inventory(char dir)
129 world.player_inventory_select = world.player_inventory_select
130 - (world.player_inventory_select > 0);
135 for (i = 0; '\0' != world.player_inventory[i]; i++)
137 n_elems = n_elems + ('\n' == world.player_inventory[i]);
139 world.player_inventory_select = world.player_inventory_select
140 + (world.player_inventory_select < n_elems);