add binary cineform hd vfw decoder to codecs.conf
[mplayer/glamo.git] / osdep / getch2.c
blob4a591da2db141865401b28c45a27c60514e5360b
1 /*
2 * GyS-TermIO v2.0 (for GySmail v3)
3 * a very small replacement of ncurses library
5 * copyright (C) 1999 A'rpi/ESP-team
7 * This file is part of MPlayer.
9 * MPlayer is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * MPlayer is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "config.h"
26 //#define HAVE_TERMCAP
27 #if !defined(__OS2__) && !defined(__MORPHOS__)
28 #define CONFIG_IOCTL
29 #endif
31 #define MAX_KEYS 64
32 #define BUF_LEN 256
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/time.h>
38 #include <sys/types.h>
39 #ifdef CONFIG_IOCTL
40 #include <sys/ioctl.h>
41 #endif
43 #ifdef HAVE_TERMIOS
44 #ifdef HAVE_TERMIOS_H
45 #include <termios.h>
46 #endif
47 #ifdef HAVE_SYS_TERMIOS_H
48 #include <sys/termios.h>
49 #endif
50 #endif
52 #if defined(HAVE_LANGINFO) && defined(CONFIG_ICONV)
53 #include <locale.h>
54 #include <langinfo.h>
55 #endif
57 #include <unistd.h>
59 #include "mp_fifo.h"
60 #include "keycodes.h"
62 #ifdef HAVE_TERMIOS
63 static struct termios tio_orig;
64 #endif
65 static int getch2_len=0;
66 static char getch2_buf[BUF_LEN];
68 int screen_width=80;
69 int screen_height=24;
70 char * erase_to_end_of_line = NULL;
72 typedef struct {
73 int len;
74 int code;
75 char chars[8];
76 } keycode_st;
77 static keycode_st getch2_keys[MAX_KEYS];
78 static int getch2_key_db=0;
80 #ifdef HAVE_TERMCAP
82 #if 0
83 #include <termcap.h>
84 #else
85 int tgetent(char *BUFFER, char *TERMTYPE);
86 int tgetnum(char *NAME);
87 int tgetflag(char *NAME);
88 char *tgetstr(char *NAME, char **AREA);
89 #endif
91 static char term_buffer[4096];
92 static char term_buffer2[4096];
93 static char *term_p=term_buffer2;
95 static void termcap_add(char *id,int code){
96 char *p=tgetstr(id,&term_p);
97 if(!p) return;
98 if(getch2_key_db>=MAX_KEYS) return;
99 getch2_keys[getch2_key_db].len=strlen(p);
100 strncpy(getch2_keys[getch2_key_db].chars,p,8);
101 getch2_keys[getch2_key_db].code=code;
102 ++getch2_key_db;
103 /* printf("%s=%s\n",id,p); */
106 static int success=0;
108 int load_termcap(char *termtype){
109 if(!termtype) termtype=getenv("TERM");
110 if(!termtype) termtype="unknown";
111 success=tgetent(term_buffer, termtype);
112 if(success<0){ printf("Could not access the 'termcap' data base.\n"); return 0; }
113 if(success==0){ printf("Terminal type `%s' is not defined.\n", termtype);return 0;}
115 screen_width=tgetnum("co");
116 screen_height=tgetnum("li");
117 if(screen_width<1 || screen_width>255) screen_width=80;
118 if(screen_height<1 || screen_height>255) screen_height=24;
119 erase_to_end_of_line= tgetstr("cd",&term_p);
121 termcap_add("kP",KEY_PGUP);
122 termcap_add("kN",KEY_PGDWN);
123 termcap_add("kh",KEY_HOME);
124 termcap_add("kH",KEY_END);
125 termcap_add("kI",KEY_INS);
126 termcap_add("kD",KEY_DEL);
127 termcap_add("kb",KEY_BS);
128 termcap_add("kl",KEY_LEFT);
129 termcap_add("kd",KEY_DOWN);
130 termcap_add("ku",KEY_UP);
131 termcap_add("kr",KEY_RIGHT);
132 termcap_add("k0",KEY_F+0);
133 termcap_add("k1",KEY_F+1);
134 termcap_add("k2",KEY_F+2);
135 termcap_add("k3",KEY_F+3);
136 termcap_add("k4",KEY_F+4);
137 termcap_add("k5",KEY_F+5);
138 termcap_add("k6",KEY_F+6);
139 termcap_add("k7",KEY_F+7);
140 termcap_add("k8",KEY_F+8);
141 termcap_add("k9",KEY_F+9);
142 termcap_add("k;",KEY_F+10);
143 return getch2_key_db;
146 #endif
148 void get_screen_size(void){
149 #ifdef CONFIG_IOCTL
150 struct winsize ws;
151 if (ioctl(0, TIOCGWINSZ, &ws) < 0 || !ws.ws_row || !ws.ws_col) return;
152 /* printf("Using IOCTL\n"); */
153 screen_width=ws.ws_col;
154 screen_height=ws.ws_row;
155 #endif
158 void getch2(void)
160 int retval = read(0, &getch2_buf[getch2_len], BUF_LEN-getch2_len);
161 if (retval < 1)
162 return;
163 getch2_len += retval;
165 while (getch2_len > 0 && (getch2_len > 1 || getch2_buf[0] != 27)) {
166 int i, len, code;
168 /* First find in the TERMCAP database: */
169 for (i = 0; i < getch2_key_db; i++) {
170 if ((len = getch2_keys[i].len) <= getch2_len)
171 if(memcmp(getch2_keys[i].chars, getch2_buf, len) == 0) {
172 code = getch2_keys[i].code;
173 goto found;
176 /* We always match some keypress here, with length 1 if nothing else.
177 * Since some of the cases explicitly test remaining buffer length
178 * having a keycode only partially read in the buffer could incorrectly
179 * use the first byte as an independent character.
180 * However the buffer is big enough that this shouldn't happen too
181 * easily, and it's been this way for years without many complaints.
182 * I see no simple fix as there's no easy test which would tell
183 * whether a string must be part of a longer keycode. */
184 len = 1;
185 code = getch2_buf[0];
186 /* Check the well-known codes... */
187 if (code != 27) {
188 if (code == 'A'-64) code = KEY_HOME;
189 else if (code == 'E'-64) code = KEY_END;
190 else if (code == 'D'-64) code = KEY_DEL;
191 else if (code == 'H'-64) code = KEY_BS;
192 else if (code == 'U'-64) code = KEY_PGUP;
193 else if (code == 'V'-64) code = KEY_PGDWN;
194 else if (code == 8 || code==127) code = KEY_BS;
195 else if (code == 10 || code==13) {
196 if (getch2_len > 1) {
197 int c = getch2_buf[1];
198 if ((c == 10 || c == 13) && (c != code))
199 len = 2;
201 code = KEY_ENTER;
204 else if (getch2_len > 1) {
205 int c = getch2_buf[1];
206 if (c == 27) {
207 code = KEY_ESC;
208 len = 2;
209 goto found;
211 if (c >= '0' && c <= '9') {
212 code = c-'0'+KEY_F;
213 len = 2;
214 goto found;
216 if (getch2_len >= 4 && c == '[' && getch2_buf[2] == '[') {
217 int c = getch2_buf[3];
218 if (c >= 'A' && c < 'A'+12) {
219 code = KEY_F+1 + c-'A';
220 len = 4;
221 goto found;
224 if ((c == '[' || c == 'O') && getch2_len >= 3) {
225 int c = getch2_buf[2];
226 const short ctable[] = {
227 KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT, 0,
228 KEY_END, KEY_PGDWN, KEY_HOME, KEY_PGUP, 0, 0, KEY_INS, 0, 0, 0,
229 KEY_F+1, KEY_F+2, KEY_F+3, KEY_F+4};
230 if (c >= 'A' && c <= 'S')
231 if (ctable[c - 'A']) {
232 code = ctable[c - 'A'];
233 len = 3;
234 goto found;
237 if (getch2_len >= 4 && c == '[' && getch2_buf[3] == '~') {
238 int c = getch2_buf[2];
239 const int ctable[8] = {KEY_HOME, KEY_INS, KEY_DEL, KEY_END, KEY_PGUP, KEY_PGDWN, KEY_HOME, KEY_END};
240 if (c >= '1' && c <= '8') {
241 code = ctable[c - '1'];
242 len = 4;
243 goto found;
246 if (getch2_len >= 5 && c == '[' && getch2_buf[4] == '~') {
247 int i = getch2_buf[2] - '0';
248 int j = getch2_buf[3] - '0';
249 if (i >= 0 && i <= 9 && j >= 0 && j <= 9) {
250 const short ftable[20] = {
251 11,12,13,14,15, 17,18,19,20,21,
252 23,24,25,26,28, 29,31,32,33,34 };
253 int a = i*10 + j;
254 for (i = 0; i < 20; i++)
255 if (ftable[i] == a) {
256 code = KEY_F+1 + i;
257 len = 5;
258 goto found;
263 found:
264 getch2_len -= len;
265 for (i = 0; i < getch2_len; i++)
266 getch2_buf[i] = getch2_buf[len+i];
267 mplayer_put_key(code);
271 static int getch2_status=0;
273 void getch2_enable(void){
274 #ifdef HAVE_TERMIOS
275 struct termios tio_new;
276 tcgetattr(0,&tio_orig);
277 tio_new=tio_orig;
278 tio_new.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
279 tio_new.c_cc[VMIN] = 1;
280 tio_new.c_cc[VTIME] = 0;
281 tcsetattr(0,TCSANOW,&tio_new);
282 #endif
283 getch2_status=1;
286 void getch2_disable(void){
287 if(!getch2_status) return; // already disabled / never enabled
288 #ifdef HAVE_TERMIOS
289 tcsetattr(0,TCSANOW,&tio_orig);
290 #endif
291 getch2_status=0;
294 #ifdef CONFIG_ICONV
295 char* get_term_charset(void)
297 char* charset = NULL;
298 #ifdef HAVE_LANGINFO
299 setlocale(LC_CTYPE, "");
300 charset = strdup(nl_langinfo(CODESET));
301 setlocale(LC_CTYPE, "C");
302 #endif
303 return charset;
305 #endif