1 /* Copyright (c) 1992, 1995 John E. Davis
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Perl Artistic License.
25 /*----------------------------------------------------------------------*\
26 * Function: static void set_ctrl_break (int state);
28 * set the control-break setting
29 \*----------------------------------------------------------------------*/
30 static void set_ctrl_break (int state
)
35 /*----------------------------------------------------------------------*\
36 * Function: int SLang_init_tty (int abort_char, int no_flow_control,
39 * initialize the keyboard interface and attempt to set-up the interrupt 9
40 * handler if ABORT_CHAR is non-zero.
41 * NO_FLOW_CONTROL and OPOST are only for compatiblity and are ignored.
42 \*----------------------------------------------------------------------*/
44 HANDLE hStdout
, hStdin
;
45 CONSOLE_SCREEN_BUFFER_INFO csbiInfo
;
47 int SLang_init_tty (int abort_char
, int no_flow_control
, int opost
)
49 SMALL_RECT windowRect
;
53 #ifndef SLANG_SAVES_CONSOLE
54 /* first off, create a new console so the old one can be restored on exit */
55 HANDLE console
= CreateConsoleScreenBuffer(GENERIC_READ
| GENERIC_WRITE
,
56 FILE_SHARE_READ
|FILE_SHARE_WRITE
,
58 CONSOLE_TEXTMODE_BUFFER
,
60 if (SetConsoleActiveScreenBuffer(console
) == FALSE
) {
65 /* start things off at the origin */
69 /* still read in characters from stdin, but output to the new console */
70 /* this way, on program exit, the original screen is restored */
71 hStdin
= GetStdHandle(STD_INPUT_HANDLE
);
72 /* hStdin = console; */
74 #ifndef SLANG_SAVES_CONSOLE
75 hStdout
= GetStdHandle(STD_OUTPUT_HANDLE
);
80 if (hStdin
== INVALID_HANDLE_VALUE
|| hStdout
== INVALID_HANDLE_VALUE
) {
81 return -1; /* failure */
84 if (!GetConsoleScreenBufferInfo(hStdout
, &csbiInfo
)) {
85 return -1; /* failure */
90 windowRect
.Right
= csbiInfo
.dwSize
.X
- 1;
91 windowRect
.Bottom
= csbiInfo
.dwSize
.Y
- 1;
92 if (!SetConsoleWindowInfo(hStdout
, TRUE
, &windowRect
)) {
96 if (SetConsoleMode(hStdin
, 0) == FALSE
) {
97 return -1; /* failure */
100 if (SetConsoleMode(hStdout
, 0) == FALSE
) {
101 return -1; /* failure */
104 if (GetConsoleMode(hStdin
, &flags
)) {
105 if (flags
& ENABLE_PROCESSED_INPUT
) {
110 (void) SetConsoleCursorPosition(hStdout
, newPosition
);
114 } /* SLang_init_tty */
116 /*----------------------------------------------------------------------*\
117 * Function: void SLang_reset_tty (void);
119 * reset the tty before exiting
120 \*----------------------------------------------------------------------*/
121 void SLang_reset_tty (void)
126 /*----------------------------------------------------------------------*\
127 * Function: int SLsys_input_pending (int tsecs);
129 * sleep for *tsecs tenths of a sec waiting for input
130 \*----------------------------------------------------------------------*/
131 int SLsys_input_pending (int tsecs
)
139 if (PeekConsoleInput(hStdin
, &record
, 1, &bytesRead
))
143 if ((record
.EventType
== KEY_EVENT
)
144 && record
.Event
.KeyEvent
.bKeyDown
)
146 /* ok, there is a keypress here */
151 /* something else is here, so read it and try again */
152 (void) ReadConsoleInput(hStdin
, &record
, 1, &bytesRead
);
157 /* no Pending events */
163 /* function failed */
171 int count = tsecs * 5;
173 if (keyWaiting()) return 1;
176 delay (20); 20 ms or 1/50 sec
177 if (keyWaiting()) break;
185 /*----------------------------------------------------------------------*\
186 * Function: unsigned int SLsys_getkey (void);
188 * wait for and get the next available keystroke.
189 * Also re-maps some useful keystrokes.
191 * Backspace (^H) => Del (127)
192 * Ctrl-Space => ^@ (^@^3 - a pc NUL char)
193 * extended keys are prefixed by a null character
194 \*----------------------------------------------------------------------*/
195 unsigned int SLsys_getkey (void)
197 unsigned int scan
, ch
, shift
;
202 if (!ReadConsoleInput(hStdin
, &record
, 1, &bytesRead
)) {
205 if (record
.EventType
== KEY_EVENT
&& record
.Event
.KeyEvent
.bKeyDown
) {
207 return record
.Event
.KeyEvent
.uChar
.AsciiChar
;
209 return record
.Event
.KeyEvent
.AsciiChar
;
213 /* ReadFile(hStdin, &key, 1, &bytesRead, NULL); */
218 /*----------------------------------------------------------------------*\
219 * Function: void SLang_set_abort_signal (void (*handler)(int));
220 \*----------------------------------------------------------------------*/
221 void SLang_set_abort_signal (void (*handler
)(int))