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

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