ao_pulse: support native mute control
[mplayer.git] / osdep / getch2.c
blob0cbaa82a25a8fee3f226515d12a1511597dd9f3f
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(__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>
58 #include <fcntl.h>
60 #include "bstr.h"
61 #include "mp_fifo.h"
62 #include "input/keycodes.h"
63 #include "getch2.h"
65 #ifdef HAVE_TERMIOS
66 static struct termios tio_orig;
67 #endif
68 static int getch2_len=0;
69 static unsigned char getch2_buf[BUF_LEN];
71 int screen_width=80;
72 int screen_height=24;
73 char * erase_to_end_of_line = NULL;
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;
83 #ifdef HAVE_TERMCAP
85 #if 0
86 #include <termcap.h>
87 #else
88 int tgetent(char *BUFFER, char *TERMTYPE);
89 int tgetnum(char *NAME);
90 int tgetflag(char *NAME);
91 char *tgetstr(char *NAME, char **AREA);
92 #endif
94 static char term_buffer[4096];
95 static char term_buffer2[4096];
96 static char *term_p=term_buffer2;
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); */
109 static int success=0;
111 int load_termcap(char *termtype){
112 if(!termtype) termtype=getenv("TERM");
113 if(!termtype) termtype="unknown";
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;}
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;
122 erase_to_end_of_line= tgetstr("ce",&term_p);
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;
149 #endif
151 void get_screen_size(void){
152 #ifdef CONFIG_IOCTL
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
161 bool getch2(struct mp_fifo *fifo)
163 int retval = read(0, &getch2_buf[getch2_len], BUF_LEN-getch2_len);
164 /* Return false on EOF to stop running select() on the FD, as it'd
165 * trigger all the time. Note that it's possible to get temporary
166 * EOF on terminal if the user presses ctrl-d, but that shouldn't
167 * happen if the terminal state change done in getch2_enable()
168 * works.
170 if (retval < 1)
171 return retval;
172 getch2_len += retval;
174 while (getch2_len > 0 && (getch2_len > 1 || getch2_buf[0] != 27)) {
175 int i, len, code;
177 /* First find in the TERMCAP database: */
178 for (i = 0; i < getch2_key_db; i++) {
179 if ((len = getch2_keys[i].len) <= getch2_len)
180 if(memcmp(getch2_keys[i].chars, getch2_buf, len) == 0) {
181 code = getch2_keys[i].code;
182 goto found;
185 /* We always match some keypress here, with length 1 if nothing else.
186 * Since some of the cases explicitly test remaining buffer length
187 * having a keycode only partially read in the buffer could incorrectly
188 * use the first byte as an independent character.
189 * However the buffer is big enough that this shouldn't happen too
190 * easily, and it's been this way for years without many complaints.
191 * I see no simple fix as there's no easy test which would tell
192 * whether a string must be part of a longer keycode. */
193 len = 1;
194 code = getch2_buf[0];
195 /* Check the well-known codes... */
196 if (code != 27) {
197 if (code == 'A'-64) code = KEY_HOME;
198 else if (code == 'E'-64) code = KEY_END;
199 else if (code == 'D'-64) code = KEY_DEL;
200 else if (code == 'H'-64) code = KEY_BS;
201 else if (code == 'U'-64) code = KEY_PGUP;
202 else if (code == 'V'-64) code = KEY_PGDWN;
203 else if (code == 8 || code==127) code = KEY_BS;
204 else if (code == 10 || code==13) {
205 if (getch2_len > 1) {
206 int c = getch2_buf[1];
207 if ((c == 10 || c == 13) && (c != code))
208 len = 2;
210 code = KEY_ENTER;
211 } else {
212 int utf8len = bstr_parse_utf8_code_length(code);
213 if (utf8len > 0 && utf8len <= getch2_len) {
214 struct bstr s = { getch2_buf, utf8len };
215 int unicode = bstr_decode_utf8(s, NULL);
216 if (unicode > 0) {
217 len = utf8len;
218 code = unicode;
223 else if (getch2_len > 1) {
224 int c = getch2_buf[1];
225 if (c == 27) {
226 code = KEY_ESC;
227 len = 2;
228 goto found;
230 if (c >= '0' && c <= '9') {
231 code = c-'0'+KEY_F;
232 len = 2;
233 goto found;
235 if (getch2_len >= 4 && c == '[' && getch2_buf[2] == '[') {
236 int c = getch2_buf[3];
237 if (c >= 'A' && c < 'A'+12) {
238 code = KEY_F+1 + c-'A';
239 len = 4;
240 goto found;
243 if ((c == '[' || c == 'O') && getch2_len >= 3) {
244 int c = getch2_buf[2];
245 const int ctable[] = {
246 KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT, 0,
247 KEY_END, KEY_PGDWN, KEY_HOME, KEY_PGUP, 0, 0, KEY_INS, 0, 0, 0,
248 KEY_F+1, KEY_F+2, KEY_F+3, KEY_F+4};
249 if (c >= 'A' && c <= 'S')
250 if (ctable[c - 'A']) {
251 code = ctable[c - 'A'];
252 len = 3;
253 goto found;
256 if (getch2_len >= 4 && c == '[' && getch2_buf[3] == '~') {
257 int c = getch2_buf[2];
258 const int ctable[8] = {KEY_HOME, KEY_INS, KEY_DEL, KEY_END, KEY_PGUP, KEY_PGDWN, KEY_HOME, KEY_END};
259 if (c >= '1' && c <= '8') {
260 code = ctable[c - '1'];
261 len = 4;
262 goto found;
265 if (getch2_len >= 5 && c == '[' && getch2_buf[4] == '~') {
266 int i = getch2_buf[2] - '0';
267 int j = getch2_buf[3] - '0';
268 if (i >= 0 && i <= 9 && j >= 0 && j <= 9) {
269 const short ftable[20] = {
270 11,12,13,14,15, 17,18,19,20,21,
271 23,24,25,26,28, 29,31,32,33,34 };
272 int a = i*10 + j;
273 for (i = 0; i < 20; i++)
274 if (ftable[i] == a) {
275 code = KEY_F+1 + i;
276 len = 5;
277 goto found;
282 found:
283 getch2_len -= len;
284 for (i = 0; i < getch2_len; i++)
285 getch2_buf[i] = getch2_buf[len+i];
286 mplayer_put_key(fifo, code);
288 return true;
291 static int getch2_status=0;
293 void getch2_enable(void){
294 #ifdef HAVE_TERMIOS
295 struct termios tio_new;
296 tcgetattr(0,&tio_orig);
297 tio_new=tio_orig;
298 tio_new.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
299 tio_new.c_cc[VMIN] = 0;
300 tio_new.c_cc[VTIME] = 0;
301 tcsetattr(0,TCSANOW,&tio_new);
302 #endif
303 getch2_status=1;
306 void getch2_disable(void){
307 if(!getch2_status) return; // already disabled / never enabled
308 #ifdef HAVE_TERMIOS
309 tcsetattr(0,TCSANOW,&tio_orig);
310 #endif
311 getch2_status=0;
314 #ifdef CONFIG_ICONV
315 char* get_term_charset(void)
317 char* charset = NULL;
318 #ifdef HAVE_LANGINFO
319 setlocale(LC_CTYPE, "");
320 charset = strdup(nl_langinfo(CODESET));
321 setlocale(LC_CTYPE, "C");
322 #endif
323 return charset;
325 #endif