1 | /* con_os2.cpp
|
---|
2 | *
|
---|
3 | * Copyright (c) 2008, eFTE SF Group (see AUTHORS file)
|
---|
4 | * Copyright (c) 1994-1998, Marko Macek
|
---|
5 | *
|
---|
6 | * You may distribute under the terms of either the GNU General Public
|
---|
7 | * License or the Artistic License, as specified in the README file.
|
---|
8 | *
|
---|
9 | */
|
---|
10 |
|
---|
11 | // include
|
---|
12 |
|
---|
13 | #include <stdio.h>
|
---|
14 | #include <stdlib.h>
|
---|
15 | #include <string.h>
|
---|
16 | #include <assert.h>
|
---|
17 | #include <signal.h>
|
---|
18 |
|
---|
19 | #include "sysdep.h"
|
---|
20 | #include "console.h"
|
---|
21 | #include "gui.h"
|
---|
22 |
|
---|
23 | extern int ShowVScroll;
|
---|
24 |
|
---|
25 | #define INCL_WIN
|
---|
26 | #define INCL_SUB
|
---|
27 | #define INCL_KBD
|
---|
28 | #define INCL_VIO
|
---|
29 | #define INCL_MOU
|
---|
30 | #define INCL_BASE
|
---|
31 | #define INCL_DOS
|
---|
32 | #define INCL_DOSDEVIOCTL
|
---|
33 | #define INCL_BASE
|
---|
34 | #define INCL_DOSEXCEPTIONS
|
---|
35 |
|
---|
36 | #include <stdlib.h>
|
---|
37 | #include <process.h>
|
---|
38 |
|
---|
39 | #ifndef OS2_INCLUDED
|
---|
40 | #include <os2.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | #define MAX_PIPES 4
|
---|
44 | #define PIPE_BUFLEN 4096
|
---|
45 |
|
---|
46 | typedef struct
|
---|
47 | {
|
---|
48 | int used;
|
---|
49 | int id;
|
---|
50 | int reading, stopped;
|
---|
51 | TID tid;
|
---|
52 | HMTX Access;
|
---|
53 | HEV ResumeRead;
|
---|
54 | HEV NewData;
|
---|
55 | char *buffer;
|
---|
56 | int buflen;
|
---|
57 | int bufused;
|
---|
58 | int bufpos;
|
---|
59 | EModel *notify;
|
---|
60 | char *Command;
|
---|
61 | int RetCode;
|
---|
62 | int DoTerm;
|
---|
63 | }
|
---|
64 | GPipe;
|
---|
65 |
|
---|
66 | static GPipe Pipes[MAX_PIPES] = {
|
---|
67 | {0}, {0}, {0}, {0}
|
---|
68 | };
|
---|
69 |
|
---|
70 | static long MouseAutoDelay = 400;
|
---|
71 | static long MouseAutoRepeat = 5;
|
---|
72 | static long MouseMultiClick = 300;
|
---|
73 |
|
---|
74 | static int Initialized = 0;
|
---|
75 | static int MousePresent = 0;
|
---|
76 | static int CursorVisible = 1; /* 1 means visible */
|
---|
77 | static int MouseVisible = 0; /* 0 means hidden */
|
---|
78 | static TEvent MouseEv = { evNone };
|
---|
79 | static TEvent EventBuf = { evNone };
|
---|
80 | static HMOU MouseHandle = 0;
|
---|
81 | static KBDINFO SaveKbdState;
|
---|
82 |
|
---|
83 | // misc
|
---|
84 |
|
---|
85 | static void DrawCursor(int Show)
|
---|
86 | {
|
---|
87 | VIOCURSORINFO vci;
|
---|
88 |
|
---|
89 | VioGetCurType(&vci, 0);
|
---|
90 | if (Show == 1)
|
---|
91 | vci.attr = 1;
|
---|
92 | else
|
---|
93 | vci.attr = (SHORT) - 1;
|
---|
94 | VioSetCurType(&vci, 0);
|
---|
95 | }
|
---|
96 |
|
---|
97 | static void DrawMouse(int Show)
|
---|
98 | {
|
---|
99 | if (!MousePresent)
|
---|
100 | return;
|
---|
101 | if (Show == 1) {
|
---|
102 | MouDrawPtr(MouseHandle);
|
---|
103 | }
|
---|
104 | else {
|
---|
105 | NOPTRRECT npr;
|
---|
106 | int W, H;
|
---|
107 |
|
---|
108 | npr.row = 0;
|
---|
109 | npr.col = 0;
|
---|
110 | ConQuerySize(&W, &H);
|
---|
111 | npr.cCol = (USHORT) (W - 1);
|
---|
112 | npr.cRow = (USHORT) (H - 1);
|
---|
113 | MouRemovePtr(&npr, MouseHandle);
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | static struct
|
---|
118 | { // TransCharScan
|
---|
119 | USHORT CharScan;
|
---|
120 | TKeyCode KeyCode;
|
---|
121 | }
|
---|
122 | TransCharScan[] =
|
---|
123 | {
|
---|
124 | {
|
---|
125 | 0x0100, kbEsc}
|
---|
126 | , {
|
---|
127 | 0x011B, kbEsc}
|
---|
128 | , {
|
---|
129 | 0x1C0D, kbEnter}
|
---|
130 | , {
|
---|
131 | 0x1C0A, kbEnter}
|
---|
132 | , {
|
---|
133 | 0x1C00, kbEnter}
|
---|
134 | , {
|
---|
135 | 0xE00D, kbEnter | kfGray}
|
---|
136 | , {
|
---|
137 | 0xA600, kbEnter | kfGray}
|
---|
138 | , {
|
---|
139 | 0xE00A, kbEnter | kfGray}
|
---|
140 | , {
|
---|
141 | 0x0E08, kbBackSp}
|
---|
142 | , {
|
---|
143 | 0x0E7F, kbBackSp}
|
---|
144 | , {
|
---|
145 | 0x0E00, kbBackSp}
|
---|
146 | , {
|
---|
147 | 0x0F09, kbTab}
|
---|
148 | , {
|
---|
149 | 0x9400, kbTab}
|
---|
150 | , {
|
---|
151 | 0xA500, kbTab}
|
---|
152 | , {
|
---|
153 | 0x0F00, kbTab}
|
---|
154 | , {
|
---|
155 | 0x4E00, '+' | kfGray}
|
---|
156 | , {
|
---|
157 | 0x9000, '+' | kfGray}
|
---|
158 | , {
|
---|
159 | 0x4E2B, '+' | kfGray}
|
---|
160 | , {
|
---|
161 | 0x4A00, '-' | kfGray}
|
---|
162 | , {
|
---|
163 | 0x8E00, '-' | kfGray}
|
---|
164 | , {
|
---|
165 | 0x4A2D, '-' | kfGray}
|
---|
166 | , {
|
---|
167 | 0x3700, '*' | kfGray}
|
---|
168 | , {
|
---|
169 | 0x9600, '*' | kfGray}
|
---|
170 | , {
|
---|
171 | 0x372A, '*' | kfGray}
|
---|
172 | , {
|
---|
173 | 0xE02F, '/' | kfGray}
|
---|
174 | , {
|
---|
175 | 0xA400, '/' | kfGray}
|
---|
176 | , {
|
---|
177 | 0x9500, '/' | kfGray}
|
---|
178 | , {
|
---|
179 | 0x0300, 0}
|
---|
180 | };
|
---|
181 |
|
---|
182 | static struct
|
---|
183 | { // TransScan
|
---|
184 | int ScanCode;
|
---|
185 | TKeyCode KeyCode;
|
---|
186 | }
|
---|
187 | TransScan[] =
|
---|
188 | {
|
---|
189 | {
|
---|
190 | 0x78, '1'}
|
---|
191 | , {
|
---|
192 | 0x79, '2'}
|
---|
193 | , {
|
---|
194 | 0x7A, '3'}
|
---|
195 | , {
|
---|
196 | 0x7B, '4'}
|
---|
197 | , {
|
---|
198 | 0x7C, '5'}
|
---|
199 | , {
|
---|
200 | 0x7D, '6'}
|
---|
201 | , {
|
---|
202 | 0x7E, '7'}
|
---|
203 | , {
|
---|
204 | 0x7F, '8'}
|
---|
205 | , {
|
---|
206 | 0x80, '9'}
|
---|
207 | , {
|
---|
208 | 0x81, '0'}
|
---|
209 | , {
|
---|
210 | 0x10, 'Q'}
|
---|
211 | , {
|
---|
212 | 0x11, 'W'}
|
---|
213 | , {
|
---|
214 | 0x12, 'E'}
|
---|
215 | , {
|
---|
216 | 0x13, 'R'}
|
---|
217 | , {
|
---|
218 | 0x14, 'T'}
|
---|
219 | , {
|
---|
220 | 0x15, 'Y'}
|
---|
221 | , {
|
---|
222 | 0x16, 'U'}
|
---|
223 | , {
|
---|
224 | 0x17, 'I'}
|
---|
225 | , {
|
---|
226 | 0x18, 'O'}
|
---|
227 | , {
|
---|
228 | 0x19, 'P'}
|
---|
229 | , {
|
---|
230 | 0x1E, 'A'}
|
---|
231 | , {
|
---|
232 | 0x1F, 'S'}
|
---|
233 | , {
|
---|
234 | 0x20, 'D'}
|
---|
235 | , {
|
---|
236 | 0x21, 'F'}
|
---|
237 | , {
|
---|
238 | 0x22, 'G'}
|
---|
239 | , {
|
---|
240 | 0x23, 'H'}
|
---|
241 | , {
|
---|
242 | 0x24, 'J'}
|
---|
243 | , {
|
---|
244 | 0x25, 'K'}
|
---|
245 | , {
|
---|
246 | 0x26, 'L'}
|
---|
247 | , {
|
---|
248 | 0x2C, 'Z'}
|
---|
249 | , {
|
---|
250 | 0x2D, 'X'}
|
---|
251 | , {
|
---|
252 | 0x2E, 'C'}
|
---|
253 | , {
|
---|
254 | 0x2F, 'V'}
|
---|
255 | , {
|
---|
256 | 0x30, 'B'}
|
---|
257 | , {
|
---|
258 | 0x31, 'N'}
|
---|
259 | , {
|
---|
260 | 0x32, 'M'}
|
---|
261 | , {
|
---|
262 | 0x29, '`'}
|
---|
263 | , {
|
---|
264 | 0x82, '-'}
|
---|
265 | , {
|
---|
266 | 0x83, '='}
|
---|
267 | , {
|
---|
268 | 0x2B, '\\'}
|
---|
269 | , {
|
---|
270 | 0x1A, '['}
|
---|
271 | , {
|
---|
272 | 0x1B, ']'}
|
---|
273 | , {
|
---|
274 | 0x27, ';'}
|
---|
275 | , {
|
---|
276 | 0x28, '\''}
|
---|
277 | , {
|
---|
278 | 0x33, ','}
|
---|
279 | , {
|
---|
280 | 0x34, '.'}
|
---|
281 | , {
|
---|
282 | 0x35, '/'}
|
---|
283 | , {
|
---|
284 | 0x37, '*'}
|
---|
285 | , {
|
---|
286 | 0x4E, '+'}
|
---|
287 | , {
|
---|
288 | 0x4A, '-'}
|
---|
289 | , {
|
---|
290 | 0x3B, kbF1}
|
---|
291 | , {
|
---|
292 | 0x3C, kbF2}
|
---|
293 | , {
|
---|
294 | 0x3D, kbF3}
|
---|
295 | , {
|
---|
296 | 0x3E, kbF4}
|
---|
297 | , {
|
---|
298 | 0x3F, kbF5}
|
---|
299 | , {
|
---|
300 | 0x40, kbF6}
|
---|
301 | , {
|
---|
302 | 0x41, kbF7}
|
---|
303 | , {
|
---|
304 | 0x42, kbF8}
|
---|
305 | , {
|
---|
306 | 0x43, kbF9}
|
---|
307 | , {
|
---|
308 | 0x44, kbF10}
|
---|
309 | , {
|
---|
310 | 0x85, kbF11}
|
---|
311 | , {
|
---|
312 | 0x86, kbF12}
|
---|
313 | , {
|
---|
314 | 0x54, kbF1}
|
---|
315 | , {
|
---|
316 | 0x55, kbF2}
|
---|
317 | , {
|
---|
318 | 0x56, kbF3}
|
---|
319 | , {
|
---|
320 | 0x57, kbF4}
|
---|
321 | , {
|
---|
322 | 0x58, kbF5}
|
---|
323 | , {
|
---|
324 | 0x59, kbF6}
|
---|
325 | , {
|
---|
326 | 0x5A, kbF7}
|
---|
327 | , {
|
---|
328 | 0x5B, kbF8}
|
---|
329 | , {
|
---|
330 | 0x5C, kbF9}
|
---|
331 | , {
|
---|
332 | 0x5D, kbF10}
|
---|
333 | , {
|
---|
334 | 0x87, kbF11}
|
---|
335 | , {
|
---|
336 | 0x88, kbF12}
|
---|
337 | , {
|
---|
338 | 0x5E, kbF1}
|
---|
339 | , {
|
---|
340 | 0x5F, kbF2}
|
---|
341 | , {
|
---|
342 | 0x60, kbF3}
|
---|
343 | , {
|
---|
344 | 0x61, kbF4}
|
---|
345 | , {
|
---|
346 | 0x62, kbF5}
|
---|
347 | , {
|
---|
348 | 0x63, kbF6}
|
---|
349 | , {
|
---|
350 | 0x64, kbF7}
|
---|
351 | , {
|
---|
352 | 0x65, kbF8}
|
---|
353 | , {
|
---|
354 | 0x66, kbF9}
|
---|
355 | , {
|
---|
356 | 0x67, kbF10}
|
---|
357 | , {
|
---|
358 | 0x89, kbF11}
|
---|
359 | , {
|
---|
360 | 0x8A, kbF12}
|
---|
361 | , {
|
---|
362 | 0x68, kbF1}
|
---|
363 | , {
|
---|
364 | 0x69, kbF2}
|
---|
365 | , {
|
---|
366 | 0x6A, kbF3}
|
---|
367 | , {
|
---|
368 | 0x6B, kbF4}
|
---|
369 | , {
|
---|
370 | 0x6C, kbF5}
|
---|
371 | , {
|
---|
372 | 0x6D, kbF6}
|
---|
373 | , {
|
---|
374 | 0x6E, kbF7}
|
---|
375 | , {
|
---|
376 | 0x6F, kbF8}
|
---|
377 | , {
|
---|
378 | 0x70, kbF9}
|
---|
379 | , {
|
---|
380 | 0x71, kbF10}
|
---|
381 | , {
|
---|
382 | 0x8B, kbF11}
|
---|
383 | , {
|
---|
384 | 0x8C, kbF12}
|
---|
385 | , {
|
---|
386 | 0x47, kbHome}
|
---|
387 | , {
|
---|
388 | 0x48, kbUp}
|
---|
389 | , {
|
---|
390 | 0x49, kbPgUp}
|
---|
391 | , {
|
---|
392 | 0x4B, kbLeft}
|
---|
393 | , {
|
---|
394 | 0x4C, kbCenter}
|
---|
395 | , {
|
---|
396 | 0x4D, kbRight}
|
---|
397 | , {
|
---|
398 | 0x4F, kbEnd}
|
---|
399 | , {
|
---|
400 | 0x50, kbDown}
|
---|
401 | , {
|
---|
402 | 0x51, kbPgDn}
|
---|
403 | , {
|
---|
404 | 0x52, kbIns}
|
---|
405 | , {
|
---|
406 | 0x53, kbDel}
|
---|
407 | , {
|
---|
408 | 0x77, kbHome}
|
---|
409 | , {
|
---|
410 | 0x8D, kbUp}
|
---|
411 | , {
|
---|
412 | 0x84, kbPgUp}
|
---|
413 | , {
|
---|
414 | 0x73, kbLeft}
|
---|
415 | , {
|
---|
416 | 0x74, kbRight}
|
---|
417 | , {
|
---|
418 | 0x75, kbEnd}
|
---|
419 | , {
|
---|
420 | 0x91, kbDown}
|
---|
421 | , {
|
---|
422 | 0x76, kbPgDn}
|
---|
423 | , {
|
---|
424 | 0x92, kbIns}
|
---|
425 | , {
|
---|
426 | 0x93, kbDel}
|
---|
427 | , {
|
---|
428 | 0x97, kbHome | kfGray}
|
---|
429 | , {
|
---|
430 | 0x98, kbUp | kfGray}
|
---|
431 | , {
|
---|
432 | 0x99, kbPgUp | kfGray}
|
---|
433 | , {
|
---|
434 | 0x9B, kbLeft | kfGray}
|
---|
435 | , {
|
---|
436 | 0x9D, kbRight | kfGray}
|
---|
437 | , {
|
---|
438 | 0x9F, kbEnd | kfGray}
|
---|
439 | , {
|
---|
440 | 0xA0, kbDown | kfGray}
|
---|
441 | , {
|
---|
442 | 0xA1, kbPgDn | kfGray}
|
---|
443 | , {
|
---|
444 | 0xA2, kbIns | kfGray}
|
---|
445 | , {
|
---|
446 | 0xA3, kbDel | kfGray}
|
---|
447 | };
|
---|
448 |
|
---|
449 | int ReadKbdEvent(TEvent * Event, int Wait)
|
---|
450 | {
|
---|
451 | KBDKEYINFO ki;
|
---|
452 | UCHAR CharCode, ScanCode;
|
---|
453 | ULONG KeyCode, KeyFlags;
|
---|
454 | USHORT CharScan, Flags;
|
---|
455 | static USHORT PrevFlags = 0;
|
---|
456 | unsigned int I;
|
---|
457 |
|
---|
458 | Event->What = evNone;
|
---|
459 | KbdCharIn(&ki, IO_NOWAIT, 0);
|
---|
460 | if (!(ki.fbStatus & 0x40))
|
---|
461 | return 0;
|
---|
462 |
|
---|
463 | Event->What = evKeyDown;
|
---|
464 |
|
---|
465 | CharCode = ki.chChar;
|
---|
466 | ScanCode = ki.chScan;
|
---|
467 | CharScan = (USHORT) ((((USHORT) ScanCode) << 8) | ((USHORT) CharCode));
|
---|
468 | Flags = ki.fsState;
|
---|
469 | KeyCode = 0;
|
---|
470 | KeyFlags = 0;
|
---|
471 |
|
---|
472 | /* printf("Key: %X %X %X %X %X \n", (unsigned long) ki.bNlsShift, (unsigned long) ki.fbStatus, (unsigned long) Flags, (unsigned long) CharCode, (unsigned long) ScanCode); */
|
---|
473 |
|
---|
474 | if ((Flags & (LEFTSHIFT | RIGHTSHIFT)) != 0)
|
---|
475 | KeyFlags |= kfShift;
|
---|
476 | if ((Flags & (LEFTCONTROL | RIGHTCONTROL)) != 0)
|
---|
477 | KeyFlags |= kfCtrl;
|
---|
478 |
|
---|
479 | /* cpCount = sizeof(cpList); */
|
---|
480 | /* rc = DosQueryCp(sizeof(cpList), cpList, &cpCount); // get active code page */
|
---|
481 | if (CharCode != 0) {
|
---|
482 | if ((Flags & (LEFTALT)) != 0)
|
---|
483 | KeyFlags |= kfAlt;
|
---|
484 | }
|
---|
485 | else {
|
---|
486 | if ((Flags & (LEFTALT | RIGHTALT)) != 0)
|
---|
487 | KeyFlags |= kfAlt;
|
---|
488 | }
|
---|
489 | /* if (rc != 0) printf("rc = %d\n", rc); */
|
---|
490 |
|
---|
491 | if (CharScan == 0) { /* shift/alt/ctrl/caps/scroll/num */
|
---|
492 |
|
---|
493 | }
|
---|
494 | else if (ScanCode == 0) { /* alt numeric */
|
---|
495 | KeyCode = CharCode;
|
---|
496 | KeyFlags |= kfAltXXX;
|
---|
497 | }
|
---|
498 | else { /* now check special combinations */
|
---|
499 | for (I = 0; I < sizeof(TransCharScan) / sizeof(TransCharScan[0]); I++)
|
---|
500 | if (TransCharScan[I].CharScan == CharScan) {
|
---|
501 | KeyCode = TransCharScan[I].KeyCode;
|
---|
502 | break;
|
---|
503 | }
|
---|
504 | if (KeyCode == 0) {
|
---|
505 | if ((CharCode == 0) || (CharCode == 0xE0)) {
|
---|
506 | if (CharCode == 0xE0)
|
---|
507 | KeyFlags |= kfGray;
|
---|
508 | for (I = 0; I < sizeof(TransScan) / sizeof(TransScan[0]); I++)
|
---|
509 | if (TransScan[I].ScanCode == ScanCode) {
|
---|
510 | KeyCode = TransScan[I].KeyCode;
|
---|
511 | break;
|
---|
512 | }
|
---|
513 | }
|
---|
514 | else {
|
---|
515 | if (CharCode < 32)
|
---|
516 | if (KeyFlags & kfCtrl)
|
---|
517 | CharCode += 64;
|
---|
518 | KeyCode = CharCode;
|
---|
519 | }
|
---|
520 | }
|
---|
521 | }
|
---|
522 | Event->Key.Code = KeyCode | KeyFlags;
|
---|
523 | PrevFlags = Flags;
|
---|
524 | return 1;
|
---|
525 | }
|
---|
526 |
|
---|
527 | #define TM_DIFF(x,y) ((long)(((long)(x) < (long)(y)) ? ((long)(y) - (long)(x)) : ((long)(x) - (long)(y))))
|
---|
528 |
|
---|
529 | int ReadMouseEvent(TEvent * Event, ULONG EventMask)
|
---|
530 | {
|
---|
531 | static unsigned short PrevState = 0;
|
---|
532 | static unsigned short PrevButtons = 0;
|
---|
533 | static TEvent LastMouseEvent = { evNone };
|
---|
534 | static ULONG LastEventTime = 0;
|
---|
535 | static ULONG LastClick = 0;
|
---|
536 | static ULONG LastClickTime = 0;
|
---|
537 | static ULONG LastClickCount = 0;
|
---|
538 | MOUEVENTINFO mi;
|
---|
539 | unsigned short Buttons, State, Btn;
|
---|
540 | USHORT fWait = MOU_NOWAIT;
|
---|
541 | MOUQUEINFO mq;
|
---|
542 | ULONG CurTime;
|
---|
543 |
|
---|
544 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &CurTime, 4);
|
---|
545 |
|
---|
546 | Event->What = evNone;
|
---|
547 | MouGetNumQueEl(&mq, MouseHandle);
|
---|
548 | if (mq.cEvents == 0) {
|
---|
549 | if (LastMouseEvent.What == evMouseAuto && (EventMask & evMouseAuto)) {
|
---|
550 | if (TM_DIFF(CurTime, LastEventTime) >= MouseAutoRepeat) {
|
---|
551 | *Event = LastMouseEvent;
|
---|
552 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &LastEventTime,
|
---|
553 | 4);
|
---|
554 | return 1;
|
---|
555 | }
|
---|
556 | }
|
---|
557 | if ((LastMouseEvent.What == evMouseDown
|
---|
558 | || LastMouseEvent.What == evMouseMove)
|
---|
559 | && (LastMouseEvent.Mouse.Buttons)
|
---|
560 | && (EventMask & evMouseAuto)) {
|
---|
561 | if (TM_DIFF(CurTime, LastEventTime) >= MouseAutoDelay) {
|
---|
562 | LastMouseEvent.What = evMouseAuto;
|
---|
563 | *Event = LastMouseEvent;
|
---|
564 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &LastEventTime,
|
---|
565 | 4);
|
---|
566 | return 1;
|
---|
567 | }
|
---|
568 | }
|
---|
569 | return 0;
|
---|
570 | }
|
---|
571 |
|
---|
572 | if (MouReadEventQue(&mi, &fWait, MouseHandle) != 0)
|
---|
573 | return 0;
|
---|
574 | Event->Mouse.X = mi.col;
|
---|
575 | Event->Mouse.Y = mi.row;
|
---|
576 | State = mi.fs;
|
---|
577 | Btn = Buttons = (USHORT) (((State & (2 | 4)) ? 1 : 0) |
|
---|
578 | ((State & (8 | 16)) ? 2 : 0) | ((State & (32 | 64)) ? 4 : 0));
|
---|
579 | if (Buttons != PrevButtons) {
|
---|
580 | Buttons ^= PrevButtons;
|
---|
581 | if (PrevButtons & Buttons)
|
---|
582 | Event->What = evMouseUp;
|
---|
583 | else
|
---|
584 | Event->What = evMouseDown;
|
---|
585 | }
|
---|
586 | else {
|
---|
587 | Event->What = evMouseMove;
|
---|
588 | if (Event->Mouse.X == LastMouseEvent.Mouse.X &&
|
---|
589 | Event->Mouse.Y == LastMouseEvent.Mouse.Y)
|
---|
590 | return 0;
|
---|
591 | }
|
---|
592 | Event->Mouse.Buttons = Buttons;
|
---|
593 | Event->Mouse.Count = 1;
|
---|
594 | PrevState = State;
|
---|
595 | PrevButtons = Btn;
|
---|
596 |
|
---|
597 | if (Event->What == evMouseDown) {
|
---|
598 | if (LastClickCount) {
|
---|
599 | if (LastClick == Event->Mouse.Buttons) {
|
---|
600 | if (TM_DIFF(CurTime, LastClickTime) <= MouseMultiClick) {
|
---|
601 | Event->Mouse.Count = ++LastClickCount;
|
---|
602 | }
|
---|
603 | else {
|
---|
604 | LastClickCount = 0;
|
---|
605 | }
|
---|
606 | }
|
---|
607 | else {
|
---|
608 | LastClick = 0;
|
---|
609 | LastClickCount = 0;
|
---|
610 | LastClickTime = 0;
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | LastClick = Event->Mouse.Buttons;
|
---|
615 | if (LastClickCount == 0)
|
---|
616 | LastClickCount = 1;
|
---|
617 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &LastClickTime, 4);
|
---|
618 | }
|
---|
619 | /* if (Event->What == evMouseMove) {
|
---|
620 | LastClick = 0;
|
---|
621 | LastClickCount = 0;
|
---|
622 | LastClickTime = 0;
|
---|
623 | } */
|
---|
624 | {
|
---|
625 | KBDINFO ki;
|
---|
626 | USHORT Flags;
|
---|
627 | TKeyCode KeyFlags = 0;
|
---|
628 |
|
---|
629 | ki.cb = sizeof(ki);
|
---|
630 | KbdGetStatus(&ki, 0);
|
---|
631 | Flags = ki.fsState;
|
---|
632 |
|
---|
633 | if ((Flags & (LEFTSHIFT | RIGHTSHIFT)) != 0)
|
---|
634 | KeyFlags |= kfShift;
|
---|
635 | if ((Flags & (LEFTCONTROL | RIGHTCONTROL)) != 0)
|
---|
636 | KeyFlags |= kfCtrl;
|
---|
637 | if ((Flags & (LEFTALT | RIGHTALT)) != 0)
|
---|
638 | KeyFlags |= kfAlt;
|
---|
639 |
|
---|
640 | Event->Mouse.KeyMask = KeyFlags;
|
---|
641 | }
|
---|
642 |
|
---|
643 | LastMouseEvent = *Event;
|
---|
644 | DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &LastEventTime, 4);
|
---|
645 | return 1;
|
---|
646 | }
|
---|
647 |
|
---|
648 | int ConClear()
|
---|
649 | {
|
---|
650 | int W, H;
|
---|
651 | TDrawBuffer B;
|
---|
652 |
|
---|
653 | MoveChar(B, 0, ConMaxCols, ' ', 0x07, 1);
|
---|
654 | if ((ConQuerySize(&W, &H) == 0) && ConSetBox(0, 0, W, H, B[0]))
|
---|
655 | return 0;
|
---|
656 | return -1;
|
---|
657 | }
|
---|
658 |
|
---|
659 | int ConPutBox(int X, int Y, int W, int H, PCell Cell)
|
---|
660 | {
|
---|
661 | int I;
|
---|
662 | int MX, MY;
|
---|
663 | int MouseHidden = 0;
|
---|
664 | unsigned char *p = (unsigned char *)Cell;
|
---|
665 |
|
---|
666 | if (MouseVisible)
|
---|
667 | ConQueryMousePos(&MX, &MY);
|
---|
668 |
|
---|
669 | for (I = 0; I < H; I++) {
|
---|
670 | if (MouseVisible)
|
---|
671 | if (Y + I == MY)
|
---|
672 | if ((MX >= X) && (MX <= X + W)) {
|
---|
673 | DrawMouse(0);
|
---|
674 | MouseHidden = 1;
|
---|
675 | }
|
---|
676 | VioWrtCellStr((PCH) p, (USHORT) (W << 1), (USHORT) (Y + I),
|
---|
677 | (USHORT) X, 0);
|
---|
678 |
|
---|
679 | if (MouseHidden) {
|
---|
680 | DrawMouse(1);
|
---|
681 | MouseHidden = 0;
|
---|
682 | }
|
---|
683 | p += W << 1;
|
---|
684 | }
|
---|
685 | return 0;
|
---|
686 | }
|
---|
687 |
|
---|
688 | int ConGetBox(int X, int Y, int W, int H, PCell Cell)
|
---|
689 | {
|
---|
690 | int I;
|
---|
691 | int MX, MY;
|
---|
692 | int MouseHidden = 0;
|
---|
693 | USHORT WW = (USHORT) (W << 1);
|
---|
694 | unsigned char *p = (unsigned char *)Cell;
|
---|
695 |
|
---|
696 | if (MouseVisible)
|
---|
697 | ConQueryMousePos(&MX, &MY);
|
---|
698 |
|
---|
699 | for (I = 0; I < H; I++) {
|
---|
700 | if (MouseVisible)
|
---|
701 | if (Y + I == MY)
|
---|
702 | if (MX >= X && MX < X + W) {
|
---|
703 | DrawMouse(0);
|
---|
704 | MouseHidden = 1;
|
---|
705 | }
|
---|
706 | VioReadCellStr((PCH) p, &WW, (USHORT) (Y + I), (USHORT) X, 0);
|
---|
707 |
|
---|
708 | if (MouseHidden) {
|
---|
709 | DrawMouse(1);
|
---|
710 | MouseHidden = 0;
|
---|
711 | }
|
---|
712 | p += W << 1;
|
---|
713 | }
|
---|
714 | return 0;
|
---|
715 | }
|
---|
716 |
|
---|
717 | int ConPutLine(int X, int Y, int W, int H, PCell Cell)
|
---|
718 | {
|
---|
719 | int I;
|
---|
720 | int MX, MY;
|
---|
721 | int MouseHidden = 0;
|
---|
722 | unsigned char *p = (unsigned char *)Cell;
|
---|
723 |
|
---|
724 | if (MouseVisible)
|
---|
725 | ConQueryMousePos(&MX, &MY);
|
---|
726 |
|
---|
727 | for (I = 0; I < H; I++) {
|
---|
728 | if (MouseVisible)
|
---|
729 | if (Y + I == MY)
|
---|
730 | if (MX >= X && MX < X + W) {
|
---|
731 | DrawMouse(0);
|
---|
732 | MouseHidden = 1;
|
---|
733 | }
|
---|
734 | VioWrtCellStr((PCH) p, (USHORT) (W << 1), (USHORT) (Y + I),
|
---|
735 | (USHORT) X, 0);
|
---|
736 |
|
---|
737 | if (MouseHidden) {
|
---|
738 | DrawMouse(1);
|
---|
739 | MouseHidden = 0;
|
---|
740 | }
|
---|
741 | }
|
---|
742 | return 0;
|
---|
743 | }
|
---|
744 |
|
---|
745 | int ConSetBox(int X, int Y, int W, int H, TCell Cell)
|
---|
746 | {
|
---|
747 | int I;
|
---|
748 | int MX, MY;
|
---|
749 | int MouseHidden = 0;
|
---|
750 | unsigned char *p = (unsigned char *)&Cell;
|
---|
751 |
|
---|
752 | if (MouseVisible)
|
---|
753 | ConQueryMousePos(&MX, &MY);
|
---|
754 |
|
---|
755 | for (I = 0; I < H; I++) {
|
---|
756 | if (MouseVisible)
|
---|
757 | if (Y + I == MY)
|
---|
758 | if (MX >= X && MX < X + W) {
|
---|
759 | DrawMouse(0);
|
---|
760 | MouseHidden = 1;
|
---|
761 | }
|
---|
762 | VioWrtNCell(p, (USHORT) (W), (USHORT) (Y + I), (USHORT) X, 0);
|
---|
763 |
|
---|
764 | if (MouseHidden) {
|
---|
765 | DrawMouse(1);
|
---|
766 | MouseHidden = 0;
|
---|
767 | }
|
---|
768 | }
|
---|
769 | return 0;
|
---|
770 | }
|
---|
771 |
|
---|
772 | int ConScroll(int Way, int X, int Y, int W, int H, TAttr Fill, int Count)
|
---|
773 | {
|
---|
774 | int MX, MY;
|
---|
775 | int MouseHidden = 0;
|
---|
776 | TCell FillCell = (TCell) (Fill << 8);
|
---|
777 |
|
---|
778 | if (MousePresent && MouseVisible) {
|
---|
779 | ConQueryMousePos(&MX, &MY);
|
---|
780 | if (MX >= X && MX < X + W && MY >= Y && MY < Y + H) {
|
---|
781 | DrawMouse(0);
|
---|
782 | MouseHidden = 1;
|
---|
783 | }
|
---|
784 | }
|
---|
785 |
|
---|
786 | switch (Way) {
|
---|
787 | case csUp:
|
---|
788 | VioScrollUp((USHORT) Y, (USHORT) X, (USHORT) (Y + H - 1),
|
---|
789 | (USHORT) (X + W - 1), (USHORT) Count, (PBYTE) & FillCell,
|
---|
790 | 0);
|
---|
791 | break;
|
---|
792 | case csDown:
|
---|
793 | VioScrollDn((USHORT) Y, (USHORT) X, (USHORT) (Y + H - 1),
|
---|
794 | (USHORT) (X + W - 1), (USHORT) Count, (PBYTE) & FillCell,
|
---|
795 | 0);
|
---|
796 | break;
|
---|
797 | case csLeft:
|
---|
798 | VioScrollLf((USHORT) Y, (USHORT) X, (USHORT) (Y + H - 1),
|
---|
799 | (USHORT) (X + W - 1), (USHORT) Count, (PBYTE) & FillCell,
|
---|
800 | 0);
|
---|
801 | break;
|
---|
802 | case csRight:
|
---|
803 | VioScrollRt((USHORT) Y, (USHORT) X, (USHORT) (Y + H - 1),
|
---|
804 | (USHORT) (X + W - 1), (USHORT) Count, (PBYTE) & FillCell,
|
---|
805 | 0);
|
---|
806 | break;
|
---|
807 | }
|
---|
808 | if (MouseHidden)
|
---|
809 | DrawMouse(1);
|
---|
810 | return 0;
|
---|
811 | }
|
---|
812 |
|
---|
813 | int ConSetSize(int X, int Y)
|
---|
814 | {
|
---|
815 | VIOMODEINFO vmi;
|
---|
816 | int rc;
|
---|
817 |
|
---|
818 | assert(X <= ConMaxCols);
|
---|
819 | assert(Y <= ConMaxRows);
|
---|
820 |
|
---|
821 | vmi.cb = sizeof(VIOMODEINFO);
|
---|
822 | VioGetMode(&vmi, 0);
|
---|
823 | vmi.col = (USHORT) X;
|
---|
824 | vmi.row = (USHORT) Y;
|
---|
825 | vmi.cb = 2 + 1 + 1 + 2 + 2;
|
---|
826 | rc = VioSetMode(&vmi, 0);
|
---|
827 | if (rc == 0)
|
---|
828 | return 0;
|
---|
829 | return -1;
|
---|
830 | }
|
---|
831 |
|
---|
832 | int ConQuerySize(int *X, int *Y)
|
---|
833 | {
|
---|
834 | VIOMODEINFO vmi;
|
---|
835 |
|
---|
836 | vmi.cb = sizeof(VIOMODEINFO);
|
---|
837 | VioGetMode(&vmi, 0);
|
---|
838 | if (X)
|
---|
839 | *X = vmi.col;
|
---|
840 | if (Y)
|
---|
841 | *Y = vmi.row;
|
---|
842 | return 0;
|
---|
843 | }
|
---|
844 |
|
---|
845 | int ConSetCursorPos(int X, int Y)
|
---|
846 | {
|
---|
847 | VioSetCurPos((USHORT) Y, (USHORT) X, 0);
|
---|
848 | return 0;
|
---|
849 | }
|
---|
850 |
|
---|
851 | int ConQueryCursorPos(int *X, int *Y)
|
---|
852 | {
|
---|
853 | USHORT AX, AY;
|
---|
854 |
|
---|
855 | VioGetCurPos(&AY, &AX, 0);
|
---|
856 | if (X)
|
---|
857 | *X = AX;
|
---|
858 | if (Y)
|
---|
859 | *Y = AY;
|
---|
860 | return 0;
|
---|
861 | }
|
---|
862 |
|
---|
863 | int ConShowCursor()
|
---|
864 | {
|
---|
865 | CursorVisible = 1;
|
---|
866 | DrawCursor(1);
|
---|
867 | return 0;
|
---|
868 | }
|
---|
869 |
|
---|
870 | int ConHideCursor()
|
---|
871 | {
|
---|
872 | CursorVisible = 0;
|
---|
873 | DrawCursor(0);
|
---|
874 | return 0;
|
---|
875 | }
|
---|
876 |
|
---|
877 | void ConSetInsertState(bool insert)
|
---|
878 | {
|
---|
879 | }
|
---|
880 |
|
---|
881 | int ConSetMousePos(int X, int Y)
|
---|
882 | {
|
---|
883 | PTRLOC mp;
|
---|
884 |
|
---|
885 | if (!MousePresent)
|
---|
886 | return -1;
|
---|
887 | mp.col = (USHORT) X;
|
---|
888 | mp.row = (USHORT) Y;
|
---|
889 | MouSetPtrPos(&mp, MouseHandle);
|
---|
890 | return 0;
|
---|
891 | }
|
---|
892 |
|
---|
893 | int ConQueryMousePos(int *X, int *Y)
|
---|
894 | {
|
---|
895 | PTRLOC mp;
|
---|
896 |
|
---|
897 | if (!MousePresent)
|
---|
898 | return -1;
|
---|
899 | MouGetPtrPos(&mp, MouseHandle);
|
---|
900 | if (X)
|
---|
901 | *X = mp.col;
|
---|
902 | if (Y)
|
---|
903 | *Y = mp.row;
|
---|
904 | return 0;
|
---|
905 | }
|
---|
906 |
|
---|
907 | int ConShowMouse()
|
---|
908 | {
|
---|
909 | MouseVisible = 1;
|
---|
910 | if (!MousePresent)
|
---|
911 | return -1;
|
---|
912 | DrawMouse(1);
|
---|
913 | return 0;
|
---|
914 | }
|
---|
915 |
|
---|
916 | int ConHideMouse()
|
---|
917 | {
|
---|
918 | MouseVisible = 0;
|
---|
919 | if (!MousePresent)
|
---|
920 | return -1;
|
---|
921 | DrawMouse(0);
|
---|
922 | return 0;
|
---|
923 | }
|
---|
924 |
|
---|
925 | int ConMouseVisible()
|
---|
926 | {
|
---|
927 | return (MouseVisible == 1);
|
---|
928 | }
|
---|
929 |
|
---|
930 | int ConQueryMouseButtons(int *ButtonCount)
|
---|
931 | {
|
---|
932 | USHORT Count;
|
---|
933 |
|
---|
934 | if (MouGetNumButtons(&Count, MouseHandle) != 0)
|
---|
935 | return -1;
|
---|
936 | if (ButtonCount)
|
---|
937 | *ButtonCount = Count;
|
---|
938 | return 0;
|
---|
939 | }
|
---|
940 |
|
---|
941 | int ConInit(int XSize, int YSize)
|
---|
942 | {
|
---|
943 | USHORT MevMask = 127;
|
---|
944 |
|
---|
945 | if (Initialized)
|
---|
946 | return 0;
|
---|
947 |
|
---|
948 | EventBuf.What = evNone;
|
---|
949 | MousePresent = (MouOpen(NULL, &MouseHandle) == 0) ? 1 : 0;
|
---|
950 |
|
---|
951 | if (MousePresent)
|
---|
952 | MouSetEventMask(&MevMask, MouseHandle);
|
---|
953 |
|
---|
954 | memset(&SaveKbdState, 0, sizeof(SaveKbdState));
|
---|
955 | SaveKbdState.cb = sizeof(SaveKbdState);
|
---|
956 | APIRET16 s = KbdGetStatus(&SaveKbdState, 0);
|
---|
957 | assert(s == 0);
|
---|
958 | ConContinue();
|
---|
959 |
|
---|
960 | Initialized = 1;
|
---|
961 |
|
---|
962 | return 0;
|
---|
963 | }
|
---|
964 |
|
---|
965 | int ConDone()
|
---|
966 | {
|
---|
967 | return ConSuspend();
|
---|
968 | }
|
---|
969 |
|
---|
970 | int ConSuspend()
|
---|
971 | {
|
---|
972 | VIOINTENSITY vi;
|
---|
973 | static KBDINFO ki;
|
---|
974 |
|
---|
975 | vi.cb = 6;
|
---|
976 | vi.type = 2;
|
---|
977 | vi.fs = 0;
|
---|
978 | VioSetState(&vi, 0);
|
---|
979 |
|
---|
980 | ki = SaveKbdState;
|
---|
981 | ki.fsMask &= ~(KEYBOARD_ECHO_OFF | KEYBOARD_BINARY_MODE);
|
---|
982 | ki.fsMask |= (KEYBOARD_ECHO_ON | KEYBOARD_ASCII_MODE);
|
---|
983 | APIRET16 s = KbdSetStatus(&ki, 0);
|
---|
984 | assert(0 == s);
|
---|
985 |
|
---|
986 | ConHideMouse();
|
---|
987 |
|
---|
988 | signal(SIGBREAK, SIG_DFL);
|
---|
989 | signal(SIGINT, SIG_DFL);
|
---|
990 |
|
---|
991 | return 0;
|
---|
992 | }
|
---|
993 |
|
---|
994 | int ConContinue()
|
---|
995 | {
|
---|
996 | VIOINTENSITY vi;
|
---|
997 | static KBDINFO ki;
|
---|
998 |
|
---|
999 | signal(SIGBREAK, SIG_IGN);
|
---|
1000 | signal(SIGINT, SIG_IGN);
|
---|
1001 |
|
---|
1002 | ki = SaveKbdState;
|
---|
1003 | ki.fsMask &= ~(KEYBOARD_ECHO_ON | KEYBOARD_ASCII_MODE);
|
---|
1004 | ki.fsMask |= (KEYBOARD_ECHO_OFF | KEYBOARD_BINARY_MODE);
|
---|
1005 | APIRET16 s = KbdSetStatus(&ki, 0);
|
---|
1006 | assert(0 == s);
|
---|
1007 |
|
---|
1008 | vi.cb = 6;
|
---|
1009 | vi.type = 2;
|
---|
1010 | vi.fs = 1;
|
---|
1011 | VioSetState(&vi, 0);
|
---|
1012 | ConShowMouse();
|
---|
1013 | return 0;
|
---|
1014 | }
|
---|
1015 |
|
---|
1016 | int GetPipeEvent(TEvent * Event)
|
---|
1017 | {
|
---|
1018 | ULONG ulPostCount;
|
---|
1019 | int i;
|
---|
1020 |
|
---|
1021 | Event->What = evNone;
|
---|
1022 | for (i = 0; i < MAX_PIPES; i++) {
|
---|
1023 | if (Pipes[i].used == 0)
|
---|
1024 | continue;
|
---|
1025 | if (Pipes[i].notify == 0)
|
---|
1026 | continue;
|
---|
1027 | if (DosResetEventSem(Pipes[i].NewData, &ulPostCount) != 0)
|
---|
1028 | continue;
|
---|
1029 | if (ulPostCount > 0) {
|
---|
1030 | //fprintf(stderr, "Pipe New Data: %d\n", i);
|
---|
1031 | Event->What = evNotify;
|
---|
1032 | Event->Msg.View = 0;
|
---|
1033 | Event->Msg.Model = Pipes[i].notify;
|
---|
1034 | Event->Msg.Command = cmPipeRead;
|
---|
1035 | Event->Msg.Param1 = i;
|
---|
1036 | return 1;
|
---|
1037 | }
|
---|
1038 | }
|
---|
1039 | return 0;
|
---|
1040 | }
|
---|
1041 |
|
---|
1042 | int ConGetEvent(TEventMask EventMask, TEvent * Event, int WaitTime,
|
---|
1043 | int Delete)
|
---|
1044 | {
|
---|
1045 | KBDINFO ki;
|
---|
1046 |
|
---|
1047 | if (EventBuf.What != evNone) {
|
---|
1048 | *Event = EventBuf;
|
---|
1049 | if (Delete)
|
---|
1050 | EventBuf.What = evNone;
|
---|
1051 | return 0;
|
---|
1052 | }
|
---|
1053 | if (MouseEv.What != evNone) {
|
---|
1054 | *Event = MouseEv;
|
---|
1055 | if (Delete)
|
---|
1056 | MouseEv.What = evNone;
|
---|
1057 | return 0;
|
---|
1058 | }
|
---|
1059 | EventBuf.What = evNone;
|
---|
1060 | Event->What = evNone;
|
---|
1061 |
|
---|
1062 | ki = SaveKbdState;
|
---|
1063 | ki.fsMask &= ~(KEYBOARD_ECHO_ON | KEYBOARD_ASCII_MODE);
|
---|
1064 | ki.fsMask |= (KEYBOARD_ECHO_OFF | KEYBOARD_BINARY_MODE);
|
---|
1065 | APIRET16 s = KbdSetStatus(&ki, 0);
|
---|
1066 | assert(0 == s);
|
---|
1067 |
|
---|
1068 | while ((WaitTime == -1) || (WaitTime >= 0)) {
|
---|
1069 | if ((ReadKbdEvent(Event, WaitTime) == 1) && (EventMask & evKeyboard))
|
---|
1070 | break;
|
---|
1071 | else if (MousePresent && (ReadMouseEvent(Event, EventMask) == 1)
|
---|
1072 | && (EventMask & evMouse))
|
---|
1073 | break;
|
---|
1074 | else if (GetPipeEvent(Event) == 1)
|
---|
1075 | break;
|
---|
1076 |
|
---|
1077 | if (WaitTime == 0)
|
---|
1078 | return -1;
|
---|
1079 | DosSleep(5);
|
---|
1080 | if (WaitTime > 0) {
|
---|
1081 | WaitTime -= 5;
|
---|
1082 | if (WaitTime <= 0)
|
---|
1083 | return -1;
|
---|
1084 | }
|
---|
1085 | }
|
---|
1086 | if (Event->What != evNone) {
|
---|
1087 | if (Event->What == evMouseMove) {
|
---|
1088 | while (ReadMouseEvent(&MouseEv, EventMask) == 1) {
|
---|
1089 | if (MouseEv.What == evMouseMove) {
|
---|
1090 | *Event = MouseEv;
|
---|
1091 | MouseEv.What = evNone;
|
---|
1092 | }
|
---|
1093 | else
|
---|
1094 | break;
|
---|
1095 | }
|
---|
1096 | }
|
---|
1097 | EventBuf = *Event;
|
---|
1098 | if (Delete)
|
---|
1099 | EventBuf.What = evNone;
|
---|
1100 | return 0;
|
---|
1101 | }
|
---|
1102 | return -1;
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | static PCell SavedScreen = 0;
|
---|
1106 | static int SavedX, SavedY, SaveCursorPosX, SaveCursorPosY;
|
---|
1107 |
|
---|
1108 | int SaveScreen()
|
---|
1109 | {
|
---|
1110 | if (SavedScreen)
|
---|
1111 | free(SavedScreen);
|
---|
1112 |
|
---|
1113 | ConQuerySize(&SavedX, &SavedY);
|
---|
1114 |
|
---|
1115 | SavedScreen = (PCell) malloc(SavedX * SavedY * sizeof(PCell));
|
---|
1116 |
|
---|
1117 | if (SavedScreen)
|
---|
1118 | ConGetBox(0, 0, SavedX, SavedY, SavedScreen);
|
---|
1119 | ConQueryCursorPos(&SaveCursorPosX, &SaveCursorPosY);
|
---|
1120 | return 0;
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | int RestoreScreen()
|
---|
1124 | {
|
---|
1125 | if (SavedScreen) {
|
---|
1126 | ConPutBox(0, 0, SavedX, SavedY, SavedScreen);
|
---|
1127 | ConSetCursorPos(SaveCursorPosX, SaveCursorPosY);
|
---|
1128 | }
|
---|
1129 | return 1;
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | GUI::GUI(int &argc, char **argv, int XSize, int YSize)
|
---|
1133 | {
|
---|
1134 | fArgc = argc;
|
---|
1135 | fArgv = argv;
|
---|
1136 | ::ConInit(-1, -1);
|
---|
1137 | SaveScreen();
|
---|
1138 | ::ConSetSize(XSize, YSize);
|
---|
1139 | gui = this;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | GUI::~GUI()
|
---|
1143 | {
|
---|
1144 | RestoreScreen();
|
---|
1145 |
|
---|
1146 | if (SavedScreen)
|
---|
1147 | free(SavedScreen);
|
---|
1148 |
|
---|
1149 | ::ConDone();
|
---|
1150 | gui = 0;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | int GUI::ConSuspend(void)
|
---|
1154 | {
|
---|
1155 | RestoreScreen();
|
---|
1156 | return::ConSuspend();
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | int GUI::ConContinue(void)
|
---|
1160 | {
|
---|
1161 | SaveScreen();
|
---|
1162 | return::ConContinue();
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | int GUI::ShowEntryScreen()
|
---|
1166 | {
|
---|
1167 | TEvent E;
|
---|
1168 |
|
---|
1169 | ConHideMouse();
|
---|
1170 | RestoreScreen();
|
---|
1171 | do {
|
---|
1172 | gui->ConGetEvent(evKeyDown, &E, -1, 1, 0);
|
---|
1173 | } while (E.What != evKeyDown);
|
---|
1174 | ConShowMouse();
|
---|
1175 | if (frames)
|
---|
1176 | frames->Repaint();
|
---|
1177 | return 1;
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | static int CreatePipeChild(PID & pid, HPIPE & hfPipe, char *Command)
|
---|
1181 | {
|
---|
1182 | static int PCount = 0;
|
---|
1183 | char szPipe[32];
|
---|
1184 | char FailBuf[256];
|
---|
1185 | char *Args;
|
---|
1186 | int arglen = 0;
|
---|
1187 | char *Prog;
|
---|
1188 | RESULTCODES rc_code;
|
---|
1189 | ULONG ulAction;
|
---|
1190 |
|
---|
1191 | //ULONG ulNew;
|
---|
1192 | HPIPE hfChildPipe;
|
---|
1193 | HFILE hfNewStdOut = (HFILE) - 1, hfNewStdErr = (HFILE) - 1;
|
---|
1194 | HFILE hfStdOut = 1, hfStdErr = 2;
|
---|
1195 | int rc;
|
---|
1196 |
|
---|
1197 | sprintf(szPipe, "\\PIPE\\eFTE%d\\CHILD%d", getpid(), PCount);
|
---|
1198 | PCount++;
|
---|
1199 |
|
---|
1200 | rc = DosCreateNPipe(szPipe, &hfPipe,
|
---|
1201 | NP_NOINHERIT | NP_ACCESS_INBOUND,
|
---|
1202 | NP_NOWAIT | NP_TYPE_BYTE | NP_READMODE_BYTE | 1,
|
---|
1203 | 0, 4096, 0);
|
---|
1204 | if (rc != 0)
|
---|
1205 | return -1;
|
---|
1206 |
|
---|
1207 | rc = DosConnectNPipe(hfPipe);
|
---|
1208 | if (rc != 0 && rc != ERROR_PIPE_NOT_CONNECTED) {
|
---|
1209 | DosClose(hfPipe);
|
---|
1210 | return -1;
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | rc = DosSetNPHState(hfPipe, NP_WAIT | NP_READMODE_BYTE);
|
---|
1214 | if (rc != 0) {
|
---|
1215 | DosClose(hfPipe);
|
---|
1216 | return -1;
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | rc = DosOpen(szPipe, &hfChildPipe, &ulAction, 0,
|
---|
1220 | FILE_NORMAL,
|
---|
1221 | OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
|
---|
1222 | OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYREADWRITE, NULL);
|
---|
1223 | if (rc != 0) {
|
---|
1224 | DosClose(hfPipe);
|
---|
1225 | return -1;
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | // Duplicate handles
|
---|
1229 | DosDupHandle(hfStdOut, &hfNewStdOut);
|
---|
1230 | DosDupHandle(hfStdErr, &hfNewStdErr);
|
---|
1231 | // Close existing handles for current process
|
---|
1232 | DosClose(hfStdOut);
|
---|
1233 | DosClose(hfStdErr);
|
---|
1234 | // Redirect existing handles to new file
|
---|
1235 | DosDupHandle(hfChildPipe, &hfStdOut);
|
---|
1236 | DosDupHandle(hfChildPipe, &hfStdErr);
|
---|
1237 | // Let started program inherit handles from parent
|
---|
1238 |
|
---|
1239 | Prog = getenv("COMSPEC");
|
---|
1240 |
|
---|
1241 | Args = (char *)malloc(strlen(Prog) + 1 + 3 + strlen(Command) + 1 + 1);
|
---|
1242 | if (Args == NULL) {
|
---|
1243 | DosClose(hfPipe);
|
---|
1244 | return -1;
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | strcpy(Args, Prog);
|
---|
1248 | arglen = strlen(Args) + 1;
|
---|
1249 | strcpy(Args + arglen, "/c ");
|
---|
1250 | arglen += 3;
|
---|
1251 | strcpy(Args + arglen, Command);
|
---|
1252 | arglen += strlen(Command) + 1;
|
---|
1253 | Args[arglen] = '\0';
|
---|
1254 |
|
---|
1255 | rc = DosExecPgm(FailBuf, sizeof(FailBuf), EXEC_ASYNCRESULT, // | EXEC_BACKGROUND,
|
---|
1256 | Args, 0, &rc_code, Prog);
|
---|
1257 |
|
---|
1258 | free(Args);
|
---|
1259 |
|
---|
1260 | // Get back original handles
|
---|
1261 | DosDupHandle(hfNewStdOut, &hfStdOut);
|
---|
1262 | DosDupHandle(hfNewStdErr, &hfStdErr);
|
---|
1263 | // Close the duplicated handles - no longer needed
|
---|
1264 | DosClose(hfNewStdOut);
|
---|
1265 | DosClose(hfNewStdErr);
|
---|
1266 |
|
---|
1267 | DosClose(hfChildPipe); // pipe one way, close out write end
|
---|
1268 |
|
---|
1269 | if (rc != 0) {
|
---|
1270 | DosClose(hfPipe);
|
---|
1271 | return -1;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 | pid = rc_code.codeTerminate; // get pid when successful
|
---|
1275 |
|
---|
1276 | return 0;
|
---|
1277 | }
|
---|
1278 | #define INCL_EXCEPTQ_CLASS
|
---|
1279 | #define INCL_LOADEXCEPTQ
|
---|
1280 | #include "exceptq.h"
|
---|
1281 | static void _LNK_CONV PipeThread(void *p)
|
---|
1282 | {
|
---|
1283 | GPipe *pipe = (GPipe *) p;
|
---|
1284 | int rc;
|
---|
1285 | ULONG ulPostCount;
|
---|
1286 | ULONG used;
|
---|
1287 | PID pid;
|
---|
1288 | HPIPE hfPipe;
|
---|
1289 | RESULTCODES rc_code;
|
---|
1290 | ScopedExceptqLoader sel;
|
---|
1291 | //EXCEPTIONREGISTRATIONRECORD exRegRec;
|
---|
1292 |
|
---|
1293 | //LoadExceptq(&exRegRec, "", "");
|
---|
1294 | rc = CreatePipeChild(pid, hfPipe, pipe->Command);
|
---|
1295 |
|
---|
1296 | if (rc != 0) {
|
---|
1297 | //fprintf(stderr, "Failed createpipe");
|
---|
1298 | DosRequestMutexSem(pipe->Access, SEM_INDEFINITE_WAIT);
|
---|
1299 | pipe->reading = 0;
|
---|
1300 | DosPostEventSem(pipe->NewData);
|
---|
1301 | DosReleaseMutexSem(pipe->Access);
|
---|
1302 | //UninstallExceptq(&exRegRec);
|
---|
1303 | return;
|
---|
1304 | }
|
---|
1305 |
|
---|
1306 | //fprintf(stderr, "Pipe: Begin: %d %s\n", pipe->id, Args);
|
---|
1307 | while (1) {
|
---|
1308 | //fprintf(stderr, "Waiting on pipe\n");
|
---|
1309 | //fread(pipe->buffer, 1, pipe->buflen, fp);
|
---|
1310 | rc = DosRead(hfPipe, pipe->buffer, pipe->buflen, &used);
|
---|
1311 | if (rc < 0)
|
---|
1312 | used = 0;
|
---|
1313 |
|
---|
1314 | DosRequestMutexSem(pipe->Access, SEM_INDEFINITE_WAIT);
|
---|
1315 | //fprintf(stderr, "Waiting on mutex\n");
|
---|
1316 | pipe->bufused = used;
|
---|
1317 | //fprintf(stderr, "Pipe: fread: %d %d\n", pipe->id, pipe->bufused);
|
---|
1318 | DosResetEventSem(pipe->ResumeRead, &ulPostCount);
|
---|
1319 | if (pipe->bufused == 0)
|
---|
1320 | break;
|
---|
1321 | if (pipe->notify) {
|
---|
1322 | DosPostEventSem(pipe->NewData);
|
---|
1323 | pipe->stopped = 0;
|
---|
1324 | }
|
---|
1325 | DosReleaseMutexSem(pipe->Access);
|
---|
1326 | if (pipe->DoTerm)
|
---|
1327 | break;
|
---|
1328 | //fprintf(stderr, "Waiting on sem\n");
|
---|
1329 | DosWaitEventSem(pipe->ResumeRead, SEM_INDEFINITE_WAIT);
|
---|
1330 | //fprintf(stderr, "Read: Released mutex\n");
|
---|
1331 | if (pipe->DoTerm)
|
---|
1332 | break;
|
---|
1333 | }
|
---|
1334 | DosClose(hfPipe);
|
---|
1335 | //fprintf(stderr, "Pipe: pClose: %d\n", pipe->id);
|
---|
1336 | rc = DosWaitChild(DCWA_PROCESS, DCWW_WAIT, &rc_code, &pid, pid);
|
---|
1337 | pipe->RetCode = rc_code.codeResult;
|
---|
1338 | // pclose(fp);
|
---|
1339 | pipe->reading = 0;
|
---|
1340 | DosPostEventSem(pipe->NewData);
|
---|
1341 | //UninstallExceptq(&exRegRec);
|
---|
1342 | DosReleaseMutexSem(pipe->Access);
|
---|
1343 | //fprintf(stderr, "Read: Released mutex\n");
|
---|
1344 | return;
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | int GUI::OpenPipe(char *Command, EModel * notify)
|
---|
1348 | {
|
---|
1349 | int i;
|
---|
1350 |
|
---|
1351 | for (i = 0; i < MAX_PIPES; i++) {
|
---|
1352 | if (Pipes[i].used == 0) {
|
---|
1353 | Pipes[i].reading = 1;
|
---|
1354 | Pipes[i].stopped = 1;
|
---|
1355 | Pipes[i].id = i;
|
---|
1356 | Pipes[i].bufused = 0;
|
---|
1357 | Pipes[i].bufpos = 0;
|
---|
1358 | Pipes[i].buflen = PIPE_BUFLEN;
|
---|
1359 | Pipes[i].Command = strdup(Command);
|
---|
1360 | Pipes[i].notify = notify;
|
---|
1361 | Pipes[i].DoTerm = 0;
|
---|
1362 | if ((Pipes[i].buffer = (char *)malloc(PIPE_BUFLEN)) == 0) {
|
---|
1363 | free(Pipes[i].Command);
|
---|
1364 | return -1;
|
---|
1365 | }
|
---|
1366 |
|
---|
1367 | if (0 != DosCreateMutexSem(0, &Pipes[i].Access, 0, 0)) {
|
---|
1368 | free(Pipes[i].Command);
|
---|
1369 | free(Pipes[i].buffer);
|
---|
1370 | return -1;
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | if (0 != DosCreateEventSem(0, &Pipes[i].ResumeRead, 0, 0)) {
|
---|
1374 | free(Pipes[i].Command);
|
---|
1375 | free(Pipes[i].buffer);
|
---|
1376 | DosCloseMutexSem(Pipes[i].Access);
|
---|
1377 | return -1;
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 | if (0 != DosCreateEventSem(0, &Pipes[i].NewData, 0, 0)) {
|
---|
1381 | free(Pipes[i].Command);
|
---|
1382 | free(Pipes[i].buffer);
|
---|
1383 | DosCloseEventSem(Pipes[i].ResumeRead);
|
---|
1384 | DosCloseMutexSem(Pipes[i].Access);
|
---|
1385 | return -1;
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | #if defined(__WATCOMC__) || defined(__MT__) || defined(__MULTI__)
|
---|
1389 | Pipes[i].tid = _beginthread(PipeThread,
|
---|
1390 | FAKE_BEGINTHREAD_NULL
|
---|
1391 | 16384, &Pipes[i]);
|
---|
1392 | #else
|
---|
1393 | DosCreateThread(Pipes[i].tid, (PFNTHREAD) PipeThread, &Pipes[i], 0, /* immediate */
|
---|
1394 | 16384);
|
---|
1395 | #endif
|
---|
1396 | Pipes[i].used = 1;
|
---|
1397 | //fprintf(stderr, "Pipe Open: %d\n", i);
|
---|
1398 | return i;
|
---|
1399 | }
|
---|
1400 | }
|
---|
1401 | return -1;
|
---|
1402 | }
|
---|
1403 |
|
---|
1404 | int GUI::SetPipeView(int id, EModel * notify)
|
---|
1405 | {
|
---|
1406 | if (id < 0 || id > MAX_PIPES)
|
---|
1407 | return -1;
|
---|
1408 | if (Pipes[id].used == 0)
|
---|
1409 | return -1;
|
---|
1410 | DosRequestMutexSem(Pipes[id].Access, SEM_INDEFINITE_WAIT);
|
---|
1411 | //fprintf(stderr, "Pipe View: %d %08X\n", id, notify);
|
---|
1412 | Pipes[id].notify = notify;
|
---|
1413 | DosReleaseMutexSem(Pipes[id].Access);
|
---|
1414 | return 0;
|
---|
1415 | }
|
---|
1416 |
|
---|
1417 | int GUI::ReadPipe(int id, void *buffer, int len)
|
---|
1418 | {
|
---|
1419 | int l;
|
---|
1420 |
|
---|
1421 | //ULONG ulPostCount;
|
---|
1422 |
|
---|
1423 | if (id < 0 || id > MAX_PIPES)
|
---|
1424 | return -1;
|
---|
1425 | if (Pipes[id].used == 0)
|
---|
1426 | return -1;
|
---|
1427 | //fprintf(stderr, "Read: Waiting on mutex\n");
|
---|
1428 | //ConContinue();
|
---|
1429 | DosRequestMutexSem(Pipes[id].Access, SEM_INDEFINITE_WAIT);
|
---|
1430 | //fprintf(stderr, "Pipe Read: Get %d %d\n", id, len);
|
---|
1431 | if (Pipes[id].bufused - Pipes[id].bufpos > 0) {
|
---|
1432 | l = len;
|
---|
1433 | if (l > Pipes[id].bufused - Pipes[id].bufpos) {
|
---|
1434 | l = Pipes[id].bufused - Pipes[id].bufpos;
|
---|
1435 | }
|
---|
1436 | memcpy(buffer, Pipes[id].buffer + Pipes[id].bufpos, l);
|
---|
1437 | Pipes[id].bufpos += l;
|
---|
1438 | if (Pipes[id].bufpos == Pipes[id].bufused) {
|
---|
1439 | Pipes[id].bufused = 0;
|
---|
1440 | Pipes[id].bufpos = 0;
|
---|
1441 | //fprintf(stderr, "Pipe Resume Read: %d\n", id);
|
---|
1442 | Pipes[id].stopped = 1;
|
---|
1443 | //fprintf(stderr, "Read: posting sem\n");
|
---|
1444 | DosPostEventSem(Pipes[id].ResumeRead);
|
---|
1445 | }
|
---|
1446 | }
|
---|
1447 | else if (Pipes[id].reading == 0)
|
---|
1448 | l = -1;
|
---|
1449 | else {
|
---|
1450 | l = 0;
|
---|
1451 | // DosBeep(200, 200);
|
---|
1452 | }
|
---|
1453 | //fprintf(stderr, "Pipe Read: Got %d %d\n", id, l);
|
---|
1454 | DosReleaseMutexSem(Pipes[id].Access);
|
---|
1455 | //fprintf(stderr, "Read: Released mutex\n");
|
---|
1456 | return l;
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 | int GUI::ClosePipe(int id)
|
---|
1460 | {
|
---|
1461 | if (id < 0 || id > MAX_PIPES)
|
---|
1462 | return -1;
|
---|
1463 | if (Pipes[id].used == 0)
|
---|
1464 | return -1;
|
---|
1465 | if (Pipes[id].reading == 1) {
|
---|
1466 | Pipes[id].DoTerm = 1;
|
---|
1467 | DosPostEventSem(Pipes[id].ResumeRead);
|
---|
1468 | DosWaitThread(&Pipes[id].tid, DCWW_WAIT);
|
---|
1469 | }
|
---|
1470 | free(Pipes[id].buffer);
|
---|
1471 | free(Pipes[id].Command);
|
---|
1472 | DosCloseEventSem(Pipes[id].NewData);
|
---|
1473 | DosCloseEventSem(Pipes[id].ResumeRead);
|
---|
1474 | DosCloseMutexSem(Pipes[id].Access);
|
---|
1475 | // fprintf(stderr, "Pipe Close: %d\n", id);
|
---|
1476 | Pipes[id].used = 0;
|
---|
1477 | //ConContinue();
|
---|
1478 | return Pipes[id].RetCode;
|
---|
1479 | }
|
---|
1480 |
|
---|
1481 | int GUI::RunProgram(int mode, char *Command, bool PM)
|
---|
1482 | {
|
---|
1483 | int rc, W, H, W1, H1;
|
---|
1484 |
|
---|
1485 | ConQuerySize(&W, &H);
|
---|
1486 | ConHideMouse();
|
---|
1487 | ConSuspend();
|
---|
1488 |
|
---|
1489 | if (Command == 0 || *Command == 0) // empty string = shell
|
---|
1490 | Command = getenv("COMSPEC");
|
---|
1491 |
|
---|
1492 | rc = system(Command);
|
---|
1493 |
|
---|
1494 | ConContinue();
|
---|
1495 | ConShowMouse();
|
---|
1496 | ConQuerySize(&W1, &H1);
|
---|
1497 |
|
---|
1498 | if (W != W1 || H != H1) {
|
---|
1499 | frames->Resize(W1, H1);
|
---|
1500 | }
|
---|
1501 | frames->Repaint();
|
---|
1502 | return rc;
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | int ConSetTitle(char *Title, char *STitle)
|
---|
1506 | {
|
---|
1507 | /* HSWITCH hsw;
|
---|
1508 | SWCNTRL sw;
|
---|
1509 | HAB hab;
|
---|
1510 | PID pid;
|
---|
1511 | TID tid;
|
---|
1512 |
|
---|
1513 | static PVOID Shmem = NULL;
|
---|
1514 |
|
---|
1515 | if (Shmem == NULL)
|
---|
1516 | DosAllocSharedMem(&Shmem, NULL, 4096,
|
---|
1517 | PAG_COMMIT | PAG_READ | PAG_WRITE | OBJ_GIVEABLE);
|
---|
1518 |
|
---|
1519 | strcpy(Shmem, Title);
|
---|
1520 |
|
---|
1521 | hab = WinInitialize(0);
|
---|
1522 |
|
---|
1523 | hsw = WinQuerySwitchHandle(NULLHANDLE, getpid());
|
---|
1524 |
|
---|
1525 | if (WinQuerySwitchEntry(hsw, &sw) != 0)
|
---|
1526 | printf("\x7\n");
|
---|
1527 | else {
|
---|
1528 |
|
---|
1529 | strlcpy (sw.szSwtitle, Title, MAXNAMEL - 1);
|
---|
1530 |
|
---|
1531 | printf("hwnd: %X, hwndIcon: %X, pid: %d\n",
|
---|
1532 | sw.hwnd,
|
---|
1533 | sw.hwndIcon,
|
---|
1534 | sw.idProcess);
|
---|
1535 |
|
---|
1536 | WinQueryWindowProcess(sw.hwnd, &pid, &tid);
|
---|
1537 |
|
---|
1538 | DosGiveSharedMem(Shmem, pid, PAG_READ | PAG_WRITE);
|
---|
1539 |
|
---|
1540 | printf("txt 1: %d\n", WinSetWindowText(sw.hwnd, Shmem));
|
---|
1541 | // printf("txt 2: %d\n", WinSetWindowText(Wsw.hwndIcon, Shmem));
|
---|
1542 |
|
---|
1543 | WinChangeSwitchEntry(hsw, &sw);
|
---|
1544 | }
|
---|
1545 |
|
---|
1546 | WinTerminate(hab);
|
---|
1547 | */
|
---|
1548 | return 0;
|
---|
1549 | }
|
---|
1550 |
|
---|
1551 | int ConGetTitle(char *Title, int MaxLen, char *STitle, int SMaxLen)
|
---|
1552 | {
|
---|
1553 | strcpy(Title, "eFTE/2");
|
---|
1554 | strcpy(STitle, "eFTE/2");
|
---|
1555 | return 0;
|
---|
1556 | }
|
---|
1557 |
|
---|
1558 | int ConCursorVisible()
|
---|
1559 | {
|
---|
1560 | return (CursorVisible == 1);
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | int ConPutEvent(TEvent Event)
|
---|
1564 | {
|
---|
1565 | EventBuf = Event;
|
---|
1566 | return 0;
|
---|
1567 | }
|
---|
1568 |
|
---|
1569 | extern int SevenBit;
|
---|
1570 |
|
---|
1571 | char ConGetDrawChar(int index)
|
---|
1572 | {
|
---|
1573 | static char tab[] = "\xDA\xBF\xC0\xD9\xC4\xB3\xC2\xC3\xB4\xC1"
|
---|
1574 | "\xC5\x1A\xFA\x04\xC4\x18\x19\xB1\xB0\x1B\x1A";
|
---|
1575 | static char tab7[] = "++++-|+++++\x1A.\x04-++#+\x1B\x1A";
|
---|
1576 |
|
---|
1577 | assert(index >= 0 && index < (int)strlen(tab));
|
---|
1578 |
|
---|
1579 | if (SevenBit)
|
---|
1580 | return tab7[index];
|
---|
1581 | else
|
---|
1582 | return tab[index];
|
---|
1583 | }
|
---|
1584 | void FindExePath(char *ExePath)
|
---|
1585 | {
|
---|
1586 | PPIB ppib;
|
---|
1587 | PTIB ptib;
|
---|
1588 | char *s;
|
---|
1589 |
|
---|
1590 | DosGetInfoBlocks(&ptib, &ppib);
|
---|
1591 | DosQueryModuleName(ppib->pib_hmte, MAXPATH, ExePath);
|
---|
1592 | s = strrchr(ExePath, '\\');
|
---|
1593 | *s = 0;
|
---|
1594 | }
|
---|