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