annotate osdep/getch2.c @ 37033:de0c74742689

Fix handling of overlong termcap strings entries. The previous code would silently truncate and then read out of bounds later in a memcmp.
author reimar
date Sun, 06 Apr 2014 18:10:31 +0000
parents 6ac59489c815
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28744
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
1 /*
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
2 * GyS-TermIO v2.0 (for GySmail v3)
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
3 * a very small replacement of ncurses library
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
4 *
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
5 * copyright (C) 1999 A'rpi/ESP-team
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
6 *
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
7 * This file is part of MPlayer.
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
8 *
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
9 * MPlayer is free software; you can redistribute it and/or modify
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
10 * it under the terms of the GNU General Public License as published by
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
11 * the Free Software Foundation; either version 2 of the License, or
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
12 * (at your option) any later version.
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
13 *
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
14 * MPlayer is distributed in the hope that it will be useful,
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
17 * GNU General Public License for more details.
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
18 *
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
19 * You should have received a copy of the GNU General Public License along
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
20 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5cfef41a1771 Add standard license headers to files.
diego
parents: 28051
diff changeset
22 */
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
23
16985
08cac43f1e38 Unify include paths, -I.. is in CFLAGS.
diego
parents: 15566
diff changeset
24 #include "config.h"
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
25
27359
d788e177a35e Rename some preprocessor directives from CONFIG_* to HAVE_* where appropriate;
diego
parents: 27341
diff changeset
26 //#define HAVE_TERMCAP
12122
cacdfa298d37 morphos support by Nicolas Det
alex
parents: 12063
diff changeset
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
5d7bd47b46ef OS2 support
arpi
parents: 3282
diff changeset
29 #endif
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
30
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
31 #define MAX_KEYS 64
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
32 #define BUF_LEN 256
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
33
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
34 #include <stdio.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
35 #include <stdlib.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
36 #include <string.h>
35856
310c8995c0d0 Prefer stdint.h over inttypes.h if both work.
cehoyos
parents: 35855
diff changeset
37 #include <stdint.h>
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
38 #include <sys/time.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
39 #include <sys/types.h>
27341
e7c989f7a7c9 Start unifying names of internal preprocessor directives.
diego
parents: 24515
diff changeset
40 #ifdef CONFIG_IOCTL
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
41 #include <sys/ioctl.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
42 #endif
3281
310c0b9bea21 detect termios.h if no sys/termios.h (qnx getch2 support working)
alex
parents: 3014
diff changeset
43
3009
e22bba6cb6a6 added HAVE_TERMIOS
alex
parents: 2089
diff changeset
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
alex
parents: 3281
diff changeset
48 #ifdef HAVE_SYS_TERMIOS_H
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
49 #include <sys/termios.h>
3009
e22bba6cb6a6 added HAVE_TERMIOS
alex
parents: 2089
diff changeset
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
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
58 #include <unistd.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
59
24129
50e1e79056b6 Make terminal input work more like VO key input
uau
parents: 22886
diff changeset
60 #include "mp_fifo.h"
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
61 #include "keycodes.h"
30554
321e9ea69b9f #include corresponding .h files in .c files.
diego
parents: 29290
diff changeset
62 #include "getch2.h"
35867
6ac59489c815 Add support for binding any key in UTF-8 range.
reimar
parents: 35856
diff changeset
63 #include "libavutil/common.h"
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
64
3009
e22bba6cb6a6 added HAVE_TERMIOS
alex
parents: 2089
diff changeset
65 #ifdef HAVE_TERMIOS
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
66 static struct termios tio_orig;
3009
e22bba6cb6a6 added HAVE_TERMIOS
alex
parents: 2089
diff changeset
67 #endif
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
68 static int getch2_len=0;
35853
3ee9fd074b8c Use more appropriate types.
reimar
parents: 30633
diff changeset
69 static uint8_t getch2_buf[BUF_LEN];
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
70
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
71 int screen_width=80;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
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
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
74
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
75 typedef struct {
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
76 int len;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
77 int code;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
78 char chars[8];
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
79 } keycode_st;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
80 static keycode_st getch2_keys[MAX_KEYS];
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
81 static int getch2_key_db=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
82
27359
d788e177a35e Rename some preprocessor directives from CONFIG_* to HAVE_* where appropriate;
diego
parents: 27341
diff changeset
83 #ifdef HAVE_TERMCAP
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
84
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
85 #if 0
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
86 #include <termcap.h>
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
87 #else
28051
9e739bdb049c Get rid of pointless 'extern' keywords.
diego
parents: 27393
diff changeset
88 int tgetent(char *BUFFER, char *TERMTYPE);
9e739bdb049c Get rid of pointless 'extern' keywords.
diego
parents: 27393
diff changeset
89 int tgetnum(char *NAME);
9e739bdb049c Get rid of pointless 'extern' keywords.
diego
parents: 27393
diff changeset
90 int tgetflag(char *NAME);
9e739bdb049c Get rid of pointless 'extern' keywords.
diego
parents: 27393
diff changeset
91 char *tgetstr(char *NAME, char **AREA);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
92 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
93
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
94 static char term_buffer[4096];
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
95 static char term_buffer2[4096];
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
96 static char *term_p=term_buffer2;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
97
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
98 static void termcap_add(char *id,int code){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
99 char *p=tgetstr(id,&term_p);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
100 if(!p) return;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
101 if(getch2_key_db>=MAX_KEYS) return;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
102 getch2_keys[getch2_key_db].len=strlen(p);
37033
de0c74742689 Fix handling of overlong termcap strings entries.
reimar
parents: 35867
diff changeset
103 if (getch2_keys[getch2_key_db].len >= sizeof(getch2_keys[getch2_key_db].chars))
de0c74742689 Fix handling of overlong termcap strings entries.
reimar
parents: 35867
diff changeset
104 {
de0c74742689 Fix handling of overlong termcap strings entries.
reimar
parents: 35867
diff changeset
105 printf("Key string entry more than 7 characters, ignoring\n");
de0c74742689 Fix handling of overlong termcap strings entries.
reimar
parents: 35867
diff changeset
106 return;
de0c74742689 Fix handling of overlong termcap strings entries.
reimar
parents: 35867
diff changeset
107 }
de0c74742689 Fix handling of overlong termcap strings entries.
reimar
parents: 35867
diff changeset
108 strcpy(getch2_keys[getch2_key_db].chars,p);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
109 getch2_keys[getch2_key_db].code=code;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
110 ++getch2_key_db;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
111 /* printf("%s=%s\n",id,p); */
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
112 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
113
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
114 static int success=0;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
115
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
116 int load_termcap(char *termtype){
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
117 if(!termtype) termtype=getenv("TERM");
8889
18427eee9205 The attached little patch fixes a core dump on termcap systems when
arpi
parents: 8016
diff changeset
118 if(!termtype) termtype="unknown";
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
119 success=tgetent(term_buffer, termtype);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
120 if(success<0){ printf("Could not access the 'termcap' data base.\n"); return 0; }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
121 if(success==0){ printf("Terminal type `%s' is not defined.\n", termtype);return 0;}
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
122
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
123 screen_width=tgetnum("co");
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
124 screen_height=tgetnum("li");
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
125 if(screen_width<1 || screen_width>255) screen_width=80;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
126 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
127 erase_to_end_of_line= tgetstr("cd",&term_p);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
128
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
129 termcap_add("kP",KEY_PGUP);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
130 termcap_add("kN",KEY_PGDWN);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
131 termcap_add("kh",KEY_HOME);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
132 termcap_add("kH",KEY_END);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
133 termcap_add("kI",KEY_INS);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
134 termcap_add("kD",KEY_DEL);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
135 termcap_add("kb",KEY_BS);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
136 termcap_add("kl",KEY_LEFT);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
137 termcap_add("kd",KEY_DOWN);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
138 termcap_add("ku",KEY_UP);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
139 termcap_add("kr",KEY_RIGHT);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
140 termcap_add("k0",KEY_F+0);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
141 termcap_add("k1",KEY_F+1);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
142 termcap_add("k2",KEY_F+2);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
143 termcap_add("k3",KEY_F+3);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
144 termcap_add("k4",KEY_F+4);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
145 termcap_add("k5",KEY_F+5);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
146 termcap_add("k6",KEY_F+6);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
147 termcap_add("k7",KEY_F+7);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
148 termcap_add("k8",KEY_F+8);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
149 termcap_add("k9",KEY_F+9);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
150 termcap_add("k;",KEY_F+10);
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
151 return getch2_key_db;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
152 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
153
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
154 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
155
17566
f580a7755ac5 Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents: 17264
diff changeset
156 void get_screen_size(void){
27341
e7c989f7a7c9 Start unifying names of internal preprocessor directives.
diego
parents: 24515
diff changeset
157 #ifdef CONFIG_IOCTL
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
158 struct winsize ws;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
159 if (ioctl(0, TIOCGWINSZ, &ws) < 0 || !ws.ws_row || !ws.ws_col) return;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
160 /* printf("Using IOCTL\n"); */
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
161 screen_width=ws.ws_col;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
162 screen_height=ws.ws_row;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
163 #endif
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
164 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
165
24129
50e1e79056b6 Make terminal input work more like VO key input
uau
parents: 22886
diff changeset
166 void getch2(void)
50e1e79056b6 Make terminal input work more like VO key input
uau
parents: 22886
diff changeset
167 {
24130
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
168 int retval = read(0, &getch2_buf[getch2_len], BUF_LEN-getch2_len);
24129
50e1e79056b6 Make terminal input work more like VO key input
uau
parents: 22886
diff changeset
169 if (retval < 1)
24130
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
170 return;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
171 getch2_len += retval;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
172
24129
50e1e79056b6 Make terminal input work more like VO key input
uau
parents: 22886
diff changeset
173 while (getch2_len > 0 && (getch2_len > 1 || getch2_buf[0] != 27)) {
24130
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
174 int i, len, code;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
175
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
176 /* First find in the TERMCAP database: */
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
177 for (i = 0; i < getch2_key_db; i++) {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
178 if ((len = getch2_keys[i].len) <= getch2_len)
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
179 if(memcmp(getch2_keys[i].chars, getch2_buf, len) == 0) {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
180 code = getch2_keys[i].code;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
181 goto found;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
182 }
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
183 }
24130
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
184 /* We always match some keypress here, with length 1 if nothing else.
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
185 * Since some of the cases explicitly test remaining buffer length
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
186 * having a keycode only partially read in the buffer could incorrectly
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
187 * use the first byte as an independent character.
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
188 * However the buffer is big enough that this shouldn't happen too
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
189 * easily, and it's been this way for years without many complaints.
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
190 * I see no simple fix as there's no easy test which would tell
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
191 * whether a string must be part of a longer keycode. */
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
192 len = 1;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
193 code = getch2_buf[0];
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
194 /* Check the well-known codes... */
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
195 if (code != 27) {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
196 if (code == 'A'-64) code = KEY_HOME;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
197 else if (code == 'E'-64) code = KEY_END;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
198 else if (code == 'D'-64) code = KEY_DEL;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
199 else if (code == 'H'-64) code = KEY_BS;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
200 else if (code == 'U'-64) code = KEY_PGUP;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
201 else if (code == 'V'-64) code = KEY_PGDWN;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
202 else if (code == 8 || code==127) code = KEY_BS;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
203 else if (code == 10 || code==13) {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
204 if (getch2_len > 1) {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
205 int c = getch2_buf[1];
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
206 if ((c == 10 || c == 13) && (c != code))
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
207 len = 2;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
208 }
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
209 code = KEY_ENTER;
35867
6ac59489c815 Add support for binding any key in UTF-8 range.
reimar
parents: 35856
diff changeset
210 } else if (code >= 0xc0) {
6ac59489c815 Add support for binding any key in UTF-8 range.
reimar
parents: 35856
diff changeset
211 uint32_t utf8 = 0;
6ac59489c815 Add support for binding any key in UTF-8 range.
reimar
parents: 35856
diff changeset
212 i = 0;
6ac59489c815 Add support for binding any key in UTF-8 range.
reimar
parents: 35856
diff changeset
213 GET_UTF8(utf8, (i < getch2_len ? getch2_buf[i++] : 0), goto not_utf8;)
6ac59489c815 Add support for binding any key in UTF-8 range.
reimar
parents: 35856
diff changeset
214 if (utf8 < KEY_BASE) {
6ac59489c815 Add support for binding any key in UTF-8 range.
reimar
parents: 35856
diff changeset
215 code = utf8;
6ac59489c815 Add support for binding any key in UTF-8 range.
reimar
parents: 35856
diff changeset
216 len = i;
6ac59489c815 Add support for binding any key in UTF-8 range.
reimar
parents: 35856
diff changeset
217 }
24130
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
218 }
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
219 }
24130
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
220 else if (getch2_len > 1) {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
221 int c = getch2_buf[1];
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
222 if (c == 27) {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
223 code = KEY_ESC;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
224 len = 2;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
225 goto found;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
226 }
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
227 if (c >= '0' && c <= '9') {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
228 code = c-'0'+KEY_F;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
229 len = 2;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
230 goto found;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
231 }
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
232 if (getch2_len >= 4 && c == '[' && getch2_buf[2] == '[') {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
233 int c = getch2_buf[3];
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
234 if (c >= 'A' && c < 'A'+12) {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
235 code = KEY_F+1 + c-'A';
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
236 len = 4;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
237 goto found;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
238 }
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
239 }
24515
c5c0cb0e90d2 getch2: Fix incorrect test
uau
parents: 24130
diff changeset
240 if ((c == '[' || c == 'O') && getch2_len >= 3) {
24130
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
241 int c = getch2_buf[2];
35854
08c22970003b Tables should be "static".
reimar
parents: 35853
diff changeset
242 static const unsigned ctable[] = {
24130
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
243 KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT, 0,
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
244 KEY_END, KEY_PGDWN, KEY_HOME, KEY_PGUP, 0, 0, KEY_INS, 0, 0, 0,
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
245 KEY_F+1, KEY_F+2, KEY_F+3, KEY_F+4};
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
246 if (c >= 'A' && c <= 'S')
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
247 if (ctable[c - 'A']) {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
248 code = ctable[c - 'A'];
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
249 len = 3;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
250 goto found;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
251 }
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
252 }
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
253 if (getch2_len >= 4 && c == '[' && getch2_buf[3] == '~') {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
254 int c = getch2_buf[2];
35854
08c22970003b Tables should be "static".
reimar
parents: 35853
diff changeset
255 static const int ctable[8] = {KEY_HOME, KEY_INS, KEY_DEL, KEY_END, KEY_PGUP, KEY_PGDWN, KEY_HOME, KEY_END};
24130
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
256 if (c >= '1' && c <= '8') {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
257 code = ctable[c - '1'];
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
258 len = 4;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
259 goto found;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
260 }
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
261 }
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
262 if (getch2_len >= 5 && c == '[' && getch2_buf[4] == '~') {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
263 int i = getch2_buf[2] - '0';
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
264 int j = getch2_buf[3] - '0';
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
265 if (i >= 0 && i <= 9 && j >= 0 && j <= 9) {
35854
08c22970003b Tables should be "static".
reimar
parents: 35853
diff changeset
266 static const uint8_t ftable[20] = {
24130
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
267 11,12,13,14,15, 17,18,19,20,21,
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
268 23,24,25,26,28, 29,31,32,33,34 };
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
269 int a = i*10 + j;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
270 for (i = 0; i < 20; i++)
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
271 if (ftable[i] == a) {
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
272 code = KEY_F+1 + i;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
273 len = 5;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
274 goto found;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
275 }
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
276 }
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
277 }
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
278 }
35867
6ac59489c815 Add support for binding any key in UTF-8 range.
reimar
parents: 35856
diff changeset
279 not_utf8:
24130
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
280 found:
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
281 getch2_len -= len;
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
282 for (i = 0; i < getch2_len; i++)
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
283 getch2_buf[i] = getch2_buf[len+i];
d62ddc94c474 Clean up getch2() code somewhat
uau
parents: 24129
diff changeset
284 mplayer_put_key(code);
24129
50e1e79056b6 Make terminal input work more like VO key input
uau
parents: 22886
diff changeset
285 }
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
286 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
287
1632
77cf72182c7f status management
arpi
parents: 1451
diff changeset
288 static int getch2_status=0;
77cf72182c7f status management
arpi
parents: 1451
diff changeset
289
17566
f580a7755ac5 Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents: 17264
diff changeset
290 void getch2_enable(void){
3009
e22bba6cb6a6 added HAVE_TERMIOS
alex
parents: 2089
diff changeset
291 #ifdef HAVE_TERMIOS
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
292 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
293 tcgetattr(0,&tio_orig);
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
294 tio_new=tio_orig;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
295 tio_new.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
296 tio_new.c_cc[VMIN] = 1;
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
297 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
298 tcsetattr(0,TCSANOW,&tio_new);
3009
e22bba6cb6a6 added HAVE_TERMIOS
alex
parents: 2089
diff changeset
299 #endif
1632
77cf72182c7f status management
arpi
parents: 1451
diff changeset
300 getch2_status=1;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
301 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
302
17566
f580a7755ac5 Patch by Stefan Huehner / stefan % huehner ! org \
rathann
parents: 17264
diff changeset
303 void getch2_disable(void){
1632
77cf72182c7f status management
arpi
parents: 1451
diff changeset
304 if(!getch2_status) return; // already disabled / never enabled
3009
e22bba6cb6a6 added HAVE_TERMIOS
alex
parents: 2089
diff changeset
305 #ifdef HAVE_TERMIOS
1038
b36fb1ae4b53 applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents: 958
diff changeset
306 tcsetattr(0,TCSANOW,&tio_orig);
3009
e22bba6cb6a6 added HAVE_TERMIOS
alex
parents: 2089
diff changeset
307 #endif
1632
77cf72182c7f status management
arpi
parents: 1451
diff changeset
308 getch2_status=0;
1
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
309 }
3b5f5d1c5041 Initial revision
arpi_esp
parents:
diff changeset
310
27393
4876c89bafdd Rename font-related preprocessor directives.
diego
parents: 27359
diff changeset
311 #ifdef CONFIG_ICONV
22886
71b3e04d0555 "()" to "(void)" function param list fixes
uau
parents: 22442
diff changeset
312 char* get_term_charset(void)
22442
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 21589
diff changeset
313 {
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 21589
diff changeset
314 char* charset = NULL;
27359
d788e177a35e Rename some preprocessor directives from CONFIG_* to HAVE_* where appropriate;
diego
parents: 27341
diff changeset
315 #ifdef HAVE_LANGINFO
22442
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 21589
diff changeset
316 setlocale(LC_CTYPE, "");
29290
ef46d5a66bb2 Use a malloced string for the get_term_charset return value.
reimar
parents: 28744
diff changeset
317 charset = strdup(nl_langinfo(CODESET));
22442
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 21589
diff changeset
318 setlocale(LC_CTYPE, "C");
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 21589
diff changeset
319 #endif
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 21589
diff changeset
320 return charset;
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 21589
diff changeset
321 }
56a0b0f8a66e Add code to detect and convert to console codepage on Windows.
reimar
parents: 21589
diff changeset
322 #endif