wskbd.c revision 1.2 1 1.2 martin /* $NetBSD: wskbd.c,v 1.2 2019/06/12 06:20:18 martin Exp $ */
2 1.1 dholland
3 1.1 dholland /*-
4 1.1 dholland * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.1 dholland * All rights reserved.
6 1.1 dholland *
7 1.1 dholland * This code is derived from software contributed to The NetBSD Foundation
8 1.1 dholland * by David Laight.
9 1.1 dholland *
10 1.1 dholland * Redistribution and use in source and binary forms, with or without
11 1.1 dholland * modification, are permitted provided that the following conditions
12 1.1 dholland * are met:
13 1.1 dholland * 1. Redistributions of source code must retain the above copyright
14 1.1 dholland * notice, this list of conditions and the following disclaimer.
15 1.1 dholland * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 dholland * notice, this list of conditions and the following disclaimer in the
17 1.1 dholland * documentation and/or other materials provided with the distribution.
18 1.1 dholland *
19 1.1 dholland * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 dholland * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 dholland * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 dholland * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 dholland * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 dholland * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 dholland * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 dholland * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 dholland * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 dholland * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 dholland * POSSIBILITY OF SUCH DAMAGE.
30 1.1 dholland */
31 1.1 dholland
32 1.1 dholland #include <sys/cdefs.h>
33 1.2 martin __RCSID("$NetBSD: wskbd.c,v 1.2 2019/06/12 06:20:18 martin Exp $");
34 1.1 dholland
35 1.1 dholland #include <unistd.h>
36 1.1 dholland #include <stdlib.h>
37 1.1 dholland #include <fcntl.h>
38 1.1 dholland #include <sys/ioctl.h>
39 1.1 dholland #include <dev/wscons/wsconsio.h>
40 1.1 dholland #include <dev/wscons/wsksymdef.h>
41 1.1 dholland
42 1.1 dholland #include "defs.h"
43 1.1 dholland #include "menu_defs.h"
44 1.1 dholland #include "msg_defs.h"
45 1.1 dholland void save_kb_encoding(void);
46 1.1 dholland
47 1.1 dholland /* wscons setup for sysinst */
48 1.1 dholland
49 1.1 dholland static const char *kbd_name = 0;
50 1.1 dholland static int kb_default = 0;
51 1.1 dholland
52 1.1 dholland struct kb_types {
53 1.1 dholland kbd_t kb_encoding;
54 1.1 dholland const char *kb_enc_txt;
55 1.1 dholland const char *kb_name;
56 1.1 dholland };
57 1.1 dholland
58 1.1 dholland /* Types and names of keyboards, maybe the names should be translated... */
59 1.1 dholland static const struct kb_types kb_types[] = {
60 1.1 dholland #define KB_sysinst(tag, tagf, value, cc, ccf, country) \
61 1.1 dholland {tag | tagf, cc ccf, country},
62 1.1 dholland KB_ENC_FUN(KB_sysinst)
63 1.1 dholland {KB_US | KB_COLEMAK, "us" ".colemak", "US-Colemak"},
64 1.1 dholland {KB_US | KB_DVORAK, "us" ".dvorak", "US-Dvorak"}
65 1.1 dholland };
66 1.1 dholland
67 1.1 dholland static int
68 1.1 dholland set_kb_encoding(menudesc *m, void *arg)
69 1.1 dholland {
70 1.1 dholland int fd = *(int *)arg;
71 1.1 dholland const struct kb_types *kbt = kb_types + m->cursel;
72 1.1 dholland
73 1.1 dholland if (kbt->kb_encoding != KB_USER) {
74 1.1 dholland ioctl(fd, WSKBDIO_SETENCODING, &kbt->kb_encoding);
75 1.1 dholland kbd_name = kbt->kb_enc_txt;
76 1.1 dholland }
77 1.1 dholland return 1;
78 1.1 dholland }
79 1.1 dholland
80 1.1 dholland static void
81 1.1 dholland set_kb_default(menudesc *m, void *arg)
82 1.1 dholland {
83 1.1 dholland m->cursel = kb_default;
84 1.1 dholland }
85 1.1 dholland
86 1.1 dholland void
87 1.1 dholland get_kb_encoding(void)
88 1.1 dholland {
89 1.1 dholland int fd;
90 1.1 dholland unsigned int i;
91 1.1 dholland int kb_menu;
92 1.1 dholland kbd_t kbdencoding;
93 1.2 martin menu_ent opt[__arraycount(kb_types)];
94 1.1 dholland const char *dflt = msg_string(MSG_kb_default);
95 1.1 dholland
96 1.2 martin memset(opt, 0, sizeof(opt));
97 1.1 dholland fd = open("/dev/wskbd0", O_WRONLY);
98 1.1 dholland if (fd < 0)
99 1.1 dholland return;
100 1.1 dholland if (ioctl(fd, WSKBDIO_GETENCODING, &kbdencoding) >= 0) {
101 1.1 dholland memset(&opt, 0, sizeof opt);
102 1.2 martin for (i = 0; i < __arraycount(opt); i++) {
103 1.1 dholland if (kb_types[i].kb_encoding == KB_USER)
104 1.1 dholland opt[0].opt_name = MSG_unchanged;
105 1.1 dholland else {
106 1.1 dholland opt[i].opt_name = kb_types[i].kb_name;
107 1.1 dholland if (strcmp(kb_types[i].kb_name, dflt) == 0)
108 1.1 dholland kb_default = i;
109 1.1 dholland }
110 1.1 dholland opt[i].opt_menu = OPT_NOMENU;
111 1.1 dholland opt[i].opt_action = set_kb_encoding;
112 1.1 dholland }
113 1.2 martin kb_menu = new_menu(MSG_Keyboard_type, opt, __arraycount(opt),
114 1.1 dholland -1, -8, 0, 0,
115 1.1 dholland MC_SCROLL | MC_NOEXITOPT,
116 1.1 dholland set_kb_default, NULL, NULL, NULL, NULL);
117 1.1 dholland if (kb_menu != -1) {
118 1.1 dholland msg_display(MSG_hello);
119 1.1 dholland process_menu(kb_menu, &fd);
120 1.1 dholland free_menu(kb_menu);
121 1.1 dholland }
122 1.1 dholland }
123 1.1 dholland close(fd);
124 1.1 dholland }
125 1.1 dholland
126 1.1 dholland void
127 1.1 dholland save_kb_encoding(void)
128 1.1 dholland {
129 1.1 dholland const char *tp = target_prefix();
130 1.1 dholland
131 1.1 dholland if (kbd_name == NULL)
132 1.1 dholland return;
133 1.1 dholland /*
134 1.1 dholland * Put the keyboard encoding into the wscons.conf file. Either:
135 1.1 dholland * 1) replace an exiting line
136 1.1 dholland * 2) replace a commented out line
137 1.1 dholland * or
138 1.1 dholland * 3) add a line to the end of the file
139 1.1 dholland */
140 1.1 dholland run_program(0, "sed -an -e 'H;$!d;g'"
141 1.1 dholland " -e 's/\\nencoding [a-zA-Z0-9.]*\\n/\\\nencoding %s\\\n/; t done'"
142 1.1 dholland " -e 's/\\n#encoding [a-zA-Z0-9.]*\\n/\\\nencoding %s\\\n/; t done'"
143 1.1 dholland " -e 's/$/\\\nencoding %s/'"
144 1.1 dholland " -e ':done'"
145 1.1 dholland " -e 'w %s/etc/wscons.conf' %s/etc/wscons.conf",
146 1.1 dholland kbd_name, kbd_name, kbd_name, tp, tp);
147 1.1 dholland }
148