home · contact · privacy
Minor code-stylistic improvements in control library.
[plomrogue] / src / control.c
1 /* control.c */
2
3 #include "control.h"
4 #include <stdint.h> /* for uint8_t, uint16_t */
5 #include "windows.h" /* for cycle_active_win(), shift_active_win(), struct Win,
6                       *  struct WinMeta
7                       */
8 #include "keybindings.h" /* for get_keycode_to_action(), mod_selected_keyb(),
9                           * move_keyb_mod_selection(), get_func_to_keycode()
10                           */
11 #include "map.h" /* for map_scroll(), map_center() */
12 #include "main.h" /* for world global */
13 #include "wincontrol.h" /* for struct WinConf, scroll_pad(), toggle_window(),
14                          * growshrink_active_window(), toggle_winconfig(),
15                          * toggle_win_size_type()
16                          */
17 #include "map_object_actions.h" /* for get_moa_id_by_name() */
18 #include "command_db.h" /* for is_command_id_shortdsc() */
19 #include "misc.h" /* for reload_interface_conf(), save_interface_conf(),
20                    * nav_inventory(), turn_over()
21                    */
22 #include "map_objects.h" /* for get_player() */
23
24
25
26 /* If "cmd" matches "match" in get_available_keycode_to_action(), execute "f"
27  * with provided char arguments and return 1; else only return 0.
28  */
29 static uint8_t try_cmd_0args(int cmd, char * match, void (* f) ());
30 static uint8_t try_cmd_1args(int cmd, char * match, void (* f) (char), char c);
31 static uint8_t try_cmd_2args(int cmd, char * match,
32                              void (* f) (char, char), char c1, char c2);
33
34 /* If "action" is id of command named "match", set player's .arg and .command
35  * and call turn_over().
36  */
37 static uint8_t try_player_cmd(int action, char * match, char * command,
38                               uint8_t arg);
39
40 /* Return pointer to global keybindings or to keybindings for wingeometry config
41  * (c = "g") or winkeys config (c = "k") or active window's keybindings ("w").
42  */
43 static struct KeyBiData * select_keybidata_pointer(char c);
44
45 /* Wrappers to make some functions compatible to try_cmd_* single char args. */
46 static void wrap_mod_selected_keyb(char c);
47 static void wrap_mv_kb_mod(char c1, char c2);
48
49
50
51 static uint8_t try_cmd_0args(int cmd, char * match, void (* f) ())
52 {
53     if (cmd == get_available_keycode_to_action(match))
54     {
55         f();
56         return 1;
57     }
58     return 0;
59 }
60
61
62
63 static uint8_t try_cmd_1args(int cmd, char * match, void (* f) (char), char c)
64 {
65     if (cmd == get_available_keycode_to_action(match))
66     {
67         f(c);
68         return 1;
69     }
70     return 0;
71 }
72
73
74
75 static uint8_t try_cmd_2args(int cmd, char * match,
76                              void (* f) (char, char), char c1, char c2)
77 {
78     if (cmd == get_available_keycode_to_action(match))
79     {
80         f(c1, c2);
81         return 1;
82     }
83     return 0;
84 }
85
86
87
88 static uint8_t try_player_cmd(int action, char * match, char * command,
89                               uint8_t arg)
90 {
91     if (is_command_id_shortdsc(action, match))
92     {
93         struct MapObj * player = get_player();
94         player->arg = arg;
95         player->command = get_moa_id_by_name(command);
96         turn_over(get_command_id(match));
97         return 1;
98     }
99     return 0;
100 }
101
102
103
104 static struct KeyBiData * select_keybidata_pointer(char c)
105 {
106     struct KeyBiData * kbd;
107     kbd = &world.kb_global;
108     if      ('g' == c)
109     {
110         kbd = &world.kb_wingeom;
111     }
112     else if ('k' == c)
113     {
114         kbd = &world.kb_winkeys;
115     }
116     else if ('w' == c)
117     {
118         struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
119         kbd = &wc->kb;
120     }
121     return kbd;
122 }
123
124
125
126 static void wrap_mod_selected_keyb(char c)
127 {
128         mod_selected_keyb(select_keybidata_pointer(c));
129 }
130
131
132
133 static void wrap_mv_kb_mod(char c1, char c2)
134 {
135         move_keyb_mod_selection(select_keybidata_pointer(c1), c2);
136 }
137
138
139
140 extern uint16_t get_available_keycode_to_action(char * name)
141 {
142     uint16_t keycode = get_keycode_to_action(world.kb_global.kbs, name);
143     if (0 != keycode || 0 == world.wmeta->active)
144     {
145         return keycode;
146     }
147     struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
148     if (0 == wc->view)
149     {
150         keycode = get_keycode_to_action(wc->kb.kbs, name);
151     }
152     else if (1 == wc->view)
153     {
154         keycode = get_keycode_to_action(world.kb_wingeom.kbs, name);
155     }
156     else if (2 == wc->view)
157     {
158         keycode = get_keycode_to_action(world.kb_winkeys.kbs, name);
159     }
160     return keycode;
161 }
162
163
164
165 extern uint8_t player_control_by_key(int key)
166 {
167     char * action_name = get_func_to_keycode(world.kb_global.kbs, key);
168     if (NULL == action_name && 0 != world.wmeta->active)
169     {
170         struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
171         action_name = get_func_to_keycode(wc->kb.kbs, key);
172     }
173     if (NULL != action_name)
174     {
175         uint8_t action_id = get_command_id(action_name);
176         return player_control_by_id(action_id);
177     }
178     return 0;
179 }
180
181
182
183
184 extern uint8_t player_control_by_id(int action)
185 {
186     if (   try_player_cmd(action, "wait", "wait", 0)
187         || try_player_cmd(action, "drop", "drop", world.inventory_sel)
188         || try_player_cmd(action, "pick", "pick_up", 0)
189         || try_player_cmd(action, "use", "use", world.inventory_sel)
190         || try_player_cmd(action, "player_u", "move", 'N')
191         || try_player_cmd(action, "player_d", "move", 'S')
192         || try_player_cmd(action, "player_r", "move", 'E')
193         || try_player_cmd(action, "player_l", "move", 'W'))
194     {
195         return 1;
196     }
197     return 0;
198 }
199
200
201
202 extern uint8_t wingeom_control(int key)
203 {
204     if (   try_cmd_1args(key, "to_height_t", toggle_win_size_type, 'y')
205         || try_cmd_1args(key, "to_width_t", toggle_win_size_type, 'x')
206         || try_cmd_1args(key, "grow_h", growshrink_active_window, '*')
207         || try_cmd_1args(key, "shri_h", growshrink_active_window, '_')
208         || try_cmd_1args(key, "grow_v", growshrink_active_window, '+')
209         || try_cmd_1args(key, "shri_v", growshrink_active_window, '-')
210         || try_cmd_1args(key, "shift_f", shift_active_win, 'f')
211         || try_cmd_1args(key, "shift_b", shift_active_win, 'b'))
212     {
213         return 1;
214     }
215     return 0;
216 }
217
218
219
220 extern uint8_t winkeyb_control(int key)
221 {
222     if (   try_cmd_1args(key, "w_keys_m", wrap_mod_selected_keyb, 'w')
223         || try_cmd_2args(key, "w_keys_u", wrap_mv_kb_mod, 'w', 'u')
224         || try_cmd_2args(key, "w_keys_d", wrap_mv_kb_mod, 'w', 'd'))
225     {
226         return 1;
227     }
228     return 0;
229 }
230
231
232
233 extern uint8_t meta_control(int key)
234 {
235     uint8_t ret = (key == get_available_keycode_to_action("quit"));
236     if (   (0 == ret)
237         && (   try_cmd_0args(key, "winconf", toggle_winconfig)
238             || try_cmd_0args(key, "reload_conf", reload_interface_conf)
239             || try_cmd_0args(key, "save_conf", save_interface_conf)
240             || try_cmd_0args(key, "map_c", map_center)
241             || try_cmd_1args(key, "scrl_r", scroll_pad, '+')
242             || try_cmd_1args(key, "scrl_l", scroll_pad, '-')
243             || try_cmd_1args(key, "to_a_keywin", toggle_window, 'k')
244             || try_cmd_1args(key, "to_g_keywin", toggle_window, '0')
245             || try_cmd_1args(key, "to_wg_keywin", toggle_window, '1')
246             || try_cmd_1args(key, "to_wk_keywin", toggle_window, '2')
247             || try_cmd_1args(key, "to_mapwin", toggle_window, 'm')
248             || try_cmd_1args(key, "to_infowin", toggle_window, 'i')
249             || try_cmd_1args(key, "to_inv", toggle_window, 'c')
250             || try_cmd_1args(key, "to_logwin", toggle_window, 'l')
251             || try_cmd_1args(key, "cyc_win_f", cycle_active_win, 'f')
252             || try_cmd_1args(key, "cyc_win_b", cycle_active_win, 'b')
253             || try_cmd_1args(key, "g_keys_m", wrap_mod_selected_keyb, 'G')
254             || try_cmd_1args(key, "wg_keys_m", wrap_mod_selected_keyb, 'g')
255             || try_cmd_1args(key, "wk_keys_m", wrap_mod_selected_keyb, 'k')
256             || try_cmd_1args(key, "inv_u", nav_inventory, 'u')
257             || try_cmd_1args(key, "inv_d", nav_inventory, 'd')
258             || try_cmd_1args(key, "map_u", map_scroll, 'N')
259             || try_cmd_1args(key, "map_d", map_scroll, 'S')
260             || try_cmd_1args(key, "map_r", map_scroll, 'E')
261             || try_cmd_1args(key, "map_l", map_scroll, 'W')
262             || try_cmd_2args(key, "g_keys_u", wrap_mv_kb_mod, 'G', 'u')
263             || try_cmd_2args(key, "g_keys_d", wrap_mv_kb_mod, 'G', 'd')
264             || try_cmd_2args(key, "wg_keys_u", wrap_mv_kb_mod, 'g', 'u')
265             || try_cmd_2args(key, "wg_keys_d", wrap_mv_kb_mod, 'g', 'd')
266             || try_cmd_2args(key, "wk_keys_u", wrap_mv_kb_mod, 'k', 'u')
267             || try_cmd_2args(key, "wk_keys_d", wrap_mv_kb_mod, 'k', 'd')))
268     {
269         ;
270     }
271     return ret;
272 }