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