Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.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  * $FreeBSD: src/usr.sbin/pcvt/loadfont/loadfont.c,v 1.6.6.1 2001/05/12 10:11:42 kris Exp $
35  * $DragonFly: src/usr.sbin/pcvt/loadfont/Attic/loadfont.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
36  */
37
38 static char *id =
39         "@(#)loadfont.c, 3.20, Last Edit-Date: [Fri Apr  7 10:13:16 1995]";
40
41 /*---------------------------------------------------------------------------*
42  *
43  *      load a font into vga character font memory
44  *
45  *      -hm     removing explicit HGC support (same as MDA ..)
46  *      -hm     new pcvt_ioctl.h SIZ_xxROWS
47  *      -hm     add -d option
48  *
49  *---------------------------------------------------------------------------*/
50
51 #include <stdio.h>
52 #include <err.h>
53 #include <fcntl.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <machine/pcvt_ioctl.h>
57
58 #define FONT8X8         2048    /* filesize for 8x8 font              */
59 #define HEIGHT8X8       8       /* 8 scan lines char cell height      */
60 #define SSCAN8X8        143     /* 400 scan lines on screen - 256 - 1 */
61
62 #define FONT8X10        2560    /* filesize for 8x10 font             */
63 #define HEIGHT8X10      10      /* 10 scan lines char cell height     */
64 #define SSCAN8X10       143     /* 400 scan lines on screen - 256 - 1 */
65
66 #define FONT8X14        3584    /* filesize for 8x14 font             */
67 #define HEIGHT8X14      14      /* 14 scan lines char cell height     */
68 #define SSCAN8X14       135     /* 392 scan lines on screen - 256 - 1 */
69
70 #define FONT8X16        4096    /* filesize for 8x16 font             */
71 #define HEIGHT8X16      16      /* 16 scan lines char cell height     */
72 #define SSCAN8X16       143     /* 400 scan lines on screen - 256 - 1 */
73
74 struct screeninfo screeninfo;
75
76 #define DEFAULTFD 0
77 int fd;
78
79
80 main(argc,argv)
81 int argc;
82 char *argv[];
83 {
84         extern int optind;
85         extern int opterr;
86         extern char *optarg;
87
88         FILE *in;
89         struct stat sbuf, *sbp;
90         unsigned char *fonttab;
91         int ret;
92         int chr_height;
93         int scr_scan;
94         int scr_rows;
95         int c;
96         int chr_set = -1;
97         char *filename;
98         int fflag = -1;
99         int info = -1;
100         int dflag = 0;
101         char *device;
102
103         while( (c = getopt(argc, argv, "c:d:f:i")) != -1)
104         {
105                 switch(c)
106                 {
107                         case 'c':
108                                 chr_set = atoi(optarg);
109                                 break;
110
111                         case 'd':
112                                 device = optarg;
113                                 dflag = 1;
114                                 break;
115
116                         case 'f':
117                                 filename = optarg;
118                                 fflag = 1;
119                                 break;
120
121                         case 'i':
122                                 info = 1;
123                                 break;
124
125                         case '?':
126                         default:
127                                 usage();
128                                 break;
129                 }
130         }
131
132         if(chr_set == -1 || fflag == -1)
133                 info = 1;
134
135         if(dflag)
136         {
137                 if((fd = open(device, O_RDWR)) == -1)
138                         err(1, "ERROR opening %s", device);
139         }
140         else
141         {
142                 fd = DEFAULTFD;
143         }
144
145         if(info == 1)
146         {
147                 int i;
148
149                 if(ioctl(fd, VGAGETSCREEN, &screeninfo) == -1)
150                         err(1, "ioctl VGAGETSCREEN failed");
151
152                 switch(screeninfo.adaptor_type)
153                 {
154                   case UNKNOWN_ADAPTOR:
155                   case MDA_ADAPTOR:
156                   case CGA_ADAPTOR:
157                     printf("Adaptor does not support Downloadable Fonts!\n");
158                     break;
159                   case EGA_ADAPTOR:
160                     printheader();
161                     for(i = 0;i < 4;i++)
162                     {
163                         printvgafontattr(i);
164                     }
165                     break;
166                   case VGA_ADAPTOR:
167                     printheader();
168                     for(i = 0;i < 8;i++)
169                     {
170                         printvgafontattr(i);
171                     }
172                 }
173                 printf("\n");
174                 exit(0);
175         }
176
177         if(chr_set < 0 || chr_set > 7)
178                 usage();
179
180         sbp = &sbuf;
181
182         if((in = fopen(filename, "r")) == NULL)
183                 err(1, "cannot open file %s for reading", filename);
184
185         if((fstat(fileno(in), sbp)) != 0)
186                 err(1, "cannot fstat file %s", filename);
187
188         switch(sbp->st_size)
189         {
190                 case FONT8X8:
191                         chr_height = HEIGHT8X8;
192                         scr_scan = SSCAN8X8;
193                         scr_rows = SIZ_50ROWS;
194                         break;
195
196                 case FONT8X10:
197                         chr_height = HEIGHT8X10;
198                         scr_scan = SSCAN8X10;
199                         scr_rows = SIZ_40ROWS;
200                         break;
201
202                 case FONT8X14:
203                         chr_height = HEIGHT8X14;
204                         scr_scan = SSCAN8X14;
205                         scr_rows = SIZ_28ROWS;
206                         break;
207
208                 case FONT8X16:
209                         chr_height = HEIGHT8X16;
210                         scr_scan = SSCAN8X16;
211                         scr_rows = SIZ_25ROWS;
212                         break;
213
214                 default:
215                         errx(1, "error, file %s is no valid font file, size=%d", argv[1], sbp->st_size);
216         }
217
218         if((fonttab = (unsigned char *)malloc((size_t)sbp->st_size)) == NULL)
219                 errx(1, "error, malloc failed");
220
221         if((ret = fread(fonttab, sizeof(*fonttab), sbp->st_size, in)) != sbp->st_size)
222                 errx(1, "error reading file %s, size = %d, read = is no valid font file, size=%d",
223                          argv[1], sbp->st_size, ret);
224
225         loadfont(chr_set, chr_height, fonttab);
226         setfont(chr_set, 1, chr_height - 1, scr_scan, scr_rows);
227
228         exit(0);
229 }
230
231 setfont(charset, fontloaded, charscan, scrscan, scrrow)
232 int charset, fontloaded, charscan, scrscan, scrrow;
233 {
234         struct vgafontattr vfattr;
235
236         vfattr.character_set = charset;
237         vfattr.font_loaded = fontloaded;
238         vfattr.character_scanlines = charscan;
239         vfattr.screen_scanlines = scrscan;
240         vfattr.screen_size = scrrow;
241
242         if(ioctl(fd, VGASETFONTATTR, &vfattr) == -1)
243                 err(1, "ioctl VGASETFONTATTR failed, error");
244 }
245
246 loadfont(fontset,charscanlines,font_table)
247 int fontset;
248 int charscanlines;
249 unsigned char *font_table;
250 {
251         int i, j;
252         struct vgaloadchar vlc;
253
254         vlc.character_set = fontset;
255         vlc.character_scanlines = charscanlines;
256
257         for(i = 0; i < 256; i++)
258         {
259                 vlc.character = i;
260                 for (j = 0; j < charscanlines; j++)
261                 {
262                         vlc.char_table[j] = font_table[j];
263                 }
264                 font_table += charscanlines;
265                 if(ioctl(fd, VGALOADCHAR, &vlc) == -1)
266                         err(1, "ioctl VGALOADCHAR failed, error");
267         }
268 }
269
270 printvgafontattr(charset)
271 int charset;
272 {
273         struct vgafontattr vfattr;
274         static int sizetab[] = { 25, 28, 35, 40, 43, 50 };
275
276         vfattr.character_set = charset;
277
278         if(ioctl(fd, VGAGETFONTATTR, &vfattr) == -1)
279                 err(1, "ioctl VGAGETFONTATTR failed, error");
280         printf(" %d  ",charset);
281         if(vfattr.font_loaded)
282         {
283
284                 printf("Loaded ");
285                 printf(" %2.2d       ", sizetab[vfattr.screen_size]);
286                 printf(" %2.2d           ",
287                        (((int)vfattr.character_scanlines) & 0x1f) + 1);
288                 printf(" %3.3d",
289                        ((int)vfattr.screen_scanlines+0x101));
290         }
291         else
292         {
293                 printf("Empty");
294         }
295         printf("\n");
296 }
297
298 printheader()
299 {
300         printf("\nEGA/VGA Charactersets Status Info:\n\n");
301         printf("Set Status Lines CharScanLines ScreenScanLines\n");
302         printf("--- ------ ----- ------------- ---------------\n");
303 }
304
305 usage()
306 {
307         fprintf(stderr,"\nloadfont - load font into ega/vga font ram for pcvt video driver\n");
308         fprintf(stderr,"usage: loadfont -c <cset> -d <dev> -f <name> -i\n");
309         fprintf(stderr,"       -c <cset> characterset to load (ega 0..3, vga 0..7)\n");
310         fprintf(stderr,"       -d <dev>  specify device\n");
311         fprintf(stderr,"       -f <name> filename containing binary font data\n");
312         fprintf(stderr,"       -i        print status and types of loaded fonts (default)\n");
313         exit(1);
314 }