1 /* src/client/misc.c */
4 #include <ncurses.h> /* delwin(), endwin(), refresh() */
5 #include <stdint.h> /* uint8_t, uint16_t */
6 #include <stdio.h> /* sprintf() */
7 #include <stdlib.h> /* exit(), free() */
8 #include <string.h> /* memset(), strlen() */
9 #include <unistd.h> /* global optarg, getopt() */
10 #include "../common/readwrite.h" /* try_fopen(), try_fclose(),
11 * try_fclose_unlink_rename(),
13 #include "cleanup.h" /* for set_cleanup_flag() */
14 #include "keybindings.h" /* free_keybindings(), read_keybindings_from_file(),
15 * write_keybindings_to_file()
17 #include "map_window.h" /* for map_center() */
18 #include "wincontrol.h" /* struct WinConf, init_wins(), get_winconf_by_win(),
19 * sorted_win_toggle_and_activate(), get_win_by_id(),
20 * toggle_window(), write_winconf_of_id_to_file(),
21 * read_winconf_from_file(), get_next_winconf_id(),
22 * read_order_wins_visible_active(),
23 * write_order_wins_visible_active()
25 #include "windows.h" /* struct Win, make_pad(), suspend_win(), free_win() */
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_conf = optarg;
48 extern void save_interface_conf()
50 char * f_name = "save_interface_conf()";
51 char * path = world.path_interface_conf;
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);
56 write_keybindings_to_file(file, &world.kb_global, delim);
57 write_keybindings_to_file(file, &world.kb_wingeom, delim);
58 write_keybindings_to_file(file, &world.kb_winkeys, delim);
59 write_order_wins_visible_active(file, delim);
61 for (i = 0; i < strlen(world.winconf_db.ids); i++)
63 write_winconf_of_id_to_file(file, world.winconf_db.ids[i], delim);
65 try_fclose_unlink_rename(file, path_tmp, path, f_name);
70 extern void load_interface_conf()
72 char * f_name = "load_interface_conf()";
74 /* Read keybindings and WincConf DB from interface config file. */
75 FILE * file = try_fopen(world.path_interface_conf, "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);
81 read_order_wins_visible_active(line, linemax, file);
82 while (read_winconf_from_file(line, linemax, file));
83 try_fclose(file, f_name);
85 /* Build windows as defined by read interface data and toggle them on. */
88 sorted_win_toggle_and_activate();
90 /* So that the interface config data and the window structs get freed. */
91 set_cleanup_flag(CLEANUP_INTERFACE);
96 extern void unload_interface_conf()
98 free_keybindings(world.kb_global.kbs);
99 world.kb_global.kbs = NULL;
100 free_keybindings(world.kb_wingeom.kbs);
101 world.kb_wingeom.kbs = NULL;
102 free_keybindings(world.kb_winkeys.kbs);
103 world.kb_winkeys.kbs = NULL;
104 while (0 != world.wmeta.active)
106 suspend_win(world.wmeta.active);
109 delwin(world.wmeta.pad);
114 extern void winch_called(int signal)
121 extern void reset_windows()
123 endwin(); /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
124 refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>. */
125 struct Win * w_p = world.wmeta.chain_start;
126 char win_ids[strlen(world.winconf_db.ids) + 1];
127 memset(win_ids, '\0', strlen(world.winconf_db.ids) + 1);
130 for (; NULL != w_p; w_p = w_p->next, i++)
132 struct WinConf * wc_p = get_winconf_by_win(w_p);
133 win_ids[i] = wc_p->id;
134 if (w_p == world.wmeta.active)
139 while (0 != world.wmeta.active)
141 w_p = world.wmeta.active;
145 while (0 != (id = get_next_winconf_id()))
147 free_win(get_win_by_id(id));
149 delwin(world.wmeta.pad);
152 if (strlen(win_ids) < 1)
156 for (i = 0; i < strlen(win_ids); i++)
158 toggle_window(win_ids[i]);
159 if (active == win_ids[i])
161 world.wmeta.active = get_win_by_id(win_ids[i]);
168 extern void reload_interface_conf()
170 unload_interface_conf();
171 load_interface_conf();
177 extern uint16_t center_offset(uint16_t position, uint16_t mapsize,
181 if (mapsize > framesize)
183 if (position > framesize / 2)
185 if (position < mapsize - (framesize / 2))
187 offset = position - (framesize / 2);
191 offset = mapsize - framesize;
200 extern void nav_inventory(char dir)
204 world.player_inventory_select = world.player_inventory_select
205 - (world.player_inventory_select > 0);
210 for (i = 0; '\0' != world.player_inventory[i]; i++)
212 n_elems = n_elems + ('\n' == world.player_inventory[i]);
214 world.player_inventory_select = world.player_inventory_select
215 + (world.player_inventory_select < n_elems);