Merge 15329:15546 from trunk
[reactos.git] / rosapps / mc / slang / slgetkey.c
1 /* Copyright (c) 1992, 1995 John E. Davis
2 * All rights reserved.
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Perl Artistic License.
6 */
7
8 #include "config.h"
9
10 #include <stdio.h>
11 #include "slang.h"
12 #include "_slang.h"
13
14 unsigned int SLang_Input_Buffer_Len = 0;
15 unsigned char SLang_Input_Buffer [MAX_INPUT_BUFFER_LEN];
16
17 int SLang_Abort_Char = 7;
18 int SLang_Ignore_User_Abort = 0;
19
20 /* This has the effect of mapping all characters in the range 128-169 to
21 * ESC [ something
22 */
23 #ifndef __GO32__
24 # if defined(__unix__) || defined(vms)
25 # define DEC_8BIT_HACK 64
26 # endif
27 #endif
28
29 #ifdef OS2_NT /* see the replacement in src/slint.c */
30 unsigned int SLang_getkey (void)
31 {
32 unsigned int imax;
33 unsigned int ch;
34
35 if (SLang_Input_Buffer_Len)
36 {
37 ch = (unsigned int) *SLang_Input_Buffer;
38 SLang_Input_Buffer_Len--;
39 imax = SLang_Input_Buffer_Len;
40
41 SLMEMCPY ((char *) SLang_Input_Buffer,
42 (char *) (SLang_Input_Buffer + 1), imax);
43 }
44 else if (0xFFFF == (ch = SLsys_getkey ())) return ch;
45
46 #ifdef DEC_8BIT_HACK
47 if (ch & 0x80)
48 {
49 unsigned char i;
50 i = (unsigned char) (ch & 0x7F);
51 if (i < ' ')
52 {
53 i += DEC_8BIT_HACK;
54 SLang_ungetkey (i);
55 ch = 27;
56 }
57 }
58 #endif
59 return(ch);
60 }
61 #endif /* OS2_NT */
62
63 void SLang_ungetkey_string (unsigned char *s, unsigned int n)
64 {
65 register unsigned char *bmax, *b, *b1;
66 if (SLang_Input_Buffer_Len + n + 3 > MAX_INPUT_BUFFER_LEN) return;
67
68 b = SLang_Input_Buffer;
69 bmax = (b - 1) + SLang_Input_Buffer_Len;
70 b1 = bmax + n;
71 while (bmax >= b) *b1-- = *bmax--;
72 bmax = b + n;
73 while (b < bmax) *b++ = *s++;
74 SLang_Input_Buffer_Len += n;
75 }
76
77 void SLang_buffer_keystring (unsigned char *s, unsigned int n)
78 {
79
80 if (n + SLang_Input_Buffer_Len + 3 > MAX_INPUT_BUFFER_LEN) return;
81
82 SLMEMCPY ((char *) SLang_Input_Buffer + SLang_Input_Buffer_Len,
83 (char *) s, n);
84 SLang_Input_Buffer_Len += n;
85 }
86
87 void SLang_ungetkey (unsigned char ch)
88 {
89 SLang_ungetkey_string(&ch, 1);
90 }
91
92 #ifdef OS2_NT /* see the replacement in src/slint.c */
93 int SLang_input_pending (int tsecs)
94 {
95 int n;
96 unsigned char c;
97 if (SLang_Input_Buffer_Len) return (int) SLang_Input_Buffer_Len;
98
99 n = SLsys_input_pending (tsecs);
100
101 if (n <= 0) return 0;
102
103 c = (unsigned char) SLang_getkey ();
104 SLang_ungetkey_string (&c, 1);
105
106 return n;
107 }
108 #endif /* OS2_NT */
109
110 void SLang_flush_input (void)
111 {
112 int quit = SLKeyBoard_Quit;
113
114 SLang_Input_Buffer_Len = 0;
115 SLKeyBoard_Quit = 0;
116 while (SLsys_input_pending (0) > 0)
117 {
118 (void) SLsys_getkey ();
119 /* Set this to 0 because SLsys_getkey may stuff keyboard buffer if
120 * key sends key sequence (OS/2, DOS, maybe VMS).
121 */
122 SLang_Input_Buffer_Len = 0;
123 }
124 SLKeyBoard_Quit = quit;
125 }