comparison pwplib/linuxcon.c @ 0:acb5694e93d9

Initial import.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 18 May 2010 04:25:44 +0300
parents
children 2c72092554fa
comparison
equal deleted inserted replaced
-1:000000000000 0:acb5694e93d9
1 #include "config.h"
2
3 #ifdef DRIVE_LINUXCON
4
5 #include <sys/stat.h>
6 #include <sys/ioctl.h>
7 #include <linux/kd.h>
8
9 #include "pwplib.h"
10
11 #include "tty.h"
12
13 /******************** vcsa code ********************/
14
15 struct{
16 int fd;
17 int width,height;
18
19 int fontw,fonth;
20 u8*fontd;
21 char*fontd_tab;
22
23 /* add cfd (control fd) or something */
24 }pwp_linuxcon;
25
26 void vcsa_dump_attr()
27 {
28 char*buf=pwplib.videobuf.d;
29 int ptr=4+pwp_linuxcon.width*
30 ((pwp_linuxcon.height-pwplib.videobuf.height)&~1);
31 if(ptr<4)ptr=4;
32
33 if(pwp_linuxcon.width==pwplib.videobuf.width)
34 {
35 lseek(pwp_linuxcon.fd,ptr,SEEK_SET);
36 write(pwp_linuxcon.fd,buf,
37 pwplib.videobuf.width*pwplib.videobuf.height*2);
38 }else
39 {
40 int y=0,y1=pwplib.videobuf.height,w=pwplib.videobuf.width;
41 ptr+=((pwp_linuxcon.width-pwplib.videobuf.width)&~1);
42 if(y1>pwp_linuxcon.height)y1=pwp_linuxcon.height;
43 if(w>pwp_linuxcon.width)w=pwp_linuxcon.width;
44 w<<=1;
45 for(;y<y1;y++)
46 {
47 lseek(pwp_linuxcon.fd,ptr,SEEK_SET);
48 write(pwp_linuxcon.fd,buf,w);
49 buf+=pwplib.videobuf.width*2;
50 ptr+=pwp_linuxcon.width*2;
51 }
52 }
53
54 pwplib.timer_counter+=pwp_linuxcon.width*pwp_linuxcon.height*2;
55 }
56
57 int vcsa_get()
58 {
59 int vcsa;
60 int majmin;
61
62 { struct stat sbuf;
63 int stderr2=dup(fileno(stderr));
64 fstat(stderr2,&sbuf);
65 majmin=sbuf.st_rdev;
66 close(stderr2);
67 }
68
69 if((majmin&~0x63)==4*256)
70 {
71 char buf[20];
72 sprintf(buf,"/dev/vcsa%i",majmin&63);
73 vcsa=open(buf,O_RDWR);
74 if(vcsa>=0)return vcsa;
75 }
76 vcsa=open("/dev/vcsa",O_RDWR);
77 if(vcsa>=0)return vcsa;
78
79 return -1;
80 }
81
82 int vcsa_init() /* init vcsa device */
83 {
84 unsigned char vcsahdr[4];
85 int vcsa=vcsa_get();
86 if(vcsa<0)return 0;
87
88 read(vcsa,vcsahdr,4);
89 pwp_linuxcon.fd=vcsa;
90 pwplib.videobuf.height=pwp_linuxcon.height=vcsahdr[0];
91 pwplib.videobuf.width=pwp_linuxcon.width=vcsahdr[1];
92
93 pwpwrite("* using vcsa device\n");
94
95 pwplib.dump_attr=vcsa_dump_attr;
96 return 1;
97 }
98
99
100 /********************************************************/
101
102 /*** some people are lazy to update their headers ... ***/
103
104 #ifndef KDFONTOP
105 #define KDFONTOP 0x4B72
106
107 struct console_font_op {
108 unsigned int op;
109 unsigned int flags;
110 unsigned int width, height;
111 unsigned int charcount;
112 unsigned char *data;
113 };
114
115 #define KD_FONT_OP_SET 0
116 #define KD_FONT_OP_GET 1
117 #define KD_FONT_OP_SET_DEFAULT 2
118 #define KD_FONT_OP_COPY 3
119
120 #define KD_FONT_FLAG_DONT_RECALC 1
121 #endif
122
123 /********************************************/
124
125 enum
126 {
127 GLY_CRAP=0,
128 GLY_BLANK,
129 GLY_FULL,
130 GLY_TOP,
131 GLY_BOTTOM,
132 GLY_LEFT,
133 GLY_RIGHT,
134 };
135
136 int tty_linux_setfont(char*fontd,int w,int h)
137 {
138 struct console_font_op fontop;
139 fontop.op=KD_FONT_OP_SET;
140 fontop.flags=0;
141 fontop.data=fontd;
142 fontop.charcount=256;
143 fontop.width=w;
144 fontop.height=h;
145
146 return ioctl(2,KDFONTOP,&fontop);
147 }
148
149 int tty_linux_setfont_tab()
150 {
151 char*font=malloc(pwp_linuxcon.fontw*32*sizeof(char));
152 char*f=font,
153 *s=pwp_linuxcon.fontd_tab;
154
155 int i,x,y;
156
157 for(i=0;i<256;i++)
158 for(y=0;y<32;y++)
159 {
160 int b=0,x1=(pwp_linuxcon.fontw+7)&~7;
161 for(x=0;x<x1;x++)
162 {
163 int pix;
164
165 if(y<pwp_linuxcon.fonth &&
166 x<pwp_linuxcon.fontw)
167 pix=*s++?1:0;else pix=0;
168
169 b<<=1;
170 b|=pix;
171
172 if((x&7)==7)
173 {
174 *f++=b;
175 b=0;
176 }
177 }
178 }
179
180 return tty_linux_setfont(font,pwp_linuxcon.fontw,pwp_linuxcon.fonth);
181 }
182
183 void tty_linux_restorefont()
184 {
185 tty_linux_setfont(
186 pwp_linuxcon.fontd,
187 pwp_linuxcon.fontw,
188 pwp_linuxcon.fonth);
189 }
190
191 int tty_linux_fixfont()
192 {
193 #if 0
194 char newfont[pwp_linuxcon.fontw*
195 pwp_linuxcon.fonth*sizeof(char)];
196
197 glyphgen_fixtoibm(
198 pwp_linuxcon.fontd_tab,
199 pwp_linuxcon.fontw,
200 pwp_linuxcon.fonth);
201
202 return tty_linux_setfont_tab();
203 #endif
204
205 return 0;
206 }
207
208 /**********************************/
209
210 int tty_linux_analyzefont()
211 {
212 u8 analysis[256];
213
214 u8*s=pwp_linuxcon.fontd;
215 char*d=pwp_linuxcon.fontd_tab;
216 int c=0;
217
218 /* fprintf(stderr,"font analysis?\n");*/
219
220 for(;c<256;c++)
221 {
222 int x0=33,x1=-1,y0=33,y1=-1,x,y;
223 int dens=0;
224
225 for(y=0;y<32;y++)
226 {
227 int byte;
228 for(x=0;x<pwp_linuxcon.fontw;x++)
229 {
230 if(!(x&7))byte=*s++;
231
232 /* if(y<pwp_linuxcon.fonth)*d++=byte&128;*/
233
234 if(byte&128)
235 {
236 if(x<x0)x0=x;
237 if(x>x1)x1=x+1;
238 if(y<y0)y0=y;
239 if(y>y1)y1=y+1;
240 dens++;
241 }
242 byte<<=1;
243 }
244 }
245
246 {int a=GLY_CRAP;
247 int w=(x1-x0),h=(y1-y0);
248
249 if(h<=0)a=GLY_BLANK;
250 else
251 {
252 if(w>=pwp_linuxcon.fontw-1)
253 {
254 if(h>=pwp_linuxcon.fontw-1)a=GLY_FULL;
255 else
256 if(y1>=pwp_linuxcon.fontw-2)a=GLY_BOTTOM;
257 else
258 if(!y0)a=GLY_TOP;
259 }
260 else
261 if(h>=pwp_linuxcon.fonth-1)
262 {
263 if(x1>=pwp_linuxcon.fonth-2)a=GLY_RIGHT;
264 else
265 if(!x0)a=GLY_LEFT;
266 }
267 }
268 analysis[c]=a;
269 /* if(a)fprintf(stderr,"char %d: %d\n",c,a);*/
270 }
271 }
272
273 if(analysis[176]==GLY_FULL &&
274 analysis[177]==GLY_FULL &&
275 analysis[178]==GLY_FULL &&
276 analysis[219]==GLY_FULL &&
277 analysis[220]==GLY_BOTTOM &&
278 analysis[223]==GLY_TOP) return 2;
279
280 /* fprintf(stderr,"done\n");*/
281
282 return 1;
283 }
284
285 int tty_linux_getfont()
286 {
287 {
288 struct console_font_op fontop;
289 fontop.op=KD_FONT_OP_GET;
290 fontop.flags=0;
291 fontop.data=NULL;
292 fontop.charcount=0;
293
294 /* if handle 2 doesn't work, try handles 1 and 0?
295 also with vcsa */
296
297 /* use this bitreverter somewhere (X11-1bpp code?) not here.
298 replaces lookups in fast machines.
299
300 a=((a&0xf0f0f0f0)>>4)|((a&0x0f0f0f0f)<<4);
301 a=((a&0xcccccccc)>>2)|((a&0x33333333)<<2);
302 a=((a&0xaaaaaaaa)>>1)|((a&0x55555555)<<1);
303 */
304
305 ioctl(2,KDFONTOP,&fontop);
306 if(fontop.charcount)
307 {
308 pwp_linuxcon.fontw=fontop.width;
309 pwp_linuxcon.fonth=fontop.height;
310
311 if(!fontop.data)
312 {
313 fontop.data=malloc(((pwp_linuxcon.fontw+7)>>3)*32*
314 fontop.charcount*sizeof(u8));
315 fontop.op=KD_FONT_OP_GET;
316 fontop.flags=KD_FONT_FLAG_DONT_RECALC;
317 if(ioctl(2,KDFONTOP,&fontop)<0)
318 goto oldcall;
319 }
320
321 pwp_linuxcon.fontd=fontop.data;
322 goto gotfont;
323 }
324 }
325
326 oldcall:
327 {
328 struct consolefontdesc fontx;
329 fontx.chardata=malloc(512*32*sizeof(u8));
330
331 if(ioctl(2,GIO_FONTX,&fontx)<0)return 0;
332
333 pwp_linuxcon.fontw=8;
334 pwp_linuxcon.fonth=fontx.charheight;
335 pwp_linuxcon.fontd=fontx.chardata;
336 }
337
338 gotfont:
339
340 pwp_linuxcon.fontd_tab=
341 malloc(pwp_linuxcon.fontw*pwp_linuxcon.fonth*sizeof(char));
342
343 return tty_linux_analyzefont();
344 }
345
346 /********************************************************/
347
348 extern void tty_ansicol_dump_attr();
349 extern void tty_ansicol_prep_attr();
350
351 int tty_linux_initstring()
352 {
353 write(pwp_tty.fd,"\33[11m",5*sizeof(char)); /* select ibmpc charset */
354 tty_vt_initstring();
355 }
356
357 int tty_linux_init()
358 {
359 if(!vcsa_init())
360 {
361 pwplib.dump_attr=tty_ansicol_dump_attr;
362 pwplib.prep_attr=tty_ansicol_prep_attr;
363 pwpwrite("* no vcsa access - using tty\n");
364 /* send init */
365 }
366
367 {int i=tty_linux_getfont();
368
369 pwpwrite("* font analysis: ");
370
371 if(i==0)pwpwrite("can't access - assuming ibm\n");else
372 if(i==1)pwpwrite("no ibm rasters - using ascii\n");else
373 if(i==2)pwpwrite("ibm rasters found - using ibm\n");
374
375 if(i==0 || i==2) conv_init(2,0,0);
376 else conv_init(2,2,1);
377 }
378
379 tty_linux_initstring();
380 }
381
382 #endif
383
384 /*
385 additional stuff:
386
387 - font customization:
388 - save font
389 - if evil: if font bigger than 8x8, change to 8x8
390 - include rasterchars (64 chars as in vt220 driver)
391 ...
392 - restore old font
393 */