mp_fifo.c, osdep/: Include corresponding .h in .c files
[mplayer.git] / osdep / getch2.c
blob663b2350f90f191993bf812852003238410eb65c
1 /* GyS-TermIO v2.0 (for GySmail v3) (C) 1999 A'rpi/ESP-team */
3 #include "config.h"
5 //#define USE_TERMCAP
6 #if !defined(__OS2__) && !defined(__MORPHOS__)
7 #define USE_IOCTL
8 #endif
10 #define MAX_KEYS 64
11 #define BUF_LEN 256
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/time.h>
17 #include <sys/types.h>
18 #ifdef USE_IOCTL
19 #include <sys/ioctl.h>
20 #endif
22 #ifdef HAVE_TERMIOS
23 #ifdef HAVE_TERMIOS_H
24 #include <termios.h>
25 #endif
26 #ifdef HAVE_SYS_TERMIOS_H
27 #include <sys/termios.h>
28 #endif
29 #endif
31 #if defined(USE_LANGINFO) && defined(USE_ICONV)
32 #include <locale.h>
33 #include <langinfo.h>
34 #endif
36 #include <unistd.h>
38 #include "mp_fifo.h"
39 #include "keycodes.h"
40 #include "getch2.h"
42 #ifdef HAVE_TERMIOS
43 static struct termios tio_orig;
44 #endif
45 static int getch2_len=0;
46 static char getch2_buf[BUF_LEN];
48 int screen_width=80;
49 int screen_height=24;
50 char * erase_to_end_of_line = NULL;
52 typedef struct {
53 int len;
54 int code;
55 char chars[8];
56 } keycode_st;
57 static keycode_st getch2_keys[MAX_KEYS];
58 static int getch2_key_db=0;
60 #ifdef USE_TERMCAP
62 #if 0
63 #include <termcap.h>
64 #else
65 extern int tgetent (char *BUFFER, char *TERMTYPE);
66 extern int tgetnum (char *NAME);
67 extern int tgetflag (char *NAME);
68 extern char *tgetstr (char *NAME, char **AREA);
69 #endif
71 static char term_buffer[4096];
72 static char term_buffer2[4096];
73 static char *term_p=term_buffer2;
75 static void termcap_add(char *id,int code){
76 char *p=tgetstr(id,&term_p);
77 if(!p) return;
78 if(getch2_key_db>=MAX_KEYS) return;
79 getch2_keys[getch2_key_db].len=strlen(p);
80 strncpy(getch2_keys[getch2_key_db].chars,p,8);
81 getch2_keys[getch2_key_db].code=code;
82 ++getch2_key_db;
83 /* printf("%s=%s\n",id,p); */
86 static int success=0;
88 int load_termcap(char *termtype){
89 if(!termtype) termtype=getenv("TERM");
90 if(!termtype) termtype="unknown";
91 success=tgetent(term_buffer, termtype);
92 if(success<0){ printf("Could not access the 'termcap' data base.\n"); return 0; }
93 if(success==0){ printf("Terminal type `%s' is not defined.\n", termtype);return 0;}
95 screen_width=tgetnum("co");
96 screen_height=tgetnum("li");
97 if(screen_width<1 || screen_width>255) screen_width=80;
98 if(screen_height<1 || screen_height>255) screen_height=24;
99 erase_to_end_of_line= tgetstr("cd",&term_p);
101 termcap_add("kP",KEY_PGUP);
102 termcap_add("kN",KEY_PGDWN);
103 termcap_add("kh",KEY_HOME);
104 termcap_add("kH",KEY_END);
105 termcap_add("kI",KEY_INS);
106 termcap_add("kD",KEY_DEL);
107 termcap_add("kb",KEY_BS);
108 termcap_add("kl",KEY_LEFT);
109 termcap_add("kd",KEY_DOWN);
110 termcap_add("ku",KEY_UP);
111 termcap_add("kr",KEY_RIGHT);
112 termcap_add("k0",KEY_F+0);
113 termcap_add("k1",KEY_F+1);
114 termcap_add("k2",KEY_F+2);
115 termcap_add("k3",KEY_F+3);
116 termcap_add("k4",KEY_F+4);
117 termcap_add("k5",KEY_F+5);
118 termcap_add("k6",KEY_F+6);
119 termcap_add("k7",KEY_F+7);
120 termcap_add("k8",KEY_F+8);
121 termcap_add("k9",KEY_F+9);
122 termcap_add("k;",KEY_F+10);
123 return getch2_key_db;
126 #endif
128 void get_screen_size(void){
129 #ifdef USE_IOCTL
130 struct winsize ws;
131 if (ioctl(0, TIOCGWINSZ, &ws) < 0 || !ws.ws_row || !ws.ws_col) return;
132 /* printf("Using IOCTL\n"); */
133 screen_width=ws.ws_col;
134 screen_height=ws.ws_row;
135 #endif
138 void getch2(void)
140 int retval = read(0, &getch2_buf[getch2_len], BUF_LEN-getch2_len);
141 if (retval < 1)
142 return;
143 getch2_len += retval;
145 while (getch2_len > 0 && (getch2_len > 1 || getch2_buf[0] != 27)) {
146 int i, len, code;
148 /* First find in the TERMCAP database: */
149 for (i = 0; i < getch2_key_db; i++) {
150 if ((len = getch2_keys[i].len) <= getch2_len)
151 if(memcmp(getch2_keys[i].chars, getch2_buf, len) == 0) {
152 code = getch2_keys[i].code;
153 goto found;
156 /* We always match some keypress here, with length 1 if nothing else.
157 * Since some of the cases explicitly test remaining buffer length
158 * having a keycode only partially read in the buffer could incorrectly
159 * use the first byte as an independent character.
160 * However the buffer is big enough that this shouldn't happen too
161 * easily, and it's been this way for years without many complaints.
162 * I see no simple fix as there's no easy test which would tell
163 * whether a string must be part of a longer keycode. */
164 len = 1;
165 code = getch2_buf[0];
166 /* Check the well-known codes... */
167 if (code != 27) {
168 if (code == 'A'-64) code = KEY_HOME;
169 else if (code == 'E'-64) code = KEY_END;
170 else if (code == 'D'-64) code = KEY_DEL;
171 else if (code == 'H'-64) code = KEY_BS;
172 else if (code == 'U'-64) code = KEY_PGUP;
173 else if (code == 'V'-64) code = KEY_PGDWN;
174 else if (code == 8 || code==127) code = KEY_BS;
175 else if (code == 10 || code==13) {
176 if (getch2_len > 1) {
177 int c = getch2_buf[1];
178 if ((c == 10 || c == 13) && (c != code))
179 len = 2;
181 code = KEY_ENTER;
184 else if (getch2_len > 1) {
185 int c = getch2_buf[1];
186 if (c == 27) {
187 code = KEY_ESC;
188 len = 2;
189 goto found;
191 if (c >= '0' && c <= '9') {
192 code = c-'0'+KEY_F;
193 len = 2;
194 goto found;
196 if (getch2_len >= 4 && c == '[' && getch2_buf[2] == '[') {
197 int c = getch2_buf[3];
198 if (c >= 'A' && c < 'A'+12) {
199 code = KEY_F+1 + c-'A';
200 len = 4;
201 goto found;
204 if ((c == '[' || c == 'O') && getch2_len >= 3) {
205 int c = getch2_buf[2];
206 const short ctable[] = {
207 KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT, 0,
208 KEY_END, KEY_PGDWN, KEY_HOME, KEY_PGUP, 0, 0, KEY_INS, 0, 0, 0,
209 KEY_F+1, KEY_F+2, KEY_F+3, KEY_F+4};
210 if (c >= 'A' && c <= 'S')
211 if (ctable[c - 'A']) {
212 code = ctable[c - 'A'];
213 len = 3;
214 goto found;
217 if (getch2_len >= 4 && c == '[' && getch2_buf[3] == '~') {
218 int c = getch2_buf[2];
219 const int ctable[8] = {KEY_HOME, KEY_INS, KEY_DEL, KEY_END, KEY_PGUP, KEY_PGDWN, KEY_HOME, KEY_END};
220 if (c >= '1' && c <= '8') {
221 code = ctable[c - '1'];
222 len = 4;
223 goto found;
226 if (getch2_len >= 5 && c == '[' && getch2_buf[4] == '~') {
227 int i = getch2_buf[2] - '0';
228 int j = getch2_buf[3] - '0';
229 if (i >= 0 && i <= 9 && j >= 0 && j <= 9) {
230 const short ftable[20] = {
231 11,12,13,14,15, 17,18,19,20,21,
232 23,24,25,26,28, 29,31,32,33,34 };
233 int a = i*10 + j;
234 for (i = 0; i < 20; i++)
235 if (ftable[i] == a) {
236 code = KEY_F+1 + i;
237 len = 5;
238 goto found;
243 found:
244 getch2_len -= len;
245 for (i = 0; i < getch2_len; i++)
246 getch2_buf[i] = getch2_buf[len+i];
247 mplayer_put_key(code);
251 static int getch2_status=0;
253 void getch2_enable(void){
254 #ifdef HAVE_TERMIOS
255 struct termios tio_new;
256 tcgetattr(0,&tio_orig);
257 tio_new=tio_orig;
258 tio_new.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
259 tio_new.c_cc[VMIN] = 1;
260 tio_new.c_cc[VTIME] = 0;
261 tcsetattr(0,TCSANOW,&tio_new);
262 #endif
263 getch2_status=1;
266 void getch2_disable(void){
267 if(!getch2_status) return; // already disabled / never enabled
268 #ifdef HAVE_TERMIOS
269 tcsetattr(0,TCSANOW,&tio_orig);
270 #endif
271 getch2_status=0;
274 #ifdef USE_ICONV
275 char* get_term_charset(void)
277 char* charset = NULL;
278 #ifdef USE_LANGINFO
279 setlocale(LC_CTYPE, "");
280 charset = nl_langinfo(CODESET);
281 setlocale(LC_CTYPE, "C");
282 #endif
283 return charset;
285 #endif