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

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