fix panel_scroll_pages start page
[midnight-commander.git] / slang / slw32tty.c
blobdb109f09989ef5c0fba1809f1359a46f6757f7a5
1 /* Copyright (c) 1992, 1995 John E. Davis
2 * All rights reserved.
3 *
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Perl Artistic License.
6 */
8 #include "config.h"
9 #include <stdio.h>
11 #include <windows.h>
12 #include <winbase.h>
14 #include "_slang.h"
16 #ifdef __cplusplus
17 # define _DOTS_ ...
18 #else
19 # define _DOTS_ void
20 #endif
24 /*----------------------------------------------------------------------*\
25 * Function: static void set_ctrl_break (int state);
27 * set the control-break setting
28 \*----------------------------------------------------------------------*/
29 static void set_ctrl_break (int state)
34 /*----------------------------------------------------------------------*\
35 * Function: int SLang_init_tty (int abort_char, int no_flow_control,
36 * int opost);
38 * initialize the keyboard interface and attempt to set-up the interrupt 9
39 * handler if ABORT_CHAR is non-zero.
40 * NO_FLOW_CONTROL and OPOST are only for compatiblity and are ignored.
41 \*----------------------------------------------------------------------*/
43 HANDLE hStdout, hStdin;
44 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
46 int SLang_init_tty (int abort_char, int no_flow_control, int opost)
48 SMALL_RECT windowRect;
49 COORD newPosition;
50 long flags;
52 #ifndef SLANG_SAVES_CONSOLE
53 /* first off, create a new console so the old one can be restored on exit */
54 HANDLE console = CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
55 FILE_SHARE_READ |FILE_SHARE_WRITE,
57 CONSOLE_TEXTMODE_BUFFER,
58 0);
59 if (SetConsoleActiveScreenBuffer(console) == FALSE) {
60 return -1;
62 #endif
64 /* start things off at the origin */
65 newPosition.X = 0;
66 newPosition.Y = 0;
68 /* still read in characters from stdin, but output to the new console */
69 /* this way, on program exit, the original screen is restored */
70 hStdin = GetStdHandle(STD_INPUT_HANDLE);
71 /* hStdin = console; */
73 #ifndef SLANG_SAVES_CONSOLE
74 hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
75 #else
76 hStdout = console;
77 #endif
79 if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE) {
80 return -1; /* failure */
83 if (!GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) {
84 return -1; /* failure */
85 } // if
87 windowRect.Left = 0;
88 windowRect.Top = 0;
89 windowRect.Right = csbiInfo.dwSize.X - 1;
90 windowRect.Bottom = csbiInfo.dwSize.Y - 1;
91 if (!SetConsoleWindowInfo(hStdout, TRUE, &windowRect)) {
92 return -1;
95 if (SetConsoleMode(hStdin, 0) == FALSE) {
96 return -1; /* failure */
99 if (SetConsoleMode(hStdout, 0) == FALSE) {
100 return -1; /* failure */
103 if (GetConsoleMode(hStdin, &flags)) {
104 if (flags & ENABLE_PROCESSED_INPUT) {
105 return -1;
109 (void) SetConsoleCursorPosition(hStdout, newPosition);
111 /* success */
112 return 0;
113 } /* SLang_init_tty */
115 /*----------------------------------------------------------------------*\
116 * Function: void SLang_reset_tty (void);
118 * reset the tty before exiting
119 \*----------------------------------------------------------------------*/
120 void SLang_reset_tty (void)
122 set_ctrl_break (1);
125 /*----------------------------------------------------------------------*\
126 * Function: int SLsys_input_pending (int tsecs);
128 * sleep for *tsecs tenths of a sec waiting for input
129 \*----------------------------------------------------------------------*/
130 int SLsys_input_pending (int tsecs)
132 INPUT_RECORD record;
133 long one = 1;
134 long bytesRead;
136 while (1)
138 if (PeekConsoleInput(hStdin, &record, 1, &bytesRead))
140 if (bytesRead == 1)
142 if ((record.EventType == KEY_EVENT)
143 && record.Event.KeyEvent.bKeyDown)
145 /* ok, there is a keypress here */
146 return 1;
148 else
150 /* something else is here, so read it and try again */
151 (void) ReadConsoleInput(hStdin, &record, 1, &bytesRead);
154 else
156 /* no Pending events */
157 return 0;
160 else
162 /* function failed */
163 return 0;
166 #if 0
167 /* no delays yet */
168 /* use Sleep */
170 int count = tsecs * 5;
172 if (keyWaiting()) return 1;
173 while (count > 0)
175 delay (20); 20 ms or 1/50 sec
176 if (keyWaiting()) break;
177 count--;
179 return (count);
181 #endif
184 /*----------------------------------------------------------------------*\
185 * Function: unsigned int SLsys_getkey (void);
187 * wait for and get the next available keystroke.
188 * Also re-maps some useful keystrokes.
190 * Backspace (^H) => Del (127)
191 * Ctrl-Space => ^@ (^@^3 - a pc NUL char)
192 * extended keys are prefixed by a null character
193 \*----------------------------------------------------------------------*/
194 unsigned int SLsys_getkey (void)
196 unsigned int scan, ch, shift;
197 long key, bytesRead;
198 INPUT_RECORD record;
200 while (1) {
201 if (!ReadConsoleInput(hStdin, &record, 1, &bytesRead)) {
202 return 0;
204 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown) {
205 #ifndef __MINGW32__
206 return record.Event.KeyEvent.uChar.AsciiChar;
207 #else
208 return record.Event.KeyEvent.AsciiChar;
209 #endif
212 /* ReadFile(hStdin, &key, 1, &bytesRead, NULL); */
214 /* return key; */
217 /*----------------------------------------------------------------------*\
218 * Function: void SLang_set_abort_signal (void (*handler)(int));
219 \*----------------------------------------------------------------------*/
220 void SLang_set_abort_signal (void (*handler)(int))