3030#include "lib/util.h"
3131
3232#include "lib/tty/tty.h"
33+ #include "lib/tty/tty-internal.h"
34+ #include "lib/tty/key.h"
35+
36+ static int mock_input_buf [1000 ];
37+ static int * mock_input_ptr ;
38+
39+ static void
40+ mock_input (const char * charinput )
41+ {
42+ mock_input_ptr = mock_input_buf ;
43+
44+ while (* charinput != '\0' )
45+ * mock_input_ptr ++ = (* charinput ++ & 0xFF );
46+ * mock_input_ptr = '\0' ;
47+
48+ mock_input_ptr = mock_input_buf ;
49+ }
50+
51+ /* @Mock */
52+ int
53+ tty_lowlevel_getch (void )
54+ {
55+ int key = * mock_input_ptr ;
56+ if (key != '\0' )
57+ {
58+ mock_input_ptr ++ ;
59+ return key ;
60+ }
61+ else
62+ return -1 ;
63+ }
64+
65+ /* @Mock */
66+ int
67+ getch_with_timeout (unsigned int delay_us )
68+ {
69+ (void ) (delay_us );
70+
71+ return tty_lowlevel_getch ();
72+ }
3373
3474/* --------------------------------------------------------------------------------------------- */
3575/* @CapturedValue */
@@ -74,6 +114,7 @@ START_TEST (test_tty_check_term_non_xterm)
74114 ck_assert_int_eq (actual_result_force_true , 1 );
75115}
76116END_TEST
117+
77118/* --------------------------------------------------------------------------------------------- */
78119
79120START_TEST (test_tty_check_term_xterm_like )
@@ -94,6 +135,54 @@ END_TEST
94135
95136/* --------------------------------------------------------------------------------------------- */
96137
138+ START_TEST (test_tty_get_key_code )
139+ {
140+ mc_global .tty .xterm_flag = TRUE;
141+
142+ setenv ("TERM" , "xterm" , 1 );
143+ init_key ();
144+ #ifdef HAVE_SLANG
145+ SLtt_get_terminfo ();
146+ load_terminfo_keys ();
147+ #endif
148+
149+ mock_input ("\x1b[1;2A" );
150+ ck_assert_int_eq (get_key_code (0 ), KEY_M_SHIFT | KEY_UP );
151+ ck_assert_int_eq (get_key_code (0 ), -1 );
152+ ck_assert_int_eq (get_key_code (0 ), -1 );
153+
154+ mock_input ("😊FG" );
155+ ck_assert_int_eq (get_key_code (0 ), 0xF0 );
156+ ck_assert_int_eq (get_key_code (0 ), 0x9F );
157+ ck_assert_int_eq (get_key_code (0 ), 0x98 );
158+ ck_assert_int_eq (get_key_code (0 ), 0x8A );
159+ ck_assert_int_eq (get_key_code (0 ), 'F' );
160+ ck_assert_int_eq (get_key_code (0 ), 'G' );
161+ ck_assert_int_eq (get_key_code (0 ), -1 );
162+ ck_assert_int_eq (get_key_code (0 ), -1 );
163+
164+ mock_input ("ц\x1b[1;2A=5ů§a" );
165+ ck_assert_int_eq (get_key_code (0 ), 0xD1 ); // 'ц'
166+ ck_assert_int_eq (get_key_code (0 ), 0x86 );
167+ ck_assert_int_eq (get_key_code (0 ), KEY_M_SHIFT | KEY_UP );
168+ ck_assert_int_eq (get_key_code (0 ), '=' );
169+ ck_assert_int_eq (get_key_code (0 ), '5' );
170+ ck_assert_int_eq (get_key_code (0 ), 0xC5 ); // 'ů'
171+ ck_assert_int_eq (get_key_code (0 ), 0xAF );
172+ ck_assert_int_eq (get_key_code (0 ), 0xC2 ); // '§'
173+ ck_assert_int_eq (get_key_code (0 ), 0xA7 );
174+ ck_assert_int_eq (get_key_code (0 ), 'a' );
175+ ck_assert_int_eq (get_key_code (0 ), -1 );
176+ ck_assert_int_eq (get_key_code (0 ), -1 );
177+
178+ mock_input ("\x1b[u" );
179+ ck_assert_int_eq (get_key_code (0 ), -1 );
180+ ck_assert_int_eq (get_key_code (0 ), -1 );
181+ }
182+ END_TEST
183+
184+ /* --------------------------------------------------------------------------------------------- */
185+
97186int
98187main (void )
99188{
@@ -105,6 +194,7 @@ main (void)
105194 tcase_add_test (tc_core , test_tty_check_term_unset );
106195 tcase_add_test (tc_core , test_tty_check_term_non_xterm );
107196 tcase_add_test (tc_core , test_tty_check_term_xterm_like );
197+ tcase_add_test (tc_core , test_tty_get_key_code );
108198 // ***********************************
109199
110200 return mctest_run_all (tc_core );
0 commit comments