New vt_mode tool for kbd?
Hi Alexey,
The console-setup package in Debian needs a way to check for the text/graphics mode of the active VT (from a shell script). This can be done with “setfont -v -o /dev/null”, for example, but that isn’t very elegant. Julien Cristau has proposed a small utility (which I’m calling “vt_mode” for now) to report this information in a canonical fashion.
I’ve adapted it to kbd; see attachment. Do you think this code is suitable for inclusion? If so, I am going to integrate it into the build system and write a short man page for it, so I can send you a proper patch. I’d like to hear your opinion!
All the best,
Signature · 3 lines
Michael Schutte | michi@{uiae.at,debian.org}
Innsbruck, Austria | happily accepting encrypted mail
OpenPGP: 0x16fb 517b a866 c3f6 8f11 1485 f3e4 122f 1D8C 261A/*
* vt_mode: report VT mode (text or graphics)
*
* Contributed by Julien Cristau <[email protected]> in
* http://bugs.debian.org/618573;
* adapted to kbd by Michael Schutte <[email protected]>. */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include <getopt.h>
#include "getfd.h"
#include "nls.h"
#include "version.h"
static void attr_noreturn
usage(int code) {
fprintf(stderr, _("Usage: vt_mode [-C DEVICE]\n"));
exit(code);
}int
main(int argc, char *argv[]) {
int fd, rc, mode, c;
char *console = NULL;
set_progname(argv[0]);
#ifndef __klibc__
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE_NAME, LOCALEDIR);
textdomain(PACKAGE_NAME);#endif
while ((c = getopt(argc, argv, "C:hV")) != EOF) {
switch (c) {
case 'C':
if (optarg == NULL || optarg[0] == '\0')
usage(1);
console = optarg;
break;
case 'h':
usage(0);
break;
case 'V':
print_version_and_exit();
break;
default:
usage(1);
}
}
fd = getfd(console);
rc = ioctl(fd, KDGETMODE, &mode);
if (rc < 0) {
fprintf(stderr, _("%s: Error reading VT mode.\n"), progname);
return 1;
}
switch (mode) {
case KD_TEXT:
printf("text\n");
break;
case KD_GRAPHICS:
printf("graphics\n");
break;
default:
printf("other (%d)\n", mode);
break;
}
return 0;
}Quoted 7 lines · Michael Schutte
Hi Alexey,
The console-setup package in Debian needs a way to check for the text/graphics mode of the active VT (from a shell script). This can be done with “setfont -v -o /dev/null”, for example, but that isn’t very elegant. Julien Cristau has proposed a small utility (which I’m calling “vt_mode” for now) to report this information in a canonical fashion.
I wonder whether you actually need to know what's the KD_ mode, or maybe your scripts just need yes/no answers on simple two questions:
- whether the active VT is in the text mode?
- whether the active VT is in the graphics mode?
In the latter case, I suggest to implement an utility with an option or argument specifying what mode to query, and a tty(1)-like exit status, so its use would be as simple as "if vt_mode text; then ...; fi".
Signature · 1 line
ldv
Hey,
Quoted 7 lines · Dmitry V. Levin
Quoted 5 lines · Michael Schutte
The console-setup package in Debian needs a way to check for the text/graphics mode of the active VT (from a shell script). This can be done with “setfont -v -o /dev/null”, for example, but that isn’t very elegant. Julien Cristau has proposed a small utility (which I’m calling “vt_mode” for now) to report this information in a canonical fashion.
Quoted 8 lines
I wonder whether you actually need to know what's the
KD_mode, or maybe your scripts just need yes/no answers on simple two questions:
- whether the active VT is in the text mode?
- whether the active VT is in the graphics mode?In the latter case, I suggest to implement an utility with an option or argument specifying what mode to query, and a tty(1)-like exit status, so its use would be as simple as "if
vt_modetext; then ...; fi".
Yes, that would certainly work as well; it could even do both things, depending on the presence of an argument. I don’t really care either way (it isn’t even my script, I’m just relaying for the console-setup developers). I just figured it would make sense to stick to the way kbd_mode works.
Cheers,
Signature · 3 lines
Michael Schutte | michi@{uiae.at,debian.org}
Innsbruck, Austria | happily accepting encrypted mail
OpenPGP: 0x16fb 517b a866 c3f6 8f11 1485 f3e4 122f 1D8C 261AQuoted 7 lines · Michael Schutte
Hi Alexey,
The console-setup package in Debian needs a way to check for the text/graphics mode of the active VT (from a shell script). This can be done with “setfont -v -o /dev/null”, for example, but that isn’t very elegant. Julien Cristau has proposed a small utility (which I’m calling “vt_mode” for now) to report this information in a canonical fashion.
May be something like this (see attachment) ?
I thing this utility is more script-friendly and more extensible.
Signature · 1 line
Rgrds, legion
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include <getopt.h>
#include "getfd.h"
#include "nls.h"
#include "version.h"
#define RETURN_BASE 100
#define GETMODE 10 #define GKBMODE 20 #define GKBMETA 30
static void attr_noreturn
usage(int code) {
fprintf(stderr, _("Usage: kbdinfo [-C DEVICE] GETMODE | GKBMODE | GKBMETA\n"));
exit(code);
}int
main(int argc, char *argv[]) {
int fd, mode, c, rc;
int quiet = 0;
char *console = NULL;
set_progname(argv[0]);
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE_NAME, LOCALEDIR);
textdomain(PACKAGE_NAME);
while ((c = getopt(argc, argv, "C:qhV")) != EOF) {
switch (c) {
case 'C':
if (optarg == NULL || optarg[0] == '\0')
usage(1);
console = optarg;
break;
case 'q':
quiet = 1;
break;
case 'V':
print_version_and_exit();
break;
case 'h':
usage(0);
break;
default:
usage(1);
}
}
if (argc == optind)
usage(1);
fd = getfd(console);
rc = EXIT_FAILURE;#define answer(typ,text,code) { \
if (!quiet) \
printf("%s\n", text); \
rc = (RETURN_BASE + typ + code); \
}; if (!strcasecmp("GETMODE", argv[optind])) {
if (ioctl(fd, KDGETMODE, &mode) == -1)
error(EXIT_FAILURE, errno, "ioctl");
switch (mode) {
case KD_TEXT: answer(GETMODE, "text", 1); break;
case KD_GRAPHICS: answer(GETMODE, "graphics", 2); break;
}
return rc;
}
if (!strcasecmp("GKBMODE", argv[optind])) {
if (ioctl(fd, KDGKBMODE, &mode) == -1)
error(EXIT_FAILURE, errno, "ioctl");
switch (mode) {
case K_RAW: answer(GKBMODE, "raw", 1); break;
case K_XLATE: answer(GKBMODE, "xlate", 2); break;
case K_MEDIUMRAW: answer(GKBMODE, "mediumraw", 3); break;
case K_UNICODE: answer(GKBMODE, "unicode", 4); break;
}
return rc;
}
if (!strcasecmp("GKBMETA", argv[optind])) {
if (ioctl(fd, KDGKBMETA, &mode) == -1)
error(EXIT_FAILURE, errno, "ioctl");
switch (mode) {
case K_METABIT: answer(GKBMETA, "metabit", 1); break;
case K_ESCPREFIX: answer(GKBMETA, "escprefix", 2); break;
}
return rc;
}
return rc;
}Quoted 10 lines · Alexey Gladkov
Quoted 5 lines · Michael Schutte
The console-setup package in Debian needs a way to check for the text/graphics mode of the active VT (from a shell script). This can be done with “setfont -v -o /dev/null”, for example, but that isn’t very elegant. Julien Cristau has proposed a small utility (which I’m calling “vt_mode” for now) to report this information in a canonical fashion.
May be something like this (see attachment) ?
I thing this utility is more script-friendly and more extensible.
That’s much better, of course!
Cheers,
Signature · 3 lines
Michael Schutte | michi@{uiae.at,debian.org}
Innsbruck, Austria | happily accepting encrypted mail
OpenPGP: 0x16fb 517b a866 c3f6 8f11 1485 f3e4 122f 1D8C 261AOn Fri, Mar 25, 2011 at 04:20:19PM +0300, Alexey Gladkov wrote:
[...]
Quoted 1 line
I thing this utility is more script-friendly and more extensible.
[...]
Quoted 4 lines
#define
RETURN_BASE100#define GETMODE 10 #define GKBMODE 20 #define GKBMETA 30
Multi-value exit status is not quite script-friendly, especially for values greater than 128, because exit codes greater than 128 are used by the shell to indicate that a program died due to a fatal signal.
Signature · 1 line
ldv
Quoted 3 lines · Dmitry V. Levin
Multi-value exit status is not quite script-friendly, especially for values greater than 128, because exit codes greater than 128 are used by the shell to indicate that a program died due to a fatal signal.
I do not really like the idea of creating for each ioctl by the utility.
Does anyone have any suggestions on how combine all the requests in a single utility?
On the other hand 127 return codes are sufficient to describe all the values.
Signature · 1 line
Rgrds, legion
Quoted 2 lines · Alexey Gladkov
On the other hand 127 return codes are sufficient to describe all the values.
According this:
http://tldp.org/LDP/abs/html/exitcodes.html
we have 123 non-reserved exit codes.
Signature · 1 line
Rgrds, legion
Quoted 9 lines · Alexey Gladkov
Quoted 3 lines · Dmitry V. Levin
Multi-value exit status is not quite script-friendly, especially for values greater than 128, because exit codes greater than 128 are used by the shell to indicate that a program died due to a fatal signal.
I do not really like the idea of creating for each ioctl by the utility.
Does anyone have any suggestions on how combine all the requests in a single utility?
Usage: kbdinfo [-C DEVICE] getmode [text|graphics]
kbdinfo [-C DEVICE] gkbmode [raw|xlate|mediumraw|unicode] kbdinfo [-C DEVICE] gkbmeta [metabit|escprefix]
If only one argument is given, then just print the current mode to stdout and exit 0 (unless ioctls returned an error).
If two arguments are given, then print nothing to stdout and exit 0 iff the current mode matches the mode described by these two arguments.
This interface would be both script-friendly and extensible.
Signature · 1 line
ldv
Quoted 11 lines · Dmitry V. Levin
Usage: kbdinfo [-C DEVICE] getmode [text|graphics]
kbdinfo [-C DEVICE] gkbmode [raw|xlate|mediumraw|unicode] kbdinfo [-C DEVICE] gkbmeta [metabit|escprefix]If only one argument is given, then just print the current mode to stdout and exit 0 (unless ioctls returned an error).
If two arguments are given, then print nothing to stdout and exit 0 iff the current mode matches the mode described by these two arguments.
This interface would be both script-friendly and extensible.
This is very good idea. Thanks!
Signature · 1 line
Rgrds, legion
Quoted 1 line · Dmitry V. Levin
This interface would be both script-friendly and extensible.
Ok. Another implementation. How about this ?
Signature · 1 line
Rgrds, legion
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include <getopt.h>
#include "getfd.h"
#include "nls.h"
#include "version.h"
static char *action = NULL;
static char *value = NULL;
static void attr_noreturn
usage(int code) {
fprintf(stderr, _("Usage: %s [-C DEVICE] getmode [text|graphics]\n"), progname);
fprintf(stderr, _(" or: %s [-C DEVICE] gkbmode [raw|xlate|mediumraw|unicode]\n"), progname);
fprintf(stderr, _(" or: %s [-C DEVICE] gkbmeta [metabit|escprefix]\n"), progname);
fprintf(stderr, _(" or: %s [-C DEVICE] gkbled [scrolllock|numlock|capslock]\n"), progname);
exit(code);
}static int
answer(char *ans) {
if (value)
return !strcasecmp(value, ans) ? EXIT_SUCCESS : EXIT_FAILURE;
printf("%s\n", ans);
return EXIT_SUCCESS;
}int
main(int argc, char **argv) {
int fd, mode, c;
int rc = EXIT_FAILURE;
char flags;
char *console = NULL;
set_progname(argv[0]);
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE_NAME, LOCALEDIR);
textdomain(PACKAGE_NAME);
while ((c = getopt(argc, argv, "C:hV")) != EOF) {
switch (c) {
case 'C':
if (optarg == NULL || optarg[0] == '\0')
usage(EXIT_FAILURE);
console = optarg;
break;
case 'V':
print_version_and_exit();
break;
case 'h':
usage(EXIT_SUCCESS);
break;
}
}
if (optind == argc) {
fprintf(stderr, _("Error: Not enough arguments.\n"));
exit(EXIT_FAILURE);
}
action = argv[optind++];
if (optind < argc)
value = argv[optind++];
fd = getfd(console);
if (!strcasecmp("GETMODE", action)) {
if (ioctl(fd, KDGETMODE, &mode) == -1)
error(EXIT_FAILURE, errno, "ioctl");
switch (mode) {
case KD_TEXT: rc = answer("text"); break;
case KD_GRAPHICS: rc = answer("graphics"); break;
}
} else if (!strcasecmp("GKBMODE", action)) {
if (ioctl(fd, KDGKBMODE, &mode) == -1)
error(EXIT_FAILURE, errno, "ioctl");
switch (mode) {
case K_RAW: rc = answer("raw"); break;
case K_XLATE: rc = answer("xlate"); break;
case K_MEDIUMRAW: rc = answer("mediumraw"); break;
case K_UNICODE: rc = answer("unicode"); break;
}
} else if (!strcasecmp("GKBMETA", action)) {
if (ioctl(fd, KDGKBMETA, &mode) == -1)
error(EXIT_FAILURE, errno, "ioctl");
switch (mode) {
case K_METABIT: rc = answer("metabit"); break;
case K_ESCPREFIX: rc = answer("escprefix"); break;
}
} else if (!strcasecmp("GKBLED", action)) {
if (ioctl(fd, KDGKBLED, &flags) == -1)
error(EXIT_FAILURE, errno, "ioctl");
mode = (flags & 0x7);
if (value) {
if (((mode & LED_SCR) && !strcasecmp(value, "scrolllock")) ||
((mode & LED_NUM) && !strcasecmp(value, "numlock")) ||
((mode & LED_CAP) && !strcasecmp(value, "capslock")))
rc = EXIT_SUCCESS;
} else {
printf("scrolllock:%s ", (mode & LED_SCR) ? "on" : "off");
printf("numlock:%s ", (mode & LED_NUM) ? "on" : "off");
printf("capslock:%s\n", (mode & LED_CAP) ? "on" : "off");
rc = EXIT_SUCCESS;
}
} else {
fprintf(stderr, _("Error: Unrecognized action: %s\n"), action);
}
close(fd);
return rc;
}Quoted 4 lines · Alexey Gladkov
Quoted 1 line · Dmitry V. Levin
This interface would be both script-friendly and extensible.
Ok. Another implementation. How about this ?
Fine with me.
Quoted 2 lines
static int
answer(char *ans){
I'd change this and some others "char *" to "const char *", but it's not important.
Quoted 1 line
return !strcasecmp(value, ans) ? EXIT_SUCCESS : EXIT_FAILURE;
I'd code it a bit differently:
return strcasecmp(value, ans) ? EXIT_FAILURE : EXIT_SUCCESS;
Signature · 1 line
ldv
Alexey Gladkov
AppliedQuoted 1 line · Dmitry V. Levin
Fine with me.
http://git.altlinux.org/people/legion/packages/kbd.git?p=kbd.git;a=commitdiff;h=4578bbc6
Applied. Thanks, Dmitry for your advices!
Signature · 1 line
Rgrds, legion