xref: /titanic_54/usr/src/cmd/kbd/kbd.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  *	Usage: kbd [-r] [-t] [-l] [-i] [-c on|off] [-a enable|disable|alternate]
32*7c478bd9Sstevel@tonic-gate  *		    [-d keyboard device]
33*7c478bd9Sstevel@tonic-gate  *	-r			reset the keyboard as if power-up
34*7c478bd9Sstevel@tonic-gate  *	-t			return the type of the keyboard being used
35*7c478bd9Sstevel@tonic-gate  *	-l			return the layout of the keyboard being used,
36*7c478bd9Sstevel@tonic-gate  *				and the Autorepeat settings
37*7c478bd9Sstevel@tonic-gate  *	-i			read in the default configuration file
38*7c478bd9Sstevel@tonic-gate  *	-c on|off		turn on|off clicking
39*7c478bd9Sstevel@tonic-gate  *	-a enable|disable|alternate	sets abort sequence
40*7c478bd9Sstevel@tonic-gate  *	-D autorepeat delay	sets autorepeat dealy, unit in ms
41*7c478bd9Sstevel@tonic-gate  *	-R autorepeat rate	sets autorepeat rate, unit in ms
42*7c478bd9Sstevel@tonic-gate  *	-d keyboard device	chooses the kbd device, default /dev/kbd.
43*7c478bd9Sstevel@tonic-gate  */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
47*7c478bd9Sstevel@tonic-gate #include <sys/kbio.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/kbd.h>
49*7c478bd9Sstevel@tonic-gate #include <stdio.h>
50*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
51*7c478bd9Sstevel@tonic-gate #include <deflt.h>
52*7c478bd9Sstevel@tonic-gate #include <unistd.h>
53*7c478bd9Sstevel@tonic-gate #include <string.h>
54*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
55*7c478bd9Sstevel@tonic-gate #include <stropts.h>
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #define	KBD_DEVICE	"/dev/kbd"		/* default keyboard device */
58*7c478bd9Sstevel@tonic-gate #define	DEF_FILE	"/etc/default/kbd"	/* kbd defaults file	*/
59*7c478bd9Sstevel@tonic-gate #define	DEF_ABORT	"KEYBOARD_ABORT="
60*7c478bd9Sstevel@tonic-gate #define	DEF_CLICK	"KEYCLICK="
61*7c478bd9Sstevel@tonic-gate #define	DEF_RPTDELAY	"REPEAT_DELAY="
62*7c478bd9Sstevel@tonic-gate #define	DEF_RPTRATE	"REPEAT_RATE="
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate static void reset(int);
65*7c478bd9Sstevel@tonic-gate static void get_type(int);
66*7c478bd9Sstevel@tonic-gate static void get_layout(int);
67*7c478bd9Sstevel@tonic-gate static void kbd_defaults(int);
68*7c478bd9Sstevel@tonic-gate static void usage(void);
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate static int click(char *, int);
71*7c478bd9Sstevel@tonic-gate static int abort_enable(char *, int);
72*7c478bd9Sstevel@tonic-gate static int set_repeat_delay(char *, int);
73*7c478bd9Sstevel@tonic-gate static int set_repeat_rate(char *, int);
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate int
76*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	int c, error;
79*7c478bd9Sstevel@tonic-gate 	int rflag, tflag, lflag, cflag, dflag, aflag, iflag, errflag,
80*7c478bd9Sstevel@tonic-gate 	    Dflag, Rflag, rtlacDRflag;
81*7c478bd9Sstevel@tonic-gate 	char *copt, *aopt, *delay, *rate;
82*7c478bd9Sstevel@tonic-gate 	char *kbdname = KBD_DEVICE;
83*7c478bd9Sstevel@tonic-gate 	int kbd;
84*7c478bd9Sstevel@tonic-gate 	extern char *optarg;
85*7c478bd9Sstevel@tonic-gate 	extern int optind;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	rflag = tflag = cflag = dflag = aflag = iflag = errflag = lflag =
88*7c478bd9Sstevel@tonic-gate 	    Dflag = Rflag = 0;
89*7c478bd9Sstevel@tonic-gate 	copt = aopt = (char *)0;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "rtlic:a:d:D:R:")) != EOF) {
92*7c478bd9Sstevel@tonic-gate 		switch (c) {
93*7c478bd9Sstevel@tonic-gate 		case 'r':
94*7c478bd9Sstevel@tonic-gate 			rflag++;
95*7c478bd9Sstevel@tonic-gate 			break;
96*7c478bd9Sstevel@tonic-gate 		case 't':
97*7c478bd9Sstevel@tonic-gate 			tflag++;
98*7c478bd9Sstevel@tonic-gate 			break;
99*7c478bd9Sstevel@tonic-gate 		case 'l':
100*7c478bd9Sstevel@tonic-gate 			lflag++;
101*7c478bd9Sstevel@tonic-gate 			break;
102*7c478bd9Sstevel@tonic-gate 		case 'i':
103*7c478bd9Sstevel@tonic-gate 			iflag++;
104*7c478bd9Sstevel@tonic-gate 			break;
105*7c478bd9Sstevel@tonic-gate 		case 'c':
106*7c478bd9Sstevel@tonic-gate 			copt = optarg;
107*7c478bd9Sstevel@tonic-gate 			cflag++;
108*7c478bd9Sstevel@tonic-gate 			break;
109*7c478bd9Sstevel@tonic-gate 		case 'a':
110*7c478bd9Sstevel@tonic-gate 			aopt = optarg;
111*7c478bd9Sstevel@tonic-gate 			aflag++;
112*7c478bd9Sstevel@tonic-gate 			break;
113*7c478bd9Sstevel@tonic-gate 		case 'd':
114*7c478bd9Sstevel@tonic-gate 			kbdname = optarg;
115*7c478bd9Sstevel@tonic-gate 			dflag++;
116*7c478bd9Sstevel@tonic-gate 			break;
117*7c478bd9Sstevel@tonic-gate 		case 'D':
118*7c478bd9Sstevel@tonic-gate 			delay = optarg;
119*7c478bd9Sstevel@tonic-gate 			Dflag++;
120*7c478bd9Sstevel@tonic-gate 			break;
121*7c478bd9Sstevel@tonic-gate 		case 'R':
122*7c478bd9Sstevel@tonic-gate 			rate = optarg;
123*7c478bd9Sstevel@tonic-gate 			Rflag++;
124*7c478bd9Sstevel@tonic-gate 			break;
125*7c478bd9Sstevel@tonic-gate 		case '?':
126*7c478bd9Sstevel@tonic-gate 			errflag++;
127*7c478bd9Sstevel@tonic-gate 			break;
128*7c478bd9Sstevel@tonic-gate 		}
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	/*
132*7c478bd9Sstevel@tonic-gate 	 * Check for valid arguments:
133*7c478bd9Sstevel@tonic-gate 	 *
134*7c478bd9Sstevel@tonic-gate 	 * If argument parsing failed or if there are left-over
135*7c478bd9Sstevel@tonic-gate 	 * command line arguments, then we're done now.
136*7c478bd9Sstevel@tonic-gate 	 */
137*7c478bd9Sstevel@tonic-gate 	if (errflag != 0 || argc != optind) {
138*7c478bd9Sstevel@tonic-gate 		usage();
139*7c478bd9Sstevel@tonic-gate 		exit(1);
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 	/*
142*7c478bd9Sstevel@tonic-gate 	 * kbd requires that the user specify either "-i" or at least one of
143*7c478bd9Sstevel@tonic-gate 	 * -[rtlacDR].  The "-d" option is, well, optional.  We don't
144*7c478bd9Sstevel@tonic-gate 	 * care if it's there or not.
145*7c478bd9Sstevel@tonic-gate 	 */
146*7c478bd9Sstevel@tonic-gate 	rtlacDRflag = rflag + tflag + lflag + aflag + cflag + Dflag + Rflag;
147*7c478bd9Sstevel@tonic-gate 	if ((iflag != 0 && rtlacDRflag != 0) ||
148*7c478bd9Sstevel@tonic-gate 	    (iflag == 0 && rtlacDRflag == 0)) {
149*7c478bd9Sstevel@tonic-gate 		usage();
150*7c478bd9Sstevel@tonic-gate 		exit(1);
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	if (Dflag && atoi(delay) <= 0) {
154*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Invalid arguments: -D %s\n", delay);
155*7c478bd9Sstevel@tonic-gate 		usage();
156*7c478bd9Sstevel@tonic-gate 		exit(1);
157*7c478bd9Sstevel@tonic-gate 	}
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	if (Rflag && atoi(rate) <= 0) {
160*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Invalid arguments: -R %s\n", rate);
161*7c478bd9Sstevel@tonic-gate 		usage();
162*7c478bd9Sstevel@tonic-gate 		exit(1);
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	/*
166*7c478bd9Sstevel@tonic-gate 	 * Open the keyboard device
167*7c478bd9Sstevel@tonic-gate 	 */
168*7c478bd9Sstevel@tonic-gate 	if ((kbd = open(kbdname, O_RDWR)) < 0) {
169*7c478bd9Sstevel@tonic-gate 		perror("opening the keyboard");
170*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "kbd: Cannot open %s\n", kbdname);
171*7c478bd9Sstevel@tonic-gate 		exit(1);
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	if (iflag) {
175*7c478bd9Sstevel@tonic-gate 		kbd_defaults(kbd);
176*7c478bd9Sstevel@tonic-gate 		exit(0);	/* A mutually exclusive option */
177*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if (tflag)
181*7c478bd9Sstevel@tonic-gate 		get_type(kbd);
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	if (lflag)
184*7c478bd9Sstevel@tonic-gate 		get_layout(kbd);
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	if (cflag && (error = click(copt, kbd)) != 0)
187*7c478bd9Sstevel@tonic-gate 		exit(error);
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate 	if (rflag)
190*7c478bd9Sstevel@tonic-gate 		reset(kbd);
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	if (aflag && (error = abort_enable(aopt, kbd)) != 0)
193*7c478bd9Sstevel@tonic-gate 		exit(error);
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate 	if (Dflag && (error = set_repeat_delay(delay, kbd)) != 0)
196*7c478bd9Sstevel@tonic-gate 		exit(error);
197*7c478bd9Sstevel@tonic-gate 
198*7c478bd9Sstevel@tonic-gate 	if (Rflag && (error = set_repeat_rate(rate, kbd)) != 0)
199*7c478bd9Sstevel@tonic-gate 		exit(error);
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	return (0);
202*7c478bd9Sstevel@tonic-gate }
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate /*
205*7c478bd9Sstevel@tonic-gate  * this routine resets the state of the keyboard as if power-up
206*7c478bd9Sstevel@tonic-gate  */
207*7c478bd9Sstevel@tonic-gate static void
208*7c478bd9Sstevel@tonic-gate reset(int kbd)
209*7c478bd9Sstevel@tonic-gate {
210*7c478bd9Sstevel@tonic-gate 	int cmd;
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	cmd = KBD_CMD_RESET;
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 	if (ioctl(kbd, KIOCCMD, &cmd)) {
215*7c478bd9Sstevel@tonic-gate 		perror("kbd: ioctl error");
216*7c478bd9Sstevel@tonic-gate 		exit(1);
217*7c478bd9Sstevel@tonic-gate 	}
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate }
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate /*
222*7c478bd9Sstevel@tonic-gate  * this routine gets the type of the keyboard being used
223*7c478bd9Sstevel@tonic-gate  */
224*7c478bd9Sstevel@tonic-gate static void
225*7c478bd9Sstevel@tonic-gate get_type(int kbd)
226*7c478bd9Sstevel@tonic-gate {
227*7c478bd9Sstevel@tonic-gate 	int kbd_type;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	if (ioctl(kbd, KIOCTYPE, &kbd_type)) {
230*7c478bd9Sstevel@tonic-gate 		perror("ioctl (kbd type)");
231*7c478bd9Sstevel@tonic-gate 		exit(1);
232*7c478bd9Sstevel@tonic-gate 	}
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	switch (kbd_type) {
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	case KB_SUN3:
237*7c478bd9Sstevel@tonic-gate 		(void) printf("Type 3 Sun keyboard\n");
238*7c478bd9Sstevel@tonic-gate 		break;
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	case KB_SUN4:
241*7c478bd9Sstevel@tonic-gate 		(void) printf("Type 4 Sun keyboard\n");
242*7c478bd9Sstevel@tonic-gate 		break;
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	case KB_ASCII:
245*7c478bd9Sstevel@tonic-gate 		(void) printf("ASCII\n");
246*7c478bd9Sstevel@tonic-gate 		break;
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	case KB_PC:
249*7c478bd9Sstevel@tonic-gate 		(void) printf("PC\n");
250*7c478bd9Sstevel@tonic-gate 		break;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	case KB_USB:
253*7c478bd9Sstevel@tonic-gate 		(void) printf("USB keyboard\n");
254*7c478bd9Sstevel@tonic-gate 		break;
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 	default:
257*7c478bd9Sstevel@tonic-gate 		(void) printf("Unknown keyboard type\n");
258*7c478bd9Sstevel@tonic-gate 		break;
259*7c478bd9Sstevel@tonic-gate 	}
260*7c478bd9Sstevel@tonic-gate }
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate /*
263*7c478bd9Sstevel@tonic-gate  * this routine gets the layout of the keyboard being used
264*7c478bd9Sstevel@tonic-gate  * also, included the autorepeat delay and rate being used
265*7c478bd9Sstevel@tonic-gate  */
266*7c478bd9Sstevel@tonic-gate static void
267*7c478bd9Sstevel@tonic-gate get_layout(int kbd)
268*7c478bd9Sstevel@tonic-gate {
269*7c478bd9Sstevel@tonic-gate 	int kbd_type;
270*7c478bd9Sstevel@tonic-gate 	int kbd_layout;
271*7c478bd9Sstevel@tonic-gate 	/* these two variables are used for getting delay&rate */
272*7c478bd9Sstevel@tonic-gate 	int delay, rate;
273*7c478bd9Sstevel@tonic-gate 	delay = rate = 0;
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate 	if (ioctl(kbd, KIOCTYPE, &kbd_type)) {
276*7c478bd9Sstevel@tonic-gate 		perror("ioctl (kbd type)");
277*7c478bd9Sstevel@tonic-gate 		exit(1);
278*7c478bd9Sstevel@tonic-gate 	}
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	if (ioctl(kbd, KIOCLAYOUT, &kbd_layout)) {
281*7c478bd9Sstevel@tonic-gate 		perror("ioctl (kbd layout)");
282*7c478bd9Sstevel@tonic-gate 		exit(1);
283*7c478bd9Sstevel@tonic-gate 	}
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 	(void) printf("type=%d\nlayout=%d (0x%.2x)\n",
286*7c478bd9Sstevel@tonic-gate 	    kbd_type, kbd_layout, kbd_layout);
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 	/* below code is used to get the autorepeat delay and rate */
289*7c478bd9Sstevel@tonic-gate 	if (ioctl(kbd, KIOCGRPTDELAY, &delay)) {
290*7c478bd9Sstevel@tonic-gate 		perror("ioctl (kbd get repeat delay)");
291*7c478bd9Sstevel@tonic-gate 		exit(1);
292*7c478bd9Sstevel@tonic-gate 	}
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	if (ioctl(kbd, KIOCGRPTRATE, &rate)) {
295*7c478bd9Sstevel@tonic-gate 		perror("ioctl (kbd get repeat rate)");
296*7c478bd9Sstevel@tonic-gate 		exit(1);
297*7c478bd9Sstevel@tonic-gate 	}
298*7c478bd9Sstevel@tonic-gate 
299*7c478bd9Sstevel@tonic-gate 	(void) printf("delay(ms)=%d\n", delay);
300*7c478bd9Sstevel@tonic-gate 	(void) printf("rate(ms)=%d\n", rate);
301*7c478bd9Sstevel@tonic-gate }
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate /*
304*7c478bd9Sstevel@tonic-gate  * this routine enables or disables clicking of the keyboard
305*7c478bd9Sstevel@tonic-gate  */
306*7c478bd9Sstevel@tonic-gate static int
307*7c478bd9Sstevel@tonic-gate click(char *copt, int kbd)
308*7c478bd9Sstevel@tonic-gate {
309*7c478bd9Sstevel@tonic-gate 	int cmd;
310*7c478bd9Sstevel@tonic-gate 
311*7c478bd9Sstevel@tonic-gate 	if (strcmp(copt, "on") == 0)
312*7c478bd9Sstevel@tonic-gate 		cmd = KBD_CMD_CLICK;
313*7c478bd9Sstevel@tonic-gate 	else if (strcmp(copt, "off") == 0)
314*7c478bd9Sstevel@tonic-gate 		cmd = KBD_CMD_NOCLICK;
315*7c478bd9Sstevel@tonic-gate 	else {
316*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "wrong option -- %s\n", copt);
317*7c478bd9Sstevel@tonic-gate 		usage();
318*7c478bd9Sstevel@tonic-gate 		return (1);
319*7c478bd9Sstevel@tonic-gate 	}
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	if (ioctl(kbd, KIOCCMD, &cmd)) {
322*7c478bd9Sstevel@tonic-gate 		perror("kbd ioctl (keyclick)");
323*7c478bd9Sstevel@tonic-gate 		return (1);
324*7c478bd9Sstevel@tonic-gate 	}
325*7c478bd9Sstevel@tonic-gate 	return (0);
326*7c478bd9Sstevel@tonic-gate }
327*7c478bd9Sstevel@tonic-gate 
328*7c478bd9Sstevel@tonic-gate /*
329*7c478bd9Sstevel@tonic-gate  * this routine enables/disables/sets BRK or abort sequence feature
330*7c478bd9Sstevel@tonic-gate  */
331*7c478bd9Sstevel@tonic-gate static int
332*7c478bd9Sstevel@tonic-gate abort_enable(char *aopt, int kbd)
333*7c478bd9Sstevel@tonic-gate {
334*7c478bd9Sstevel@tonic-gate 	int enable;
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate 	if (strcmp(aopt, "alternate") == 0)
337*7c478bd9Sstevel@tonic-gate 		enable = KIOCABORTALTERNATE;
338*7c478bd9Sstevel@tonic-gate 	else if (strcmp(aopt, "enable") == 0)
339*7c478bd9Sstevel@tonic-gate 		enable = KIOCABORTENABLE;
340*7c478bd9Sstevel@tonic-gate 	else if (strcmp(aopt, "disable") == 0)
341*7c478bd9Sstevel@tonic-gate 		enable = KIOCABORTDISABLE;
342*7c478bd9Sstevel@tonic-gate 	else {
343*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "wrong option -- %s\n", aopt);
344*7c478bd9Sstevel@tonic-gate 		usage();
345*7c478bd9Sstevel@tonic-gate 		return (1);
346*7c478bd9Sstevel@tonic-gate 	}
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	if (ioctl(kbd, KIOCSKABORTEN, &enable)) {
349*7c478bd9Sstevel@tonic-gate 		perror("kbd ioctl (abort enable)");
350*7c478bd9Sstevel@tonic-gate 		return (1);
351*7c478bd9Sstevel@tonic-gate 	}
352*7c478bd9Sstevel@tonic-gate 	return (0);
353*7c478bd9Sstevel@tonic-gate }
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate /*
356*7c478bd9Sstevel@tonic-gate  * this routine set autorepeat delay
357*7c478bd9Sstevel@tonic-gate  */
358*7c478bd9Sstevel@tonic-gate static int
359*7c478bd9Sstevel@tonic-gate set_repeat_delay(char *delay_str, int kbd)
360*7c478bd9Sstevel@tonic-gate {
361*7c478bd9Sstevel@tonic-gate 	int delay = atoi(delay_str);
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	/*
364*7c478bd9Sstevel@tonic-gate 	 * The error message depends on the different inputs.
365*7c478bd9Sstevel@tonic-gate 	 * a. the input is a invalid integer(unit in ms)
366*7c478bd9Sstevel@tonic-gate 	 * b. the input is a integer less than the minimal delay setting.
367*7c478bd9Sstevel@tonic-gate 	 * The condition (a) has been covered by main function and set_default
368*7c478bd9Sstevel@tonic-gate 	 * function.
369*7c478bd9Sstevel@tonic-gate 	 */
370*7c478bd9Sstevel@tonic-gate 	if (ioctl(kbd, KIOCSRPTDELAY, &delay) == -1) {
371*7c478bd9Sstevel@tonic-gate 		if (delay < KIOCRPTDELAY_MIN)
372*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "kbd: specified delay %d is "
373*7c478bd9Sstevel@tonic-gate 			    "less than minimum %d\n", delay, KIOCRPTDELAY_MIN);
374*7c478bd9Sstevel@tonic-gate 		else
375*7c478bd9Sstevel@tonic-gate 			perror("kbd: set repeat delay");
376*7c478bd9Sstevel@tonic-gate 		return (1);
377*7c478bd9Sstevel@tonic-gate 	}
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 	return (0);
380*7c478bd9Sstevel@tonic-gate }
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate /*
383*7c478bd9Sstevel@tonic-gate  * this routine set autorepeat rate
384*7c478bd9Sstevel@tonic-gate  */
385*7c478bd9Sstevel@tonic-gate static int
386*7c478bd9Sstevel@tonic-gate set_repeat_rate(char *rate_str, int kbd)
387*7c478bd9Sstevel@tonic-gate {
388*7c478bd9Sstevel@tonic-gate 	int rate = atoi(rate_str);
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate 	/*
391*7c478bd9Sstevel@tonic-gate 	 * The error message depends on the different inputs.
392*7c478bd9Sstevel@tonic-gate 	 * a. the input is a invalid integer(unit in ms)
393*7c478bd9Sstevel@tonic-gate 	 * b. the input is a integer less than the minimal rate setting.
394*7c478bd9Sstevel@tonic-gate 	 * The condition (a) has been covered by main function and set_default
395*7c478bd9Sstevel@tonic-gate 	 * function.
396*7c478bd9Sstevel@tonic-gate 	 */
397*7c478bd9Sstevel@tonic-gate 	if (ioctl(kbd, KIOCSRPTRATE, &rate) == -1) {
398*7c478bd9Sstevel@tonic-gate 		if (rate < KIOCRPTRATE_MIN)
399*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "kbd: specified rate %d is "
400*7c478bd9Sstevel@tonic-gate 			    "less than minimum %d\n", rate, KIOCRPTRATE_MIN);
401*7c478bd9Sstevel@tonic-gate 		else
402*7c478bd9Sstevel@tonic-gate 			perror("kbd: set repeat rate");
403*7c478bd9Sstevel@tonic-gate 		return (1);
404*7c478bd9Sstevel@tonic-gate 	}
405*7c478bd9Sstevel@tonic-gate 
406*7c478bd9Sstevel@tonic-gate 	return (0);
407*7c478bd9Sstevel@tonic-gate }
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate #define	BAD_DEFAULT	"kbd: bad default value for %s: %s\n"
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate static void
412*7c478bd9Sstevel@tonic-gate kbd_defaults(int kbd)
413*7c478bd9Sstevel@tonic-gate {
414*7c478bd9Sstevel@tonic-gate 	char *p;
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	if (defopen(DEF_FILE) != 0) {
417*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "Can't open default file: %s\n",
418*7c478bd9Sstevel@tonic-gate 		    DEF_FILE);
419*7c478bd9Sstevel@tonic-gate 		exit(1);
420*7c478bd9Sstevel@tonic-gate 	}
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 	p = defread(DEF_CLICK);
423*7c478bd9Sstevel@tonic-gate 	if (p != NULL) {
424*7c478bd9Sstevel@tonic-gate 		/*
425*7c478bd9Sstevel@tonic-gate 		 * KEYCLICK must equal "on" or "off"
426*7c478bd9Sstevel@tonic-gate 		 */
427*7c478bd9Sstevel@tonic-gate 		if ((strcmp(p, "on") == 0) || (strcmp(p, "off") == 0))
428*7c478bd9Sstevel@tonic-gate 			(void) click(p, kbd);
429*7c478bd9Sstevel@tonic-gate 		else
430*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, BAD_DEFAULT, DEF_CLICK, p);
431*7c478bd9Sstevel@tonic-gate 	}
432*7c478bd9Sstevel@tonic-gate 
433*7c478bd9Sstevel@tonic-gate 	p = defread(DEF_ABORT);
434*7c478bd9Sstevel@tonic-gate 	if (p != NULL) {
435*7c478bd9Sstevel@tonic-gate 		/*
436*7c478bd9Sstevel@tonic-gate 		 * ABORT must equal "enable", "disable" or "alternate"
437*7c478bd9Sstevel@tonic-gate 		 */
438*7c478bd9Sstevel@tonic-gate 		if ((strcmp(p, "enable") == 0) ||
439*7c478bd9Sstevel@tonic-gate 		    (strcmp(p, "alternate") == 0) ||
440*7c478bd9Sstevel@tonic-gate 		    (strcmp(p, "disable") == 0))
441*7c478bd9Sstevel@tonic-gate 			(void) abort_enable(p, kbd);
442*7c478bd9Sstevel@tonic-gate 		else
443*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, BAD_DEFAULT, DEF_ABORT, p);
444*7c478bd9Sstevel@tonic-gate 	}
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate 	p = defread(DEF_RPTDELAY);
447*7c478bd9Sstevel@tonic-gate 	if (p != NULL) {
448*7c478bd9Sstevel@tonic-gate 		/*
449*7c478bd9Sstevel@tonic-gate 		 * REPEAT_DELAY unit in ms
450*7c478bd9Sstevel@tonic-gate 		 */
451*7c478bd9Sstevel@tonic-gate 		if (atoi(p) > 0)
452*7c478bd9Sstevel@tonic-gate 			(void) set_repeat_delay(p, kbd);
453*7c478bd9Sstevel@tonic-gate 		else
454*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, BAD_DEFAULT, DEF_RPTDELAY, p);
455*7c478bd9Sstevel@tonic-gate 	}
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate 	p = defread(DEF_RPTRATE);
458*7c478bd9Sstevel@tonic-gate 	if (p != NULL) {
459*7c478bd9Sstevel@tonic-gate 		/*
460*7c478bd9Sstevel@tonic-gate 		 * REPEAT_RATE unit in ms
461*7c478bd9Sstevel@tonic-gate 		 */
462*7c478bd9Sstevel@tonic-gate 		if (atoi(p) > 0)
463*7c478bd9Sstevel@tonic-gate 			(void) set_repeat_rate(p, kbd);
464*7c478bd9Sstevel@tonic-gate 		else
465*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, BAD_DEFAULT, DEF_RPTRATE, p);
466*7c478bd9Sstevel@tonic-gate 	}
467*7c478bd9Sstevel@tonic-gate }
468*7c478bd9Sstevel@tonic-gate 
469*7c478bd9Sstevel@tonic-gate static char *usage1 = "kbd [-r] [-t] [-l] [-a enable|disable|alternate]";
470*7c478bd9Sstevel@tonic-gate static char *usage2 = "    [-c on|off][-D delay][-R rate][-d keyboard device]";
471*7c478bd9Sstevel@tonic-gate static char *usage3 = "kbd -i [-d keyboard device]";
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate static void
474*7c478bd9Sstevel@tonic-gate usage(void)
475*7c478bd9Sstevel@tonic-gate {
476*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "Usage:\t%s\n\t%s\n\t%s\n", usage1, usage2,
477*7c478bd9Sstevel@tonic-gate 	    usage3);
478*7c478bd9Sstevel@tonic-gate }
479