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