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