File: slw32tty.c

package info (click to toggle)
mc 1:4.6.0-4.6.1-pre3-3sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 15,224 kB
  • ctags: 11,992
  • sloc: ansic: 93,654; sh: 5,102; perl: 1,961; makefile: 1,107; yacc: 316; cs: 43; sed: 16
file content (354 lines) | stat: -rw-r--r-- 9,257 bytes parent folder | download | duplicates (2)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* Copyright (c) 1992, 1999, 2001, 2002, 2003 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 <windows.h>
#include <winbase.h>

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

#ifdef __cplusplus
# define _DOTS_ ...
#else
# define _DOTS_ void
#endif

static int Process_Mouse_Events;

/*----------------------------------------------------------------------*\
 *  Function:	static void set_ctrl_break (int state);
 *
 * set the control-break setting
\*----------------------------------------------------------------------*/
static void set_ctrl_break (int state)
{
}

/*----------------------------------------------------------------------*\
 *  Function:	int SLang_init_tty (int abort_char, int no_flow_control,
 *				    int opost);
 *
 * initialize the keyboard interface and attempt to set-up the interrupt 9
 * handler if ABORT_CHAR is non-zero.
 * NO_FLOW_CONTROL and OPOST are only for compatiblity and are ignored.
\*----------------------------------------------------------------------*/

HANDLE _SLw32_Hstdin = INVALID_HANDLE_VALUE;

int SLang_init_tty (int abort_char, int no_flow_control, int opost)
{
   (void) opost;
   (void) no_flow_control;

   if (_SLw32_Hstdin != INVALID_HANDLE_VALUE)
     return 0;

#if 1
   /* stdin may have been redirected.  So try this */
   _SLw32_Hstdin = CreateFile ("CONIN$", GENERIC_READ|GENERIC_WRITE, 
			       FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, 
			       OPEN_EXISTING, 0, NULL);
   if (_SLw32_Hstdin == INVALID_HANDLE_VALUE)
     return -1;
#else
   if (INVALID_HANDLE_VALUE == (_SLw32_Hstdin = GetStdHandle(STD_INPUT_HANDLE)))
     return -1;
#endif

   if (FALSE == SetConsoleMode(_SLw32_Hstdin, ENABLE_WINDOW_INPUT|ENABLE_MOUSE_INPUT))
     {
	_SLw32_Hstdin = INVALID_HANDLE_VALUE;
	return -1;
     }
   
   if (abort_char > 0)
     SLang_Abort_Char = abort_char;

   return 0;
}
/* SLang_init_tty */

/*----------------------------------------------------------------------*\
 *  Function:	void SLang_reset_tty (void);
 *
 * reset the tty before exiting
\*----------------------------------------------------------------------*/
void SLang_reset_tty (void)
{
   _SLw32_Hstdin = INVALID_HANDLE_VALUE;
   set_ctrl_break (1);
}

static int process_mouse_event (MOUSE_EVENT_RECORD *m)
{
   char buf [8];

   if (Process_Mouse_Events == 0)
     return -1;

   if (m->dwEventFlags)
     return -1;			       /* double click or movement event */

   /* A button was either pressed or released.  Now make sure that
    * the shift keys were not also pressed.
    */
   if (m->dwControlKeyState
       & (LEFT_ALT_PRESSED|RIGHT_ALT_PRESSED
	  |LEFT_CTRL_PRESSED|RIGHT_CTRL_PRESSED
	  |SHIFT_PRESSED))
     return -1;

   /* We have a simple press or release.  Encode it as an escape sequence
    * and buffer the result.  The encoding is:
    *   'ESC [ M b x y'
    *  where b represents the button state, and x,y represent the coordinates.
    * The ESC is handled by the calling routine.
    */
   if (m->dwButtonState & 1) buf[3] = ' ';
   else if (m->dwButtonState & 2) buf[3] = ' ' + 2;
   else if (m->dwButtonState & 4) buf[3] = ' ' + 1;
   else return -1;

   buf[0] = 27;
   buf[1] = '[';
   buf[2] = 'M';


   buf[4] = 1 + ' ' + m->dwMousePosition.X;
   buf[5] = 1 + ' ' + m->dwMousePosition.Y;

   return SLang_buffer_keystring ((unsigned char *)buf, 6);
}

static int process_key_event (KEY_EVENT_RECORD *key)
{
   unsigned int key_state = 0;
   unsigned int scan;
   char c1;
   DWORD d = key->dwControlKeyState;
   unsigned char buf[4];

   if (!key->bKeyDown) return 0;
   if (d & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED))
     key_state |= _SLTT_KEY_ALT;
   if (d & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) 
     key_state |= _SLTT_KEY_CTRL;
   if (d & SHIFT_PRESSED)
     key_state |= _SLTT_KEY_SHIFT;

   scan = key->wVirtualScanCode;

   switch (scan)
     {
      case 0x00E:		       /* backspace */
	return SLang_buffer_keystring ((unsigned char *)"\x7F", 1);

      case 0x003:		       /* 2 key */
	if (key_state & _SLTT_KEY_ALT)
	  break;
	/* Drop */
      case 0x039: 		       /* space */
	if (key_state & _SLTT_KEY_CTRL)
	  return SLang_buffer_keystring ((unsigned char *)"\x00\x03", 2);
	break;

      case 0x007:		       /* 6 key */
	if (_SLTT_KEY_CTRL == (key_state & (_SLTT_KEY_ALT|_SLTT_KEY_CTRL)))
	  return SLang_buffer_keystring ((unsigned char *)"\x1E", 1);   /* Ctrl-^ */
	break;

      case 0x00C:		       /* -/_ key */
	if (_SLTT_KEY_CTRL == (key_state & (_SLTT_KEY_ALT|_SLTT_KEY_CTRL)))
	  return SLang_buffer_keystring ((unsigned char *)"\x1F", 1);
	break;

      case 0x00F:		       /* TAB */
	if (_SLTT_KEY_SHIFT == key_state)
	  return SLang_buffer_keystring ((unsigned char *)"\x00\x09", 2);
	break;

      case 0xE02F:		       /* KEYPAD SLASH */
      case 0x037:		       /* KEYPAD STAR */
      case 0x04A:		       /* KEYPAD MINUS */
      case 0x04E:		       /* KEYPAD PLUS */
	if (d & NUMLOCK_ON)
	  break;
      case 0x047:		       /* KEYPAD HOME */
      case 0x048:		       /* KEYPAD UP */
      case 0x049:		       /* KEYPAD PGUP */
      case 0x04B:		       /* KEYPAD LEFT */
      case 0x04C:		       /* KEYPAD 5 */
      case 0x04D:		       /* KEYPAD RIGHT */
      case 0x04F:		       /* KEYPAD END */
      case 0x050:		       /* KEYPAD DOWN */
      case 0x051:		       /* KEYPAD PGDN */
      case 0x052:		       /* KEYPAD INSERT */
      case 0x053:		       /* KEYPAD DEL */
	if (d & ENHANCED_KEY)
	  scan |= 0xE000;
	else
	  {
	     if (d & NUMLOCK_ON)
	       break;
	  }
	(void) _SLpc_convert_scancode (scan, key_state, 0);
	return 0;

      case 0x3b:		       /* F1 */
      case 0x3c:
      case 0x3d:
      case 0x3e:
      case 0x3f:
      case 0x40:
      case 0x41:
      case 0x42:
      case 0x43:
      case 0x44:
      case 0x57:
      case 0x58:		       /* F12 */
	(void) _SLpc_convert_scancode (scan, key_state, 0);
     }
   
   c1 = key->uChar.AsciiChar;
   if (c1 != 0)
     {
	if (_SLTT_KEY_ALT == (key_state & (_SLTT_KEY_ALT|_SLTT_KEY_CTRL)))
	  {
	     buf[0] = 27;
	     buf[1] = c1;
	     return SLang_buffer_keystring (buf, 2);
	  }
	if (c1 == SLang_Abort_Char)
	  {
	     if (SLang_Ignore_User_Abort == 0) SLang_Error = USER_BREAK;
	     SLKeyBoard_Quit = 1;
	  }
	buf[0] = c1;
	return SLang_buffer_keystring (buf, 1);
     }
   return 0;
}


static void process_console_records(void)
{
   INPUT_RECORD record;
   DWORD bytesRead;
   DWORD n = 0;

   if (FALSE == GetNumberOfConsoleInputEvents(_SLw32_Hstdin, &n))
     return;

   while (n > 0)
     {
	ReadConsoleInput(_SLw32_Hstdin, &record, 1, &bytesRead);
	switch (record.EventType)
	  {
	   case KEY_EVENT:
	     (void) process_key_event(&record.Event.KeyEvent);
	     break;

	   case MOUSE_EVENT:
	     process_mouse_event(&record.Event.MouseEvent);
	     break;

	   case WINDOW_BUFFER_SIZE_EVENT:
	     /* process_resize_records(&record.Event.WindowBufferSizeEvent); */
	     break;
	  }
	n--;
     }
}

/*----------------------------------------------------------------------*\
 *  Function:	int _SLsys_input_pending (int tsecs);
 *
 *  sleep for *tsecs tenths of a sec waiting for input
\*----------------------------------------------------------------------*/
int _SLsys_input_pending (int tsecs)
{
   long ms;

   if (_SLw32_Hstdin == INVALID_HANDLE_VALUE)
     return -1;

   if (tsecs < 0) ms = -tsecs;	       /* specifies 1/1000 */
   else ms = tsecs * 100L;	       /* convert 1/10 to 1/1000 secs */

   process_console_records ();
   while ((ms > 0)
	  && (SLang_Input_Buffer_Len == 0))
     {
	long t;

	t = GetTickCount ();

	(void) WaitForSingleObject (_SLw32_Hstdin, ms);
	process_console_records ();
	ms -= GetTickCount () - t;
     }
   
   return SLang_Input_Buffer_Len;
}

/*----------------------------------------------------------------------*\
 *  Function:	unsigned int _SLsys_getkey (void);
 *
 * wait for and get the next available keystroke.
 * Also re-maps some useful keystrokes.
 *
 *	Backspace (^H)	=>	Del (127)
 *	Ctrl-Space	=>	^@	(^@^3 - a pc NUL char)
 *	extended keys are prefixed by a null character
\*----------------------------------------------------------------------*/
unsigned int _SLsys_getkey (void)
{
   /* Check the input buffer because _SLsys_input_pending may have been 
    * called prior to this to stuff the input buffer.
    */
   if (SLang_Input_Buffer_Len)
     return SLang_getkey ();

   if (_SLw32_Hstdin == INVALID_HANDLE_VALUE)
     return SLANG_GETKEY_ERROR;

   while (1)
     {
	int status;

	if (SLKeyBoard_Quit)
	  return SLang_Abort_Char;
	
	status = _SLsys_input_pending (600);
	if (status == -1)
	  return SLANG_GETKEY_ERROR;
	
	if (status > 0)
	  return SLang_getkey ();
     }
}

/*----------------------------------------------------------------------*\
 *  Function:	int SLang_set_abort_signal (void (*handler)(int));
\*----------------------------------------------------------------------*/
int SLang_set_abort_signal (void (*handler)(int))
{
   if (_SLw32_Hstdin == INVALID_HANDLE_VALUE)
     return -1;

   return 0;
}

int SLtt_set_mouse_mode (int mode, int force)
{
   (void) force;

   Process_Mouse_Events = mode;
   return 0;
}