1 /* $NetBSD: wsfontload.c,v 1.8 2001/10/29 17:58:19 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 <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 #include <sys/types.h> 41 #include <sys/ioctl.h> 42 #include <err.h> 43 #include <malloc.h> 44 45 #include <dev/wscons/wsconsio.h> 46 47 #define DEFDEV "/dev/wsfont" 48 #define DEFWIDTH 8 49 #define DEFHEIGHT 16 50 #define DEFENC WSDISPLAY_FONTENC_ISO 51 #define DEFBITORDER WSDISPLAY_FONTORDER_L2R 52 #define DEFBYTEORDER WSDISPLAY_FONTORDER_L2R 53 54 int main __P((int, char**)); 55 static void usage __P((void)); 56 static int getencoding __P((char *)); 57 static char *rgetencoding __P((int)); 58 static char *rgetfontorder __P((int)); 59 60 static struct { 61 char *name; 62 int val; 63 } fontorders[] = { 64 { "known", WSDISPLAY_FONTORDER_KNOWN}, 65 { "l2r", WSDISPLAY_FONTORDER_L2R}, 66 { "r2l", WSDISPLAY_FONTORDER_R2L}, 67 }; 68 69 static struct { 70 char *name; 71 int val; 72 } encodings[] = { 73 {"iso", WSDISPLAY_FONTENC_ISO}, 74 {"ibm", WSDISPLAY_FONTENC_IBM}, 75 {"pcvt", WSDISPLAY_FONTENC_PCVT}, 76 {"iso7", WSDISPLAY_FONTENC_ISO7}, 77 {"iso2", WSDISPLAY_FONTENC_ISO2}, 78 }; 79 80 static void 81 usage() 82 { 83 84 (void)fprintf(stderr, 85 "Usage: %s [-f wsdev] [-w width] [-h height] [-e encoding]" 86 " [-N name] [-b] [-B] [fontfile]\n", 87 getprogname()); 88 exit(1); 89 } 90 91 /* 92 * map given fontorder to it's string representation 93 */ 94 static char * 95 rgetfontorder(fontorder) 96 int fontorder; 97 { 98 int i; 99 100 for (i = 0; i < sizeof(fontorders) / sizeof(fontorders[0]); i++) 101 if (fontorders[i].val == fontorder) 102 return (fontorders[i].name); 103 104 return "unknown"; 105 } 106 107 /* 108 * map given encoding to it's string representation 109 */ 110 static char * 111 rgetencoding(enc) 112 int enc; 113 { 114 int i; 115 116 for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++) 117 if (encodings[i].val == enc) 118 return (encodings[i].name); 119 120 return "unknown"; 121 } 122 123 /* 124 * map given encoding string to integer value 125 */ 126 static int 127 getencoding(name) 128 char *name; 129 { 130 int i; 131 132 for (i = 0; i < sizeof(encodings) / sizeof(encodings[0]); i++) 133 if (!strcmp(name, encodings[i].name)) 134 return (encodings[i].val); 135 136 if (sscanf(name, "%d", &i) != 1) 137 errx(1, "invalid encoding"); 138 return (i); 139 } 140 141 int 142 main(argc, argv) 143 int argc; 144 char **argv; 145 { 146 char *wsdev; 147 struct wsdisplay_font f; 148 int c, res, wsfd, ffd, verbose = 0; 149 size_t len; 150 void *buf; 151 152 wsdev = DEFDEV; 153 f.fontwidth = DEFWIDTH; 154 f.fontheight = DEFHEIGHT; 155 f.firstchar = 0; 156 f.numchars = 256; 157 f.stride = 0; 158 f.encoding = DEFENC; 159 f.name = 0; 160 f.bitorder = DEFBITORDER; 161 f.byteorder = DEFBYTEORDER; 162 163 while ((c = getopt(argc, argv, "f:w:h:e:N:bB:v")) != -1) { 164 switch (c) { 165 case 'f': 166 wsdev = optarg; 167 break; 168 case 'w': 169 if (sscanf(optarg, "%d", &f.fontwidth) != 1) 170 errx(1, "invalid font width"); 171 break; 172 case 'h': 173 if (sscanf(optarg, "%d", &f.fontheight) != 1) 174 errx(1, "invalid font height"); 175 break; 176 case 'e': 177 f.encoding = getencoding(optarg); 178 break; 179 case 'N': 180 f.name = optarg; 181 break; 182 case 'b': 183 f.bitorder = WSDISPLAY_FONTORDER_R2L; 184 break; 185 case 'B': 186 f.byteorder = WSDISPLAY_FONTORDER_R2L; 187 break; 188 case 'v': 189 verbose = 1; 190 break; 191 case '?': 192 default: 193 usage(); 194 break; 195 } 196 } 197 argc -= optind; 198 argv += optind; 199 200 if (argc > 1) 201 usage(); 202 203 wsfd = open(wsdev, O_RDWR, 0); 204 if (wsfd < 0) 205 err(2, "open ws-device %s", wsdev); 206 207 if (argc > 0) { 208 ffd = open(argv[0], O_RDONLY, 0); 209 if (ffd < 0) 210 err(4, "open font %s", argv[0]); 211 if (!f.name) 212 f.name = argv[0]; 213 } else 214 ffd = 0; 215 216 if (!f.stride) 217 f.stride = (f.fontwidth + 7) / 8; 218 len = f.fontheight * f.numchars * f.stride; 219 if (!len) 220 errx(1, "invalid font size"); 221 222 buf = malloc(len); 223 if (!buf) 224 errx(1, "malloc"); 225 res = read(ffd, buf, len); 226 if (res < 0) 227 err(4, "read font"); 228 if (res != len) 229 errx(4, "short read"); 230 231 f.data = buf; 232 233 if (verbose) { 234 printf("name: %s\n", f.name); 235 printf("firstchar: %d\n", f.firstchar); 236 printf("numchars: %d\n", f.numchars); 237 printf("encoding: %s (%d)\n", 238 rgetencoding(f.encoding), f.encoding); 239 printf("fontwidth: %d\n", f.fontwidth); 240 printf("fontheight: %d\n", f.fontheight); 241 printf("stride: %d\n", f.stride); 242 printf("bitorder: %s (%d)\n", 243 rgetfontorder(f.bitorder), f.bitorder); 244 printf("byteorder: %s (%d)\n", 245 rgetfontorder(f.byteorder), f.byteorder); 246 } 247 248 res = ioctl(wsfd, WSDISPLAYIO_LDFONT, &f); 249 if (res < 0) 250 err(3, "WSDISPLAYIO_LDFONT"); 251 252 return (0); 253 } 254