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