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  */
23 
24 #include "config.h"
25 
26 //#define HAVE_TERMCAP
27 #if !defined(__OS2__) && !defined(__MORPHOS__)
28 #define CONFIG_IOCTL
29 #endif
30 
31 #define MAX_KEYS 64
32 #define BUF_LEN 256
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <sys/time.h>
39 #include <sys/types.h>
40 #ifdef CONFIG_IOCTL
41 #include <sys/ioctl.h>
42 #endif
43 
44 #ifdef HAVE_TERMIOS
45 #ifdef HAVE_TERMIOS_H
46 #include <termios.h>
47 #endif
48 #ifdef HAVE_SYS_TERMIOS_H
49 #include <sys/termios.h>
50 #endif
51 #endif
52 
53 #if defined(HAVE_LANGINFO) && defined(CONFIG_ICONV)
54 #include <locale.h>
55 #include <langinfo.h>
56 #endif
57 
58 #include <unistd.h>
59 
60 #include "mp_fifo.h"
61 #include "keycodes.h"
62 #include "getch2.h"
63 #include "libavutil/common.h"
64 
65 #ifdef HAVE_TERMIOS
66 static struct termios tio_orig;
67 #endif
68 static int getch2_len=0;
69 static uint8_t getch2_buf[BUF_LEN];
70 
71 int screen_width=80;
72 int screen_height=24;
73 char * erase_to_end_of_line = NULL;
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 
83 #ifdef HAVE_TERMCAP
84 
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
93 
94 static char term_buffer[4096];
95 static char term_buffer2[4096];
96 static char *term_p=term_buffer2;
97 
termcap_add(char * id,int code)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   if (getch2_keys[getch2_key_db].len >= sizeof(getch2_keys[getch2_key_db].chars))
104   {
105     printf("Key string entry more than 7 characters, ignoring\n");
106     return;
107   }
108   strcpy(getch2_keys[getch2_key_db].chars,p);
109   getch2_keys[getch2_key_db].code=code;
110   ++getch2_key_db;
111 /*  printf("%s=%s\n",id,p); */
112 }
113 
114 static int success=0;
115 
load_termcap(char * termtype)116 int load_termcap(char *termtype){
117   if(!termtype) termtype=getenv("TERM");
118   if(!termtype) termtype="unknown";
119   success=tgetent(term_buffer, termtype);
120   if(success<0){ printf("Could not access the 'termcap' data base.\n"); return 0; }
121   if(success==0){ printf("Terminal type `%s' is not defined.\n", termtype);return 0;}
122 
123   screen_width=tgetnum("co");
124   screen_height=tgetnum("li");
125   if(screen_width<1 || screen_width>255) screen_width=80;
126   if(screen_height<1 || screen_height>255) screen_height=24;
127   erase_to_end_of_line= tgetstr("cd",&term_p);
128 
129   termcap_add("kP",KEY_PGUP);
130   termcap_add("kN",KEY_PGDWN);
131   termcap_add("kh",KEY_HOME);
132   termcap_add("kH",KEY_END);
133   termcap_add("kI",KEY_INS);
134   termcap_add("kD",KEY_DEL);
135   termcap_add("kb",KEY_BS);
136   termcap_add("kl",KEY_LEFT);
137   termcap_add("kd",KEY_DOWN);
138   termcap_add("ku",KEY_UP);
139   termcap_add("kr",KEY_RIGHT);
140   termcap_add("k0",KEY_F+0);
141   termcap_add("k1",KEY_F+1);
142   termcap_add("k2",KEY_F+2);
143   termcap_add("k3",KEY_F+3);
144   termcap_add("k4",KEY_F+4);
145   termcap_add("k5",KEY_F+5);
146   termcap_add("k6",KEY_F+6);
147   termcap_add("k7",KEY_F+7);
148   termcap_add("k8",KEY_F+8);
149   termcap_add("k9",KEY_F+9);
150   termcap_add("k;",KEY_F+10);
151   return getch2_key_db;
152 }
153 
154 #endif
155 
get_screen_size(void)156 void get_screen_size(void){
157 #ifdef CONFIG_IOCTL
158   struct winsize ws;
159   if (ioctl(0, TIOCGWINSZ, &ws) < 0 || !ws.ws_row || !ws.ws_col) return;
160 /*  printf("Using IOCTL\n"); */
161   screen_width=ws.ws_col;
162   screen_height=ws.ws_row;
163 #endif
164 }
165 
getch2(void)166 void getch2(void)
167 {
168     int retval = read(0, &getch2_buf[getch2_len], BUF_LEN-getch2_len);
169     if (retval < 1)
170         return;
171     getch2_len += retval;
172 
173     while (getch2_len > 0 && (getch2_len > 1 || getch2_buf[0] != 27)) {
174         int i, len, code;
175 
176         /* First find in the TERMCAP database: */
177         for (i = 0; i < getch2_key_db; i++) {
178             if ((len = getch2_keys[i].len) <= getch2_len)
179                 if(memcmp(getch2_keys[i].chars, getch2_buf, len) == 0) {
180                     code = getch2_keys[i].code;
181                     goto found;
182                 }
183         }
184         /* We always match some keypress here, with length 1 if nothing else.
185          * Since some of the cases explicitly test remaining buffer length
186          * having a keycode only partially read in the buffer could incorrectly
187          * use the first byte as an independent character.
188          * However the buffer is big enough that this shouldn't happen too
189          * easily, and it's been this way for years without many complaints.
190          * I see no simple fix as there's no easy test which would tell
191          * whether a string must be part of a longer keycode. */
192         len = 1;
193         code = getch2_buf[0];
194         /* Check the well-known codes... */
195         if (code != 27) {
196             if (code == 'A'-64) code = KEY_HOME;
197             else if (code == 'E'-64) code = KEY_END;
198             else if (code == 'D'-64) code = KEY_DEL;
199             else if (code == 'H'-64) code = KEY_BS;
200             else if (code == 'U'-64) code = KEY_PGUP;
201             else if (code == 'V'-64) code = KEY_PGDWN;
202             else if (code == 8 || code==127) code = KEY_BS;
203             else if (code == 10 || code==13) {
204                 if (getch2_len > 1) {
205                     int c = getch2_buf[1];
206                     if ((c == 10 || c == 13) && (c != code))
207                         len = 2;
208                 }
209                 code = KEY_ENTER;
210             } else if (code >= 0xc0) {
211                 uint32_t utf8 = 0;
212                 i = 0;
213                 GET_UTF8(utf8, (i < getch2_len ? getch2_buf[i++] : 0), goto not_utf8;)
214                 if (utf8 < KEY_BASE) {
215                     code = utf8;
216                     len = i;
217                 }
218             }
219         }
220         else if (getch2_len > 1) {
221             int c = getch2_buf[1];
222             if (c == 27) {
223                 code = KEY_ESC;
224                 len = 2;
225                 goto found;
226             }
227             if (c >= '0' && c <= '9') {
228                 code = c-'0'+KEY_F;
229                 len = 2;
230                 goto found;
231             }
232             if (getch2_len >= 4 && c == '[' && getch2_buf[2] == '[') {
233                 int c = getch2_buf[3];
234                 if (c >= 'A' && c < 'A'+12) {
235                     code = KEY_F+1 + c-'A';
236                     len = 4;
237                     goto found;
238                 }
239             }
240             if ((c == '[' || c == 'O') && getch2_len >= 3) {
241                 int c = getch2_buf[2];
242                 static const unsigned ctable[] = {
243                     KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT, 0,
244                     KEY_END, KEY_PGDWN, KEY_HOME, KEY_PGUP, 0, 0, KEY_INS, 0, 0, 0,
245                     KEY_F+1, KEY_F+2, KEY_F+3, KEY_F+4};
246                 if (c >= 'A' && c <= 'S')
247                     if (ctable[c - 'A']) {
248                         code = ctable[c - 'A'];
249                         len = 3;
250                         goto found;
251                     }
252             }
253             if (getch2_len >= 4 && c == '[' && getch2_buf[3] == '~') {
254                 int c = getch2_buf[2];
255                 static const int ctable[8] = {KEY_HOME, KEY_INS, KEY_DEL, KEY_END, KEY_PGUP, KEY_PGDWN, KEY_HOME, KEY_END};
256                 if (c >= '1' && c <= '8') {
257                     code = ctable[c - '1'];
258                     len = 4;
259                     goto found;
260                 }
261             }
262             if (getch2_len >= 5 && c == '[' && getch2_buf[4] == '~') {
263                 int i = getch2_buf[2] - '0';
264                 int j = getch2_buf[3] - '0';
265                 if (i >= 0 && i <= 9 && j >= 0 && j <= 9) {
266                     static const uint8_t ftable[20] = {
267                         11,12,13,14,15, 17,18,19,20,21,
268                         23,24,25,26,28, 29,31,32,33,34 };
269                     int a = i*10 + j;
270                     for (i = 0; i < 20; i++)
271                         if (ftable[i] == a) {
272                             code = KEY_F+1 + i;
273                             len = 5;
274                             goto found;
275                         }
276                 }
277             }
278         }
279     not_utf8:
280     found:
281         getch2_len -= len;
282         for (i = 0; i < getch2_len; i++)
283             getch2_buf[i] = getch2_buf[len+i];
284         mplayer_put_key(code);
285     }
286 }
287 
288 static int getch2_status=0;
289 
getch2_enable(void)290 void getch2_enable(void){
291 #ifdef HAVE_TERMIOS
292 struct termios tio_new;
293     tcgetattr(0,&tio_orig);
294     tio_new=tio_orig;
295     tio_new.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
296     tio_new.c_cc[VMIN] = 1;
297     tio_new.c_cc[VTIME] = 0;
298     tcsetattr(0,TCSANOW,&tio_new);
299 #endif
300     getch2_status=1;
301 }
302 
getch2_disable(void)303 void getch2_disable(void){
304     if(!getch2_status) return; // already disabled / never enabled
305 #ifdef HAVE_TERMIOS
306     tcsetattr(0,TCSANOW,&tio_orig);
307 #endif
308     getch2_status=0;
309 }
310 
311 #ifdef CONFIG_ICONV
get_term_charset(void)312 char* get_term_charset(void)
313 {
314     char* charset = NULL;
315 #ifdef HAVE_LANGINFO
316     setlocale(LC_CTYPE, "");
317     charset = strdup(nl_langinfo(CODESET));
318     setlocale(LC_CTYPE, "C");
319 #endif
320     return charset;
321 }
322 #endif
323