home · contact · privacy
3d9c4903df8e82d0451cfdc86436b5b143e5cc6e
[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 /* If c == c_to_match, set "string" to "string_to_set". */
44 static uint8_t set_string_if_char_match(char c, char c_to_match,
45                                         char ** string, char * string_to_set);
46
47 /* Transform "command" to server command + argument string (free externally). */
48 static char * build_server_message_with_argument(struct Command * command);
49
50 /* Try out "command" as one for server messaging; sending is .server_msg,
51  * followed by either a string representing "command"'s .arg, or, if .arg is
52  * 'i', world.player_inventory_select, or, if .arg is '0', nothing. Return 1 on
53  * success, 0 on failure.
54  */
55 static uint8_t try_server_commands(struct Command * command);
56
57
58
59 static void nav_inventory(char dir)
60 {
61     if ('u' == dir)
62     {
63         world.player_inventory_select = world.player_inventory_select
64                                         - (world.player_inventory_select > 0);
65         return;
66     }
67     uint8_t n_elems = 0;
68     uint32_t i;
69     char * err = "Inventory string is too large.";
70     exit_err(UINT32_MAX <= strlen(world.player_inventory), err);
71     for (i = 0; '\0' != world.player_inventory[i]; i++)
72     {
73         n_elems = n_elems + ('\n' == world.player_inventory[i]);
74     }
75     world.player_inventory_select = world.player_inventory_select
76                                     + (world.player_inventory_select < n_elems);
77 }
78
79
80
81 static uint8_t try_0args(struct Command * command, char * match, void (* f) ())
82 {
83     if (!strcmp(command->dsc_short, match))
84     {
85         f();
86         return 1;
87     }
88     return 0;
89 }
90
91 static uint8_t try_1args(struct Command * command, char * match,
92                              void (* f) (char), char c)
93 {
94     if (!strcmp(command->dsc_short, match))
95     {
96         f(c);
97         return 1;
98     }
99     return 0;
100 }
101
102
103
104 static uint8_t try_2args(struct Command * command, char * match,
105                              void (* f) (char, char), char c1, char c2)
106 {
107     if (!strcmp(command->dsc_short, match))
108     {
109         f(c1, c2);
110         return 1;
111     }
112     return 0;
113 }
114
115
116
117 static uint8_t try_client_commands(struct Command * command)
118 {
119     return (   try_0args(command, "map_c", map_center)
120             || try_0args(command, "to_autofocus", toggle_autofocus)
121             || try_1args(command, "map_u", map_scroll, '8')
122             || try_1args(command, "map_d", map_scroll, '2')
123             || try_1args(command, "map_r", map_scroll, '6')
124             || try_1args(command, "map_l", map_scroll, '4')
125             || try_1args(command, "inv_u", nav_inventory, 'u')
126             || try_1args(command, "inv_d", nav_inventory, 'd')
127             || try_1args(command, "cyc_win_f", cycle_active_win, 'f')
128             || try_1args(command, "cyc_win_b", cycle_active_win, 'b')
129             || try_1args(command, "scrl_r", scroll_v_screen, '+')
130             || try_1args(command, "scrl_l", scroll_v_screen, '-')
131             || try_1args(command, "to_a_keywin", toggle_window, 'k')
132             || try_1args(command, "to_g_keywin", toggle_window, '0')
133             || try_1args(command, "to_wg_keywin", toggle_window, '1')
134             || try_1args(command, "to_wk_keywin", toggle_window, '2')
135             || try_1args(command, "to_mapwin", toggle_window, 'm')
136             || try_1args(command, "to_infowin", toggle_window, 'i')
137             || try_1args(command, "to_inv", toggle_window, 'c')
138             || try_1args(command, "to_logwin", toggle_window, 'l')
139             || try_0args(command, "winconf", toggle_winconfig)
140             || try_1args(command, "grow_h", resize_active_win, '*')
141             || try_1args(command, "shri_h", resize_active_win, '_')
142             || try_1args(command, "grow_v", resize_active_win, '+')
143             || try_1args(command, "shri_v", resize_active_win, '-')
144             || try_0args(command, "to_break", toggle_linebreak_type)
145             || try_1args(command, "to_height_t", toggle_win_size_type, 'y')
146             || try_1args(command, "to_width_t", toggle_win_size_type, 'x')
147             || try_1args(command, "shift_f", shift_active_win, 'f')
148             || try_1args(command, "shift_b", shift_active_win, 'b')
149             || try_0args(command, "reload_conf", reload_interface_conf)
150             || try_0args(command, "save_conf", save_interface_conf)
151             || try_1args(command, "g_keys_m", mod_selected_keyb, 'G')
152             || try_2args(command, "g_keys_u", move_keyb_selection, 'G', 'u')
153             || try_2args(command, "g_keys_d", move_keyb_selection, 'G', 'd')
154             || try_1args(command, "w_keys_m", mod_selected_keyb, 'w')
155             || try_2args(command, "w_keys_u", move_keyb_selection, 'w', 'u')
156             || try_2args(command, "w_keys_d", move_keyb_selection, 'w', 'd')
157             || try_1args(command, "wg_keys_m", mod_selected_keyb, 'g')
158             || try_2args(command, "wg_keys_u", move_keyb_selection, 'g', 'u')
159             || try_2args(command, "wg_keys_d", move_keyb_selection, 'g', 'd')
160             || try_1args(command, "wk_keys_m", mod_selected_keyb, 'k')
161             || try_2args(command, "wk_keys_u", move_keyb_selection, 'k', 'u')
162             || try_2args(command, "wk_keys_d", move_keyb_selection, 'k', 'd'));
163 }
164
165
166
167 static uint8_t set_string_if_char_match(char c, char c_to_match,
168                                         char ** string, char * string_to_set)
169 {
170     if (c == c_to_match)
171     {
172         *string = string_to_set;
173         return 1;
174     }
175     return 0;
176 }
177
178
179
180 static char * build_server_message_with_argument(struct Command * cmd)
181 {
182     uint8_t command_size = strlen(cmd->server_msg);
183     char * arg_str = "";
184     uint8_t arg_size = 0;
185     if ('i' == cmd->arg)
186     {
187         arg_size = 3;
188         arg_str = try_malloc(arg_size + 1, __func__);
189         int test = sprintf(arg_str, "%d",world.player_inventory_select);
190         exit_trouble(test < 0, __func__, "sprintf");
191     }
192     else if (   set_string_if_char_match(cmd->arg, 'd', &arg_str, "east")
193              || set_string_if_char_match(cmd->arg, 'c', &arg_str, "south-east")
194              || set_string_if_char_match(cmd->arg, 'x', &arg_str, "south-west")
195              || set_string_if_char_match(cmd->arg, 's', &arg_str, "west")
196              || set_string_if_char_match(cmd->arg, 'w', &arg_str, "north-west")
197              || set_string_if_char_match(cmd->arg, 'e', &arg_str, "north-east"))
198     {
199         arg_size = strlen(arg_str);
200     }
201     else
202     {
203         exit_err(1, "Illegal server command argument.");
204     }
205     char * msg = try_malloc(command_size + 1 + arg_size + 1, __func__);
206     int test = sprintf(msg, "%s %s", cmd->server_msg, arg_str);
207     exit_trouble(test < 0, __func__, "sprintf");
208     if ('i' == cmd->arg)
209     {
210         free(arg_str);
211     }
212     return msg;
213 }
214
215
216
217 static uint8_t try_server_commands(struct Command * command)
218 {
219     if (command->server_msg)
220     {
221         if ('0' == command->arg)
222         {
223             send(command->server_msg);
224         }
225         else
226         {
227             char * msg = build_server_message_with_argument(command);
228             send(msg);
229             free(msg);
230         }
231         return 1;
232     }
233     return 0;
234 }
235
236
237
238 extern uint8_t try_key(uint16_t key)
239 {
240     struct Command * command = get_command_to_keycode(&world.kb_global, key);
241     if (!command && world.winDB.active)
242     {
243         struct Win * w = get_win_by_id(world.winDB.active);
244         if      (0 == w->view)
245         {
246             command = get_command_to_keycode(&w->kb, key);
247         }
248         else if (1 == w->view)
249         {
250             command = get_command_to_keycode(&world.kb_wingeom, key);
251         }
252         else if (2 == w->view)
253         {
254             command = get_command_to_keycode(&world.kb_winkeys, key);
255         }
256     }
257     if (command)
258     {
259         if      (try_server_commands(command))
260         {
261             return 1;
262         }
263         else if (try_client_commands(command))
264         {
265             return 1;
266         }
267         else if (!strcmp("quit", command->dsc_short))
268         {
269             return 2;
270         }
271     }
272     return 0;
273 }