home · contact · privacy
01cf1d2e7ab68962fdaca3af4db42e9cc297f83c
[plomrogue] / src / client / misc.c
1 /* src/client/misc.c */
2
3 #include "misc.h"
4 #include <ncurses.h> /* delwin(), endwin(), refresh() */
5 #include <stdint.h> /* uint8_t, uint16_t */
6 #include <string.h> /* memset(), strlen() */
7 #include "cleanup.h" /* for set_cleanup_flag() */
8 #include "keybindings.h" /* init_keybindings(), free_keybindings(),
9                           * save_keybindings()
10                           */
11 #include "map_window.h" /* for map_center() */
12 #include "wincontrol.h" /* struct WinConf, init_winconfs(), init_wins(),
13                          * sorted_wintoggle_and_activate(), get_win_by_id(),
14                          * get_winconf_by_win(), toggle_window(),
15                          * get_next_winconf_id()
16                          */
17 #include "windows.h" /* struct Win, make_pad(), suspend_win(), free_win() */
18 #include "world.h" /* global world */
19
20
21
22 extern void save_interface_conf()
23 {
24     save_keybindings("confclient/keybindings_global", &world.kb_global);
25     save_keybindings("confclient/keybindings_wingeom", &world.kb_wingeom);
26     save_keybindings("confclient/keybindings_winkeys", &world.kb_winkeys);
27     save_win_configs();
28 }
29
30
31
32 extern void load_interface_conf()
33 {
34     init_keybindings("confclient/keybindings_global",  &world.kb_global);
35     init_keybindings("confclient/keybindings_wingeom", &world.kb_wingeom);
36     init_keybindings("confclient/keybindings_winkeys", &world.kb_winkeys);
37     init_winconfs();
38     make_pad();
39     init_wins();
40     sorted_wintoggle_and_activate();
41     set_cleanup_flag(CLEANUP_INTERFACE);
42 }
43
44
45
46 extern void unload_interface_conf()
47 {
48     free_keybindings(world.kb_global.kbs);
49     free_keybindings(world.kb_wingeom.kbs);
50     free_keybindings(world.kb_winkeys.kbs);
51     while (0 != world.wmeta.active)
52     {
53         suspend_win(world.wmeta.active);
54     }
55     free_winconfs();
56     delwin(world.wmeta.pad);
57 }
58
59
60
61 extern void winch_called(int signal)
62 {
63     world.winch = 1;
64 }
65
66
67
68 extern void reset_windows()
69 {
70     endwin();  /* "[S]tandard way" to recalibrate ncurses post SIGWINCH, says */
71     refresh(); /* <http://invisible-island.net/ncurses/ncurses-intro.html>.   */
72     struct Win * w_p = world.wmeta.chain_start;
73     char win_ids[strlen(world.winconf_db.winconf_ids) + 1];
74     memset(win_ids, '\0', strlen(world.winconf_db.winconf_ids) + 1);
75     uint8_t i = 0;
76     char active = '\0';
77     for (; NULL != w_p; w_p = w_p->next, i++)
78     {
79         struct WinConf * wc_p = get_winconf_by_win(w_p);
80         win_ids[i] = wc_p->id;
81         if (w_p == world.wmeta.active)
82         {
83             active = wc_p->id;
84         }
85     }
86     while (0 != world.wmeta.active)
87     {
88         w_p = world.wmeta.active;
89         suspend_win(w_p);
90     }
91     char id;
92     while (0 != (id = get_next_winconf_id()))
93     {
94         free_win(get_win_by_id(id));
95     }
96     delwin(world.wmeta.pad);
97     make_pad();
98     init_wins();
99     if (strlen(win_ids) < 1)
100     {
101         return;
102     }
103     for (i = 0; i < strlen(win_ids); i++)
104     {
105         toggle_window(win_ids[i]);
106         if (active == win_ids[i])
107         {
108             world.wmeta.active = get_win_by_id(win_ids[i]);
109         }
110     }
111 }
112
113
114
115 extern void reload_interface_conf()
116 {
117     unload_interface_conf();
118     load_interface_conf();
119     map_center();
120 }
121
122
123
124 extern uint16_t center_offset(uint16_t position, uint16_t mapsize,
125                               uint16_t framesize)
126 {
127     uint16_t offset = 0;
128     if (mapsize > framesize)
129     {
130         if (position > framesize / 2)
131         {
132             if (position < mapsize - (framesize / 2))
133             {
134                 offset = position - (framesize / 2);
135             }
136             else
137             {
138                 offset = mapsize - framesize;
139             }
140         }
141     }
142     return offset;
143 }
144
145
146
147 extern void nav_inventory(char dir)
148 {
149     if ('u' == dir)
150     {
151         world.player_inventory_select = world.player_inventory_select
152                                         - (world.player_inventory_select > 0);
153         return;
154     }
155     uint8_t n_elems = 0;
156     uint8_t i;
157     for (i = 0; '\0' != world.player_inventory[i]; i++)
158     {
159         n_elems = n_elems + ('\n' == world.player_inventory[i]);
160     }
161     world.player_inventory_select = world.player_inventory_select
162                                     + (world.player_inventory_select < n_elems);
163 }
164