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

Last change on this file since 268 was 268, checked in by Sam Hocevar, 18 years ago
  • src/io.c: + Added caca_wait_event(), a blocking caca_get_event().
  • src/ examples/: + More documentation.
  • doc/doxygen.cfg.in: + doc/doxygen.cfg is now generated by configure, so that we can use

@top_srcdir@ and we no longer need to hardcode PROJECT_NUMBER.

+ Create manpages.

  • Property svn:keywords set to Id
File size: 8.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 268 2003-12-23 13:27:40Z 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#endif
45
46#include "caca.h"
47#include "caca_internals.h"
48
49static void _push_key(unsigned int);
50static unsigned int _pop_key(void);
51static unsigned int _read_key(void);
52
53#define KEY_BUFLEN 10
54static unsigned int keybuf[KEY_BUFLEN + 1]; /* zero-terminated */
55static int keys = 0;
56
57/** \brief Get the next mouse or keyboard input event.
58 *
59 *  This function polls the event queue for mouse or keyboard events and
60 *  returns the event. It is non-blocking and returns zero if no event is
61 *  pending in the queue. See also caca_wait_event() for a blocking version
62 *  of this function.
63 *
64 * \return The next event in the queue, or 0 if no event is pending.
65 */
66unsigned int caca_get_event(void)
67{
68    unsigned int event = 0;
69
70    /* Read all available key events */
71    while(keys < KEY_BUFLEN)
72    {
73        unsigned int key = _read_key();
74        if(!key)
75            break;
76        _push_key(key);
77    }
78
79    if(!keys)
80        return 0;
81
82#if defined(USE_NCURSES)
83    if(_caca_driver == CACA_DRIVER_NCURSES)
84    {
85        if(keybuf[0] == KEY_MOUSE)
86        {
87            MEVENT mevent;
88            _pop_key();
89            getmouse(&mevent);
90
91            event |= (1) << 16;
92            event |= (mevent.x) << 8;
93            event |= (mevent.y) << 0;
94
95            return CACA_EVENT_MOUSE_CLICK | event;
96        }
97
98        switch(keybuf[0])
99        {
100            case KEY_UP: event = CACA_EVENT_KEY_PRESS | CACA_KEY_UP; break;
101            case KEY_DOWN: event = CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN; break;
102            case KEY_LEFT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT; break;
103            case KEY_RIGHT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT; break;
104
105            case KEY_F(1): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F1; break;
106            case KEY_F(2): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F2; break;
107            case KEY_F(3): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F3; break;
108            case KEY_F(4): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F4; break;
109            case KEY_F(5): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F5; break;
110            case KEY_F(6): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F6; break;
111            case KEY_F(7): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F7; break;
112            case KEY_F(8): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F8; break;
113            case KEY_F(9): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F9; break;
114            case KEY_F(10): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F10; break;
115            case KEY_F(11): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F11; break;
116            case KEY_F(12): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F12; break;
117        }
118
119        if(event)
120        {
121            _pop_key();
122            return event;
123        }
124    }
125#endif
126
127    if(keybuf[0] != '\x1b')
128        return CACA_EVENT_KEY_PRESS | _pop_key();
129
130    /*
131     * Handle known escape sequences
132     */
133
134    _pop_key();
135
136    if(keybuf[0] == 'O' && keybuf[1] >= 'P' && keybuf[1] <= 'S')
137    {
138        /* ^[OP ^[OQ ^[OR ^[OS */
139        static unsigned int keylist[] =
140            { CACA_KEY_F1, CACA_KEY_F2, CACA_KEY_F3, CACA_KEY_F4 };
141        _pop_key();
142        return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'P'];
143    }
144    else if(keybuf[0] == '[' && keybuf[1] >= 'A' && keybuf[1] <= 'D')
145    {
146        /* ^[[A ^[[B ^[[C ^[[D */
147        static unsigned int keylist[] =
148            { CACA_KEY_UP, CACA_KEY_DOWN, CACA_KEY_RIGHT, CACA_KEY_LEFT };
149        _pop_key();
150        return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'A'];
151    }
152    else if(keybuf[0] == '[' && keybuf[1] == 'M' &&
153            keybuf[2] && keybuf[3] && keybuf[3])
154    {
155        /* ^[[Mxxx */
156        _pop_key();
157        _pop_key();
158        event |= (_pop_key() - ' ') << 16;
159        event |= (_pop_key() - '!') << 8;
160        event |= (_pop_key() - '!') << 0;
161
162        return CACA_EVENT_MOUSE_CLICK | event;
163    }
164    else if(keybuf[0] == '[' && keybuf[1] == '1' && keybuf[3] == '~' &&
165            keybuf[2] >= '5' && keybuf[2] != '6' && keybuf[2] <= '9')
166    {
167        /* ^[[15~ ^[[17~ ^[[18~ ^[[19~ */
168        static unsigned int keylist[] =
169            { CACA_KEY_F5, 0, CACA_KEY_F6, CACA_KEY_F7, CACA_KEY_F8 };
170        _pop_key();
171        _pop_key();
172        event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '5'];
173        _pop_key();
174        return event;
175    }
176    else if(keybuf[0] == '[' && keybuf[1] == '2' && keybuf[3] == '~' &&
177            keybuf[2] >= '0' && keybuf[2] != '2' && keybuf[2] <= '4')
178    {
179        /* ^[[20~ ^[[21~ ^[[23~ ^[[24~ */
180        static unsigned int keylist[] =
181            { CACA_KEY_F9, CACA_KEY_F10, 0, CACA_KEY_F11, CACA_KEY_F12 };
182        _pop_key();
183        _pop_key();
184        event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '0'];
185        _pop_key();
186        return event;
187    }
188
189    /* Unknown escape sequence: return the ESC key */
190    return CACA_EVENT_KEY_PRESS | '\x1b';
191}
192
193/** \brief Wait for the next mouse or keyboard input event.
194 *
195 *  This function returns the first mouse or keyboard event in the queue. If
196 *  no event is pending, it blocks until an event is received. See also
197 *  caca_get_event() for a non-blocking version of this function.
198 *
199 *  \return The next event in the queue.
200 */
201unsigned int caca_wait_event(void)
202{
203    for( ; ; )
204    {
205        unsigned int event = caca_get_event();
206
207        if(event)
208            return event;
209
210        usleep(1000);
211    }
212}
213
214/*
215 * XXX: The following functions are local.
216 */
217
218static void _push_key(unsigned int key)
219{
220    if(keys == KEY_BUFLEN)
221        return;
222    keybuf[keys] = key;
223    keys++;
224    keybuf[keys] = 0;
225}
226
227static unsigned int _pop_key(void)
228{
229    int i;
230    unsigned int key = keybuf[0];
231    keys--;
232    for(i = 0; i < keys; i++)
233        keybuf[i] = keybuf[i + 1];
234    keybuf[keys] = 0;
235
236    return key;
237}
238
239static unsigned int _read_key(void)
240{
241#if defined(USE_NCURSES)
242    int intkey;
243#endif
244#if defined(USE_X11)
245    XEvent event;
246    char key;
247#endif
248
249    switch(_caca_driver)
250    {
251#if defined(USE_SLANG)
252    case CACA_DRIVER_SLANG:
253        return SLang_input_pending(0) ? SLang_getkey() : 0;
254#endif
255#if defined(USE_NCURSES)
256    case CACA_DRIVER_NCURSES:
257        intkey = getch();
258        return (intkey == ERR) ? 0 : intkey;
259#endif
260#if defined(USE_CONIO)
261    case CACA_DRIVER_CONIO:
262        return _conio_kbhit() ? getch() : 0;
263#endif
264#if defined(USE_X11)
265    case CACA_DRIVER_X11:
266        while(XCheckWindowEvent(x11_dpy, x11_window, KeyPressMask, &event)
267               == True)
268        {
269            if(event.type == KeyPress)
270            {
271                //KeySym keysym;
272                //keysym = XKeycodeToKeysym(_caca_dpy, event.xkey.keycode, 0);
273                if(XLookupString(&event.xkey, &key, 1, NULL, NULL))
274                    return key;
275            }
276        }
277
278        return 0;
279#endif
280    default:
281        break;
282    }
283
284    return 0;
285}
286
Note: See TracBrowser for help on using the repository browser.