2 * setleds.c - aeb, 940130, 940909, 991008
4 * Call: setleds [-L] [-D] [-F] [{+|-}{num|caps|scroll}]*
5 * will set or clear the indicated flags on the stdin tty,
6 * and report the settings before and after.
7 * In particular, setleds without arguments will only report.
13 #include <sys/ioctl.h>
26 " setleds [-v] [-L] [-D] [-F] [[+|-][ num | caps | scroll %s]]\n"
28 " setleds +caps -num\n"
29 "will set CapsLock, clear NumLock and leave ScrollLock unchanged.\n"
30 "The settings before and after the change (if any) are reported\n"
31 "when the -v option is given or when no change is requested.\n"
32 "Normally, setleds influences the vt flag settings\n"
33 "(and these are usually reflected in the leds).\n"
34 "With -L, setleds only sets the leds, and leaves the flags alone.\n"
35 "With -D, setleds sets both the flags and the default flags, so\n"
36 "that a subsequent reset will not change the flags.\n"
47 #define onoff(a) ((a) ? _("on ") : _("off"))
49 /* report the bits, in the order seen on the (my) keyboard */
57 printf("NumLock %s Compose %s ScrollLock %s CapsLock %s\n",
58 onoff(leds & LED_NLOCK),
59 onoff(leds & LED_CMPOSE),
60 onoff(leds & LED_SCRLCK),
61 onoff(leds & LED_CLOCK));
66 printf("NumLock %s CapsLock %s ScrollLock %s\n",
67 onoff(leds & LED_NUM),
68 onoff(leds & LED_CAP),
69 onoff(leds & LED_SCR));
77 { "scroll", LED_SCR, LED_SCRLCK },
78 { "num", LED_NUM, LED_NLOCK },
79 { "caps", LED_CAP, LED_CLOCK },
81 { "compose", 0, LED_CMPOSE }
87 if (ioctl(0, KDGETLED, leds)) {
90 _("Error reading current led setting. Maybe stdin is not a VT?\n"));
97 if (ioctl(0, KDSETLED, leds)) {
105 getflags(char *flags) {
106 if (ioctl(0, KDGKBLED, flags)) {
109 _("Error reading current flags setting. "
110 "Maybe you are not on the console?\n"));
115 static int sunkbdfd = -1;
118 sungetleds(char *leds) {
120 if (ioctl(sunkbdfd, KIOCGLED, leds)) {
123 _("Error reading current led setting from /dev/kbd.\n"));
127 fprintf(stderr, _("KIOCGLED unavailable?\n"));
133 sunsetleds(char *leds) {
135 if (ioctl(sunkbdfd, KIOCSLED, leds)) {
138 _("Error reading current led setting from /dev/kbd.\n"));
142 fprintf(stderr, _("KIOCSLED unavailable?\n"));
148 main(int argc, char **argv) {
149 int optL = 0, optD = 0, optF = 0, verbose = 0;
150 char oleds, nleds, oflags, nflags, odefflags, ndefflags;
151 char nval, ndef, sign;
152 char osunleds, nsunleds, nsunval, nsundef;
156 set_progname(argv[0]);
158 setlocale(LC_ALL, "");
159 bindtextdomain(PACKAGE, LOCALEDIR);
162 if (argc == 2 && (!strcmp("-V", argv[1]) || !strcmp("--version", argv[1])))
163 print_version_and_exit();
166 sunkbdfd = open("/dev/kbd", O_RDONLY);
169 fprintf(stderr, _("Error opening /dev/kbd.\n"));
177 sungetleds(&osunleds);
180 if (!strcmp("-L", argv[1]))
182 else if (!strcmp("-D", argv[1]))
184 else if (!strcmp("-F", argv[1]))
186 else if (!strcmp("-v", argv[1]))
194 odefflags = ndefflags = ((oflags >> 4) & 7);
195 oflags = nflags = (oflags & 7);
200 if (setleds(nleds)) {
201 fprintf(stderr, _("Error resetting ledmode\n"));
206 /* If nothing to do, report, even if not verbose */
207 if (!optD && !optL && !optF)
208 optD = optL = optF = 1;
210 printf(_("Current default flags: "));
214 printf(_("Current flags: "));
218 printf(_("Current leds: "));
236 sign = 1; /* by default: set */
239 else if(*ap == '-') {
243 for (lp = leds; lp-leds < sizeof(leds)/sizeof(leds[0]); lp++) {
244 if(!strcmp(ap, lp->name)) {
247 nsunval |= lp->sunbit;
250 nsundef |= lp->sunbit;
254 fprintf(stderr, _("unrecognized argument: _%s_\n\n"), ap);
261 ndefflags = (odefflags & ~ndef) | nval;
263 printf(_("Old default flags: "));
265 printf(_("New default flags: "));
270 nflags = ((oflags & ~ndef) | nval);
272 printf(_("Old flags: "));
274 printf(_("New flags: "));
279 if (ioctl(0, KDSKBLED, (ndefflags << 4) | nflags)) {
286 nsunleds = (osunleds & ~nsundef) | nsunval;
288 printf(_("Old leds: "));
290 printf(_("New leds: "));
293 sunsetleds(&nsunleds);
295 nleds = (oleds & ~ndef) | nval;
297 printf(_("Old leds: "));
299 printf(_("New leds: "));