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

Last change on this file since 269 was 269, checked in by Sam Hocevar, 18 years ago
  • src/io.c: + Fixed a warning by including <unistd.h>. + Support for arrow and function keys under X11.
  • Property svn:keywords set to Id
File size: 9.0 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 269 2003-12-23 16:32:56Z 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(keybuf[0] != '\x1b')
132        return CACA_EVENT_KEY_PRESS | _pop_key();
133
134    /*
135     * Handle known escape sequences
136     */
137
138    _pop_key();
139
140    if(keybuf[0] == 'O' && keybuf[1] >= 'P' && keybuf[1] <= 'S')
141    {
142        /* ^[OP ^[OQ ^[OR ^[OS */
143        static unsigned int keylist[] =
144            { CACA_KEY_F1, CACA_KEY_F2, CACA_KEY_F3, CACA_KEY_F4 };
145        _pop_key();
146        return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'P'];
147    }
148    else if(keybuf[0] == '[' && keybuf[1] >= 'A' && keybuf[1] <= 'D')
149    {
150        /* ^[[A ^[[B ^[[C ^[[D */
151        static unsigned int keylist[] =
152            { CACA_KEY_UP, CACA_KEY_DOWN, CACA_KEY_RIGHT, CACA_KEY_LEFT };
153        _pop_key();
154        return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'A'];
155    }
156    else if(keybuf[0] == '[' && keybuf[1] == 'M' &&
157            keybuf[2] && keybuf[3] && keybuf[3])
158    {
159        /* ^[[Mxxx */
160        _pop_key();
161        _pop_key();
162        event |= (_pop_key() - ' ') << 16;
163        event |= (_pop_key() - '!') << 8;
164        event |= (_pop_key() - '!') << 0;
165
166        return CACA_EVENT_MOUSE_CLICK | event;
167    }
168    else if(keybuf[0] == '[' && keybuf[1] == '1' && keybuf[3] == '~' &&
169            keybuf[2] >= '5' && keybuf[2] != '6' && keybuf[2] <= '9')
170    {
171        /* ^[[15~ ^[[17~ ^[[18~ ^[[19~ */
172        static unsigned int keylist[] =
173            { CACA_KEY_F5, 0, CACA_KEY_F6, CACA_KEY_F7, CACA_KEY_F8 };
174        _pop_key();
175        _pop_key();
176        event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '5'];
177        _pop_key();
178        return event;
179    }
180    else if(keybuf[0] == '[' && keybuf[1] == '2' && keybuf[3] == '~' &&
181            keybuf[2] >= '0' && keybuf[2] != '2' && keybuf[2] <= '4')
182    {
183        /* ^[[20~ ^[[21~ ^[[23~ ^[[24~ */
184        static unsigned int keylist[] =
185            { CACA_KEY_F9, CACA_KEY_F10, 0, CACA_KEY_F11, CACA_KEY_F12 };
186        _pop_key();
187        _pop_key();
188        event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '0'];
189        _pop_key();
190        return event;
191    }
192
193    /* Unknown escape sequence: return the ESC key */
194    return CACA_EVENT_KEY_PRESS | '\x1b';
195}
196
197/** \brief Wait for the next mouse or keyboard input event.
198 *
199 *  This function returns the first mouse or keyboard event in the queue. If
200 *  no event is pending, it blocks until an event is received. See also
201 *  caca_get_event() for a non-blocking version of this function.
202 *
203 *  \return The next event in the queue.
204 */
205unsigned int caca_wait_event(void)
206{
207    for( ; ; )
208    {
209        unsigned int event = caca_get_event();
210
211        if(event)
212            return event;
213
214        usleep(1000);
215    }
216}
217
218/*
219 * XXX: The following functions are local.
220 */
221
222static void _push_key(unsigned int key)
223{
224    if(keys == KEY_BUFLEN)
225        return;
226    keybuf[keys] = key;
227    keys++;
228    keybuf[keys] = 0;
229}
230
231static unsigned int _pop_key(void)
232{
233    int i;
234    unsigned int key = keybuf[0];
235    keys--;
236    for(i = 0; i < keys; i++)
237        keybuf[i] = keybuf[i + 1];
238    keybuf[keys] = 0;
239
240    return key;
241}
242
243static unsigned int _read_key(void)
244{
245#if defined(USE_NCURSES)
246    int intkey;
247#endif
248#if defined(USE_X11)
249    XEvent event;
250    char key;
251#endif
252
253    switch(_caca_driver)
254    {
255#if defined(USE_SLANG)
256    case CACA_DRIVER_SLANG:
257        return SLang_input_pending(0) ? SLang_getkey() : 0;
258#endif
259#if defined(USE_NCURSES)
260    case CACA_DRIVER_NCURSES:
261        intkey = getch();
262        return (intkey == ERR) ? 0 : intkey;
263#endif
264#if defined(USE_CONIO)
265    case CACA_DRIVER_CONIO:
266        return _conio_kbhit() ? getch() : 0;
267#endif
268#if defined(USE_X11)
269    case CACA_DRIVER_X11:
270        while(XCheckWindowEvent(x11_dpy, x11_window, KeyPressMask, &event)
271               == True)
272        {
273            KeySym keysym;
274
275            if(event.type != KeyPress)
276                continue;
277
278            if(XLookupString(&event.xkey, &key, 1, NULL, NULL))
279                return key;
280
281            keysym = XKeycodeToKeysym(x11_dpy, event.xkey.keycode, 0);
282            switch(keysym)
283            {
284            case XK_F1:    return CACA_KEY_F1;
285            case XK_F2:    return CACA_KEY_F2;
286            case XK_F3:    return CACA_KEY_F3;
287            case XK_F4:    return CACA_KEY_F4;
288            case XK_F5:    return CACA_KEY_F5;
289            case XK_F6:    return CACA_KEY_F6;
290            case XK_F7:    return CACA_KEY_F7;
291            case XK_F8:    return CACA_KEY_F8;
292            case XK_F9:    return CACA_KEY_F9;
293            case XK_F10:   return CACA_KEY_F10;
294            case XK_F11:   return CACA_KEY_F11;
295            case XK_F12:   return CACA_KEY_F12;
296            case XK_F13:   return CACA_KEY_F13;
297            case XK_F14:   return CACA_KEY_F14;
298            case XK_F15:   return CACA_KEY_F15;
299            case XK_Left:  return CACA_KEY_LEFT;
300            case XK_Right: return CACA_KEY_RIGHT;
301            case XK_Up:    return CACA_KEY_UP;
302            case XK_Down:  return CACA_KEY_DOWN;
303            default:       return 0;
304            }
305        }
306
307        return 0;
308#endif
309    default:
310        break;
311    }
312
313    return 0;
314}
315
Note: See TracBrowser for help on using the repository browser.