2 Copyright (C) 2004, 2005, 2006 John E. Davis
4 This file is part of the S-Lang Library.
6 The S-Lang Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The S-Lang Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 unsigned int SLang_Input_Buffer_Len
= 0;
28 unsigned char SLang_Input_Buffer
[SL_MAX_INPUT_BUFFER_LEN
];
30 int SLang_Abort_Char
= 7;
31 int SLang_Ignore_User_Abort
= 0;
33 /* This has the effect of mapping all characters in the range 128-169 to
37 unsigned int SLang_getkey (void)
42 if (SLang_Input_Buffer_Len
)
44 ch
= (unsigned int) *SLang_Input_Buffer
;
45 SLang_Input_Buffer_Len
--;
46 imax
= SLang_Input_Buffer_Len
;
48 SLMEMCPY ((char *) SLang_Input_Buffer
,
49 (char *) (SLang_Input_Buffer
+ 1), imax
);
51 else if (SLANG_GETKEY_ERROR
== (ch
= _pSLsys_getkey ())) return ch
;
53 #if SLANG_MAP_VTXXX_8BIT
54 # if !defined(IBMPC_SYSTEM)
58 i
= (unsigned char) (ch
& 0x7F);
71 int SLang_ungetkey_string (unsigned char *s
, unsigned int n
)
73 register unsigned char *bmax
, *b
, *b1
;
74 if (SLang_Input_Buffer_Len
+ n
+ 3 > SL_MAX_INPUT_BUFFER_LEN
)
77 b
= SLang_Input_Buffer
;
78 bmax
= (b
- 1) + SLang_Input_Buffer_Len
;
80 while (bmax
>= b
) *b1
-- = *bmax
--;
82 while (b
< bmax
) *b
++ = *s
++;
83 SLang_Input_Buffer_Len
+= n
;
87 int SLang_buffer_keystring (unsigned char *s
, unsigned int n
)
90 if (n
+ SLang_Input_Buffer_Len
+ 3 > SL_MAX_INPUT_BUFFER_LEN
) return -1;
92 SLMEMCPY ((char *) SLang_Input_Buffer
+ SLang_Input_Buffer_Len
,
94 SLang_Input_Buffer_Len
+= n
;
98 int SLang_ungetkey (unsigned char ch
)
100 return SLang_ungetkey_string(&ch
, 1);
103 int SLang_input_pending (int tsecs
)
107 if (SLang_Input_Buffer_Len
) return (int) SLang_Input_Buffer_Len
;
109 n
= _pSLsys_input_pending (tsecs
);
111 if (n
<= 0) return 0;
113 c
= (unsigned char) SLang_getkey ();
114 SLang_ungetkey_string (&c
, 1);
119 void SLang_flush_input (void)
121 int quit
= SLKeyBoard_Quit
;
123 SLang_Input_Buffer_Len
= 0;
125 while (_pSLsys_input_pending (0) > 0)
127 (void) _pSLsys_getkey ();
128 /* Set this to 0 because _pSLsys_getkey may stuff keyboard buffer if
129 * key sends key sequence (OS/2, DOS, maybe VMS).
131 SLang_Input_Buffer_Len
= 0;
133 SLKeyBoard_Quit
= quit
;
137 static int Map_To_ANSI
;
138 int SLgetkey_map_to_ansi (int enable
)
140 Map_To_ANSI
= enable
;
144 static int convert_scancode (unsigned int scan
,
147 unsigned int *ret_key
)
149 unsigned char buf
[16];
154 shift
&= (_pSLTT_KEY_ALT
|_pSLTT_KEY_SHIFT
|_pSLTT_KEY_CTRL
);
157 if (_pSLTT_KEY_ALT
== shift
)
169 if (shift
== _pSLTT_KEY_CTRL
)
171 else if (shift
== _pSLTT_KEY_SHIFT
)
176 /* These mappings correspond to what rxvt produces under Linux */
182 case 0x47: /* home */
189 case 0x49: /* PgUp */
192 case 0x4B: /* Left */
196 case 0x4D: /* Right */
203 case 0x50: /* Down */
207 case 0x51: /* PgDn */
210 case 0x52: /* Insert */
213 case 0x53: /* Delete */
275 if (is_arrow
&& shift
)
277 if (shift
== _pSLTT_KEY_CTRL
)
286 (void) SLang_buffer_keystring (buf
+ 1, (unsigned int) (b
- (buf
+ 1)));
291 (void) SLang_buffer_keystring (buf
, (unsigned int) (b
- buf
));
296 unsigned int _pSLpc_convert_scancode (unsigned int scan
,
300 unsigned char buf
[16];
304 if (0 == convert_scancode (scan
, shift
, getkey
, &scan
))
310 buf
[0] = scan
& 0xFF;
311 SLang_buffer_keystring (buf
, 1);
312 return (scan
>> 8) & 0xFF;
314 buf
[0] = (scan
>> 8) & 0xFF;
315 buf
[1] = scan
& 0xFF;
316 (void) SLang_buffer_keystring (buf
, 2);