1 #include "io/input-key-acceptor.h"
2 #include "cmd-io/macro-util.h"
3 #include "core/stuff-handler.h"
4 #include "core/window-redrawer.h"
5 #include "game-option/input-options.h"
6 #include "game-option/map-screen-options.h"
7 #include "io/signal-handlers.h"
8 #include "system/player-type-definition.h"
9 #include "term/gameterm.h"
10 #include "util/string-processor.h"
11 #include "world/world.h"
13 bool inkey_base; /* See the "inkey()" function */
14 bool inkey_xtra; /* See the "inkey()" function */
15 bool inkey_scan; /* See the "inkey()" function */
16 bool inkey_flag; /* See the "inkey()" function */
21 * This special pointer allows a sequence of keys to be "inserted" into
22 * the stream of keys returned by "inkey()". This key sequence will not
23 * trigger any macros, and cannot be bypassed by the Borg. It is used
24 * in Angband to handle "keymaps".
26 concptr inkey_next = NULL;
28 /* Save macro trigger string for use in inkey_special() */
29 static char inkey_macro_trigger_string[1024];
32 * Local variable -- we are inside a "macro action"
34 * Do not match any macros until "ascii 30" is found.
36 static bool parse_macro = FALSE;
39 * Local variable -- we are inside a "macro trigger"
41 * Strip all keypresses until a low ascii value is found.
43 static bool parse_under = FALSE;
46 * @brief 全てのウィンドウの再描画を行う
51 static void all_term_fresh(int x, int y)
53 p_ptr->window_flags |= PW_ALL;
56 term_activate(angband_term[0]);
62 * Cancel macro action on the queue
64 static void forget_macro_action(void)
71 if (term_inkey(&ch, FALSE, TRUE))
83 * Helper function called only from "inkey()"
85 * This function does almost all of the "macro" processing.
87 * We use the "term_key_push()" function to handle "failed" macros, as well
88 * as "extra" keys read in while choosing the proper macro, and also to hold
89 * the action for the macro, plus a special "ascii 30" character indicating
90 * that any macro action in progress is complete. Embedded macros are thus
91 * illegal, unless a macro action includes an explicit "ascii 30" character,
92 * which would probably be a massive hack, and might break things.
94 * Only 500 (0+1+2+...+29+30) milliseconds may elapse between each key in
95 * the macro trigger sequence. If a key sequence forms the "prefix" of a
96 * macro trigger, 500 milliseconds must pass before the key sequence is
97 * known not to be that macro trigger.
99 static char inkey_aux(void)
101 int k = 0, n, p = 0, w = 0;
103 char *buf = inkey_macro_trigger_string;
108 if (term_inkey(&ch, FALSE, TRUE)) {
112 (void)(term_inkey(&ch, TRUE, TRUE));
127 k = macro_find_check(buf);
132 k = macro_find_maybe(buf);
137 if (0 == term_inkey(&ch, FALSE, TRUE)) {
146 term_xtra(TERM_XTRA_DELAY, w);
150 k = macro_find_ready(buf);
153 if (term_key_push(buf[--p]))
157 (void)term_inkey(&ch, TRUE, TRUE);
161 concptr pat = macro__pat[k];
164 if (term_key_push(buf[--p]))
169 if (term_key_push(30))
172 concptr act = macro__act[k];
176 if (term_key_push(act[--n]))
184 * @brief キー入力を受け付けるメインルーチン / Get a keypress from the user.
185 * @param do_all_term_refresh trueであれば強制的にhandle_stuffと再描画を行う。デフォルト false
188 char inkey(bool do_all_term_refresh)
192 term_type *old = Term;
194 if (inkey_next && *inkey_next && !inkey_xtra) {
196 inkey_base = inkey_xtra = inkey_flag = inkey_scan = FALSE;
208 (void)term_get_cursor(&v);
210 /* Show the cursor if waiting, except sometimes in "command" mode */
211 if (!inkey_scan && (!inkey_flag || hilite_player || current_world_ptr->character_icky_depth > 0)) {
212 (void)term_set_cursor(1);
215 term_activate(angband_term[0]);
216 auto y = angband_term[0]->scr->cy;
217 auto x = angband_term[0]->scr->cx;
220 if (!inkey_base && inkey_scan && (0 != term_inkey(&kk, FALSE, FALSE))) {
224 if (!done && (0 != term_inkey(&kk, FALSE, FALSE))) {
226 if (do_all_term_refresh)
227 all_term_fresh(x, y);
230 current_world_ptr->character_saved = FALSE;
239 if (0 == term_inkey(&ch, TRUE, TRUE)) {
247 if (0 == term_inkey(&ch, FALSE, TRUE)) {
254 term_xtra(TERM_XTRA_DELAY, w);
267 if (parse_under && (ch <= 32)) {
274 } else if (ch == 31) {
277 } else if (parse_under) {
284 inkey_base = inkey_xtra = inkey_flag = inkey_scan = FALSE;
289 * Get a keypress from the user.
290 * And interpret special keys as internal code.
292 * This function is a Mega-Hack and depend on pref-xxx.prf's.
293 * Currently works on Linux(UNIX), Windows, and Macintosh only.
295 int inkey_special(bool numpad_cursor)
297 static const struct {
300 } modifier_key_list[] = {
301 { "shift-", SKEY_MOD_SHIFT },
302 { "control-", SKEY_MOD_CONTROL },
306 static const struct {
310 } special_key_list[] = {
311 { FALSE, "Down]", SKEY_DOWN },
312 { FALSE, "Left]", SKEY_LEFT },
313 { FALSE, "Right]", SKEY_RIGHT },
314 { FALSE, "Up]", SKEY_UP },
315 { FALSE, "Page_Up]", SKEY_PGUP },
316 { FALSE, "Page_Down]", SKEY_PGDOWN },
317 { FALSE, "Home]", SKEY_TOP },
318 { FALSE, "End]", SKEY_BOTTOM },
319 { TRUE, "KP_Down]", SKEY_DOWN },
320 { TRUE, "KP_Left]", SKEY_LEFT },
321 { TRUE, "KP_Right]", SKEY_RIGHT },
322 { TRUE, "KP_Up]", SKEY_UP },
323 { TRUE, "KP_Page_Up]", SKEY_PGUP },
324 { TRUE, "KP_Page_Down]", SKEY_PGDOWN },
325 { TRUE, "KP_Home]", SKEY_TOP },
326 { TRUE, "KP_End]", SKEY_BOTTOM },
327 { TRUE, "KP_2]", SKEY_DOWN },
328 { TRUE, "KP_4]", SKEY_LEFT },
329 { TRUE, "KP_6]", SKEY_RIGHT },
330 { TRUE, "KP_8]", SKEY_UP },
331 { TRUE, "KP_9]", SKEY_PGUP },
332 { TRUE, "KP_3]", SKEY_PGDOWN },
333 { TRUE, "KP_7]", SKEY_TOP },
334 { TRUE, "KP_1]", SKEY_BOTTOM },
338 static const struct {
341 } gcu_special_key_list[] = {
347 { "4~", SKEY_BOTTOM },
349 { "6~", SKEY_PGDOWN },
362 * Forget macro trigger ----
363 * It's important if we are already expanding macro action
365 inkey_macro_trigger_string[0] = '\0';
368 trig_len = strlen(inkey_macro_trigger_string);
370 return (int)((unsigned char)key);
371 if (trig_len == 1 && parse_macro) {
372 char c = inkey_macro_trigger_string[0];
373 forget_macro_action();
374 return (int)((unsigned char)c);
377 ascii_to_text(buf, inkey_macro_trigger_string);
378 if (prefix(str, "\\[")) {
381 for (i = 0; modifier_key_list[i].keyname; i++) {
382 if (prefix(str, modifier_key_list[i].keyname)) {
383 str += strlen(modifier_key_list[i].keyname);
384 modifier |= modifier_key_list[i].keyflag;
388 if (!modifier_key_list[i].keyname)
392 if (!numpad_as_cursorkey)
393 numpad_cursor = FALSE;
395 for (i = 0; special_key_list[i].keyname; i++) {
396 if ((!special_key_list[i].numpad || numpad_cursor) && streq(str, special_key_list[i].keyname)) {
397 skey = special_key_list[i].keycode;
403 forget_macro_action();
404 return (skey | modifier);
408 if (prefix(str, "\\e[")) {
411 for (i = 0; gcu_special_key_list[i].keyname; i++) {
412 if (streq(str, gcu_special_key_list[i].keyname)) {
413 return gcu_special_key_list[i].keycode;
418 inkey_macro_trigger_string[0] = '\0';
419 return (int)((unsigned char)key);
423 * @brief 全てのウィンドウの描画を止める
426 void stop_term_fresh(void)
428 for (int j = 0; j < 8; j++) {
430 angband_term[j]->never_fresh = TRUE;
435 * @brief 全てのウィンドウの描画を再開する
438 void start_term_fresh(void)
440 for (int j = 0; j < 8; j++) {
442 angband_term[j]->never_fresh = FALSE;
447 * @brief マクロ実行中かの判定関数
448 * @return 実行中であればtrue
450 bool macro_running(void)
452 /* マクロ展開中のみ詳細に判定する */
454 int diff = angband_term[0]->key_head - angband_term[0]->key_tail;
456 /* 最終入力を展開した直後はdiff==1となる */
460 /* 最終入力の処理中はまだtrueを返す */
461 if (inkey_next && *inkey_next && !inkey_xtra)