Mercurial > mplayer.hg
annotate linux/getch2.c @ 1755:ce3123d61380
ws.h/app.h
author | pontscho |
---|---|
date | Wed, 29 Aug 2001 17:45:40 +0000 |
parents | 77cf72182c7f |
children | d7920c8257e2 |
rev | line source |
---|---|
1 | 1 /* GyS-TermIO v2.0 (for GySmail v3) (C) 1999 A'rpi/ESP-team */ |
2 | |
3 #include "../config.h" | |
4 | |
5 //#define USE_TERMCAP | |
6 #define USE_IOCTL | |
7 | |
8 #define MAX_KEYS 64 | |
9 #define BUF_LEN 256 | |
10 | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <string.h> | |
14 #include <sys/time.h> | |
15 #include <sys/types.h> | |
16 #ifdef USE_IOCTL | |
17 #include <sys/ioctl.h> | |
18 #endif | |
19 #include <sys/termios.h> | |
20 #include <unistd.h> | |
21 | |
22 #include "keycodes.h" | |
23 | |
24 static struct termios tio_orig; | |
25 static int getch2_len=0; | |
26 static char getch2_buf[BUF_LEN]; | |
27 | |
28 int screen_width=80; | |
29 int screen_height=24; | |
30 | |
31 typedef struct { | |
32 int len; | |
33 int code; | |
34 char chars[8]; | |
35 } keycode_st; | |
36 static keycode_st getch2_keys[MAX_KEYS]; | |
37 static int getch2_key_db=0; | |
38 | |
39 #ifdef USE_TERMCAP | |
40 | |
41 #if 0 | |
42 #include <termcap.h> | |
43 #else | |
44 extern int tgetent (char *BUFFER, char *TERMTYPE); | |
45 extern int tgetnum (char *NAME); | |
46 extern int tgetflag (char *NAME); | |
47 extern char *tgetstr (char *NAME, char **AREA); | |
48 #endif | |
49 | |
50 static char term_buffer[4096]; | |
51 static char term_buffer2[4096]; | |
52 static char *term_p=term_buffer2; | |
53 | |
54 static void termcap_add(char *id,int code){ | |
55 char *p=tgetstr(id,&term_p); | |
56 if(!p) return; | |
57 if(getch2_key_db>=MAX_KEYS) return; | |
58 getch2_keys[getch2_key_db].len=strlen(p); | |
59 strncpy(getch2_keys[getch2_key_db].chars,p,8); | |
60 getch2_keys[getch2_key_db].code=code; | |
61 ++getch2_key_db; | |
62 /* printf("%s=%s\n",id,p); */ | |
63 } | |
64 | |
65 static int success=0; | |
66 | |
67 int load_termcap(char *termtype){ | |
68 if(!termtype) termtype=getenv("TERM"); | |
69 success=tgetent(term_buffer, termtype); | |
70 if(success<0){ printf("Could not access the 'termcap' data base.\n"); return 0; } | |
71 if(success==0){ printf("Terminal type `%s' is not defined.\n", termtype);return 0;} | |
72 | |
73 screen_width=tgetnum("co"); | |
74 screen_height=tgetnum("li"); | |
75 if(screen_width<1 || screen_width>255) screen_width=80; | |
76 if(screen_height<1 || screen_height>255) screen_height=24; | |
77 | |
78 termcap_add("kP",KEY_PGUP); | |
79 termcap_add("kN",KEY_PGDWN); | |
80 termcap_add("kh",KEY_HOME); | |
81 termcap_add("kH",KEY_END); | |
82 termcap_add("kI",KEY_INS); | |
83 termcap_add("kD",KEY_DEL); | |
84 termcap_add("kb",KEY_BS); | |
85 termcap_add("kl",KEY_LEFT); | |
86 termcap_add("kd",KEY_DOWN); | |
87 termcap_add("ku",KEY_UP); | |
88 termcap_add("kr",KEY_RIGHT); | |
89 termcap_add("k0",KEY_F+0); | |
90 termcap_add("k1",KEY_F+1); | |
91 termcap_add("k2",KEY_F+2); | |
92 termcap_add("k3",KEY_F+3); | |
93 termcap_add("k4",KEY_F+4); | |
94 termcap_add("k5",KEY_F+5); | |
95 termcap_add("k6",KEY_F+6); | |
96 termcap_add("k7",KEY_F+7); | |
97 termcap_add("k8",KEY_F+8); | |
98 termcap_add("k9",KEY_F+9); | |
99 termcap_add("k;",KEY_F+10); | |
100 return getch2_key_db; | |
101 } | |
102 | |
103 #endif | |
104 | |
105 void get_screen_size(){ | |
106 #ifdef USE_IOCTL | |
107 struct winsize ws; | |
108 if (ioctl(0, TIOCGWINSZ, &ws) < 0 || !ws.ws_row || !ws.ws_col) return; | |
109 /* printf("Using IOCTL\n"); */ | |
110 screen_width=ws.ws_col; | |
111 screen_height=ws.ws_row; | |
112 #endif | |
113 } | |
114 | |
115 int getch2(int time){ | |
116 int len=0; | |
117 int code=0; | |
118 int i=0; | |
119 | |
120 while(!getch2_len || (getch2_len==1 && getch2_buf[0]==27)){ | |
121 fd_set rfds; | |
122 struct timeval tv; | |
123 int retval; | |
124 /* Watch stdin (fd 0) to see when it has input. */ | |
125 FD_ZERO(&rfds); FD_SET(0,&rfds); | |
126 /* Wait up to 'time' microseconds. */ | |
127 tv.tv_sec=time/1000; tv.tv_usec = (time%1000)*1000; | |
128 retval=select(1, &rfds, NULL, NULL, &tv); | |
129 if(!retval) return -1; | |
130 /* Data is available now. */ | |
131 retval=read(0,&getch2_buf[getch2_len],BUF_LEN-getch2_len); | |
132 if(retval<1) return -1; | |
133 getch2_len+=retval; | |
134 } | |
135 | |
136 /* First find in the TERMCAP database: */ | |
137 for(i=0;i<getch2_key_db;i++){ | |
138 if((len=getch2_keys[i].len)<=getch2_len) | |
139 if(memcmp(getch2_keys[i].chars,getch2_buf,len)==0){ | |
140 code=getch2_keys[i].code; goto found; | |
141 } | |
142 } | |
143 len=1;code=getch2_buf[0]; | |
144 /* Check the well-known codes... */ | |
145 if(code!=27){ | |
146 if(code=='A'-64){ code=KEY_HOME; goto found;} | |
147 if(code=='E'-64){ code=KEY_END; goto found;} | |
148 if(code=='D'-64){ code=KEY_DEL; goto found;} | |
149 if(code=='H'-64){ code=KEY_BS; goto found;} | |
150 if(code=='U'-64){ code=KEY_PGUP; goto found;} | |
151 if(code=='V'-64){ code=KEY_PGDWN; goto found;} | |
152 if(code==8 || code==127){ code=KEY_BS; goto found;} | |
153 if(code==10 || code==13){ | |
154 if(getch2_len>1){ | |
155 int c=getch2_buf[1]; | |
156 if(c==10 || c==13) if(c!=code) len=2; | |
157 } | |
158 code=KEY_ENTER; | |
159 goto found; | |
160 } | |
161 } else if(getch2_len>1){ | |
162 int c=getch2_buf[1]; | |
163 if(c==27){ code=KEY_ESC; len=2; goto found;} | |
164 if(c>='0' && c<='9'){ code=c-'0'+KEY_F; len=2; goto found;} | |
165 if(getch2_len>=4 && c=='[' && getch2_buf[2]=='['){ | |
166 int c=getch2_buf[3]; | |
167 if(c>='A' && c<'A'+12){ code=KEY_F+1+c-'A';len=4;goto found;} | |
168 } | |
169 if(c=='[' || c=='O') if(getch2_len>=3){ | |
170 int c=getch2_buf[2]; | |
171 static short int ctable[]={ KEY_UP,KEY_DOWN,KEY_RIGHT,KEY_LEFT,0, | |
172 KEY_END,KEY_PGDWN,KEY_HOME,KEY_PGUP,0,0,KEY_INS,0,0,0, | |
173 KEY_F+1,KEY_F+2,KEY_F+3,KEY_F+4}; | |
174 if(c>='A' && c<='S') | |
175 if(ctable[c-'A']){ code=ctable[c-'A']; len=3; goto found;} | |
176 } | |
177 if(getch2_len>=4 && c=='[' && getch2_buf[3]=='~'){ | |
178 int c=getch2_buf[2]; | |
179 int ctable[8]={KEY_HOME,KEY_INS,KEY_DEL,KEY_END,KEY_PGUP,KEY_PGDWN,KEY_HOME,KEY_END}; | |
180 if(c>='1' && c<='8'){ code=ctable[c-'1']; len=4; goto found;} | |
181 } | |
182 if(getch2_len>=5 && c=='[' && getch2_buf[4]=='~'){ | |
183 int i=getch2_buf[2]-'0'; | |
184 int j=getch2_buf[3]-'0'; | |
185 if(i>=0 && i<=9 && j>=0 && j<=9){ | |
186 static short int ftable[20]={ | |
187 11,12,13,14,15, 17,18,19,20,21, | |
188 23,24,25,26,28, 29,31,32,33,34 }; | |
189 int a=i*10+j; | |
190 for(i=0;i<20;i++) if(ftable[i]==a){ code=KEY_F+1+i;len=5;goto found;} | |
191 } | |
192 } | |
193 } | |
194 found: | |
195 if((getch2_len-=len)>0){ | |
196 int i; | |
197 for(i=0;i<getch2_len;i++) getch2_buf[i]=getch2_buf[len+i]; | |
198 } | |
199 return code; | |
200 } | |
201 | |
1632 | 202 static int getch2_status=0; |
203 | |
1 | 204 void getch2_enable(){ |
205 struct termios tio_new; | |
1433 | 206 #if defined(__NetBSD__) || defined(__svr4__) || defined(__CYGWIN__) |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
958
diff
changeset
|
207 tcgetattr(0,&tio_orig); |
1451
267264560623
Start OpenBSD support and fix detection of older GNU as.
atmos4
parents:
1433
diff
changeset
|
208 #elif defined(__FreeBSD__) || defined(__OpenBSD__) |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
958
diff
changeset
|
209 ioctl(0,TIOCGETA,&tio_orig); |
958
162a78d3cc08
FreeBSD support by Vladimir Kushnir vkushnir@Alfacom.net
arpi_esp
parents:
1
diff
changeset
|
210 #else |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
958
diff
changeset
|
211 ioctl(0,TCGETS,&tio_orig); |
958
162a78d3cc08
FreeBSD support by Vladimir Kushnir vkushnir@Alfacom.net
arpi_esp
parents:
1
diff
changeset
|
212 #endif |
1 | 213 tio_new=tio_orig; |
214 tio_new.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */ | |
215 tio_new.c_cc[VMIN] = 1; | |
216 tio_new.c_cc[VTIME] = 0; | |
1433 | 217 #if defined(__NetBSD__) || defined(__svr4__) || defined(__CYGWIN__) |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
958
diff
changeset
|
218 tcsetattr(0,TCSANOW,&tio_new); |
1451
267264560623
Start OpenBSD support and fix detection of older GNU as.
atmos4
parents:
1433
diff
changeset
|
219 #elif defined(__FreeBSD__) || defined(__OpenBSD__) |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
958
diff
changeset
|
220 ioctl(0,TIOCSETA,&tio_new); |
958
162a78d3cc08
FreeBSD support by Vladimir Kushnir vkushnir@Alfacom.net
arpi_esp
parents:
1
diff
changeset
|
221 #else |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
958
diff
changeset
|
222 ioctl(0,TCSETS,&tio_new); |
958
162a78d3cc08
FreeBSD support by Vladimir Kushnir vkushnir@Alfacom.net
arpi_esp
parents:
1
diff
changeset
|
223 #endif |
1632 | 224 getch2_status=1; |
1 | 225 } |
226 | |
227 void getch2_disable(){ | |
1632 | 228 if(!getch2_status) return; // already disabled / never enabled |
1433 | 229 #if defined(__NetBSD__) || defined(__svr4__) || defined(__CYGWIN__) |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
958
diff
changeset
|
230 tcsetattr(0,TCSANOW,&tio_orig); |
1451
267264560623
Start OpenBSD support and fix detection of older GNU as.
atmos4
parents:
1433
diff
changeset
|
231 #elif defined(__FreeBSD__) || defined(__OpenBSD__) |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
958
diff
changeset
|
232 ioctl(0,TIOCSETA,&tio_orig); |
958
162a78d3cc08
FreeBSD support by Vladimir Kushnir vkushnir@Alfacom.net
arpi_esp
parents:
1
diff
changeset
|
233 #else |
1038
b36fb1ae4b53
applied solaris8/netbsd/other fixes patch by J¸«ärgen Keil <jk@tools.de>
arpi_esp
parents:
958
diff
changeset
|
234 ioctl(0,TCSETS,&tio_orig); |
958
162a78d3cc08
FreeBSD support by Vladimir Kushnir vkushnir@Alfacom.net
arpi_esp
parents:
1
diff
changeset
|
235 #endif |
1632 | 236 getch2_status=0; |
1 | 237 } |
238 |