1 /* src/client/control.c */
4 #include <stdint.h> /* uint8_t, uint16_t */
5 #include <stdio.h> /* sprintf() */
6 #include <string.h> /* strlen() */
7 #include "command_db.h" /* get_command_data() */
8 #include "io.h" /* try_send() */
9 #include "keybindings.h" /* struct KeyBindingDB, get_command_to_keycode(),
10 * get_keycode_to_command(), mod_selected_keyb(),
11 * move_keyb_mod_selection()
13 #include "map_window.h" /* for map_scroll(), map_center() */
14 #include "misc.h" /* reload_interface_conf(), save_interface_conf(),
17 #include "wincontrol.h" /* struct WinConf, toggle_window(), toggle_winconfig(),
18 * scroll_pad(), get_winconf_by_win(),
19 * growshrink_active_window(), toggle_win_size_type()
21 #include "windows.h" /* for cycle_active_win(), shift_active_win() */
22 #include "world.h" /* for global world */
26 /* If "keycode" matches "match" in get_available_keycode_to_command(), execute
27 * "f" with provided char arguments and return 1; else only return 0.
29 static uint8_t try_cmd_0args(int keycode, char * match, void (* f) ());
30 static uint8_t try_cmd_1args(int keycode, char * match,
31 void (* f) (char), char c);
32 static uint8_t try_cmd_2args(int keycode, char * match,
33 void (* f) (char, char), char c1, char c2);
35 /* Return keycode to "command" if it is available in current window config. */
36 static uint16_t get_available_keycode_to_command(char * command);
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 KeyBindingDB * select_keybindingdb_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);
49 static uint8_t try_cmd_0args(int keycode, char * match, void (* f) ())
51 if (keycode == get_available_keycode_to_command(match))
61 static uint8_t try_cmd_1args(int keycode, char * match,
62 void (* f) (char), char c)
64 if (keycode == get_available_keycode_to_command(match))
74 static uint8_t try_cmd_2args(int keycode, char * match,
75 void (* f) (char, char), char c1, char c2)
77 if (keycode == get_available_keycode_to_command(match))
87 static uint16_t get_available_keycode_to_command(char * command)
89 uint16_t keycode = get_keycode_to_command(world.kb_global.kbs, command);
90 if (0 != keycode || 0 == world.wmeta.active)
94 struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
97 keycode = get_keycode_to_command(wc->kb.kbs, command);
99 else if (1 == wc->view)
101 keycode = get_keycode_to_command(world.kb_wingeom.kbs, command);
103 else if (2 == wc->view)
105 keycode = get_keycode_to_command(world.kb_winkeys.kbs, command);
112 static struct KeyBindingDB * select_keybindingdb_pointer(char c)
114 struct KeyBindingDB * kbd;
115 kbd = &world.kb_global;
118 kbd = &world.kb_wingeom;
122 kbd = &world.kb_winkeys;
126 struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
134 static void wrap_mod_selected_keyb(char c)
136 mod_selected_keyb(select_keybindingdb_pointer(c));
141 static void wrap_mv_kb_mod(char c1, char c2)
143 move_keyb_mod_selection(select_keybindingdb_pointer(c1), c2);
148 extern uint8_t player_control(int key)
150 char * cmd = get_command_to_keycode(world.kb_global.kbs, key);
151 if (NULL == cmd && 0 != world.wmeta.active)
153 struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
154 cmd = get_command_to_keycode(wc->kb.kbs, key);
158 struct Command * command_data = get_command_data(cmd);
159 uint8_t arg = (uint8_t) command_data->arg;
162 arg = world.player_inventory_select;
164 uint8_t command_size = strlen(command_data->server_msg);
165 uint8_t arg_size = 3;
166 char msg[command_size + 1 + arg_size + 1];
167 sprintf(msg, "%s %d", command_data->server_msg, arg);
176 extern uint8_t wingeom_control(int key)
178 if ( try_cmd_1args(key, "to_height_t", toggle_win_size_type, 'y')
179 || try_cmd_1args(key, "to_width_t", toggle_win_size_type, 'x')
180 || try_cmd_1args(key, "grow_h", growshrink_active_window, '*')
181 || try_cmd_1args(key, "shri_h", growshrink_active_window, '_')
182 || try_cmd_1args(key, "grow_v", growshrink_active_window, '+')
183 || try_cmd_1args(key, "shri_v", growshrink_active_window, '-')
184 || try_cmd_1args(key, "shift_f", shift_active_win, 'f')
185 || try_cmd_1args(key, "shift_b", shift_active_win, 'b'))
194 extern uint8_t winkeyb_control(int key)
196 if ( try_cmd_1args(key, "w_keys_m", wrap_mod_selected_keyb, 'w')
197 || try_cmd_2args(key, "w_keys_u", wrap_mv_kb_mod, 'w', 'u')
198 || try_cmd_2args(key, "w_keys_d", wrap_mv_kb_mod, 'w', 'd'))
207 extern uint8_t meta_control(int key)
209 uint8_t ret = 2 * (key == get_available_keycode_to_command("quit"));
211 && ( try_cmd_0args(key, "winconf", toggle_winconfig)
212 || try_cmd_0args(key, "reload_conf", reload_interface_conf)
213 || try_cmd_0args(key, "save_conf", save_interface_conf)
214 || try_cmd_0args(key, "map_c", map_center)
215 || try_cmd_1args(key, "scrl_r", scroll_pad, '+')
216 || try_cmd_1args(key, "scrl_l", scroll_pad, '-')
217 || try_cmd_1args(key, "to_a_keywin", toggle_window, 'k')
218 || try_cmd_1args(key, "to_g_keywin", toggle_window, '0')
219 || try_cmd_1args(key, "to_wg_keywin", toggle_window, '1')
220 || try_cmd_1args(key, "to_wk_keywin", toggle_window, '2')
221 || try_cmd_1args(key, "to_mapwin", toggle_window, 'm')
222 || try_cmd_1args(key, "to_infowin", toggle_window, 'i')
223 || try_cmd_1args(key, "to_inv", toggle_window, 'c')
224 || try_cmd_1args(key, "to_logwin", toggle_window, 'l')
225 || try_cmd_1args(key, "cyc_win_f", cycle_active_win, 'f')
226 || try_cmd_1args(key, "cyc_win_b", cycle_active_win, 'b')
227 || try_cmd_1args(key, "g_keys_m", wrap_mod_selected_keyb, 'G')
228 || try_cmd_1args(key, "wg_keys_m", wrap_mod_selected_keyb, 'g')
229 || try_cmd_1args(key, "wk_keys_m", wrap_mod_selected_keyb, 'k')
230 || try_cmd_1args(key, "inv_u", nav_inventory, 'u')
231 || try_cmd_1args(key, "inv_d", nav_inventory, 'd')
232 || try_cmd_1args(key, "map_u", map_scroll, 'N')
233 || try_cmd_1args(key, "map_d", map_scroll, 'S')
234 || try_cmd_1args(key, "map_r", map_scroll, 'E')
235 || try_cmd_1args(key, "map_l", map_scroll, 'W')
236 || try_cmd_2args(key, "g_keys_u", wrap_mv_kb_mod, 'G', 'u')
237 || try_cmd_2args(key, "g_keys_d", wrap_mv_kb_mod, 'G', 'd')
238 || try_cmd_2args(key, "wg_keys_u", wrap_mv_kb_mod, 'g', 'u')
239 || try_cmd_2args(key, "wg_keys_d", wrap_mv_kb_mod, 'g', 'd')
240 || try_cmd_2args(key, "wk_keys_u", wrap_mv_kb_mod, 'k', 'u')
241 || try_cmd_2args(key, "wk_keys_d", wrap_mv_kb_mod, 'k', 'd')))