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

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