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