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