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" /* get_command_to_keycode(), get_keycode_to_command(),
9 * mod_selected_keyb(), move_keyb_selection()
11 #include "map_window.h" /* for map_scroll(), map_center() */
12 #include "misc.h" /* reload_interface_conf(), save_interface_conf(),
15 #include "wincontrol.h" /* struct WinConf, toggle_window(), toggle_winconfig(),
16 * scroll_pad(), get_winconf_by_win(),
17 * growshrink_active_window(), toggle_win_size_type()
19 #include "windows.h" /* for cycle_active_win(), shift_active_win() */
20 #include "world.h" /* for global world */
24 /* If "command"'s .dsc_short fits "match", apply "f" with provided char
25 * arguments and return 1; else, return 0.
27 static uint8_t try_0args(struct Command * command, char * match, void (* f) ());
28 static uint8_t try_1args(struct Command * command, char * match,
29 void (* f) (char), char c);
30 static uint8_t try_2args(struct Command * command, char * match,
31 void (* f) (char, char), char c1, char c2);
33 /* Try if "command" matches a hard-coded list of client-only commands and, if
34 * successful, execute the match and return 1. Else, return 0.
36 static uint8_t try_client_commands(struct Command * command);
38 /* Try out "command" as one for server messaging; sending is .server_msg,
39 * followed by either a string representing "command"'s .arg, or, if .arg is
40 * 'i', world.player_inventory_select. Return 1 on success, 0 on failure.
42 static uint8_t try_server_commands(struct Command * command);
46 static uint8_t try_0args(struct Command * command, char * match, void (* f) ())
48 if (!strcmp(command->dsc_short, match))
56 static uint8_t try_1args(struct Command * command, char * match,
57 void (* f) (char), char c)
59 if (!strcmp(command->dsc_short, match))
69 static uint8_t try_2args(struct Command * command, char * match,
70 void (* f) (char, char), char c1, char c2)
72 if (!strcmp(command->dsc_short, match))
82 static uint8_t try_client_commands(struct Command * command)
84 return ( try_0args(command, "map_c", map_center)
85 || try_1args(command, "map_u", map_scroll, 'N')
86 || try_1args(command, "map_d", map_scroll, 'S')
87 || try_1args(command, "map_r", map_scroll, 'E')
88 || try_1args(command, "map_l", map_scroll, 'W')
89 || try_1args(command, "inv_u", nav_inventory, 'u')
90 || try_1args(command, "inv_d", nav_inventory, 'd')
91 || try_1args(command, "cyc_win_f", cycle_active_win, 'f')
92 || try_1args(command, "cyc_win_b", cycle_active_win, 'b')
93 || try_1args(command, "scrl_r", scroll_pad, '+')
94 || try_1args(command, "scrl_l", scroll_pad, '-')
95 || try_1args(command, "to_a_keywin", toggle_window, 'k')
96 || try_1args(command, "to_g_keywin", toggle_window, '0')
97 || try_1args(command, "to_wg_keywin", toggle_window, '1')
98 || try_1args(command, "to_wk_keywin", toggle_window, '2')
99 || try_1args(command, "to_mapwin", toggle_window, 'm')
100 || try_1args(command, "to_infowin", toggle_window, 'i')
101 || try_1args(command, "to_inv", toggle_window, 'c')
102 || try_1args(command, "to_logwin", toggle_window, 'l')
103 || try_0args(command, "winconf", toggle_winconfig)
104 || try_1args(command, "grow_h", growshrink_active_window, '*')
105 || try_1args(command, "shri_h", growshrink_active_window, '_')
106 || try_1args(command, "grow_v", growshrink_active_window, '+')
107 || try_1args(command, "shri_v", growshrink_active_window, '-')
108 || try_1args(command, "to_height_t", toggle_win_size_type, 'y')
109 || try_1args(command, "to_width_t", toggle_win_size_type, 'x')
110 || try_1args(command, "shift_f", shift_active_win, 'f')
111 || try_1args(command, "shift_b", shift_active_win, 'b')
112 || try_0args(command, "reload_conf", reload_interface_conf)
113 || try_0args(command, "save_conf", save_interface_conf)
114 || try_1args(command, "g_keys_m", mod_selected_keyb, 'G')
115 || try_2args(command, "g_keys_u", move_keyb_selection, 'G', 'u')
116 || try_2args(command, "g_keys_d", move_keyb_selection, 'G', 'd')
117 || try_1args(command, "w_keys_m", mod_selected_keyb, 'w')
118 || try_2args(command, "w_keys_u", move_keyb_selection, 'w', 'u')
119 || try_2args(command, "w_keys_d", move_keyb_selection, 'w', 'd')
120 || try_1args(command, "wg_keys_m", mod_selected_keyb, 'g')
121 || try_2args(command, "wg_keys_u", move_keyb_selection, 'g', 'u')
122 || try_2args(command, "wg_keys_d", move_keyb_selection, 'g', 'd')
123 || try_1args(command, "wk_keys_m", mod_selected_keyb, 'k')
124 || try_2args(command, "wk_keys_u", move_keyb_selection, 'k', 'u')
125 || try_2args(command, "wk_keys_d", move_keyb_selection, 'k', 'd'));
130 static uint8_t try_server_commands(struct Command * command)
132 if (command->server_msg)
134 uint8_t arg = (uint8_t) command->arg;
137 arg = world.player_inventory_select;
139 uint8_t command_size = strlen(command->server_msg);
140 uint8_t arg_size = 3;
141 char msg[command_size + 1 + arg_size + 1];
142 sprintf(msg, "%s %d", command->server_msg, arg);
151 extern uint8_t try_key(uint16_t key)
153 struct Command * command = get_command_to_keycode(world.kb_global.kbs, key);
156 struct WinConf * wc = get_winconf_by_win(world.wmeta.active);
159 command = get_command_to_keycode(wc->kb.kbs, key);
161 else if (1 == wc->view)
163 command = get_command_to_keycode(world.kb_wingeom.kbs, key);
165 else if (2 == wc->view)
167 command = get_command_to_keycode(world.kb_winkeys.kbs, key);
172 if (try_server_commands(command))
176 else if (try_client_commands(command))
180 else if (!strcmp("quit", command->dsc_short))