This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_win32keyboard.c
324 lines (283 loc) · 9.42 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2010 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
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
23
24
#include "SDL_win32video.h"
25
26
#include "../../events/SDL_keyboard_c.h"
27
#include "../../events/scancodes_win32.h"
28
29
30
31
#include <msctf.h>
#include <imm.h>
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef MAPVK_VK_TO_VSC
#define MAPVK_VK_TO_VSC 0
#endif
#ifndef MAPVK_VSC_TO_VK
#define MAPVK_VSC_TO_VK 1
#endif
#ifndef MAPVK_VK_TO_CHAR
#define MAPVK_VK_TO_CHAR 2
#endif
/* Alphabetic scancodes for PC keyboards */
BYTE alpha_scancodes[26] = {
30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50, 49, 24,
25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44
};
47
48
49
50
BYTE keypad_scancodes[10] = {
82, 79, 80, 81, 75, 76, 77, 71, 72, 73
};
51
52
53
54
55
56
void IME_Disable(SDL_VideoData *videodata, HWND hwnd);
void IME_Enable(SDL_VideoData *videodata, HWND hwnd);
void IME_Init(SDL_VideoData *videodata, HWND hwnd);
void IME_Quit(SDL_VideoData *videodata);
57
58
59
60
void
WIN_InitKeyboard(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
61
62
63
int i;
/* Make sure the alpha scancodes are correct. T isn't usually remapped */
64
if (MapVirtualKey('T', MAPVK_VK_TO_VSC) != alpha_scancodes['T' - 'A']) {
65
#if 0
66
67
printf
("Fixing alpha scancode map, assuming US QWERTY layout!\nPlease send the following 26 lines of output to the SDL mailing list <sdl@libsdl.org>, including a description of your keyboard hardware.\n");
68
#endif
69
for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
70
alpha_scancodes[i] = MapVirtualKey('A' + i, MAPVK_VK_TO_VSC);
71
#if 0
72
printf("%d = %d\n", i, alpha_scancodes[i]);
73
#endif
74
75
}
}
76
if (MapVirtualKey(VK_NUMPAD0, MAPVK_VK_TO_VSC) != keypad_scancodes[0]) {
77
#if 0
78
79
printf
("Fixing keypad scancode map!\nPlease send the following 10 lines of output to the SDL mailing list <sdl@libsdl.org>, including a description of your keyboard hardware.\n");
80
#endif
81
for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) {
82
83
keypad_scancodes[i] =
MapVirtualKey(VK_NUMPAD0 + i, MAPVK_VK_TO_VSC);
84
#if 0
85
printf("%d = %d\n", i, keypad_scancodes[i]);
86
#endif
87
88
}
}
89
90
91
data->key_layout = win32_scancode_table;
92
93
94
95
96
97
98
99
100
data->ime_com_initialized = SDL_FALSE;
data->ime_thread_mgr = 0;
data->ime_initialized = SDL_FALSE;
data->ime_enabled = SDL_FALSE;
data->ime_available = SDL_FALSE;
data->ime_hwnd_main = 0;
data->ime_hwnd_current = 0;
data->ime_himc = 0;
101
WIN_UpdateKeymap();
102
103
104
105
106
107
108
SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Windows");
SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Windows");
}
void
109
WIN_UpdateKeymap()
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
{
int i;
SDL_scancode scancode;
SDLKey keymap[SDL_NUM_SCANCODES];
SDL_GetDefaultKeymap(keymap);
for (i = 0; i < SDL_arraysize(win32_scancode_table); i++) {
/* Make sure this scancode is a valid character scancode */
scancode = win32_scancode_table[i];
if (scancode == SDL_SCANCODE_UNKNOWN ||
(keymap[scancode] & SDLK_SCANCODE_MASK)) {
continue;
}
125
126
127
/* Alphabetic keys are handled specially, since Windows remaps them */
if (i >= 'A' && i <= 'Z') {
128
BYTE vsc = alpha_scancodes[i - 'A'];
129
130
131
132
keymap[scancode] = MapVirtualKey(vsc, MAPVK_VSC_TO_VK) + 0x20;
} else {
keymap[scancode] = (MapVirtualKey(i, MAPVK_VK_TO_CHAR) & 0x7FFF);
}
133
}
134
SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
135
136
137
138
139
}
void
WIN_QuitKeyboard(_THIS)
{
140
IME_Quit((SDL_VideoData *)_this->driverdata);
141
142
}
143
void
144
WIN_StartTextInput(_THIS, SDL_Window *window)
145
{
146
147
148
149
HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
IME_Init(videodata, hwnd);
IME_Enable(videodata, hwnd);
150
151
152
}
void
153
WIN_StopTextInput(_THIS, SDL_Window *window)
154
{
155
156
157
158
HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
IME_Init(videodata, hwnd);
IME_Disable(videodata, hwnd);
159
160
161
162
163
164
165
166
}
void
WIN_SetTextInputRect(_THIS, SDL_Rect *rect)
{
}
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
void
IME_Disable(SDL_VideoData *videodata, HWND hwnd)
{
if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
return;
if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
ImmAssociateContext(videodata->ime_hwnd_current, NULL);
videodata->ime_enabled = SDL_FALSE;
}
void
IME_Enable(SDL_VideoData *videodata, HWND hwnd)
{
if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
return;
if (!videodata->ime_available) {
IME_Disable(videodata, hwnd);
return;
}
if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
ImmAssociateContext(videodata->ime_hwnd_current, videodata->ime_himc);
videodata->ime_enabled = SDL_TRUE;
}
void
IME_Init(SDL_VideoData *videodata, HWND hwnd)
{
if (videodata->ime_initialized)
return;
videodata->ime_hwnd_main = hwnd;
if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) {
videodata->ime_com_initialized = SDL_TRUE;
CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, &videodata->ime_thread_mgr);
}
videodata->ime_initialized = SDL_TRUE;
videodata->ime_hwnd_current = videodata->ime_hwnd_main;
if (videodata->ime_thread_mgr) {
struct ITfDocumentMgr *document_mgr = 0;
if (SUCCEEDED(videodata->ime_thread_mgr->lpVtbl->AssociateFocus(videodata->ime_thread_mgr, hwnd, NULL, &document_mgr))) {
if (document_mgr)
document_mgr->lpVtbl->Release(document_mgr);
}
}
videodata->ime_himc = ImmGetContext(hwnd);
ImmReleaseContext(hwnd, videodata->ime_himc);
if (!videodata->ime_himc) {
videodata->ime_available = SDL_FALSE;
IME_Disable(videodata, hwnd);
return;
}
videodata->ime_available = SDL_TRUE;
IME_Disable(videodata, hwnd);
}
void
IME_Quit(SDL_VideoData *videodata)
{
if (!videodata->ime_initialized)
return;
if (videodata->ime_hwnd_main)
ImmAssociateContext(videodata->ime_hwnd_main, videodata->ime_himc);
videodata->ime_hwnd_main = 0;
videodata->ime_himc = 0;
if (videodata->ime_thread_mgr)
{
videodata->ime_thread_mgr->lpVtbl->Release(videodata->ime_thread_mgr);
videodata->ime_thread_mgr = 0;
}
if (videodata->ime_com_initialized)
{
CoUninitialize();
videodata->ime_com_initialized = SDL_FALSE;
}
videodata->ime_initialized = SDL_FALSE;
}
SDL_bool
IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SDL_VideoData *videodata)
{
SDL_bool trap = SDL_FALSE;
HIMC himc = 0;
WCHAR Buffer[SDL_TEXTINPUTEVENT_TEXT_SIZE / 2];
if (!videodata->ime_initialized || !videodata->ime_available || !videodata->ime_enabled)
return SDL_FALSE;
switch (msg)
{
case WM_INPUTLANGCHANGE:
break;
case WM_IME_SETCONTEXT:
*lParam = 0;
break;
case WM_IME_STARTCOMPOSITION:
trap = SDL_TRUE;
break;
case WM_IME_COMPOSITION:
trap = SDL_TRUE;
himc = ImmGetContext(hwnd);
if (*lParam & GCS_RESULTSTR)
{
LONG Length = 0;
char *s = 0;
Length = ImmGetCompositionStringW(himc, GCS_RESULTSTR, Buffer, sizeof(Buffer) * sizeof(Buffer[0]));
Buffer[Length / sizeof(Buffer[0])] = 0;
s = WIN_StringToUTF8(Buffer);
SDL_SendKeyboardText(s);
SDL_free(s);
}
if (*lParam & GCS_COMPSTR)
{
LONG Length = 0;
DWORD Cursor = 0;
char *s = 0;
Length = ImmGetCompositionStringW(himc, GCS_COMPSTR, Buffer, sizeof(Buffer) * sizeof(Buffer[0]));
Buffer[Length / sizeof(Buffer[0])] = 0;
s = WIN_StringToUTF8(Buffer);
Cursor = LOWORD(ImmGetCompositionStringW(himc, GCS_CURSORPOS, 0, 0));
SDL_SendEditingText(s, Cursor, 0);
SDL_free(s);
}
ImmReleaseContext(hwnd, himc);
break;
case WM_IME_ENDCOMPOSITION:
SDL_SendKeyboardText("");
break;
case WM_IME_NOTIFY:
switch (wParam)
{
case IMN_SETCONVERSIONMODE:
break;
case IMN_SETOPENSTATUS:
break;
case IMN_OPENCANDIDATE:
case IMN_CHANGECANDIDATE:
trap = SDL_TRUE;
break;
case IMN_CLOSECANDIDATE:
trap = SDL_TRUE;
break;
case IMN_PRIVATE:
break;
default:
trap = SDL_TRUE;
break;
}
break;
}
return trap;
}
324
/* vi: set ts=4 sw=4 expandtab: */