Linux console tools development discussion
 help / color / mirror / Atom feed
From: Dustin Kirkland <kirkland@ubuntu.com>
To: kbd@lists.altlinux.org
Cc: Michael Schutte <michi@debian.org>
Subject: [kbd] Feature Request: add the setvtrgb utility
Date: Mon, 28 Mar 2011 15:29:06 -0500
Message-ID: <AANLkTi=pvp8zcCLEvwTuFE3EMmavA4OPD6Pi-exHWBax@mail.gmail.com> (raw)
In-Reply-To: <AANLkTinA-dur8+mqEpMLHorD-OOPCJZiDTk3fO+X68Op@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1956 bytes --]

Howdy!

We're carrying a new feature in Ubuntu's kbd package, provided by a
neat little utility called /sbin/setvtrgb.

I recently became aware of a nifty little ioctl, ioctl(fd, PIO_CMAP,
cmap), which is able to dynamically modify the console colors in all
virtual terminals.

Providing this utility gives distributions, as well as interested
system administrators, a really convenient way to make the console's
color scheme easier on the eyes, or higher contrast for the
vision-impaired.

See the included manpage for detailed operation.

As for values, we are using the following values selected by the
Ubuntu Design Team, which are within the current Ubuntu color scheme:

/etc/vtrgb:
1,222,57,255,0,118,44,204,128,255,0,255,0,255,0,255
1,56,181,199,111,38,181,204,128,0,255,255,0,0,255,255
1,43,74,6,184,113,233,204,128,0,0,0,255,255,255,255

Alternatively, the stock, default Linux kernel's VGA values are:
0,170,0,170,0,170,0,170,85,255,85,255,85,255,85,255
0,0,170,85,0,0,170,170,85,85,255,255,85,85,255,255
0,0,0,0,170,170,170,170,85,85,85,85,255,255,255,255

You can place these values, or a modified version of them in a file,
and then run "sudo setvtrgb [FILE]" to test this.  The three lines of
configuration represent 16 red values, 16 green values, and 16 blue
values.  The format matches those exported by the kernel in sysfs.
See:
 $ cat /sys/module/vt/parameters/default_{red,grn,blu}

I'm attaching a patch.  You may choose to put something like the
following in a changelog entry if you apply my patch:

[ Dustin Kirkland <kirkland@ubuntu.com> ]
 * src/setvtrgb.c, man/man8/setvtrgb.8, COPYING, debian/rules,
   debian/copyright: LP: #730672
   - Add a 'setvtrgb' utility to configure vt/console colors
   - add to udeb (so that this can improve installer usability)
   - add to /sbin, run by root
   - update copyright statement


--
:-Dustin

Dustin Kirkland
Ubuntu Core Developer

[-- Attachment #2: kbd-setvtrgb.patch --]
[-- Type: text/x-patch, Size: 7903 bytes --]

diff -u kbd-1.15/debian/copyright kbd-1.15/debian/copyright
--- kbd-1.15/debian/copyright
+++ kbd-1.15/debian/copyright
@@ -63,0 +64,6 @@
+
+Files: setvtrgb.c
+Copyright © 2011 Canonical Ltd.
+License: GPL-2+
+Authors: Seth Forshee <seth.forshee@canonical.com>
+         Dustin Kirkland <kirkland@canonical.com>
diff -u kbd-1.15/debian/rules kbd-1.15/debian/rules
--- kbd-1.15/debian/rules
+++ kbd-1.15/debian/rules
@@ -4,12 +4,12 @@
 KBD = $(TOPDIR)/debian/kbd
 KBD_UDEB = $(TOPDIR)/debian/kbd-udeb
 
-UDEB_BINARIES = loadkeys kbd_mode setfont
+UDEB_BINARIES = loadkeys kbd_mode setfont setvtrgb
 EXTRA_BINARIES = screendump setlogcons setvesablank
 CONTRIB_BINARIES = codepage splitfont vcstime
 
 MOVE_TO_BIN = kbd_mode setfont fgconsole openvt chvt dumpkeys unicode_start loadkeys
-MOVE_TO_SBIN = kbdrate
+MOVE_TO_SBIN = kbdrate setvtrgb
 MOVE_TO_USR_SBIN = vcstime setvesablank
 
 configure: configure-stamp
only in patch2:
unchanged:
--- kbd-1.15.orig/COPYING
+++ kbd-1.15/COPYING
@@ -28,6 +28,12 @@
 (and changes to earlier mentioned programs)
 are Copyright (C) 1994-1999 Andries E. Brouwer.
 
+The file
+	setvtrgb.c
+is Copyright (C) 2011 Canonical Ltd.
+     Authors: Seth Forshee <seth.forshee@canonical.com>
+              Dustin Kirkland <kirkland@canonical.com>
+
 All files in this package may be freely copied under the terms
 of the GNU General Public License (GPL), version 2, or at your
 option any later version - except possibly for the restrictions
only in patch2:
unchanged:
--- kbd-1.15.orig/man/man8/setvtrgb.8
+++ kbd-1.15/man/man8/setvtrgb.8
@@ -0,0 +1,21 @@
+.\" @(#)man/man8/setvtrgb.8	1.0 Mar  3 12:32:18 CST 2011
+.TH SETVTRGB 8 "3 Mar 2011" "Set Virtual Terminal RGB Colors"
+.SH NAME
+setvtrgb \- set the virtual terminal RGB colors
+.SH SYNOPSIS
+.B setvtrgb
+.I vga|[FILE]
+.SH DESCRIPTION
+The
+.I setvtrgb
+command takes a single argument, either the string "vga", or a path to a file containing the red, green, and blue colors to be used by the Linux virtual terminals.
+
+If you use the FILE parameter, FILE should be exactly 3 lines of 16 comma-separated decimal values for RED, GREEN, and BLUE.
+
+To seed a valid FILE:
+  cat /sys/module/vt/parameters/default_{red,grn,blu} > FILE
+
+And then edit the values in FILE.
+
+.SH AUTHOR
+This manpage and the utility were written by Dustin Kirkland <kirkland@canonical.com> for Ubuntu systems (but may be used by others).   Permission is  granted to copy, distribute and/or modify this document and the utility under the terms of the GNU General Public License, Version 2 or any later version published by the Free Software Foundation.  The complete text  of  the  GNU  General  Public  License  can  be  found  in  /usr/share/common-licenses/GPL-2  on  Debian/Ubuntu  systems, or on the web at http://www.gnu.org/licenses/gpl2.txt.
only in patch2:
unchanged:
--- kbd-1.15.orig/man/man8/Makefile.am
+++ kbd-1.15/man/man8/Makefile.am
@@ -1,6 +1,6 @@
 gen_MANS = loadunimap.8 mapscrn.8 setfont.8
 dist_man_MANS = getkeycodes.8 kbdrate.8 resizecons.8 setkeycodes.8 \
-		showconsolefont.8 $(gen_MANS)
+		showconsolefont.8 setvtrgb.8 $(gen_MANS)
 
 install-data-hook:
 	cd $(DESTDIR)$(mandir)/man8 && \
only in patch2:
unchanged:
--- kbd-1.15.orig/src/Makefile.am
+++ kbd-1.15/src/Makefile.am
@@ -4,7 +4,7 @@
 PROGS = \
 	dumpkeys loadkeys showkey setfont showconsolefont \
 	setleds setmetamode kbd_mode chvt deallocvt \
-	psfxtable kbdrate fgconsole openvt
+	psfxtable kbdrate fgconsole openvt setvtrgb
 
 if KEYCODES_PROGS
 PROGS += getkeycodes setkeycodes
@@ -54,6 +54,7 @@
 screendump_SOURCES      = $(ALL_S) screendump.c $(XMAL_S)
 setfont_SOURCES         = $(ALL_S) setfont.c $(FIND_S) $(XMAL_S) $(GETFD_S) mapscrn.c $(KDMA_S) $(PSF_S) $(UTF8_S) kdfontop.c kdfontop.h $(UNIM_S)
 setkeycodes_SOURCES     = $(ALL_S) setkeycodes.c $(GETFD_S)
+setvtrgb_SOURCES        = $(ALL_S) setvtrgb.c $(GETFD_S)
 setlogcons_SOURCES      = $(ALL_S) setlogcons.c $(GETFD_S)
 setpalette_SOURCES      = $(ALL_S) setpalette.c $(GETFD_S)
 setvesablank_SOURCES    = $(ALL_S) setvesablank.c $(GETFD_S)
only in patch2:
unchanged:
--- kbd-1.15.orig/src/setvtrgb.c
+++ kbd-1.15/src/setvtrgb.c
@@ -0,0 +1,136 @@
+/*
+ *    setvtrgb - set the virtual terminal RGB colors
+ *    Copyright (C) 2011 Canonical Ltd.
+ *
+ *    Authors: Seth Forshee <seth.forshee@canonical.com>
+ *	       Dustin Kirkland <kirkland@canonical.com>
+ *
+ *    This program is free software; you can redistribute it and/or modify
+ *    it under the terms of the GNU General Public License as published by
+ *    the Free Software Foundation; either version 2 of the License, or
+ *    (at your option) any later version.
+ *   
+ *    This program is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU General Public License for more details.
+ * 
+ *    You should have received a copy of the GNU General Public License along
+ *    with this program; if not, write to the Free Software Foundation, Inc.,
+ *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <linux/kd.h>
+#include <errno.h>
+
+/* Standard VGA terminal colors, matching those hardcoded in the Linux kernel's
+ * drivers/tty/vt/vt.c
+ */
+unsigned char vga_colors[] = {
+	0x00, 0x00, 0x00,
+	0xaa, 0x00, 0x00,
+	0x00, 0xaa, 0x00,
+	0xaa, 0x55, 0x00,
+	0x00, 0x00, 0xaa,
+	0xaa, 0x00, 0xaa,
+	0x00, 0xaa, 0xaa,
+	0xaa, 0xaa, 0xaa,
+	0x55, 0x55, 0x55,
+	0xff, 0x55, 0x55,
+	0x55, 0xff, 0x55,
+	0xff, 0xff, 0x55,
+	0x55, 0x55, 0xff,
+	0xff, 0x55, 0xff,
+	0x55, 0xff, 0xff,
+	0xff, 0xff, 0xff,
+};
+
+/* Usage statement */
+void usage(void) {
+	fprintf(stderr,
+"\nUSAGE: setvtrgb vga|[FILE]\n"
+"\n"
+"If you use the FILE parameter, FILE should be exactly 3 lines of\n"
+"comma-separated decimal values for RED, GREEN, and BLUE.\n"
+"\n"
+"To seed a valid FILE:\n"
+"  cat /sys/module/vt/parameters/default_{red,grn,blu} > FILE\n"
+"\n"
+"And then edit the values in FILE.\n\n"
+);
+	exit(EINVAL);
+}
+
+int main(int argc, const char *argv[]) {
+	int fd;
+	FILE *fp = NULL;
+	int i, j;
+	char *c;
+	unsigned char *cmap;
+	char s[1024];
+
+	/* Must have at least one argument */
+	if (argc != 2)
+		usage();
+
+	if (strcmp(argv[1], "vga") == 0)
+		/* Use the Standard VGA colors */
+		cmap = vga_colors;
+	else if (fp = fopen(argv[1], "r" )) {
+		/* Use a color palette specified in a file */
+		/* Allocate a 48 character array */
+		cmap = calloc(3*16, sizeof(unsigned char));
+		for (i=0; i<3; i++) {
+			j = 0;
+			/* Retrieve a line from file */
+			/* Tokenize the CSV string, convert to int, then to char */
+			if (fgets(s, sizeof(s), fp)) {
+				if ((c = strtok(s, ",")) != NULL)
+					cmap[i+j*3] = (unsigned char)atoi(c);
+				else {
+					fprintf(stderr, "ERROR: Error reading color value at line [%d], position [%d], file [%s]\n", i+1, 1, argv[1]);
+					usage();
+				}
+				for (j=1; j<16; j++) {
+					if ((c = strtok(NULL, ",")) != NULL)
+						cmap[i+j*3] = (unsigned char)atoi(c);
+					else {
+						fprintf(stderr, "ERROR: Error reading color value at line [%d], position [%d], file [%s]\n", i+1, j+1, argv[1]);
+						usage();
+					}
+				}
+			} else {
+				fprintf(stderr, "ERROR: Invalid file at [%s]\n", argv[1]);
+				usage();
+			}
+		}
+		fclose(fp);
+	} else {
+		perror(argv[1]);
+		usage();
+	}
+
+	/* Ensure that this user can open tty, before going further */
+	fd = open("/dev/tty0", O_RDONLY);
+	if (fd < 0) {
+		perror(argv[1]);
+		exit(errno);
+	}
+
+	/* Apply the color map to the tty via ioctl */
+	if (ioctl(fd, PIO_CMAP, cmap) < 0) {
+		perror("ioctl");
+		exit(errno);
+	}
+	close(fd);
+	return(0);
+}

       reply	other threads:[~2011-03-28 20:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-03-28 20:29 ` Dustin Kirkland [this message]
2011-04-01 23:39   ` Alexey Gladkov
2011-04-04 14:12       ` Alexey Gladkov
2011-04-04 14:49           ` Alexey Gladkov
2011-04-04 15:02             ` Dustin Kirkland
2011-04-04 16:38           ` Michael Schutte

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='AANLkTi=pvp8zcCLEvwTuFE3EMmavA4OPD6Pi-exHWBax@mail.gmail.com' \
    --to=kirkland@ubuntu.com \
    --cc=kbd@lists.altlinux.org \
    --cc=michi@debian.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux console tools development discussion

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://lore.altlinux.org/kbd/0 kbd/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 kbd kbd/ http://lore.altlinux.org/kbd \
		kbd@lists.altlinux.org kbd@lists.altlinux.ru kbd@lists.altlinux.com
	public-inbox-index kbd

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://lore.altlinux.org/org.altlinux.lists.kbd


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git