1 #include "io/input-key-acceptor.h"
2 #include "cmd-io/macro-util.h"
3 #include "core/window-redrawer.h"
4 #include "game-option/input-options.h"
5 #include "game-option/map-screen-options.h"
6 #include "io/signal-handlers.h"
7 #include "term/gameterm.h"
8 #include "util/string-processor.h"
9 #include "world/world.h"
11 bool inkey_base; /* See the "inkey()" function */
12 bool inkey_xtra; /* See the "inkey()" function */
13 bool inkey_scan; /* See the "inkey()" function */
14 bool inkey_flag; /* See the "inkey()" function */
19 * This special pointer allows a sequence of keys to be "inserted" into
20 * the stream of keys returned by "inkey()". This key sequence will not
21 * trigger any macros, and cannot be bypassed by the Borg. It is used
22 * in Angband to handle "keymaps".
24 concptr inkey_next = NULL;
26 /* Save macro trigger string for use in inkey_special() */
27 static char inkey_macro_trigger_string[1024];
30 * Local variable -- we are inside a "macro action"
32 * Do not match any macros until "ascii 30" is found.
34 static bool parse_macro = FALSE;
37 * Local variable -- we are inside a "macro trigger"
39 * Strip all keypresses until a low ascii value is found.
41 static bool parse_under = FALSE;
44 * Cancel macro action on the queue
46 static void forget_macro_action(void)
53 if (term_inkey(&ch, FALSE, TRUE))
65 * Helper function called only from "inkey()"
67 * This function does almost all of the "macro" processing.
69 * We use the "term_key_push()" function to handle "failed" macros, as well
70 * as "extra" keys read in while choosing the proper macro, and also to hold
71 * the action for the macro, plus a special "ascii 30" character indicating
72 * that any macro action in progress is complete. Embedded macros are thus
73 * illegal, unless a macro action includes an explicit "ascii 30" character,
74 * which would probably be a massive hack, and might break things.
76 * Only 500 (0+1+2+...+29+30) milliseconds may elapse between each key in
77 * the macro trigger sequence. If a key sequence forms the "prefix" of a
78 * macro trigger, 500 milliseconds must pass before the key sequence is
79 * known not to be that macro trigger.
81 static char inkey_aux(void)
83 int k = 0, n, p = 0, w = 0;
85 char *buf = inkey_macro_trigger_string;
90 if (term_inkey(&ch, FALSE, TRUE)) {
94 (void)(term_inkey(&ch, TRUE, TRUE));
109 k = macro_find_check(buf);
114 k = macro_find_maybe(buf);
119 if (0 == term_inkey(&ch, FALSE, TRUE)) {
128 term_xtra(TERM_XTRA_DELAY, w);
132 k = macro_find_ready(buf);
135 if (term_key_push(buf[--p]))
139 (void)term_inkey(&ch, TRUE, TRUE);
143 concptr pat = macro__pat[k];
146 if (term_key_push(buf[--p]))
151 if (term_key_push(30))
154 concptr act = macro__act[k];
158 if (term_key_push(act[--n]))
166 * @brief キー入力を受け付けるメインルーチン / Get a keypress from the user.
174 term_type *old = Term;
176 if (inkey_next && *inkey_next && !inkey_xtra) {
178 inkey_base = inkey_xtra = inkey_flag = inkey_scan = FALSE;
190 (void)term_get_cursor(&v);
192 /* Show the cursor if waiting, except sometimes in "command" mode */
193 if (!inkey_scan && (!inkey_flag || hilite_player || current_world_ptr->character_icky_depth > 0)) {
194 (void)term_set_cursor(1);
197 term_activate(angband_term[0]);
200 if (!inkey_base && inkey_scan && (0 != term_inkey(&kk, FALSE, FALSE))) {
204 if (!done && (0 != term_inkey(&kk, FALSE, FALSE))) {
208 term_activate(angband_term[0]);
209 current_world_ptr->character_saved = FALSE;
218 if (0 == term_inkey(&ch, TRUE, TRUE)) {
226 if (0 == term_inkey(&ch, FALSE, TRUE)) {
233 term_xtra(TERM_XTRA_DELAY, w);
246 if (parse_under && (ch <= 32)) {
253 } else if (ch == 31) {
256 } else if (parse_under) {
263 inkey_base = inkey_xtra = inkey_flag = inkey_scan = FALSE;
268 * Get a keypress from the user.
269 * And interpret special keys as internal code.
271 * This function is a Mega-Hack and depend on pref-xxx.prf's.
272 * Currently works on Linux(UNIX), Windows, and Macintosh only.
274 int inkey_special(bool numpad_cursor)
276 static const struct {
279 } modifier_key_list[] = {
280 { "shift-", SKEY_MOD_SHIFT },
281 { "control-", SKEY_MOD_CONTROL },
285 static const struct {
289 } special_key_list[] = {
290 { FALSE, "Down]", SKEY_DOWN },
291 { FALSE, "Left]", SKEY_LEFT },
292 { FALSE, "Right]", SKEY_RIGHT },
293 { FALSE, "Up]", SKEY_UP },
294 { FALSE, "Page_Up]", SKEY_PGUP },
295 { FALSE, "Page_Down]", SKEY_PGDOWN },
296 { FALSE, "Home]", SKEY_TOP },
297 { FALSE, "End]", SKEY_BOTTOM },
298 { TRUE, "KP_Down]", SKEY_DOWN },
299 { TRUE, "KP_Left]", SKEY_LEFT },
300 { TRUE, "KP_Right]", SKEY_RIGHT },
301 { TRUE, "KP_Up]", SKEY_UP },
302 { TRUE, "KP_Page_Up]", SKEY_PGUP },
303 { TRUE, "KP_Page_Down]", SKEY_PGDOWN },
304 { TRUE, "KP_Home]", SKEY_TOP },
305 { TRUE, "KP_End]", SKEY_BOTTOM },
306 { TRUE, "KP_2]", SKEY_DOWN },
307 { TRUE, "KP_4]", SKEY_LEFT },
308 { TRUE, "KP_6]", SKEY_RIGHT },
309 { TRUE, "KP_8]", SKEY_UP },
310 { TRUE, "KP_9]", SKEY_PGUP },
311 { TRUE, "KP_3]", SKEY_PGDOWN },
312 { TRUE, "KP_7]", SKEY_TOP },
313 { TRUE, "KP_1]", SKEY_BOTTOM },
317 static const struct {
320 } gcu_special_key_list[] = {
326 { "4~", SKEY_BOTTOM },
328 { "6~", SKEY_PGDOWN },
341 * Forget macro trigger ----
342 * It's important if we are already expanding macro action
344 inkey_macro_trigger_string[0] = '\0';
347 trig_len = strlen(inkey_macro_trigger_string);
349 return (int)((unsigned char)key);
350 if (trig_len == 1 && parse_macro) {
351 char c = inkey_macro_trigger_string[0];
352 forget_macro_action();
353 return (int)((unsigned char)c);
356 ascii_to_text(buf, inkey_macro_trigger_string);
357 if (prefix(str, "\\[")) {
360 for (i = 0; modifier_key_list[i].keyname; i++) {
361 if (prefix(str, modifier_key_list[i].keyname)) {
362 str += strlen(modifier_key_list[i].keyname);
363 modifier |= modifier_key_list[i].keyflag;
367 if (!modifier_key_list[i].keyname)
371 if (!numpad_as_cursorkey)
372 numpad_cursor = FALSE;
374 for (i = 0; special_key_list[i].keyname; i++) {
375 if ((!special_key_list[i].numpad || numpad_cursor) && streq(str, special_key_list[i].keyname)) {
376 skey = special_key_list[i].keycode;
382 forget_macro_action();
383 return (skey | modifier);
387 if (prefix(str, "\\e[")) {
390 for (i = 0; gcu_special_key_list[i].keyname; i++) {
391 if (streq(str, gcu_special_key_list[i].keyname)) {
392 return gcu_special_key_list[i].keycode;
397 inkey_macro_trigger_string[0] = '\0';
398 return (int)((unsigned char)key);
401 void stop_term_fresh(void)
403 for (int j = 0; j < 8; j++) {
405 angband_term[j]->never_fresh = TRUE;
409 void start_term_fresh(void)
411 for (int j = 0; j < 8; j++) {
413 angband_term[j]->never_fresh = FALSE;