1 /* $NetBSD: wskbd.c,v 1.7 2008/09/03 08:52:36 ghen Exp $ */
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: wskbd.c,v 1.7 2008/09/03 08:52:36 ghen Exp $");
38 #include <sys/ioctl.h>
39 #include <dev/wscons/wsconsio.h>
40 #include <dev/wscons/wsksymdef.h>
43 #include "menu_defs.h"
45 void save_kb_encoding(void);
47 #define nelem(x) (sizeof (x) / sizeof *(x))
49 /* wscons setup for sysinst */
51 static const char *kbd_name
= 0;
52 static int kb_default
= 0;
56 const char *kb_enc_txt
;
60 /* Types and names of keyboards, maybe the names should be translated... */
61 static const struct kb_types kb_types
[] = {
62 #define KB_sysinst(tag, tagf, value, cc, ccf, country) \
63 {tag | tagf, cc ccf, country},
64 KB_ENC_FUN(KB_sysinst
)
65 {KB_US
| KB_COLEMAK
, "us" ".colemak", "US-Colemak"},
66 {KB_US
| KB_DVORAK
, "us" ".dvorak", "US-Dvorak"}
70 set_kb_encoding(menudesc
*m
, void *arg
)
73 const struct kb_types
*kbt
= kb_types
+ m
->cursel
;
75 if (kbt
->kb_encoding
!= KB_USER
) {
76 ioctl(fd
, WSKBDIO_SETENCODING
, &kbt
->kb_encoding
);
77 kbd_name
= kbt
->kb_enc_txt
;
83 set_kb_default(menudesc
*m
, void *arg
)
85 m
->cursel
= kb_default
;
95 menu_ent opt
[nelem(kb_types
)];
96 const char *dflt
= msg_string(MSG_kb_default
);
98 fd
= open("/dev/wskbd0", O_WRONLY
);
101 if (ioctl(fd
, WSKBDIO_GETENCODING
, &kbdencoding
) >= 0) {
102 memset(&opt
, 0, sizeof opt
);
103 for (i
= 0; i
< nelem(opt
); i
++) {
104 if (kb_types
[i
].kb_encoding
== KB_USER
)
105 opt
[0].opt_name
= MSG_unchanged
;
107 opt
[i
].opt_name
= kb_types
[i
].kb_name
;
108 if (strcmp(kb_types
[i
].kb_name
, dflt
) == 0)
111 opt
[i
].opt_menu
= OPT_NOMENU
;
112 opt
[i
].opt_action
= set_kb_encoding
;
114 kb_menu
= new_menu(MSG_Keyboard_type
, opt
, nelem(opt
),
116 MC_SCROLL
| MC_NOEXITOPT
,
117 set_kb_default
, NULL
, NULL
, NULL
, NULL
);
119 msg_display(MSG_hello
);
120 process_menu(kb_menu
, &fd
);
128 save_kb_encoding(void)
130 const char *tp
= target_prefix();
132 if (kbd_name
== NULL
)
135 * Put the keyboard encoding into the wscons.conf file. Either:
136 * 1) replace an exiting line
137 * 2) replace a commented out line
139 * 3) add a line to the end of the file
141 run_program(0, "sed -an -e 'H;$!d;g'"
142 " -e 's/\\nencoding [a-zA-Z0-9.]*\\n/\\\nencoding %s\\\n/; t done'"
143 " -e 's/\\n#encoding [a-zA-Z0-9.]*\\n/\\\nencoding %s\\\n/; t done'"
144 " -e 's/$/\\\nencoding %s/'"
146 " -e 'w %s/etc/wscons.conf' %s/etc/wscons.conf",
147 kbd_name
, kbd_name
, kbd_name
, tp
, tp
);