Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
324 lines (283 loc) · 9.42 KB

SDL_win32keyboard.c

File metadata and controls

324 lines (283 loc) · 9.42 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Jan 24, 2010
Jan 24, 2010
3
Copyright (C) 1997-2010 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
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
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
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
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
Jul 10, 2006
Jul 10, 2006
24
#include "SDL_win32video.h"
Apr 26, 2001
Apr 26, 2001
25
Jul 10, 2006
Jul 10, 2006
26
#include "../../events/SDL_keyboard_c.h"
Feb 8, 2008
Feb 8, 2008
27
#include "../../events/scancodes_win32.h"
Apr 26, 2001
Apr 26, 2001
28
Jul 12, 2010
Jul 12, 2010
29
30
31
#include <msctf.h>
#include <imm.h>
Feb 9, 2008
Feb 9, 2008
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
};
Aug 27, 2008
Aug 27, 2008
47
Feb 11, 2008
Feb 11, 2008
48
49
50
BYTE keypad_scancodes[10] = {
82, 79, 80, 81, 75, 76, 77, 71, 72, 73
};
Feb 9, 2008
Feb 9, 2008
51
Jul 12, 2010
Jul 12, 2010
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);
Jul 10, 2006
Jul 10, 2006
57
58
59
60
void
WIN_InitKeyboard(_THIS)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
Feb 9, 2008
Feb 9, 2008
61
62
63
int i;
/* Make sure the alpha scancodes are correct. T isn't usually remapped */
Mar 7, 2008
Mar 7, 2008
64
if (MapVirtualKey('T', MAPVK_VK_TO_VSC) != alpha_scancodes['T' - 'A']) {
Aug 26, 2008
Aug 26, 2008
65
#if 0
Aug 26, 2008
Aug 26, 2008
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");
Aug 26, 2008
Aug 26, 2008
68
#endif
Feb 9, 2008
Feb 9, 2008
69
for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
Mar 7, 2008
Mar 7, 2008
70
alpha_scancodes[i] = MapVirtualKey('A' + i, MAPVK_VK_TO_VSC);
Aug 26, 2008
Aug 26, 2008
71
#if 0
Mar 7, 2008
Mar 7, 2008
72
printf("%d = %d\n", i, alpha_scancodes[i]);
Aug 26, 2008
Aug 26, 2008
73
#endif
Feb 9, 2008
Feb 9, 2008
74
75
}
}
Feb 11, 2008
Feb 11, 2008
76
if (MapVirtualKey(VK_NUMPAD0, MAPVK_VK_TO_VSC) != keypad_scancodes[0]) {
Aug 26, 2008
Aug 26, 2008
77
#if 0
Aug 26, 2008
Aug 26, 2008
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");
Aug 26, 2008
Aug 26, 2008
80
#endif
Feb 11, 2008
Feb 11, 2008
81
for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) {
Mar 7, 2008
Mar 7, 2008
82
83
keypad_scancodes[i] =
MapVirtualKey(VK_NUMPAD0 + i, MAPVK_VK_TO_VSC);
Aug 26, 2008
Aug 26, 2008
84
#if 0
Mar 7, 2008
Mar 7, 2008
85
printf("%d = %d\n", i, keypad_scancodes[i]);
Aug 26, 2008
Aug 26, 2008
86
#endif
Feb 11, 2008
Feb 11, 2008
87
88
}
}
Apr 26, 2001
Apr 26, 2001
89
Feb 8, 2008
Feb 8, 2008
90
91
data->key_layout = win32_scancode_table;
Jul 12, 2010
Jul 12, 2010
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;
May 10, 2010
May 10, 2010
101
WIN_UpdateKeymap();
Feb 8, 2008
Feb 8, 2008
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
May 10, 2010
May 10, 2010
109
WIN_UpdateKeymap()
Feb 8, 2008
Feb 8, 2008
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;
}
Feb 9, 2008
Feb 9, 2008
125
126
127
/* Alphabetic keys are handled specially, since Windows remaps them */
if (i >= 'A' && i <= 'Z') {
Mar 7, 2008
Mar 7, 2008
128
BYTE vsc = alpha_scancodes[i - 'A'];
Feb 9, 2008
Feb 9, 2008
129
130
131
132
keymap[scancode] = MapVirtualKey(vsc, MAPVK_VSC_TO_VK) + 0x20;
} else {
keymap[scancode] = (MapVirtualKey(i, MAPVK_VK_TO_CHAR) & 0x7FFF);
}
Feb 8, 2008
Feb 8, 2008
133
}
May 10, 2010
May 10, 2010
134
SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
Jul 10, 2006
Jul 10, 2006
135
136
137
138
139
}
void
WIN_QuitKeyboard(_THIS)
{
Jul 12, 2010
Jul 12, 2010
140
IME_Quit((SDL_VideoData *)_this->driverdata);
Jul 10, 2006
Jul 10, 2006
141
142
}
Jul 6, 2010
Jul 6, 2010
143
void
Jul 8, 2010
Jul 8, 2010
144
WIN_StartTextInput(_THIS, SDL_Window *window)
Jul 6, 2010
Jul 6, 2010
145
{
Jul 12, 2010
Jul 12, 2010
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);
Jul 6, 2010
Jul 6, 2010
150
151
152
}
void
Jul 10, 2010
Jul 10, 2010
153
WIN_StopTextInput(_THIS, SDL_Window *window)
Jul 6, 2010
Jul 6, 2010
154
{
Jul 12, 2010
Jul 12, 2010
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);
Jul 6, 2010
Jul 6, 2010
159
160
161
162
163
164
165
166
}
void
WIN_SetTextInputRect(_THIS, SDL_Rect *rect)
{
}
Jul 12, 2010
Jul 12, 2010
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;
}
Jul 10, 2006
Jul 10, 2006
324
/* vi: set ts=4 sw=4 expandtab: */