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

Last change on this file since 198 was 198, checked in by Sam Hocevar, 19 years ago
  • src/io.c src/caca.h: + Interpret escape sequences for F1, F2, left, right, etc.
  • src/Makefile.am examples/Makefile.am: + Install caca.h in /usr/include. + Do not install example programs.
  • src/blit.c: + Minor improvements to grayscale colors.
  • Property svn:keywords set to Id
File size: 3.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 198 2003-11-17 16:11:34Z 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 unsigned char back[5] = {0, 0, 0, 0, 0};
40
41static void _push_key(unsigned char key)
42{
43    back[4] = back[3];
44    back[3] = back[2];
45    back[2] = back[1];
46    back[1] = back[0];
47    back[0] = key;
48}
49
50static unsigned char _pop_key(void)
51{
52    unsigned char key = back[0];
53    back[0] = back[1];
54    back[1] = back[2];
55    back[2] = back[3];
56    back[3] = back[4];
57    return key;
58}
59
60static unsigned char _read_key(void)
61{
62#if defined(USE_SLANG)
63    return SLang_input_pending(0) ? SLang_getkey() : 0;
64#elif defined(USE_NCURSES)
65    unsigned char key = getch();
66    return (key == ERR) ? 0 : key;
67#elif defined(USE_CONIO)
68    return _conio_kbhit() ? getch() : 0;
69#endif
70}
71
72int caca_get_key(void)
73{
74    unsigned char key[5];
75
76    /* If there were legacy keys, pop them */
77    key[0] = _pop_key();
78    if(key[0])
79        return key[0];
80
81    key[0] = _read_key();
82    if(key[0] != 0x1b)
83        return key[0];
84
85    key[1] = _read_key();
86    if(!key[1])
87        return key[0];
88
89    key[2] = _read_key();
90    if(!key[2])
91    {
92        _push_key(key[1]);
93        return key[0];
94    }
95
96    if(key[1] == 0x4f)
97    {
98        switch(key[2])
99        {
100        case 0x50: return CACA_KEY_F1;
101        case 0x51: return CACA_KEY_F2;
102        case 0x52: return CACA_KEY_F3;
103        case 0x53: return CACA_KEY_F4;
104        }
105    }
106    else if(key[1] == 0x5b)
107    {
108        switch(key[2])
109        {
110        case 0x41: return CACA_KEY_UP;
111        case 0x42: return CACA_KEY_DOWN;
112        case 0x43: return CACA_KEY_LEFT;
113        case 0x44: return CACA_KEY_RIGHT;
114        }
115
116        key[3] = _read_key();
117        key[4] = _read_key();
118
119        if(key[2] == 0x31 && key[4] == 0x7e)
120            switch(key[3])
121            {
122            case 0x35: return CACA_KEY_F5;
123            case 0x37: return CACA_KEY_F6;
124            case 0x38: return CACA_KEY_F7;
125            case 0x39: return CACA_KEY_F8;
126            }
127
128        if(key[2] == 0x32 && key[4] == 0x7e)
129            switch(key[3])
130            {
131            case 0x30: return CACA_KEY_F9;
132            case 0x31: return CACA_KEY_F10;
133            case 0x33: return CACA_KEY_F11;
134            case 0x34: return CACA_KEY_F12;
135            }
136
137        _push_key(key[4]);
138        _push_key(key[3]);
139    }
140
141    /* Unknown escape sequence: return 0 */
142    _push_key(key[2]);
143    _push_key(key[1]);
144    return key[0];
145}
146
Note: See TracBrowser for help on using the repository browser.