summaryrefslogtreecommitdiff
path: root/sys/dev/wscons/wsemul_vt100_keys.c
blob: d94618b4bbf5cd23500eae5ffd1550defc8bcb63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* $OpenBSD: wsemul_vt100_keys.c,v 1.7 2013/10/18 22:06:41 miod Exp $ */
/* $NetBSD: wsemul_vt100_keys.c,v 1.3 1999/04/22 20:06:02 mycroft Exp $ */

/*
 * Copyright (c) 1998
 *	Matthias Drochner.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <sys/param.h>
#include <sys/systm.h>

#include <dev/wscons/wscons_features.h>
#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsdisplayvar.h>
#include <dev/wscons/wsksymvar.h>
#include <dev/wscons/wsksymdef.h>
#include <dev/wscons/wsemulvar.h>
#include <dev/wscons/wsemul_vt100var.h>

static const u_char *vt100_fkeys[] = {
	"\033[11~",	/* F1 */
	"\033[12~",
	"\033[13~",		/* F1-F5 normally don't send codes */
	"\033[14~",
	"\033[15~",	/* F5 */
	"\033[17~",	/* F6 */
	"\033[18~",
	"\033[19~",
	"\033[20~",
	"\033[21~",
	"\033[23~",	/* VT100: ESC */
	"\033[24~",	/* VT100: BS */
	"\033[25~",	/* VT100: LF */
	"\033[26~",
	"\033[28~",	/* help */
	"\033[29~",	/* do */
	"\033[31~",
	"\033[32~",
	"\033[33~",
	"\033[34~",	/* F20 */
	"\033[35~",
	"\033[36~",
	"\033[37~",
	"\033[38~"
};

static const u_char *vt100_pfkeys[] = {
	"\033OP",	/* PF1 */
	"\033OQ",
	"\033OR",
	"\033OS",	/* PF4 */
};

static const u_char *vt100_numpad[] = {
	"\033Op",	/* KP 0 */
	"\033Oq",	/* KP 1 */
	"\033Or",	/* KP 2 */
	"\033Os",	/* KP 3 */
	"\033Ot",	/* KP 4 */
	"\033Ou",	/* KP 5 */
	"\033Ov",	/* KP 6 */
	"\033Ow",	/* KP 7 */
	"\033Ox",	/* KP 8 */
	"\033Oy",	/* KP 9 */
};

int
wsemul_vt100_translate(void *cookie, kbd_t layout, keysym_t in,
    const u_char **out)
{
	struct wsemul_vt100_emuldata *edp = cookie;

	if (KS_GROUP(in) == KS_GROUP_Ascii) {
		*out = edp->translatebuf;
		return (wsemul_utf8_translate(KS_VALUE(in), layout,
		    edp->translatebuf, edp->flags & VTFL_UTF8));
	}

	if (in >= KS_f1 && in <= KS_f24) {
		*out = vt100_fkeys[in - KS_f1];
		return (5);
	}
	if (in >= KS_F1 && in <= KS_F24) {
		*out = vt100_fkeys[in - KS_F1];
		return (5);
	}
	if (in >= KS_KP_F1 && in <= KS_KP_F4) {
		*out = vt100_pfkeys[in - KS_KP_F1];
		return (3);
	}
	if (edp->flags & VTFL_APPLKEYPAD) {
		if (in >= KS_KP_0 && in <= KS_KP_9) {
			*out = vt100_numpad[in - KS_KP_0];
			return (3);
		}
		switch (in) {
		    case KS_KP_Tab:
			*out = "\033OI";
			return (3);
		    case KS_KP_Enter:
			*out = "\033OM";
			return (3);
		    case KS_KP_Multiply:
			*out = "\033Oj";
			return (3);
		    case KS_KP_Add:
			*out = "\033Ok";
			return (3);
		    case KS_KP_Separator:
			*out = "\033Ol";
			return (3);
		    case KS_KP_Subtract:
			*out = "\033Om";
			return (3);
		    case KS_KP_Decimal:
			*out = "\033On";
			return (3);
		    case KS_KP_Divide:
			*out = "\033Oo";
			return (3);
		}
	} else {
		if (!(in & 0x80)) {
			edp->translatebuf[0] = in & 0xff; /* turn into ASCII */
			*out = edp->translatebuf;
			return (1);
		}
	}
	switch (in) {
	    case KS_Help:
		*out = vt100_fkeys[15 - 1];
		return (5);
	    case KS_Execute: /* "Do" */
		*out = vt100_fkeys[16 - 1];
		return (5);
	    case KS_Find:
		*out = "\033[1~";
		return (4);
	    case KS_Insert:
	    case KS_KP_Insert:
		*out = "\033[2~";
		return (4);
	    case KS_KP_Delete:
		*out = "\033[3~";
		return (4);
	    case KS_Select:
		*out = "\033[4~";
		return (4);
	    case KS_Prior:
	    case KS_KP_Prior:
		*out = "\033[5~";
		return (4);
	    case KS_Next:
	    case KS_KP_Next:
		*out = "\033[6~";
		return (4);
	    case KS_Home:
	    case KS_KP_Home:
		*out = "\033[7~";
		return (4);
	    case KS_End:
	    case KS_KP_End:
		*out = "\033[8~";
		return (4);
	    case KS_Up:
	    case KS_KP_Up:
		if (edp->flags & VTFL_APPLCURSOR)
			*out = "\033OA";
		else
			*out = "\033[A";
		return (3);
	    case KS_Down:
	    case KS_KP_Down:
		if (edp->flags & VTFL_APPLCURSOR)
			*out = "\033OB";
		else
			*out = "\033[B";
		return (3);
	    case KS_Left:
	    case KS_KP_Left:
		if (edp->flags & VTFL_APPLCURSOR)
			*out = "\033OD";
		else
			*out = "\033[D";
		return (3);
	    case KS_Right:
	    case KS_KP_Right:
		if (edp->flags & VTFL_APPLCURSOR)
			*out = "\033OC";
		else
			*out = "\033[C";
		return (3);
	}
	return (0);
}