xref: /minix3/minix/commands/loadfont/loadfont.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1 /* loadfont.c - Load custom font into EGA, VGA video card
2  *
3  * Author: Hrvoje Stipetic (hs@hck.hr) Jun-1995.
4  *
5  */
6 
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <termios.h>
14 #include <sys/ioctl.h>
15 
tell(char * s)16 void tell(char *s)
17 {
18   write(2, s, strlen(s));
19 }
20 
itoa(unsigned i)21 char *itoa(unsigned i)
22 {
23   static char a[3*sizeof(int)];
24   char *p = a+sizeof(a)-1;
25 
26   do {
27 	*--p = '0' + i%10;
28   } while ((i /= 10) > 0);
29 
30   return p;
31 }
32 
report(char * say)33 void report(char *say)
34 {
35   int err = errno;
36   tell("loadfont: ");
37   if (say != NULL) {
38 	tell(say);
39 	tell(": ");
40   }
41   tell(strerror(err));
42   tell("\n");
43 }
44 
usage(void)45 void usage(void)
46 {
47   tell("Usage: loadfont fontfile\n");
48   exit(1);
49 }
50 
51 
main(int argc,char * argv[])52 int main(int argc, char *argv[])
53 {
54   static u8_t font[256][32];
55   static u8_t font_file[256 * (16+14+8) + 1];
56   u8_t *ff;
57   int fd, size, tsize, ch, ln;
58   struct winsize ws;
59 
60 
61   if (argc != 2)
62 	usage();
63 
64   if ((fd = open(argv[1], O_RDONLY)) < 0) {
65 	report(argv[1]);
66 	exit(1);
67   }
68 
69   switch (read(fd, font_file, sizeof(font_file))) {
70   case 256 * 8:
71   	size = 8;
72   	break;
73   case 256 * 14:
74   	size = 14;
75   	break;
76   case 256 * 16:
77   	size = 16;
78   	break;
79   case 256 * (16+14+8):
80   	size = 0;
81   	break;
82   case -1:
83 	report(argv[1]);
84 	exit(1);
85   default:
86 	tell("loadfont: ");
87 	tell(argv[1]);
88 	tell(": fontfile is not an 8x8, 8x14, 8x16, or compound font\n");
89 	exit(1);
90   }
91   close(fd);
92 
93   if (ioctl(0, TIOCGWINSZ, &ws) < 0 || (errno= ENOTTY, ws.ws_row == 0)) {
94 	report(NULL);
95 	exit(1);
96   }
97   tsize = ws.ws_ypixel / ws.ws_row;
98 
99   if (size == 0) {
100 	if (tsize >= 16) {
101 		ff = font_file + 256 * (0);
102 	} else
103 	if (tsize >= 14) {
104 		ff = font_file + 256 * (16);
105 	} else {
106 		ff = font_file + 256 * (16 + 14);
107 	}
108 	size = tsize;
109   } else {
110 	ff = font_file;
111   }
112 
113   for (ch = 0; ch < 256; ch++) {
114 	for (ln = 0; ln < size; ln++) font[ch][ln] = ff[ch * size + ln];
115   }
116 
117   if (ioctl(0, TIOCSFON, font) < 0) {
118 	report(NULL);
119 	exit(1);
120   }
121   exit(0);
122 }
123