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

Last change on this file since 265 was 265, checked in by Sam Hocevar, 17 years ago
  • src/graphics.c src/caca.c src/io.c: + Support for simultaneously compiled-in drivers. + Honour the CACA_DRIVER environment variable.
  • configure.ac: + Drivers are no longer mutually exclusive.
  • Property svn:keywords set to Id
File size: 7.2 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 265 2003-12-22 15:26:12Z 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/**
58 * \brief Get the next mouse or keyboard input event.
59 *
60 * \return The next event in the queue, or 0 if no event is pending.
61 */
62unsigned int caca_get_event(void)
63{
64    unsigned int event = 0;
65
66    /* Read all available key events */
67    while(keys < KEY_BUFLEN)
68    {
69        unsigned int key = _read_key();
70        if(!key)
71            break;
72        _push_key(key);
73    }
74
75    if(!keys)
76        return 0;
77
78#if defined(USE_NCURSES)
79    if(_caca_driver == CACA_DRIVER_NCURSES)
80    {
81        if(keybuf[0] == KEY_MOUSE)
82        {
83            MEVENT mevent;
84            _pop_key();
85            getmouse(&mevent);
86
87            event |= (1) << 16;
88            event |= (mevent.x) << 8;
89            event |= (mevent.y) << 0;
90
91            return CACA_EVENT_MOUSE_CLICK | event;
92        }
93
94        switch(keybuf[0])
95        {
96            case KEY_UP: event = CACA_EVENT_KEY_PRESS | CACA_KEY_UP; break;
97            case KEY_DOWN: event = CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN; break;
98            case KEY_LEFT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT; break;
99            case KEY_RIGHT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT; break;
100
101            case KEY_F(1): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F1; break;
102            case KEY_F(2): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F2; break;
103            case KEY_F(3): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F3; break;
104            case KEY_F(4): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F4; break;
105            case KEY_F(5): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F5; break;
106            case KEY_F(6): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F6; break;
107            case KEY_F(7): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F7; break;
108            case KEY_F(8): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F8; break;
109            case KEY_F(9): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F9; break;
110            case KEY_F(10): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F10; break;
111            case KEY_F(11): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F11; break;
112            case KEY_F(12): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F12; break;
113        }
114
115        if(event)
116        {
117            _pop_key();
118            return event;
119        }
120    }
121#endif
122
123    if(keybuf[0] != '\x1b')
124        return CACA_EVENT_KEY_PRESS | _pop_key();
125
126    /*
127     * Handle known escape sequences
128     */
129
130    _pop_key();
131
132    if(keybuf[0] == 'O' && keybuf[1] >= 'P' && keybuf[1] <= 'S')
133    {
134        /* ^[OP ^[OQ ^[OR ^[OS */
135        static unsigned int keylist[] =
136            { CACA_KEY_F1, CACA_KEY_F2, CACA_KEY_F3, CACA_KEY_F4 };
137        _pop_key();
138        return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'P'];
139    }
140    else if(keybuf[0] == '[' && keybuf[1] >= 'A' && keybuf[1] <= 'D')
141    {
142        /* ^[[A ^[[B ^[[C ^[[D */
143        static unsigned int keylist[] =
144            { CACA_KEY_UP, CACA_KEY_DOWN, CACA_KEY_RIGHT, CACA_KEY_LEFT };
145        _pop_key();
146        return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'A'];
147    }
148    else if(keybuf[0] == '[' && keybuf[1] == 'M' &&
149            keybuf[2] && keybuf[3] && keybuf[3])
150    {
151        /* ^[[Mxxx */
152        _pop_key();
153        _pop_key();
154        event |= (_pop_key() - ' ') << 16;
155        event |= (_pop_key() - '!') << 8;
156        event |= (_pop_key() - '!') << 0;
157
158        return CACA_EVENT_MOUSE_CLICK | event;
159    }
160    else if(keybuf[0] == '[' && keybuf[1] == '1' && keybuf[3] == '~' &&
161            keybuf[2] >= '5' && keybuf[2] != '6' && keybuf[2] <= '9')
162    {
163        /* ^[[15~ ^[[17~ ^[[18~ ^[[19~ */
164        static unsigned int keylist[] =
165            { CACA_KEY_F5, 0, CACA_KEY_F6, CACA_KEY_F7, CACA_KEY_F8 };
166        _pop_key();
167        _pop_key();
168        event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '5'];
169        _pop_key();
170        return event;
171    }
172    else if(keybuf[0] == '[' && keybuf[1] == '2' && keybuf[3] == '~' &&
173            keybuf[2] >= '0' && keybuf[2] != '2' && keybuf[2] <= '4')
174    {
175        /* ^[[20~ ^[[21~ ^[[23~ ^[[24~ */
176        static unsigned int keylist[] =
177            { CACA_KEY_F9, CACA_KEY_F10, 0, CACA_KEY_F11, CACA_KEY_F12 };
178        _pop_key();
179        _pop_key();
180        event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '0'];
181        _pop_key();
182        return event;
183    }
184
185    /* Unknown escape sequence: return the ESC key */
186    return CACA_EVENT_KEY_PRESS | '\x1b';
187}
188
189static void _push_key(unsigned int key)
190{
191    if(keys == KEY_BUFLEN)
192        return;
193    keybuf[keys] = key;
194    keys++;
195    keybuf[keys] = 0;
196}
197
198static unsigned int _pop_key(void)
199{
200    int i;
201    unsigned int key = keybuf[0];
202    keys--;
203    for(i = 0; i < keys; i++)
204        keybuf[i] = keybuf[i + 1];
205    keybuf[keys] = 0;
206
207    return key;
208}
209
210static unsigned int _read_key(void)
211{
212#if defined(USE_NCURSES)
213    int intkey;
214#endif
215#if defined(USE_X11)
216    XEvent event;
217    char key;
218#endif
219
220    switch(_caca_driver)
221    {
222#if defined(USE_SLANG)
223    case CACA_DRIVER_SLANG:
224        return SLang_input_pending(0) ? SLang_getkey() : 0;
225#endif
226#if defined(USE_NCURSES)
227    case CACA_DRIVER_NCURSES:
228        intkey = getch();
229        return (intkey == ERR) ? 0 : intkey;
230#endif
231#if defined(USE_CONIO)
232    case CACA_DRIVER_CONIO:
233        return _conio_kbhit() ? getch() : 0;
234#endif
235#if defined(USE_X11)
236    case CACA_DRIVER_X11:
237        while(XCheckWindowEvent(x11_dpy, x11_window, KeyPressMask, &event)
238               == True)
239        {
240            if(event.type == KeyPress)
241            {
242                //KeySym keysym;
243                //keysym = XKeycodeToKeysym(_caca_dpy, event.xkey.keycode, 0);
244                if(XLookupString(&event.xkey, &key, 1, NULL, NULL))
245                    return key;
246            }
247        }
248
249        return 0;
250#endif
251    default:
252        break;
253    }
254
255    return 0;
256}
257
Note: See TracBrowser for help on using the repository browser.