source: libcaca/branches/0.5/src/io.c @ 4104

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