1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
|
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (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
# or http://www.opensolaris.org/os/licensing.
# 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 (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
#
MANIFEST = keymap.xml
SVCMETHOD = keymap
include ../Makefile.cmd
ROOTMANIFESTDIR = $(ROOTSVCSYSTEM)
.KEEP_STATE:
PROG = kbd
CFLAGS += $(CCVERBOSE)
CERRWARN += $(CNOWARN_UNINIT)
LDLIBS += -lscf
all: $(PROG)
install: all $(ROOTPROG) $(ROOTMANIFEST) $(ROOTSVCMETHOD)
check: $(CHKMANIFEST)
clean:
lint: lint_PROG
include ../Makefile.targ
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (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
* or http://www.opensolaris.org/os/licensing.
* 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 (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Usage: kbd [-r] [-t] [-l] [-c on|off] [-a enable|disable|alternate]
* [-d keyboard device] [-A autorepeat count]
* [-D autorepeat delay] [-R autorepeat rate]
* kbd [-i] [-d keyboard device]
* kbd -s [language]
* kbd -b [keyboard|console] frequency
* -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
* -A autorepeat count sets autorepeat sequence length in chars.
* -D autorepeat delay sets autorepeat delay, unit in ms
* -R autorepeat rate sets autorepeat rate, unit in ms
* -d keyboard device chooses the kbd device, default /dev/kbd.
* -s keyboard layout sets keyboard layout
* -b [keyboard| console] frequency
* sets keyboard or console beeper frequency
*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/kbio.h>
#include <sys/kbd.h>
#include <stdio.h>
#include <fcntl.h>
#include <deflt.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stropts.h>
#include <libintl.h>
#include <locale.h>
#include <errno.h>
#include <inttypes.h>
#include <libscf.h>
#include <limits.h>
#define KBD_DEVICE "/dev/kbd" /* default keyboard device */
#define KBD_LAYOUT_FILE "/usr/share/lib/keytables/type_6/kbd_layouts"
#define MAX_LAYOUT_NUM 128
#define MAX_LINE_SIZE 256
#define DEFAULT_KBD_LAYOUT 33
#define KBD_FMRI "svc:/system/keymap:default"
#define KBD_PG "keymap"
#define KBD_PROP_LAYOUT "layout"
#define KBD_PROP_KEYCLICK "keyclick"
#define KBD_PROP_KEYBOARD_ABORT "keyboard_abort"
#define KBD_PROP_RPTDELAY "repeat_delay"
#define KBD_PROP_RPTRATE "repeat_rate"
#define KBD_PROP_RPTCOUNT "repeat_count"
#define KBD_PROP_FREQ "kbd_beeper_freq"
#define KBD_PROP_CONSFREQ "console_beeper_freq"
#define KBD_MAX_NAME_LEN 1024
char *layout_names[MAX_LAYOUT_NUM];
int layout_numbers[MAX_LAYOUT_NUM];
static int layout_count;
static int default_layout_number = 0;
static void reset(int);
static int 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_count(char *, int);
static int set_rptcount(int, int);
static int set_repeat_delay(char *, int);
static int set_rptdelay(int, int);
static int set_repeat_rate(char *, int);
static int set_rptrate(int, int);
static int get_layout_number(char *);
static int set_layout(int, int);
static int get_layouts(void);
static int set_kbd_layout(int, char *);
static int set_beep_freq(int, char *, int);
int
main(int argc, char **argv)
{
int c, error;
int rflag, tflag, lflag, cflag, dflag, aflag, iflag, errflag,
Aflag, Dflag, Rflag, rtlacADRflag, sflag, bflag;
char *copt, *aopt, *count, *delay, *rate, *layout_name, *b_type;
char *freq_str;
char *kbdname = KBD_DEVICE, *endptr = NULL;
int kbd, freq_val;
extern char *optarg;
extern int optind;
rflag = tflag = cflag = dflag = aflag = iflag = errflag = lflag =
Aflag = Dflag = Rflag = sflag = bflag = 0;
copt = aopt = NULL;
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
while ((c = getopt(argc, argv, "rtlisc:a:d:A:D:R:b:")) != EOF) {
switch (c) {
case 'r':
rflag++;
break;
case 't':
tflag++;
break;
case 'l':
lflag++;
break;
case 'i':
iflag++;
break;
case 's':
sflag++;
break;
case 'c':
copt = optarg;
cflag++;
break;
case 'a':
aopt = optarg;
aflag++;
break;
case 'd':
kbdname = optarg;
dflag++;
break;
case 'A':
count = optarg;
Aflag++;
break;
case 'D':
delay = optarg;
Dflag++;
break;
case 'R':
rate = optarg;
Rflag++;
break;
case 'b':
bflag++;
break;
case '?':
errflag++;
break;
}
}
/*
* Check for valid arguments:
*
* If argument parsing failed or if there are left-over
* command line arguments(except -s and -b option),
* then we're done now.
*/
if (errflag != 0 || (sflag == 0 && bflag == 0 && argc != optind)) {
usage();
exit(1);
}
/*
* kbd requires that the user specify either "-i" or "-s" or "-b" or
* at least one of -[rtlacADR]. The "-d" option is, well, optional.
* We don't care if it's there or not.
*/
rtlacADRflag = rflag + tflag + lflag + aflag + cflag + Aflag +
Dflag + Rflag;
if (!((iflag != 0 && sflag == 0 && bflag == 0 && rtlacADRflag == 0) ||
(iflag == 0 && sflag != 0 && bflag == 0 && dflag == 0 &&
rtlacADRflag == 0) ||
(iflag == 0 && sflag == 0 && bflag == 0 && rtlacADRflag != 0) ||
(iflag == 0 && sflag == 0 && bflag != 0 && rtlacADRflag == 0))) {
usage();
exit(1);
}
if (Aflag && atoi(count) < -1) {
(void) fprintf(stderr, "Invalid arguments: -A %s\n", count);
usage();
exit(1);
}
if (Dflag && atoi(delay) <= 0) {
(void) fprintf(stderr, "Invalid arguments: -D %s\n", delay);
usage();
exit(1);
}
if (Rflag && atoi(rate) <= 0) {
(void) fprintf(stderr, "Invalid arguments: -R %s\n", rate);
usage();
exit(1);
}
/*
* Open the keyboard device
*/
if ((kbd = open(kbdname, O_RDWR)) < 0) {
perror("opening the keyboard");
(void) fprintf(stderr, "kbd: Cannot open %s\n", kbdname);
exit(1);
}
if (iflag) {
kbd_defaults(kbd);
exit(0); /* A mutually exclusive option */
/*NOTREACHED*/
}
if (tflag)
(void) get_type(kbd);
if (lflag)
get_layout(kbd);
if (cflag && (error = click(copt, kbd)) != 0)
exit(error);
if (rflag)
reset(kbd);
if (aflag && (error = abort_enable(aopt, kbd)) != 0)
exit(error);
if (Aflag && (error = set_repeat_count(count, kbd)) != 0)
exit(error);
if (Dflag && (error = set_repeat_delay(delay, kbd)) != 0)
exit(error);
if (Rflag && (error = set_repeat_rate(rate, kbd)) != 0)
exit(error);
if (sflag) {
if (argc == optind) {
layout_name = NULL;
} else if (argc == (optind + 1)) {
layout_name = argv[optind];
} else {
usage();
exit(1);
}
if ((error = set_kbd_layout(kbd, layout_name)) != 0)
exit(error);
}
if (bflag) {
if (argc == optind) {
b_type = "keyboard";
} else if (argc == (optind + 1)) {
b_type = argv[argc - 2];
} else {
usage();
exit(1);
}
if (strcmp(b_type, "keyboard") && strcmp(b_type, "console")) {
usage();
exit(1);
}
freq_str = argv[argc - 1];
errno = 0;
freq_val = (int)strtol(freq_str, &endptr, 10);
if (errno != 0 || endptr[0] != '\0') {
usage();
exit(1);
}
if (freq_val < 0 || freq_val > INT16_MAX) {
(void) fprintf(stderr, "Invalid arguments: -b %s\n",
freq_str);
(void) fprintf(stderr, "Frequency range: [0 - %d]\n",
INT16_MAX);
exit(1);
}
if ((error = set_beep_freq(kbd, b_type, freq_val)) != 0)
exit(1);
}
return (0);
}
/*
* this routine gets the type of the keyboard being used
*/
static int
set_kbd_layout(int kbd, char *layout_name)
{
int layout_num;
int error = 1;
/* layout setting is possible only for USB type keyboards */
if (get_type(kbd) != KB_USB) {
(void) fprintf(stderr, "The -s option does not apply for this"
" keyboard type.\n"
"Only USB/PS2 type keyboards support this option.\n");
return (error);
}
/* get the language info from the layouts file */
if (get_layouts() != 0)
return (error);
if (layout_name != NULL) {
if ((layout_num = get_layout_number(layout_name)) == -1) {
(void) fprintf(stderr, "%s: unknown layout name\n"
"Please refer to 'kbd -s' to get the "
"supported layouts.\n", layout_name);
return (error);
}
} else {
int i, j, print_cnt, input_num;
boolean_t input_right = B_TRUE;
boolean_t default_input = B_FALSE;
char input[8]; /* 8 chars is enough for numbers */
print_cnt = (layout_count % 2) ?
layout_count/2+1 : layout_count/2;
for (i = 1; i <= print_cnt; i++) {
(void) printf("%2d. %-30s", i,
layout_names[i-1]);
j = i + print_cnt;
if (j <= layout_count) {
(void) printf("%-2d. %-30s\n", j,
layout_names[j-1]);
}
}
(void) printf(gettext("\nTo select the keyboard layout,"
" enter a number [default %d]:"),
default_layout_number+1);
for (;;) {
if (input_right == B_FALSE)
(void) printf(gettext("Invalid input. "
"Please input a number "
"(1,2,...):"));
(void) memset(input, 0, 8);
(void) fflush(stdin);
(void) fgets(input, 8, stdin);
if (strlen(input) > 4) {
input_right = B_FALSE;
continue;
}
if (input[0] == '\n') {
default_input = B_TRUE;
break;
}
input_right = B_TRUE;
/* check if the inputs are numbers 0~9 */
for (i = 0; i < (strlen(input) - 1); i++) {
if ((input[i] < '0') ||
(input[i] > '9')) {
input_right = B_FALSE;
break;
}
}
if (input_right == B_FALSE)
continue;
input_num = atoi(input);
if ((input_num > 0) &&
(input_num <= layout_count))
break;
else
input_right = B_FALSE;
}
if (default_input == B_TRUE)
layout_num = DEFAULT_KBD_LAYOUT;
else
layout_num = layout_numbers[--input_num];
}
if ((error = set_layout(kbd, layout_num)) != 0)
return (error);
return (0);
}
/*
* This routine sets keyboard or console beeper frequency
*/
static int
set_beep_freq(int fd, char *type, int freq)
{
struct freq_request fr_struct;
if (strcmp(type, "keyboard") == 0)
fr_struct.type = KBD_BEEP;
else if (strcmp(type, "console") == 0)
fr_struct.type = CONSOLE_BEEP;
else
return (EINVAL);
fr_struct.freq = (int16_t)freq;
return (ioctl(fd, KIOCSETFREQ, &fr_struct));
}
/*
* this routine resets the state of the keyboard as if power-up
*/
static void
reset(int kbd)
{
int cmd;
cmd = KBD_CMD_RESET;
if (ioctl(kbd, KIOCCMD, &cmd)) {
perror("kbd: ioctl error");
exit(1);
}
}
/*
* this routine gets the type of the keyboard being used
*/
static int
get_type(int kbd)
{
int kbd_type;
if (ioctl(kbd, KIOCTYPE, &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;
}
return (kbd_type);
}
/*
* 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 three variables are used for getting delay&rate&count */
int delay, rate, count = -1;
delay = rate = 0;
if (ioctl(kbd, KIOCTYPE, &kbd_type)) {
perror("ioctl (kbd type)");
exit(1);
}
if (ioctl(kbd, KIOCLAYOUT, &kbd_layout)) {
perror("ioctl (kbd layout)");
exit(1);
}
(void) printf("type=%d\nlayout=%d (0x%.2x)\n",
kbd_type, kbd_layout, kbd_layout);
/* below code is used to get the autorepeat delay and rate */
if (ioctl(kbd, KIOCGRPTDELAY, &delay)) {
perror("ioctl (kbd get repeat delay)");
exit(1);
}
if (ioctl(kbd, KIOCGRPTRATE, &rate)) {
perror("ioctl (kbd get repeat rate)");
exit(1);
}
if (ioctl(kbd, KIOCGRPTCOUNT, &count)) {
perror("ioctl (kbd get repeat count)");
exit(1);
}
(void) printf("delay(ms)=%d\n", delay);
(void) printf("rate(ms)=%d\n", rate);
if (count == -1)
(void) printf("count=unlimited\n");
else
(void) printf("count=%d\n", count);
}
/*
* this routine enables or disables clicking of the keyboard
*/
static int
click(char *copt, int kbd)
{
int cmd;
if (strcmp(copt, "on") == 0)
cmd = KBD_CMD_CLICK;
else if (strcmp(copt, "off") == 0)
cmd = KBD_CMD_NOCLICK;
else {
(void) fprintf(stderr, "wrong option -- %s\n", copt);
usage();
return (1);
}
if (ioctl(kbd, KIOCCMD, &cmd)) {
perror("kbd ioctl (keyclick)");
return (1);
}
return (0);
}
/*
* this routine enables/disables/sets BRK or abort sequence feature
*/
static int
abort_enable(char *aopt, int kbd)
{
int enable;
if (strcmp(aopt, "alternate") == 0)
enable = KIOCABORTALTERNATE;
else if (strcmp(aopt, "enable") == 0)
enable = KIOCABORTENABLE;
else if (strcmp(aopt, "disable") == 0)
enable = KIOCABORTDISABLE;
else {
(void) fprintf(stderr, "wrong option -- %s\n", aopt);
usage();
return (1);
}
if (ioctl(kbd, KIOCSKABORTEN, &enable)) {
perror("kbd ioctl (abort enable)");
return (1);
}
return (0);
}
static int
set_rptcount(int count, int kbd)
{
if (ioctl(kbd, KIOCSRPTCOUNT, &count) == -1) {
perror("kbd: set repeat count");
return (1);
}
return (0);
}
static int
set_repeat_count(char *count_str, int kbd)
{
int count = atoi(count_str);
return (set_rptcount(count, kbd));
}
static int
set_rptdelay(int delay, int kbd)
{
/*
* 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 kbd_defaults
* function.
*/
if (ioctl(kbd, KIOCSRPTDELAY, &delay) == -1) {
if (delay < KIOCRPTDELAY_MIN)
(void) fprintf(stderr, "kbd: specified delay %d is "
"less than minimum %d\n", delay, KIOCRPTDELAY_MIN);
else
perror("kbd: set repeat delay");
return (1);
}
return (0);
}
/*
* this routine set autorepeat delay
*/
static int
set_repeat_delay(char *delay_str, int kbd)
{
int delay = atoi(delay_str);
return (set_rptdelay(delay, kbd));
}
static int
set_rptrate(int rate, int kbd)
{
/*
* The input validation check has been covered by main function
* and kbd_defaults function.Here just give an error message if
* the ioctl fails.
*/
if (ioctl(kbd, KIOCSRPTRATE, &rate) == -1) {
perror("kbd: set repeat rate");
return (1);
}
return (0);
}
/*
* this routine set autorepeat rate
*/
static int
set_repeat_rate(char *rate_str, int kbd)
{
int rate = atoi(rate_str);
return (set_rptrate(rate, kbd));
}
#define BAD_DEFAULT_STR "kbd: bad default value for %s: %s\n"
#define BAD_DEFAULT_INT "kbd: bad default value for %s: %d\n"
#define BAD_DEFAULT_LLINT "kbd: bad default value for %s: %lld\n"
static void
kbd_defaults(int kbd)
{
scf_handle_t *h = NULL;
scf_snapshot_t *snap = NULL;
scf_instance_t *inst = NULL;
scf_propertygroup_t *pg = NULL;
scf_property_t *prop = NULL;
scf_value_t *val = NULL;
int layout_num;
char *val_layout = NULL, *val_abort = NULL;
uint8_t val_click;
int64_t val_delay, val_rate, val_count;
int64_t val_kbd_beeper, val_console_beeper;
if ((h = scf_handle_create(SCF_VERSION)) == NULL ||
scf_handle_bind(h) != 0 ||
(inst = scf_instance_create(h)) == NULL ||
(snap = scf_snapshot_create(h)) == NULL ||
(pg = scf_pg_create(h)) == NULL ||
(prop = scf_property_create(h)) == NULL ||
(val = scf_value_create(h)) == NULL) {
goto out;
}
if (scf_handle_decode_fmri(h, KBD_FMRI, NULL, NULL, inst,
NULL, NULL, SCF_DECODE_FMRI_REQUIRE_INSTANCE) != 0) {
goto out;
}
if (scf_instance_get_snapshot(inst, "running", snap) != 0) {
scf_snapshot_destroy(snap);
snap = NULL;
}
if (scf_instance_get_pg_composed(inst, snap, KBD_PG, pg) != 0) {
goto out;
}
if ((val_abort = malloc(KBD_MAX_NAME_LEN)) == NULL) {
(void) fprintf(stderr,
"Can not alloc memory for keyboard properties\n");
goto out;
}
if ((val_layout = malloc(KBD_MAX_NAME_LEN)) == NULL) {
(void) fprintf(stderr,
"Can not alloc memory for keyboard properties\n");
goto out;
}
if (scf_pg_get_property(pg, KBD_PROP_KEYCLICK, prop) != 0 ||
scf_property_get_value(prop, val) != 0 ||
scf_value_get_boolean(val, &val_click) == -1) {
(void) fprintf(stderr, "Can not get KEYCLICK\n");
}
if (val_click == 1)
(void) click("on", kbd);
else if (val_click == 0)
(void) click("off", kbd);
else
(void) fprintf(stderr,
BAD_DEFAULT_INT, KBD_PROP_KEYCLICK, val_click);
if (scf_pg_get_property(pg, KBD_PROP_KEYBOARD_ABORT, prop) != 0 ||
scf_property_get_value(prop, val) != 0 ||
scf_value_get_astring(val, val_abort, KBD_MAX_NAME_LEN) == -1) {
(void) fprintf(stderr, "Can not get KEYBOARD_ABORT\n");
}
if (*val_abort != '\0') {
/*
* ABORT must equal "enable", "disable" or "alternate"
*/
if ((strcmp(val_abort, "enable") == 0) ||
(strcmp(val_abort, "alternate") == 0) ||
(strcmp(val_abort, "disable") == 0))
(void) abort_enable(val_abort, kbd);
else
(void) fprintf(stderr, BAD_DEFAULT_STR,
KBD_PROP_KEYBOARD_ABORT, val_abort);
}
if (scf_pg_get_property(pg, KBD_PROP_RPTCOUNT, prop) != 0 ||
scf_property_get_value(prop, val) != 0 ||
scf_value_get_integer(val, &val_count) == -1) {
(void) fprintf(stderr, "Can not get RPTCOUNT\n");
}
if (val_count == -1 || (val_count > 0 && val_count < INT_MAX))
(void) set_rptcount(val_count, kbd);
else
(void) fprintf(stderr,
BAD_DEFAULT_LLINT, KBD_PROP_RPTCOUNT, val_count);
if (scf_pg_get_property(pg, KBD_PROP_RPTDELAY, prop) != 0 ||
scf_property_get_value(prop, val) != 0 ||
scf_value_get_integer(val, &val_delay) == -1) {
(void) fprintf(stderr, "Can not get RPTDELAY\n");
}
if (val_delay > 0)
(void) set_rptdelay(val_delay, kbd);
else
(void) fprintf(stderr,
BAD_DEFAULT_LLINT, KBD_PROP_RPTDELAY, val_delay);
if (scf_pg_get_property(pg, KBD_PROP_RPTRATE, prop) != 0 ||
scf_property_get_value(prop, val) != 0 ||
scf_value_get_integer(val, &val_rate) == -1) {
(void) fprintf(stderr, "Can not get RPTRATE\n");
}
if (val_rate > 0)
(void) set_rptrate(val_rate, kbd);
else
(void) fprintf(stderr,
BAD_DEFAULT_LLINT, KBD_PROP_RPTRATE, val_rate);
if (scf_pg_get_property(pg, KBD_PROP_LAYOUT, prop) != 0 ||
scf_property_get_value(prop, val) != 0 ||
scf_value_get_astring(val, val_layout, KBD_MAX_NAME_LEN) == -1) {
(void) fprintf(stderr, "Can not get LAYOUT\n");
}
if (*val_layout != '\0') {
/*
* LAYOUT must be one of the layouts supported in kbd_layouts
*/
if (get_layouts() != 0)
goto out;
if ((layout_num = get_layout_number(val_layout)) == -1) {
(void) fprintf(stderr,
BAD_DEFAULT_STR, KBD_PROP_LAYOUT, val_layout);
goto out;
}
(void) set_layout(kbd, layout_num);
}
if (scf_pg_get_property(pg, KBD_PROP_FREQ, prop) != 0 ||
scf_property_get_value(prop, val) != 0 ||
scf_value_get_integer(val, &val_kbd_beeper) == -1) {
(void) fprintf(stderr, "Can not get FREQ\n");
}
if (val_kbd_beeper >= 0 && val_kbd_beeper <= INT16_MAX)
(void) set_beep_freq(kbd, "keyboard", val_kbd_beeper);
else
(void) fprintf(stderr,
BAD_DEFAULT_LLINT, KBD_PROP_FREQ, val_kbd_beeper);
if (scf_pg_get_property(pg, KBD_PROP_CONSFREQ, prop) != 0 ||
scf_property_get_value(prop, val) != 0 ||
scf_value_get_integer(val, &val_console_beeper) == -1) {
(void) fprintf(stderr, "Can not get CONSFREQ\n");
}
if (val_console_beeper >= 0 && val_console_beeper <= INT16_MAX)
(void) set_beep_freq(kbd, "console", val_console_beeper);
else
(void) fprintf(stderr,
BAD_DEFAULT_LLINT, KBD_PROP_CONSFREQ, val_console_beeper);
out:
if (val_layout != NULL)
free(val_layout);
if (val_abort != NULL)
free(val_abort);
if (snap != NULL)
scf_snapshot_destroy(snap);
scf_value_destroy(val);
scf_property_destroy(prop);
scf_pg_destroy(pg);
scf_instance_destroy(inst);
scf_handle_destroy(h);
}
static int
get_layout_number(char *layout)
{
int i;
int layout_number = -1;
for (i = 0; i < layout_count; i ++) {
if (strcmp(layout, layout_names[i]) == 0) {
layout_number = layout_numbers[i];
break;
}
}
return (layout_number);
}
static int
get_layouts()
{
FILE *stream;
char buffer[MAX_LINE_SIZE];
char *result = NULL;
int i = 0;
char *tmpbuf;
if ((stream = fopen(KBD_LAYOUT_FILE, "r")) == 0) {
perror(KBD_LAYOUT_FILE);
return (1);
}
while ((fgets(buffer, MAX_LINE_SIZE, stream) != NULL) &&
(i < MAX_LAYOUT_NUM)) {
if (buffer[0] == '#')
continue;
if ((result = strtok(buffer, "=")) == NULL)
continue;
if ((tmpbuf = strdup(result)) != NULL) {
layout_names[i] = tmpbuf;
} else {
perror("out of memory getting layout names");
return (1);
}
if ((result = strtok(NULL, "\n")) == NULL)
continue;
layout_numbers[i] = atoi(result);
if (strcmp(tmpbuf, "US-English") == 0)
default_layout_number = i;
i++;
}
layout_count = i;
return (0);
}
/*
* this routine sets the layout of the keyboard being used
*/
static int
set_layout(int kbd, int layout_num)
{
if (ioctl(kbd, KIOCSLAYOUT, layout_num)) {
perror("ioctl (set kbd layout)");
return (1);
}
(void) system("/usr/bin/loadkeys");
return (0);
}
static char *usage1 =
"kbd [-r] [-t] [-l] [-a enable|disable|alternate] [-c on|off]\n"
"\t [-d keyboard device] [-A count] [-D delay] [-R rate]";
static char *usage2 = "kbd -i [-d keyboard device]";
static char *usage3 = "kbd -s [language]";
static char *usage4 = "kbd -b [keyboard|console] frequency";
static void
usage(void)
{
(void) fprintf(stderr, "Usage:\t%s\n\t%s\n\t%s\n\t%s\n", usage1,
usage2, usage3, usage4);
}
#!/sbin/sh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (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
# or http://www.opensolaris.org/os/licensing.
# 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 (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
#
# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T.
# All rights reserved.
#
# This file loads keyboard mappings and initializes keyboard defaults
. /lib/svc/share/smf_include.sh
smf_is_globalzone || exit $SMF_EXIT_OK
#
# Systems with no hardware keyboard ID may provide an eeprom value,
# or the value can be supplied in svc:/system/keymap:default. A value
# supplied in svc:/system/keymap:default will override the value
# supplied in eeprom.
#
[ -x /usr/lib/set_keyboard_layout ] && /usr/lib/set_keyboard_layout
# Initialize the keyboard defaults
[ -h /dev/kbd -a -x /usr/bin/kbd ] && /usr/bin/kbd -i >/dev/null 2>&1
# Load the keymap for the attached keyboard.
/usr/bin/loadkeys
<?xml version="1.0"?>
<!DOCTYPE service_bundle SYSTEM "/usr/share/lib/xml/dtd/service_bundle.dtd.1">
<!--
Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
CDDL HEADER START
The contents of this file are subject to the terms of the
Common Development and Distribution License (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
or http://www.opensolaris.org/os/licensing.
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
NOTE: This service manifest is not editable; its contents will
be overwritten by package or patch operations, including
operating system upgrade. Make customizations in a different
file.
Service manifest for establishing hardware keyboard defaults.
-->
<service_bundle type='manifest' name='SUNWcsr:keymap'>
<service
name='system/keymap'
type='service'
version='1'>
<single_instance />
<dependency
name='usr'
type='service'
grouping='require_all'
restart_on='none'>
<service_fmri value='svc:/system/filesystem/usr' />
</dependency>
<!--
Hammerhead: depend on milestone/devices so /dev/openprom exists
before eeprom is invoked. Without this, keymap can race the
devfsadm creation of the openeepr pseudo-device, fail three
times in one second, and wedge in maintenance.
-->
<dependency
name='devices'
type='service'
grouping='require_all'
restart_on='none'>
<service_fmri value='svc:/milestone/devices' />
</dependency>
<dependent
name='keymap_single-user'
grouping='optional_all'
restart_on='none'>
<service_fmri value='svc:/milestone/single-user' />
</dependent>
<exec_method
type='method'
name='start'
exec='/lib/svc/method/keymap'
timeout_seconds='0' />
<exec_method
type='method'
name='stop'
exec=':true'
timeout_seconds='60' />
<property_group name='startd' type='framework'>
<propval name='duration' type='astring'
value='transient' />
</property_group>
<instance name='default' enabled='false'>
<property_group name='keymap' type='system'>
<propval name='keyboard_abort' type='astring' value='enable' />
<propval name='keyclick' type='boolean' value='false' />
<propval name='repeat_count' type='integer' value='-1' />
<propval name='repeat_delay' type='integer' value='500' />
<propval name='repeat_rate' type='integer' value='40' />
<propval name='layout' type='astring' value='US-English' />
<propval name='kbd_beeper_freq' type='integer' value='2000' />
<propval name='console_beeper_freq' type='integer' value='900' />
</property_group>
</instance>
<stability value='Unstable' />
<template>
<common_name>
<loctext xml:lang='C'>keyboard defaults
</loctext>
</common_name>
<documentation>
<manpage
title='kbd'
section='8'
manpath='/usr/share/man' />
</documentation>
<pg_pattern name='keymap' type='system'>
<description>
<loctext xml:lang='C'>
Default keyboard configurations
</loctext>
</description>
<prop_pattern name='keyboard_abort' type='astring' required='true'>
<description>
<loctext xml:lang='C'>
The behavior of keyboard abort sequence
</loctext>
</description>
<cardinality min="1" max="1"/>
</prop_pattern>
<prop_pattern name='keyclick' type='boolean' required='true'>
<description>
<loctext xml:lang='C'>
The keyclick behavior
</loctext>
</description>
<cardinality min="1" max="1"/>
</prop_pattern>
<prop_pattern name='repeat_delay' type='integer' required='true'>
<description>
<loctext xml:lang='C'>
The autorepeat delay
</loctext>
</description>
<cardinality min="1" max="1"/>
</prop_pattern>
<prop_pattern name='repeat_rate' type='integer' required='true'>
<description>
<loctext xml:lang='C'>
The autorepeat rate
</loctext>
</description>
<cardinality min="1" max="1"/>
</prop_pattern>
<prop_pattern name='layout' type='astring' required='true'>
<description>
<loctext xml:lang='C'>
The keyboard layout
</loctext>
</description>
<cardinality min="1" max="1"/>
</prop_pattern>
<prop_pattern name='kbd_beeper_freq' type='integer' required='true'>
<description>
<loctext xml:lang='C'>
Keyboard beeper frequency
</loctext>
</description>
<cardinality min="1" max="1"/>
<constraints>
<range min="0" max="32767"/>
</constraints>
</prop_pattern>
<prop_pattern name='console_beeper_freq' type='integer' required='true'>
<description>
<loctext xml:lang='C'>
Console beeper frequency
</loctext>
</description>
<cardinality min="1" max="1"/>
<constraints>
<range min="0" max="32767"/>
</constraints>
</prop_pattern>
</pg_pattern>
</template>
</service>
</service_bundle>
|