This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_wsconsevents.c
247 lines (216 loc) · 6.37 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2006 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Lesser General Public License for more details.
14
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20
21
Sam Lantinga
slouken@libsdl.org
*/
22
#include "SDL_config.h"
23
24
25
26
#include <sys/types.h>
#include <dev/wscons/wsdisplay_usl_io.h>
#include <sys/ioctl.h>
27
28
#include <fcntl.h>
#include <unistd.h>
29
#include <termios.h>
30
#include <errno.h>
31
32
33
#include <string.h>
#include "SDL.h"
34
35
#include "../../events/SDL_sysevents.h"
#include "../../events/SDL_events_c.h"
36
37
38
39
40
#include "SDL_wsconsvideo.h"
#include "SDL_wsconsevents_c.h"
static int posted = 0;
41
42
int
WSCONS_InitKeyboard(_THIS)
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
struct termios tty;
if (ioctl(private->fd, WSKBDIO_GTYPE, &private->kbdType) == -1) {
WSCONS_ReportError("cannot get keyboard type: %s", strerror(errno));
return -1;
}
if (tcgetattr(private->fd, &private->saved_tty) == -1) {
WSCONS_ReportError("cannot get terminal attributes: %s",
strerror(errno));
return -1;
}
private->did_save_tty = 1;
tty = private->saved_tty;
tty.c_iflag = IGNPAR | IGNBRK;
tty.c_oflag = 0;
tty.c_cflag = CREAD | CS8;
tty.c_lflag = 0;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
cfsetispeed(&tty, 9600);
cfsetospeed(&tty, 9600);
if (tcsetattr(private->fd, TCSANOW, &tty) < 0) {
WSCONS_ReportError("cannot set terminal attributes: %s",
strerror(errno));
return -1;
}
if (ioctl(private->fd, KDSKBMODE, K_RAW) == -1) {
WSCONS_ReportError("cannot set raw keyboard mode: %s",
strerror(errno));
return -1;
}
return 0;
78
79
}
80
81
void
WSCONS_ReleaseKeyboard(_THIS)
82
{
83
84
85
86
87
88
89
90
91
92
93
94
95
if (private->fd != -1) {
if (ioctl(private->fd, KDSKBMODE, K_XLATE) == -1) {
WSCONS_ReportError
("cannot restore keyboard to translated mode: %s",
strerror(errno));
}
if (private->did_save_tty) {
if (tcsetattr(private->fd, TCSANOW, &private->saved_tty) < 0) {
WSCONS_ReportError
("cannot restore keynoard attributes: %s",
strerror(errno));
}
}
96
97
98
}
}
99
100
static void
updateMouse()
101
102
103
104
105
{
}
static SDLKey keymap[128];
106
107
static SDL_keysym *
TranslateKey(int scancode, SDL_keysym * keysym)
108
{
109
110
111
keysym->scancode = scancode;
keysym->sym = SDLK_UNKNOWN;
keysym->mod = KMOD_NONE;
112
113
114
if (scancode < SDL_arraysize(keymap))
keysym->sym = keymap[scancode];
115
116
117
if (keysym->sym == SDLK_UNKNOWN)
printf("Unknown mapping for scancode %d\n", scancode);
118
119
return keysym;
120
121
}
122
123
static void
updateKeyboard(_THIS)
124
{
125
126
127
128
129
130
131
132
133
134
135
136
137
unsigned char buf[100];
SDL_keysym keysym;
int n, i;
if ((n = read(private->fd, buf, sizeof(buf))) > 0) {
for (i = 0; i < n; i++) {
unsigned char c = buf[i] & 0x7f;
if (c == 224) // special key prefix -- what should we do with it?
continue;
posted +=
SDL_PrivateKeyboard((buf[i] & 0x80) ? SDL_RELEASED :
SDL_PRESSED, TranslateKey(c, &keysym));
}
138
139
140
}
}
141
142
void
WSCONS_PumpEvents(_THIS)
143
{
144
145
146
147
148
149
do {
posted = 0;
updateMouse();
updateKeyboard(this);
}
while (posted);
150
151
}
152
153
void
WSCONS_InitOSKeymap(_THIS)
154
{
155
int i;
156
157
158
159
160
/* Make sure unknown keys are mapped correctly */
for (i = 0; i < SDL_arraysize(keymap); i++) {
keymap[i] = SDLK_UNKNOWN;
}
161
162
switch (private->kbdType) {
163
#ifdef WSKBD_TYPE_ZAURUS
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
case WSKBD_TYPE_ZAURUS:
/* top row */
keymap[2] = SDLK_1;
keymap[3] = SDLK_2;
keymap[4] = SDLK_3;
keymap[5] = SDLK_4;
keymap[6] = SDLK_5;
keymap[7] = SDLK_6;
keymap[8] = SDLK_7;
keymap[9] = SDLK_8;
keymap[10] = SDLK_9;
keymap[11] = SDLK_0;
keymap[14] = SDLK_BACKSPACE;
/* second row */
keymap[16] = SDLK_q;
keymap[17] = SDLK_w;
keymap[18] = SDLK_e;
keymap[19] = SDLK_r;
keymap[20] = SDLK_t;
keymap[21] = SDLK_y;
keymap[22] = SDLK_u;
keymap[23] = SDLK_i;
keymap[24] = SDLK_o;
keymap[25] = SDLK_p;
/* third row */
keymap[15] = SDLK_TAB;
keymap[30] = SDLK_a;
keymap[31] = SDLK_s;
keymap[32] = SDLK_d;
keymap[33] = SDLK_f;
keymap[34] = SDLK_g;
keymap[35] = SDLK_h;
keymap[36] = SDLK_j;
keymap[37] = SDLK_k;
keymap[38] = SDLK_l;
/* fourth row */
keymap[42] = SDLK_LSHIFT;
keymap[44] = SDLK_z;
keymap[45] = SDLK_x;
keymap[46] = SDLK_c;
keymap[47] = SDLK_v;
keymap[48] = SDLK_b;
keymap[49] = SDLK_n;
keymap[50] = SDLK_m;
keymap[54] = SDLK_RSHIFT;
keymap[28] = SDLK_RETURN;
/* fifth row */
keymap[56] = SDLK_LALT;
keymap[29] = SDLK_LCTRL;
/* keymap[56] = ; */
keymap[0] = SDLK_LSUPER;
keymap[12] = SDLK_MINUS;
keymap[57] = SDLK_SPACE;
keymap[51] = SDLK_COMMA;
keymap[52] = SDLK_PERIOD;
/* misc */
keymap[59] = SDLK_F1;
keymap[60] = SDLK_F2;
keymap[61] = SDLK_F3;
keymap[62] = SDLK_F4;
keymap[63] = SDLK_F5;
keymap[1] = SDLK_ESCAPE;
/* keymap[28] = SDLK_KP_ENTER; */
keymap[72] = SDLK_UP;
keymap[75] = SDLK_LEFT;
keymap[77] = SDLK_RIGHT;
keymap[80] = SDLK_DOWN;
break;
237
#endif /* WSKBD_TYPE_ZAURUS */
238
239
240
241
242
243
default:
WSCONS_ReportError("Unable to map keys for keyboard type %u",
private->kbdType);
break;
}
244
245
246
}
/* end of SDL_wsconsevents.c ... */
247
/* vi: set ts=4 sw=4 expandtab: */