/
win32.c
497 lines (431 loc) · 9.56 KB
/
win32.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*_ win32.c Wed May 24 1995 */
/*
* The routines in this file provide support for computers with
* WIN32 console I/O support.
* It compiles into nothing if not a WIN32 device.
*/
#include "ed.h"
#if _WIN32
#include <windows.h>
#include <winuser.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <msmouse.h>
#include <disp.h>
#include <malloc.h>
#define BEL 0x07 /* BEL character. */
#define ESC 0x1B /* ESC character. */
extern int win32open(); /* Forward references. */
extern int win32close();
extern int ttgetc();
extern int ttputc(int c);
extern int win32beep();
static char set43 = FALSE; /* TRUE if we set to 43 line mode */
/* so we can set it back to 25 */
/* line mode upon exit */
static HANDLE hStdin; // console input handle
static DWORD fdwSaveOldMode;
static INPUT_RECORD lookaheadir;
static int lookahead; // !=0 if data in lookaheadir
/*
* Standard terminal interface dispatch table. Most of the fields point into
* "termio" code.
*/
typedef int (*TERMFP)();
TERM term = {
0,
0,
(TERMFP) win32open,
(TERMFP) win32close,
(TERMFP) ttgetc,
(TERMFP) disp_putc,
(TERMFP) disp_flush,
(TERMFP) disp_move, /* (row,col) */
(TERMFP) disp_eeol,
(TERMFP) disp_eeop,
(TERMFP) win32beep,
(TERMFP) disp_startstand,
(TERMFP) disp_endstand,
(TERMFP) NULL,
(TERMFP) NULL
};
win32open()
{
hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE)
{ printf("getstdhandle\n");
exit(EXIT_FAILURE);
}
if (!GetConsoleMode(hStdin,&fdwSaveOldMode))
{ printf("getconsolemode\n");
exit(EXIT_FAILURE);
}
if (!SetConsoleMode(hStdin,ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT))
{ printf("setconsolemode\n");
exit(EXIT_FAILURE);
}
disp_open();
disp_setcursortype(DISP_CURSORBLOCK);
term.t_ncol = disp_numcols;
term.t_nrow = disp_numrows;
}
win32close()
{
if (set43)
{ disp_reset43(); /* MS-DOS can't deal with 43 line mode */
disp_move(0,0);
disp_eeop();
}
disp_close();
if (!SetConsoleMode(hStdin,fdwSaveOldMode))
{ printf("restore console mode\n");
exit(EXIT_FAILURE);
}
}
/****************************
* Toggle 43 line mode.
*/
win32toggle43()
{
#if 0
return 0;
#else
WINDOW *wp;
int row;
L1:
if (disp_ega)
{
if (disp_numrows == 25)
{ set43 = TRUE;
vttidy(); /* release old video buffers */
disp_set43();
}
else
{ vttidy();
disp_reset43();
}
term.t_nrow = disp_numrows;
/* BUG: vtinit() can run out of memory, in which case the */
/* program will gracelessly abort. */
vtinit(); /* initialize display buffers */
/* The windows all need to be readjusted */
row = 0;
for (wp = wheadp; wp; wp = wp->w_wndp) /* for each window */
{ wp->w_flag = WFMODE | WFFORCE; /* force reformatting */
if (row >= term.t_nrow - 2) /* if can't readjust window */
goto L1; /* switch it back */
wp->w_toprow = row;
if (row + wp->w_ntrows > term.t_nrow - 2 ||
!wp->w_wndp && row + wp->w_ntrows != term.t_nrow - 2)
wp->w_ntrows = term.t_nrow - 2 - row;
row += wp->w_ntrows + 1; /* starting row of next window */
}
mlerase();
window_refresh(FALSE,1); /* and redraw the display */
}
return disp_ega; /* FALSE if we failed */
#endif
}
/********************************************
*/
win32beep()
{
ttputc(BEL);
}
/********************************************
*/
updateline(int row,attchar_t *buffer,attchar_t *physical)
{
int col;
int numcols;
CHAR_INFO *psb;
CHAR_INFO sbbuf[256];
CHAR_INFO *sb;
COORD sbsize;
static COORD sbcoord;
SMALL_RECT sdrect;
sbsize.X = disp_numcols;
sbsize.Y = 1;
sbcoord.X = 0;
sbcoord.Y = 0;
sdrect.Left = 0;
sdrect.Top = row;
sdrect.Right = disp_numcols - 1;
sdrect.Bottom = row;
numcols = disp_numcols;
sb = sbbuf;
if (numcols > arraysize(sbbuf))
{
sb = (CHAR_INFO *)alloca(numcols * sizeof(CHAR_INFO));
}
for (col = 0; col < numcols; col++)
{
sb[col].Char.AsciiChar = buffer[col] & 0xFF;
sb[col].Attributes = (buffer[col] >> 8) & 0xFF;
//printf("col = %2d, x%2x, '%c'\n",col,sb[col].Char.AsciiChar,sb[col].Char.AsciiChar);
}
if (!WriteConsoleOutput((HANDLE)disp_state.handle,sb,sbsize,sbcoord,&sdrect))
{
// error
}
memcpy(physical,buffer,sizeof(buffer[0]) * disp_numcols);
}
/*********************************
*/
int __cdecl msm_init(void)
{
return GetSystemMetrics(SM_MOUSEPRESENT);
}
void __cdecl msm_term(void) { }
void __cdecl msm_showcursor(void) { }
void __cdecl msm_hidecursor(void) { }
static struct msm_status // current state of mouse
{
unsigned row;
unsigned col;
int buttons;
} mstat;
/*************************
* Fold MOUSE_EVENT into mstat.
*/
static void mstat_update(MOUSE_EVENT_RECORD *pme)
{
mstat.row = pme->dwMousePosition.Y;
mstat.col = pme->dwMousePosition.X;
mstat.buttons = pme->dwButtonState & 3;
}
int __cdecl msm_getstatus(unsigned *pcol,unsigned *prow)
{
INPUT_RECORD buf;
DWORD cNumRead;
if (lookahead)
{ buf = lookaheadir;
cNumRead = 1;
}
else if (!PeekConsoleInput(hStdin,&buf,1,&cNumRead))
goto Lret;
if (cNumRead)
switch (buf.EventType)
{
case MOUSE_EVENT:
mstat_update(&buf.Event.MouseEvent);
default:
Ldiscard:
if (lookahead)
lookahead = 0;
else
ReadConsoleInput(hStdin,&buf,1,&cNumRead); // discard
break;
case KEY_EVENT:
if (mstat.buttons & 3)
goto Ldiscard;
break;
}
Lret:
*prow = mstat.row;
*pcol = mstat.col;
return mstat.buttons;
}
/*************************************
* Translate key from WIN32 to IBM PC style.
* Returns:
* 0 if ignore it
*/
static unsigned win32_keytran(KEY_EVENT_RECORD *pkey)
{ unsigned c;
c = 0;
if (!pkey->bKeyDown)
goto Lret; // ignore button up events
c = pkey->uChar.AsciiChar & 0xFF;
if (c == 0)
{
switch (pkey->wVirtualScanCode)
{
case 0x1D: // Ctrl
case 0x38: // Alt
case 0x2A: // Left Shift
case 0x36: // Right Shift
break; // ignore
default:
c = (pkey->wVirtualScanCode << 8) & 0xFF00;
if (pkey->dwControlKeyState & (RIGHT_CTRL_PRESSED | LEFT_CTRL_PRESSED))
{
switch (c)
{ case 0x4700: c = 0x7700; break; // Home
case 0x4F00: c = 0x7500; break; // End
case 0x4900: c = 0x8400; break; // PgUp
case 0x5100: c = 0x7600; break; // PgDn
}
}
break;
}
}
else if (pkey->dwControlKeyState & (RIGHT_ALT_PRESSED | LEFT_ALT_PRESSED))
{
c = (pkey->wVirtualScanCode << 8) & 0xFF00;
}
Lret:
return c;
}
/*************************************
* Wait for any input (yield to other processes).
*/
ttyield()
{
if (!lookahead)
{
DWORD cNumRead;
if (!ReadConsoleInput(hStdin,&lookaheadir,1,&cNumRead))
{ printf("readconsoleinput\n");
goto Lret;
}
}
lookahead = 1;
Lret: ;
}
/*************************************
*/
int ttkeysininput()
{
INPUT_RECORD buf;
DWORD cNumRead;
if (lookahead)
{ buf = lookaheadir;
cNumRead = 1;
}
else if (!PeekConsoleInput(hStdin,&buf,1,&cNumRead))
goto Lret;
if (cNumRead)
{
switch (buf.EventType)
{
case MOUSE_EVENT:
mstat_update(&buf.Event.MouseEvent);
default:
Ldiscard:
if (lookahead)
lookahead = 0;
else
ReadConsoleInput(hStdin,&buf,1,&cNumRead); // discard
cNumRead = 0;
break;
case KEY_EVENT:
if (!win32_keytran(&buf.Event.KeyEvent))
goto Ldiscard;
break;
}
}
Lret:
return cNumRead != 0;
}
/*************************************
*/
int ttputc(int c)
{
disp_putc(c);
}
/*************************************
*/
int ttgetc()
{
INPUT_RECORD buf;
DWORD cNumRead;
int c;
while (1)
{
if (lookahead)
{ buf = lookaheadir;
lookahead = 0;
}
else if (!ReadConsoleInput(hStdin,&buf,1,&cNumRead))
{ c = 3; // ^C
goto Lret;
}
switch (buf.EventType)
{
case MOUSE_EVENT:
mstat_update(&buf.Event.MouseEvent);
continue;
default:
continue; // ignore
case KEY_EVENT:
c = win32_keytran(&buf.Event.KeyEvent);
if (!c)
continue;
goto Lret;
}
}
Lret:
return c;
}
void setClipboard(char *s, size_t len)
{
if (OpenClipboard(NULL))
{
EmptyClipboard();
HGLOBAL hmem = GlobalAlloc(GMEM_MOVEABLE, (len + 1) * sizeof(char));
if (hmem)
{
char *p = (char *)GlobalLock(hmem);
memcpy(p, s, len * sizeof(char));
p[len] = 0;
GlobalUnlock(hmem);
SetClipboardData(CF_TEXT, hmem);
}
CloseClipboard();
}
}
char *getClipboard()
{
char *s = NULL;
if (IsClipboardFormatAvailable(CF_TEXT) &&
OpenClipboard(NULL))
{
HANDLE h = GetClipboardData(CF_TEXT); // CF_UNICODETEXT is UTF-16
if (h)
{
char *p = (char*)GlobalLock(h);
if (p)
{
size_t len = strlen(p);
s = (char *)malloc(len + 1);
if (s)
memcpy(s, p, len + 1);
}
GlobalUnlock(h);
}
CloseClipboard();
}
return s;
}
/***********************
* Open browser on help file.
*/
int help(f, n)
{
char resolved_name[MAX_PATH + 1];
if (GetModuleFileNameA(NULL, resolved_name, MAX_PATH + 1))
{
size_t len = strlen(resolved_name);
size_t i;
for (i = len; i; --i)
{
if (resolved_name[i] == '/' ||
resolved_name[i] == '\\' ||
resolved_name[i] == ':')
{
++i;
break;
}
}
static char doc[] = "me.html";
if (i + sizeof(doc) <= MAX_PATH)
{
strcpy(resolved_name + i, doc);
browse(resolved_name);
}
}
return FALSE;
}
#endif /* _WIN32 */