Mercurial > mplayer.hg
annotate osdep/getch2.c @ 36166:a6263abe514f
Handle X events even while waiting for a map event.
author | reimar |
---|---|
date | Thu, 09 May 2013 14:06:17 +0000 |
parents | 6ac59489c815 |
children | de0c74742689 |
rev | line source |
---|---|
28744 | 1 /* |
2 * GyS-TermIO v2.0 (for GySmail v3) | |
3 * a very small replacement of ncurses library | |
4 * | |
5 * copyright (C) 1999 A'rpi/ESP-team | |
6 * | |
7 * This file is part of MPlayer. | |
8 * | |
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. | |
13 * | |
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. | |
18 * | |
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. | |
22 */ | |
1 | 23 |
16985 | 24 #include "config.h" |
1 | 25 |
27359
d788e177a35e
Rename some preprocessor directives from CONFIG_* to HAVE_* where appropriate;
diego
parents:
27341
diff
changeset
|
26 //#define HAVE_TERMCAP |
12122 | 27 #if !defined(__OS2__) && !defined(__MORPHOS__) |
27341
e7c989f7a7c9
Start unifying names of internal preprocessor directives.
diego
parents:
24515
diff
changeset
|
28 #define CONFIG_IOCTL |
5300 | 29 #endif |
1 | 30 |
31 #define MAX_KEYS 64 | |
32 #define BUF_LEN 256 | |
33 | |
34 #include <stdio.h> | |
35 #include <stdlib.h> | |
36 #include <string.h> | |
35856 | 37 #include <stdint.h> |
1 | 38 #include <sys/time.h> |
39 #include <sys/types.h> | |
27341
e7c989f7a7c9
Start unifying names of internal preprocessor directives.
diego
parents:
24515
diff
changeset
|
40 #ifdef CONFIG_IOCTL |
1 | 41 #include <sys/ioctl.h> |
42 #endif | |
3281
310c0b9bea21
detect termios.h if no sys/termios.h (qnx getch2 support working)
alex
parents:
3014
diff
changeset
|
43 |
3009 | 44 #ifdef HAVE_TERMIOS |
3281
310c0b9bea21
detect termios.h if no sys/termios.h (qnx getch2 support working)
alex
parents:
3014
diff
changeset
|
45 #ifdef HAVE_TERMIOS_H |
310c0b9bea21
detect termios.h if no sys/termios.h (qnx getch2 support working)
alex
parents:
3014
diff
changeset
|
46 #include <termios.h> |
310c0b9bea21
detect termios.h if no sys/termios.h (qnx getch2 support working)
alex
parents:
3014
diff
changeset
|
47 #endif |
3282 | 48 #ifdef HAVE_SYS_TERMIOS_H |
1 | 49 #include <sys/termios.h> |
3009 | 50 #endif |
3281
310c0b9bea21
detect termios.h if no sys/termios.h (qnx getch2 support working)
alex
parents:
3014
diff
changeset
|
51 #endif |
310c0b9bea21
detect termios.h if no sys/termios.h (qnx getch2 support working)
alex
parents:
3014
diff
changeset
|
52 |
27393 | 53 #if defined(HAVE_LANGINFO) && defined(CONFIG_ICONV) |
22442
56a0b0f8a66e
Add code to detect and convert to console codepage on Windows.
reimar
parents:
21589
diff
changeset
|
54 #include <locale.h> |
56a0b0f8a66e
Add code to detect and convert to console codepage on Windows.
reimar
parents:
21589
diff
changeset
|
55 #include <langinfo.h> |
56a0b0f8a66e
Add code to detect and convert to console codepage on Windows.
reimar
parents:
21589
diff
changeset
|
56 #endif |
56a0b0f8a66e
Add code to detect and convert to console codepage on Windows.
reimar
parents:
21589
diff
changeset
|
57 |
1 | 58 #include <unistd.h> |
59 | |
24129 | 60 #include "mp_fifo.h" |
1 | 61 #include "keycodes.h" |
30554 | 62 #include "getch2.h" |
35867 | 63 #include "libavutil/common.h" |
1 | 64 |
3009 | 65 #ifdef HAVE_TERMIOS |
1 | 66 static struct termios tio_orig; |
3009 | 67 #endif |
1 | 68 static int getch2_len=0; |
35853 | 69 static uint8_t getch2_buf[BUF_LEN]; |
1 | 70 |
71 int screen_width=80; | |
72 int screen_height=24; | |
17258
3d02f6e2a432
change erase to end of line, fall back to old behavior if no termcap found
ods15
parents:
17240
diff
changeset
|
73 char * erase_to_end_of_line = NULL; |
1 | 74 |
75 typedef struct { | |
76 int len; | |
77 int code; | |
78 char chars[8]; | |
79 } keycode_st; | |
80 static keycode_st getch2_keys[MAX_KEYS]; | |
81 static int getch2_key_db=0; | |
82 | |
27359
d788e177a35e
Rename some preprocessor directives from CONFIG_* to HAVE_* where appropriate;
diego
parents:
27341
diff
changeset
|
83 #ifdef HAVE_TERMCAP |
1 | 84 |
85 #if 0 | |
86 #include <termcap.h> | |
87 #else | |
28051 | 88 int tgetent(char *BUFFER, char *TERMTYPE); |
89 int tgetnum(char *NAME); | |
90 int tgetflag(char *NAME); | |
91 char *tgetstr(char *NAME, char **AREA); | |
1 | 92 #endif |
93 | |
94 static char term_buffer[4096]; | |
95 static char term_buffer2[4096]; | |
96 static char *term_p=term_buffer2; | |
97 | |
98 static void termcap_add(char *id,int code){ | |
99 char *p=tgetstr(id,&term_p); | |
100 if(!p) return; | |
101 if(getch2_key_db>=MAX_KEYS) return; | |
102 getch2_keys[getch2_key_db].len=strlen(p); | |
103 strncpy(getch2_keys[getch2_key_db].chars,p,8); | |
104 getch2_keys[getch2_key_db].code=code; | |
105 ++getch2_key_db; | |
106 /* printf("%s=%s\n",id,p); */ | |
107 } | |
108 | |
109 static int success=0; | |
110 | |
111 int load_termcap(char *termtype){ | |
112 if(!termtype) termtype=getenv("TERM"); | |
8889
18427eee9205
The attached little patch fixes a core dump on termcap systems when
arpi
parents:
8016
diff
changeset
|
113 if(!termtype) termtype="unknown"; |
1 | 114 success=tgetent(term_buffer, termtype); |
115 if(success<0){ printf("Could not access the 'termcap' data base.\n"); return 0; } | |
116 if(success==0){ printf("Terminal type `%s' is not defined.\n", termtype);return 0;} | |
117 | |
118 screen_width=tgetnum("co"); | |
119 screen_height=tgetnum("li"); | |
120 if(screen_width<1 || screen_width>255) screen_width=80; | |
121 if(screen_height<1 || screen_height>255) screen_height=24; | |
17258
3d02f6e2a432
change erase to end of line, fall back to old behavior if no termcap found
ods15
parents:
17240
diff
changeset
|
122 erase_to_end_of_line= tgetstr("cd",&term_p); |
1 | 123 |
124 termcap_add("kP",KEY_PGUP); | |
125 termcap_add("kN",KEY_PGDWN); | |
126 termcap_add("kh",KEY_HOME); | |
127 termcap_add("kH",KEY_END); | |
128 termcap_add("kI",KEY_INS); | |
129 termcap_add("kD",KEY_DEL); | |
130 termcap_add("kb",KEY_BS); | |
131 termcap_add("kl",KEY_LEFT); | |
132 termcap_add("kd",KEY_DOWN); | |
133 termcap_add("ku",KEY_UP); | |
134 termcap_add("kr",KEY_RIGHT); | |
135 termcap_add("k0",KEY_F+0); | |
136 termcap_add("k1",KEY_F+1); | |
137 termcap_add("k2",KEY_F+2); | |
138 termcap_add("k3",KEY_F+3); | |
139 termcap_add("k4",KEY_F+4); | |
140 termcap_add("k5",KEY_F+5); | |
141 termcap_add("k6",KEY_F+6); | |
142 termcap_add("k7",KEY_F+7); | |
143 termcap_add("k8",KEY_F+8); | |
144 termcap_add("k9",KEY_F+9); | |
145 termcap_add("k;",KEY_F+10); | |
146 return getch2_key_db; | |
147 } | |
148 | |
149 #endif | |
150 | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
17264
diff
changeset
|
151 void get_screen_size(void){ |
27341
e7c989f7a7c9
Start unifying names of internal preprocessor directives.
diego
parents:
24515
diff
changeset
|
152 #ifdef CONFIG_IOCTL |
1 | 153 struct winsize ws; |
154 if (ioctl(0, TIOCGWINSZ, &ws) < 0 || !ws.ws_row || !ws.ws_col) return; | |
155 /* printf("Using IOCTL\n"); */ | |
156 screen_width=ws.ws_col; | |
157 screen_height=ws.ws_row; | |
158 #endif | |
159 } | |
160 | |
24129 | 161 void getch2(void) |
162 { | |
24130 | 163 int retval = read(0, &getch2_buf[getch2_len], BUF_LEN-getch2_len); |
24129 | 164 if (retval < 1) |
24130 | 165 return; |
166 getch2_len += retval; | |
1 | 167 |
24129 | 168 while (getch2_len > 0 && (getch2_len > 1 || getch2_buf[0] != 27)) { |
24130 | 169 int i, len, code; |
170 | |
171 /* First find in the TERMCAP database: */ | |
172 for (i = 0; i < getch2_key_db; i++) { | |
173 if ((len = getch2_keys[i].len) <= getch2_len) | |
174 if(memcmp(getch2_keys[i].chars, getch2_buf, len) == 0) { | |
175 code = getch2_keys[i].code; | |
176 goto found; | |
177 } | |
1 | 178 } |
24130 | 179 /* We always match some keypress here, with length 1 if nothing else. |
180 * Since some of the cases explicitly test remaining buffer length | |
181 * having a keycode only partially read in the buffer could incorrectly | |
182 * use the first byte as an independent character. | |
183 * However the buffer is big enough that this shouldn't happen too | |
184 * easily, and it's been this way for years without many complaints. | |
185 * I see no simple fix as there's no easy test which would tell | |
186 * whether a string must be part of a longer keycode. */ | |
187 len = 1; | |
188 code = getch2_buf[0]; | |
189 /* Check the well-known codes... */ | |
190 if (code != 27) { | |
191 if (code == 'A'-64) code = KEY_HOME; | |
192 else if (code == 'E'-64) code = KEY_END; | |
193 else if (code == 'D'-64) code = KEY_DEL; | |
194 else if (code == 'H'-64) code = KEY_BS; | |
195 else if (code == 'U'-64) code = KEY_PGUP; | |
196 else if (code == 'V'-64) code = KEY_PGDWN; | |
197 else if (code == 8 || code==127) code = KEY_BS; | |
198 else if (code == 10 || code==13) { | |
199 if (getch2_len > 1) { | |
200 int c = getch2_buf[1]; | |
201 if ((c == 10 || c == 13) && (c != code)) | |
202 len = 2; | |
203 } | |
204 code = KEY_ENTER; | |
35867 | 205 } else if (code >= 0xc0) { |
206 uint32_t utf8 = 0; | |
207 i = 0; | |
208 GET_UTF8(utf8, (i < getch2_len ? getch2_buf[i++] : 0), goto not_utf8;) | |
209 if (utf8 < KEY_BASE) { | |
210 code = utf8; | |
211 len = i; | |
212 } | |
24130 | 213 } |
1 | 214 } |
24130 | 215 else if (getch2_len > 1) { |
216 int c = getch2_buf[1]; | |
217 if (c == 27) { | |
218 code = KEY_ESC; | |
219 len = 2; | |
220 goto found; | |
221 } | |
222 if (c >= '0' && c <= '9') { | |
223 code = c-'0'+KEY_F; | |
224 len = 2; | |
225 goto found; | |
226 } | |
227 if (getch2_len >= 4 && c == '[' && getch2_buf[2] == '[') { | |
228 int c = getch2_buf[3]; | |
229 if (c >= 'A' && c < 'A'+12) { | |
230 code = KEY_F+1 + c-'A'; | |
231 len = 4; | |
232 goto found; | |
233 } | |
234 } | |
24515 | 235 if ((c == '[' || c == 'O') && getch2_len >= 3) { |
24130 | 236 int c = getch2_buf[2]; |
35854 | 237 static const unsigned ctable[] = { |
24130 | 238 KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT, 0, |
239 KEY_END, KEY_PGDWN, KEY_HOME, KEY_PGUP, 0, 0, KEY_INS, 0, 0, 0, | |
240 KEY_F+1, KEY_F+2, KEY_F+3, KEY_F+4}; | |
241 if (c >= 'A' && c <= 'S') | |
242 if (ctable[c - 'A']) { | |
243 code = ctable[c - 'A']; | |
244 len = 3; | |
245 goto found; | |
246 } | |
247 } | |
248 if (getch2_len >= 4 && c == '[' && getch2_buf[3] == '~') { | |
249 int c = getch2_buf[2]; | |
35854 | 250 static const int ctable[8] = {KEY_HOME, KEY_INS, KEY_DEL, KEY_END, KEY_PGUP, KEY_PGDWN, KEY_HOME, KEY_END}; |
24130 | 251 if (c >= '1' && c <= '8') { |
252 code = ctable[c - '1']; | |
253 len = 4; | |
254 goto found; | |
255 } | |
256 } | |
257 if (getch2_len >= 5 && c == '[' && getch2_buf[4] == '~') { | |
258 int i = getch2_buf[2] - '0'; | |
259 int j = getch2_buf[3] - '0'; | |
260 if (i >= 0 && i <= 9 && j >= 0 && j <= 9) { | |
35854 | 261 static const uint8_t ftable[20] = { |
24130 | 262 11,12,13,14,15, 17,18,19,20,21, |
263 23,24,25,26,28, 29,31,32,33,34 }; | |
264 int a = i*10 + j; | |
265 for (i = 0; i < 20; i++) | |
266 if (ftable[i] == a) { | |
267 code = KEY_F+1 + i; | |
268 len = 5; | |
269 goto found; | |
270 } | |
271 } | |
272 } | |
1 | 273 } |
35867 | 274 not_utf8: |
24130 | 275 found: |
276 getch2_len -= len; | |
277 for (i = 0; i < getch2_len; i++) | |
278 getch2_buf[i] = getch2_buf[len+i]; | |
279 mplayer_put_key(code); | |
24129 | 280 } |
1 | 281 } |
282 | |
1632 | 283 static int getch2_status=0; |
284 | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
17264
diff
changeset
|
285 void getch2_enable(void){ |
3009 | 286 #ifdef HAVE_TERMIOS |
1 | 287 struct termios tio_new; |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
958
diff
changeset
|
288 tcgetattr(0,&tio_orig); |
1 | 289 tio_new=tio_orig; |
290 tio_new.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */ | |
291 tio_new.c_cc[VMIN] = 1; | |
292 tio_new.c_cc[VTIME] = 0; | |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
958
diff
changeset
|
293 tcsetattr(0,TCSANOW,&tio_new); |
3009 | 294 #endif |
1632 | 295 getch2_status=1; |
1 | 296 } |
297 | |
17566
f580a7755ac5
Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents:
17264
diff
changeset
|
298 void getch2_disable(void){ |
1632 | 299 if(!getch2_status) return; // already disabled / never enabled |
3009 | 300 #ifdef HAVE_TERMIOS |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
958
diff
changeset
|
301 tcsetattr(0,TCSANOW,&tio_orig); |
3009 | 302 #endif |
1632 | 303 getch2_status=0; |
1 | 304 } |
305 | |
27393 | 306 #ifdef CONFIG_ICONV |
22886 | 307 char* get_term_charset(void) |
22442
56a0b0f8a66e
Add code to detect and convert to console codepage on Windows.
reimar
parents:
21589
diff
changeset
|
308 { |
56a0b0f8a66e
Add code to detect and convert to console codepage on Windows.
reimar
parents:
21589
diff
changeset
|
309 char* charset = NULL; |
27359
d788e177a35e
Rename some preprocessor directives from CONFIG_* to HAVE_* where appropriate;
diego
parents:
27341
diff
changeset
|
310 #ifdef HAVE_LANGINFO |
22442
56a0b0f8a66e
Add code to detect and convert to console codepage on Windows.
reimar
parents:
21589
diff
changeset
|
311 setlocale(LC_CTYPE, ""); |
29290
ef46d5a66bb2
Use a malloced string for the get_term_charset return value.
reimar
parents:
28744
diff
changeset
|
312 charset = strdup(nl_langinfo(CODESET)); |
22442
56a0b0f8a66e
Add code to detect and convert to console codepage on Windows.
reimar
parents:
21589
diff
changeset
|
313 setlocale(LC_CTYPE, "C"); |
56a0b0f8a66e
Add code to detect and convert to console codepage on Windows.
reimar
parents:
21589
diff
changeset
|
314 #endif |
56a0b0f8a66e
Add code to detect and convert to console codepage on Windows.
reimar
parents:
21589
diff
changeset
|
315 return charset; |
56a0b0f8a66e
Add code to detect and convert to console codepage on Windows.
reimar
parents:
21589
diff
changeset
|
316 } |
56a0b0f8a66e
Add code to detect and convert to console codepage on Windows.
reimar
parents:
21589
diff
changeset
|
317 #endif |