| 1 |
/************************************************************************** |
| 2 |
* This program is Copyright (C) 1986-2002 by Jonathan Payne. JOVE is * |
| 3 |
* provided by Jonathan and Jovehacks without charge and without * |
| 4 |
* warranty. You may copy, modify, and/or distribute JOVE, provided that * |
| 5 |
* this notice is included in all the source files and documentation. * |
| 6 |
**************************************************************************/ |
| 7 |
|
| 8 |
/* Win32 support routines for Jove Keyboard and Screen */ |
| 9 |
|
| 10 |
#include "jove.h" |
| 11 |
|
| 12 |
#ifdef WIN32 /* the body is the rest of this file */ |
| 13 |
|
| 14 |
#include "fp.h" /* scr_putchar */ |
| 15 |
#include "chars.h" |
| 16 |
#include "screen.h" |
| 17 |
#include "disp.h" /* for redisplay() */ |
| 18 |
|
| 19 |
# undef Fill /* sigh, used as a field name in some windows header! */ |
| 20 |
# undef CR /* sigh, used as a field name in some windows header! */ |
| 21 |
# include <windows.h> |
| 22 |
|
| 23 |
INPUT_RECORD in_event[NCHARS], *eventp = in_event; /* Input events e.g. keyboard, mouse-click */ |
| 24 |
DWORD nevents; |
| 25 |
private HANDLE conin, conout, conerr; /* Console handles */ |
| 26 |
private COORD curpos; |
| 27 |
private COORD maxpos; |
| 28 |
private HANDLE old_stdout, old_stderr; |
| 29 |
private WORD old_attributes; |
| 30 |
|
| 31 |
private BOOL WINAPI ctrlHandler(DWORD type); /* Control handler */ |
| 32 |
#define CHECK(fn) { if (!(fn)) ConsoleFail(#fn); } |
| 33 |
private void ConsoleFail(char *fdef); |
| 34 |
|
| 35 |
void |
| 36 |
getTERM(void) |
| 37 |
{ |
| 38 |
} |
| 39 |
|
| 40 |
void |
| 41 |
ttysetattr(jbool n) |
| 42 |
/* `n' is also used as subscript! */ |
| 43 |
{ |
| 44 |
CONSOLE_SCREEN_BUFFER_INFO info; |
| 45 |
COORD bufsize; |
| 46 |
|
| 47 |
if (n) { |
| 48 |
if (conout == NULL) { |
| 49 |
/* Create our own console buffer so we can easily restore |
| 50 |
* the startup environment. This also allows us to resize |
| 51 |
* it to eliminate the scroll bars, at least where the |
| 52 |
* full Console API is implemented (i.e. on Windows NT). |
| 53 |
* (Windows 95 seems to ignore whatever we do to the window). |
| 54 |
*/ |
| 55 |
old_stdout = GetStdHandle(STD_OUTPUT_HANDLE); |
| 56 |
old_stderr = GetStdHandle(STD_ERROR_HANDLE); |
| 57 |
CHECK(GetConsoleScreenBufferInfo(old_stdout, &info)); |
| 58 |
old_attributes = info.wAttributes; |
| 59 |
conout = CreateConsoleScreenBuffer(GENERIC_READ|GENERIC_WRITE, |
| 60 |
FILE_SHARE_READ|FILE_SHARE_WRITE, |
| 61 |
NULL, CONSOLE_TEXTMODE_BUFFER, NULL); |
| 62 |
CHECK(DuplicateHandle(GetCurrentProcess(), conout, |
| 63 |
GetCurrentProcess(), &conerr, |
| 64 |
0, TRUE, DUPLICATE_SAME_ACCESS)); |
| 65 |
bufsize.X = info.srWindow.Right-info.srWindow.Left+1; |
| 66 |
bufsize.Y = info.srWindow.Bottom-info.srWindow.Top+1; |
| 67 |
CHECK(SetConsoleScreenBufferSize(conout, bufsize)); |
| 68 |
#ifdef DEBUG |
| 69 |
{ |
| 70 |
char tracebuf[100]; |
| 71 |
DWORD version = GetVersion(); |
| 72 |
wsprintf(tracebuf, "%s Windows %d.%d Build %d\n", |
| 73 |
(version > 0 ? "MS-DOS" : "NT"), |
| 74 |
LOBYTE(LOWORD(version)), HIBYTE(LOWORD(version)), |
| 75 |
HIWORD(version)&0x7FFF); |
| 76 |
OutputDebugString(tracebuf); |
| 77 |
wsprintf(tracebuf, "Console Size=(%d,%d), Window=(%d,%d) to (%d,%d)\n", |
| 78 |
info.dwSize.X, info.dwSize.Y, |
| 79 |
info.srWindow.Left, info.srWindow.Top, |
| 80 |
info.srWindow.Right, info.srWindow.Bottom); |
| 81 |
OutputDebugString(tracebuf); |
| 82 |
} |
| 83 |
#endif |
| 84 |
CHECK(SetStdHandle(STD_OUTPUT_HANDLE, conout)); |
| 85 |
CHECK(SetStdHandle(STD_ERROR_HANDLE, conerr)); |
| 86 |
CHECK(SetConsoleActiveScreenBuffer(conout)); |
| 87 |
} |
| 88 |
CHECK(SetConsoleTitle("Jove for Win32")); |
| 89 |
conin = GetStdHandle(STD_INPUT_HANDLE); |
| 90 |
/* conout = GetStdHandle(STD_OUTPUT_HANDLE); */ |
| 91 |
|
| 92 |
# ifdef MOUSE |
| 93 |
SetConsoleMode(conin, |
| 94 |
ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT); |
| 95 |
MouseOn(); |
| 96 |
# else |
| 97 |
SetConsoleMode(conin, ENABLE_WINDOW_INPUT); |
| 98 |
# endif /* !MOUSE */ |
| 99 |
SetConsoleMode(conout, |
| 100 |
ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); |
| 101 |
SetFileApisToOEM(); |
| 102 |
SetConsoleCtrlHandler(ctrlHandler, TRUE); |
| 103 |
} else { |
| 104 |
CHECK(SetStdHandle(STD_OUTPUT_HANDLE, old_stdout)); |
| 105 |
CHECK(SetStdHandle(STD_ERROR_HANDLE, old_stderr)); |
| 106 |
CHECK(SetConsoleActiveScreenBuffer(old_stdout)); |
| 107 |
CloseHandle(conout); |
| 108 |
CloseHandle(conerr); |
| 109 |
SetConsoleTextAttribute(old_stdout, old_attributes); |
| 110 |
conin = conout = conerr = NULL; |
| 111 |
# ifdef MOUSE |
| 112 |
MouseOff(); |
| 113 |
# endif |
| 114 |
SetConsoleCtrlHandler(ctrlHandler, FALSE); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
void |
| 119 |
ttsize(void) |
| 120 |
{ |
| 121 |
/* ??? We really ought to wait until the screen is big enough: |
| 122 |
* at least three lines high (one line each for buffer, mode, |
| 123 |
* and message) and at least twelve columns wide (eight for |
| 124 |
* line number, one for content, two for overflow indicators, |
| 125 |
* and one blank at end). |
| 126 |
*/ |
| 127 |
CONSOLE_SCREEN_BUFFER_INFO info; |
| 128 |
GetConsoleScreenBufferInfo(conout, &info); |
| 129 |
maxpos.X = info.srWindow.Right-info.srWindow.Left+1; |
| 130 |
maxpos.Y = info.srWindow.Bottom-info.srWindow.Top+1; |
| 131 |
if (maxpos.X != info.dwSize.X || maxpos.Y != info.dwSize.Y) { |
| 132 |
CHECK(SetConsoleScreenBufferSize(conout, maxpos)); |
| 133 |
curpos.X = curpos.Y = 0; |
| 134 |
} else { |
| 135 |
curpos = info.dwCursorPosition; |
| 136 |
} |
| 137 |
CO = maxpos.X; |
| 138 |
if (CO > MAXCOLS) |
| 139 |
CO = MAXCOLS; |
| 140 |
LI = maxpos.Y; |
| 141 |
ILI = LI - 1; |
| 142 |
} |
| 143 |
|
| 144 |
#ifdef DEBUG |
| 145 |
private char * |
| 146 |
formatChar(char c) |
| 147 |
{ |
| 148 |
static char cbuf[6]; |
| 149 |
|
| 150 |
if (c >= ' ' && c <= '~') |
| 151 |
wsprintf(cbuf, "%c", c); |
| 152 |
else |
| 153 |
wsprintf(cbuf, "\\%03o", (unsigned char)c); |
| 154 |
return cbuf; |
| 155 |
} |
| 156 |
|
| 157 |
private char * |
| 158 |
formatEvent(INPUT_RECORD* event) |
| 159 |
{ |
| 160 |
static char buffer[128]; |
| 161 |
|
| 162 |
switch (event->EventType) { |
| 163 |
case KEY_EVENT: |
| 164 |
wsprintf(buffer, "KEY%s '%s%s%s%s'[%d times] Scan=%d Keycode=%d\n", |
| 165 |
event->Event.KeyEvent.bKeyDown?"DOWN":"UP", |
| 166 |
(event->Event.KeyEvent.dwControlKeyState & |
| 167 |
(RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED) ? "ALT-" : ""), |
| 168 |
(event->Event.KeyEvent.dwControlKeyState & |
| 169 |
(RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED) ? "CTRL-" : ""), |
| 170 |
(event->Event.KeyEvent.dwControlKeyState & |
| 171 |
(SHIFT_PRESSED) ? "SHIFT-" : ""), |
| 172 |
formatChar(event->Event.KeyEvent.uChar.AsciiChar), |
| 173 |
event->Event.KeyEvent.wRepeatCount, |
| 174 |
event->Event.KeyEvent.wVirtualScanCode, |
| 175 |
event->Event.KeyEvent.wVirtualKeyCode |
| 176 |
); |
| 177 |
break; |
| 178 |
case MOUSE_EVENT: |
| 179 |
wsprintf(buffer, "MOUSE%s%s %s%s%s%s%s%s (%d,%d)\n", |
| 180 |
(event->Event.MouseEvent.dwEventFlags&MOUSE_MOVED ? |
| 181 |
" MOVED" : ""), |
| 182 |
(event->Event.MouseEvent.dwEventFlags&DOUBLE_CLICK? |
| 183 |
" DOUBLE_CLICK" : ""), |
| 184 |
(event->Event.MouseEvent.dwButtonState&1 ? "LEFT" : ""), |
| 185 |
(event->Event.MouseEvent.dwButtonState&2 ? "RIGHT" : ""), |
| 186 |
(event->Event.MouseEvent.dwButtonState&4 ? "MIDDLE" : ""), |
| 187 |
(event->Event.MouseEvent.dwControlKeyState & |
| 188 |
(RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED) ? "ALT-" : ""), |
| 189 |
(event->Event.MouseEvent.dwControlKeyState & |
| 190 |
(RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED) ? "CTRL-" : ""), |
| 191 |
(event->Event.MouseEvent.dwControlKeyState & |
| 192 |
(SHIFT_PRESSED) ? "SHIFT-" : ""), |
| 193 |
event->Event.MouseEvent.dwMousePosition.X, |
| 194 |
event->Event.MouseEvent.dwMousePosition.Y |
| 195 |
); |
| 196 |
break; |
| 197 |
case WINDOW_BUFFER_SIZE_EVENT: |
| 198 |
wsprintf(buffer, "RESIZE (%d,%d)\n", |
| 199 |
eventp->Event.WindowBufferSizeEvent.dwSize.Y, |
| 200 |
eventp->Event.WindowBufferSizeEvent.dwSize.X); |
| 201 |
break; |
| 202 |
case MENU_EVENT: |
| 203 |
wsprintf(buffer, "MENU %d\n", eventp->Event.MenuEvent.dwCommandId); |
| 204 |
break; |
| 205 |
case FOCUS_EVENT: |
| 206 |
wsprintf(buffer, "FOCUS %d\n", eventp->Event.FocusEvent.bSetFocus); |
| 207 |
break; |
| 208 |
default: |
| 209 |
wsprintf(buffer, "UNKNOWN %d\n", eventp->EventType); |
| 210 |
break; |
| 211 |
} |
| 212 |
return buffer; |
| 213 |
} |
| 214 |
#endif /* DEBUG */ |
| 215 |
|
| 216 |
/* MapKeyEventToChars maps Console KEY_RECORDs into a string of characters. |
| 217 |
* The mapping rules are basically those of the IBM AT BIOS as modified for |
| 218 |
* use by JOVE (i.e. the result should agree with what a JOVE user would see |
| 219 |
* running the MS-DOS version of Jove). |
| 220 |
*/ |
| 221 |
private int |
| 222 |
MapKeyEventToChars(KEY_EVENT_RECORD* key, char *bptr) |
| 223 |
{ |
| 224 |
int cnt = 0; |
| 225 |
|
| 226 |
if (key->bKeyDown) { |
| 227 |
if (key->uChar.AsciiChar) { |
| 228 |
if ((key->dwControlKeyState & |
| 229 |
(RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED)) |
| 230 |
&& key->uChar.AsciiChar == ' ') |
| 231 |
{ |
| 232 |
bptr[cnt++] = '\0'; |
| 233 |
} else if (key->dwControlKeyState & |
| 234 |
(RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED)) |
| 235 |
{ |
| 236 |
bptr[cnt++] = PCNONASCII; |
| 237 |
if (key->wVirtualScanCode >= 2 |
| 238 |
&& key->wVirtualScanCode <= 13) |
| 239 |
{ |
| 240 |
/* Top row (numeric keys) */ |
| 241 |
bptr[cnt++] = key->wVirtualScanCode + 118; |
| 242 |
} else { |
| 243 |
bptr[cnt++] = key->wVirtualScanCode; |
| 244 |
} |
| 245 |
} else { |
| 246 |
bptr[cnt++] = key->uChar.AsciiChar; |
| 247 |
} |
| 248 |
} else { |
| 249 |
# define ShiftSelect(plain, shifted, ctrled, alted) ( \ |
| 250 |
(key->dwControlKeyState & SHIFT_PRESSED) \ |
| 251 |
? (shifted) \ |
| 252 |
: (key->dwControlKeyState & (RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED)) \ |
| 253 |
? (ctrled) \ |
| 254 |
: (key->dwControlKeyState & (RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED)) \ |
| 255 |
? (alted) \ |
| 256 |
: (plain)) |
| 257 |
|
| 258 |
switch (key->wVirtualKeyCode) { |
| 259 |
case VK_F1: case VK_F2: case VK_F3: case VK_F4: case VK_F5: |
| 260 |
case VK_F6: case VK_F7: case VK_F8: case VK_F9: case VK_F10: |
| 261 |
bptr[cnt++] = PCNONASCII; |
| 262 |
bptr[cnt++] = key->wVirtualKeyCode - VK_F1 |
| 263 |
+ ShiftSelect(59, 84, 94, 104); |
| 264 |
break; |
| 265 |
case VK_F11: case VK_F12: |
| 266 |
bptr[cnt++] = PCNONASCII; |
| 267 |
bptr[cnt++] = key->wVirtualKeyCode - VK_F11 |
| 268 |
+ ShiftSelect(133, 135, 137, 139); |
| 269 |
break; |
| 270 |
case '1': case '2': case '3': case '4': case '5': |
| 271 |
case '6': case '7': case '8': case '9': case '0': |
| 272 |
bptr[cnt++] = PCNONASCII; |
| 273 |
bptr[cnt++] = key->wVirtualScanCode + 118; |
| 274 |
break; |
| 275 |
case VK_HOME: |
| 276 |
bptr[cnt++] = PCNONASCII; |
| 277 |
bptr[cnt++] = ShiftSelect(71, 171, 119, 151); |
| 278 |
break; |
| 279 |
case VK_UP: |
| 280 |
bptr[cnt++] = PCNONASCII; |
| 281 |
bptr[cnt++] = ShiftSelect(72, 171, 141, 152); |
| 282 |
break; |
| 283 |
case VK_PRIOR: |
| 284 |
bptr[cnt++] = PCNONASCII; |
| 285 |
bptr[cnt++] = ShiftSelect(73, 172, 132, 153); |
| 286 |
break; |
| 287 |
case VK_SUBTRACT: /* KEYPAD- */ |
| 288 |
bptr[cnt++] = PCNONASCII; |
| 289 |
if (key->dwControlKeyState & |
| 290 |
(RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED)) |
| 291 |
{ |
| 292 |
if (key->dwControlKeyState & SHIFT_PRESSED) { |
| 293 |
bptr[cnt++] = (unsigned char)174; |
| 294 |
} else { |
| 295 |
bptr[cnt++] = 74; |
| 296 |
} |
| 297 |
} else { |
| 298 |
bptr[cnt++] = (unsigned char)142; |
| 299 |
} |
| 300 |
break; |
| 301 |
case VK_LEFT: |
| 302 |
bptr[cnt++] = PCNONASCII; |
| 303 |
bptr[cnt++] = ShiftSelect(75, 175, 115, 155); |
| 304 |
break; |
| 305 |
case VK_RIGHT: |
| 306 |
bptr[cnt++] = PCNONASCII; |
| 307 |
bptr[cnt++] = ShiftSelect(77, 177, 116, 157); |
| 308 |
break; |
| 309 |
case VK_ADD: /* KEYPAD+ */ |
| 310 |
bptr[cnt++] = PCNONASCII; |
| 311 |
if (key->dwControlKeyState & |
| 312 |
(RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED)) |
| 313 |
{ |
| 314 |
if (key->dwControlKeyState & SHIFT_PRESSED) { |
| 315 |
bptr[cnt++] = (unsigned char)178; |
| 316 |
} else { |
| 317 |
bptr[cnt++] = 78; |
| 318 |
} |
| 319 |
} else { |
| 320 |
bptr[cnt++] = (unsigned char)144; |
| 321 |
} |
| 322 |
break; |
| 323 |
case VK_END: |
| 324 |
bptr[cnt++] = PCNONASCII; |
| 325 |
bptr[cnt++] = ShiftSelect(79, 179, 117, 159); |
| 326 |
break; |
| 327 |
case VK_DOWN: |
| 328 |
bptr[cnt++] = PCNONASCII; |
| 329 |
bptr[cnt++] = ShiftSelect(80, 180, 145, 160); |
| 330 |
break; |
| 331 |
case VK_NEXT: |
| 332 |
bptr[cnt++] = PCNONASCII; |
| 333 |
bptr[cnt++] = ShiftSelect(81, 181, 118, 161); |
| 334 |
break; |
| 335 |
case VK_INSERT: |
| 336 |
bptr[cnt++] = PCNONASCII; |
| 337 |
bptr[cnt++] = ShiftSelect(82, 182, 146, 162); |
| 338 |
break; |
| 339 |
case VK_DELETE: |
| 340 |
if (key->dwControlKeyState & SHIFT_PRESSED) { |
| 341 |
bptr[cnt++] = PCNONASCII; |
| 342 |
bptr[cnt++] = (unsigned char)183; |
| 343 |
} else if (key->dwControlKeyState & |
| 344 |
(RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED)) |
| 345 |
{ |
| 346 |
bptr[cnt++] = PCNONASCII; |
| 347 |
bptr[cnt++] = (unsigned char)147; |
| 348 |
} else if (key->dwControlKeyState & |
| 349 |
(RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED)) |
| 350 |
{ |
| 351 |
bptr[cnt++] = PCNONASCII; |
| 352 |
bptr[cnt++] = (unsigned char)163; |
| 353 |
} else { |
| 354 |
bptr[cnt++] = '\177'; /* Mapped to ASCII DEL */ |
| 355 |
} |
| 356 |
break; |
| 357 |
case VK_DIVIDE: /* KEYPAD/ */ |
| 358 |
bptr[cnt++] = PCNONASCII; |
| 359 |
if (key->dwControlKeyState & |
| 360 |
(RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED)) |
| 361 |
{ |
| 362 |
bptr[cnt++] = (unsigned char)164; |
| 363 |
} else { |
| 364 |
bptr[cnt++] = (unsigned char)149; |
| 365 |
} |
| 366 |
break; |
| 367 |
case VK_MULTIPLY: /* KEYPAD* */ |
| 368 |
bptr[cnt++] = PCNONASCII; |
| 369 |
if (key->dwControlKeyState & |
| 370 |
(RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED)) |
| 371 |
{ |
| 372 |
bptr[cnt++] = 55; |
| 373 |
} else { |
| 374 |
bptr[cnt++] = (unsigned char)150; |
| 375 |
} |
| 376 |
break; |
| 377 |
case VK_NUMPAD5: |
| 378 |
bptr[cnt++] = PCNONASCII; |
| 379 |
bptr[cnt++] = (unsigned char)143; |
| 380 |
break; |
| 381 |
case VK_PRINT: /* PRINT SCREEN */ |
| 382 |
bptr[cnt++] = PCNONASCII; |
| 383 |
bptr[cnt++] = 113; |
| 384 |
break; |
| 385 |
case VK_ESCAPE: |
| 386 |
if (key->dwControlKeyState & (RIGHT_ALT_PRESSED|LEFT_ALT_PRESSED)) { |
| 387 |
bptr[cnt++] = PCNONASCII; |
| 388 |
bptr[cnt++] = key->wVirtualScanCode; |
| 389 |
} else { |
| 390 |
bptr[cnt++] = '\033'; /* ASCII ESC */ |
| 391 |
} |
| 392 |
break; |
| 393 |
case 'C': /* PRINT SCREEN */ |
| 394 |
if (key->dwControlKeyState & (RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED)) { |
| 395 |
bptr[cnt++] = '\003'; /* Ctrl-C : remapped in Windows 95 */ |
| 396 |
} else { |
| 397 |
bptr[cnt++] = PCNONASCII; |
| 398 |
bptr[cnt++] = key->wVirtualScanCode; |
| 399 |
} |
| 400 |
break; |
| 401 |
case VK_CAPITAL: |
| 402 |
case VK_SHIFT: |
| 403 |
case VK_CONTROL: |
| 404 |
case VK_MENU: |
| 405 |
case VK_NUMLOCK: |
| 406 |
case VK_SCROLL: |
| 407 |
/* Modifiers - ignore them. */ |
| 408 |
break; |
| 409 |
default: |
| 410 |
bptr[cnt++] = PCNONASCII; |
| 411 |
bptr[cnt++] = key->wVirtualScanCode; |
| 412 |
break; |
| 413 |
} |
| 414 |
# undef ShiftSelect |
| 415 |
} |
| 416 |
} |
| 417 |
return cnt; |
| 418 |
} |
| 419 |
|
| 420 |
int |
| 421 |
getInputEvents(char *bp, int size) |
| 422 |
{ |
| 423 |
int nchars = 0; |
| 424 |
|
| 425 |
if (eventp >= in_event+nevents) { |
| 426 |
if (!ReadConsoleInput(conin, in_event, NCHARS, &nevents)) { |
| 427 |
complain("ReadConsole failed (shouldn't happen)"); |
| 428 |
/* NOTREACHED */ |
| 429 |
} |
| 430 |
eventp = in_event; |
| 431 |
} |
| 432 |
while (eventp < in_event+nevents) { |
| 433 |
switch (eventp->EventType) { |
| 434 |
case KEY_EVENT: |
| 435 |
nchars += MapKeyEventToChars(&eventp->Event.KeyEvent, bp+nchars); |
| 436 |
if (--eventp->Event.KeyEvent.wRepeatCount == 0) |
| 437 |
eventp += 1; |
| 438 |
break; |
| 439 |
case MOUSE_EVENT: |
| 440 |
#ifdef MOUSE |
| 441 |
/* We are only interested in hits */ |
| 442 |
if (eventp->Event.MouseEvent.dwButtonState != 0) { |
| 443 |
bp[nchars++] = PCNONASCII; |
| 444 |
bp[nchars++] = '\xb0'+((char)eventp->Event.MouseEvent.dwButtonState&017); |
| 445 |
bp[nchars++] = (char)eventp->Event.MouseEvent.dwMousePosition.X; |
| 446 |
bp[nchars++] = (char)eventp->Event.MouseEvent.dwMousePosition.Y; |
| 447 |
} |
| 448 |
#endif |
| 449 |
eventp += 1; |
| 450 |
break; |
| 451 |
|
| 452 |
case WINDOW_BUFFER_SIZE_EVENT: |
| 453 |
maxpos = eventp->Event.WindowBufferSizeEvent.dwSize; |
| 454 |
eventp += 1; |
| 455 |
ResizePending = YES; |
| 456 |
break; |
| 457 |
default: |
| 458 |
eventp += 1; |
| 459 |
break; |
| 460 |
} |
| 461 |
if (nchars >= size - 5) |
| 462 |
break; |
| 463 |
} |
| 464 |
return nchars; |
| 465 |
} |
| 466 |
|
| 467 |
BOOL |
| 468 |
inputEventWaiting(int period) |
| 469 |
{ |
| 470 |
#ifdef IPROCS |
| 471 |
return wait_for_any_input(period) == WAIT_OBJECT_0; |
| 472 |
#else |
| 473 |
INPUT_RECORD evnt[10]; |
| 474 |
PINPUT_RECORD pEvnt; |
| 475 |
DWORD cnt; |
| 476 |
static BOOL first=TRUE; |
| 477 |
|
| 478 |
if (first) |
| 479 |
return first=FALSE; /* Force display on first call */ |
| 480 |
|
| 481 |
if (eventp < in_event+nevents) |
| 482 |
return TRUE; /* Already something in the queue */ |
| 483 |
|
| 484 |
while (WaitForSingleObject(conin, period) == WAIT_OBJECT_0) { |
| 485 |
if (!PeekConsoleInput(conin, evnt |
| 486 |
, sizeof(evnt)/sizeof(evnt[0]), &cnt)) |
| 487 |
break; |
| 488 |
|
| 489 |
/* Check if the waiting event is of interest. */ |
| 490 |
for (pEvnt=evnt; pEvnt<evnt+cnt; ++pEvnt) { |
| 491 |
if ((pEvnt->EventType == KEY_EVENT |
| 492 |
&& pEvnt->Event.KeyEvent.bKeyDown) |
| 493 |
|| pEvnt->EventType == WINDOW_BUFFER_SIZE_EVENT |
| 494 |
|| pEvnt->EventType == MOUSE_EVENT) |
| 495 |
return TRUE; |
| 496 |
} |
| 497 |
/* If we reach here, the event(s) are uninteresting - chuck them. */ |
| 498 |
if (!ReadConsoleInput(conin, evnt, cnt, &cnt)) |
| 499 |
break; |
| 500 |
} |
| 501 |
return FALSE; |
| 502 |
#endif |
| 503 |
} |
| 504 |
|
| 505 |
private void |
| 506 |
SaveBufferFile(Buffer *b) |
| 507 |
{ |
| 508 |
Buffer *save_buf = curbuf; |
| 509 |
char title[80]; |
| 510 |
|
| 511 |
if (b->b_fname) { |
| 512 |
wsprintf(title, "Save Jove buffer `%s'?", b->b_name); |
| 513 |
if (MessageBox(NULL, b->b_fname, title, MB_YESNO) != IDYES) |
| 514 |
return; |
| 515 |
} else { |
| 516 |
OPENFILENAME ofn; /* common dialog box structure */ |
| 517 |
char szFile[_MAX_PATH]; /* filename string */ |
| 518 |
char szDirPath[_MAX_PATH]; /* filename string */ |
| 519 |
|
| 520 |
/* Set the members of the OPENFILENAME structure. */ |
| 521 |
memset(&ofn, 0, sizeof(ofn)); |
| 522 |
ofn.lStructSize = sizeof(OPENFILENAME); |
| 523 |
szFile[0] = '\0'; |
| 524 |
ofn.lpstrFile = szFile; |
| 525 |
ofn.nMaxFile = _MAX_PATH;; |
| 526 |
ofn.lpstrFilter = "All (*.*)\0*.*\0" |
| 527 |
"C source (*.c;*.cpp;*.cxx)\0*.c;*.cpp;*,cxx\0" |
| 528 |
"C header (*.h;*.hpp;*.hxx)\0*.h;*.hpp;*.hxx\0"; |
| 529 |
ofn.lpstrInitialDir = szDirPath; |
| 530 |
ofn.lpstrTitle = title; |
| 531 |
wsprintf(title, "Save Jove buffer `%s' as:", b->b_name); |
| 532 |
|
| 533 |
getcwd(szDirPath, _MAX_PATH-1); |
| 534 |
ofn.Flags = OFN_SHOWHELP | OFN_OVERWRITEPROMPT | |
| 535 |
OFN_PATHMUSTEXIST; |
| 536 |
if (!GetSaveFileName(&ofn)) |
| 537 |
return; |
| 538 |
|
| 539 |
setfname(b, ofn.lpstrFile); |
| 540 |
} |
| 541 |
|
| 542 |
/* Still here? Save the file, then. */ |
| 543 |
SetBuf(b); |
| 544 |
SaveFile(); |
| 545 |
SetBuf(save_buf); |
| 546 |
} |
| 547 |
|
| 548 |
private void |
| 549 |
MessageCloseFiles(void) |
| 550 |
{ |
| 551 |
/* We use a static buffer pointer so that we can detect if we |
| 552 |
* have been re-entered. If so, we do nothing. This can happen |
| 553 |
* if the user takes too long to respond). |
| 554 |
*/ |
| 555 |
static Buffer *b; |
| 556 |
|
| 557 |
if (b == NULL) |
| 558 |
for (b = world; b != NULL; b = b->b_next) |
| 559 |
if (b->b_type != B_SCRATCH |
| 560 |
&& b->b_type == B_FILE |
| 561 |
&& IsModified(b)) |
| 562 |
SaveBufferFile(b); |
| 563 |
} |
| 564 |
|
| 565 |
/* Control interrupt handler deals with CTRL-BREAK and exit requests */ |
| 566 |
private BOOL WINAPI |
| 567 |
ctrlHandler(DWORD type) |
| 568 |
{ |
| 569 |
switch(type) { |
| 570 |
case CTRL_C_EVENT: |
| 571 |
return TRUE; /* Ignore, keyboard handler will see it. */ |
| 572 |
case CTRL_BREAK_EVENT: |
| 573 |
return FALSE; |
| 574 |
case CTRL_CLOSE_EVENT: |
| 575 |
case CTRL_LOGOFF_EVENT: |
| 576 |
case CTRL_SHUTDOWN_EVENT: |
| 577 |
MessageCloseFiles(); |
| 578 |
break; |
| 579 |
} |
| 580 |
return FALSE; /* Carry on */ |
| 581 |
} |
| 582 |
|
| 583 |
private WORD |
| 584 |
c_attr = 0x07; /* current attribute white on black */ |
| 585 |
|
| 586 |
int |
| 587 |
Txattr = 0x07, /* VAR: text-attribute (white on black) */ |
| 588 |
Mlattr = 0x70, /* VAR: mode-line-attribute (black on white) */ |
| 589 |
Hlattr = 0x10; /* VAR: highlight-attribute */ |
| 590 |
|
| 591 |
#define c_row curpos.Y /* current row */ |
| 592 |
#define c_col curpos.X /* current column */ |
| 593 |
|
| 594 |
#define cur_mov(r, c) { \ |
| 595 |
curpos.X = (c); curpos.Y = (r); \ |
| 596 |
CHECK(SetConsoleCursorPosition(conout, curpos)); \ |
| 597 |
} |
| 598 |
#define setcolor(c) { \ |
| 599 |
c_attr = (c); \ |
| 600 |
CHECK(SetConsoleTextAttribute(conout, c_attr)); \ |
| 601 |
} |
| 602 |
|
| 603 |
/* WIN32 calls aren't cheap, so we buffer output */ |
| 604 |
|
| 605 |
private char displaybuf[1024]; |
| 606 |
private DWORD bufpos = 0; |
| 607 |
|
| 608 |
/* getLastErrorString is a WIN32 helper function that returns a description |
| 609 |
* of the error code that GetLastError() returns. |
| 610 |
* Implementation note: we use a mode of operation whereby FormatMessage allocates |
| 611 |
* a string large enough; the returned value will be valid until the function is |
| 612 |
* called again. |
| 613 |
*/ |
| 614 |
private char *reason; |
| 615 |
|
| 616 |
private void |
| 617 |
freeReason(void) |
| 618 |
{ |
| 619 |
if (reason) { |
| 620 |
VirtualFree((PVOID)reason, 0, MEM_RELEASE); |
| 621 |
reason = NULL; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
char * |
| 626 |
getLastErrorString(void) |
| 627 |
{ |
| 628 |
static BOOL cleanupRegistered; |
| 629 |
char *ptr; |
| 630 |
|
| 631 |
if (!cleanupRegistered) { |
| 632 |
atexit(freeReason); |
| 633 |
cleanupRegistered = TRUE; |
| 634 |
} |
| 635 |
freeReason(); |
| 636 |
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_ALLOCATE_BUFFER, |
| 637 |
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL), |
| 638 |
(char*)&reason, 0, NULL); |
| 639 |
if (((ptr = strchr(reason, '\r')) != NULL) |
| 640 |
|| ((ptr = strchr(reason, '\n')) != NULL)) |
| 641 |
*ptr = '\0'; /* Trim off carriage control */ |
| 642 |
return reason; |
| 643 |
} |
| 644 |
|
| 645 |
private void |
| 646 |
ConsoleFail(char *fdef) |
| 647 |
{ |
| 648 |
char why[200]; |
| 649 |
char *reason = getLastErrorString(); |
| 650 |
|
| 651 |
wsprintf(why, "Console function \"%s\" failed: %s", fdef, reason); |
| 652 |
if (MessageBox(NULL, getLastErrorString(), why, MB_OKCANCEL) == IDCANCEL) |
| 653 |
abort(); |
| 654 |
} |
| 655 |
|
| 656 |
private void |
| 657 |
scr_win(int no, int ulr, int ulc, int lrr, int lrc) |
| 658 |
{ |
| 659 |
SMALL_RECT r, clip; |
| 660 |
COORD whereto; |
| 661 |
CHAR_INFO charinfo; |
| 662 |
|
| 663 |
clip.Top = ulr; |
| 664 |
clip.Left = ulc; |
| 665 |
clip.Bottom = lrr; |
| 666 |
clip.Right = lrc; |
| 667 |
whereto.X = ulc; |
| 668 |
r.Left = ulc; |
| 669 |
r.Right = lrc; |
| 670 |
if (no > 0) { |
| 671 |
r.Top = ulr + no; |
| 672 |
r.Bottom = lrr; |
| 673 |
whereto.Y = ulr; |
| 674 |
} else { |
| 675 |
r.Top = ulr; |
| 676 |
r.Bottom = lrr + no; |
| 677 |
whereto.Y = ulr - no; |
| 678 |
} |
| 679 |
|
| 680 |
charinfo.Char.AsciiChar = ' '; |
| 681 |
charinfo.Attributes = c_attr; |
| 682 |
|
| 683 |
CHECK(ScrollConsoleScreenBuffer(conout, &r, &clip, whereto, &charinfo)); |
| 684 |
|
| 685 |
/* If the scrolled region is less than half of the clip area, there will |
| 686 |
* be an unscrolled area in the middle - it must be filled with space. |
| 687 |
*/ |
| 688 |
if (no > 0) { |
| 689 |
whereto.Y += (r.Bottom - r.Top + 1); |
| 690 |
while (whereto.Y < r.Top) { |
| 691 |
DWORD written; |
| 692 |
|
| 693 |
CHECK(FillConsoleOutputCharacter(conout, ' ', lrc-ulc+1, whereto, &written)); |
| 694 |
whereto.Y += 1; |
| 695 |
} |
| 696 |
} else { |
| 697 |
while (--whereto.Y > r.Bottom) { |
| 698 |
DWORD written; |
| 699 |
|
| 700 |
CHECK(FillConsoleOutputCharacter(conout, ' ', lrc-ulc+1, whereto, &written)); |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
void |
| 706 |
i_lines(top, bottom, num) |
| 707 |
int top, bottom, num; |
| 708 |
{ |
| 709 |
scr_win(-num, top, 0, bottom, CO-1); |
| 710 |
} |
| 711 |
|
| 712 |
void |
| 713 |
d_lines(top, bottom, num) |
| 714 |
int top, bottom, num; |
| 715 |
{ |
| 716 |
scr_win(num, top, 0, bottom, CO-1); |
| 717 |
} |
| 718 |
|
| 719 |
void |
| 720 |
clr_page(void) |
| 721 |
{ |
| 722 |
DWORD written; |
| 723 |
|
| 724 |
SO_off(); |
| 725 |
cur_mov(0, 0); |
| 726 |
CHECK(FillConsoleOutputCharacter(conout, ' ', maxpos.X*maxpos.Y, curpos, &written)); |
| 727 |
CHECK(FillConsoleOutputAttribute(conout, c_attr, maxpos.X*maxpos.Y, curpos, &written)); |
| 728 |
} |
| 729 |
|
| 730 |
void |
| 731 |
flushscreen(void) |
| 732 |
{ |
| 733 |
if (bufpos != 0) { |
| 734 |
DWORD written; |
| 735 |
|
| 736 |
CHECK(WriteConsole(conout, displaybuf, bufpos, &written, NULL)); |
| 737 |
if (written != bufpos) { |
| 738 |
byte_move(displaybuf + written, displaybuf, bufpos - written); |
| 739 |
} |
| 740 |
bufpos = 0; |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
void |
| 745 |
clr_eoln(void) |
| 746 |
{ |
| 747 |
DWORD written; |
| 748 |
|
| 749 |
CHECK(FillConsoleOutputCharacter(conout, ' ', CO-c_col, curpos, &written)); |
| 750 |
CHECK(FillConsoleOutputAttribute(conout, c_attr, CO-c_col, curpos, &written)); |
| 751 |
} |
| 752 |
|
| 753 |
void |
| 754 |
dobell(n) /* declared in term.h */ |
| 755 |
int n; |
| 756 |
{ |
| 757 |
MessageBeep(n); |
| 758 |
} |
| 759 |
|
| 760 |
void |
| 761 |
ResizeWindow(void) |
| 762 |
{ |
| 763 |
/* Must update window size to eliminate those ugly scroll bars */ |
| 764 |
SMALL_RECT newsize; |
| 765 |
|
| 766 |
newsize.Top = newsize.Left = 0; |
| 767 |
newsize.Right = maxpos.X-1; |
| 768 |
newsize.Bottom = maxpos.Y-1; |
| 769 |
/* Note: the following fails with "invalid address". If you can figure out why, please fix it. */ |
| 770 |
SetConsoleWindowInfo(conout, TRUE, &newsize); |
| 771 |
} |
| 772 |
|
| 773 |
/* scr_putchar: put char on screen. Declared in fp.h */ |
| 774 |
|
| 775 |
/* char is subject to default argument promotions */ |
| 776 |
void |
| 777 |
scr_putchar(char c) |
| 778 |
{ |
| 779 |
if (bufpos >= sizeof(displaybuf)) |
| 780 |
flushscreen(); |
| 781 |
displaybuf[bufpos++] = c; |
| 782 |
switch (c) { |
| 783 |
case LF: |
| 784 |
c_row += 1; |
| 785 |
break; |
| 786 |
case CTL('M'): /* CR */ |
| 787 |
c_col = 0; |
| 788 |
break; |
| 789 |
case BS: |
| 790 |
if (c_col > 0) |
| 791 |
c_col -= 1; |
| 792 |
break; |
| 793 |
case CTL('G'): /* ??? is this ever used? */ |
| 794 |
dobell(1); |
| 795 |
break; |
| 796 |
default: |
| 797 |
if (++c_col > CO-1) { |
| 798 |
c_col = 0; |
| 799 |
c_row += 1; |
| 800 |
} |
| 801 |
break; |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
/* No cursor optimization on an WIN32, this simplifies things a lot. |
| 806 |
* Think about it: it would be silly! |
| 807 |
*/ |
| 808 |
|
| 809 |
void |
| 810 |
Placur(line, col) |
| 811 |
int line, |
| 812 |
col; |
| 813 |
{ |
| 814 |
flushscreen(); |
| 815 |
cur_mov(line, col); |
| 816 |
CapCol = col; |
| 817 |
CapLine = line; |
| 818 |
} |
| 819 |
|
| 820 |
private jbool |
| 821 |
doing_so = NO, |
| 822 |
doing_us = NO; |
| 823 |
|
| 824 |
private void |
| 825 |
doattr(void) |
| 826 |
{ |
| 827 |
flushscreen(); |
| 828 |
setcolor((doing_so? Mlattr : Txattr) ^ (doing_us? Hlattr : 0)); |
| 829 |
} |
| 830 |
|
| 831 |
void |
| 832 |
SO_effect(jbool f) |
| 833 |
{ |
| 834 |
doing_so = f; |
| 835 |
doattr(); |
| 836 |
} |
| 837 |
|
| 838 |
void |
| 839 |
US_effect(jbool f) |
| 840 |
{ |
| 841 |
doing_us = f; |
| 842 |
doattr(); |
| 843 |
} |
| 844 |
|
| 845 |
int |
| 846 |
FatalErrorMessage(char *str) |
| 847 |
{ |
| 848 |
return MessageBox(NULL, str, NULL, MB_YESNO) == IDYES? 'y' : 'n'; |
| 849 |
} |
| 850 |
|
| 851 |
#endif /* WIN32 */ |