Mercurial > hg > forks > pwpunix
annotate pwplib/tty.c @ 54:85671798fdb3
Various code cleanups, cosmetics, warning fixes.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 26 May 2010 01:22:51 +0300 |
parents | d803c129c1e2 |
children | 77f40a3c0095 |
rev | line source |
---|---|
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 | |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
14 void tty_restore_termios(void) |
0 | 15 { |
16 tcsetattr(STDIN,TCSADRAIN,&pwp_tty.setup); | |
17 } | |
18 | |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
19 void tty_init_termios(void) |
0 | 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 | |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
32 pwplib_regdestr(tty_restore_termios); |
0 | 33 } |
34 | |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
35 void tty_getwinsize(void) |
0 | 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 | |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
64 void tty_probe_raw(void) |
0 | 65 { |
66 fd_set fdset; | |
67 struct timeval timeout; | |
68 | |
69 tty_init_termios(); | |
70 | |
54
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
71 memset(&fdset, 0, sizeof(fd_set)); |
0 | 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 | |
36
d803c129c1e2
Obviously this is a bug, fixed.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
141 if(pwp_tty.type==TTY_VT2XX)pwp_tty.type=TTY_VT1XX; |
0 | 142 #endif |
143 | |
144 } | |
145 else | |
146 pwp_tty.type=TTY_DUMB; | |
147 | |
148 tty_restore_termios(); | |
149 } | |
150 | |
151 /******************************************************************/ | |
152 | |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
153 int tty_probe(void) |
0 | 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 | |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
207 int tty_init(void) |
0 | 208 { |
209 #ifdef HAVE_TTYSTUFF | |
210 | |
54
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
211 /* int colors=2,chars=2,rasters=1; */ |
0 | 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 | |
54
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
230 switch(pwp_tty.type) |
0 | 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 | |
19 | 239 pwpwrite("vt220/compatible (vt%d)\n",pwp_tty.minor); |
0 | 240 tty_vt2xx_init(); |
241 break; | |
242 #endif | |
243 case(TTY_VT1XX): | |
244 pwpwrite("vt100/compatible"); | |
19 | 245 if (pwp_tty.minor>=200) |
246 pwpwrite(" (a vt%d wannabe)\n",pwp_tty.minor); | |
247 else | |
248 pwpwrite("\n"); | |
0 | 249 tty_vt1xx_init(); |
250 break; | |
251 | |
252 | |
253 # ifdef DRIVE_LINUXCON | |
254 case(TTY_LINUX): | |
255 pwpwrite("linux console\n"); | |
256 tty_linux_init(); | |
257 break; | |
258 # endif | |
259 | |
260 case(TTY_XVT_COLOR): | |
261 pwpwrite("xterm/compatible with color\n"); | |
262 tty_ansicol_init(); | |
263 break; | |
264 | |
265 case(TTY_XVT_MONO): | |
266 pwpwrite("xterm (monochrome)\n"); | |
267 tty_vt1xx_init(); | |
268 break; | |
269 | |
270 case(TTY_ANSISYS): | |
271 pwpwrite("msdos ansi or compatible\n"); | |
272 tty_ansisys_init(); | |
273 break; | |
274 } | |
275 #endif | |
276 return 1; | |
277 } | |
278 | |
279 #endif |