summaryrefslogtreecommitdiff
path: root/payloads/libpayload/curses/PDCurses/os2/pdckbd.c
blob: df79882ac7f6f03fcf0264dd290985a2a60b3555 (plain)
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/* Public Domain Curses */

#if defined(__EMX__) || defined(__WATCOMC__) || defined(__IBMC__) || \
defined(__TURBOC__)
# define HAVE_SIGNAL
# include <signal.h>
#endif

#include "pdcos2.h"

RCSID("$Id: pdckbd.c,v 1.89 2008/07/14 04:24:51 wmcbrine Exp $")

/*man-start**************************************************************

  Name:                                                         pdckbd

  Synopsis:
        unsigned long PDC_get_input_fd(void);

  Description:
        PDC_get_input_fd() returns the file descriptor that PDCurses
        reads its input from. It can be used for select().

  Portability                                X/Open    BSD    SYS V
        PDC_get_input_fd                        -       -       -

**man-end****************************************************************/

#ifdef EMXVIDEO
# include <termios.h>
static int tahead = -1;
#else
static KBDINFO kbdinfo;     /* default keyboard mode */
static HMOU mouse_handle = 0;
static MOUSE_STATUS old_mouse_status;
static USHORT old_shift = 0;
static bool key_pressed = FALSE;
static int mouse_events = 0;
#endif

/************************************************************************
 *    Table for key code translation of function keys in keypad mode    *
 *    These values are for strict IBM keyboard compatibles only         *
 ************************************************************************/

static short key_table[] =
{
    -1,             ALT_ESC,        -1,             0,
    -1,             -1,             -1,             -1,
    -1,             -1,             -1,             -1,
    -1,             -1,             ALT_BKSP,       KEY_BTAB,
    ALT_Q,          ALT_W,          ALT_E,          ALT_R,
    ALT_T,          ALT_Y,          ALT_U,          ALT_I,
    ALT_O,          ALT_P,          ALT_LBRACKET,   ALT_RBRACKET,
    ALT_ENTER,      -1,             ALT_A,          ALT_S,
    ALT_D,          ALT_F,          ALT_G,          ALT_H,
    ALT_J,          ALT_K,          ALT_L,          ALT_SEMICOLON,
    ALT_FQUOTE,     ALT_BQUOTE,     -1,             ALT_BSLASH,
    ALT_Z,          ALT_X,          ALT_C,          ALT_V,
    ALT_B,          ALT_N,          ALT_M,          ALT_COMMA,
    ALT_STOP,       ALT_FSLASH,     -1,             ALT_PADSTAR,
    -1,             -1,             -1,             KEY_F(1),
    KEY_F(2),       KEY_F(3),       KEY_F(4),       KEY_F(5),
    KEY_F(6),       KEY_F(7),       KEY_F(8),       KEY_F(9),
    KEY_F(10),      -1,             -1,             KEY_HOME,
    KEY_UP,         KEY_PPAGE,      ALT_PADMINUS,   KEY_LEFT,
    KEY_B2,         KEY_RIGHT,      ALT_PADPLUS,    KEY_END,
    KEY_DOWN,       KEY_NPAGE,      KEY_IC,         KEY_DC,
    KEY_F(13),      KEY_F(14),      KEY_F(15),      KEY_F(16),
    KEY_F(17),      KEY_F(18),      KEY_F(19),      KEY_F(20),
    KEY_F(21),      KEY_F(22),      KEY_F(25),      KEY_F(26),
    KEY_F(27),      KEY_F(28),      KEY_F(29),      KEY_F(30),
    KEY_F(31),      KEY_F(32),      KEY_F(33),      KEY_F(34),
    KEY_F(37),      KEY_F(38),      KEY_F(39),      KEY_F(40),
    KEY_F(41),      KEY_F(42),      KEY_F(43),      KEY_F(44),
    KEY_F(45),      KEY_F(46),      -1,             CTL_LEFT,
    CTL_RIGHT,      CTL_END,        CTL_PGDN,       CTL_HOME,
    ALT_1,          ALT_2,          ALT_3,          ALT_4,
    ALT_5,          ALT_6,          ALT_7,          ALT_8,
    ALT_9,          ALT_0,          ALT_MINUS,      ALT_EQUAL,
    CTL_PGUP,       KEY_F(11),      KEY_F(12),      KEY_F(23),
    KEY_F(24),      KEY_F(35),      KEY_F(36),      KEY_F(47),
    KEY_F(48),      CTL_UP,         CTL_PADMINUS,   CTL_PADCENTER,
    CTL_PADPLUS,    CTL_DOWN,       CTL_INS,        CTL_DEL,
    CTL_TAB,        CTL_PADSLASH,   CTL_PADSTAR,    ALT_HOME,
    ALT_UP,         ALT_PGUP,       -1,             ALT_LEFT,
    -1,             ALT_RIGHT,      -1,             ALT_END,
    ALT_DOWN,       ALT_PGDN,       ALT_INS,        ALT_DEL,
    ALT_PADSLASH,   ALT_TAB,        ALT_PADENTER,   -1
};

unsigned long pdc_key_modifiers = 0L;

unsigned long PDC_get_input_fd(void)
{
    PDC_LOG(("PDC_get_input_fd() - called\n"));

    return (unsigned long)fileno(stdin);
}

#ifndef EMXVIDEO

void PDC_get_keyboard_info(void)
{
    kbdinfo.cb = sizeof(kbdinfo);
    KbdGetStatus(&kbdinfo, 0);
}

void PDC_set_keyboard_default(void)
{
    KbdSetStatus(&kbdinfo, 0);
}

#endif /* ifndef EMXVIDEO */

void PDC_set_keyboard_binary(bool on)
{
    PDC_LOG(("PDC_set_keyboard_binary() - called\n"));

#ifndef EMXVIDEO
    if (on)
    {
        kbdinfo.fsMask &= ~(KEYBOARD_ASCII_MODE);
        kbdinfo.fsMask |= KEYBOARD_BINARY_MODE;
    }
    else
    {
        kbdinfo.fsMask &= ~(KEYBOARD_BINARY_MODE);
        kbdinfo.fsMask |= KEYBOARD_ASCII_MODE;
    }

    KbdSetStatus(&kbdinfo, 0);
#endif

#ifdef HAVE_SIGNAL
    signal(SIGBREAK, on ? SIG_IGN : SIG_DFL);
#endif
}

/* check if a key or mouse event is waiting */

bool PDC_check_key(void)
{
#if !defined(_MSC_VER) && !defined(EMXVIDEO)
    KBDKEYINFO keyInfo = {0};
#endif

#ifdef EMXVIDEO
    if (tahead == -1)       /* Nothing typed yet */
    {
        tahead = _read_kbd(0, 0, 0);

        /* Read additional */

        if (tahead == 0)
            tahead = _read_kbd(0, 1, 0) << 8;
    }

    return (tahead != -1);
#else
# ifndef _MSC_VER

    KbdGetStatus(&kbdinfo, 0);

    if (mouse_handle)
    {
        MOUQUEINFO queue;

        MouGetNumQueEl(&queue, mouse_handle);
        mouse_events = queue.cEvents;

        if (mouse_events)
            return TRUE;
    }

    if (old_shift && !kbdinfo.fsState)  /* modifier released */
    {
        if (!key_pressed && SP->return_key_modifiers)
            return TRUE;
    }
    else if (!old_shift && kbdinfo.fsState) /* modifier pressed */
        key_pressed = FALSE;

    old_shift = kbdinfo.fsState;

    KbdPeek(&keyInfo, 0);   /* peek at keyboard  */
    return (keyInfo.fbStatus != 0);
# else
    return kbhit();
# endif
#endif
}

#ifndef EMXVIDEO

static int _process_mouse_events(void)
{
    MOUEVENTINFO event;
    static const USHORT button_mask[] = {6, 96, 24},
                        move_mask[] = {2, 32, 8},
                        press_mask[] = {4, 64, 16};
    USHORT count = 1;
    short shift_flags = 0;
    int i;

    MouReadEventQue(&event, &count, mouse_handle);
    mouse_events--;

    for (i = 0; i < 3; i++)
    {
        pdc_mouse_status.button[i] =
            ((event.fs & move_mask[i]) ? BUTTON_MOVED : 0) |
            ((event.fs & press_mask[i]) ? BUTTON_PRESSED : 0);

        /* PRESS events are sometimes mistakenly reported as MOVE
           events. A MOVE should always follow a PRESS, so treat a MOVE
           immediately after a RELEASE as a PRESS. */

        if ((pdc_mouse_status.button[i] == BUTTON_MOVED) &&
            (old_mouse_status.button[i] == BUTTON_RELEASED))
        {
            pdc_mouse_status.button[i] = BUTTON_PRESSED;
        }

        if (pdc_mouse_status.button[i] == BUTTON_PRESSED && SP->mouse_wait)
        {
            /* Check for a click -- a PRESS followed immediately by a
               release */

            if (!mouse_events)
            {
                MOUQUEINFO queue;

                napms(SP->mouse_wait);

                MouGetNumQueEl(&queue, mouse_handle);
                mouse_events = queue.cEvents;
            }

            if (mouse_events)
            {
                MouReadEventQue(&event, &count, mouse_handle);

                if (!(event.fs & button_mask[i]))
                    pdc_mouse_status.button[i] = BUTTON_CLICKED;
            }
        }
    }

    pdc_mouse_status.x = event.col;
    pdc_mouse_status.y = event.row;

    pdc_mouse_status.changes = 0;

    for (i = 0; i < 3; i++)
    {
        if (old_mouse_status.button[i] != pdc_mouse_status.button[i])
            pdc_mouse_status.changes |= (1 << i);

        if (pdc_mouse_status.button[i] == BUTTON_MOVED)
        {
            /* Discard non-moved "moves" */

            if (pdc_mouse_status.x == old_mouse_status.x &&
                pdc_mouse_status.y == old_mouse_status.y)
                return -1;

            /* Motion events always flag the button as changed */

            pdc_mouse_status.changes |= (1 << i);
            pdc_mouse_status.changes |= PDC_MOUSE_MOVED;
            break;
        }
    }

    old_mouse_status = pdc_mouse_status;

    /* Treat click events as release events for comparison purposes */

    for (i = 0; i < 3; i++)
    {
        if (old_mouse_status.button[i] == BUTTON_CLICKED)
            old_mouse_status.button[i] = BUTTON_RELEASED;
    }

    /* Check for SHIFT/CONTROL/ALT */

    if (kbdinfo.fsState & KBDSTF_ALT)
        shift_flags |= BUTTON_ALT;

    if (kbdinfo.fsState & KBDSTF_CONTROL)
        shift_flags |= BUTTON_CONTROL;

    if (kbdinfo.fsState & (KBDSTF_LEFTSHIFT|KBDSTF_RIGHTSHIFT))
        shift_flags |= BUTTON_SHIFT;

    if (shift_flags)
    {
        for (i = 0; i < 3; i++)
        {
            if (pdc_mouse_status.changes & (1 << i))
                pdc_mouse_status.button[i] |= shift_flags;
        }
    }

    old_shift = kbdinfo.fsState;
    key_pressed = TRUE;

    SP->key_code = TRUE;
    return KEY_MOUSE;
}

#endif

/* return the next available key or mouse event */

int PDC_get_key(void)
{
    int key, scan;
#ifndef EMXVIDEO
    KBDKEYINFO keyInfo = {0};
#endif

#ifdef EMXVIDEO
    if (tahead == -1)
    {
        tahead = _read_kbd(0, 1, 0);

        /* Read additional */

        if (tahead == 0)
            tahead = _read_kbd(0, 1, 0) << 8;
    }

    key = tahead & 0xff;
    scan = tahead >> 8;
    pdc_key_modifiers = 0L;

    tahead = -1;
#else
    pdc_key_modifiers = 0L;

    if (mouse_handle && mouse_events)
        return _process_mouse_events();

    if (old_shift && !kbdinfo.fsState)
    {
        key = -1;

        if (old_shift & KBDSTF_LEFTALT)
        {
            key = KEY_ALT_L;
        }
        else if (old_shift & KBDSTF_RIGHTALT)
        {
            key = KEY_ALT_R;
        }
        else if (old_shift & KBDSTF_LEFTCONTROL)
        {
            key = KEY_CONTROL_L;
        }
        else if (old_shift & KBDSTF_RIGHTCONTROL)
        {
            key = KEY_CONTROL_R;
        }
        else if (old_shift & KBDSTF_LEFTSHIFT)
        {
            key = KEY_SHIFT_L;
        }
        else if (old_shift & KBDSTF_RIGHTSHIFT)
        {
            key = KEY_SHIFT_R;
        }

        key_pressed = FALSE;
        old_shift = kbdinfo.fsState;

        SP->key_code = TRUE;
        return key;
    }

    KbdCharIn(&keyInfo, IO_WAIT, 0);    /* get a character */

    key = keyInfo.chChar;
    scan = keyInfo.chScan;

    if (SP->save_key_modifiers)
    {
        if (keyInfo.fsState & KBDSTF_ALT)
            pdc_key_modifiers |= PDC_KEY_MODIFIER_ALT;

        if (keyInfo.fsState & KBDSTF_CONTROL)
            pdc_key_modifiers |= PDC_KEY_MODIFIER_CONTROL;

        if (keyInfo.fsState & KBDSTF_NUMLOCK_ON)
            pdc_key_modifiers |= PDC_KEY_MODIFIER_NUMLOCK;

        if (keyInfo.fsState & (KBDSTF_LEFTSHIFT|KBDSTF_RIGHTSHIFT))
            pdc_key_modifiers |= PDC_KEY_MODIFIER_SHIFT;
    }
#endif
    if (scan == 0x1c && key == 0x0a)    /* ^Enter */
        key = CTL_ENTER;
    else if (scan == 0xe0 && key == 0x0d)   /* PadEnter */
        key = PADENTER;
    else if (scan == 0xe0 && key == 0x0a)   /* ^PadEnter */
        key = CTL_PADENTER;
    else if (scan == 0x37 && key == 0x2a)   /* Star */
        key = PADSTAR;
    else if (scan == 0x4a && key == 0x2d)   /* Minus */
        key = PADMINUS;
    else if (scan == 0x4e && key == 0x2b)   /* Plus */
        key = PADPLUS;
    else if (scan == 0xe0 && key == 0x2f)   /* Slash */
        key = PADSLASH;
    else if (key == 0x00 || (key == 0xe0 && scan > 53 && scan != 86))
        key = (scan > 0xa7) ? -1 : key_table[scan];

    if (keyInfo.fsState & (KBDSTF_LEFTSHIFT|KBDSTF_RIGHTSHIFT))
    {
        switch (key)
        {
        case KEY_HOME:  /* Shift Home */
            key = KEY_SHOME;
            break;
        case KEY_UP:    /* Shift Up */
            key = KEY_SUP;
            break;
        case KEY_PPAGE: /* Shift PgUp */
            key = KEY_SPREVIOUS;
            break;
        case KEY_LEFT:  /* Shift Left */
            key = KEY_SLEFT;
            break;
        case KEY_RIGHT: /* Shift Right */
            key = KEY_SRIGHT;
            break;
        case KEY_END:   /* Shift End */
            key = KEY_SEND;
            break;
        case KEY_DOWN:  /* Shift Down */
            key = KEY_SDOWN;
            break;
        case KEY_NPAGE: /* Shift PgDn */
            key = KEY_SNEXT;
            break;
        case KEY_IC:    /* Shift Ins */
            key = KEY_SIC;
            break;
        case KEY_DC:    /* Shift Del */
            key = KEY_SDC;
        }
    }

    key_pressed = TRUE;
    SP->key_code = ((unsigned)key >= 256);

    return key;
}

/* discard any pending keyboard or mouse input -- this is the core
   routine for flushinp() */

void PDC_flushinp(void)
{
    PDC_LOG(("PDC_flushinp() - called\n"));

#ifdef EMXVIDEO
    tcflush(0, TCIFLUSH);
#else
    if (mouse_handle)
        MouFlushQue(mouse_handle);

    KbdFlushBuffer(0);
#endif
}

int PDC_mouse_set(void)
{
#ifndef EMXVIDEO

    unsigned long mbe = SP->_trap_mbe;

    if (mbe && !mouse_handle)
    {
        memset(&old_mouse_status, 0, sizeof(MOUSE_STATUS));
        MouOpen(NULL, &mouse_handle);
        if (mouse_handle)
            MouDrawPtr(mouse_handle);
    }
    else if (!mbe && mouse_handle)
    {
        MouClose(mouse_handle);
        mouse_handle = 0;
    }

    if (mbe && mouse_handle)
    {
        USHORT mask = ((mbe & (BUTTON1_PRESSED | BUTTON1_CLICKED |
                               BUTTON1_MOVED)) ? 6 : 0) |

                      ((mbe & (BUTTON3_PRESSED | BUTTON3_CLICKED |
                               BUTTON3_MOVED)) ? 24 : 0) |

                      ((mbe & (BUTTON2_PRESSED | BUTTON2_CLICKED |
                               BUTTON2_MOVED)) ? 96 : 0);

        MouSetEventMask(&mask, mouse_handle);
    }
#endif
    return OK;
}

int PDC_modifiers_set(void)
{
    key_pressed = FALSE;

    return OK;
}