source: libcaca/trunk/src/io.c @ 316

Last change on this file since 316 was 316, checked in by Sam Hocevar, 19 years ago
  • src/io.c: + caca_get_event() and caca_wait_event() now accept a mask as an argument

in order to select events.

  • Property svn:keywords set to Id
File size: 11.3 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 io.c
23 *  \version \$Id: io.c 316 2004-01-05 18:17:20Z 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
51#include <unistd.h>
52
53#include "caca.h"
54#include "caca_internals.h"
55
56static unsigned int _get_next_event(void);
57static void _push_key(unsigned int);
58static unsigned int _pop_key(void);
59static unsigned int _read_key(void);
60
61#define KEY_BUFLEN 10
62static unsigned int keybuf[KEY_BUFLEN + 1]; /* zero-terminated */
63static int keys = 0;
64
65/** \brief Get the next mouse or keyboard input event.
66 *
67 *  This function polls the event queue for mouse or keyboard events matching
68 *  the event mask and returns the first matching event. Non-matching events
69 *  are discarded. It is non-blocking and returns zero if no more events are
70 *  pending in the queue. See also caca_wait_event() for a blocking version
71 *  of this function.
72 *
73 * \param event_mask Bitmask of requested events.
74 * \return The next matching event in the queue, or 0 if no event is pending.
75 */
76unsigned int caca_get_event(unsigned int event_mask)
77{
78    for( ; ; )
79    {
80        unsigned int event = _get_next_event();
81
82        if(!event || event & event_mask)
83            return event;
84    }
85}
86
87/** \brief Wait for the next mouse or keyboard input event.
88 *
89 *  This function returns the first mouse or keyboard event in the queue
90 *  that matches the event mask. If no event is pending, it blocks until a
91 *  matching event is received. See also caca_get_event() for a non-blocking
92 *  version of this function.
93 *
94 *  \param event_mask Bitmask of requested events.
95 *  \return The next event in the queue.
96 */
97unsigned int caca_wait_event(unsigned int event_mask)
98{
99    for( ; ; )
100    {
101        unsigned int event = _get_next_event();
102
103        if(event & event_mask)
104            return event;
105
106        usleep(1000);
107    }
108}
109
110/*
111 * XXX: The following functions are local.
112 */
113
114static unsigned int _get_next_event(void)
115{
116    unsigned int event = 0;
117
118#if defined(USE_X11)
119    /* The X11 event check routine */
120    if(_caca_driver == CACA_DRIVER_X11)
121    {
122        XEvent xevent;
123        static unsigned int x11_x = 0, x11_y = 0;
124        long int xevent_mask = KeyPressMask | KeyReleaseMask | ButtonPressMask
125                                | ButtonReleaseMask | PointerMotionMask;
126        char key;
127
128        while(XCheckWindowEvent(x11_dpy, x11_window, xevent_mask, &xevent)
129               == True)
130        {
131            KeySym keysym;
132
133            /* Check for mouse motion events */
134            if(xevent.type == MotionNotify)
135            {
136                unsigned int newx = xevent.xmotion.x / x11_font_width;
137                unsigned int newy = xevent.xmotion.y / x11_font_height;
138
139                if(x11_x == newx && x11_y == newy)
140                    continue;
141
142                if(newx >= _caca_width)
143                    newx = _caca_width - 1;
144                if(newy >= _caca_height)
145                    newy = _caca_height - 1;
146
147                x11_x = newx & 0xfff;
148                x11_y = newy & 0xfff;
149
150                return CACA_EVENT_MOUSE_MOTION | (newx << 12) | (newy << 0);
151            }
152
153            /* Check for mouse press and release events */
154            if(xevent.type == ButtonPress)
155                return CACA_EVENT_MOUSE_PRESS | 1;
156
157            if(xevent.type == ButtonRelease)
158                return CACA_EVENT_MOUSE_RELEASE | 1;
159
160            /* Check for key press and release events */
161            if(xevent.type == KeyPress)
162                event |= CACA_EVENT_KEY_PRESS;
163            else if(xevent.type == KeyRelease)
164                event |= CACA_EVENT_KEY_RELEASE;
165            else
166                continue;
167
168            if(XLookupString(&xevent.xkey, &key, 1, NULL, NULL))
169                return event | key;
170
171            keysym = XKeycodeToKeysym(x11_dpy, xevent.xkey.keycode, 0);
172            switch(keysym)
173            {
174            case XK_F1:    return event | CACA_KEY_F1;
175            case XK_F2:    return event | CACA_KEY_F2;
176            case XK_F3:    return event | CACA_KEY_F3;
177            case XK_F4:    return event | CACA_KEY_F4;
178            case XK_F5:    return event | CACA_KEY_F5;
179            case XK_F6:    return event | CACA_KEY_F6;
180            case XK_F7:    return event | CACA_KEY_F7;
181            case XK_F8:    return event | CACA_KEY_F8;
182            case XK_F9:    return event | CACA_KEY_F9;
183            case XK_F10:   return event | CACA_KEY_F10;
184            case XK_F11:   return event | CACA_KEY_F11;
185            case XK_F12:   return event | CACA_KEY_F12;
186            case XK_F13:   return event | CACA_KEY_F13;
187            case XK_F14:   return event | CACA_KEY_F14;
188            case XK_F15:   return event | CACA_KEY_F15;
189            case XK_Left:  return event | CACA_KEY_LEFT;
190            case XK_Right: return event | CACA_KEY_RIGHT;
191            case XK_Up:    return event | CACA_KEY_UP;
192            case XK_Down:  return event | CACA_KEY_DOWN;
193            default:       return 0;
194            }
195        }
196
197        return 0;
198    }
199#endif
200
201    /* Read all available key events and push them into the buffer */
202    while(keys < KEY_BUFLEN)
203    {
204        unsigned int key = _read_key();
205        if(!key)
206            break;
207        _push_key(key);
208    }
209
210    /* If no keys were read, return */
211    if(!keys)
212        return 0;
213
214#if defined(USE_NCURSES)
215    if(_caca_driver == CACA_DRIVER_NCURSES)
216    {
217        if(keybuf[0] == KEY_MOUSE)
218        {
219            MEVENT mevent;
220            _pop_key();
221            getmouse(&mevent);
222            _push_key(CACA_EVENT_MOUSE_PRESS | 1);
223            return CACA_EVENT_MOUSE_MOTION | (mevent.x << 12) | mevent.y;
224        }
225
226        switch(keybuf[0])
227        {
228            case KEY_UP: event = CACA_EVENT_KEY_PRESS | CACA_KEY_UP; break;
229            case KEY_DOWN: event = CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN; break;
230            case KEY_LEFT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT; break;
231            case KEY_RIGHT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT; break;
232
233            case KEY_F(1): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F1; break;
234            case KEY_F(2): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F2; break;
235            case KEY_F(3): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F3; break;
236            case KEY_F(4): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F4; break;
237            case KEY_F(5): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F5; break;
238            case KEY_F(6): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F6; break;
239            case KEY_F(7): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F7; break;
240            case KEY_F(8): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F8; break;
241            case KEY_F(9): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F9; break;
242            case KEY_F(10): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F10; break;
243            case KEY_F(11): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F11; break;
244            case KEY_F(12): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F12; break;
245        }
246
247        if(event)
248        {
249            _pop_key();
250            return event;
251        }
252    }
253#endif
254
255    /* If it's already a special event, return it */
256    if((keybuf[0] & ~0xff) != 0)
257        return _pop_key();
258
259    /* If it's not an escape sequence, return the key */
260    if(keybuf[0] != '\x1b')
261        return CACA_EVENT_KEY_PRESS | _pop_key();
262
263    /*
264     * Handle known escape sequences
265     */
266
267    _pop_key();
268
269    if(keybuf[0] == 'O' && keybuf[1] >= 'P' && keybuf[1] <= 'S')
270    {
271        /* ^[OP ^[OQ ^[OR ^[OS */
272        static unsigned int keylist[] =
273            { CACA_KEY_F1, CACA_KEY_F2, CACA_KEY_F3, CACA_KEY_F4 };
274        _pop_key();
275        return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'P'];
276    }
277    else if(keybuf[0] == '[' && keybuf[1] >= 'A' && keybuf[1] <= 'D')
278    {
279        /* ^[[A ^[[B ^[[C ^[[D */
280        static unsigned int keylist[] =
281            { CACA_KEY_UP, CACA_KEY_DOWN, CACA_KEY_RIGHT, CACA_KEY_LEFT };
282        _pop_key();
283        return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'A'];
284    }
285    else if(keybuf[0] == '[' && keybuf[1] == 'M' &&
286            keybuf[2] && keybuf[3] && keybuf[3])
287    {
288        /* ^[[Mxxx */
289        _pop_key();
290        _pop_key();
291        _push_key(CACA_EVENT_MOUSE_PRESS | (_pop_key() - ' '));
292        return CACA_EVENT_MOUSE_MOTION
293                | ((_pop_key() - '!') << 12) | ((_pop_key() - '!') << 0);
294    }
295    else if(keybuf[0] == '[' && keybuf[1] == '1' && keybuf[3] == '~' &&
296            keybuf[2] >= '5' && keybuf[2] != '6' && keybuf[2] <= '9')
297    {
298        /* ^[[15~ ^[[17~ ^[[18~ ^[[19~ */
299        static unsigned int keylist[] =
300            { CACA_KEY_F5, 0, CACA_KEY_F6, CACA_KEY_F7, CACA_KEY_F8 };
301        _pop_key();
302        _pop_key();
303        event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '5'];
304        _pop_key();
305        return event;
306    }
307    else if(keybuf[0] == '[' && keybuf[1] == '2' && keybuf[3] == '~' &&
308            keybuf[2] >= '0' && keybuf[2] != '2' && keybuf[2] <= '4')
309    {
310        /* ^[[20~ ^[[21~ ^[[23~ ^[[24~ */
311        static unsigned int keylist[] =
312            { CACA_KEY_F9, CACA_KEY_F10, 0, CACA_KEY_F11, CACA_KEY_F12 };
313        _pop_key();
314        _pop_key();
315        event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '0'];
316        _pop_key();
317        return event;
318    }
319
320    /* Unknown escape sequence: return the ESC key */
321    return CACA_EVENT_KEY_PRESS | '\x1b';
322}
323
324static void _push_key(unsigned int key)
325{
326    if(keys == KEY_BUFLEN)
327        return;
328    keybuf[keys] = key;
329    keys++;
330    keybuf[keys] = 0;
331}
332
333static unsigned int _pop_key(void)
334{
335    int i;
336    unsigned int key = keybuf[0];
337    keys--;
338    for(i = 0; i < keys; i++)
339        keybuf[i] = keybuf[i + 1];
340    keybuf[keys] = 0;
341
342    return key;
343}
344
345static unsigned int _read_key(void)
346{
347#if defined(USE_NCURSES)
348    int intkey;
349#endif
350#if defined(USE_X11)
351#endif
352
353    switch(_caca_driver)
354    {
355#if defined(USE_SLANG)
356    case CACA_DRIVER_SLANG:
357        return SLang_input_pending(0) ? SLang_getkey() : 0;
358#endif
359#if defined(USE_NCURSES)
360    case CACA_DRIVER_NCURSES:
361        intkey = getch();
362        return (intkey == ERR) ? 0 : intkey;
363#endif
364#if defined(USE_CONIO)
365    case CACA_DRIVER_CONIO:
366        return _conio_kbhit() ? getch() : 0;
367#endif
368#if defined(USE_X11)
369    case CACA_DRIVER_X11:
370#endif
371    default:
372        break;
373    }
374
375    return 0;
376}
377
Note: See TracBrowser for help on using the repository browser.