source: libcaca/trunk/src/event.c @ 347

Last change on this file since 347 was 347, checked in by Sam Hocevar, 17 years ago
  • src/event.c: + Added the CACA_EVENT_RESIZE event. + Added caca_get_mouse_x() and caca_get_mouse_y().
  • src/graphics.c: + Window resize handling in the X11 driver.
  • examples/aafire.c: + Fixed a bad assumption in the resize handling code.
  • Property svn:keywords set to Id
File size: 23.2 KB
Line 
1/*
2 *  libcaca       ASCII-Art library
3 *  Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the GNU Lesser General Public
8 *  License as published by the Free Software Foundation; either
9 *  version 2 of the License, or (at your option) any later version.
10 *
11 *  This library is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 *  Lesser General Public License for more details.
15 *
16 *  You should have received a copy of the GNU Lesser General Public
17 *  License along with this library; if not, write to the Free Software
18 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 *  02111-1307  USA
20 */
21
22/** \file event.c
23 *  \version \$Id: event.c 347 2004-01-13 20:03:48Z sam $
24 *  \author Sam Hocevar <sam@zoy.org>
25 *  \brief Event handling
26 *
27 *  This file contains event handling functions for keyboard and mouse input.
28 */
29
30#include "config.h"
31
32#if defined(USE_SLANG)
33#   if defined(HAVE_SLANG_SLANG_H)
34#       include <slang/slang.h>
35#   else
36#       include <slang.h>
37#   endif
38#endif
39#if defined(USE_NCURSES)
40#   include <curses.h>
41#endif
42#if defined(USE_CONIO)
43#   include <conio.h>
44#endif
45#if defined(USE_X11)
46#   include <X11/Xlib.h>
47#   include <X11/Xutil.h>
48#   include <X11/keysym.h>
49#endif
50#if defined(USE_WIN32)
51#   include <windows.h>
52#endif
53
54#include "caca.h"
55#include "caca_internals.h"
56
57static unsigned int _get_next_event(void);
58static unsigned int _lowlevel_event(void);
59#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
60static void _push_event(unsigned int);
61static unsigned int _pop_event(void);
62#endif
63
64#if !defined(_DOXYGEN_SKIP_ME)
65#define EVENTBUF_LEN 10
66#endif
67#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
68static unsigned int eventbuf[EVENTBUF_LEN];
69static int events = 0;
70#endif
71
72static unsigned int mouse_x = 0, mouse_y = 0;
73
74#if !defined(_DOXYGEN_SKIP_ME)
75/* If no new key was pressed after AUTOREPEAT_THRESHOLD usec, assume the
76 * key was released */
77#define AUTOREPEAT_THRESHOLD 200000
78/* Start repeating key after AUTOREPEAT_TRIGGER usec and send keypress
79 * events every AUTOREPEAT_RATE usec. */
80#define AUTOREPEAT_TRIGGER 300000
81#define AUTOREPEAT_RATE 100000
82#endif
83
84/** \brief Get the next mouse or keyboard input event.
85 *
86 *  This function polls the event queue for mouse or keyboard events matching
87 *  the event mask and returns the first matching event. Non-matching events
88 *  are discarded. \c event_mask must have a non-zero value. This function is
89 *  non-blocking and returns zero if no more events are pending in the queue.
90 *  See also caca_wait_event() for a blocking version of this function.
91 *
92 * \param event_mask Bitmask of requested events.
93 * \return The next matching event in the queue, or 0 if no event is pending.
94 */
95unsigned int caca_get_event(unsigned int event_mask)
96{
97    if(!event_mask)
98        return CACA_EVENT_NONE;
99
100    for( ; ; )
101    {
102        unsigned int event = _get_next_event();
103
104        if(!event || event & event_mask)
105            return event;
106    }
107}
108
109/** \brief Wait for the next mouse or keyboard input event.
110 *
111 *  This function returns the first mouse or keyboard event in the queue
112 *  that matches the event mask. If no event is pending, it blocks until a
113 *  matching event is received. \c event_mask must have a non-zero value.
114 *  See also caca_get_event() for a non-blocking version of this function.
115 *
116 *  \param event_mask Bitmask of requested events.
117 *  \return The next event in the queue.
118 */
119unsigned int caca_wait_event(unsigned int event_mask)
120{
121    if(!event_mask)
122        return CACA_EVENT_NONE;
123
124    for( ; ; )
125    {
126        unsigned int event = _get_next_event();
127
128        if(event & event_mask)
129            return event;
130
131        _caca_sleep(10000);
132    }
133}
134
135/** \brief Return the X mouse coordinate.
136 *
137 *  This function returns the X coordinate of the mouse position last time
138 *  it was detected. This function is not reliable if the ncurses or S-Lang
139 *  drivers are being used, because mouse position is only detected when
140 *  the mouse is clicked. Other drivers such as X11 work well.
141 *
142 *  \return The X mouse coordinate.
143 */
144unsigned int caca_get_mouse_x(void)
145{
146    return mouse_x;
147}
148
149/** \brief Return the Y mouse coordinate.
150 *
151 *  This function returns the Y coordinate of the mouse position last time
152 *  it was detected. This function is not reliable if the ncurses or S-Lang
153 *  drivers are being used, because mouse position is only detected when
154 *  the mouse is clicked. Other drivers such as X11 work well.
155 *
156 *  \return The Y mouse coordinate.
157 */
158unsigned int caca_get_mouse_y(void)
159{
160    return mouse_y;
161}
162
163/*
164 * XXX: The following functions are local.
165 */
166
167static unsigned int _get_next_event(void)
168{
169#if defined(USE_SLANG) || defined(USE_NCURSES)
170    static struct caca_timer key_timer = CACA_TIMER_INITIALIZER;
171    static unsigned int last_key_ticks = 0;
172    static unsigned int autorepeat_ticks = 0;
173    static unsigned int last_key = 0;
174    unsigned int ticks;
175#endif
176    unsigned int event = _lowlevel_event();
177
178#if defined(USE_SLANG)
179    if(_caca_driver != CACA_DRIVER_SLANG)
180#endif
181#if defined(USE_NCURSES)
182    if(_caca_driver != CACA_DRIVER_NCURSES)
183#endif
184    return event;
185
186#if defined(USE_SLANG) || defined(USE_NCURSES)
187    /* Simulate long keypresses using autorepeat features */
188    ticks = _caca_getticks(&key_timer);
189    last_key_ticks += ticks;
190    autorepeat_ticks += ticks;
191
192    /* Handle autorepeat */
193    if(last_key && autorepeat_ticks > AUTOREPEAT_TRIGGER
194                && autorepeat_ticks > AUTOREPEAT_THRESHOLD
195                && autorepeat_ticks > AUTOREPEAT_RATE)
196    {
197        _push_event(event);
198        autorepeat_ticks -= AUTOREPEAT_RATE;
199        return CACA_EVENT_KEY_PRESS | last_key;
200    }
201
202    /* We are in autorepeat mode and the same key was just pressed, ignore
203     * this event and return the next one by calling ourselves. */
204    if(event == (CACA_EVENT_KEY_PRESS | last_key))
205    {
206        last_key_ticks = 0;
207        return _get_next_event();
208    }
209
210    /* We are in autorepeat mode, but key has expired or a new key was
211     * pressed - store our event and return a key release event first */
212    if(last_key && (last_key_ticks > AUTOREPEAT_THRESHOLD
213                     || (event & CACA_EVENT_KEY_PRESS)))
214    {
215        _push_event(event);
216        event = CACA_EVENT_KEY_RELEASE | last_key;
217        last_key = 0;
218        return event;
219    }
220
221    /* A new key was pressed, enter autorepeat mode */
222    if(event & CACA_EVENT_KEY_PRESS)
223    {
224        last_key_ticks = 0;
225        autorepeat_ticks = 0;
226        last_key = event & 0x00ffffff;
227    }
228
229    return event;
230#endif
231}
232
233static unsigned int _lowlevel_event(void)
234{
235    unsigned int event;
236
237#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
238    event = _pop_event();
239
240    if(event)
241        return event;
242#endif
243
244#if defined(USE_X11)
245    /* The X11 event check routine */
246    if(_caca_driver == CACA_DRIVER_X11)
247    {
248        XEvent xevent;
249        char key;
250
251        while(XCheckWindowEvent(x11_dpy, x11_window, x11_event_mask, &xevent)
252               == True)
253        {
254            KeySym keysym;
255
256            /* Resize event */
257            if(xevent.type == ConfigureNotify)
258            {
259                unsigned int w = xevent.xconfigure.width / x11_font_width;
260                unsigned int h = xevent.xconfigure.height / x11_font_height;
261
262                if(w == _caca_width && h == _caca_height)
263                    continue;
264
265                _caca_new_width = w;
266                _caca_new_height = h;
267
268                return CACA_EVENT_RESIZE;
269            }
270
271            /* Check for mouse motion events */
272            if(xevent.type == MotionNotify)
273            {
274                unsigned int newx = xevent.xmotion.x / x11_font_width;
275                unsigned int newy = xevent.xmotion.y / x11_font_height;
276
277                if(newx >= _caca_width)
278                    newx = _caca_width - 1;
279                if(newy >= _caca_height)
280                    newy = _caca_height - 1;
281
282                if(mouse_x == newx && mouse_y == newy)
283                    continue;
284
285                mouse_x = newx;
286                mouse_y = newy;
287
288                return CACA_EVENT_MOUSE_MOTION | (mouse_x << 12) | mouse_y;
289            }
290
291            /* Check for mouse press and release events */
292            if(xevent.type == ButtonPress)
293                return CACA_EVENT_MOUSE_PRESS
294                        | ((XButtonEvent *)&xevent)->button;
295
296            if(xevent.type == ButtonRelease)
297                return CACA_EVENT_MOUSE_RELEASE
298                        | ((XButtonEvent *)&xevent)->button;
299
300            /* Check for key press and release events */
301            if(xevent.type == KeyPress)
302                event |= CACA_EVENT_KEY_PRESS;
303            else if(xevent.type == KeyRelease)
304                event |= CACA_EVENT_KEY_RELEASE;
305            else
306                continue;
307
308            if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
309                return event | key;
310
311            keysym = XKeycodeToKeysym(x11_dpy, xevent.xkey.keycode, 0);
312            switch(keysym)
313            {
314            case XK_F1:    return event | CACA_KEY_F1;
315            case XK_F2:    return event | CACA_KEY_F2;
316            case XK_F3:    return event | CACA_KEY_F3;
317            case XK_F4:    return event | CACA_KEY_F4;
318            case XK_F5:    return event | CACA_KEY_F5;
319            case XK_F6:    return event | CACA_KEY_F6;
320            case XK_F7:    return event | CACA_KEY_F7;
321            case XK_F8:    return event | CACA_KEY_F8;
322            case XK_F9:    return event | CACA_KEY_F9;
323            case XK_F10:   return event | CACA_KEY_F10;
324            case XK_F11:   return event | CACA_KEY_F11;
325            case XK_F12:   return event | CACA_KEY_F12;
326            case XK_F13:   return event | CACA_KEY_F13;
327            case XK_F14:   return event | CACA_KEY_F14;
328            case XK_F15:   return event | CACA_KEY_F15;
329            case XK_Left:  return event | CACA_KEY_LEFT;
330            case XK_Right: return event | CACA_KEY_RIGHT;
331            case XK_Up:    return event | CACA_KEY_UP;
332            case XK_Down:  return event | CACA_KEY_DOWN;
333            default:       return CACA_EVENT_NONE;
334            }
335        }
336
337        return CACA_EVENT_NONE;
338    }
339    else
340#endif
341#if defined(USE_NCURSES)
342    if(_caca_driver == CACA_DRIVER_NCURSES)
343    {
344        int intkey = getch();
345        if(intkey == ERR)
346            return CACA_EVENT_NONE;
347
348        if(intkey < 0x100)
349        {
350            return CACA_EVENT_KEY_PRESS | intkey;
351        }
352
353        if(intkey == KEY_MOUSE)
354        {
355            MEVENT mevent;
356            getmouse(&mevent);
357
358            switch(mevent.bstate)
359            {
360                case BUTTON1_PRESSED:
361                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
362                    break;
363                case BUTTON1_RELEASED:
364                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
365                    break;
366                case BUTTON1_CLICKED:
367                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
368                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
369                    break;
370                case BUTTON1_DOUBLE_CLICKED:
371                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
372                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
373                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
374                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
375                    break;
376                case BUTTON1_TRIPLE_CLICKED:
377                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
378                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
379                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
380                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
381                    _push_event(CACA_EVENT_MOUSE_PRESS | 1);
382                    _push_event(CACA_EVENT_MOUSE_RELEASE | 1);
383                    break;
384                case BUTTON1_RESERVED_EVENT:
385                    break;
386
387                case BUTTON2_PRESSED:
388                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
389                    break;
390                case BUTTON2_RELEASED:
391                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
392                    break;
393                case BUTTON2_CLICKED:
394                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
395                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
396                    break;
397                case BUTTON2_DOUBLE_CLICKED:
398                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
399                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
400                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
401                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
402                    break;
403                case BUTTON2_TRIPLE_CLICKED:
404                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
405                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
406                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
407                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
408                    _push_event(CACA_EVENT_MOUSE_PRESS | 2);
409                    _push_event(CACA_EVENT_MOUSE_RELEASE | 2);
410                    break;
411                case BUTTON2_RESERVED_EVENT:
412                    break;
413
414                case BUTTON3_PRESSED:
415                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
416                    break;
417                case BUTTON3_RELEASED:
418                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
419                    break;
420                case BUTTON3_CLICKED:
421                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
422                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
423                    break;
424                case BUTTON3_DOUBLE_CLICKED:
425                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
426                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
427                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
428                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
429                    break;
430                case BUTTON3_TRIPLE_CLICKED:
431                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
432                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
433                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
434                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
435                    _push_event(CACA_EVENT_MOUSE_PRESS | 3);
436                    _push_event(CACA_EVENT_MOUSE_RELEASE | 3);
437                    break;
438                case BUTTON3_RESERVED_EVENT:
439                    break;
440
441                case BUTTON4_PRESSED:
442                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
443                    break;
444                case BUTTON4_RELEASED:
445                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
446                    break;
447                case BUTTON4_CLICKED:
448                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
449                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
450                    break;
451                case BUTTON4_DOUBLE_CLICKED:
452                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
453                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
454                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
455                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
456                    break;
457                case BUTTON4_TRIPLE_CLICKED:
458                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
459                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
460                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
461                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
462                    _push_event(CACA_EVENT_MOUSE_PRESS | 4);
463                    _push_event(CACA_EVENT_MOUSE_RELEASE | 4);
464                    break;
465                case BUTTON4_RESERVED_EVENT:
466                    break;
467
468                default:
469                    break;
470            }
471
472            if(mouse_x == (unsigned int)mevent.x &&
473               mouse_y == (unsigned int)mevent.y)
474                return _pop_event();
475
476            mouse_x = mevent.x;
477            mouse_y = mevent.y;
478
479            return CACA_EVENT_MOUSE_MOTION | (mouse_x << 12) | mouse_y;
480        }
481
482        event = CACA_EVENT_KEY_PRESS;
483
484        switch(intkey)
485        {
486            case KEY_UP: return event | CACA_KEY_UP;
487            case KEY_DOWN: return event | CACA_KEY_DOWN;
488            case KEY_LEFT: return event | CACA_KEY_LEFT;
489            case KEY_RIGHT: return event | CACA_KEY_RIGHT;
490
491            case KEY_IC: return event | CACA_KEY_INSERT;
492            case KEY_DC: return event | CACA_KEY_DELETE;
493            case KEY_HOME: return event | CACA_KEY_HOME;
494            case KEY_END: return event | CACA_KEY_END;
495            case KEY_PPAGE: return event | CACA_KEY_PAGEUP;
496            case KEY_NPAGE: return event | CACA_KEY_PAGEDOWN;
497
498            case KEY_F(1): return event | CACA_KEY_F1;
499            case KEY_F(2): return event | CACA_KEY_F2;
500            case KEY_F(3): return event | CACA_KEY_F3;
501            case KEY_F(4): return event | CACA_KEY_F4;
502            case KEY_F(5): return event | CACA_KEY_F5;
503            case KEY_F(6): return event | CACA_KEY_F6;
504            case KEY_F(7): return event | CACA_KEY_F7;
505            case KEY_F(8): return event | CACA_KEY_F8;
506            case KEY_F(9): return event | CACA_KEY_F9;
507            case KEY_F(10): return event | CACA_KEY_F10;
508            case KEY_F(11): return event | CACA_KEY_F11;
509            case KEY_F(12): return event | CACA_KEY_F12;
510        }
511
512        return CACA_EVENT_NONE;
513    }
514    else
515#endif
516#if defined(USE_SLANG)
517    if(_caca_driver == CACA_DRIVER_SLANG)
518    {
519        int intkey;
520
521        if(!SLang_input_pending(0))
522            return CACA_EVENT_NONE;
523
524        /* We first use SLang_getkey() to see whether Esc was pressed
525         * alone, then (if it wasn't) we unget the key and use SLkp_getkey()
526         * instead, so that escape sequences are interpreted. */
527        intkey = SLang_getkey();
528
529        if(intkey != 0x1b /* Esc */ || SLang_input_pending(0))
530        {
531            SLang_ungetkey(intkey);
532            intkey = SLkp_getkey();
533        }
534
535        /* If the key was ASCII, return it immediately */
536        if(intkey < 0x100)
537        {
538            return CACA_EVENT_KEY_PRESS | intkey;
539        }
540
541        if(intkey == 0x3e9)
542        {
543            int button = (SLang_getkey() - ' ' + 1) & 0xf;
544            unsigned int x = SLang_getkey() - '!';
545            unsigned int y = SLang_getkey() - '!';
546            _push_event(CACA_EVENT_MOUSE_PRESS | button);
547            _push_event(CACA_EVENT_MOUSE_RELEASE | button);
548
549            if(mouse_x == x && mouse_y == y)
550                return _pop_event();
551
552            mouse_x = x;
553            mouse_y = y;
554
555            return CACA_EVENT_MOUSE_MOTION | (mouse_x << 12) | mouse_y;
556        }
557
558        event = CACA_EVENT_KEY_PRESS;
559
560        switch(intkey)
561        {
562            case SL_KEY_UP: return event | CACA_KEY_UP;
563            case SL_KEY_DOWN: return event | CACA_KEY_DOWN;
564            case SL_KEY_LEFT: return event | CACA_KEY_LEFT;
565            case SL_KEY_RIGHT: return event | CACA_KEY_RIGHT;
566
567            case SL_KEY_IC: return event | CACA_KEY_INSERT;
568            case SL_KEY_DELETE: return event | CACA_KEY_DELETE;
569            case SL_KEY_HOME: return event | CACA_KEY_HOME;
570            case SL_KEY_END: return event | CACA_KEY_END;
571            case SL_KEY_PPAGE: return event | CACA_KEY_PAGEUP;
572            case SL_KEY_NPAGE: return event | CACA_KEY_PAGEDOWN;
573
574            case SL_KEY_F(1): return event | CACA_KEY_F1;
575            case SL_KEY_F(2): return event | CACA_KEY_F2;
576            case SL_KEY_F(3): return event | CACA_KEY_F3;
577            case SL_KEY_F(4): return event | CACA_KEY_F4;
578            case SL_KEY_F(5): return event | CACA_KEY_F5;
579            case SL_KEY_F(6): return event | CACA_KEY_F6;
580            case SL_KEY_F(7): return event | CACA_KEY_F7;
581            case SL_KEY_F(8): return event | CACA_KEY_F8;
582            case SL_KEY_F(9): return event | CACA_KEY_F9;
583            case SL_KEY_F(10): return event | CACA_KEY_F10;
584            case SL_KEY_F(11): return event | CACA_KEY_F11;
585            case SL_KEY_F(12): return event | CACA_KEY_F12;
586        }
587
588        return CACA_EVENT_NONE;
589    }
590    else
591#endif
592#if defined(USE_CONIO)
593    if(_caca_driver == CACA_DRIVER_CONIO)
594    {
595        if(!_conio_kbhit())
596            return CACA_EVENT_NONE;
597
598        event = getch();
599        _push_event(CACA_EVENT_KEY_RELEASE | event);
600        return CACA_EVENT_KEY_PRESS | event;
601    }
602    else
603#endif
604#if defined(USE_WIN32)
605    if(_caca_driver == CACA_DRIVER_WIN32)
606    {
607        INPUT_RECORD rec;
608        DWORD num;
609
610        for( ; ; )
611        {
612            GetNumberOfConsoleInputEvents(win32_hin, &num);
613            if(num == 0)
614                break;
615
616            ReadConsoleInput(win32_hin, &rec, 1, &num);
617            if(rec.EventType == KEY_EVENT)
618            {
619                if(rec.Event.KeyEvent.bKeyDown)
620                    event = CACA_EVENT_KEY_PRESS;
621                else
622                    event = CACA_EVENT_KEY_RELEASE;
623
624                if(rec.Event.KeyEvent.uChar.AsciiChar)
625                    return event | rec.Event.KeyEvent.uChar.AsciiChar;
626            }
627
628            if(rec.EventType == MOUSE_EVENT)
629            {
630                if(rec.Event.MouseEvent.dwEventFlags == 0)
631                {
632                    if(rec.Event.MouseEvent.dwButtonState & 0x01)
633                        return CACA_EVENT_MOUSE_PRESS | 0x000001;
634
635                    if(rec.Event.MouseEvent.dwButtonState & 0x02)
636                        return CACA_EVENT_MOUSE_PRESS | 0x000002;
637                }
638                else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_MOVED)
639                {
640                    COORD pos = rec.Event.MouseEvent.dwMousePosition;
641
642                    if(mouse_x == (unsigned int)pos.X &&
643                       mouse_y == (unsigned int)pos.Y)
644                        continue;
645
646                    mouse_x = pos.X;
647                    mouse_y = pos.Y;
648
649                    return CACA_EVENT_MOUSE_MOTION | (mouse_x << 12) | mouse_y;
650                }
651#if 0
652                else if(rec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
653                {
654                    cout << rec.Event.MouseEvent.dwMousePosition.X << "," <<
655                            rec.Event.MouseEvent.dwMousePosition.Y << "  " << flush;
656                }
657                else if(rec.Event.MouseEvent.dwEventFlags == MOUSE_WHEELED)
658                {
659                    SetConsoleCursorPosition(hOut,
660                                             WheelWhere);
661                    if(rec.Event.MouseEvent.dwButtonState & 0xFF000000)
662                        cout << "Down" << flush;
663                    else
664                        cout << "Up  " << flush;
665                }
666#endif
667            }
668
669            /* Unknown event */
670            return CACA_EVENT_NONE;
671        }
672
673        /* No event */
674        return CACA_EVENT_NONE;
675    }
676    else
677#endif
678    {
679        /* Dummy */
680    }
681
682    return CACA_EVENT_NONE;
683}
684
685#if defined(USE_SLANG) || defined(USE_NCURSES) || defined(USE_CONIO)
686static void _push_event(unsigned int event)
687{
688    if(events == EVENTBUF_LEN)
689        return;
690    eventbuf[events] = event;
691    events++;
692}
693
694static unsigned int _pop_event(void)
695{
696    int i;
697    unsigned int event;
698
699    if(events == 0)
700        return CACA_EVENT_NONE;
701
702    event = eventbuf[0];
703    for(i = 1; i < events; i++)
704        eventbuf[i - 1] = eventbuf[i];
705    events--;
706
707    return event;
708}
709#endif
710
Note: See TracBrowser for help on using the repository browser.