]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pcvt/loadfont/loadfont.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / usr.sbin / pcvt / loadfont / loadfont.c
1 /*
2  * Copyright (c) 1992, 1995 Hellmuth Michaelis
3  *
4  * Copyright (c) 1992, 1994 Brian Dunford-Shore
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by
19  *      Hellmuth Michaelis and Brian Dunford-Shore
20  * 4. The name authors may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 /*---------------------------------------------------------------------------*
36  *
37  *      load a font into VGA character font memory
38  *      ------------------------------------------
39  *
40  *      Last Edit-Date: [Mon Mar 27 17:09:25 2000];
41  *
42  * $FreeBSD$
43  *
44  *---------------------------------------------------------------------------*/
45
46 #include <stdio.h>
47 #include <err.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <machine/pcvt_ioctl.h>
53
54 #define FONT8X8         2048    /* filesize for 8x8 font              */
55 #define HEIGHT8X8       8       /* 8 scan lines char cell height      */
56 #define SSCAN8X8        143     /* 400 scan lines on screen - 256 - 1 */
57
58 #define FONT8X10        2560    /* filesize for 8x10 font             */
59 #define HEIGHT8X10      10      /* 10 scan lines char cell height     */
60 #define SSCAN8X10       143     /* 400 scan lines on screen - 256 - 1 */
61
62 #define FONT8X14        3584    /* filesize for 8x14 font             */
63 #define HEIGHT8X14      14      /* 14 scan lines char cell height     */
64 #define SSCAN8X14       135     /* 392 scan lines on screen - 256 - 1 */
65
66 #define FONT8X16        4096    /* filesize for 8x16 font             */
67 #define HEIGHT8X16      16      /* 16 scan lines char cell height     */
68 #define SSCAN8X16       143     /* 400 scan lines on screen - 256 - 1 */
69
70 struct screeninfo screeninfo;
71
72 #define DEFAULTFD 0
73 int fd;
74
75
76 main(argc,argv)
77 int argc;
78 char *argv[];
79 {
80         FILE *in;
81         struct stat sbuf, *sbp;
82         unsigned char *fonttab;
83         int ret;
84         int chr_height;
85         int scr_scan;
86         int scr_rows;
87         int c;
88         int chr_set = -1;
89         char *filename;
90         int fflag = -1;
91         int info = -1;
92         int dflag = 0;
93         char *device;
94
95         while( (c = getopt(argc, argv, "c:d:f:i")) != -1)
96         {
97                 switch(c)
98                 {
99                         case 'c':
100                                 chr_set = atoi(optarg);
101                                 break;
102
103                         case 'd':
104                                 device = optarg;
105                                 dflag = 1;
106                                 break;
107
108                         case 'f':
109                                 filename = optarg;
110                                 fflag = 1;
111                                 break;
112
113                         case 'i':
114                                 info = 1;
115                                 break;
116
117                         case '?':
118                         default:
119                                 usage();
120                                 break;
121                 }
122         }
123
124         if(chr_set == -1 || fflag == -1)
125                 info = 1;
126
127         if(dflag)
128         {
129                 if((fd = open(device, O_RDWR)) == -1)
130                         err(1, "ERROR opening %s", device);
131         }
132         else
133         {
134                 fd = DEFAULTFD;
135         }
136
137         if(info == 1)
138         {
139                 int i;
140
141                 if(ioctl(fd, VGAGETSCREEN, &screeninfo) == -1)
142                         err(1, "ioctl VGAGETSCREEN failed");
143
144                 switch(screeninfo.adaptor_type)
145                 {
146                   case UNKNOWN_ADAPTOR:
147                   case MDA_ADAPTOR:
148                   case CGA_ADAPTOR:
149                     printf("Adaptor does not support Downloadable Fonts!\n");
150                     break;
151                   case EGA_ADAPTOR:
152                     printheader();
153                     for(i = 0;i < 4;i++)
154                     {
155                         printvgafontattr(i);
156                     }
157                     break;
158                   case VGA_ADAPTOR:
159                     printheader();
160                     for(i = 0;i < 8;i++)
161                     {
162                         printvgafontattr(i);
163                     }
164                 }
165                 printf("\n");
166                 exit(0);
167         }
168
169         if(chr_set < 0 || chr_set > 7)
170                 usage();
171
172         sbp = &sbuf;
173
174         if((in = fopen(filename, "r")) == NULL)
175                 err(1, "cannot open file %s for reading", filename);
176
177         if((fstat(fileno(in), sbp)) != 0)
178                 err(1, "cannot fstat file %s", filename);
179
180         switch(sbp->st_size)
181         {
182                 case FONT8X8:
183                         chr_height = HEIGHT8X8;
184                         scr_scan = SSCAN8X8;
185                         scr_rows = SIZ_50ROWS;
186                         break;
187
188                 case FONT8X10:
189                         chr_height = HEIGHT8X10;
190                         scr_scan = SSCAN8X10;
191                         scr_rows = SIZ_40ROWS;
192                         break;
193
194                 case FONT8X14:
195                         chr_height = HEIGHT8X14;
196                         scr_scan = SSCAN8X14;
197                         scr_rows = SIZ_28ROWS;
198                         break;
199
200                 case FONT8X16:
201                         chr_height = HEIGHT8X16;
202                         scr_scan = SSCAN8X16;
203                         scr_rows = SIZ_25ROWS;
204                         break;
205
206                 default:
207                         errx(1, "error, file %s is no valid font file, size=%d", argv[1], sbp->st_size);
208         }
209
210         if((fonttab = (unsigned char *)malloc((size_t)sbp->st_size)) == NULL)
211                 errx(1, "error, malloc failed");
212
213         if((ret = fread(fonttab, sizeof(*fonttab), sbp->st_size, in)) != sbp->st_size)
214                 errx(1, "error reading file %s, size = %d, read = is no valid font file, size=%d",
215                          argv[1], sbp->st_size, ret);
216
217         loadfont(chr_set, chr_height, fonttab);
218         setfont(chr_set, 1, chr_height - 1, scr_scan, scr_rows);
219
220         exit(0);
221 }
222
223 setfont(charset, fontloaded, charscan, scrscan, scrrow)
224 int charset, fontloaded, charscan, scrscan, scrrow;
225 {
226         struct vgafontattr vfattr;
227
228         vfattr.character_set = charset;
229         vfattr.font_loaded = fontloaded;
230         vfattr.character_scanlines = charscan;
231         vfattr.screen_scanlines = scrscan;
232         vfattr.screen_size = scrrow;
233
234         if(ioctl(fd, VGASETFONTATTR, &vfattr) == -1)
235                 err(1, "ioctl VGASETFONTATTR failed, error");
236 }
237
238 loadfont(fontset,charscanlines,font_table)
239 int fontset;
240 int charscanlines;
241 unsigned char *font_table;
242 {
243         int i, j;
244         struct vgaloadchar vlc;
245
246         vlc.character_set = fontset;
247         vlc.character_scanlines = charscanlines;
248
249         for(i = 0; i < 256; i++)
250         {
251                 vlc.character = i;
252                 for (j = 0; j < charscanlines; j++)
253                 {
254                         vlc.char_table[j] = font_table[j];
255                 }
256                 font_table += charscanlines;
257                 if(ioctl(fd, VGALOADCHAR, &vlc) == -1)
258                         err(1, "ioctl VGALOADCHAR failed, error");
259         }
260 }
261
262 printvgafontattr(charset)
263 int charset;
264 {
265         struct vgafontattr vfattr;
266         static int sizetab[] = { 25, 28, 35, 40, 43, 50 };
267
268         vfattr.character_set = charset;
269
270         if(ioctl(fd, VGAGETFONTATTR, &vfattr) == -1)
271                 err(1, "ioctl VGAGETFONTATTR failed, error");
272         printf(" %d  ",charset);
273         if(vfattr.font_loaded)
274         {
275
276                 printf("Loaded ");
277                 printf(" %2.2d       ", sizetab[vfattr.screen_size]);
278                 printf(" %2.2d           ",
279                        (((int)vfattr.character_scanlines) & 0x1f) + 1);
280                 printf(" %3.3d",
281                        ((int)vfattr.screen_scanlines+0x101));
282         }
283         else
284         {
285                 printf("Empty");
286         }
287         printf("\n");
288 }
289
290 printheader()
291 {
292         printf("\nEGA/VGA Charactersets Status Info:\n\n");
293         printf("Set Status Lines CharScanLines ScreenScanLines\n");
294         printf("--- ------ ----- ------------- ---------------\n");
295 }
296
297 usage()
298 {
299         fprintf(stderr,"\nloadfont - load font into ega/vga font ram for pcvt video driver\n");
300         fprintf(stderr,"usage: loadfont -c <cset> -d <dev> -f <name> -i\n");
301         fprintf(stderr,"       -c <cset> characterset to load (ega 0..3, vga 0..7)\n");
302         fprintf(stderr,"       -d <dev>  specify device\n");
303         fprintf(stderr,"       -f <name> filename containing binary font data\n");
304         fprintf(stderr,"       -i        print status and types of loaded fonts (default)\n");
305         exit(1);
306 }