Mercurial > hg > forks > pwpunix
annotate pwplib/tty.c @ 57:7d4f6c33a6ac
Warning cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 28 May 2010 15:24:04 +0300 |
parents | 5d819ba6891c |
children |
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 | |
55 | 14 static void tty_restore_termios(void) |
0 | 15 { |
16 tcsetattr(STDIN,TCSADRAIN,&pwp_tty.setup); | |
17 } | |
18 | |
55 | 19 static 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 | |
55 | 35 static 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 | |
57 | 54 void tty_write(void *data, int lgt) |
0 | 55 { |
57 | 56 if (lgt < 0) lgt = strlen((char *) data); |
57 write(pwp_tty.fd, data, lgt); | |
58 pwplib.timer_counter += lgt; | |
0 | 59 } |
60 | |
61 /******************************************************************/ | |
62 | |
55 | 63 static void tty_probe_raw(void) |
0 | 64 { |
65 fd_set fdset; | |
66 struct timeval timeout; | |
67 | |
68 tty_init_termios(); | |
69 | |
54
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
70 memset(&fdset, 0, sizeof(fd_set)); |
0 | 71 FD_SET(0,&fdset); |
72 timeout.tv_sec=0; | |
73 timeout.tv_usec=250000; | |
74 | |
75 write(STDOUT,"\33[c",3*sizeof(char)); | |
76 | |
77 usleep(250000); /* give it some time to react */ | |
78 | |
79 select(0,&fdset,NULL,NULL,&timeout); | |
80 | |
81 if(FD_ISSET(STDIN,&fdset)) | |
82 { | |
83 char buf[40],*b; | |
84 int lgt,num; | |
85 | |
86 lgt=read(STDIN,buf,40*sizeof(char))/sizeof(char); | |
87 | |
88 if(lgt<3) return; | |
89 if( (buf[0]!=27) || | |
90 (buf[1]!='[') || | |
91 (buf[2]!='?')) return; | |
92 | |
93 b=buf+3;lgt-=3;num=0; | |
94 | |
95 pwp_tty.type=TTY_VT1XX; | |
96 pwp_tty.minor=0; | |
97 pwp_tty.vtcap=0; | |
98 | |
99 while(lgt) | |
100 { | |
101 char c=*b++; | |
102 if(c>='0' && c<='9') | |
103 num=(num*10)+c-'0'; | |
104 else | |
105 { | |
106 /*fprintf(stderr,"NUMBAH %d\n",num);*/ | |
107 if(num>=62) | |
108 { | |
109 pwp_tty.type =TTY_VT2XX; | |
110 pwp_tty.minor=20+(num-60)*100; | |
111 } | |
112 else | |
113 if(num<32)pwp_tty.vtcap|=(1<<num); | |
114 | |
115 num=0; | |
116 } | |
117 lgt--; | |
118 } | |
119 | |
120 if(pwp_tty.vtcap&(1<<3)) /* ReGIS */ | |
121 { | |
122 if(pwp_tty.minor==220) | |
123 pwp_tty.minor=240; | |
124 if(pwp_tty.minor==320) | |
125 { | |
126 pwp_tty.minor=330; | |
127 /* how to check 340? */ | |
128 } | |
129 /* ... otherz */ | |
130 } | |
131 | |
132 /* protection from vt2xx wannabes - enable after lag fix */ | |
133 #ifdef DRIVE_TTY_VT2XX | |
134 /* if( (pwp_tty.type==TTY_VT2XX) && | |
135 (!(pwp_tty.vtcap&(1<<7)))) | |
136 { | |
137 pwp_tty.type=TTY_VT1XX; | |
138 }*/ | |
139 #else | |
36
d803c129c1e2
Obviously this is a bug, fixed.
Matti Hamalainen <ccr@tnsp.org>
parents:
19
diff
changeset
|
140 if(pwp_tty.type==TTY_VT2XX)pwp_tty.type=TTY_VT1XX; |
0 | 141 #endif |
142 | |
143 } | |
144 else | |
145 pwp_tty.type=TTY_DUMB; | |
146 | |
147 tty_restore_termios(); | |
148 } | |
149 | |
150 /******************************************************************/ | |
151 | |
55 | 152 static int tty_probe(void) |
0 | 153 { |
154 char*tt; | |
155 | |
156 pwp_tty.type=TTY_DUMB; | |
157 | |
158 pwpwrite("* getting terminal type..\n"); | |
159 | |
160 tt=pwplib.set.term; | |
161 if(tt==NULL) | |
162 { | |
163 tty_probe_raw(); | |
164 tt=getenv("TERM"); | |
165 if(tt==NULL)return pwp_tty.type; | |
166 } | |
167 | |
168 if(tt[0]=='x')pwp_tty.type=TTY_XVT_MONO; | |
169 | |
170 if( (!strcmp(tt,"xterm-color")) || | |
171 (!strcmp(tt,"aixterm")) | |
172 ) | |
173 { | |
174 return TTY_XVT_COLOR; | |
175 } | |
176 | |
177 if( (pwp_tty.type==TTY_VT1XX) || | |
178 (pwp_tty.type==TTY_DUMB)) | |
179 { | |
180 if(tt[0]=='v') | |
181 { | |
182 pwp_tty.minor=atoi(tt+2); | |
183 | |
184 if(pwp_tty.minor>=200 && pwp_tty.type!=TTY_VT1XX) | |
185 return TTY_VT2XX; | |
186 else return TTY_VT1XX; | |
187 } | |
188 | |
189 if(tt[0]=='a') | |
190 return TTY_ANSISYS; | |
191 | |
192 if(tt[0]=='l') | |
193 return TTY_LINUX; | |
194 | |
195 if(!strcmp(tt,"dumb")) | |
196 return TTY_DUMB; | |
197 } | |
198 | |
199 return pwp_tty.type; | |
200 } | |
201 | |
202 #endif | |
203 | |
204 /******************************************************************/ | |
205 | |
17
c60e531d19cd
Some misc. cleanups and minor warning removals.
Matti Hamalainen <ccr@tnsp.org>
parents:
0
diff
changeset
|
206 int tty_init(void) |
0 | 207 { |
208 #ifdef HAVE_TTYSTUFF | |
209 | |
54
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
210 /* int colors=2,chars=2,rasters=1; */ |
0 | 211 |
212 pwp_tty.fd=2; /* stderr */ | |
213 | |
214 tty_init_termios(); | |
215 | |
216 tty_getwinsize(); | |
217 pwp_tty.type=tty_probe(); | |
218 #endif | |
219 pwplib.videobuf.width=pwp_tty.width; | |
220 pwplib.videobuf.height=pwp_tty.height; | |
221 pwplib.videobuf.d=malloc(pwplib.videobuf.width* | |
222 pwplib.videobuf.height*sizeof(u16)); | |
223 #ifdef HAVE_TTYSTUFF | |
224 pwp_tty.prevbuf=malloc(pwplib.videobuf.width* | |
225 pwplib.videobuf.height*sizeof(u16)); | |
226 | |
227 pwpwrite("* terminal on stdout: "); | |
228 | |
54
85671798fdb3
Various code cleanups, cosmetics, warning fixes.
Matti Hamalainen <ccr@tnsp.org>
parents:
36
diff
changeset
|
229 switch(pwp_tty.type) |
0 | 230 { |
231 case(TTY_DUMB): | |
232 pwpwrite("dumb or unsupported\n"); | |
233 tty_dumb_init(); | |
234 break; | |
235 | |
236 case(TTY_VT2XX): | |
237 #ifdef DRIVE_TTY_VT2XX | |
19 | 238 pwpwrite("vt220/compatible (vt%d)\n",pwp_tty.minor); |
0 | 239 tty_vt2xx_init(); |
240 break; | |
241 #endif | |
242 case(TTY_VT1XX): | |
243 pwpwrite("vt100/compatible"); | |
19 | 244 if (pwp_tty.minor>=200) |
245 pwpwrite(" (a vt%d wannabe)\n",pwp_tty.minor); | |
246 else | |
247 pwpwrite("\n"); | |
0 | 248 tty_vt1xx_init(); |
249 break; | |
250 | |
251 | |
252 # ifdef DRIVE_LINUXCON | |
253 case(TTY_LINUX): | |
254 pwpwrite("linux console\n"); | |
255 tty_linux_init(); | |
256 break; | |
257 # endif | |
258 | |
259 case(TTY_XVT_COLOR): | |
260 pwpwrite("xterm/compatible with color\n"); | |
261 tty_ansicol_init(); | |
262 break; | |
263 | |
264 case(TTY_XVT_MONO): | |
265 pwpwrite("xterm (monochrome)\n"); | |
266 tty_vt1xx_init(); | |
267 break; | |
268 | |
269 case(TTY_ANSISYS): | |
270 pwpwrite("msdos ansi or compatible\n"); | |
271 tty_ansisys_init(); | |
272 break; | |
273 } | |
274 #endif | |
275 return 1; | |
276 } | |
277 | |
278 #endif |