summaryrefslogtreecommitdiffstats
path: root/mdk-stage1/slang/slgetkey.c
blob: 2f2914f07776bf5ecbc09c9b786e48ba78d6cf89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* Copyright (c) 1992, 1999, 2001 John E. Davis
 * This file is part of the S-Lang library.
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Perl Artistic License.
 */

#include "slinclud.h"

#include "slang.h"
#include "_slang.h"

unsigned int SLang_Input_Buffer_Len = 0;
unsigned char SLang_Input_Buffer [SL_MAX_INPUT_BUFFER_LEN];

int SLang_Abort_Char = 7;
int SLang_Ignore_User_Abort = 0;

/* This has the effect of mapping all characters in the range 128-169 to
 * ESC [ something
 */

unsigned int SLang_getkey (void)
{
   unsigned int imax;
   unsigned int ch;

   if (SLang_Input_Buffer_Len)
     {
	ch = (unsigned int) *SLang_Input_Buffer;
	SLang_Input_Buffer_Len--;
	imax = SLang_Input_Buffer_Len;

	SLMEMCPY ((char *) SLang_Input_Buffer,
		(char *) (SLang_Input_Buffer + 1), imax);
     }
   else if (SLANG_GETKEY_ERROR == (ch = _SLsys_getkey ())) return ch;

#if _SLANG_MAP_VTXXX_8BIT
# if !defined(IBMPC_SYSTEM)
   if (ch & 0x80)
     {
	unsigned char i;
	i = (unsigned char) (ch & 0x7F);
	if (i < ' ')
	  {
	     i += 64;
	     SLang_ungetkey (i);
	     ch = 27;
	  }
     }
# endif
#endif
   return(ch);
}

int SLang_ungetkey_string (unsigned char *s, unsigned int n)
{
   register unsigned char *bmax, *b, *b1;
   if (SLang_Input_Buffer_Len + n + 3 > SL_MAX_INPUT_BUFFER_LEN)
     return -1;

   b = SLang_Input_Buffer;
   bmax = (b - 1) + SLang_Input_Buffer_Len;
   b1 = bmax + n;
   while (bmax >= b) *b1-- = *bmax--;
   bmax = b + n;
   while (b < bmax) *b++ = *s++;
   SLang_Input_Buffer_Len += n;
   return 0;
}

int SLang_buffer_keystring (unsigned char *s, unsigned int n)
{

   if (n + SLang_Input_Buffer_Len + 3 > SL_MAX_INPUT_BUFFER_LEN) return -1;

   SLMEMCPY ((char *) SLang_Input_Buffer + SLang_Input_Buffer_Len,
	   (char *) s, n);
   SLang_Input_Buffer_Len += n;
   return 0;
}

int SLang_ungetkey (unsigned char ch)
{
   return SLang_ungetkey_string(&ch, 1);
}

int SLang_input_pending (int tsecs)
{
   int n;
   unsigned char c;
   if (SLang_Input_Buffer_Len) return (int) SLang_Input_Buffer_Len;

   n = _SLsys_input_pending (tsecs);

   if (n <= 0) return 0;

   c = (unsigned char) SLang_getkey ();
   SLang_ungetkey_string (&c, 1);

   return n;
}

void SLang_flush_input (void)
{
   int quit = SLKeyBoard_Quit;

   SLang_Input_Buffer_Len = 0;
   SLKeyBoard_Quit = 0;
   while (_SLsys_input_pending (0) > 0)
     {
	(void) _SLsys_getkey ();
	/* Set this to 0 because _SLsys_getkey may stuff keyboard buffer if
	 * key sends key sequence (OS/2, DOS, maybe VMS).
	 */
	SLang_Input_Buffer_Len = 0;
     }
   SLKeyBoard_Quit = quit;
}

#ifdef IBMPC_SYSTEM
static int Map_To_ANSI;
int SLgetkey_map_to_ansi (int enable)
{
   Map_To_ANSI = enable;
   return 0;
}

static int convert_scancode (unsigned int scan, 
			     unsigned int shift,
			     int getkey,
			     unsigned int *ret_key)
{
   unsigned char buf[16];
   unsigned char *b;
   unsigned char end;
   int is_arrow;

   shift &= (_SLTT_KEY_ALT|_SLTT_KEY_SHIFT|_SLTT_KEY_CTRL);

   b = buf;
   if (_SLTT_KEY_ALT == shift)
     {
	shift = 0;
	*b++ = 27;
     }
   *b++ = 27;
   *b++ = '[';

   is_arrow = 0;
   end = '~';
   if (shift)
     {
	if (shift == _SLTT_KEY_CTRL)
	  end = '^';
	else if (shift == _SLTT_KEY_SHIFT)
	  end = '$';
	else shift = 0;
     }

   /* These mappings correspond to what rxvt produces under Linux */
   switch (scan & 0xFF)
     {
      default:
	return -1;

      case 0x47:		       /* home */
	*b++ = '1';
	break;
      case 0x48:		       /* up */
	end = 'A';
	is_arrow = 1;
	break;
      case 0x49:		       /* PgUp */
	*b++ = '5';
	break;
      case 0x4B:		       /* Left */
	end = 'D';
	is_arrow = 1;
	break;
      case 0x4D:		       /* Right */
	end = 'C';
	is_arrow = 1;
	break;
      case 0x4F:		       /* End */
	*b++ = '4';
	break;
      case 0x50:		       /* Down */
	end = 'B';
	is_arrow = 1;
	break;
      case 0x51:		       /* PgDn */
	*b++ = '6';
	break;
      case 0x52:		       /* Insert */
	*b++ = '2';
	break;
      case 0x53:		       /* Delete */
	*b++ = '3';
	break;
      case ';':			       /* F1 */
	*b++ = '1';
	*b++ = '1';
	break;
      case '<':			       /* F2 */
	*b++ = '1';
	*b++ = '2';
	break;
      case '=':			       /* F3 */
	*b++ = '1';
	*b++ = '3';
	break;

      case '>':			       /* F4 */
	*b++ = '1';
	*b++ = '4';
	break;

      case '?':			       /* F5 */
	*b++ = '1';
	*b++ = '5';
	break;

      case '@':			       /* F6 */
	*b++ = '1';
	*b++ = '7';
	break;

      case 'A':			       /* F7 */
	*b++ = '1';
	*b++ = '8';
	break;

      case 'B':			       /* F8 */
	*b++ = '1';
	*b++ = '9';
	break;

      case 'C':			       /* F9 */
	*b++ = '2';
	*b++ = '0';
	break;

      case 'D':			       /* F10 */
	*b++ = '2';
	*b++ = '1';
	break;

      case 0x57:		       /* F11 */
	*b++ = '2';
	*b++ = '3';
	break;

      case 0x58:		       /* F12 */
	*b++ = '2';
	*b++ = '4';
	break;
     }

   if (is_arrow && shift)
     {
	if (shift == _SLTT_KEY_CTRL)
	  end &= 0x1F;
	else
	  end |= 0x20;
     }
   *b++ = end;
   
   if (getkey)
     {
	(void) SLang_buffer_keystring (buf + 1, (unsigned int) (b - (buf + 1)));
	*ret_key = buf[0];
	return 0;
     }

   (void) SLang_buffer_keystring (buf, (unsigned int) (b - buf));
   return 0;
}

   
unsigned int _SLpc_convert_scancode (unsigned int scan,
				     unsigned int shift,
				     int getkey)
{
   unsigned char buf[16];

   if (Map_To_ANSI)
     {
	if (0 == convert_scancode (scan, shift, getkey, &scan))
	  return scan;
     }
   
   if (getkey)
     {
	buf[0] = scan & 0xFF;
	SLang_buffer_keystring (buf, 1);
	return (scan >> 8) & 0xFF;
     }
   buf[0] = (scan >> 8) & 0xFF;
   buf[1] = scan & 0xFF;
   (void) SLang_buffer_keystring (buf, 2);
   return 0;
}

#endif