Just a fuzzy trim...
[midnight-commander.git] / slang / slgetkey.c
blob9345300e88f7628fb15d1c1ec3e9464ecf0ebfb8
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 */
8 #include "config.h"
10 #include <stdio.h>
11 #include "_slang.h"
13 unsigned int SLang_Input_Buffer_Len = 0;
14 unsigned char SLang_Input_Buffer [MAX_INPUT_BUFFER_LEN];
16 int SLang_Abort_Char = 7;
17 int SLang_Ignore_User_Abort = 0;
19 /* This has the effect of mapping all characters in the range 128-169 to
20 * ESC [ something
22 #ifndef __GO32__
23 # if defined(__unix__) || defined(vms)
24 # define DEC_8BIT_HACK 64
25 # endif
26 #endif
28 #ifdef OS2_NT /* see the replacement in src/slint.c */
29 unsigned int SLang_getkey (void)
31 unsigned int imax;
32 unsigned int ch;
34 if (SLang_Input_Buffer_Len)
36 ch = (unsigned int) *SLang_Input_Buffer;
37 SLang_Input_Buffer_Len--;
38 imax = SLang_Input_Buffer_Len;
40 SLMEMCPY ((char *) SLang_Input_Buffer,
41 (char *) (SLang_Input_Buffer + 1), imax);
43 else if (0xFFFF == (ch = SLsys_getkey ())) return ch;
45 #ifdef DEC_8BIT_HACK
46 if (ch & 0x80)
48 unsigned char i;
49 i = (unsigned char) (ch & 0x7F);
50 if (i < ' ')
52 i += DEC_8BIT_HACK;
53 SLang_ungetkey (i);
54 ch = 27;
57 #endif
58 return(ch);
60 #endif /* OS2_NT */
62 void SLang_ungetkey_string (unsigned char *s, unsigned int n)
64 register unsigned char *bmax, *b, *b1;
65 if (SLang_Input_Buffer_Len + n + 3 > MAX_INPUT_BUFFER_LEN) return;
67 b = SLang_Input_Buffer;
68 bmax = (b - 1) + SLang_Input_Buffer_Len;
69 b1 = bmax + n;
70 while (bmax >= b) *b1-- = *bmax--;
71 bmax = b + n;
72 while (b < bmax) *b++ = *s++;
73 SLang_Input_Buffer_Len += n;
76 void SLang_buffer_keystring (unsigned char *s, unsigned int n)
79 if (n + SLang_Input_Buffer_Len + 3 > MAX_INPUT_BUFFER_LEN) return;
81 SLMEMCPY ((char *) SLang_Input_Buffer + SLang_Input_Buffer_Len,
82 (char *) s, n);
83 SLang_Input_Buffer_Len += n;
86 void SLang_ungetkey (unsigned char ch)
88 SLang_ungetkey_string(&ch, 1);
91 #ifdef OS2_NT /* see the replacement in src/slint.c */
92 int SLang_input_pending (int tsecs)
94 int n;
95 unsigned char c;
96 if (SLang_Input_Buffer_Len) return (int) SLang_Input_Buffer_Len;
98 n = SLsys_input_pending (tsecs);
100 if (n <= 0) return 0;
102 c = (unsigned char) SLang_getkey ();
103 SLang_ungetkey_string (&c, 1);
105 return n;
107 #endif /* OS2_NT */
109 void SLang_flush_input (void)
111 int quit = SLKeyBoard_Quit;
113 SLang_Input_Buffer_Len = 0;
114 SLKeyBoard_Quit = 0;
115 while (SLsys_input_pending (0) > 0)
117 (void) SLsys_getkey ();
118 /* Set this to 0 because SLsys_getkey may stuff keyboard buffer if
119 * key sends key sequence (OS/2, DOS, maybe VMS).
121 SLang_Input_Buffer_Len = 0;
123 SLKeyBoard_Quit = quit;