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

Last change on this file since 257 was 257, checked in by Sam Hocevar, 19 years ago
  • src/: + Doxygenated public functions.
  • Property svn:keywords set to Id
File size: 6.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 257 2003-12-18 00:11:52Z 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#elif defined(USE_NCURSES)
35#   include <curses.h>
36#elif defined(USE_CONIO)
37#   include <conio.h>
38#elif defined(USE_X11)
39#   include <X11/Xlib.h>
40#   include <X11/Xutil.h>
41#else
42#   error "no graphics library detected"
43#endif
44
45#include "caca.h"
46#include "caca_internals.h"
47
48static void _push_key(unsigned int);
49static unsigned int _pop_key(void);
50static unsigned int _read_key(void);
51
52#define KEY_BUFLEN 10
53static unsigned int keybuf[KEY_BUFLEN + 1]; /* zero-terminated */
54static int keys = 0;
55
56/**
57 * \brief Get the next mouse or keyboard input event.
58 *
59 * \return The next event in the queue, or 0 if no event is pending.
60 */
61unsigned int caca_get_event(void)
62{
63    unsigned int event = 0;
64
65    /* Read all available key events */
66    while(keys < KEY_BUFLEN)
67    {
68        unsigned int key = _read_key();
69        if(!key)
70            break;
71        _push_key(key);
72    }
73
74    if(!keys)
75        return 0;
76
77#if defined(USE_NCURSES)
78    if(keybuf[0] == KEY_MOUSE)
79    {
80        MEVENT mevent;
81        _pop_key();
82        getmouse(&mevent);
83
84        event |= (1) << 16;
85        event |= (mevent.x) << 8;
86        event |= (mevent.y) << 0;
87
88        return CACA_EVENT_MOUSE_CLICK | event;
89    }
90
91    switch(keybuf[0])
92    {
93        case KEY_UP: event = CACA_EVENT_KEY_PRESS | CACA_KEY_UP; break;
94        case KEY_DOWN: event = CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN; break;
95        case KEY_LEFT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT; break;
96        case KEY_RIGHT: event = CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT; break;
97
98        case KEY_F(1): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F1; break;
99        case KEY_F(2): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F2; break;
100        case KEY_F(3): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F3; break;
101        case KEY_F(4): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F4; break;
102        case KEY_F(5): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F5; break;
103        case KEY_F(6): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F6; break;
104        case KEY_F(7): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F7; break;
105        case KEY_F(8): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F8; break;
106        case KEY_F(9): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F9; break;
107        case KEY_F(10): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F10; break;
108        case KEY_F(11): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F11; break;
109        case KEY_F(12): event = CACA_EVENT_KEY_PRESS | CACA_KEY_F12; break;
110    }
111
112    if(event)
113    {
114        _pop_key();
115        return event;
116    }
117#endif
118
119    if(keybuf[0] != '\x1b')
120        return CACA_EVENT_KEY_PRESS | _pop_key();
121
122    /*
123     * Handle known escape sequences
124     */
125
126    _pop_key();
127
128    if(keybuf[0] == 'O' && keybuf[1] >= 'P' && keybuf[1] <= 'S')
129    {
130        /* ^[OP ^[OQ ^[OR ^[OS */
131        static unsigned int keylist[] =
132            { CACA_KEY_F1, CACA_KEY_F2, CACA_KEY_F3, CACA_KEY_F4 };
133        _pop_key();
134        return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'P'];
135    }
136    else if(keybuf[0] == '[' && keybuf[1] >= 'A' && keybuf[1] <= 'D')
137    {
138        /* ^[[A ^[[B ^[[C ^[[D */
139        static unsigned int keylist[] =
140            { CACA_KEY_UP, CACA_KEY_DOWN, CACA_KEY_RIGHT, CACA_KEY_LEFT };
141        _pop_key();
142        return CACA_EVENT_KEY_PRESS | keylist[_pop_key() - 'A'];
143    }
144    else if(keybuf[0] == '[' && keybuf[1] == 'M' &&
145            keybuf[2] && keybuf[3] && keybuf[3])
146    {
147        /* ^[[Mxxx */
148        _pop_key();
149        _pop_key();
150        event |= (_pop_key() - ' ') << 16;
151        event |= (_pop_key() - '!') << 8;
152        event |= (_pop_key() - '!') << 0;
153
154        return CACA_EVENT_MOUSE_CLICK | event;
155    }
156    else if(keybuf[0] == '[' && keybuf[1] == '1' && keybuf[3] == '~' &&
157            keybuf[2] >= '5' && keybuf[2] != '6' && keybuf[2] <= '9')
158    {
159        /* ^[[15~ ^[[17~ ^[[18~ ^[[19~ */
160        static unsigned int keylist[] =
161            { CACA_KEY_F5, 0, CACA_KEY_F6, CACA_KEY_F7, CACA_KEY_F8 };
162        _pop_key();
163        _pop_key();
164        event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '5'];
165        _pop_key();
166        return event;
167    }
168    else if(keybuf[0] == '[' && keybuf[1] == '2' && keybuf[3] == '~' &&
169            keybuf[2] >= '0' && keybuf[2] != '2' && keybuf[2] <= '4')
170    {
171        /* ^[[20~ ^[[21~ ^[[23~ ^[[24~ */
172        static unsigned int keylist[] =
173            { CACA_KEY_F9, CACA_KEY_F10, 0, CACA_KEY_F11, CACA_KEY_F12 };
174        _pop_key();
175        _pop_key();
176        event = CACA_EVENT_KEY_PRESS | keylist[_pop_key() - '0'];
177        _pop_key();
178        return event;
179    }
180
181    /* Unknown escape sequence: return the ESC key */
182    return CACA_EVENT_KEY_PRESS | '\x1b';
183}
184
185static void _push_key(unsigned int key)
186{
187    if(keys == KEY_BUFLEN)
188        return;
189    keybuf[keys] = key;
190    keys++;
191    keybuf[keys] = 0;
192}
193
194static unsigned int _pop_key(void)
195{
196    int i;
197    unsigned int key = keybuf[0];
198    keys--;
199    for(i = 0; i < keys; i++)
200        keybuf[i] = keybuf[i + 1];
201    keybuf[keys] = 0;
202
203    return key;
204}
205
206static unsigned int _read_key(void)
207{
208#if defined(USE_SLANG)
209    return SLang_input_pending(0) ? SLang_getkey() : 0;
210#elif defined(USE_NCURSES)
211    int key = getch();
212    return (key == ERR) ? 0 : key;
213#elif defined(USE_CONIO)
214    return _conio_kbhit() ? getch() : 0;
215#elif defined(USE_X11)
216    XEvent event;
217    char key;
218
219    while(XCheckWindowEvent(_caca_dpy, _caca_window, KeyPressMask, &event)
220           == True)
221    {
222        if(event.type == KeyPress)
223        {
224            //KeySym keysym;
225            //keysym = XKeycodeToKeysym(_caca_dpy, event.xkey.keycode, 0);
226            if(XLookupString(&event.xkey, &key, 1, NULL, NULL))
227                return key;
228        }
229    }
230
231    return 0;
232#endif
233}
234
Note: See TracBrowser for help on using the repository browser.