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