xref: /openbsd/sys/dev/wscons/wsemul_vt100_keys.c (revision 91f110e0)
1 /* $OpenBSD: wsemul_vt100_keys.c,v 1.7 2013/10/18 22:06:41 miod 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/wscons_features.h>
34 #include <dev/wscons/wsconsio.h>
35 #include <dev/wscons/wsdisplayvar.h>
36 #include <dev/wscons/wsksymvar.h>
37 #include <dev/wscons/wsksymdef.h>
38 #include <dev/wscons/wsemulvar.h>
39 #include <dev/wscons/wsemul_vt100var.h>
40 
41 static const u_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 	"\033[35~",
63 	"\033[36~",
64 	"\033[37~",
65 	"\033[38~"
66 };
67 
68 static const u_char *vt100_pfkeys[] = {
69 	"\033OP",	/* PF1 */
70 	"\033OQ",
71 	"\033OR",
72 	"\033OS",	/* PF4 */
73 };
74 
75 static const u_char *vt100_numpad[] = {
76 	"\033Op",	/* KP 0 */
77 	"\033Oq",	/* KP 1 */
78 	"\033Or",	/* KP 2 */
79 	"\033Os",	/* KP 3 */
80 	"\033Ot",	/* KP 4 */
81 	"\033Ou",	/* KP 5 */
82 	"\033Ov",	/* KP 6 */
83 	"\033Ow",	/* KP 7 */
84 	"\033Ox",	/* KP 8 */
85 	"\033Oy",	/* KP 9 */
86 };
87 
88 int
89 wsemul_vt100_translate(void *cookie, kbd_t layout, keysym_t in,
90     const u_char **out)
91 {
92 	struct wsemul_vt100_emuldata *edp = cookie;
93 
94 	if (KS_GROUP(in) == KS_GROUP_Ascii) {
95 		*out = edp->translatebuf;
96 		return (wsemul_utf8_translate(KS_VALUE(in), layout,
97 		    edp->translatebuf, edp->flags & VTFL_UTF8));
98 	}
99 
100 	if (in >= KS_f1 && in <= KS_f24) {
101 		*out = vt100_fkeys[in - KS_f1];
102 		return (5);
103 	}
104 	if (in >= KS_F1 && in <= KS_F24) {
105 		*out = vt100_fkeys[in - KS_F1];
106 		return (5);
107 	}
108 	if (in >= KS_KP_F1 && in <= KS_KP_F4) {
109 		*out = vt100_pfkeys[in - KS_KP_F1];
110 		return (3);
111 	}
112 	if (edp->flags & VTFL_APPLKEYPAD) {
113 		if (in >= KS_KP_0 && in <= KS_KP_9) {
114 			*out = vt100_numpad[in - KS_KP_0];
115 			return (3);
116 		}
117 		switch (in) {
118 		    case KS_KP_Tab:
119 			*out = "\033OI";
120 			return (3);
121 		    case KS_KP_Enter:
122 			*out = "\033OM";
123 			return (3);
124 		    case KS_KP_Multiply:
125 			*out = "\033Oj";
126 			return (3);
127 		    case KS_KP_Add:
128 			*out = "\033Ok";
129 			return (3);
130 		    case KS_KP_Separator:
131 			*out = "\033Ol";
132 			return (3);
133 		    case KS_KP_Subtract:
134 			*out = "\033Om";
135 			return (3);
136 		    case KS_KP_Decimal:
137 			*out = "\033On";
138 			return (3);
139 		    case KS_KP_Divide:
140 			*out = "\033Oo";
141 			return (3);
142 		}
143 	} else {
144 		if (!(in & 0x80)) {
145 			edp->translatebuf[0] = in & 0xff; /* turn into ASCII */
146 			*out = edp->translatebuf;
147 			return (1);
148 		}
149 	}
150 	switch (in) {
151 	    case KS_Help:
152 		*out = vt100_fkeys[15 - 1];
153 		return (5);
154 	    case KS_Execute: /* "Do" */
155 		*out = vt100_fkeys[16 - 1];
156 		return (5);
157 	    case KS_Find:
158 		*out = "\033[1~";
159 		return (4);
160 	    case KS_Insert:
161 	    case KS_KP_Insert:
162 		*out = "\033[2~";
163 		return (4);
164 	    case KS_KP_Delete:
165 		*out = "\033[3~";
166 		return (4);
167 	    case KS_Select:
168 		*out = "\033[4~";
169 		return (4);
170 	    case KS_Prior:
171 	    case KS_KP_Prior:
172 		*out = "\033[5~";
173 		return (4);
174 	    case KS_Next:
175 	    case KS_KP_Next:
176 		*out = "\033[6~";
177 		return (4);
178 	    case KS_Home:
179 	    case KS_KP_Home:
180 		*out = "\033[7~";
181 		return (4);
182 	    case KS_End:
183 	    case KS_KP_End:
184 		*out = "\033[8~";
185 		return (4);
186 	    case KS_Up:
187 	    case KS_KP_Up:
188 		if (edp->flags & VTFL_APPLCURSOR)
189 			*out = "\033OA";
190 		else
191 			*out = "\033[A";
192 		return (3);
193 	    case KS_Down:
194 	    case KS_KP_Down:
195 		if (edp->flags & VTFL_APPLCURSOR)
196 			*out = "\033OB";
197 		else
198 			*out = "\033[B";
199 		return (3);
200 	    case KS_Left:
201 	    case KS_KP_Left:
202 		if (edp->flags & VTFL_APPLCURSOR)
203 			*out = "\033OD";
204 		else
205 			*out = "\033[D";
206 		return (3);
207 	    case KS_Right:
208 	    case KS_KP_Right:
209 		if (edp->flags & VTFL_APPLCURSOR)
210 			*out = "\033OC";
211 		else
212 			*out = "\033[C";
213 		return (3);
214 	}
215 	return (0);
216 }
217