wskbd.c revision 1.1 1 1.1 dholland /* $NetBSD: wskbd.c,v 1.1 2014/07/26 19:30:44 dholland 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.1 dholland __RCSID("$NetBSD: wskbd.c,v 1.1 2014/07/26 19:30:44 dholland 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 #define nelem(x) (sizeof (x) / sizeof *(x))
48 1.1 dholland
49 1.1 dholland /* wscons setup for sysinst */
50 1.1 dholland
51 1.1 dholland static const char *kbd_name = 0;
52 1.1 dholland static int kb_default = 0;
53 1.1 dholland
54 1.1 dholland struct kb_types {
55 1.1 dholland kbd_t kb_encoding;
56 1.1 dholland const char *kb_enc_txt;
57 1.1 dholland const char *kb_name;
58 1.1 dholland };
59 1.1 dholland
60 1.1 dholland /* Types and names of keyboards, maybe the names should be translated... */
61 1.1 dholland static const struct kb_types kb_types[] = {
62 1.1 dholland #define KB_sysinst(tag, tagf, value, cc, ccf, country) \
63 1.1 dholland {tag | tagf, cc ccf, country},
64 1.1 dholland KB_ENC_FUN(KB_sysinst)
65 1.1 dholland {KB_US | KB_COLEMAK, "us" ".colemak", "US-Colemak"},
66 1.1 dholland {KB_US | KB_DVORAK, "us" ".dvorak", "US-Dvorak"}
67 1.1 dholland };
68 1.1 dholland
69 1.1 dholland static int
70 1.1 dholland set_kb_encoding(menudesc *m, void *arg)
71 1.1 dholland {
72 1.1 dholland int fd = *(int *)arg;
73 1.1 dholland const struct kb_types *kbt = kb_types + m->cursel;
74 1.1 dholland
75 1.1 dholland if (kbt->kb_encoding != KB_USER) {
76 1.1 dholland ioctl(fd, WSKBDIO_SETENCODING, &kbt->kb_encoding);
77 1.1 dholland kbd_name = kbt->kb_enc_txt;
78 1.1 dholland }
79 1.1 dholland return 1;
80 1.1 dholland }
81 1.1 dholland
82 1.1 dholland static void
83 1.1 dholland set_kb_default(menudesc *m, void *arg)
84 1.1 dholland {
85 1.1 dholland m->cursel = kb_default;
86 1.1 dholland }
87 1.1 dholland
88 1.1 dholland void
89 1.1 dholland get_kb_encoding(void)
90 1.1 dholland {
91 1.1 dholland int fd;
92 1.1 dholland unsigned int i;
93 1.1 dholland int kb_menu;
94 1.1 dholland kbd_t kbdencoding;
95 1.1 dholland menu_ent opt[nelem(kb_types)];
96 1.1 dholland const char *dflt = msg_string(MSG_kb_default);
97 1.1 dholland
98 1.1 dholland fd = open("/dev/wskbd0", O_WRONLY);
99 1.1 dholland if (fd < 0)
100 1.1 dholland return;
101 1.1 dholland if (ioctl(fd, WSKBDIO_GETENCODING, &kbdencoding) >= 0) {
102 1.1 dholland memset(&opt, 0, sizeof opt);
103 1.1 dholland for (i = 0; i < nelem(opt); i++) {
104 1.1 dholland if (kb_types[i].kb_encoding == KB_USER)
105 1.1 dholland opt[0].opt_name = MSG_unchanged;
106 1.1 dholland else {
107 1.1 dholland opt[i].opt_name = kb_types[i].kb_name;
108 1.1 dholland if (strcmp(kb_types[i].kb_name, dflt) == 0)
109 1.1 dholland kb_default = i;
110 1.1 dholland }
111 1.1 dholland opt[i].opt_menu = OPT_NOMENU;
112 1.1 dholland opt[i].opt_action = set_kb_encoding;
113 1.1 dholland }
114 1.1 dholland kb_menu = new_menu(MSG_Keyboard_type, opt, nelem(opt),
115 1.1 dholland -1, -8, 0, 0,
116 1.1 dholland MC_SCROLL | MC_NOEXITOPT,
117 1.1 dholland set_kb_default, NULL, NULL, NULL, NULL);
118 1.1 dholland if (kb_menu != -1) {
119 1.1 dholland msg_display(MSG_hello);
120 1.1 dholland process_menu(kb_menu, &fd);
121 1.1 dholland free_menu(kb_menu);
122 1.1 dholland }
123 1.1 dholland }
124 1.1 dholland close(fd);
125 1.1 dholland }
126 1.1 dholland
127 1.1 dholland void
128 1.1 dholland save_kb_encoding(void)
129 1.1 dholland {
130 1.1 dholland const char *tp = target_prefix();
131 1.1 dholland
132 1.1 dholland if (kbd_name == NULL)
133 1.1 dholland return;
134 1.1 dholland /*
135 1.1 dholland * Put the keyboard encoding into the wscons.conf file. Either:
136 1.1 dholland * 1) replace an exiting line
137 1.1 dholland * 2) replace a commented out line
138 1.1 dholland * or
139 1.1 dholland * 3) add a line to the end of the file
140 1.1 dholland */
141 1.1 dholland run_program(0, "sed -an -e 'H;$!d;g'"
142 1.1 dholland " -e 's/\\nencoding [a-zA-Z0-9.]*\\n/\\\nencoding %s\\\n/; t done'"
143 1.1 dholland " -e 's/\\n#encoding [a-zA-Z0-9.]*\\n/\\\nencoding %s\\\n/; t done'"
144 1.1 dholland " -e 's/$/\\\nencoding %s/'"
145 1.1 dholland " -e ':done'"
146 1.1 dholland " -e 'w %s/etc/wscons.conf' %s/etc/wscons.conf",
147 1.1 dholland kbd_name, kbd_name, kbd_name, tp, tp);
148 1.1 dholland }
149