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,
9 #include "keybindings.h" /* for get_keycode_to_action(), mod_selected_keyb(),
10 * move_keyb_mod_selection(), get_func_to_keycode()
12 #include "map.h" /* for map_scroll(), map_center() */
13 #include "main.h" /* for world global */
14 #include "rexit.h" /* for exit_err() */
15 #include "wincontrol.h" /* for struct WinConf, scroll_pad(), toggle_window(),
16 * growshrink_active_window(),toggle_winconfig(),
17 * toggle_win_height_type(), toggle_win_width_type()
19 #include "map_object_actions.h" /* for struct MapObjAct, actor_wait(),
20 * actor_move(), actor_drop(), actor_pick(),
23 #include "command_db.h" /* for is_command_id_shortdsc() */
24 #include "misc.h" /* for reload_interface_conf(), save_interface_conf(),
25 * nav_inventory(), turn_over()
27 #include "map_objects.h" /* for get_player() */
31 /* If "cmd" matches "match" in get_available_keycode_to_action(), execute "f"
32 * with provided char arguments and return 1; else only return 0.
34 static uint8_t try_cmd_0args(int cmd, char * match, void (* f) ());
35 static uint8_t try_cmd_1args(int cmd, char * match, void (* f) (char), char c);
36 static uint8_t try_cmd_2args(int cmd, char * match,
37 void (* f) (char, char), char c1, char c2);
39 /* try_player_cmd() helper, returns world.map_obj_acts action id for "name". */
40 static uint8_t get_moa_id_by_name(char * name);
42 /* If "action" is id of command named "match", set player->arg, ->command and
45 static uint8_t try_player_cmd(int action, char * match, char * command,
48 /* Return pointer to global keybindings or to keybindings for wingeometry config
49 * (c = "g") or winkeys config (c = "k") or active window's keybindings ("w").
51 static struct KeyBiData * select_keybidata_pointer(char c);
53 /* Wrappers to make some functions compatible to try_cmd_* single char args. */
54 static void wrap_mod_selected_keyb(char c);
55 static void wrap_mv_kb_mod(char c1, char c2);
59 static uint8_t try_cmd_0args(int cmd, char * match, void (* f) ())
61 if (cmd == get_available_keycode_to_action(match))
71 static uint8_t try_cmd_1args(int cmd, char * match, void (* f) (char), char c)
73 if (cmd == get_available_keycode_to_action(match))
83 static uint8_t try_cmd_2args(int cmd, char * match,
84 void (* f) (char, char), char c1, char c2)
86 if (cmd == get_available_keycode_to_action(match))
96 static uint8_t get_moa_id_by_name(char * name)
98 struct MapObjAct * moa = world.map_obj_acts;
101 if (0 == strcmp(moa->name, name))
107 exit_err(NULL == moa, "get_moa_id_name() did not find map object action.");
113 static uint8_t try_player_cmd(int action, char * match, char * command,
116 if (is_command_id_shortdsc(action, match))
118 struct MapObj * player = get_player();
120 player->command = get_moa_id_by_name(command);
121 turn_over(get_command_id(match));
129 static struct KeyBiData * select_keybidata_pointer(char c)
131 struct KeyBiData * kbd;
132 kbd = &world.kb_global;
135 kbd = &world.kb_wingeom;
139 kbd = &world.kb_winkeys;
143 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
151 static void wrap_mod_selected_keyb(char c)
153 mod_selected_keyb(select_keybidata_pointer(c));
158 static void wrap_mv_kb_mod(char c1, char c2)
160 move_keyb_mod_selection(select_keybidata_pointer(c1), c2);
165 extern uint16_t get_available_keycode_to_action(char * name)
167 uint16_t keycode = get_keycode_to_action(world.kb_global.kbs, name);
168 if (0 != keycode || 0 == world.wmeta->active)
172 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
175 keycode = get_keycode_to_action(wc->kb.kbs, name);
177 else if (1 == wc->view)
179 keycode = get_keycode_to_action(world.kb_wingeom.kbs, name);
181 else if (2 == wc->view)
183 keycode = get_keycode_to_action(world.kb_winkeys.kbs, name);
190 extern uint8_t player_control_by_key(int key)
192 char * action_name = get_func_to_keycode(world.kb_global.kbs, key);
193 if (NULL == action_name && 0 != world.wmeta->active)
195 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
196 action_name = get_func_to_keycode(wc->kb.kbs, key);
198 if (NULL != action_name)
200 uint8_t action_id = get_command_id(action_name);
201 return player_control_by_id(action_id);
209 extern uint8_t player_control_by_id(int action)
211 if ( try_player_cmd(action, "wait", "wait", 0)
212 || try_player_cmd(action, "drop", "drop", world.inventory_select)
213 || try_player_cmd(action, "pick", "pick_up", 0)
214 || try_player_cmd(action, "use", "use", world.inventory_select)
215 || try_player_cmd(action, "player_u", "move", 'N')
216 || try_player_cmd(action, "player_d", "move", 'S')
217 || try_player_cmd(action, "player_r", "move", 'E')
218 || try_player_cmd(action, "player_l", "move", 'W'))
227 extern uint8_t wingeom_control(int key)
229 if ( try_cmd_0args(key, "to_height_t", toggle_win_height_type)
230 || try_cmd_0args(key, "to_width_t", toggle_win_width_type)
231 || try_cmd_1args(key, "grow_h", growshrink_active_window, '*')
232 || try_cmd_1args(key, "shri_h", growshrink_active_window, '_')
233 || try_cmd_1args(key, "grow_v", growshrink_active_window, '+')
234 || try_cmd_1args(key, "shri_v", growshrink_active_window, '-')
235 || try_cmd_1args(key, "shift_f", shift_active_win, 'f')
236 || try_cmd_1args(key, "shift_b", shift_active_win, 'b'))
245 extern uint8_t winkeyb_control(int key)
247 if ( try_cmd_1args(key, "w_keys_m", wrap_mod_selected_keyb, 'w')
248 || try_cmd_2args(key, "w_keys_u", wrap_mv_kb_mod, 'w', 'u')
249 || try_cmd_2args(key, "w_keys_d", wrap_mv_kb_mod, 'w', 'd'))
258 extern uint8_t meta_control(int key)
260 uint8_t ret = (key == get_available_keycode_to_action("quit"));
262 && ( try_cmd_0args(key, "winconf", toggle_winconfig)
263 || try_cmd_0args(key, "reload_conf", reload_interface_conf)
264 || try_cmd_0args(key, "save_conf", save_interface_conf)
265 || try_cmd_0args(key, "map_c", map_center)
266 || try_cmd_1args(key, "scrl_r", scroll_pad, '+')
267 || try_cmd_1args(key, "scrl_l", scroll_pad, '-')
268 || try_cmd_1args(key, "to_a_keywin", toggle_window, 'k')
269 || try_cmd_1args(key, "to_g_keywin", toggle_window, '0')
270 || try_cmd_1args(key, "to_wg_keywin", toggle_window, '1')
271 || try_cmd_1args(key, "to_wk_keywin", toggle_window, '2')
272 || try_cmd_1args(key, "to_mapwin", toggle_window, 'm')
273 || try_cmd_1args(key, "to_infowin", toggle_window, 'i')
274 || try_cmd_1args(key, "to_inv", toggle_window, 'c')
275 || try_cmd_1args(key, "to_logwin", toggle_window, 'l')
276 || try_cmd_1args(key, "cyc_win_f", cycle_active_win, 'f')
277 || try_cmd_1args(key, "cyc_win_b", cycle_active_win, 'b')
278 || try_cmd_1args(key, "g_keys_m", wrap_mod_selected_keyb, 'G')
279 || try_cmd_1args(key, "wg_keys_m", wrap_mod_selected_keyb, 'g')
280 || try_cmd_1args(key, "wk_keys_m", wrap_mod_selected_keyb, 'k')
281 || try_cmd_1args(key, "inv_u", nav_inventory, 'u')
282 || try_cmd_1args(key, "inv_d", nav_inventory, 'd')
283 || try_cmd_1args(key, "map_u", map_scroll, 'N')
284 || try_cmd_1args(key, "map_d", map_scroll, 'S')
285 || try_cmd_1args(key, "map_r", map_scroll, 'E')
286 || try_cmd_1args(key, "map_l", map_scroll, 'W')
287 || try_cmd_2args(key, "g_keys_u", wrap_mv_kb_mod, 'G', 'u')
288 || try_cmd_2args(key, "g_keys_d", wrap_mv_kb_mod, 'G', 'd')
289 || try_cmd_2args(key, "wg_keys_u", wrap_mv_kb_mod, 'g', 'u')
290 || try_cmd_2args(key, "wg_keys_d", wrap_mv_kb_mod, 'g', 'd')
291 || try_cmd_2args(key, "wk_keys_u", wrap_mv_kb_mod, 'k', 'u')
292 || try_cmd_2args(key, "wk_keys_d", wrap_mv_kb_mod, 'k', 'd')))