Home | History | Annotate | Line # | Download | only in wsfontload
wsfontload.c revision 1.1
      1 /* $NetBSD: wsfontload.c,v 1.1 1999/01/13 19:02:34 drochner 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 
     50 int main __P((int, char**));
     51 static void usage __P((void));
     52 static int getencoding __P((char *));
     53 
     54 static void
     55 usage()
     56 {
     57 	extern char *__progname;
     58 
     59 	(void)fprintf(stderr,
     60 		"Usage: %s [-f wsdev] [-w width] [-h height] [-e encoding]"
     61 		" [-N name] [fontfile]\n",
     62 		      __progname);
     63 	exit(1);
     64 }
     65 
     66 int
     67 main(argc, argv)
     68 	int argc;
     69 	char **argv;
     70 {
     71 	char *wsdev;
     72 	struct wsdisplay_font f;
     73 	int c, res, wsfd, ffd;
     74 	size_t len;
     75 	void *buf;
     76 
     77 	wsdev = DEFDEV;
     78 	f.fontwidth = DEFWIDTH;
     79 	f.fontheight = DEFHEIGHT;
     80 	f.firstchar = 0;
     81 	f.numchars = 256;
     82 	f.stride = 0;
     83 	f.encoding = DEFENC;
     84 	f.name = 0;
     85 
     86 	while ((c = getopt(argc, argv, "f:w:h:e:N:")) != -1) {
     87 		switch (c) {
     88 		case 'f':
     89 			wsdev = optarg;
     90 			break;
     91 		case 'w':
     92 			if (sscanf(optarg, "%d", &f.fontwidth) != 1)
     93 				errx(1, "invalid font width");
     94 			break;
     95 		case 'h':
     96 			if (sscanf(optarg, "%d", &f.fontheight) != 1)
     97 				errx(1, "invalid font height");
     98 			break;
     99 		case 'e':
    100 			f.encoding = getencoding(optarg);
    101 			break;
    102 		case 'N':
    103 			f.name = optarg;
    104 			break;
    105 		case '?':
    106 		default:
    107 			usage();
    108 			break;
    109 		}
    110 	}
    111 	argc -= optind;
    112 	argv += optind;
    113 
    114 	if (argc > 1)
    115 		usage();
    116 
    117 	wsfd = open(wsdev, O_RDWR, 0);
    118 	if (wsfd < 0)
    119 		err(2, "open ws");
    120 
    121 	if (argc > 0) {
    122 		ffd = open(argv[0], O_RDONLY, 0);
    123 		if (ffd < 0)
    124 			err(4, "open font");
    125 		if (!f.name)
    126 			f.name = argv[0];
    127 	} else
    128 		ffd = 0;
    129 
    130 	if (!f.stride)
    131 		f.stride = (f.fontwidth + 7) / 8;
    132 	len = f.fontheight * f.numchars * f.stride;
    133 	if (!len)
    134 		errx(1, "invalid font size");
    135 
    136 	buf = malloc(len);
    137 	if (!buf)
    138 		errx(1, "malloc");
    139 	res = read(ffd, buf, len);
    140 	if (res < 0)
    141 		err(4, "read font");
    142 	if (res != len)
    143 		errx(4, "short read");
    144 
    145 	f.data = buf;
    146 
    147 	res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f);
    148 	if (res < 0)
    149 		err(3, "WSDISPLAYIO_LDFONT");
    150 
    151 	return (0);
    152 }
    153 
    154 static struct {
    155 	char *name;
    156 	int val;
    157 } encodings[] = {
    158 	{"iso", WSDISPLAY_FONTENC_ISO},
    159 	{"ibm", WSDISPLAY_FONTENC_IBM},
    160 	{"pcvt", WSDISPLAY_FONTENC_PCVT},
    161 };
    162 
    163 static int
    164 getencoding(name)
    165 	char *name;
    166 {
    167 	int i;
    168 
    169 	for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++)
    170 		if (!strcmp(name, encodings[i].name))
    171 			return (encodings[i].val);
    172 
    173 	if (sscanf(name, "%d", &i) != 1)
    174 		errx(1, "invalid encoding");
    175 	return (i);
    176 }
    177