xref: /netbsd-src/sbin/wsconsctl/display.c (revision 7d8ccb8ed09fff77ff0a670e8a7472737206e9e9)
1 /*	$NetBSD: display.c,v 1.19 2024/10/20 13:49:41 mlelstv Exp $ */
2 
3 /*-
4  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Juergen Hannken-Illjes.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/ioctl.h>
33 #include <sys/time.h>
34 
35 #include <dev/wscons/wsconsio.h>
36 
37 #include <err.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include "wsconsctl.h"
44 
45 static int border;
46 static int dpytype;
47 static struct wsdisplay_usefontdata font;
48 static struct wsdisplay_getfont gfont;
49 static char fontname_buf[256];
50 static struct wsdisplay_param backlight;
51 static struct wsdisplay_param brightness;
52 static struct wsdisplay_param contrast;
53 static struct wsdisplay_scroll_data scroll_l;
54 static struct wsdisplayio_edid_info edid_info;
55 static int msg_default_attrs, msg_default_bg, msg_default_fg;
56 static int msg_kernel_attrs, msg_kernel_bg, msg_kernel_fg;
57 static int splash_enable, splash_progress;
58 
59 struct field display_field_tab[] = {
60     { "border",			&border,	FMT_COLOR,	0 },
61     { "type",			&dpytype,	FMT_DPYTYPE,	FLG_RDONLY },
62     { "font",			&font.name,	FMT_STRING,	0 },
63     { "backlight",		&backlight.curval,  FMT_UINT,	0 },
64     { "brightness",		&brightness.curval, FMT_UINT,	FLG_MODIFY },
65     { "contrast",		&contrast.curval,   FMT_UINT,	FLG_MODIFY },
66     { "scroll.fastlines",	&scroll_l.fastlines, FMT_UINT,	FLG_MODIFY },
67     { "scroll.slowlines",	&scroll_l.slowlines, FMT_UINT,	FLG_MODIFY },
68     { "edid",			&edid_info, FMT_EDID,		FLG_RDONLY|FLG_NOAUTO },
69     { "msg.default.attrs",	&msg_default_attrs, FMT_ATTRS,	0 },
70     { "msg.default.bg",		&msg_default_bg, FMT_COLOR,	0 },
71     { "msg.default.fg",		&msg_default_fg, FMT_COLOR,	0 },
72     { "msg.kernel.attrs",	&msg_kernel_attrs, FMT_ATTRS,	0 },
73     { "msg.kernel.bg",		&msg_kernel_bg, FMT_COLOR,	0 },
74     { "msg.kernel.fg",		&msg_kernel_fg, FMT_COLOR,	0 },
75     { "splash.enable",		&splash_enable, FMT_UINT,	FLG_WRONLY },
76     { "splash.progress",	&splash_progress, FMT_UINT,	FLG_WRONLY },
77 };
78 
79 int display_field_tab_len = sizeof(display_field_tab) /
80 	sizeof(display_field_tab[0]);
81 
82 static int
83 display_get_edid(int fd, struct wsdisplayio_edid_info *ei)
84 {
85 	size_t sz = 256;
86 	int res;
87 
88 	while (sz <= 65536) {
89 		ei->buffer_size = sz;
90 		ei->edid_data = malloc(sz);
91 		if (ei->edid_data == NULL) {
92 			res = -1;
93 			break;
94 		}
95 
96 		res = ioctl(fd, WSDISPLAYIO_GET_EDID, ei);
97 		if (res == 0 || errno != EAGAIN)
98 			break;
99 
100 		free(ei->edid_data);
101 		ei->edid_data = NULL;
102 		sz *= 2;
103 	}
104 
105 	return res;
106 }
107 
108 void
109 display_get_values(int fd)
110 {
111 	if (field_by_value(&font.name)->flags & FLG_GET) {
112 		gfont.gf_name = fontname_buf;
113 		gfont.gf_size = sizeof(fontname_buf);
114 		font.name = gfont.gf_name;
115 		if (ioctl(fd, WSDISPLAYIO_GFONT, &gfont) < 0)
116 			field_disable_by_value(&font.name);
117 	}
118 
119 	if (field_by_value(&dpytype)->flags & FLG_GET)
120 		if (ioctl(fd, WSDISPLAYIO_GTYPE, &dpytype) < 0)
121 			err(EXIT_FAILURE, "WSDISPLAYIO_GTYPE");
122 
123 	if (field_by_value(&border)->flags & FLG_GET)
124 		if (ioctl(fd, WSDISPLAYIO_GBORDER, &border) < 0)
125 			field_disable_by_value(&border);
126 
127 	if (field_by_value(&backlight.curval)->flags & FLG_GET) {
128 		backlight.param = WSDISPLAYIO_PARAM_BACKLIGHT;
129 		if (ioctl(fd, WSDISPLAYIO_GETPARAM, &backlight) < 0)
130 			field_disable_by_value(&backlight.curval);
131 	}
132 
133 	if (field_by_value(&brightness.curval)->flags & FLG_GET) {
134 		brightness.param = WSDISPLAYIO_PARAM_BRIGHTNESS;
135 		if (ioctl(fd, WSDISPLAYIO_GETPARAM, &brightness))
136 			field_disable_by_value(&brightness.curval);
137 	}
138 
139 	if (field_by_value(&contrast.curval)->flags & FLG_GET) {
140 		contrast.param = WSDISPLAYIO_PARAM_CONTRAST;
141 		if (ioctl(fd, WSDISPLAYIO_GETPARAM, &contrast))
142 			field_disable_by_value(&contrast.curval);
143 	}
144 
145 	if (field_by_value(&msg_default_attrs)->flags & FLG_GET ||
146 	    field_by_value(&msg_default_bg)->flags & FLG_GET ||
147 	    field_by_value(&msg_default_fg)->flags & FLG_GET ||
148 	    field_by_value(&msg_kernel_attrs)->flags & FLG_GET ||
149 	    field_by_value(&msg_kernel_bg)->flags & FLG_GET ||
150 	    field_by_value(&msg_kernel_fg)->flags & FLG_GET) {
151 		struct wsdisplay_msgattrs ma;
152 
153 		if (ioctl(fd, WSDISPLAYIO_GMSGATTRS, &ma) < 0) {
154 			field_disable_by_value(&msg_default_attrs);
155 			field_disable_by_value(&msg_default_bg);
156 			field_disable_by_value(&msg_default_fg);
157 			field_disable_by_value(&msg_kernel_attrs);
158 			field_disable_by_value(&msg_kernel_bg);
159 			field_disable_by_value(&msg_kernel_fg);
160 		} else {
161 			msg_default_attrs = ma.default_attrs;
162 			if (ma.default_attrs & WSATTR_WSCOLORS) {
163 				msg_default_bg = ma.default_bg;
164 				msg_default_fg = ma.default_fg;
165 			} else
166 				msg_default_bg = msg_default_fg = -1;
167 
168 			msg_kernel_attrs = ma.kernel_attrs;
169 			if (ma.kernel_attrs & WSATTR_WSCOLORS) {
170 				msg_kernel_bg = ma.kernel_bg;
171 				msg_kernel_fg = ma.kernel_fg;
172 			} else
173 				msg_kernel_bg = msg_kernel_fg = -1;
174 		}
175 	}
176 
177 	if (field_by_value(&scroll_l.fastlines)->flags & FLG_GET ||
178 	    field_by_value(&scroll_l.slowlines)->flags & FLG_GET) {
179 		if (ioctl(fd, WSDISPLAYIO_DGSCROLL, &scroll_l) < 0) {
180 			field_disable_by_value(&scroll_l.fastlines);
181 			field_disable_by_value(&scroll_l.slowlines);
182 		}
183 	}
184 
185 	if (field_by_value(&edid_info)->flags & FLG_GET) {
186 		if (display_get_edid(fd, &edid_info) < 0)
187 			field_disable_by_value(&edid_info);
188 	}
189 }
190 
191 void
192 display_put_values(int fd)
193 {
194 
195 	if (field_by_value(&font.name)->flags & FLG_SET) {
196 		if (ioctl(fd, WSDISPLAYIO_SFONT, &font) < 0)
197 			err(EXIT_FAILURE, "WSDISPLAYIO_SFONT");
198 		pr_field(field_by_value(&font.name), " -> ");
199 	}
200 
201 	if (field_by_value(&border)->flags & FLG_SET) {
202 		if (ioctl(fd, WSDISPLAYIO_SBORDER, &border) < 0)
203 			err(EXIT_FAILURE, "WSDISPLAYIO_SBORDER");
204 		pr_field(field_by_value(&border), " -> ");
205 	}
206 
207 	if (field_by_value(&backlight.curval)->flags & FLG_SET) {
208 		backlight.param = WSDISPLAYIO_PARAM_BACKLIGHT;
209 		if (ioctl(fd, WSDISPLAYIO_SETPARAM, &backlight) < 0)
210 			err(EXIT_FAILURE, "WSDISPLAYIO_PARAM_BACKLIGHT");
211 		pr_field(field_by_value(&backlight.curval), " -> ");
212 	}
213 
214 	if (field_by_value(&brightness.curval)->flags & FLG_SET) {
215 		brightness.param = WSDISPLAYIO_PARAM_BRIGHTNESS;
216 		if (ioctl(fd, WSDISPLAYIO_SETPARAM, &brightness) < 0)
217 			err(EXIT_FAILURE, "WSDISPLAYIO_PARAM_BRIGHTNESS");
218 		pr_field(field_by_value(&brightness.curval), " -> ");
219 	}
220 
221 	if (field_by_value(&contrast.curval)->flags & FLG_SET) {
222 		contrast.param = WSDISPLAYIO_PARAM_CONTRAST;
223 		if (ioctl(fd, WSDISPLAYIO_SETPARAM, &contrast) < 0)
224 			err(EXIT_FAILURE, "WSDISPLAYIO_PARAM_CONTRAST");
225 		pr_field(field_by_value(&contrast.curval), " -> ");
226 	}
227 
228 	if (field_by_value(&splash_enable)->flags & FLG_SET) {
229 		if (ioctl(fd, WSDISPLAYIO_SSPLASH, &splash_enable) < 0)
230 			err(EXIT_FAILURE, "WSDISPLAYIO_SSPLASH");
231 		pr_field(field_by_value(&splash_enable), " -> ");
232 	}
233 
234 	if (field_by_value(&splash_progress)->flags & FLG_SET) {
235 		if (ioctl(fd, WSDISPLAYIO_SPROGRESS, &splash_progress) < 0)
236 			err(EXIT_FAILURE, "WSDISPLAYIO_SPROGRESS");
237 		pr_field(field_by_value(&splash_progress), " -> ");
238 	}
239 
240 	if (field_by_value(&msg_default_attrs)->flags & FLG_SET ||
241 	    field_by_value(&msg_default_bg)->flags & FLG_SET ||
242 	    field_by_value(&msg_default_fg)->flags & FLG_SET ||
243 	    field_by_value(&msg_kernel_attrs)->flags & FLG_SET ||
244 	    field_by_value(&msg_kernel_bg)->flags & FLG_SET ||
245 	    field_by_value(&msg_kernel_fg)->flags & FLG_SET) {
246 		struct wsdisplay_msgattrs ma;
247 
248 		if (ioctl(fd, WSDISPLAYIO_GMSGATTRS, &ma) < 0)
249 			err(EXIT_FAILURE, "WSDISPLAYIO_GMSGATTRS");
250 
251 		if (field_by_value(&msg_default_attrs)->flags & FLG_SET) {
252 			ma.default_attrs = msg_default_attrs;
253 			pr_field(field_by_value(&msg_default_attrs), " -> ");
254 		}
255 		if (ma.default_attrs & WSATTR_WSCOLORS) {
256 			if (field_by_value(&msg_default_bg)->flags & FLG_SET) {
257 				ma.default_bg = msg_default_bg;
258 				pr_field(field_by_value(&msg_default_bg),
259 				    " -> ");
260 			}
261 			if (field_by_value(&msg_default_fg)->flags & FLG_SET) {
262 				ma.default_fg = msg_default_fg;
263 				pr_field(field_by_value(&msg_default_fg),
264 				    " -> ");
265 			}
266 		}
267 
268 		if (field_by_value(&msg_kernel_attrs)->flags & FLG_SET) {
269 			ma.kernel_attrs = msg_kernel_attrs;
270 			pr_field(field_by_value(&msg_kernel_attrs), " -> ");
271 		}
272 		if (ma.default_attrs & WSATTR_WSCOLORS) {
273 			if (field_by_value(&msg_kernel_bg)->flags & FLG_SET) {
274 				ma.kernel_bg = msg_kernel_bg;
275 				pr_field(field_by_value(&msg_kernel_bg),
276 				    " -> ");
277 			}
278 			if (field_by_value(&msg_kernel_fg)->flags & FLG_SET) {
279 				ma.kernel_fg = msg_kernel_fg;
280 				pr_field(field_by_value(&msg_kernel_fg),
281 				    " -> ");
282 			}
283 		}
284 
285 		if (ioctl(fd, WSDISPLAYIO_SMSGATTRS, &ma) < 0)
286 			err(EXIT_FAILURE, "WSDISPLAYIO_SMSGATTRS");
287 	}
288 
289 	scroll_l.which = 0;
290 	if (field_by_value(&scroll_l.fastlines)->flags & FLG_SET)
291 		scroll_l.which |= WSDISPLAY_SCROLL_DOFASTLINES;
292 	if (field_by_value(&scroll_l.slowlines)->flags & FLG_SET)
293 		scroll_l.which |= WSDISPLAY_SCROLL_DOSLOWLINES;
294 	if (scroll_l.which != 0 &&
295 	    ioctl(fd, WSDISPLAYIO_DSSCROLL, &scroll_l) < 0)
296 		err(EXIT_FAILURE, "WSDISPLAYIO_DSSCROLL");
297 	if (scroll_l.which & WSDISPLAY_SCROLL_DOFASTLINES)
298 		pr_field(field_by_value(&scroll_l.fastlines), " -> ");
299 	if (scroll_l.which & WSDISPLAY_SCROLL_DOSLOWLINES)
300 		pr_field(field_by_value(&scroll_l.slowlines), " -> ");
301 }
302