xref: /netbsd/usr.sbin/sysinst/wskbd.c (revision 26165e63)
1 /*	$NetBSD: wskbd.c,v 1.1 2014/07/26 19:30:44 dholland Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by David Laight.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: wskbd.c,v 1.1 2014/07/26 19:30:44 dholland Exp $");
34 
35 #include <unistd.h>
36 #include <stdlib.h>
37 #include <fcntl.h>
38 #include <sys/ioctl.h>
39 #include <dev/wscons/wsconsio.h>
40 #include <dev/wscons/wsksymdef.h>
41 
42 #include "defs.h"
43 #include "menu_defs.h"
44 #include "msg_defs.h"
45 void save_kb_encoding(void);
46 
47 #define nelem(x) (sizeof (x) / sizeof *(x))
48 
49 /* wscons setup for sysinst */
50 
51 static const char *kbd_name = 0;
52 static int kb_default = 0;
53 
54 struct kb_types {
55 	kbd_t		kb_encoding;
56 	const char	*kb_enc_txt;
57 	const char	*kb_name;
58 };
59 
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"}
67 };
68 
69 static int
70 set_kb_encoding(menudesc *m, void *arg)
71 {
72 	int fd = *(int *)arg;
73 	const struct kb_types *kbt = kb_types + m->cursel;
74 
75 	if (kbt->kb_encoding != KB_USER) {
76 		ioctl(fd, WSKBDIO_SETENCODING, &kbt->kb_encoding);
77 		kbd_name = kbt->kb_enc_txt;
78 	}
79 	return 1;
80 }
81 
82 static void
83 set_kb_default(menudesc *m, void *arg)
84 {
85 	m->cursel = kb_default;
86 }
87 
88 void
89 get_kb_encoding(void)
90 {
91 	int fd;
92 	unsigned int i;
93 	int kb_menu;
94 	kbd_t kbdencoding;
95 	menu_ent opt[nelem(kb_types)];
96 	const char *dflt = msg_string(MSG_kb_default);
97 
98 	fd = open("/dev/wskbd0", O_WRONLY);
99 	if (fd < 0)
100 		return;
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;
106 			else {
107 				opt[i].opt_name = kb_types[i].kb_name;
108 				if (strcmp(kb_types[i].kb_name, dflt) == 0)
109 					kb_default = i;
110 			}
111 			opt[i].opt_menu = OPT_NOMENU;
112 			opt[i].opt_action = set_kb_encoding;
113 		}
114 		kb_menu = new_menu(MSG_Keyboard_type, opt, nelem(opt),
115 			-1, -8, 0, 0,
116 			MC_SCROLL | MC_NOEXITOPT,
117 			set_kb_default, NULL, NULL, NULL, NULL);
118 		if (kb_menu != -1) {
119 			msg_display(MSG_hello);
120 			process_menu(kb_menu, &fd);
121 			free_menu(kb_menu);
122 		}
123 	}
124 	close(fd);
125 }
126 
127 void
128 save_kb_encoding(void)
129 {
130 	const char *tp = target_prefix();
131 
132 	if (kbd_name == NULL)
133 		return;
134 	/*
135 	 * Put the keyboard encoding into the wscons.conf file. Either:
136 	 * 1) replace an exiting line
137 	 * 2) replace a commented out line
138 	 * or
139 	 * 3) add a line to the end of the file
140 	 */
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/'"
145 	    " -e ':done'"
146 	    " -e 'w %s/etc/wscons.conf' %s/etc/wscons.conf",
147 	    kbd_name, kbd_name, kbd_name, tp, tp);
148 }
149