home · contact · privacy
License everything (GPL).
[plomrogue] / src / client / control.c
1 /* src/client/control.c
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  */
7
8 #include "control.h"
9 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
10 #include <stdlib.h> /* free() */
11 #include <stdio.h> /* sprintf() */
12 #include <string.h> /* strlen() */
13 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
14 #include "../common/try_malloc.h" /* try_malloc() */
15 #include "interface_conf.h" /* reload_interface_conf(), save_interface_conf() */
16 #include "io.h" /* send() */
17 #include "keybindings.h" /* get_command_to_keycode(), get_keycode_to_command(),
18                           * mod_selected_keyb(), move_keyb_selection()
19                           */
20 #include "map.h" /* map_scroll(), map_center(), toggle_autofocus() */
21 #include "wincontrol.h" /* shift_active_win(), resize_active_win(),
22                          * toggle_win_size_type(), toggle_window(),
23                          * cycle_active_win(), scroll_v_screen(),
24                          * toggle_linebreak_type(), toggle_winconfig()
25                          */
26 #include "windows.h" /* get_win_by_id() */
27 #include "world.h" /* world */
28
29
30
31 /* Move world.inventory_sel up ("dir"="u") or down (else) as far as possible. */
32 static void nav_inventory(char dir);
33
34 /* If "command"'s .dsc_short fits "match", apply "f" with provided char
35  * arguments and return 1; else, return 0.
36  */
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);
42
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.
45  */
46 static uint8_t try_client_commands(struct Command * command);
47
48 /* If c == c_to_match, set "string" to "string_to_set". */
49 static uint8_t set_string_if_char_match(char c, char c_to_match,
50                                         char ** string, char * string_to_set);
51
52 /* Transform "command" to server command + argument string (free externally). */
53 static char * build_server_message_with_argument(struct Command * command);
54
55 /* Try out "command" as one for server messaging; sending is .server_msg,
56  * followed by either a string representing "command"'s .arg, or, if .arg is
57  * 'i', world.player_inventory_select, or, if .arg is '0', nothing. Return 1 on
58  * success, 0 on failure.
59  */
60 static uint8_t try_server_commands(struct Command * command);
61
62
63
64 static void nav_inventory(char dir)
65 {
66     if ('u' == dir)
67     {
68         world.player_inventory_select = world.player_inventory_select
69                                         - (world.player_inventory_select > 0);
70         return;
71     }
72     uint8_t n_elems = 0;
73     uint32_t i;
74     char * err = "Inventory string is too large.";
75     exit_err(UINT32_MAX <= strlen(world.player_inventory), err);
76     for (i = 0; '\0' != world.player_inventory[i]; i++)
77     {
78         n_elems = n_elems + ('\n' == world.player_inventory[i]);
79     }
80     world.player_inventory_select = world.player_inventory_select
81                                     + (world.player_inventory_select < n_elems);
82 }
83
84
85
86 static uint8_t try_0args(struct Command * command, char * match, void (* f) ())
87 {
88     if (!strcmp(command->dsc_short, match))
89     {
90         f();
91         return 1;
92     }
93     return 0;
94 }
95
96 static uint8_t try_1args(struct Command * command, char * match,
97                              void (* f) (char), char c)
98 {
99     if (!strcmp(command->dsc_short, match))
100     {
101         f(c);
102         return 1;
103     }
104     return 0;
105 }
106
107
108
109 static uint8_t try_2args(struct Command * command, char * match,
110                              void (* f) (char, char), char c1, char c2)
111 {
112     if (!strcmp(command->dsc_short, match))
113     {
114         f(c1, c2);
115         return 1;
116     }
117     return 0;
118 }
119
120
121
122 static uint8_t try_client_commands(struct Command * command)
123 {
124     return (   try_0args(command, "map_c", map_center)
125             || try_0args(command, "to_autofocus", toggle_autofocus)
126             || try_1args(command, "map_u", map_scroll, '8')
127             || try_1args(command, "map_d", map_scroll, '2')
128             || try_1args(command, "map_r", map_scroll, '6')
129             || try_1args(command, "map_l", map_scroll, '4')
130             || try_1args(command, "inv_u", nav_inventory, 'u')
131             || try_1args(command, "inv_d", nav_inventory, 'd')
132             || try_1args(command, "cyc_win_f", cycle_active_win, 'f')
133             || try_1args(command, "cyc_win_b", cycle_active_win, 'b')
134             || try_1args(command, "scrl_r", scroll_v_screen, '+')
135             || try_1args(command, "scrl_l", scroll_v_screen, '-')
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_0args(command, "winconf", toggle_winconfig)
145             || try_1args(command, "grow_h", resize_active_win, '*')
146             || try_1args(command, "shri_h", resize_active_win, '_')
147             || try_1args(command, "grow_v", resize_active_win, '+')
148             || try_1args(command, "shri_v", resize_active_win, '-')
149             || try_0args(command, "to_break", toggle_linebreak_type)
150             || try_1args(command, "to_height_t", toggle_win_size_type, 'y')
151             || try_1args(command, "to_width_t", toggle_win_size_type, 'x')
152             || try_1args(command, "shift_f", shift_active_win, 'f')
153             || try_1args(command, "shift_b", shift_active_win, 'b')
154             || try_0args(command, "reload_conf", reload_interface_conf)
155             || try_0args(command, "save_conf", save_interface_conf)
156             || try_1args(command, "g_keys_m", mod_selected_keyb, 'G')
157             || try_2args(command, "g_keys_u", move_keyb_selection, 'G', 'u')
158             || try_2args(command, "g_keys_d", move_keyb_selection, 'G', 'd')
159             || try_1args(command, "w_keys_m", mod_selected_keyb, 'w')
160             || try_2args(command, "w_keys_u", move_keyb_selection, 'w', 'u')
161             || try_2args(command, "w_keys_d", move_keyb_selection, 'w', 'd')
162             || try_1args(command, "wg_keys_m", mod_selected_keyb, 'g')
163             || try_2args(command, "wg_keys_u", move_keyb_selection, 'g', 'u')
164             || try_2args(command, "wg_keys_d", move_keyb_selection, 'g', 'd')
165             || try_1args(command, "wk_keys_m", mod_selected_keyb, 'k')
166             || try_2args(command, "wk_keys_u", move_keyb_selection, 'k', 'u')
167             || try_2args(command, "wk_keys_d", move_keyb_selection, 'k', 'd'));
168 }
169
170
171
172 static uint8_t set_string_if_char_match(char c, char c_to_match,
173                                         char ** string, char * string_to_set)
174 {
175     if (c == c_to_match)
176     {
177         *string = string_to_set;
178         return 1;
179     }
180     return 0;
181 }
182
183
184
185 static char * build_server_message_with_argument(struct Command * cmd)
186 {
187     uint8_t command_size = strlen(cmd->server_msg);
188     char * arg_str = "";
189     uint8_t arg_size = 0;
190     if ('i' == cmd->arg)
191     {
192         arg_size = 3;
193         arg_str = try_malloc(arg_size + 1, __func__);
194         int test = sprintf(arg_str, "%d",world.player_inventory_select);
195         exit_trouble(test < 0, __func__, "sprintf");
196     }
197     else if (   set_string_if_char_match(cmd->arg, 'd', &arg_str, "east")
198              || set_string_if_char_match(cmd->arg, 'c', &arg_str, "south-east")
199              || set_string_if_char_match(cmd->arg, 'x', &arg_str, "south-west")
200              || set_string_if_char_match(cmd->arg, 's', &arg_str, "west")
201              || set_string_if_char_match(cmd->arg, 'w', &arg_str, "north-west")
202              || set_string_if_char_match(cmd->arg, 'e', &arg_str, "north-east"))
203     {
204         arg_size = strlen(arg_str);
205     }
206     else
207     {
208         exit_err(1, "Illegal server command argument.");
209     }
210     char * msg = try_malloc(command_size + 1 + arg_size + 1, __func__);
211     int test = sprintf(msg, "%s %s", cmd->server_msg, arg_str);
212     exit_trouble(test < 0, __func__, "sprintf");
213     if ('i' == cmd->arg)
214     {
215         free(arg_str);
216     }
217     return msg;
218 }
219
220
221
222 static uint8_t try_server_commands(struct Command * command)
223 {
224     if (command->server_msg)
225     {
226         if ('0' == command->arg)
227         {
228             send(command->server_msg);
229         }
230         else
231         {
232             char * msg = build_server_message_with_argument(command);
233             send(msg);
234             free(msg);
235         }
236         return 1;
237     }
238     return 0;
239 }
240
241
242
243 extern uint8_t try_key(uint16_t key)
244 {
245     struct Command * command = get_command_to_keycode(&world.kb_global, key);
246     if (!command && world.winDB.active)
247     {
248         struct Win * w = get_win_by_id(world.winDB.active);
249         if      (0 == w->view)
250         {
251             command = get_command_to_keycode(&w->kb, key);
252         }
253         else if (1 == w->view)
254         {
255             command = get_command_to_keycode(&world.kb_wingeom, key);
256         }
257         else if (2 == w->view)
258         {
259             command = get_command_to_keycode(&world.kb_winkeys, key);
260         }
261     }
262     if (command)
263     {
264         if      (try_server_commands(command))
265         {
266             return 1;
267         }
268         else if (try_client_commands(command))
269         {
270             return 1;
271         }
272         else if (!strcmp("quit", command->dsc_short))
273         {
274             return 2;
275         }
276     }
277     return 0;
278 }