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