File: slw32tty.c

package info (click to toggle)
mc 4.1.35-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 6,924 kB
  • ctags: 9,665
  • sloc: ansic: 84,273; tcl: 1,779; makefile: 1,266; sh: 864; perl: 262; awk: 148; sed: 93; csh: 1
file content (224 lines) | stat: -rw-r--r-- 5,546 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
/* Copyright (c) 1992, 1995 John E. Davis
 * All rights reserved.
 * 
 * You may distribute under the terms of either the GNU General Public
 * License or the Perl Artistic License.
 */

#include "config.h"
#include <stdio.h>

#include <windows.h>
#include <winbase.h>

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

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



/*----------------------------------------------------------------------*\
 *  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 hStdout, hStdin;
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

int SLang_init_tty (int abort_char, int no_flow_control, int opost)
{
  SMALL_RECT windowRect;
  COORD newPosition;
  long flags;

#ifndef SLANG_SAVES_CONSOLE
  /* first off, create a new console so the old one can be restored on exit */
  HANDLE console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
					     FILE_SHARE_READ |FILE_SHARE_WRITE,
					     0,
					     CONSOLE_TEXTMODE_BUFFER,
					     0);
  if (SetConsoleActiveScreenBuffer(console) == FALSE) {
    return -1;
  }
#endif

  /* start things off at the origin */
  newPosition.X = 0;
  newPosition.Y = 0;

  /* still read in characters from stdin, but output to the new console */
  /* this way, on program exit, the original screen is restored */
  hStdin = GetStdHandle(STD_INPUT_HANDLE);
/*   hStdin = console; */

#ifndef SLANG_SAVES_CONSOLE
  hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
#else
  hStdout = console;
#endif

  if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE) {
    return -1; /* failure */
  }

  if (!GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) {
    return -1; /* failure */
  } // if

  windowRect.Left = 0;
  windowRect.Top = 0;
  windowRect.Right = csbiInfo.dwSize.X - 1;
  windowRect.Bottom = csbiInfo.dwSize.Y - 1;
  if (!SetConsoleWindowInfo(hStdout, TRUE, &windowRect)) {
    return -1;
  }

  if (SetConsoleMode(hStdin, 0) == FALSE) {
    return -1; /* failure */
  }

  if (SetConsoleMode(hStdout, 0) == FALSE) {
    return -1; /* failure */
  }

  if (GetConsoleMode(hStdin, &flags)) {
    if (flags & ENABLE_PROCESSED_INPUT) {
      return -1;
    }
  } 

  (void) SetConsoleCursorPosition(hStdout, newPosition);  

  /* success */
  return 0;
} /* SLang_init_tty */

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

/*----------------------------------------------------------------------*\
 *  Function:	int SLsys_input_pending (int tsecs);
 *
 *  sleep for *tsecs tenths of a sec waiting for input
\*----------------------------------------------------------------------*/
int SLsys_input_pending (int tsecs)
{
   INPUT_RECORD record;
   long one = 1;
   long bytesRead;

   while (1)
     {
	if (PeekConsoleInput(hStdin, &record, 1, &bytesRead)) 
	  {
	     if (bytesRead == 1) 
	       {
		  if ((record.EventType == KEY_EVENT)
		      && record.Event.KeyEvent.bKeyDown) 
		    {
		       /* ok, there is a keypress here */
		       return 1;
		    } 
		  else 
		    {
		       /* something else is here, so read it and try again */
		       (void) ReadConsoleInput(hStdin, &record, 1, &bytesRead);
		    }
	       }
	     else
	       {
		  /* no Pending events */
		  return 0;
	       }
	  }
	else
	  {
	     /* function failed */
	     return 0;
	  }
     }
#if 0
  /* no delays yet */
  /* use Sleep */
  /*
   int count = tsecs * 5;

   if (keyWaiting()) return 1;
   while (count > 0)
     {
	delay (20);	 20 ms or 1/50 sec 
	if (keyWaiting()) break;
	count--;
     }
   return (count);
   */
#endif
}

/*----------------------------------------------------------------------*\
 *  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)
{
  unsigned int scan, ch, shift;
  long key, bytesRead;
  INPUT_RECORD record;

  while (1) {
    if (!ReadConsoleInput(hStdin, &record, 1, &bytesRead)) {
      return 0;
    }
    if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown) {
#ifndef __MINGW32__
      return record.Event.KeyEvent.uChar.AsciiChar;
#else
      return record.Event.KeyEvent.AsciiChar;
#endif
    }
  }
/*   ReadFile(hStdin, &key, 1, &bytesRead, NULL); */

/*   return key; */
}

/*----------------------------------------------------------------------*\
 *  Function:	void SLang_set_abort_signal (void (*handler)(int));
\*----------------------------------------------------------------------*/
void SLang_set_abort_signal (void (*handler)(int))
{

}