Home | History | Annotate | Line # | Download | only in wsfontload
wsfontload.c revision 1.3
      1 /* $NetBSD: wsfontload.c,v 1.3 2000/01/25 01:04:07 ad Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1999
      5  *	Matthias Drochner.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project
     18  *	by Matthias Drochner.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include <stdio.h>
     36 #include <fcntl.h>
     37 #include <unistd.h>
     38 #include <sys/types.h>
     39 #include <sys/ioctl.h>
     40 #include <err.h>
     41 #include <malloc.h>
     42 
     43 #include <dev/wscons/wsconsio.h>
     44 
     45 #define DEFDEV		"/dev/ttyEcfg"
     46 #define DEFWIDTH	8
     47 #define DEFHEIGHT	16
     48 #define DEFENC		WSDISPLAY_FONTENC_ISO
     49 #define DEFBITORDER	WSDISPLAY_FONTORDER_L2R
     50 #define DEFBYTEORDER	WSDISPLAY_FONTORDER_L2R
     51 
     52 int main __P((int, char**));
     53 static void usage __P((void));
     54 static int getencoding __P((char *));
     55 
     56 static void
     57 usage()
     58 {
     59 	extern char *__progname;
     60 
     61 	(void)fprintf(stderr,
     62 		"Usage: %s [-f wsdev] [-w width] [-h height] [-e encoding]"
     63 		" [-N name] [-b] [-B] [fontfile]\n",
     64 		      __progname);
     65 	exit(1);
     66 }
     67 
     68 int
     69 main(argc, argv)
     70 	int argc;
     71 	char **argv;
     72 {
     73 	char *wsdev;
     74 	struct wsdisplay_font f;
     75 	int c, res, wsfd, ffd;
     76 	size_t len;
     77 	void *buf;
     78 
     79 	wsdev = DEFDEV;
     80 	f.fontwidth = DEFWIDTH;
     81 	f.fontheight = DEFHEIGHT;
     82 	f.firstchar = 0;
     83 	f.numchars = 256;
     84 	f.stride = 0;
     85 	f.encoding = DEFENC;
     86 	f.name = 0;
     87 	f.bitorder = DEFBITORDER;
     88 	f.byteorder = DEFBYTEORDER;
     89 
     90 	while ((c = getopt(argc, argv, "f:w:h:e:N:bB")) != -1) {
     91 		switch (c) {
     92 		case 'f':
     93 			wsdev = optarg;
     94 			break;
     95 		case 'w':
     96 			if (sscanf(optarg, "%d", &f.fontwidth) != 1)
     97 				errx(1, "invalid font width");
     98 			break;
     99 		case 'h':
    100 			if (sscanf(optarg, "%d", &f.fontheight) != 1)
    101 				errx(1, "invalid font height");
    102 			break;
    103 		case 'e':
    104 			f.encoding = getencoding(optarg);
    105 			break;
    106 		case 'N':
    107 			f.name = optarg;
    108 			break;
    109 		case 'b':
    110 			f.bitorder = WSDISPLAY_FONTORDER_R2L;
    111 			break;
    112 		case 'B':
    113 			f.byteorder = WSDISPLAY_FONTORDER_R2L;
    114 			break;
    115 		case '?':
    116 		default:
    117 			usage();
    118 			break;
    119 		}
    120 	}
    121 	argc -= optind;
    122 	argv += optind;
    123 
    124 	if (argc > 1)
    125 		usage();
    126 
    127 	wsfd = open(wsdev, O_RDWR, 0);
    128 	if (wsfd < 0)
    129 		err(2, "open ws");
    130 
    131 	if (argc > 0) {
    132 		ffd = open(argv[0], O_RDONLY, 0);
    133 		if (ffd < 0)
    134 			err(4, "open font");
    135 		if (!f.name)
    136 			f.name = argv[0];
    137 	} else
    138 		ffd = 0;
    139 
    140 	if (!f.stride)
    141 		f.stride = (f.fontwidth + 7) / 8;
    142 	len = f.fontheight * f.numchars * f.stride;
    143 	if (!len)
    144 		errx(1, "invalid font size");
    145 
    146 	buf = malloc(len);
    147 	if (!buf)
    148 		errx(1, "malloc");
    149 	res = read(ffd, buf, len);
    150 	if (res < 0)
    151 		err(4, "read font");
    152 	if (res != len)
    153 		errx(4, "short read");
    154 
    155 	f.data = buf;
    156 
    157 	res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f);
    158 	if (res < 0)
    159 		err(3, "WSDISPLAYIO_LDFONT");
    160 
    161 	return (0);
    162 }
    163 
    164 static struct {
    165 	char *name;
    166 	int val;
    167 } encodings[] = {
    168 	{"iso", WSDISPLAY_FONTENC_ISO},
    169 	{"ibm", WSDISPLAY_FONTENC_IBM},
    170 	{"pcvt", WSDISPLAY_FONTENC_PCVT},
    171 };
    172 
    173 static int
    174 getencoding(name)
    175 	char *name;
    176 {
    177 	int i;
    178 
    179 	for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
    180 		if (!strcmp(name, encodings[i].name))
    181 			return (encodings[i].val);
    182 
    183 	if (sscanf(name, "%d", &i) != 1)
    184 		errx(1, "invalid encoding");
    185 	return (i);
    186 }
    187