xref: /freebsd/usr.sbin/vidcontrol/vidcontrol.c (revision 11afcc8f9f96d657b8e6f7547c02c1957331fc96)
1 /*-
2  * Copyright (c) 1994-1996 S�ren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software withough specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef lint
30 static const char rcsid[] =
31 	"$Id: vidcontrol.c,v 1.18 1997/10/27 07:52:10 charnier Exp $";
32 #endif /* not lint */
33 
34 #include <ctype.h>
35 #include <err.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <machine/console.h>
41 #include <sys/errno.h>
42 #include "path.h"
43 #include "decode.h"
44 
45 char 	legal_colors[16][16] = {
46 	"black", "blue", "green", "cyan",
47 	"red", "magenta", "brown", "white",
48 	"grey", "lightblue", "lightgreen", "lightcyan",
49 	"lightred", "lightmagenta", "yellow", "lightwhite"
50 };
51 int 	hex = 0;
52 int 	number;
53 char 	letter;
54 struct 	vid_info info;
55 
56 
57 static void
58 usage()
59 {
60 	fprintf(stderr, "%s\n%s\n%s\n",
61 "usage: vidcontrol [-r fg bg] [-b color] [-c appearance] [-d] [-l scrmap]",
62 "                  [-L] [-m on|off] [-f size file] [-s number] [-t N|off]",
63 "                  [-x] [mode] [fgcol [bgcol]] [show]");
64 	exit(1);
65 }
66 
67 char *
68 nextarg(int ac, char **av, int *indp, int oc)
69 {
70 	if (*indp < ac)
71 		return(av[(*indp)++]);
72 	errx(1, "option requires two arguments -- %c", oc);
73 	return("");
74 }
75 
76 char *
77 mkfullname(const char *s1, const char *s2, const char *s3)
78 {
79 	static char *buf = NULL;
80 	static int bufl = 0;
81 	int f;
82 
83 	f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
84 	if (f > bufl)
85 		if (buf)
86 			buf = (char *)realloc(buf, f);
87 		else
88 			buf = (char *)malloc(f);
89 	if (!buf) {
90 		bufl = 0;
91 		return(NULL);
92 	}
93 
94 	bufl = f;
95 	strcpy(buf, s1);
96 	strcat(buf, s2);
97 	strcat(buf, s3);
98 	return(buf);
99 }
100 
101 void
102 load_scrnmap(char *filename)
103 {
104 	FILE *fd = 0;
105 	int i, size;
106 	char *name;
107 	scrmap_t scrnmap;
108 	char *prefix[]  = {"", "", SCRNMAP_PATH, SCRNMAP_PATH, NULL};
109 	char *postfix[] = {"", ".scm", "", ".scm"};
110 
111 	for (i=0; prefix[i]; i++) {
112 		name = mkfullname(prefix[i], filename, postfix[i]);
113 		fd = fopen(name, "r");
114 		if (fd)
115 			break;
116 	}
117 	if (fd == NULL) {
118 		warn("screenmap file not found");
119 		return;
120 	}
121 	size = sizeof(scrnmap);
122 	if (decode(fd, (char *)&scrnmap) != size) {
123 		rewind(fd);
124 		if (fread(&scrnmap, 1, size, fd) != size) {
125 			warnx("bad screenmap file");
126 			fclose(fd);
127 			return;
128 		}
129 	}
130 	if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
131 		warn("can't load screenmap");
132 	fclose(fd);
133 }
134 
135 void
136 load_default_scrnmap()
137 {
138 	scrmap_t scrnmap;
139 	int i;
140 
141 	for (i=0; i<256; i++)
142 		*((char*)&scrnmap + i) = i;
143 	if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
144 		warn("can't load default screenmap");
145 }
146 
147 void
148 print_scrnmap()
149 {
150 	unsigned char map[256];
151 	int i;
152 
153 	if (ioctl(0, GIO_SCRNMAP, &map) < 0) {
154 		warn("getting screenmap");
155 		return;
156 	}
157 	for (i=0; i<sizeof(map); i++) {
158 		if (i > 0 && i % 16 == 0)
159 			fprintf(stdout, "\n");
160 		if (hex)
161 			fprintf(stdout, " %02x", map[i]);
162 		else
163 			fprintf(stdout, " %03d", map[i]);
164 	}
165 	fprintf(stdout, "\n");
166 
167 }
168 
169 void
170 load_font(char *type, char *filename)
171 {
172 	FILE	*fd = 0;
173 	int	i, size;
174 	unsigned long io;
175 	char	*name, *fontmap;
176 	char	*prefix[]  = {"", "", FONT_PATH, FONT_PATH, NULL};
177 	char	*postfix[] = {"", ".fnt", "", ".fnt"};
178 
179 	for (i=0; prefix[i]; i++) {
180 		name = mkfullname(prefix[i], filename, postfix[i]);
181 		fd = fopen(name, "r");
182 		if (fd)
183 			break;
184 	}
185 	if (fd == NULL) {
186 		warn("font file not found");
187 		return;
188 	}
189 	if (!strcmp(type, "8x8")) {
190 		size = 8*256;
191 		io = PIO_FONT8x8;
192 	}
193 	else if (!strcmp(type, "8x14")) {
194 		size = 14*256;
195 		io = PIO_FONT8x14;
196 	}
197 	else if (!strcmp(type, "8x16")) {
198 		size = 16*256;
199 		io = PIO_FONT8x16;
200 	}
201 	else {
202 		warn("bad font size specification");
203 		fclose(fd);
204 		return;
205 	}
206 	fontmap = (char*) malloc(size);
207 	if (decode(fd, fontmap) != size) {
208 		rewind(fd);
209 		if (fread(fontmap, 1, size, fd) != size) {
210 			warnx("bad font file");
211 			fclose(fd);
212 			free(fontmap);
213 			return;
214 		}
215 	}
216 	if (ioctl(0, io, fontmap) < 0)
217 		warn("can't load font");
218 	fclose(fd);
219 	free(fontmap);
220 }
221 
222 void
223 set_screensaver_timeout(char *arg)
224 {
225 	int nsec;
226 
227 	if (!strcmp(arg, "off"))
228 		nsec = 0;
229 	else {
230 		nsec = atoi(arg);
231 		if ((*arg == '\0') || (nsec < 1)) {
232 			warnx("argument must be a positive number");
233 			return;
234 		}
235 	}
236 	if (ioctl(0, CONS_BLANKTIME, &nsec) == -1)
237 		warn("setting screensaver period");
238 }
239 
240 void
241 set_cursor_type(char *appearence)
242 {
243 	int type;
244 
245 	if (!strcmp(appearence, "normal"))
246 		type = 0;
247 	else if (!strcmp(appearence, "blink"))
248 		type = 1;
249 	else if (!strcmp(appearence, "destructive"))
250 		type = 3;
251 	else {
252 		warnx("argument to -c must be normal, blink or destructive");
253 		return;
254 	}
255 	ioctl(0, CONS_CURSORTYPE, &type);
256 }
257 
258 void
259 video_mode(int argc, char **argv, int *index)
260 {
261 	unsigned long mode;
262 
263 	if (*index < argc) {
264 		if (!strcmp(argv[*index], "VGA_40x25"))
265 			mode = SW_VGA_C40x25;
266 		else if (!strcmp(argv[*index], "VGA_80x25"))
267 			mode = SW_VGA_C80x25;
268 		else if (!strcmp(argv[*index], "VGA_80x30"))
269 			mode = SW_VGA_C80x30;
270 		else if (!strcmp(argv[*index], "VGA_80x50"))
271 			mode = SW_VGA_C80x50;
272 		else if (!strcmp(argv[*index], "VGA_80x60"))
273 			mode = SW_VGA_C80x60;
274 		else if (!strcmp(argv[*index], "VGA_320x200"))
275 			mode = SW_VGA_CG320;
276 		else if (!strcmp(argv[*index], "EGA_80x25"))
277 			mode = SW_ENH_C80x25;
278 		else if (!strcmp(argv[*index], "EGA_80x43"))
279 			mode = SW_ENH_C80x43;
280 		else
281 			return;
282 		if (ioctl(0, mode, NULL) < 0)
283 			warn("cannot set videomode");
284 		(*index)++;
285 	}
286 	return;
287 }
288 
289 int
290 get_color_number(char *color)
291 {
292 	int i;
293 
294 	for (i=0; i<16; i++)
295 		if (!strcmp(color, legal_colors[i]))
296 			return i;
297 	return -1;
298 }
299 
300 void
301 set_normal_colors(int argc, char **argv, int *index)
302 {
303 	int color;
304 
305 	if (*index < argc && (color = get_color_number(argv[*index])) != -1) {
306 		(*index)++;
307 		fprintf(stderr, "[=%dF", color);
308 		if (*index < argc
309 		    && (color = get_color_number(argv[*index])) != -1
310 		    && color < 8) {
311 			(*index)++;
312 			fprintf(stderr, "[=%dG", color);
313 		}
314 	}
315 }
316 
317 void
318 set_reverse_colors(int argc, char **argv, int *index)
319 {
320 	int color;
321 
322 	if ((color = get_color_number(argv[*(index)-1])) != -1) {
323 		fprintf(stderr, "[=%dH", color);
324 		if (*index < argc
325 		    && (color = get_color_number(argv[*index])) != -1
326 		    && color < 8) {
327 			(*index)++;
328 			fprintf(stderr, "[=%dI", color);
329 		}
330 	}
331 }
332 
333 void
334 set_console(char *arg)
335 {
336 	int n;
337 
338 	if( !arg || strspn(arg,"0123456789") != strlen(arg)) {
339 		warnx("bad console number");
340 		return;
341 	}
342 
343 	n = atoi(arg);
344 	if (n < 1 || n > 12) {
345 		warnx("console number out of range");
346 	} else if (ioctl(0,VT_ACTIVATE,(char *)n) == -1)
347 		warn("ioctl(VT_ACTIVATE)");
348 }
349 
350 void
351 set_border_color(char *arg)
352 {
353 	int color;
354 
355 	if ((color = get_color_number(arg)) != -1) {
356 		fprintf(stderr, "[=%dA", color);
357 	}
358 	else
359 		usage();
360 }
361 
362 void
363 set_mouse(char *arg)
364 {
365 	struct mouse_info mouse;
366 
367 	if (!strcmp(arg, "on"))
368 		mouse.operation = MOUSE_SHOW;
369 	else if (!strcmp(arg, "off"))
370 		mouse.operation = MOUSE_HIDE;
371 	else {
372 		warnx("argument to -m must either on or off");
373 		return;
374 	}
375 	ioctl(0, CONS_MOUSECTL, &mouse);
376 }
377 
378 void
379 test_frame()
380 {
381 	int i;
382 
383 	fprintf(stdout, "[=0G\n\n");
384 	for (i=0; i<8; i++) {
385 		fprintf(stdout, "[=15F[=0G        %2d [=%dF%-16s"
386 				"[=15F[=0G        %2d [=%dF%-16s        "
387 				"[=15F %2d [=%dGBACKGROUND[=0G\n",
388 			i, i, legal_colors[i], i+8, i+8,
389 			legal_colors[i+8], i, i);
390 	}
391 	fprintf(stdout, "[=%dF[=%dG[=%dH[=%dI\n",
392 		info.mv_norm.fore, info.mv_norm.back,
393 		info.mv_rev.fore, info.mv_rev.back);
394 }
395 
396 int
397 main(int argc, char **argv)
398 {
399 	int		opt;
400 
401 
402 	info.size = sizeof(info);
403 	if (ioctl(0, CONS_GETINFO, &info) < 0)
404 		err(1, "must be on a virtual console");
405 	while((opt = getopt(argc, argv, "b:c:df:l:Lm:r:s:t:x")) != -1)
406 		switch(opt) {
407 			case 'b':
408 				set_border_color(optarg);
409 				break;
410 			case 'c':
411 				set_cursor_type(optarg);
412 				break;
413 			case 'd':
414 				print_scrnmap();
415 				break;
416 			case 'f':
417 				load_font(optarg,
418 					nextarg(argc, argv, &optind, 'f'));
419 				break;
420 			case 'l':
421 				load_scrnmap(optarg);
422 				break;
423 			case 'L':
424 				load_default_scrnmap();
425 				break;
426 			case 'm':
427 				set_mouse(optarg);
428 				break;
429 			case 'r':
430 				set_reverse_colors(argc, argv, &optind);
431 				break;
432 			case 's':
433 				set_console(optarg);
434 				break;
435 			case 't':
436 				set_screensaver_timeout(optarg);
437 				break;
438 			case 'x':
439 				hex = 1;
440 				break;
441 			default:
442 				usage();
443 		}
444 	video_mode(argc, argv, &optind);
445 	set_normal_colors(argc, argv, &optind);
446 	if (optind < argc && !strcmp(argv[optind], "show")) {
447 		test_frame();
448 		optind++;
449 	}
450 	if ((optind != argc) || (argc == 1))
451 		usage();
452 	return 0;
453 }
454 
455