1 /* $OpenBSD: wsemul_vt100_keys.c,v 1.2 2004/04/02 04:39:51 deraadt Exp $ */
2 /* $NetBSD: wsemul_vt100_keys.c,v 1.3 1999/04/22 20:06:02 mycroft Exp $ */
3
4 /*
5 * Copyright (c) 1998
6 * Matthias Drochner. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32
33 #include <dev/wscons/wsdisplayvar.h>
34 #include <dev/wscons/wsksymvar.h>
35 #include <dev/wscons/wsksymdef.h>
36 #include <dev/wscons/wsemul_vt100var.h>
37
38 static char *vt100_fkeys[] = {
39 "\033[11~", /* F1 */
40 "\033[12~",
41 "\033[13~", /* F1-F5 normally don't send codes */
42 "\033[14~",
43 "\033[15~", /* F5 */
44 "\033[17~", /* F6 */
45 "\033[18~",
46 "\033[19~",
47 "\033[20~",
48 "\033[21~",
49 "\033[23~", /* VT100: ESC */
50 "\033[24~", /* VT100: BS */
51 "\033[25~", /* VT100: LF */
52 "\033[26~",
53 "\033[28~", /* help */
54 "\033[29~", /* do */
55 "\033[31~",
56 "\033[32~",
57 "\033[33~",
58 "\033[34~", /* F20 */
59 };
60
61 static char *vt100_pfkeys[] = {
62 "\033OP", /* PF1 */
63 "\033OQ",
64 "\033OR",
65 "\033OS", /* PF4 */
66 };
67
68 static char *vt100_numpad[] = {
69 "\033Op", /* KP 0 */
70 "\033Oq", /* KP 1 */
71 "\033Or", /* KP 2 */
72 "\033Os", /* KP 3 */
73 "\033Ot", /* KP 4 */
74 "\033Ou", /* KP 5 */
75 "\033Ov", /* KP 6 */
76 "\033Ow", /* KP 7 */
77 "\033Ox", /* KP 8 */
78 "\033Oy", /* KP 9 */
79 };
80
81 int
82 wsemul_vt100_translate(cookie, in, out)
83 void *cookie;
84 keysym_t in;
85 char **out;
86 {
87 struct wsemul_vt100_emuldata *edp = cookie;
88 static char c;
89
90 if (in >= KS_f1 && in <= KS_f20) {
91 *out = vt100_fkeys[in - KS_f1];
92 return (5);
93 }
94 if (in >= KS_F1 && in <= KS_F20) {
95 *out = vt100_fkeys[in - KS_F1];
96 return (5);
97 }
98 if (in >= KS_KP_F1 && in <= KS_KP_F4) {
99 *out = vt100_pfkeys[in - KS_KP_F1];
100 return (3);
101 }
102 if (edp->flags & VTFL_APPLKEYPAD) {
103 if (in >= KS_KP_0 && in <= KS_KP_9) {
104 *out = vt100_numpad[in - KS_KP_0];
105 return (3);
106 }
107 switch (in) {
108 case KS_KP_Tab:
109 *out = "\033OI";
110 return (3);
111 case KS_KP_Enter:
112 *out = "\033OM";
113 return (3);
114 case KS_KP_Multiply:
115 *out = "\033Oj";
116 return (3);
117 case KS_KP_Add:
118 *out = "\033Ok";
119 return (3);
120 case KS_KP_Separator:
121 *out = "\033Ol";
122 return (3);
123 case KS_KP_Subtract:
124 *out = "\033Om";
125 return (3);
126 case KS_KP_Decimal:
127 *out = "\033On";
128 return (3);
129 case KS_KP_Divide:
130 *out = "\033Oo";
131 return (3);
132 }
133 } else {
134 if (!(in & 0x80)) {
135 c = in & 0xff; /* turn into ASCII */
136 *out = &c;
137 return (1);
138 }
139 }
140 switch (in) {
141 case KS_Help:
142 *out = vt100_fkeys[15 - 1];
143 return (5);
144 case KS_Execute: /* "Do" */
145 *out = vt100_fkeys[16 - 1];
146 return (5);
147 case KS_Find:
148 *out = "\033[1~";
149 return (4);
150 case KS_Insert:
151 case KS_KP_Insert:
152 *out = "\033[2~";
153 return (4);
154 case KS_KP_Delete:
155 *out = "\033[3~";
156 return (4);
157 case KS_Select:
158 *out = "\033[4~";
159 return (4);
160 case KS_Prior:
161 case KS_KP_Prior:
162 *out = "\033[5~";
163 return (4);
164 case KS_Next:
165 case KS_KP_Next:
166 *out = "\033[6~";
167 return (4);
168 case KS_Home:
169 case KS_KP_Home:
170 *out = "\033[7~";
171 return (4);
172 case KS_End:
173 case KS_KP_End:
174 *out = "\033[8~";
175 return (4);
176 case KS_Up:
177 case KS_KP_Up:
178 if (edp->flags & VTFL_APPLCURSOR)
179 *out = "\033OA";
180 else
181 *out = "\033[A";
182 return (3);
183 case KS_Down:
184 case KS_KP_Down:
185 if (edp->flags & VTFL_APPLCURSOR)
186 *out = "\033OB";
187 else
188 *out = "\033[B";
189 return (3);
190 case KS_Left:
191 case KS_KP_Left:
192 if (edp->flags & VTFL_APPLCURSOR)
193 *out = "\033OD";
194 else
195 *out = "\033[D";
196 return (3);
197 case KS_Right:
198 case KS_KP_Right:
199 if (edp->flags & VTFL_APPLCURSOR)
200 *out = "\033OC";
201 else
202 *out = "\033[C";
203 return (3);
204 }
205 return (0);
206 }