vidcontrol.c revision 42505
1275970Scy/*-
2275970Scy * Copyright (c) 1994-1996 S�ren Schmidt
3275970Scy * All rights reserved.
4275970Scy *
5275970Scy * Redistribution and use in source and binary forms, with or without
6275970Scy * modification, are permitted provided that the following conditions
7275970Scy * are met:
8275970Scy * 1. Redistributions of source code must retain the above copyright
9275970Scy *    notice, this list of conditions and the following disclaimer,
10275970Scy *    in this position and unchanged.
11275970Scy * 2. Redistributions in binary form must reproduce the above copyright
12275970Scy *    notice, this list of conditions and the following disclaimer in the
13275970Scy *    documentation and/or other materials provided with the distribution.
14275970Scy * 3. The name of the author may not be used to endorse or promote products
15275970Scy *    derived from this software withough specific prior written permission
16275970Scy *
17275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27275970Scy */
28275970Scy
29275970Scy#ifndef lint
30275970Scystatic const char rcsid[] =
31275970Scy	"$Id: vidcontrol.c,v 1.24 1998/10/01 11:40:22 yokota Exp $";
32275970Scy#endif /* not lint */
33275970Scy
34275970Scy#include <ctype.h>
35275970Scy#include <err.h>
36275970Scy#include <stdio.h>
37275970Scy#include <stdlib.h>
38275970Scy#include <string.h>
39275970Scy#include <unistd.h>
40275970Scy#include <machine/console.h>
41275970Scy#include <sys/errno.h>
42275970Scy#include "path.h"
43275970Scy#include "decode.h"
44275970Scy
45275970Scychar 	legal_colors[16][16] = {
46275970Scy	"black", "blue", "green", "cyan",
47275970Scy	"red", "magenta", "brown", "white",
48275970Scy	"grey", "lightblue", "lightgreen", "lightcyan",
49275970Scy	"lightred", "lightmagenta", "yellow", "lightwhite"
50275970Scy};
51275970Scyint 	hex = 0;
52275970Scyint 	number;
53275970Scychar 	letter;
54275970Scystruct 	vid_info info;
55275970Scy
56275970Scy
57275970Scystatic void
58275970Scyusage()
59275970Scy{
60275970Scy	fprintf(stderr, "%s\n%s\n%s\n",
61275970Scy"usage: vidcontrol [-r fg bg] [-b color] [-c appearance] [-d] [-l scrmap]",
62275970Scy"                  [-i adapter | mode] [-L] [-m on|off] [-f size file]",
63275970Scy"                  [-s number] [-t N|off] [-x] [mode] [fgcol [bgcol]] [show]");
64275970Scy	exit(1);
65275970Scy}
66275970Scy
67275970Scychar *
68275970Scynextarg(int ac, char **av, int *indp, int oc)
69275970Scy{
70275970Scy	if (*indp < ac)
71275970Scy		return(av[(*indp)++]);
72275970Scy	errx(1, "option requires two arguments -- %c", oc);
73275970Scy	return("");
74275970Scy}
75275970Scy
76275970Scychar *
77275970Scymkfullname(const char *s1, const char *s2, const char *s3)
78275970Scy{
79275970Scy	static char *buf = NULL;
80275970Scy	static int bufl = 0;
81275970Scy	int f;
82275970Scy
83275970Scy	f = strlen(s1) + strlen(s2) + strlen(s3) + 1;
84275970Scy	if (f > bufl)
85275970Scy		if (buf)
86275970Scy			buf = (char *)realloc(buf, f);
87275970Scy		else
88275970Scy			buf = (char *)malloc(f);
89275970Scy	if (!buf) {
90275970Scy		bufl = 0;
91275970Scy		return(NULL);
92275970Scy	}
93275970Scy
94275970Scy	bufl = f;
95275970Scy	strcpy(buf, s1);
96275970Scy	strcat(buf, s2);
97275970Scy	strcat(buf, s3);
98275970Scy	return(buf);
99275970Scy}
100275970Scy
101275970Scyvoid
102275970Scyload_scrnmap(char *filename)
103275970Scy{
104275970Scy	FILE *fd = 0;
105275970Scy	int i, size;
106275970Scy	char *name;
107275970Scy	scrmap_t scrnmap;
108275970Scy	char *prefix[]  = {"", "", SCRNMAP_PATH, SCRNMAP_PATH, NULL};
109275970Scy	char *postfix[] = {"", ".scm", "", ".scm"};
110275970Scy
111275970Scy	for (i=0; prefix[i]; i++) {
112275970Scy		name = mkfullname(prefix[i], filename, postfix[i]);
113275970Scy		fd = fopen(name, "r");
114275970Scy		if (fd)
115275970Scy			break;
116275970Scy	}
117275970Scy	if (fd == NULL) {
118275970Scy		warn("screenmap file not found");
119275970Scy		return;
120275970Scy	}
121275970Scy	size = sizeof(scrnmap);
122275970Scy	if (decode(fd, (char *)&scrnmap) != size) {
123275970Scy		rewind(fd);
124275970Scy		if (fread(&scrnmap, 1, size, fd) != size) {
125275970Scy			warnx("bad screenmap file");
126275970Scy			fclose(fd);
127282408Scy			return;
128282408Scy		}
129282408Scy	}
130282408Scy	if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
131282408Scy		warn("can't load screenmap");
132282408Scy	fclose(fd);
133275970Scy}
134275970Scy
135275970Scyvoid
136load_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
147void
148print_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
169void
170load_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
222void
223set_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
240void
241set_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
258void
259video_mode(int argc, char **argv, int *index)
260{
261	static struct {
262		char *name;
263		unsigned long mode;
264	} modes[] = {
265#ifdef SW_TEXT_80x25
266		{ "80x25",		SW_TEXT_80x25 },
267		{ "80x30",		SW_TEXT_80x30 },
268		{ "80x43",		SW_TEXT_80x43 },
269		{ "80x50",		SW_TEXT_80x50 },
270		{ "80x60",		SW_TEXT_80x60 },
271		{ "132x25",		SW_TEXT_132x25 },
272		{ "132x30",		SW_TEXT_132x30 },
273		{ "132x43",		SW_TEXT_132x43 },
274		{ "132x50",		SW_TEXT_132x50 },
275		{ "132x60",		SW_TEXT_132x60 },
276#endif
277		{ "VGA_40x25",		SW_VGA_C40x25 },
278		{ "VGA_80x25",		SW_VGA_C80x25 },
279		{ "VGA_80x30",		SW_VGA_C80x30 },
280		{ "VGA_80x50",		SW_VGA_C80x50 },
281		{ "VGA_80x60",		SW_VGA_C80x60 },
282		{ "VGA_320x200",	SW_VGA_CG320 },
283		{ "EGA_80x25",		SW_ENH_C80x25 },
284		{ "EGA_80x43",		SW_ENH_C80x43 },
285		{ "VESA_132x25",	SW_VESA_C132x25 },
286		{ "VESA_132x43",	SW_VESA_C132x43 },
287		{ "VESA_132x50",	SW_VESA_C132x50 },
288		{ "VESA_132x60",	SW_VESA_C132x60 },
289		{ "VESA_800x600",	SW_VESA_800x600 },
290		{ NULL },
291	};
292	unsigned long mode;
293	int size[3];
294	int i;
295
296	if (*index < argc) {
297		for (i = 0; modes[i].name != NULL; ++i) {
298			if (!strcmp(argv[*index], modes[i].name)) {
299				mode = modes[i].mode;
300				break;
301			}
302		}
303		if (modes[i].name == NULL)
304			return;
305		if (ioctl(0, mode, NULL) < 0)
306			warn("cannot set videomode");
307		if (mode == SW_VESA_800x600) {
308			size[0] = 80;	/* columns */
309			size[1] = 25;	/* rows */
310			size[2] = 16;	/* font size */
311			if (ioctl(0, KDRASTER, size))
312				warn("cannot activate raster display");
313		}
314		(*index)++;
315	}
316	return;
317}
318
319int
320get_color_number(char *color)
321{
322	int i;
323
324	for (i=0; i<16; i++)
325		if (!strcmp(color, legal_colors[i]))
326			return i;
327	return -1;
328}
329
330void
331set_normal_colors(int argc, char **argv, int *index)
332{
333	int color;
334
335	if (*index < argc && (color = get_color_number(argv[*index])) != -1) {
336		(*index)++;
337		fprintf(stderr, "[=%dF", color);
338		if (*index < argc
339		    && (color = get_color_number(argv[*index])) != -1
340		    && color < 8) {
341			(*index)++;
342			fprintf(stderr, "[=%dG", color);
343		}
344	}
345}
346
347void
348set_reverse_colors(int argc, char **argv, int *index)
349{
350	int color;
351
352	if ((color = get_color_number(argv[*(index)-1])) != -1) {
353		fprintf(stderr, "[=%dH", color);
354		if (*index < argc
355		    && (color = get_color_number(argv[*index])) != -1
356		    && color < 8) {
357			(*index)++;
358			fprintf(stderr, "[=%dI", color);
359		}
360	}
361}
362
363void
364set_console(char *arg)
365{
366	int n;
367
368	if( !arg || strspn(arg,"0123456789") != strlen(arg)) {
369		warnx("bad console number");
370		return;
371	}
372
373	n = atoi(arg);
374	if (n < 1 || n > 12) {
375		warnx("console number out of range");
376	} else if (ioctl(0,VT_ACTIVATE,(char *)n) == -1)
377		warn("ioctl(VT_ACTIVATE)");
378}
379
380void
381set_border_color(char *arg)
382{
383	int color;
384
385	if ((color = get_color_number(arg)) != -1) {
386		fprintf(stderr, "[=%dA", color);
387	}
388	else
389		usage();
390}
391
392void
393set_mouse(char *arg)
394{
395	struct mouse_info mouse;
396
397	if (!strcmp(arg, "on"))
398		mouse.operation = MOUSE_SHOW;
399	else if (!strcmp(arg, "off"))
400		mouse.operation = MOUSE_HIDE;
401	else {
402		warnx("argument to -m must either on or off");
403		return;
404	}
405	ioctl(0, CONS_MOUSECTL, &mouse);
406}
407
408static char
409*adapter_name(int type)
410{
411    static struct {
412	int type;
413	char *name;
414    } names[] = {
415	{ KD_MONO,	"MDA" },
416	{ KD_HERCULES,	"Hercules" },
417	{ KD_CGA,	"CGA" },
418	{ KD_EGA,	"EGA" },
419	{ KD_VGA,	"VGA" },
420	{ KD_PC98,	"PC-98xx" },
421	{ -1,		"Unknown" },
422    };
423    int i;
424
425    for (i = 0; names[i].type != -1; ++i)
426	if (names[i].type == type)
427	    break;
428    return names[i].name;
429}
430
431void
432show_adapter_info(void)
433{
434	struct video_adapter_info ad;
435
436	ad.va_index = 0;
437	if (ioctl(0, CONS_ADPINFO, &ad)) {
438		warn("failed to obtain adapter information");
439		return;
440	}
441
442	printf("fb%d:\n", ad.va_index);
443	printf("    %.*s%d, type:%s%s (%d), flags:0x%x\n",
444	       (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
445	       (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
446	       adapter_name(ad.va_type), ad.va_type, ad.va_flags);
447	printf("    initial mode:%d, current mode:%d, BIOS mode:%d\n",
448	       ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
449}
450
451void
452show_mode_info(void)
453{
454	struct video_info info;
455	char buf[80];
456	int mode;
457	int c;
458
459	printf("    mode#     flags   type    size       "
460	       "font      window      linear buffer\n");
461	printf("---------------------------------------"
462	       "---------------------------------------\n");
463	for (mode = 0; mode < M_VESA_MODE_MAX; ++mode) {
464		info.vi_mode = mode;
465		if (ioctl(0, CONS_MODEINFO, &info))
466			continue;
467
468		printf("%3d (0x%03x)", mode, mode);
469    		printf(" 0x%08x", info.vi_flags);
470		if (info.vi_flags & V_INFO_GRAPHICS) {
471			c = 'G';
472			snprintf(buf, sizeof(buf), "%dx%dx%d %d",
473				 info.vi_width, info.vi_height,
474				 info.vi_depth, info.vi_planes);
475		} else {
476			c = 'T';
477			snprintf(buf, sizeof(buf), "%dx%d",
478				 info.vi_width, info.vi_height);
479		}
480		printf(" %c %-15s", c, buf);
481		snprintf(buf, sizeof(buf), "%dx%d",
482			 info.vi_cwidth, info.vi_cheight);
483		printf(" %-5s", buf);
484    		printf(" 0x%05x %2dk %2dk",
485		       info.vi_window, info.vi_window_size/1024,
486		       info.vi_window_gran/1024);
487    		printf(" 0x%08x %2dk\n",
488		       info.vi_buffer, info.vi_buffer_size/1024);
489	}
490}
491
492void
493show_info(char *arg)
494{
495	if (!strcmp(arg, "adapter"))
496		show_adapter_info();
497	else if (!strcmp(arg, "mode"))
498		show_mode_info();
499	else {
500		warnx("argument to -i must either adapter or mode");
501		return;
502	}
503}
504
505void
506test_frame()
507{
508	int i;
509
510	fprintf(stdout, "[=0G\n\n");
511	for (i=0; i<8; i++) {
512		fprintf(stdout, "[=15F[=0G        %2d [=%dF%-16s"
513				"[=15F[=0G        %2d [=%dF%-16s        "
514				"[=15F %2d [=%dGBACKGROUND[=0G\n",
515			i, i, legal_colors[i], i+8, i+8,
516			legal_colors[i+8], i, i);
517	}
518	fprintf(stdout, "[=%dF[=%dG[=%dH[=%dI\n",
519		info.mv_norm.fore, info.mv_norm.back,
520		info.mv_rev.fore, info.mv_rev.back);
521}
522
523int
524main(int argc, char **argv)
525{
526	int		opt;
527
528
529	info.size = sizeof(info);
530	if (ioctl(0, CONS_GETINFO, &info) < 0)
531		err(1, "must be on a virtual console");
532	while((opt = getopt(argc, argv, "b:c:df:i:l:Lm:r:s:t:x")) != -1)
533		switch(opt) {
534			case 'b':
535				set_border_color(optarg);
536				break;
537			case 'c':
538				set_cursor_type(optarg);
539				break;
540			case 'd':
541				print_scrnmap();
542				break;
543			case 'f':
544				load_font(optarg,
545					nextarg(argc, argv, &optind, 'f'));
546				break;
547			case 'i':
548				show_info(optarg);
549				break;
550			case 'l':
551				load_scrnmap(optarg);
552				break;
553			case 'L':
554				load_default_scrnmap();
555				break;
556			case 'm':
557				set_mouse(optarg);
558				break;
559			case 'r':
560				set_reverse_colors(argc, argv, &optind);
561				break;
562			case 's':
563				set_console(optarg);
564				break;
565			case 't':
566				set_screensaver_timeout(optarg);
567				break;
568			case 'x':
569				hex = 1;
570				break;
571			default:
572				usage();
573		}
574	video_mode(argc, argv, &optind);
575	set_normal_colors(argc, argv, &optind);
576	if (optind < argc && !strcmp(argv[optind], "show")) {
577		test_frame();
578		optind++;
579	}
580	if ((optind != argc) || (argc == 1))
581		usage();
582	return 0;
583}
584
585