xref: /openbsd-src/sbin/wsconsctl/display.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: display.c,v 1.20 2015/05/08 19:12:51 miod Exp $	*/
2 /*	$NetBSD: display.c,v 1.1 1998/12/28 14:01:16 hannken Exp $ */
3 
4 /*-
5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Juergen Hannken-Illjes.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/ioctl.h>
34 #include <sys/time.h>
35 #include <dev/wscons/wsconsio.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <err.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include "wsconsctl.h"
42 
43 u_int dpytype;
44 u_int width, height, depth;
45 int focus;
46 struct field_pc brightness, contrast, backlight;
47 int burnon, burnoff, vblank, kbdact, msact, outact;
48 char fontname[WSFONT_NAME_SIZE];
49 struct wsdisplay_emultype emuls;
50 struct wsdisplay_screentype screens;
51 
52 struct field display_field_tab[] = {
53     { "type",		&dpytype,	FMT_DPYTYPE,	FLG_RDONLY },
54     { "width",		&width,		FMT_UINT,	FLG_RDONLY },
55     { "height",		&height,	FMT_UINT,	FLG_RDONLY },
56     { "depth",		&depth,		FMT_UINT,	FLG_RDONLY },
57     { "emulations",	&emuls,		FMT_EMUL,	FLG_RDONLY },
58     { "screentypes",	&screens,	FMT_SCREEN,	FLG_RDONLY },
59     { "focus",		&focus,		FMT_INT,	FLG_NORDBACK },
60     { "brightness",	&brightness,	FMT_PC,		FLG_MODIFY|FLG_INIT },
61     { "contrast",	&contrast,	FMT_PC,		FLG_MODIFY|FLG_INIT },
62     { "backlight",	&backlight,	FMT_PC,		FLG_MODIFY|FLG_INIT },
63     /* screen burner section, outact MUST BE THE LAST, see the set_values */
64     { "screen_on",	&burnon,	FMT_UINT,	FLG_MODIFY|FLG_INIT },
65     { "screen_off",	&burnoff,	FMT_UINT,	FLG_MODIFY|FLG_INIT },
66     { "vblank",		&vblank,	FMT_BOOL,	FLG_MODIFY|FLG_INIT },
67     { "kbdact",		&kbdact,	FMT_BOOL,	FLG_MODIFY|FLG_INIT },
68     { "msact",		&msact,		FMT_BOOL,	FLG_MODIFY|FLG_INIT },
69     { "outact",		&outact,	FMT_BOOL,	FLG_MODIFY|FLG_INIT },
70     { "font",		fontname,	FMT_STRING,	FLG_WRONLY },
71     { NULL }
72 };
73 
74 #define	fillioctl(n)	{ cmd = n; cmd_str = #n; }
75 
76 void
77 display_get_values(int fd)
78 {
79 	struct wsdisplay_addscreendata gscr;
80 	struct wsdisplay_param param;
81 	struct wsdisplay_burner burners;
82 	struct wsdisplay_fbinfo fbinfo;
83 	struct field *pf;
84 	const char *cmd_str;
85 	void *ptr;
86 	unsigned long cmd;
87 	int bon = 0, fbon = 0;
88 
89 	focus = gscr.idx = -1;
90 	for (pf = display_field_tab; pf->name; pf++) {
91 
92 		if (!(pf->flags & FLG_GET) || pf->flags & FLG_DEAD)
93 			continue;
94 
95 		ptr = pf->valp;
96 
97 		if (ptr == &dpytype) {
98 			fillioctl(WSDISPLAYIO_GTYPE);
99 		} else if (ptr == &focus) {
100 			fillioctl(WSDISPLAYIO_GETSCREEN);
101 			ptr = &gscr;
102 		} else if (ptr == &emuls) {
103 			fillioctl(WSDISPLAYIO_GETEMULTYPE);
104 			emuls.idx=0;
105 		} else if (ptr == &screens) {
106 			fillioctl(WSDISPLAYIO_GETSCREENTYPE);
107 			screens.idx=0;
108 		} else if (ptr == &brightness) {
109 			ptr = &param;
110 			param.param = WSDISPLAYIO_PARAM_BRIGHTNESS;
111 		} else if (ptr == &contrast) {
112 			ptr = &param;
113 			param.param = WSDISPLAYIO_PARAM_CONTRAST;
114 		} else if (ptr == &backlight) {
115 			ptr = &param;
116 			param.param = WSDISPLAYIO_PARAM_BACKLIGHT;
117 		} else if (ptr == &burnon || ptr == &burnoff ||
118 			   ptr == &vblank || ptr == &kbdact ||
119 			   ptr == &outact || ptr == &msact) {
120 			fillioctl(WSDISPLAYIO_GBURNER);
121 			ptr = &burners;
122 			if (!bon)
123 				bzero(&burners, sizeof(burners));
124 		} else if (ptr == &height || ptr == &width ||
125 			   ptr == &depth) {
126 			fillioctl(WSDISPLAYIO_GINFO);
127 			ptr = &fbinfo;
128 			if (!fbon)
129 				bzero(&fbinfo, sizeof(fbinfo));
130 		} else
131 			cmd = 0;
132 
133 		if (ptr == &param) {
134 			fillioctl(WSDISPLAYIO_GETPARAM);
135 		}
136 
137 		if ((cmd != WSDISPLAYIO_GBURNER && cmd != WSDISPLAYIO_GINFO) ||
138 		    (cmd == WSDISPLAYIO_GBURNER && !bon) ||
139 		    (cmd == WSDISPLAYIO_GINFO && !fbon)) {
140 			errno = ENOTTY;
141 			if (!cmd || ioctl(fd, cmd, ptr) < 0) {
142 				if (errno == ENOTTY) {
143 					pf->flags |= FLG_DEAD;
144 					continue;
145 				} else
146 					warn("%s", cmd_str);
147 			}
148 		}
149 
150 		if (ptr == &burners) {
151 			if (!bon) {
152 				burnon = burners.on;
153 				burnoff = burners.off;
154 				vblank = burners.flags & WSDISPLAY_BURN_VBLANK;
155 				kbdact = burners.flags & WSDISPLAY_BURN_KBD;
156 				msact = burners.flags & WSDISPLAY_BURN_MOUSE;
157 				outact = burners.flags & WSDISPLAY_BURN_OUTPUT;
158 			}
159 			bon++;
160 		} else if (ptr == &fbinfo) {
161 			if (!fbon) {
162 				width = fbinfo.width;
163 				height = fbinfo.height;
164 				depth = fbinfo.depth;
165 			}
166 			fbon++;
167 		} else if (ptr == &emuls) {
168 			emuls.idx=fd;
169 		} else if (ptr == &screens) {
170 			screens.idx=fd;
171 		} else if (ptr == &param) {
172 			struct field_pc *pc = pf->valp;
173 
174 			pc->min = param.min;
175 			pc->cur = param.curval;
176 			pc->max = param.max;
177 		} else if (ptr == &gscr)
178 			focus = gscr.idx;
179 	}
180 }
181 
182 int
183 display_put_values(int fd)
184 {
185 	struct wsdisplay_param param;
186 	struct wsdisplay_burner burners;
187 	struct wsdisplay_font font;
188 	struct field *pf;
189 	const char *cmd_str;
190 	void *ptr;
191 	unsigned long cmd;
192 	int id;
193 
194 	for (pf = display_field_tab; pf->name; pf++) {
195 
196 		if (!(pf->flags & FLG_SET) || pf->flags & FLG_DEAD)
197 			continue;
198 
199 		ptr = pf->valp;
200 
201 		if (ptr == &focus) {
202 			fillioctl(WSDISPLAYIO_SETSCREEN);
203 		} else if (ptr == &brightness) {
204 			ptr = &param;
205 			id = WSDISPLAYIO_PARAM_BRIGHTNESS;
206 		} else if (ptr == &contrast) {
207 			ptr = &param;
208 			id = WSDISPLAYIO_PARAM_CONTRAST;
209 		} else if (ptr == &backlight) {
210 			ptr = &param;
211 			id = WSDISPLAYIO_PARAM_BACKLIGHT;
212 		} else if (ptr == &burnon || ptr == &burnoff ||
213 			   ptr == &vblank || ptr == &kbdact ||
214 			   ptr == &outact || ptr == &msact) {
215 
216 			bzero(&burners, sizeof(burners));
217 			burners.on = burnon;
218 			burners.off = burnoff;
219 			if (vblank)
220 				burners.flags |= WSDISPLAY_BURN_VBLANK;
221 			else
222 				burners.flags &= ~WSDISPLAY_BURN_VBLANK;
223 			if (kbdact)
224 				burners.flags |= WSDISPLAY_BURN_KBD;
225 			else
226 				burners.flags &= ~WSDISPLAY_BURN_KBD;
227 			if (msact)
228 				burners.flags |= WSDISPLAY_BURN_MOUSE;
229 			else
230 				burners.flags &= ~WSDISPLAY_BURN_MOUSE;
231 			if (outact)
232 				burners.flags |= WSDISPLAY_BURN_OUTPUT;
233 			else
234 				burners.flags &= ~WSDISPLAY_BURN_OUTPUT;
235 
236 			fillioctl(WSDISPLAYIO_SBURNER);
237 			ptr = &burners;
238 		} else if (ptr == fontname) {
239 			bzero(&font, sizeof(font));
240 			strlcpy(font.name, ptr, sizeof font.name);
241 			fillioctl(WSDISPLAYIO_USEFONT);
242 		} else
243 			cmd = 0;
244 
245 		if (ptr == &param) {
246 			struct field_pc *pc = pf->valp;
247 
248 			bzero(&param, sizeof(param));
249 			param.param = id;
250 			param.min = pc->min;
251 			param.curval = pc->cur;
252 			param.max = pc->max;
253 			fillioctl(WSDISPLAYIO_SETPARAM);
254 		}
255 
256 		errno = ENOTTY;
257 		if (!cmd || ioctl(fd, cmd, ptr) < 0) {
258 			if (errno == ENOTTY) {
259 				pf->flags |= FLG_DEAD;
260 				continue;
261 			} else {
262 				warn("%s", cmd_str);
263 				return 1;
264 			}
265 		}
266 	}
267 
268 	return 0;
269 }
270 
271 char *
272 display_next_device(int index)
273 {
274 	static char devname[20];
275 
276 	if (index > 7)
277 		return (NULL);
278 
279 	snprintf(devname, sizeof(devname), "/dev/tty%c0", index + 'C');
280 	return (devname);
281 }
282