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