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

Last change on this file since 298 was 298, checked in by Sam Hocevar, 17 years ago
  • doc/doxygen.cfg.in: + Removed caca_internal.h from the list of documented files.
  • src/: + Minor documentation updates.
  • 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 298 2003-12-31 14:21:08Z 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#   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 newx = xevent.xmotion.x / x11_font_width;
90                unsigned int newy = xevent.xmotion.y / x11_font_height;
91
92                if(x11_x == newx && x11_y == newy)
93                    continue;
94
95                if(newx >= _caca_width)
96                    newx = _caca_width - 1;
97                if(newy >= _caca_height)
98                    newy = _caca_height - 1;
99
100                x11_x = newx & 0xfff;
101                x11_y = newy & 0xfff;
102
103                return CACA_EVENT_MOUSE_MOTION | (newx << 12) | (newy << 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            _push_key(CACA_EVENT_MOUSE_PRESS | 1);
174            return CACA_EVENT_MOUSE_MOTION
175                    | (mevent.x << 12) | (mevent.y << 0);
176        }
177
178        switch(keybuf[0])
179        {
180            case KEY_UP: event = CACA_EVENT_KEY_PRESS | CACA_KEY_UP; break;
181            case KEY_DOWN: event = CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN; break;
182            case KEY_LEFT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT; break;
183            case KEY_RIGHT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT; break;
184
185            case KEY_F(1): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F1; break;
186            case KEY_F(2): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F2; break;
187            case KEY_F(3): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F3; break;
188            case KEY_F(4): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F4; break;
189            case KEY_F(5): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F5; break;
190            case KEY_F(6): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F6; break;
191            case KEY_F(7): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F7; break;
192            case KEY_F(8): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F8; break;
193            case KEY_F(9): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F9; break;
194            case KEY_F(10): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F10; break;
195            case KEY_F(11): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F11; break;
196            case KEY_F(12): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F12; break;
197        }
198
199        if(event)
200        {
201            _pop_key();
202            return event;
203        }
204    }
205#endif
206
207    /* If it's already a special event, return it */
208    if((keybuf[0] & ~0xff) != 0)
209        return _pop_key();
210
211    /* If it's not an escape sequence, return the key */
212    if(keybuf[0] != '\x1b')
213        return CACA_EVENT_KEY_PRESS | _pop_key();
214
215    /*
216     * Handle known escape sequences
217     */
218
219    _pop_key();
220
221    if(keybuf[0] == 'O' && keybuf[1] >= 'P' && keybuf[1] <= 'S')
222    {
223        /* ^[OP ^[OQ ^[OR ^[OS */
224        static unsigned int keylist[] =
225            { CACA_KEY_F1, CACA_KEY_F2, CACA_KEY_F3, CACA_KEY_F4 };
226        _pop_key();
227        return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'P'];
228    }
229    else if(keybuf[0] == '[' && keybuf[1] >= 'A' && keybuf[1] <= 'D')
230    {
231        /* ^[[A ^[[B ^[[C ^[[D */
232        static unsigned int keylist[] =
233            { CACA_KEY_UP, CACA_KEY_DOWN, CACA_KEY_RIGHT, CACA_KEY_LEFT };
234        _pop_key();
235        return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'A'];
236    }
237    else if(keybuf[0] == '[' && keybuf[1] == 'M' &&
238            keybuf[2] && keybuf[3] && keybuf[3])
239    {
240        /* ^[[Mxxx */
241        _pop_key();
242        _pop_key();
243        _push_key(CACA_EVENT_MOUSE_PRESS | (_pop_key() - ' '));
244        return CACA_EVENT_MOUSE_MOTION
245                | ((_pop_key() - '!') << 12) | ((_pop_key() - '!') << 0);
246    }
247    else if(keybuf[0] == '[' && keybuf[1] == '1' && keybuf[3] == '~' &&
248            keybuf[2] >= '5' && keybuf[2] != '6' && keybuf[2] <= '9')
249    {
250        /* ^[[15~ ^[[17~ ^[[18~ ^[[19~ */
251        static unsigned int keylist[] =
252            { CACA_KEY_F5, 0, CACA_KEY_F6, CACA_KEY_F7, CACA_KEY_F8 };
253        _pop_key();
254        _pop_key();
255        event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '5'];
256        _pop_key();
257        return event;
258    }
259    else if(keybuf[0] == '[' && keybuf[1] == '2' && keybuf[3] == '~' &&
260            keybuf[2] >= '0' && keybuf[2] != '2' && keybuf[2] <= '4')
261    {
262        /* ^[[20~ ^[[21~ ^[[23~ ^[[24~ */
263        static unsigned int keylist[] =
264            { CACA_KEY_F9, CACA_KEY_F10, 0, CACA_KEY_F11, CACA_KEY_F12 };
265        _pop_key();
266        _pop_key();
267        event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '0'];
268        _pop_key();
269        return event;
270    }
271
272    /* Unknown escape sequence: return the ESC key */
273    return CACA_EVENT_KEY_PRESS | '\x1b';
274}
275
276/** \brief Wait for the next mouse or keyboard input event.
277 *
278 *  This function returns the first mouse or keyboard event in the queue. If
279 *  no event is pending, it blocks until an event is received. See also
280 *  caca_get_event() for a non-blocking version of this function.
281 *
282 *  \return The next event in the queue.
283 */
284unsigned int caca_wait_event(void)
285{
286    for( ; ; )
287    {
288        unsigned int event = caca_get_event();
289
290        if(event)
291            return event;
292
293        usleep(1000);
294    }
295}
296
297/*
298 * XXX: The following functions are local.
299 */
300
301static void _push_key(unsigned int key)
302{
303    if(keys == KEY_BUFLEN)
304        return;
305    keybuf[keys] = key;
306    keys++;
307    keybuf[keys] = 0;
308}
309
310static unsigned int _pop_key(void)
311{
312    int i;
313    unsigned int key = keybuf[0];
314    keys--;
315    for(i = 0; i < keys; i++)
316        keybuf[i] = keybuf[i + 1];
317    keybuf[keys] = 0;
318
319    return key;
320}
321
322static unsigned int _read_key(void)
323{
324#if defined(USE_NCURSES)
325    int intkey;
326#endif
327#if defined(USE_X11)
328#endif
329
330    switch(_caca_driver)
331    {
332#if defined(USE_SLANG)
333    case CACA_DRIVER_SLANG:
334        return SLang_input_pending(0) ? SLang_getkey() : 0;
335#endif
336#if defined(USE_NCURSES)
337    case CACA_DRIVER_NCURSES:
338        intkey = getch();
339        return (intkey == ERR) ? 0 : intkey;
340#endif
341#if defined(USE_CONIO)
342    case CACA_DRIVER_CONIO:
343        return _conio_kbhit() ? getch() : 0;
344#endif
345#if defined(USE_X11)
346    case CACA_DRIVER_X11:
347#endif
348    default:
349        break;
350    }
351
352    return 0;
353}
354
Note: See TracBrowser for help on using the repository browser.