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 "io.h" /* try_send() */
8 #include "keybindings.h" /* struct KeyBindingDB, get_command_to_keycode(),
9 * get_keycode_to_command(), mod_selected_keyb(),
10 * move_keyb_mod_selection()
12 #include "map_window.h" /* for map_scroll(), map_center() */
13 #include "misc.h" /* reload_interface_conf(), save_interface_conf(),
16 #include "wincontrol.h" /* struct WinConf, toggle_window(), toggle_winconfig(),
17 * scroll_pad(), get_winconf_by_win(),
18 * growshrink_active_window(), toggle_win_size_type()
20 #include "windows.h" /* for cycle_active_win(), shift_active_win() */
21 #include "world.h" /* for global world */
25 /* Return pointer to global keybindings or to keybindings for wingeometry config
26 * (c = "g") or winkeys config (c = "k") or active window's keybindings ("w").
28 static struct KeyBindingDB * select_keybindingdb_pointer(char c);
30 /* Wrappers to make some functions compatible to try_cmd_* single char args. */
31 static void wrap_mod_selected_keyb(char c);
32 static void wrap_mv_kb_mod(char c1, char c2);
34 /* If "command"'s .dsc_short fits "match", apply "f" with provided char
35 * arguments and return 1; else, return 0.
37 static uint8_t try_0args(struct Command * command, char * match, void (* f) ());
38 static uint8_t try_1args(struct Command * command, char * match,
39 void (* f) (char), char c);
40 static uint8_t try_2args(struct Command * command, char * match,
41 void (* f) (char, char), char c1, char c2);
43 /* Try if "command" matches a hard-coded list of client-only commands and, if
44 * successful, execute the match and return 1. Else, return 0.
46 static uint8_t try_client_commands(struct Command * command);
48 /* Try out "command" as one for server messaging; sending is .server_msg,
49 * followed by either a string representing "command"'s .arg, or, if .arg is
50 * 'i', world.player_inventory_select. Return 1 on success, 0 on failure.
52 static uint8_t try_server_commands(struct Command * command);
56 static struct KeyBindingDB * select_keybindingdb_pointer(char c)
58 struct KeyBindingDB * kbd;
59 kbd = &world.kb_global;
62 kbd = &world.kb_wingeom;
66 kbd = &world.kb_winkeys;
70 struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
78 static void wrap_mod_selected_keyb(char c)
80 mod_selected_keyb(select_keybindingdb_pointer(c));
85 static void wrap_mv_kb_mod(char c1, char c2)
87 move_keyb_mod_selection(select_keybindingdb_pointer(c1), c2);
92 static uint8_t try_0args(struct Command * command, char * match, void (* f) ())
94 if (!strcmp(command->dsc_short, match))
102 static uint8_t try_1args(struct Command * command, char * match,
103 void (* f) (char), char c)
105 if (!strcmp(command->dsc_short, match))
115 static uint8_t try_2args(struct Command * command, char * match,
116 void (* f) (char, char), char c1, char c2)
118 if (!strcmp(command->dsc_short, match))
128 static uint8_t try_client_commands(struct Command * command)
130 return ( try_0args(command, "winconf", toggle_winconfig)
131 || try_0args(command, "reload_conf", reload_interface_conf)
132 || try_0args(command, "save_conf", save_interface_conf)
133 || try_0args(command, "map_c", map_center)
134 || try_1args(command, "scrl_r", scroll_pad, '+')
135 || try_1args(command, "scrl_l", scroll_pad, '-')
136 || try_1args(command, "to_a_keywin", toggle_window, 'k')
137 || try_1args(command, "to_g_keywin", toggle_window, '0')
138 || try_1args(command, "to_wg_keywin", toggle_window, '1')
139 || try_1args(command, "to_wk_keywin", toggle_window, '2')
140 || try_1args(command, "to_mapwin", toggle_window, 'm')
141 || try_1args(command, "to_infowin", toggle_window, 'i')
142 || try_1args(command, "to_inv", toggle_window, 'c')
143 || try_1args(command, "to_logwin", toggle_window, 'l')
144 || try_1args(command, "cyc_win_f", cycle_active_win, 'f')
145 || try_1args(command, "cyc_win_b", cycle_active_win, 'b')
146 || try_1args(command, "g_keys_m", wrap_mod_selected_keyb, 'G')
147 || try_1args(command, "wg_keys_m", wrap_mod_selected_keyb, 'g')
148 || try_1args(command, "wk_keys_m", wrap_mod_selected_keyb, 'k')
149 || try_1args(command, "inv_u", nav_inventory, 'u')
150 || try_1args(command, "inv_d", nav_inventory, 'd')
151 || try_1args(command, "map_u", map_scroll, 'N')
152 || try_1args(command, "map_d", map_scroll, 'S')
153 || try_1args(command, "map_r", map_scroll, 'E')
154 || try_1args(command, "map_l", map_scroll, 'W')
155 || try_1args(command, "to_height_t", toggle_win_size_type, 'y')
156 || try_1args(command, "to_width_t", toggle_win_size_type, 'x')
157 || try_1args(command, "grow_h", growshrink_active_window, '*')
158 || try_1args(command, "shri_h", growshrink_active_window, '_')
159 || try_1args(command, "grow_v", growshrink_active_window, '+')
160 || try_1args(command, "shri_v", growshrink_active_window, '-')
161 || try_1args(command, "shift_f", shift_active_win, 'f')
162 || try_1args(command, "shift_b", shift_active_win, 'b')
163 || try_1args(command, "w_keys_m", wrap_mod_selected_keyb, 'w')
164 || try_2args(command, "w_keys_u", wrap_mv_kb_mod, 'w', 'u')
165 || try_2args(command, "w_keys_d", wrap_mv_kb_mod, 'w', 'd')
166 || try_2args(command, "g_keys_u", wrap_mv_kb_mod, 'G', 'u')
167 || try_2args(command, "g_keys_d", wrap_mv_kb_mod, 'G', 'd')
168 || try_2args(command, "wg_keys_u", wrap_mv_kb_mod, 'g', 'u')
169 || try_2args(command, "wg_keys_d", wrap_mv_kb_mod, 'g', 'd')
170 || try_2args(command, "wk_keys_u", wrap_mv_kb_mod, 'k', 'u')
171 || try_2args(command, "wk_keys_d", wrap_mv_kb_mod, 'k', 'd'));
176 static uint8_t try_server_commands(struct Command * command)
178 if (command->server_msg)
180 uint8_t arg = (uint8_t) command->arg;
183 arg = world.player_inventory_select;
185 uint8_t command_size = strlen(command->server_msg);
186 uint8_t arg_size = 3;
187 char msg[command_size + 1 + arg_size + 1];
188 sprintf(msg, "%s %d", command->server_msg, arg);
197 extern uint8_t try_key(uint16_t key)
199 struct Command * command = get_command_to_keycode(world.kb_global.kbs, key);
202 struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
205 command = get_command_to_keycode(wc->kb.kbs, key);
207 else if (1 == wc->view)
209 command = get_command_to_keycode(world.kb_wingeom.kbs, key);
211 else if (2 == wc->view)
213 command = get_command_to_keycode(world.kb_winkeys.kbs, key);
218 if (try_client_commands(command))
222 else if (!strcmp("quit", command->dsc_short))
226 else if (try_server_commands(command))