home · contact · privacy
Client: Send no arguments for commands where the server expects none.
[plomrogue] / src / client / control.c
1 /* src/client/control.c */
2
3 #include "control.h"
4 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
5 #include <stdlib.h> /* free() */
6 #include <stdio.h> /* sprintf() */
7 #include <string.h> /* strlen() */
8 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
9 #include "../common/try_malloc.h" /* try_malloc() */
10 #include "interface_conf.h" /* reload_interface_conf(), save_interface_conf() */
11 #include "io.h" /* send() */
12 #include "keybindings.h" /* get_command_to_keycode(), get_keycode_to_command(),
13                           * mod_selected_keyb(), move_keyb_selection()
14                           */
15 #include "map.h" /* map_scroll(), map_center(), toggle_autofocus() */
16 #include "wincontrol.h" /* shift_active_win(), resize_active_win(),
17                          * toggle_win_size_type(), toggle_window(),
18                          * cycle_active_win(), scroll_v_screen(),
19                          * toggle_linebreak_type(), toggle_winconfig()
20                          */
21 #include "windows.h" /* get_win_by_id() */
22 #include "world.h" /* world */
23
24
25
26 /* Move world.inventory_sel up ("dir"="u") or down (else) as far as possible. */
27 static void nav_inventory(char dir);
28
29 /* If "command"'s .dsc_short fits "match", apply "f" with provided char
30  * arguments and return 1; else, return 0.
31  */
32 static uint8_t try_0args(struct Command * command, char * match, void (* f) ());
33 static uint8_t try_1args(struct Command * command, char * match,
34                          void (* f) (char), char c);
35 static uint8_t try_2args(struct Command * command, char * match,
36                          void (* f) (char, char), char c1, char c2);
37
38 /* Try if "command" matches a hard-coded list of client-only commands and, if
39  * successful, execute the match and return 1. Else, return 0.
40  */
41 static uint8_t try_client_commands(struct Command * command);
42
43 /* Try out "command" as one for server messaging; sending is .server_msg,
44  * followed by either a string representing "command"'s .arg, or, if .arg is
45  * 'i', world.player_inventory_select, or, if .arg is '0', nothing. Return 1 on
46  * success, 0 on failure.
47  */
48 static uint8_t try_server_commands(struct Command * command);
49
50
51
52 static void nav_inventory(char dir)
53 {
54     if ('u' == dir)
55     {
56         world.player_inventory_select = world.player_inventory_select
57                                         - (world.player_inventory_select > 0);
58         return;
59     }
60     uint8_t n_elems = 0;
61     uint32_t i;
62     char * err = "Inventory string is too large.";
63     exit_err(UINT32_MAX <= strlen(world.player_inventory), err);
64     for (i = 0; '\0' != world.player_inventory[i]; i++)
65     {
66         n_elems = n_elems + ('\n' == world.player_inventory[i]);
67     }
68     world.player_inventory_select = world.player_inventory_select
69                                     + (world.player_inventory_select < n_elems);
70 }
71
72
73
74 static uint8_t try_0args(struct Command * command, char * match, void (* f) ())
75 {
76     if (!strcmp(command->dsc_short, match))
77     {
78         f();
79         return 1;
80     }
81     return 0;
82 }
83
84 static uint8_t try_1args(struct Command * command, char * match,
85                              void (* f) (char), char c)
86 {
87     if (!strcmp(command->dsc_short, match))
88     {
89         f(c);
90         return 1;
91     }
92     return 0;
93 }
94
95
96
97 static uint8_t try_2args(struct Command * command, char * match,
98                              void (* f) (char, char), char c1, char c2)
99 {
100     if (!strcmp(command->dsc_short, match))
101     {
102         f(c1, c2);
103         return 1;
104     }
105     return 0;
106 }
107
108
109
110 static uint8_t try_client_commands(struct Command * command)
111 {
112     return (   try_0args(command, "map_c", map_center)
113             || try_0args(command, "to_autofocus", toggle_autofocus)
114             || try_1args(command, "map_u", map_scroll, '8')
115             || try_1args(command, "map_d", map_scroll, '2')
116             || try_1args(command, "map_r", map_scroll, '6')
117             || try_1args(command, "map_l", map_scroll, '4')
118             || try_1args(command, "inv_u", nav_inventory, 'u')
119             || try_1args(command, "inv_d", nav_inventory, 'd')
120             || try_1args(command, "cyc_win_f", cycle_active_win, 'f')
121             || try_1args(command, "cyc_win_b", cycle_active_win, 'b')
122             || try_1args(command, "scrl_r", scroll_v_screen, '+')
123             || try_1args(command, "scrl_l", scroll_v_screen, '-')
124             || try_1args(command, "to_a_keywin", toggle_window, 'k')
125             || try_1args(command, "to_g_keywin", toggle_window, '0')
126             || try_1args(command, "to_wg_keywin", toggle_window, '1')
127             || try_1args(command, "to_wk_keywin", toggle_window, '2')
128             || try_1args(command, "to_mapwin", toggle_window, 'm')
129             || try_1args(command, "to_infowin", toggle_window, 'i')
130             || try_1args(command, "to_inv", toggle_window, 'c')
131             || try_1args(command, "to_logwin", toggle_window, 'l')
132             || try_0args(command, "winconf", toggle_winconfig)
133             || try_1args(command, "grow_h", resize_active_win, '*')
134             || try_1args(command, "shri_h", resize_active_win, '_')
135             || try_1args(command, "grow_v", resize_active_win, '+')
136             || try_1args(command, "shri_v", resize_active_win, '-')
137             || try_0args(command, "to_break", toggle_linebreak_type)
138             || try_1args(command, "to_height_t", toggle_win_size_type, 'y')
139             || try_1args(command, "to_width_t", toggle_win_size_type, 'x')
140             || try_1args(command, "shift_f", shift_active_win, 'f')
141             || try_1args(command, "shift_b", shift_active_win, 'b')
142             || try_0args(command, "reload_conf", reload_interface_conf)
143             || try_0args(command, "save_conf", save_interface_conf)
144             || try_1args(command, "g_keys_m", mod_selected_keyb, 'G')
145             || try_2args(command, "g_keys_u", move_keyb_selection, 'G', 'u')
146             || try_2args(command, "g_keys_d", move_keyb_selection, 'G', 'd')
147             || try_1args(command, "w_keys_m", mod_selected_keyb, 'w')
148             || try_2args(command, "w_keys_u", move_keyb_selection, 'w', 'u')
149             || try_2args(command, "w_keys_d", move_keyb_selection, 'w', 'd')
150             || try_1args(command, "wg_keys_m", mod_selected_keyb, 'g')
151             || try_2args(command, "wg_keys_u", move_keyb_selection, 'g', 'u')
152             || try_2args(command, "wg_keys_d", move_keyb_selection, 'g', 'd')
153             || try_1args(command, "wk_keys_m", mod_selected_keyb, 'k')
154             || try_2args(command, "wk_keys_u", move_keyb_selection, 'k', 'u')
155             || try_2args(command, "wk_keys_d", move_keyb_selection, 'k', 'd'));
156 }
157
158
159
160 static uint8_t try_server_commands(struct Command * command)
161 {
162     char * f_name = "try_server_commands()";
163     if (command->server_msg)
164     {
165         uint8_t arg = (uint8_t) command->arg;
166         if ('0' == arg)
167         {
168             send(command->server_msg);
169         }
170         else
171         {
172             if ('i' == arg)
173             {
174                 arg = world.player_inventory_select;
175             }
176             uint8_t command_size = strlen(command->server_msg);
177             uint8_t arg_size = 3;
178             char * msg = try_malloc(command_size + 1 + arg_size + 1, f_name);
179             int test = sprintf(msg, "%s %d", command->server_msg, arg);
180             exit_trouble(test < 0, f_name, "sprintf()");
181             send(msg);
182             free(msg);
183         }
184         return 1;
185     }
186     return 0;
187 }
188
189
190
191 extern uint8_t try_key(uint16_t key)
192 {
193     struct Command * command = get_command_to_keycode(&world.kb_global, key);
194     if (!command && world.winDB.active)
195     {
196         struct Win * w = get_win_by_id(world.winDB.active);
197         if      (0 == w->view)
198         {
199             command = get_command_to_keycode(&w->kb, key);
200         }
201         else if (1 == w->view)
202         {
203             command = get_command_to_keycode(&world.kb_wingeom, key);
204         }
205         else if (2 == w->view)
206         {
207             command = get_command_to_keycode(&world.kb_winkeys, key);
208         }
209     }
210     if (command)
211     {
212         if      (try_server_commands(command))
213         {
214             return 1;
215         }
216         else if (try_client_commands(command))
217         {
218             return 1;
219         }
220         else if (!strcmp("quit", command->dsc_short))
221         {
222             return 2;
223         }
224     }
225     return 0;
226 }