0
|
1 #include "config.h"
|
|
2 #ifdef DRIVE_TTY
|
|
3
|
|
4 #include "pwplib.h"
|
|
5
|
|
6 #define __TTY_C
|
|
7
|
|
8 #include "tty.h"
|
|
9
|
|
10 /****************/
|
|
11
|
|
12 #ifdef HAVE_TTYSTUFF
|
|
13
|
|
14 void tty_restore_termios()
|
|
15 {
|
|
16 tcsetattr(STDIN,TCSADRAIN,&pwp_tty.setup);
|
|
17 }
|
|
18
|
|
19 void tty_init_termios()
|
|
20 {
|
|
21 struct termios t;
|
|
22 tcgetattr(STDIN,&t);
|
|
23 memcpy(&pwp_tty.setup,&t,sizeof(struct termios));
|
|
24
|
|
25 t.c_lflag=ISIG; /* &=~(ECHO+ECHOE+ECHOK+ECHONL+NOFLSH);*/
|
|
26
|
|
27 /* t.c_lflag=t.c_oflag=0; */
|
|
28 t.c_cc[VMIN]=1;
|
|
29 t.c_cc[VTIME]=0;
|
|
30 tcsetattr(STDIN,TCSADRAIN,&t);
|
|
31
|
|
32 pwp_regdestr(tty_restore_termios);
|
|
33 }
|
|
34
|
|
35 void tty_getwinsize()
|
|
36 {
|
|
37 # ifdef TIOCGWINSZ
|
|
38 {struct winsize wsz;
|
|
39 if(ioctl(0,TIOCGWINSZ,&wsz)>=0){
|
|
40 pwp_tty.width=wsz.ws_col;pwp_tty.height=wsz.ws_row;
|
|
41 if(pwp_tty.width>0 && pwp_tty.height>0) return;
|
|
42 }}
|
|
43 # endif
|
|
44
|
|
45 {char*tmp;
|
|
46 pwp_tty.height=((tmp=getenv("LINES"))?atoi(tmp):0);
|
|
47 pwp_tty.width=((tmp=getenv("COLUMNS"))?atoi(tmp):0);
|
|
48 if(pwp_tty.height>0 && pwp_tty.width>0)return;}
|
|
49
|
|
50 pwp_tty.height=24;
|
|
51 pwp_tty.width=80;
|
|
52 }
|
|
53
|
|
54 void tty_write(u8*data,int lgt)
|
|
55 {
|
|
56 if(lgt<0)lgt=strlen(data);
|
|
57
|
|
58 write(pwp_tty.fd,data,lgt);
|
|
59 pwplib.timer_counter+=lgt;
|
|
60 }
|
|
61
|
|
62 /******************************************************************/
|
|
63
|
|
64 void tty_probe_raw()
|
|
65 {
|
|
66 fd_set fdset;
|
|
67 struct timeval timeout;
|
|
68
|
|
69 tty_init_termios();
|
|
70
|
|
71 memset((void*)&fdset,0,sizeof(fd_set));
|
|
72 FD_SET(0,&fdset);
|
|
73 timeout.tv_sec=0;
|
|
74 timeout.tv_usec=250000;
|
|
75
|
|
76 write(STDOUT,"\33[c",3*sizeof(char));
|
|
77
|
|
78 usleep(250000); /* give it some time to react */
|
|
79
|
|
80 select(0,&fdset,NULL,NULL,&timeout);
|
|
81
|
|
82 if(FD_ISSET(STDIN,&fdset))
|
|
83 {
|
|
84 char buf[40],*b;
|
|
85 int lgt,num;
|
|
86
|
|
87 lgt=read(STDIN,buf,40*sizeof(char))/sizeof(char);
|
|
88
|
|
89 if(lgt<3) return;
|
|
90 if( (buf[0]!=27) ||
|
|
91 (buf[1]!='[') ||
|
|
92 (buf[2]!='?')) return;
|
|
93
|
|
94 b=buf+3;lgt-=3;num=0;
|
|
95
|
|
96 pwp_tty.type=TTY_VT1XX;
|
|
97 pwp_tty.minor=0;
|
|
98 pwp_tty.vtcap=0;
|
|
99
|
|
100 while(lgt)
|
|
101 {
|
|
102 char c=*b++;
|
|
103 if(c>='0' && c<='9')
|
|
104 num=(num*10)+c-'0';
|
|
105 else
|
|
106 {
|
|
107 /*fprintf(stderr,"NUMBAH %d\n",num);*/
|
|
108 if(num>=62)
|
|
109 {
|
|
110 pwp_tty.type =TTY_VT2XX;
|
|
111 pwp_tty.minor=20+(num-60)*100;
|
|
112 }
|
|
113 else
|
|
114 if(num<32)pwp_tty.vtcap|=(1<<num);
|
|
115
|
|
116 num=0;
|
|
117 }
|
|
118 lgt--;
|
|
119 }
|
|
120
|
|
121 if(pwp_tty.vtcap&(1<<3)) /* ReGIS */
|
|
122 {
|
|
123 if(pwp_tty.minor==220)
|
|
124 pwp_tty.minor=240;
|
|
125 if(pwp_tty.minor==320)
|
|
126 {
|
|
127 pwp_tty.minor=330;
|
|
128 /* how to check 340? */
|
|
129 }
|
|
130 /* ... otherz */
|
|
131 }
|
|
132
|
|
133 /* protection from vt2xx wannabes - enable after lag fix */
|
|
134 #ifdef DRIVE_TTY_VT2XX
|
|
135 /* if( (pwp_tty.type==TTY_VT2XX) &&
|
|
136 (!(pwp_tty.vtcap&(1<<7))))
|
|
137 {
|
|
138 pwp_tty.type=TTY_VT1XX;
|
|
139 }*/
|
|
140 #else
|
|
141 if(pwp_tty.type=TTY_VT2XX)pwp_tty.type=TTY_VT1XX;
|
|
142 #endif
|
|
143
|
|
144 }
|
|
145 else
|
|
146 pwp_tty.type=TTY_DUMB;
|
|
147
|
|
148 tty_restore_termios();
|
|
149 }
|
|
150
|
|
151 /******************************************************************/
|
|
152
|
|
153 int tty_probe()
|
|
154 {
|
|
155 char*tt;
|
|
156
|
|
157 pwp_tty.type=TTY_DUMB;
|
|
158
|
|
159 pwpwrite("* getting terminal type..\n");
|
|
160
|
|
161 tt=pwplib.set.term;
|
|
162 if(tt==NULL)
|
|
163 {
|
|
164 tty_probe_raw();
|
|
165 tt=getenv("TERM");
|
|
166 if(tt==NULL)return pwp_tty.type;
|
|
167 }
|
|
168
|
|
169 if(tt[0]=='x')pwp_tty.type=TTY_XVT_MONO;
|
|
170
|
|
171 if( (!strcmp(tt,"xterm-color")) ||
|
|
172 (!strcmp(tt,"aixterm"))
|
|
173 )
|
|
174 {
|
|
175 return TTY_XVT_COLOR;
|
|
176 }
|
|
177
|
|
178 if( (pwp_tty.type==TTY_VT1XX) ||
|
|
179 (pwp_tty.type==TTY_DUMB))
|
|
180 {
|
|
181 if(tt[0]=='v')
|
|
182 {
|
|
183 pwp_tty.minor=atoi(tt+2);
|
|
184
|
|
185 if(pwp_tty.minor>=200 && pwp_tty.type!=TTY_VT1XX)
|
|
186 return TTY_VT2XX;
|
|
187 else return TTY_VT1XX;
|
|
188 }
|
|
189
|
|
190 if(tt[0]=='a')
|
|
191 return TTY_ANSISYS;
|
|
192
|
|
193 if(tt[0]=='l')
|
|
194 return TTY_LINUX;
|
|
195
|
|
196 if(!strcmp(tt,"dumb"))
|
|
197 return TTY_DUMB;
|
|
198 }
|
|
199
|
|
200 return pwp_tty.type;
|
|
201 }
|
|
202
|
|
203 #endif
|
|
204
|
|
205 /******************************************************************/
|
|
206
|
|
207 int tty_init()
|
|
208 {
|
|
209 #ifdef HAVE_TTYSTUFF
|
|
210
|
|
211 int colors=2,chars=2,rasters=1;
|
|
212
|
|
213 pwp_tty.fd=2; /* stderr */
|
|
214
|
|
215 tty_init_termios();
|
|
216
|
|
217 tty_getwinsize();
|
|
218 pwp_tty.type=tty_probe();
|
|
219 #endif
|
|
220 pwplib.videobuf.width=pwp_tty.width;
|
|
221 pwplib.videobuf.height=pwp_tty.height;
|
|
222 pwplib.videobuf.d=malloc(pwplib.videobuf.width*
|
|
223 pwplib.videobuf.height*sizeof(u16));
|
|
224 #ifdef HAVE_TTYSTUFF
|
|
225 pwp_tty.prevbuf=malloc(pwplib.videobuf.width*
|
|
226 pwplib.videobuf.height*sizeof(u16));
|
|
227
|
|
228 pwpwrite("* terminal on stdout: ");
|
|
229
|
|
230 switch((int)pwp_tty.type)
|
|
231 {
|
|
232 case(TTY_DUMB):
|
|
233 pwpwrite("dumb or unsupported\n");
|
|
234 tty_dumb_init();
|
|
235 break;
|
|
236
|
|
237 case(TTY_VT2XX):
|
|
238 #ifdef DRIVE_TTY_VT2XX
|
|
239 {
|
|
240 char buf[40];
|
|
241 sprintf(buf,"vt220/compatible (vt%d)\n",pwp_tty.minor);
|
|
242 pwpwrite(buf);
|
|
243 }
|
|
244 tty_vt2xx_init();
|
|
245 break;
|
|
246 #endif
|
|
247 case(TTY_VT1XX):
|
|
248 pwpwrite("vt100/compatible");
|
|
249 if(pwp_tty.minor>=200)
|
|
250 { char buf[40];
|
|
251 sprintf(buf," (a vt%d wannabe)\n",pwp_tty.minor);
|
|
252 pwpwrite(buf);
|
|
253 }
|
|
254 else pwpwrite("\n");
|
|
255 tty_vt1xx_init();
|
|
256 break;
|
|
257
|
|
258
|
|
259 # ifdef DRIVE_LINUXCON
|
|
260 case(TTY_LINUX):
|
|
261 pwpwrite("linux console\n");
|
|
262 tty_linux_init();
|
|
263 break;
|
|
264 # endif
|
|
265
|
|
266 case(TTY_XVT_COLOR):
|
|
267 pwpwrite("xterm/compatible with color\n");
|
|
268 tty_ansicol_init();
|
|
269 break;
|
|
270
|
|
271 case(TTY_XVT_MONO):
|
|
272 pwpwrite("xterm (monochrome)\n");
|
|
273 tty_vt1xx_init();
|
|
274 break;
|
|
275
|
|
276 case(TTY_ANSISYS):
|
|
277 pwpwrite("msdos ansi or compatible\n");
|
|
278 tty_ansisys_init();
|
|
279 break;
|
|
280 }
|
|
281 #endif
|
|
282 return 1;
|
|
283 }
|
|
284
|
|
285 #endif
|
|
286
|
|
287
|