Fix umpnpmgr build
[reactos.git] / rosapps / mc / slang / slw32tty.c
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 */
7
8 #include "config.h"
9 #include <stdio.h>
10
11 #include <windows.h>
12 #include <winbase.h>
13
14 #include "slang.h"
15 #include "_slang.h"
16
17 #ifdef __cplusplus
18 # define _DOTS_ ...
19 #else
20 # define _DOTS_ void
21 #endif
22
23
24
25 /*----------------------------------------------------------------------*\
26 * Function: static void set_ctrl_break (int state);
27 *
28 * set the control-break setting
29 \*----------------------------------------------------------------------*/
30 static void set_ctrl_break (int state)
31 {
32 }
33
34
35 /*----------------------------------------------------------------------*\
36 * Function: int SLang_init_tty (int abort_char, int no_flow_control,
37 * int opost);
38 *
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 \*----------------------------------------------------------------------*/
43
44 HANDLE hStdout, hStdin;
45 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
46
47 int SLang_init_tty (int abort_char, int no_flow_control, int opost)
48 {
49 SMALL_RECT windowRect;
50 COORD newPosition;
51 DWORD flags;
52
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,
57 0,
58 CONSOLE_TEXTMODE_BUFFER,
59 0);
60 if (SetConsoleActiveScreenBuffer(console) == FALSE) {
61 return -1;
62 }
63 #endif
64
65 /* start things off at the origin */
66 newPosition.X = 0;
67 newPosition.Y = 0;
68
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; */
73
74 #ifndef SLANG_SAVES_CONSOLE
75 hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
76 #else
77 hStdout = console;
78 #endif
79
80 if (hStdin == INVALID_HANDLE_VALUE || hStdout == INVALID_HANDLE_VALUE) {
81 return -1; /* failure */
82 }
83
84 if (!GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) {
85 return -1; /* failure */
86 } // if
87
88 windowRect.Left = 0;
89 windowRect.Top = 0;
90 windowRect.Right = csbiInfo.srWindow.Right - csbiInfo.srWindow.Left; //dwMaximumWindowSize.X - 1;
91 windowRect.Bottom = csbiInfo.srWindow.Bottom - csbiInfo.srWindow.Top; //dwMaximumWindowSize.Y - 1;
92 if (!SetConsoleWindowInfo(hStdout, TRUE, &windowRect)) {
93 return -1;
94 }
95
96 if (SetConsoleMode(hStdin, 0) == FALSE) {
97 return -1; /* failure */
98 }
99
100 if (SetConsoleMode(hStdout, 0) == FALSE) {
101 return -1; /* failure */
102 }
103
104 if (GetConsoleMode(hStdin, &flags)) {
105 if (flags & ENABLE_PROCESSED_INPUT) {
106 return -1;
107 }
108 }
109
110 (void) SetConsoleCursorPosition(hStdout, newPosition);
111
112 /* success */
113 return 0;
114 } /* SLang_init_tty */
115
116 /*----------------------------------------------------------------------*\
117 * Function: void SLang_reset_tty (void);
118 *
119 * reset the tty before exiting
120 \*----------------------------------------------------------------------*/
121 void SLang_reset_tty (void)
122 {
123 set_ctrl_break (1);
124 }
125
126 /*----------------------------------------------------------------------*\
127 * Function: int SLsys_input_pending (int tsecs);
128 *
129 * sleep for *tsecs tenths of a sec waiting for input
130 \*----------------------------------------------------------------------*/
131 int SLsys_input_pending (int tsecs)
132 {
133 INPUT_RECORD record;
134 long one = 1;
135 DWORD bytesRead;
136
137 while (1)
138 {
139 if (PeekConsoleInput(hStdin, &record, 1, &bytesRead))
140 {
141 if (bytesRead == 1)
142 {
143 if ((record.EventType == KEY_EVENT)
144 && record.Event.KeyEvent.bKeyDown)
145 {
146 /* ok, there is a keypress here */
147 return 1;
148 }
149 else
150 {
151 /* something else is here, so read it and try again */
152 (void) ReadConsoleInput(hStdin, &record, 1, &bytesRead);
153 }
154 }
155 else
156 {
157 /* no Pending events */
158 return 0;
159 }
160 }
161 else
162 {
163 /* function failed */
164 return 0;
165 }
166 }
167 #if 0
168 /* no delays yet */
169 /* use Sleep */
170 /*
171 int count = tsecs * 5;
172
173 if (keyWaiting()) return 1;
174 while (count > 0)
175 {
176 delay (20); 20 ms or 1/50 sec
177 if (keyWaiting()) break;
178 count--;
179 }
180 return (count);
181 */
182 #endif
183 }
184
185 /*----------------------------------------------------------------------*\
186 * Function: unsigned int SLsys_getkey (void);
187 *
188 * wait for and get the next available keystroke.
189 * Also re-maps some useful keystrokes.
190 *
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)
196 {
197 unsigned int scan, ch, shift;
198 long key;
199 DWORD bytesRead;
200 INPUT_RECORD record;
201
202 while (1) {
203 if (!ReadConsoleInput(hStdin, &record, 1, &bytesRead)) {
204 return 0;
205 }
206 if (record.EventType == KEY_EVENT && record.Event.KeyEvent.bKeyDown) {
207 /*#ifndef __MINGW32__*/
208 return record.Event.KeyEvent.uChar.AsciiChar;
209 /*#else
210 return record.Event.KeyEvent.AsciiChar;
211 #endif*/
212 }
213 }
214 /* ReadFile(hStdin, &key, 1, &bytesRead, NULL); */
215
216 /* return key; */
217 }
218
219 /*----------------------------------------------------------------------*\
220 * Function: void SLang_set_abort_signal (void (*handler)(int));
221 \*----------------------------------------------------------------------*/
222 void SLang_set_abort_signal (void (*handler)(int))
223 {
224
225 }