File: event.c

package info (click to toggle)
libcaca 0.9-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,196 kB
  • ctags: 478
  • sloc: ansic: 5,750; sh: 3,544; makefile: 225
file content (754 lines) | stat: -rw-r--r-- 24,901 bytes parent folder | download
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
 *  libcaca       ASCII-Art library
 *  Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
 *                All Rights Reserved
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307  USA
 */

/** \file event.c
 *  \version \$Id: event.c 258 2004-01-26 10:52:30Z sam $
 *  \author Sam Hocevar <sam@zoy.org>
 *  \brief Event handling
 *
 *  This file contains event handling functions for keyboard and mouse input.
 */

#include "config.h"

#if defined(USE_SLANG)
#   if defined(HAVE_SLANG_SLANG_H)
#       include <slang/slang.h>
#   else
#       include <slang.h>
#   endif
#endif
#if defined(USE_NCURSES)
#   if defined(HAVE_NCURSES_H)
#       include <ncurses.h>
#   else
#       include <curses.h>
#   endif
#endif
#if defined(USE_CONIO)
#   include <conio.h>
#endif
#if defined(USE_X11)
#   include <X11/Xlib.h>
#   include <X11/Xutil.h>
#   include <X11/keysym.h>
#endif
#if defined(USE_WIN32)
#   include <windows.h>
#endif

#include "caca.h"
#include "caca_internals.h"

static unsigned int _get_next_event(void);
static unsigned int _lowlevel_event(void);
#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
static void _push_event(unsigned int);
static unsigned int _pop_event(void);
#endif

#if !defined(_DOXYGEN_SKIP_ME)
#define EVENTBUF_LEN 10
#endif
#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
static unsigned int eventbuf[EVENTBUF_LEN];
static int events = 0;
#endif

static unsigned int mouse_x = 0, mouse_y = 0;

#if !defined(_DOXYGEN_SKIP_ME)
/* If no new key was pressed after AUTOREPEAT_THRESHOLD usec, assume the
 * key was released */
#define AUTOREPEAT_THRESHOLD 200000
/* Start repeating key after AUTOREPEAT_TRIGGER usec and send keypress
 * events every AUTOREPEAT_RATE usec. */
#define AUTOREPEAT_TRIGGER 300000
#define AUTOREPEAT_RATE 100000
#endif

/** \brief Get the next mouse or keyboard input event.
 *
 *  This function polls the event queue for mouse or keyboard events matching
 *  the event mask and returns the first matching event. Non-matching events
 *  are discarded. \c event_mask must have a non-zero value. This function is
 *  non-blocking and returns zero if no more events are pending in the queue.
 *  See also caca_wait_event() for a blocking version of this function.
 *
 * \param event_mask Bitmask of requested events.
 * \return The next matching event in the queue, or 0 if no event is pending.
 */
unsigned int caca_get_event(unsigned int event_mask)
{
    if(!event_mask)
        return CACA_EVENT_NONE;

    for( ; ; )
    {
        unsigned int event = _get_next_event();

        if(!event || event & event_mask)
            return event;
    }
}

/** \brief Wait for the next mouse or keyboard input event.
 *
 *  This function returns the first mouse or keyboard event in the queue
 *  that matches the event mask. If no event is pending, it blocks until a
 *  matching event is received. \c event_mask must have a non-zero value.
 *  See also caca_get_event() for a non-blocking version of this function.
 *
 *  \param event_mask Bitmask of requested events.
 *  \return The next event in the queue.
 */
unsigned int caca_wait_event(unsigned int event_mask)
{
    if(!event_mask)
        return CACA_EVENT_NONE;

    for( ; ; )
    {
        unsigned int event = _get_next_event();

        if(event & event_mask)
            return event;

        _caca_sleep(10000);
    }
}

/** \brief Return the X mouse coordinate.
 *
 *  This function returns the X coordinate of the mouse position last time
 *  it was detected. This function is not reliable if the ncurses or S-Lang
 *  drivers are being used, because mouse position is only detected when
 *  the mouse is clicked. Other drivers such as X11 work well.
 *
 *  \return The X mouse coordinate.
 */
unsigned int caca_get_mouse_x(void)
{
    if(mouse_x >= _caca_width)
        mouse_x = _caca_width - 1;

    return mouse_x;
}

/** \brief Return the Y mouse coordinate.
 *
 *  This function returns the Y coordinate of the mouse position last time
 *  it was detected. This function is not reliable if the ncurses or S-Lang
 *  drivers are being used, because mouse position is only detected when
 *  the mouse is clicked. Other drivers such as X11 work well.
 *
 *  \return The Y mouse coordinate.
 */
unsigned int caca_get_mouse_y(void)
{
    if(mouse_y >= _caca_height)
        mouse_y = _caca_height - 1;

    return mouse_y;
}

/*
 * XXX: The following functions are local.
 */

static unsigned int _get_next_event(void)
{
#if defined(USE_SLANG) || defined(USE_NCURSES)
    static struct caca_timer key_timer = CACA_TIMER_INITIALIZER;
    static unsigned int last_key_ticks = 0;
    static unsigned int autorepeat_ticks = 0;
    static unsigned int last_key = 0;
    unsigned int ticks;
#endif
    unsigned int event = _lowlevel_event();

#if defined(USE_SLANG)
    if(_caca_driver != CACA_DRIVER_SLANG)
#endif
#if defined(USE_NCURSES)
    if(_caca_driver != CACA_DRIVER_NCURSES)
#endif
    return event;

#if defined(USE_SLANG) || defined(USE_NCURSES)
    /* Simulate long keypresses using autorepeat features */
    ticks = _caca_getticks(&key_timer);
    last_key_ticks += ticks;
    autorepeat_ticks += ticks;

    /* Handle autorepeat */
    if(last_key && autorepeat_ticks > AUTOREPEAT_TRIGGER
                && autorepeat_ticks > AUTOREPEAT_THRESHOLD
                && autorepeat_ticks > AUTOREPEAT_RATE)
    {
        _push_event(event);
        autorepeat_ticks -= AUTOREPEAT_RATE;
        return CACA_EVENT_KEY_PRESS | last_key;
    }

    /* We are in autorepeat mode and the same key was just pressed, ignore
     * this event and return the next one by calling ourselves. */
    if(event == (CACA_EVENT_KEY_PRESS | last_key))
    {
        last_key_ticks = 0;
        return _get_next_event();
    }

    /* We are in autorepeat mode, but key has expired or a new key was
     * pressed - store our event and return a key release event first */
    if(last_key && (last_key_ticks > AUTOREPEAT_THRESHOLD
                     || (event & CACA_EVENT_KEY_PRESS)))
    {
        _push_event(event);
        event = CACA_EVENT_KEY_RELEASE | last_key;
        last_key = 0;
        return event;
    }

    /* A new key was pressed, enter autorepeat mode */
    if(event & CACA_EVENT_KEY_PRESS)
    {
        last_key_ticks = 0;
        autorepeat_ticks = 0;
        last_key = event & 0x00ffffff;
    }

    return event;
#endif
}

static unsigned int _lowlevel_event(void)
{
    unsigned int event;

#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
    event = _pop_event();

    if(event)
        return event;
#endif

#if defined(USE_X11)
    /* The X11 event check routine */
    if(_caca_driver == CACA_DRIVER_X11)
    {
        XEvent xevent;
        char key;

        while(XCheckWindowEvent(x11_dpy, x11_window, x11_event_mask, &xevent)
               == True)
        {
            KeySym keysym;

            /* Expose event */
            if(xevent.type == Expose)
            {
                XCopyArea(x11_dpy, x11_pixmap, x11_window, x11_gc, 0, 0,
                          _caca_width * x11_font_width,
                          _caca_height * x11_font_height, 0, 0);
                continue;
            }

            /* Resize event */
            if(xevent.type == ConfigureNotify)
            {
                unsigned int w, h;

                w = (xevent.xconfigure.width + x11_font_width / 3)
                      / x11_font_width;
                h = (xevent.xconfigure.height + x11_font_height / 3)
                      / x11_font_height;

                if(!w || !h || (w == _caca_width && h == _caca_height))
                    continue;

                x11_new_width = w;
                x11_new_height = h;

                if(_caca_resize)
                    continue;

                _caca_resize = 1;

                return CACA_EVENT_RESIZE;
            }

            /* Check for mouse motion events */
            if(xevent.type == MotionNotify)
            {
                unsigned int newx = xevent.xmotion.x / x11_font_width;
                unsigned int newy = xevent.xmotion.y / x11_font_height;

                if(newx >= _caca_width)
                    newx = _caca_width - 1;
                if(newy >= _caca_height)
                    newy = _caca_height - 1;

                if(mouse_x == newx && mouse_y == newy)
                    continue;

                mouse_x = newx;
                mouse_y = newy;

                return CACA_EVENT_MOUSE_MOTION | (mouse_x << 12) | mouse_y;
            }

            /* Check for mouse press and release events */
            if(xevent.type == ButtonPress)
                return CACA_EVENT_MOUSE_PRESS
                        | ((XButtonEvent *)&xevent)->button;

            if(xevent.type == ButtonRelease)
                return CACA_EVENT_MOUSE_RELEASE
                        | ((XButtonEvent *)&xevent)->button;

            /* Check for key press and release events */
            if(xevent.type == KeyPress)
                event |= CACA_EVENT_KEY_PRESS;
            else if(xevent.type == KeyRelease)
                event |= CACA_EVENT_KEY_RELEASE;
            else
                continue;

            if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
                return event | key;

            keysym = XKeycodeToKeysym(x11_dpy, xevent.xkey.keycode, 0);
            switch(keysym)
            {
            case XK_F1:    return event | CACA_KEY_F1;
            case XK_F2:    return event | CACA_KEY_F2;
            case XK_F3:    return event | CACA_KEY_F3;
            case XK_F4:    return event | CACA_KEY_F4;
            case XK_F5:    return event | CACA_KEY_F5;
            case XK_F6:    return event | CACA_KEY_F6;
            case XK_F7:    return event | CACA_KEY_F7;
            case XK_F8:    return event | CACA_KEY_F8;
            case XK_F9:    return event | CACA_KEY_F9;
            case XK_F10:   return event | CACA_KEY_F10;
            case XK_F11:   return event | CACA_KEY_F11;
            case XK_F12:   return event | CACA_KEY_F12;
            case XK_F13:   return event | CACA_KEY_F13;
            case XK_F14:   return event | CACA_KEY_F14;
            case XK_F15:   return event | CACA_KEY_F15;
            case XK_Left:  return event | CACA_KEY_LEFT;
            case XK_Right: return event | CACA_KEY_RIGHT;
            case XK_Up:    return event | CACA_KEY_UP;
            case XK_Down:  return event | CACA_KEY_DOWN;
            default:       return CACA_EVENT_NONE;
            }
        }

        return CACA_EVENT_NONE;
    }
    else
#endif
#if defined(USE_NCURSES)
    if(_caca_driver == CACA_DRIVER_NCURSES)
    {
        int intkey;

        if(_caca_resize_event)
        {
            _caca_resize_event = 0;
            _caca_resize = 1;
            return CACA_EVENT_RESIZE;
        }

        intkey = getch();
        if(intkey == ERR)
            return CACA_EVENT_NONE;

        if(intkey < 0x100)
        {
            return CACA_EVENT_KEY_PRESS | intkey;
        }

        if(intkey == KEY_MOUSE)
        {
            MEVENT mevent;
            getmouse(&mevent);

            switch(mevent.bstate)
            {
                case BUTTON1_PRESSED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
                    break;
                case BUTTON1_RELEASED:
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
                    break;
                case BUTTON1_CLICKED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
                    break;
                case BUTTON1_DOUBLE_CLICKED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
                    break;
                case BUTTON1_TRIPLE_CLICKED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
                    break;
                case BUTTON1_RESERVED_EVENT:
                    break;

                case BUTTON2_PRESSED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
                    break;
                case BUTTON2_RELEASED:
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
                    break;
                case BUTTON2_CLICKED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
                    break;
                case BUTTON2_DOUBLE_CLICKED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
                    break;
                case BUTTON2_TRIPLE_CLICKED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
                    break;
                case BUTTON2_RESERVED_EVENT:
                    break;

                case BUTTON3_PRESSED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
                    break;
                case BUTTON3_RELEASED:
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
                    break;
                case BUTTON3_CLICKED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
                    break;
                case BUTTON3_DOUBLE_CLICKED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
                    break;
                case BUTTON3_TRIPLE_CLICKED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
                    break;
                case BUTTON3_RESERVED_EVENT:
                    break;

                case BUTTON4_PRESSED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
                    break;
                case BUTTON4_RELEASED:
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
                    break;
                case BUTTON4_CLICKED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
                    break;
                case BUTTON4_DOUBLE_CLICKED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
                    break;
                case BUTTON4_TRIPLE_CLICKED:
                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
                    break;
                case BUTTON4_RESERVED_EVENT:
                    break;

                default:
                    break;
            }

            if(mouse_x == (unsigned int)mevent.x &&
               mouse_y == (unsigned int)mevent.y)
                return _pop_event();

            mouse_x = mevent.x;
            mouse_y = mevent.y;

            return CACA_EVENT_MOUSE_MOTION | (mouse_x << 12) | mouse_y;
        }

        event = CACA_EVENT_KEY_PRESS;

        switch(intkey)
        {
            case KEY_UP: return event | CACA_KEY_UP;
            case KEY_DOWN: return event | CACA_KEY_DOWN;
            case KEY_LEFT: return event | CACA_KEY_LEFT;
            case KEY_RIGHT: return event | CACA_KEY_RIGHT;

            case KEY_IC: return event | CACA_KEY_INSERT;
            case KEY_DC: return event | CACA_KEY_DELETE;
            case KEY_HOME: return event | CACA_KEY_HOME;
            case KEY_END: return event | CACA_KEY_END;
            case KEY_PPAGE: return event | CACA_KEY_PAGEUP;
            case KEY_NPAGE: return event | CACA_KEY_PAGEDOWN;

            case KEY_F(1): return event | CACA_KEY_F1;
            case KEY_F(2): return event | CACA_KEY_F2;
            case KEY_F(3): return event | CACA_KEY_F3;
            case KEY_F(4): return event | CACA_KEY_F4;
            case KEY_F(5): return event | CACA_KEY_F5;
            case KEY_F(6): return event | CACA_KEY_F6;
            case KEY_F(7): return event | CACA_KEY_F7;
            case KEY_F(8): return event | CACA_KEY_F8;
            case KEY_F(9): return event | CACA_KEY_F9;
            case KEY_F(10): return event | CACA_KEY_F10;
            case KEY_F(11): return event | CACA_KEY_F11;
            case KEY_F(12): return event | CACA_KEY_F12;
        }

        return CACA_EVENT_NONE;
    }
    else
#endif
#if defined(USE_SLANG)
    if(_caca_driver == CACA_DRIVER_SLANG)
    {
        int intkey;

        if(_caca_resize_event)
        {
            _caca_resize_event = 0;
            _caca_resize = 1;
            return CACA_EVENT_RESIZE;
        }

        if(!SLang_input_pending(0))
            return CACA_EVENT_NONE;

        /* We first use SLang_getkey() to see whether Esc was pressed
         * alone, then (if it wasn't) we unget the key and use SLkp_getkey()
         * instead, so that escape sequences are interpreted. */
        intkey = SLang_getkey();

        if(intkey != 0x1b /* Esc */ || SLang_input_pending(0))
        {
            SLang_ungetkey(intkey);
            intkey = SLkp_getkey();
        }

        /* If the key was ASCII, return it immediately */
        if(intkey < 0x100)
        {
            return CACA_EVENT_KEY_PRESS | intkey;
        }

        if(intkey == 0x3e9)
        {
            int button = (SLang_getkey() - ' ' + 1) & 0xf;
            unsigned int x = SLang_getkey() - '!';
            unsigned int y = SLang_getkey() - '!';
            _push_event(CACA_EVENT_MOUSE_PRESS | button);
            _push_event(CACA_EVENT_MOUSE_RELEASE | button);

            if(mouse_x == x && mouse_y == y)
                return _pop_event();

            mouse_x = x;
            mouse_y = y;

            return CACA_EVENT_MOUSE_MOTION | (mouse_x << 12) | mouse_y;
        }

        event = CACA_EVENT_KEY_PRESS;

        switch(intkey)
        {
            case SL_KEY_UP: return event | CACA_KEY_UP;
            case SL_KEY_DOWN: return event | CACA_KEY_DOWN;
            case SL_KEY_LEFT: return event | CACA_KEY_LEFT;
            case SL_KEY_RIGHT: return event | CACA_KEY_RIGHT;

            case SL_KEY_IC: return event | CACA_KEY_INSERT;
            case SL_KEY_DELETE: return event | CACA_KEY_DELETE;
            case SL_KEY_HOME: return event | CACA_KEY_HOME;
            case SL_KEY_END: return event | CACA_KEY_END;
            case SL_KEY_PPAGE: return event | CACA_KEY_PAGEUP;
            case SL_KEY_NPAGE: return event | CACA_KEY_PAGEDOWN;

            case SL_KEY_F(1): return event | CACA_KEY_F1;
            case SL_KEY_F(2): return event | CACA_KEY_F2;
            case SL_KEY_F(3): return event | CACA_KEY_F3;
            case SL_KEY_F(4): return event | CACA_KEY_F4;
            case SL_KEY_F(5): return event | CACA_KEY_F5;
            case SL_KEY_F(6): return event | CACA_KEY_F6;
            case SL_KEY_F(7): return event | CACA_KEY_F7;
            case SL_KEY_F(8): return event | CACA_KEY_F8;
            case SL_KEY_F(9): return event | CACA_KEY_F9;
            case SL_KEY_F(10): return event | CACA_KEY_F10;
            case SL_KEY_F(11): return event | CACA_KEY_F11;
            case SL_KEY_F(12): return event | CACA_KEY_F12;
        }

        return CACA_EVENT_NONE;
    }
    else
#endif
#if defined(USE_CONIO)
    if(_caca_driver == CACA_DRIVER_CONIO)
    {
        if(!_conio_kbhit())
            return CACA_EVENT_NONE;

        event = getch();
        _push_event(CACA_EVENT_KEY_RELEASE | event);
        return CACA_EVENT_KEY_PRESS | event;
    }
    else
#endif
#if defined(USE_WIN32)
    if(_caca_driver == CACA_DRIVER_WIN32)
    {
        INPUT_RECORD rec;
        DWORD num;

        for( ; ; )
        {
            GetNumberOfConsoleInputEvents(win32_hin, &num);
            if(num == 0)
                break;

            ReadConsoleInput(win32_hin, &rec, 1, &num);
            if(rec.EventType == KEY_EVENT)
            {
                if(rec.Event.KeyEvent.bKeyDown)
                    event = CACA_EVENT_KEY_PRESS;
                else
                    event = CACA_EVENT_KEY_RELEASE;

                if(rec.Event.KeyEvent.uChar.AsciiChar)
                    return event | rec.Event.KeyEvent.uChar.AsciiChar;
            }

            if(rec.EventType == MOUSE_EVENT)
            {
                if(rec.Event.MouseEvent.dwEventFlags == 0)
                {
                    if(rec.Event.MouseEvent.dwButtonState & 0x01)
                        return CACA_EVENT_MOUSE_PRESS | 0x000001;

                    if(rec.Event.MouseEvent.dwButtonState & 0x02)
                        return CACA_EVENT_MOUSE_PRESS | 0x000002;
                }
                else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
                {
                    COORD pos = rec.Event.MouseEvent.dwMousePosition;

                    if(mouse_x == (unsigned int)pos.X &&
                       mouse_y == (unsigned int)pos.Y)
                        continue;

                    mouse_x = pos.X;
                    mouse_y = pos.Y;

                    return CACA_EVENT_MOUSE_MOTION | (mouse_x << 12) | mouse_y;
                }
#if 0
                else if(rec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
                {
                    cout << rec.Event.MouseEvent.dwMousePosition.X << "," <<
                            rec.Event.MouseEvent.dwMousePosition.Y << "  " << flush;
                }
                else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
                {
                    SetConsoleCursorPosition(hOut,
                                             WheelWhere);
                    if(rec.Event.MouseEvent.dwButtonState & 0xFF000000)
                        cout << "Down" << flush;
                    else
                        cout << "Up  " << flush;
                }
#endif
            }

            /* Unknown event */
            return CACA_EVENT_NONE;
        }

        /* No event */
        return CACA_EVENT_NONE;
    }
    else
#endif
    {
        /* Dummy */
    }

    return CACA_EVENT_NONE;
}

#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
static void _push_event(unsigned int event)
{
    if(!event || events == EVENTBUF_LEN)
        return;
    eventbuf[events] = event;
    events++;
}

static unsigned int _pop_event(void)
{
    int i;
    unsigned int event;

    if(events == 0)
        return CACA_EVENT_NONE;

    event = eventbuf[0];
    for(i = 1; i < events; i++)
        eventbuf[i - 1] = eventbuf[i];
    events--;

    return event;
}
#endif