kbd.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* Usage: kbd [-r] [-t] [-l] [-i] [-c on|off] [-a enable|disable|alternate]
* [-d keyboard device]
* -r reset the keyboard as if power-up
* -t return the type of the keyboard being used
* -l return the layout of the keyboard being used,
* and the Autorepeat settings
* -i read in the default configuration file
* -c on|off turn on|off clicking
* -a enable|disable|alternate sets abort sequence
* -D autorepeat delay sets autorepeat dealy, unit in ms
* -R autorepeat rate sets autorepeat rate, unit in ms
*/
#include <stdio.h>
#include <fcntl.h>
#include <deflt.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stropts.h>
#define DEF_ABORT "KEYBOARD_ABORT="
#define DEF_CLICK "KEYCLICK="
#define DEF_RPTDELAY "REPEAT_DELAY="
#define DEF_RPTRATE "REPEAT_RATE="
static void reset(int);
static void get_type(int);
static void get_layout(int);
static void kbd_defaults(int);
static void usage(void);
static int click(char *, int);
static int abort_enable(char *, int);
static int set_repeat_delay(char *, int);
static int set_repeat_rate(char *, int);
int
{
int c, error;
char *kbdname = KBD_DEVICE;
int kbd;
extern char *optarg;
extern int optind;
switch (c) {
case 'r':
rflag++;
break;
case 't':
tflag++;
break;
case 'l':
lflag++;
break;
case 'i':
iflag++;
break;
case 'c':
cflag++;
break;
case 'a':
aflag++;
break;
case 'd':
dflag++;
break;
case 'D':
Dflag++;
break;
case 'R':
Rflag++;
break;
case '?':
errflag++;
break;
}
}
/*
* Check for valid arguments:
*
* If argument parsing failed or if there are left-over
* command line arguments, then we're done now.
*/
usage();
exit(1);
}
/*
* kbd requires that the user specify either "-i" or at least one of
* -[rtlacDR]. The "-d" option is, well, optional. We don't
* care if it's there or not.
*/
if ((iflag != 0 && rtlacDRflag != 0) ||
(iflag == 0 && rtlacDRflag == 0)) {
usage();
exit(1);
}
usage();
exit(1);
}
usage();
exit(1);
}
/*
* Open the keyboard device
*/
perror("opening the keyboard");
exit(1);
}
if (iflag) {
exit(0); /* A mutually exclusive option */
/*NOTREACHED*/
}
if (tflag)
if (lflag)
if (rflag)
return (0);
}
/*
* this routine resets the state of the keyboard as if power-up
*/
static void
{
int cmd;
cmd = KBD_CMD_RESET;
perror("kbd: ioctl error");
exit(1);
}
}
/*
* this routine gets the type of the keyboard being used
*/
static void
{
int kbd_type;
perror("ioctl (kbd type)");
exit(1);
}
switch (kbd_type) {
case KB_SUN3:
(void) printf("Type 3 Sun keyboard\n");
break;
case KB_SUN4:
(void) printf("Type 4 Sun keyboard\n");
break;
case KB_ASCII:
(void) printf("ASCII\n");
break;
case KB_PC:
(void) printf("PC\n");
break;
case KB_USB:
(void) printf("USB keyboard\n");
break;
default:
(void) printf("Unknown keyboard type\n");
break;
}
}
/*
* this routine gets the layout of the keyboard being used
* also, included the autorepeat delay and rate being used
*/
static void
get_layout(int kbd)
{
int kbd_type;
int kbd_layout;
/* these two variables are used for getting delay&rate */
perror("ioctl (kbd type)");
exit(1);
}
perror("ioctl (kbd layout)");
exit(1);
}
(void) printf("type=%d\nlayout=%d (0x%.2x)\n",
/* below code is used to get the autorepeat delay and rate */
perror("ioctl (kbd get repeat delay)");
exit(1);
}
perror("ioctl (kbd get repeat rate)");
exit(1);
}
}
/*
* this routine enables or disables clicking of the keyboard
*/
static int
{
int cmd;
cmd = KBD_CMD_CLICK;
else {
usage();
return (1);
}
perror("kbd ioctl (keyclick)");
return (1);
}
return (0);
}
/*
*/
static int
{
int enable;
else {
usage();
return (1);
}
perror("kbd ioctl (abort enable)");
return (1);
}
return (0);
}
/*
* this routine set autorepeat delay
*/
static int
{
/*
* The error message depends on the different inputs.
* a. the input is a invalid integer(unit in ms)
* b. the input is a integer less than the minimal delay setting.
* The condition (a) has been covered by main function and set_default
* function.
*/
if (delay < KIOCRPTDELAY_MIN)
else
perror("kbd: set repeat delay");
return (1);
}
return (0);
}
/*
* this routine set autorepeat rate
*/
static int
{
/*
* The error message depends on the different inputs.
* a. the input is a invalid integer(unit in ms)
* b. the input is a integer less than the minimal rate setting.
* The condition (a) has been covered by main function and set_default
* function.
*/
if (rate < KIOCRPTRATE_MIN)
else
perror("kbd: set repeat rate");
return (1);
}
return (0);
}
#define BAD_DEFAULT "kbd: bad default value for %s: %s\n"
static void
kbd_defaults(int kbd)
{
char *p;
DEF_FILE);
exit(1);
}
if (p != NULL) {
/*
* KEYCLICK must equal "on" or "off"
*/
else
}
if (p != NULL) {
/*
* ABORT must equal "enable", "disable" or "alternate"
*/
if ((strcmp(p, "enable") == 0) ||
(strcmp(p, "alternate") == 0) ||
(strcmp(p, "disable") == 0))
(void) abort_enable(p, kbd);
else
}
p = defread(DEF_RPTDELAY);
if (p != NULL) {
/*
* REPEAT_DELAY unit in ms
*/
if (atoi(p) > 0)
(void) set_repeat_delay(p, kbd);
else
}
p = defread(DEF_RPTRATE);
if (p != NULL) {
/*
* REPEAT_RATE unit in ms
*/
if (atoi(p) > 0)
(void) set_repeat_rate(p, kbd);
else
}
}
static char *usage1 = "kbd [-r] [-t] [-l] [-a enable|disable|alternate]";
static char *usage2 = " [-c on|off][-D delay][-R rate][-d keyboard device]";
static char *usage3 = "kbd -i [-d keyboard device]";
static void
usage(void)
{
usage3);
}