4 #include <stdint.h> /* for uint8_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 "rexit.h" /* for exit_err() */
14 #include "wincontrol.h" /* for struct WinConf, scroll_pad(), toggle_window(),
15 * growshrink_active_window(),toggle_winconfig(),
16 * toggle_win_height_type(), toggle_win_width_type()
18 #include "map_object_actions.h" /* for player_wait(), move_player(),
19 * player_drop(), player_pick()
21 #include "command_db.h" /* for is_command_id_shortdsc() */
22 #include "misc.h" /* for reload_interface_conf(), save_interface_conf(),
28 /* If "cmd" (either (type = "i") command or (type = "k") keybinding identifier)
29 * matches "match" in is_cmd_id_shortdsc() or get_available_keycode_to_action(),
30 * execute "f" with provided char arguments and return 1; else only return 0.
32 static uint8_t try_cmd_0args(char type, int cmd, char * match, void (* f) ());
33 static uint8_t try_cmd_1args(char type, int cmd, char * match,
34 void (* f) (char), char c);
35 static uint8_t try_cmd_2args(char type, int cmd, char * match,
36 void (* f) (char, char), char c1, char c2);
38 /* Return pointer to global keybindings or to keybindings for wingeometry config
39 * (c = "g") or winkeys config (c = "k") or active window's keybindings ("w").
41 static struct KeyBiData * select_keybidata_pointer(char c);
43 /* Wrappers to make some functions compatible to try_cmd_* single char args. */
44 static void wrap_mod_selected_keyb(char c);
45 static void wrap_mv_kb_mod(char c1, char c2);
46 static void wrap_cycle_active_win(char c);
47 static void wrap_shift_active_win(char c);
51 static uint8_t try_cmd_0args(char type, int cmd, char * match, void (* f) ())
53 if ( ('k' == type && cmd == get_available_keycode_to_action(match))
54 || ('i' == type && is_command_id_shortdsc(cmd, match)))
64 static uint8_t try_cmd_1args(char type, int cmd, char * match,
65 void (* f) (char), char c)
67 if ( ('k' == type && cmd == get_available_keycode_to_action(match))
68 || ('i' == type && is_command_id_shortdsc(cmd, match)))
78 static uint8_t try_cmd_2args(char type, int cmd, char * match,
79 void (* f) (char, char), char c1, char c2)
81 if ( ('k' == type && cmd == get_available_keycode_to_action(match))
82 || ('i' == type && is_command_id_shortdsc(cmd, match)))
92 static struct KeyBiData * select_keybidata_pointer(char c)
94 struct KeyBiData * kbd;
95 kbd = &world.kb_global;
98 kbd = &world.kb_wingeom;
102 kbd = &world.kb_winkeys;
106 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
114 static void wrap_mod_selected_keyb(char c)
116 mod_selected_keyb(select_keybidata_pointer(c));
121 static void wrap_mv_kb_mod(char c1, char c2)
123 move_keyb_mod_selection(select_keybidata_pointer(c1), c2);
128 static void wrap_cycle_active_win(char c)
130 cycle_active_win(world.wmeta, c);
135 static void wrap_shift_active_win(char c)
137 char * err = "Trouble with shift_active_win() in wingeom_control().";
138 exit_err(shift_active_win(world.wmeta, c), err);
143 extern uint16_t get_available_keycode_to_action(char * name)
145 uint16_t keycode = get_keycode_to_action(world.kb_global.kbs, name);
146 if (0 != keycode || 0 == world.wmeta->active)
150 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
153 keycode = get_keycode_to_action(wc->kb.kbs, name);
155 else if (1 == wc->view)
157 keycode = get_keycode_to_action(world.kb_wingeom.kbs, name);
159 else if (2 == wc->view)
161 keycode = get_keycode_to_action(world.kb_winkeys.kbs, name);
168 extern uint8_t player_control_by_key(int key)
170 char * action_name = get_func_to_keycode(world.kb_global.kbs, key);
171 if (NULL == action_name && 0 != world.wmeta->active)
173 struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
174 action_name = get_func_to_keycode(wc->kb.kbs, key);
176 if (NULL != action_name)
178 uint8_t action_id = get_command_id(action_name);
179 return player_control_by_id(action_id);
186 extern uint8_t player_control_by_id(int action)
188 if ( try_cmd_0args('i', action, "wait", player_wait)
189 || try_cmd_0args('i', action, "drop", player_drop)
190 || try_cmd_0args('i', action, "pick", player_pick)
191 || try_cmd_1args('i', action, "player_u", move_player, 'N')
192 || try_cmd_1args('i', action, "player_d", move_player, 'S')
193 || try_cmd_1args('i', action, "player_r", move_player, 'E')
194 || try_cmd_1args('i', action, "player_l", move_player, 'W'))
203 extern uint8_t wingeom_control(int key)
205 if ( try_cmd_0args('k', key, "to_height_t", toggle_win_height_type)
206 || try_cmd_0args('k', key, "to_width_t", toggle_win_width_type)
207 || try_cmd_1args('k', key, "grow_h", growshrink_active_window, '*')
208 || try_cmd_1args('k', key, "shri_h", growshrink_active_window, '_')
209 || try_cmd_1args('k', key, "grow_v", growshrink_active_window, '+')
210 || try_cmd_1args('k', key, "shri_v", growshrink_active_window, '-')
211 || try_cmd_1args('k', key, "shift_f", wrap_shift_active_win, 'f')
212 || try_cmd_1args('k', key, "shift_b", wrap_shift_active_win, 'b'))
221 extern uint8_t winkeyb_control(int key)
223 if ( try_cmd_1args('k', key, "w_keys_m", wrap_mod_selected_keyb, 'w')
224 || try_cmd_2args('k', key, "w_keys_u", wrap_mv_kb_mod, 'w', 'u')
225 || try_cmd_2args('k', key, "w_keys_d", wrap_mv_kb_mod, 'w', 'd'))
234 extern uint8_t meta_control(int key)
236 uint8_t ret = (key == get_available_keycode_to_action("quit"));
238 && ( try_cmd_0args('k', key, "winconf", toggle_winconfig)
239 || try_cmd_0args('k', key, "reload_conf", reload_interface_conf)
240 || try_cmd_0args('k', key, "save_conf", save_interface_conf)
241 || try_cmd_0args('k', key, "map_c", map_center)
242 || try_cmd_1args('k', key, "scrl_r", scroll_pad, '+')
243 || try_cmd_1args('k', key, "scrl_l", scroll_pad, '-')
244 || try_cmd_1args('k', key, "to_a_keywin", toggle_window, 'k')
245 || try_cmd_1args('k', key, "to_g_keywin", toggle_window, '0')
246 || try_cmd_1args('k', key, "to_wg_keywin", toggle_window, '1')
247 || try_cmd_1args('k', key, "to_wk_keywin", toggle_window, '2')
248 || try_cmd_1args('k', key, "to_mapwin", toggle_window, 'm')
249 || try_cmd_1args('k', key, "to_infowin", toggle_window, 'i')
250 || try_cmd_1args('k', key, "to_inv", toggle_window, 'c')
251 || try_cmd_1args('k', key, "to_logwin", toggle_window, 'l')
252 || try_cmd_1args('k', key, "cyc_win_f", wrap_cycle_active_win, 'f')
253 || try_cmd_1args('k', key, "cyc_win_b", wrap_cycle_active_win, 'b')
254 || try_cmd_1args('k', key, "g_keys_m", wrap_mod_selected_keyb, 'G')
255 || try_cmd_1args('k', key, "wg_keys_m", wrap_mod_selected_keyb, 'g')
256 || try_cmd_1args('k', key, "wk_keys_m", wrap_mod_selected_keyb, 'k')
257 || try_cmd_1args('k', key, "inv_u", nav_inventory, 'u')
258 || try_cmd_1args('k', key, "inv_d", nav_inventory, 'd')
259 || try_cmd_1args('k', key, "map_u", map_scroll, 'N')
260 || try_cmd_1args('k', key, "map_d", map_scroll, 'S')
261 || try_cmd_1args('k', key, "map_r", map_scroll, 'E')
262 || try_cmd_1args('k', key, "map_l", map_scroll, 'W')
263 || try_cmd_2args('k', key, "g_keys_u", wrap_mv_kb_mod, 'G', 'u')
264 || try_cmd_2args('k', key, "g_keys_d", wrap_mv_kb_mod, 'G', 'd')
265 || try_cmd_2args('k', key, "wg_keys_u", wrap_mv_kb_mod, 'g', 'u')
266 || try_cmd_2args('k', key, "wg_keys_d", wrap_mv_kb_mod, 'g', 'd')
267 || try_cmd_2args('k', key, "wk_keys_u", wrap_mv_kb_mod, 'k', 'u')
268 || try_cmd_2args('k', key, "wk_keys_d", wrap_mv_kb_mod, 'k', 'd')))