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