home · contact · privacy
7DRL: Make "Things here" window scrollable.
[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" /* toggle_lookmode(), lookmode_nav()*/
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 void nav_stack(char dir)  //
91 {
92     FILE * file = fopen("test", "a");
93     fprintf(file, "called %d\n", world.things_here_scroll);
94     if ('u' == dir && world.things_here_scroll > 0)
95     {
96         world.things_here_scroll--;
97         fprintf(file, "u\n");
98     }
99     else if ('d' == dir && world.things_here)
100     {
101         uint32_t i;
102         uint32_t n_lines = 1;
103         for (i = 0; '\0' != world.things_here[i]; i++)
104         {
105             n_lines = n_lines + (world.things_here[i] == '\n');
106         }
107         uint16_t f_h = get_win_by_id('s')->frame_size.y;
108         fprintf(file, "%d %d\n", n_lines, f_h);
109         if (n_lines > f_h && world.things_here_scroll < n_lines - f_h)
110         {
111             world.things_here_scroll++;
112         }
113         fprintf(file, "d %d\n", world.things_here_scroll);
114     }
115     fclose(file);
116 }
117
118
119
120 static uint8_t try_0args(struct Command * command, char * match, void (* f) ())
121 {
122     if (!strcmp(command->dsc_short, match))
123     {
124         f();
125         return 1;
126     }
127     return 0;
128 }
129
130
131
132 static uint8_t try_1args(struct Command * command, char * match,
133                              void (* f) (char), char c)
134 {
135     if (!strcmp(command->dsc_short, match))
136     {
137         f(c);
138         return 1;
139     }
140     return 0;
141 }
142
143
144
145 static uint8_t try_kb_manip(char * command)
146 {
147     char * cmp = "keyb_";
148     if (strlen(command) == strlen(cmp)+2 && !strncmp(command, cmp, strlen(cmp)))
149     {
150         if ('m' == command[strlen(cmp)])
151         {
152             mod_selected_keyb(command[strlen(cmp) + 1]);
153         }
154         else if ('u' == command[strlen(cmp)] || 'd' == command[strlen(cmp)])
155         {
156             move_keyb_selection(command[strlen(cmp) + 1], command[strlen(cmp)]);
157         }
158         return 1;
159     }
160     return 0;
161 }
162
163
164
165 static uint8_t try_client_commands(struct Command * command)
166 {
167     return (   try_1args(command, "inv_u", nav_inventory, 'u')
168             || try_1args(command, "inv_d", nav_inventory, 'd')
169             || try_1args(command, "stack_u", nav_stack, 'u') //
170             || try_1args(command, "stack_d", nav_stack, 'd') //
171             || try_1args(command, "cyc_win_f", cycle_active_win, 'f')
172             || try_1args(command, "cyc_win_b", cycle_active_win, 'b')
173             || try_1args(command, "scrl_r", scroll_v_screen, '+')
174             || try_1args(command, "scrl_l", scroll_v_screen, '-')
175             || try_1args(command, "to_g_keywin", toggle_window, '0')
176             || try_1args(command, "to_wg_keywin", toggle_window, '1')
177             || try_1args(command, "to_wk_keywin", toggle_window, '2')
178             || try_1args(command, "to_mapwin", toggle_window, 'm')
179             || try_1args(command, "to_infowin", toggle_window, 'i')
180             || try_1args(command, "to_inv", toggle_window, 'c')
181             || try_1args(command, "to_logwin", toggle_window, 'l')
182             || try_1args(command, "to_terrain", toggle_window, 's')
183             || try_0args(command, "winconf", toggle_winconfig)
184             || try_1args(command, "grow_h", resize_active_win, '*')
185             || try_1args(command, "shri_h", resize_active_win, '_')
186             || try_1args(command, "grow_v", resize_active_win, '+')
187             || try_1args(command, "shri_v", resize_active_win, '-')
188             || try_0args(command, "to_break", toggle_linebreak_type)
189             || try_1args(command, "to_height_t", toggle_win_size_type, 'y')
190             || try_1args(command, "to_width_t", toggle_win_size_type, 'x')
191             || try_1args(command, "shift_f", shift_active_win, 'f')
192             || try_1args(command, "shift_b", shift_active_win, 'b')
193             || try_0args(command, "reload_conf", reload_interface_conf)
194             || try_0args(command, "save_conf", save_interface_conf)
195             || try_0args(command, "to_look", toggle_lookmode)
196             || try_kb_manip(command->dsc_short));
197 }
198
199
200
201 static uint8_t set_string_if_char_match(char c, char c_to_match,
202                                         char ** string, char * string_to_set)
203 {
204     if (c == c_to_match)
205     {
206         *string = string_to_set;
207         return 1;
208     }
209     return 0;
210 }
211
212
213
214 static char * build_server_message_with_argument(struct Command * cmd)
215 {
216     uint8_t command_size = strlen(cmd->server_msg);
217     char * arg_str = "";
218     uint8_t arg_size = 0;
219     if ('i' == cmd->arg)
220     {
221         arg_size = 3;
222         arg_str = try_malloc(arg_size + 1, __func__);
223         int test = sprintf(arg_str, "%d",world.player_inventory_select);
224         exit_trouble(test < 0, __func__, "sprintf");
225     }
226     else if (   set_string_if_char_match(cmd->arg, 'd', &arg_str, "east")
227              || set_string_if_char_match(cmd->arg, 'c', &arg_str, "south-east")
228              || set_string_if_char_match(cmd->arg, 'x', &arg_str, "south-west")
229              || set_string_if_char_match(cmd->arg, 's', &arg_str, "west")
230              || set_string_if_char_match(cmd->arg, 'w', &arg_str, "north-west")
231              || set_string_if_char_match(cmd->arg, 'e', &arg_str, "north-east"))
232     {
233         arg_size = strlen(arg_str);
234     }
235     else
236     {
237         exit_err(1, "Illegal server command argument.");
238     }
239     char * msg = try_malloc(command_size + 1 + arg_size + 1, __func__);
240     int test = sprintf(msg, "%s %s", cmd->server_msg, arg_str);
241     exit_trouble(test < 0, __func__, "sprintf");
242     if ('i' == cmd->arg)
243     {
244         free(arg_str);
245     }
246     return msg;
247 }
248
249
250
251 static uint8_t try_server_commands(struct Command * command)
252 {
253     if (command->server_msg)
254     {
255         if ('0' == command->arg)
256         {
257             send(command->server_msg);
258         }
259         else
260         {
261             char * msg = build_server_message_with_argument(command);
262             send(msg);
263             free(msg);
264         }
265         return 1;
266     }
267     return 0;
268 }
269
270
271
272 extern uint8_t try_key(uint16_t key)
273 {
274     struct Command * command = get_command_to_keycode(&world.kb_global, key);
275     if (!command && world.winDB.active)
276     {
277         struct Win * w = get_win_by_id(world.winDB.active);
278         if      (0 == w->view)
279         {
280             command = get_command_to_keycode(&w->kb, key);
281         }
282         else if (1 == w->view)
283         {
284             command = get_command_to_keycode(&world.kb_wingeom, key);
285         }
286         else if (2 == w->view)
287         {
288             command = get_command_to_keycode(&world.kb_winkeys, key);
289         }
290     }
291     if (command)
292     {
293         if (world.look && lookmode_nav(command->dsc_short))
294         {
295             return 1;
296         }
297         else if (try_client_commands(command))
298         {
299             return 1;
300         }
301         else if (try_server_commands(command))
302         {
303             return 1;
304         }
305         else if (!strcmp("quit", command->dsc_short))
306         {
307             return 2;
308         }
309     }
310     return 0;
311 }