4 #include <stdint.h> /* for uint8_t, uint16_t */
5 #include "windows.h" /* for cycle_active_win(), shift_active_win(), struct Win,
8 #include "keybindings.h" /* for get_keycode_to_action(), mod_selected_keyb(),
9 * move_keyb_mod_selection(), get_func_to_keycode()
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()
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()
22 #include "map_objects.h" /* for get_player() */
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.
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);
34 /* If "action" is id of command named "match", set player's .arg and .command
35 * and call turn_over().
37 static uint8_t try_player_cmd(int action, char * match, char * command,
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").
43 static struct KeyBiData * select_keybidata_pointer(char c);
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);
51 static uint8_t try_cmd_0args(int cmd, char * match, void (* f) ())
53 if (cmd == get_available_keycode_to_action(match))
63 static uint8_t try_cmd_1args(int cmd, char * match, void (* f) (char), char c)
65 if (cmd == get_available_keycode_to_action(match))
75 static uint8_t try_cmd_2args(int cmd, char * match,
76 void (* f) (char, char), char c1, char c2)
78 if (cmd == get_available_keycode_to_action(match))
88 static uint8_t try_player_cmd(int action, char * match, char * command,
91 if (is_command_id_shortdsc(action, match))
93 struct MapObj * player = get_player();
95 player->command = get_moa_id_by_name(command);
96 turn_over(get_command_id(match));
104 static struct KeyBiData * select_keybidata_pointer(char c)
106 struct KeyBiData * kbd;
107 kbd = &world.kb_global;
110 kbd = &world.kb_wingeom;
114 kbd = &world.kb_winkeys;
118 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
126 static void wrap_mod_selected_keyb(char c)
128 mod_selected_keyb(select_keybidata_pointer(c));
133 static void wrap_mv_kb_mod(char c1, char c2)
135 move_keyb_mod_selection(select_keybidata_pointer(c1), c2);
140 extern uint16_t get_available_keycode_to_action(char * name)
142 uint16_t keycode = get_keycode_to_action(world.kb_global.kbs, name);
143 if (0 != keycode || 0 == world.wmeta->active)
147 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
150 keycode = get_keycode_to_action(wc->kb.kbs, name);
152 else if (1 == wc->view)
154 keycode = get_keycode_to_action(world.kb_wingeom.kbs, name);
156 else if (2 == wc->view)
158 keycode = get_keycode_to_action(world.kb_winkeys.kbs, name);
165 extern uint8_t player_control_by_key(int key)
167 char * action_name = get_func_to_keycode(world.kb_global.kbs, key);
168 if (NULL == action_name && 0 != world.wmeta->active)
170 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
171 action_name = get_func_to_keycode(wc->kb.kbs, key);
173 if (NULL != action_name)
175 uint8_t action_id = get_command_id(action_name);
176 return player_control_by_id(action_id);
184 extern uint8_t player_control_by_id(int action)
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'))
202 extern uint8_t wingeom_control(int key)
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'))
220 extern uint8_t winkeyb_control(int key)
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'))
233 extern uint8_t meta_control(int key)
235 uint8_t ret = (key == get_available_keycode_to_action("quit"));
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')))