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

Last change on this file since 201 was 201, checked in by Sam Hocevar, 17 years ago
  • TODO: New stuff to do.
  • src/io.c: left and right arrow keys were swapped.
  • Property svn:keywords set to Id
File size: 4.4 KB
Line 
1/*
2 *   libcaca       ASCII-Art library
3 *   Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
4 *                 All Rights Reserved
5 *
6 *   $Id: io.c 201 2003-11-19 18:38:49Z sam $
7 *
8 *   This library is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU Lesser General Public
10 *   License as published by the Free Software Foundation; either
11 *   version 2 of the License, or (at your option) any later version.
12 *
13 *   This library is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 *   Lesser General Public License for more details.
17 *
18 *   You should have received a copy of the GNU Lesser General Public
19 *   License along with this library; if not, write to the Free Software
20 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 *   02111-1307  USA
22 */
23
24#include "config.h"
25
26#if defined(USE_SLANG)
27#   include <slang.h>
28#elif defined(USE_NCURSES)
29#   include <curses.h>
30#elif defined(USE_CONIO)
31#   include <conio.h>
32#else
33#   error "no graphics library detected"
34#endif
35
36#include "caca.h"
37#include "caca_internals.h"
38
39static void _push_key(unsigned char);
40static unsigned char _pop_key(void);
41static unsigned char _read_key(void);
42
43static unsigned char back[5] = {0, 0, 0, 0, 0};
44
45int caca_get_event(void)
46{
47    unsigned char key[6];
48
49    /* If there were legacy keys, pop them */
50    key[0] = _pop_key();
51    if(key[0])
52        return CACA_EVENT_KEY_PRESS | key[0];
53
54    key[0] = _read_key();
55    if(!key[0])
56        return 0;
57
58    if(key[0] != 0x1b)
59        return CACA_EVENT_KEY_PRESS | key[0];
60
61    /*
62     * Handle escape sequences
63     */
64
65    key[1] = _read_key();
66    if(!key[1])
67        return CACA_EVENT_KEY_PRESS | key[0];
68
69    key[2] = _read_key();
70    if(!key[2])
71    {
72        _push_key(key[1]);
73        return CACA_EVENT_KEY_PRESS | key[0];
74    }
75
76    if(key[1] == 'O')
77    {
78        switch(key[2])
79        {
80        case 'P': return CACA_EVENT_KEY_PRESS | CACA_KEY_F1;
81        case 'Q': return CACA_EVENT_KEY_PRESS | CACA_KEY_F2;
82        case 'R': return CACA_EVENT_KEY_PRESS | CACA_KEY_F3;
83        case 'S': return CACA_EVENT_KEY_PRESS | CACA_KEY_F4;
84        }
85    }
86    else if(key[1] == '[')
87    {
88        switch(key[2])
89        {
90        case 'A': return CACA_EVENT_KEY_PRESS | CACA_KEY_UP;
91        case 'B': return CACA_EVENT_KEY_PRESS | CACA_KEY_DOWN;
92        case 'C': return CACA_EVENT_KEY_PRESS | CACA_KEY_RIGHT;
93        case 'D': return CACA_EVENT_KEY_PRESS | CACA_KEY_LEFT;
94        }
95
96        key[3] = _read_key();
97        key[4] = _read_key();
98
99        if(key[2] == 'M')
100        {
101            int event = CACA_EVENT_MOUSE_CLICK;
102
103            key[5] = _read_key();
104
105            event |= (int)(key[3] - ' ') << 16;
106            event |= (int)(key[4] - ' ') << 8;
107            event |= (int)(key[5] - ' ') << 0;
108
109            return event;
110        }
111
112        if(key[2] == '1' && key[4] == '~')
113            switch(key[3])
114            {
115            case '5': return CACA_EVENT_KEY_PRESS | CACA_KEY_F5;
116            case '7': return CACA_EVENT_KEY_PRESS | CACA_KEY_F6;
117            case '8': return CACA_EVENT_KEY_PRESS | CACA_KEY_F7;
118            case '9': return CACA_EVENT_KEY_PRESS | CACA_KEY_F8;
119            }
120
121        if(key[2] == '2' && key[4] == '~')
122            switch(key[3])
123            {
124            case '0': return CACA_EVENT_KEY_PRESS | CACA_KEY_F9;
125            case '1': return CACA_EVENT_KEY_PRESS | CACA_KEY_F10;
126            case '3': return CACA_EVENT_KEY_PRESS | CACA_KEY_F11;
127            case '4': return CACA_EVENT_KEY_PRESS | CACA_KEY_F12;
128            }
129
130        _push_key(key[4]);
131        _push_key(key[3]);
132    }
133
134    /* Unknown escape sequence: return each key one after the other */
135    _push_key(key[2]);
136    _push_key(key[1]);
137    return CACA_EVENT_KEY_PRESS | key[0];
138}
139
140static void _push_key(unsigned char key)
141{
142    back[4] = back[3];
143    back[3] = back[2];
144    back[2] = back[1];
145    back[1] = back[0];
146    back[0] = key;
147}
148
149static unsigned char _pop_key(void)
150{
151    unsigned char key = back[0];
152    back[0] = back[1];
153    back[1] = back[2];
154    back[2] = back[3];
155    back[3] = back[4];
156    return key;
157}
158
159static unsigned char _read_key(void)
160{
161#if defined(USE_SLANG)
162    return SLang_input_pending(0) ? SLang_getkey() : 0;
163#elif defined(USE_NCURSES)
164    unsigned char key = getch();
165    return (key == ERR) ? 0 : key;
166#elif defined(USE_CONIO)
167    return _conio_kbhit() ? getch() : 0;
168#endif
169}
170
Note: See TracBrowser for help on using the repository browser.