xref: /netbsd-src/sys/dev/wscons/wsemul_vt100_keys.c (revision de4fa6c51a9708fc05f88b618fa6fad87c9508ec)
1 /* $NetBSD: wsemul_vt100_keys.c,v 1.9 2005/12/11 12:24:12 christos Exp $ */
2 
3 /*
4  * Copyright (c) 1998
5  *	Matthias Drochner.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: wsemul_vt100_keys.c,v 1.9 2005/12/11 12:24:12 christos Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 
35 #include <dev/wscons/wsconsio.h>
36 #include <dev/wscons/wsdisplayvar.h>
37 #include <dev/wscons/wsksymvar.h>
38 #include <dev/wscons/wsksymdef.h>
39 #include <dev/wscons/wsemul_vt100var.h>
40 
41 static const char *vt100_fkeys[] = {
42 	"\033[11~",	/* F1 */
43 	"\033[12~",
44 	"\033[13~",		/* F1-F5 normally don't send codes */
45 	"\033[14~",
46 	"\033[15~",	/* F5 */
47 	"\033[17~",	/* F6 */
48 	"\033[18~",
49 	"\033[19~",
50 	"\033[20~",
51 	"\033[21~",
52 	"\033[23~",	/* VT100: ESC */
53 	"\033[24~",	/* VT100: BS */
54 	"\033[25~",	/* VT100: LF */
55 	"\033[26~",
56 	"\033[28~",	/* help */
57 	"\033[29~",	/* do */
58 	"\033[31~",
59 	"\033[32~",
60 	"\033[33~",
61 	"\033[34~",	/* F20 */
62 };
63 
64 static const char *vt100_pfkeys[] = {
65 	"\033OP",	/* PF1 */
66 	"\033OQ",
67 	"\033OR",
68 	"\033OS",	/* PF4 */
69 };
70 
71 static const char *vt100_numpad[] = {
72 	"\033Op",	/* KP 0 */
73 	"\033Oq",	/* KP 1 */
74 	"\033Or",	/* KP 2 */
75 	"\033Os",	/* KP 3 */
76 	"\033Ot",	/* KP 4 */
77 	"\033Ou",	/* KP 5 */
78 	"\033Ov",	/* KP 6 */
79 	"\033Ow",	/* KP 7 */
80 	"\033Ox",	/* KP 8 */
81 	"\033Oy",	/* KP 9 */
82 };
83 
84 int
85 wsemul_vt100_translate(void *cookie, keysym_t in, const 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 }
207