Debian Codegraph @

terps/tads/tads2/msdos/ossdos32.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/msdos/ossdos32.c,v 1.2 1999/05/17 02:52:19 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1997, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 ossdos32.c - ossdos functions for win32
15 Function
16
17 Notes
18
19 Modified
20 11/22/97 MJRoberts - Creation
21 */
22
23 #include <windows.h>
24 #include <wincon.h>
25 #include <string.h>
26 #include "os.h"
27 #include "osgen.h"
28
29 /*
30 * Console output buffer handle
31 */
32 HANDLE G_out_bufhdl;
33 HANDLE G_in_bufhdl;
34
35 /* "plain mode" flag */
36 extern int os_f_plain;
37
38 /* original console mode */
39 static DWORD S_orig_console_out_mode;
40 static DWORD S_orig_console_in_mode;
41
42 /* screen width */
43 static int S_scrwid;
44
45 /* original size of console screen buffer */
46 static COORD S_orig_buffer_size;
47
48 /* initialize console */
49 void oss_con_init(void)
50 {
51 DWORD new_console_mode;
52 OSVERSIONINFO ver_info;
53
54 /* get the console input and output handles */
55 G_out_bufhdl = GetStdHandle(STD_OUTPUT_HANDLE);
56 G_in_bufhdl = GetStdHandle(STD_INPUT_HANDLE);
57
58 /* get the original console mode so we can restore it when we're done */
59 GetConsoleMode(G_out_bufhdl, &S_orig_console_out_mode);
60 GetConsoleMode(G_in_bufhdl, &S_orig_console_in_mode);
61
62 /* if we're in 'plain' mode, don't change the console mode */
63 if (os_f_plain)
64 return;
65
66 /* start with the original console mode bits */
67 new_console_mode = S_orig_console_out_mode;
68
69 /*
70 * Turn off wrapping when writing at the end of the line - the
71 * automatic wrapping screws up our scrolling, and we don't need it
72 * anyway since we keep track of all of our cursor positioning
73 * ourselves, hence it's easier if we just turn this feature off
74 */
75 new_console_mode &= ~ENABLE_WRAP_AT_EOL_OUTPUT;
76
77 /*
78 * If we're on a 95 derivative (95, 98, ME), also clear the "processed
79 * output" flag. This flag is necessary on NT for proper character
80 * set handling, but is not on 95/98/ME; and if that flag isn't
81 * cleared on 95/98/ME, clearing the wrap-at-eol flag has no effect.
82 * So, we must clear this flag on 95/98/ME, but we must leave it
83 * enabled on NT. (What a pain.)
84 */
85 ver_info.dwOSVersionInfoSize = sizeof(ver_info);
86 GetVersionEx(&ver_info);
87 if (ver_info.dwPlatformId != VER_PLATFORM_WIN32_NT)
88 new_console_mode &= ~ENABLE_PROCESSED_OUTPUT;
89
90 /* set the new console mode */
91 SetConsoleMode(G_out_bufhdl, new_console_mode);
92 }
93
94 /* uninitialize the console */
95 void oss_con_uninit(void)
96 {
97 CONSOLE_SCREEN_BUFFER_INFO info;
98 extern int os_f_plain;
99
100 /*
101 * If we're not in 'plain' mode, clear the screen below the cursor.
102 * This will ensure that we don't leave a bunch of leftovers from
103 * banner windows below the cursor, which the command prompt would
104 * happily spill into and create a terribly confusing display. We'll
105 * trust the osgen layer to have left the cursor at the bottom of the
106 * main text window, so this approach will preserve as much output as
107 * possible in the main text window - this is good if there was an
108 * error or farewell message just before the program existed.
109 */
110 if (!os_f_plain)
111 {
112 /* get the current cursor position */
113 GetConsoleScreenBufferInfo(G_out_bufhdl, &info);
114
115 /*
116 * If we're not already at the start of a line, add a newline.
117 * Otherwise, write a blank space and then locate back at the start
118 * of the line. In either case, writing this extra character will
119 * ensure that we leave the console with the default output color
120 * set to a the main text color.
121 */
122 if (info.dwCursorPosition.X != 0)
123 {
124 /* we're not at the start of a line - add a newline */
125 ossdsp(info.dwCursorPosition.Y, info.dwCursorPosition.X,
126 ossgetcolor(OSGEN_COLOR_TEXT, OSGEN_COLOR_TEXTBG, 0, 0),
127 "\n");
128 }
129 else
130 {
131 /*
132 * we're at the start of a line - write out a space just to set
133 * the color to something reasonable
134 */
135 ossdsp(info.dwCursorPosition.Y, info.dwCursorPosition.X,
136 ossgetcolor(OSGEN_COLOR_TEXT, OSGEN_COLOR_TEXTBG, 0, 0),
137 " ");
138
139 /* go back to the start of the line */
140 ossloc(info.dwCursorPosition.Y, info.dwCursorPosition.X);
141 }
142
143 /* clear the screen from the current line to the bottom */
144 GetConsoleScreenBufferInfo(G_out_bufhdl, &info);
145 ossclr(info.dwCursorPosition.Y, 0,
146 G_oss_screen_height - 1, G_oss_screen_width - 1,
147 ossgetcolor(OSGEN_COLOR_TEXT, OSGEN_COLOR_TEXTBG, 0, 0));
148 }
149
150 /* restore the console to its original mode settings */
151 SetConsoleMode(G_out_bufhdl, S_orig_console_out_mode);
152 SetConsoleMode(G_in_bufhdl, S_orig_console_in_mode);
153
154 /* get the current screen buffer size */
155 if (!os_f_plain && GetConsoleScreenBufferInfo(G_out_bufhdl, &info))
156 {
157 /*
158 * restore the original window buffer size, but make sure we don't
159 * set the size smaller than the current buffer size, since that
160 * will have no effect (the user could have expanded the window or
161 * the buffer size while we were running, so we need to check and
162 * adjust our original size accordingly)
163 */
164 if (info.dwSize.X > S_orig_buffer_size.X)
165 S_orig_buffer_size.X = info.dwSize.X;
166 if (info.dwSize.Y > S_orig_buffer_size.Y)
167 S_orig_buffer_size.Y = info.dwSize.Y;
168
169 /* set the original buffer size */
170 SetConsoleScreenBufferSize(G_out_bufhdl, S_orig_buffer_size);
171 }
172 }
173
174 /* process a WINDOW_BUFFER_SIZE_EVENT on input */
175 void oss_win_resize_event()
176 {
177 CONSOLE_SCREEN_BUFFER_INFO info;
178
179 /*
180 * The window buffer size has changed. Since we use our own
181 * scrolling, we always want to keep the console window buffer the
182 * same size as the window itself. So, set the buffer size back to
183 * the window size, but notify the osgen layer of the change in the
184 * underlying window size, if any.
185 */
186 if (!os_f_plain && GetConsoleScreenBufferInfo(G_out_bufhdl, &info))
187 {
188 COORD buf_size;
189
190 /* scroll it to the top left */
191 info.srWindow.Bottom -= info.srWindow.Top;
192 info.srWindow.Top = 0;
193 info.srWindow.Right -= info.srWindow.Left;
194 info.srWindow.Left = 0;
195 SetConsoleWindowInfo(G_out_bufhdl, TRUE, &info.srWindow);
196
197 /* set the window buffer size to equal the window size */
198 buf_size.X = info.srWindow.Right + 1;
199 buf_size.Y = info.srWindow.Bottom + 1;
200 SetConsoleScreenBufferSize(G_out_bufhdl, buf_size);
201
202 /* calculate the new screen size for the osgen layer */
203 G_oss_screen_height = buf_size.Y;
204 G_oss_screen_width = buf_size.X;
205
206 /* notify the osgen layer of the change */
207 osssb_on_resize_screen();
208 }
209 }
210
211 /* scroll up */
212 void ossscu(int top, int left, int bottom, int right, int blankcolor)
213 {
214 COORD newpos;
215 SMALL_RECT pos;
216 SMALL_RECT clip;
217 CHAR_INFO fill;
218
219 pos.Top = clip.Top = top;
220 pos.Left = clip.Left = left;
221 pos.Bottom = clip.Bottom = bottom;
222 pos.Right = clip.Right = right;
223
224 newpos.X = left;
225 newpos.Y = top + 1;
226
227 fill.Char.AsciiChar = ' ';
228 fill.Attributes = blankcolor;
229
230 ScrollConsoleScreenBuffer(G_out_bufhdl, &pos, &clip, newpos, &fill);
231 }
232
233 /* scroll down */
234 void ossscr(int top, int left, int bottom, int right, int blankcolor)
235 {
236 COORD newpos;
237 SMALL_RECT pos;
238 SMALL_RECT clip;
239 CHAR_INFO fill;
240
241 pos.Top = clip.Top = top;
242 pos.Left = clip.Left = left;
243 pos.Bottom = clip.Bottom = bottom;
244 pos.Right = clip.Right = right;
245
246 newpos.X = left;
247 newpos.Y = top - 1;
248
249 fill.Char.AsciiChar = ' ';
250 fill.Attributes = blankcolor;
251
252 ScrollConsoleScreenBuffer(G_out_bufhdl, &pos, &clip, newpos, &fill);
253 }
254
255 /* clear an area of the screen */
256 void ossclr(int top, int left, int bottom, int right, int color)
257 {
258 int curline;
259 DWORD line_len;
260
261 /* calculate the length of each line that we'll write */
262 line_len = right - left + 1;
263
264 /* write each line */
265 for (curline = top ; curline <= bottom ; ++curline)
266 {
267 COORD pos;
268 DWORD actual;
269
270 pos.X = left;
271 pos.Y = curline;
272 FillConsoleOutputAttribute(G_out_bufhdl, (WORD)color, line_len,
273 pos, &actual);
274 FillConsoleOutputCharacter(G_out_bufhdl, ' ', line_len,
275 pos, &actual);
276 }
277 }
278
279 /* locate the cursor */
280 void ossloc(int line, int column)
281 {
282 COORD pos;
283 CONSOLE_SCREEN_BUFFER_INFO info;
284
285 /* if we're not changing the position, leave it alone */
286 if (GetConsoleScreenBufferInfo(G_out_bufhdl, &info)
287 && info.dwCursorPosition.X == column
288 && info.dwCursorPosition.Y == line)
289 return;
290
291 /* set the new position */
292 pos.X = column;
293 pos.Y = line;
294 SetConsoleCursorPosition(G_out_bufhdl, pos);
295 }
296
297 /* check for monochrome mode */
298 int ossmon(void)
299 {
300 return 0;
301 }
302
303 /* display text */
304 void ossdsp(int line, int column, int color, const char *msg)
305 {
306 COORD pos;
307 DWORD nwritten;
308
309 pos.X = column;
310 pos.Y = line;
311 SetConsoleTextAttribute(G_out_bufhdl, (WORD)color);
312 SetConsoleCursorPosition(G_out_bufhdl, pos);
313 WriteConsole(G_out_bufhdl, msg, strlen(msg), &nwritten, 0);
314 }
315
316 /* get the display metrics */
317 void ossgmx(int *max_linep, int *max_columnp)
318 {
319 CONSOLE_SCREEN_BUFFER_INFO info;
320
321 /* get the console handle */
322 G_out_bufhdl = GetStdHandle(STD_OUTPUT_HANDLE);
323
324 /* get the size of the screen and return it to the caller's variables */
325 if (GetConsoleScreenBufferInfo(G_out_bufhdl, &info))
326 {
327 /*
328 * If it's scrolled down, scroll it to the top. If the screen
329 * buffer size is larger than the window size, reduce the buffer
330 * size to match the window size: we do all of the scrolling
331 * ourselves, so we don't want to do that on top of a separate
332 * scrolling canvas managed by the system.
333 */
334 if (!os_f_plain
335 && (info.srWindow.Top != 0 || info.srWindow.Left != 0
336 || info.dwSize.X > info.srWindow.Right + 1
337 || info.dwSize.Y > info.srWindow.Bottom + 1))
338 {
339 COORD buf_size;
340
341 /* scroll it to the top left */
342 info.srWindow.Bottom -= info.srWindow.Top;
343 info.srWindow.Top = 0;
344 info.srWindow.Right -= info.srWindow.Left;
345 info.srWindow.Left = 0;
346 SetConsoleWindowInfo(G_out_bufhdl, TRUE, &info.srWindow);
347
348 /* remember the original buffer size */
349 S_orig_buffer_size = info.dwSize;
350
351 /* set the window buffer size to equal the window size */
352 buf_size.X = info.srWindow.Right + 1;
353 buf_size.Y = info.srWindow.Bottom + 1;
354 SetConsoleScreenBufferSize(G_out_bufhdl, buf_size);
355 }
356
357 /* got the information - use the current window size */
358 *max_columnp = info.srWindow.Right - info.srWindow.Left;
359 *max_linep = info.srWindow.Bottom - info.srWindow.Top;
360 }
361 else
362 {
363 /* failed to get info - use default sizes */
364 *max_columnp = 79;
365 *max_linep = 24;
366 }
367
368 /* note the screen width for our own use */
369 S_scrwid = info.dwMaximumWindowSize.X;
370 }