This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_dibevents.c
506 lines (454 loc) · 15.9 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
Sam Lantinga
20
slouken@libsdl.org
21
*/
22
#include "SDL_config.h"
23
24
25
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
26
27
#include "SDL_main.h"
28
29
#include "SDL_events.h"
#include "SDL_syswm.h"
30
31
32
#include "../../events/SDL_sysevents.h"
#include "../../events/SDL_events_c.h"
#include "../wincommon/SDL_lowvideo.h"
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "SDL_dibvideo.h"
#include "SDL_vkeys.h"
#ifndef WM_APP
#define WM_APP 0x8000
#endif
#ifdef _WIN32_WCE
#define NO_GETKEYBOARDSTATE
#endif
/* The translation table from a Microsoft VK keysym to a SDL keysym */
static SDLKey VK_keymap[SDLK_LAST];
46
47
static SDL_keysym *TranslateKey (WPARAM vkey, UINT scancode,
SDL_keysym * keysym, int pressed);
48
49
50
51
52
/* Masks for processing the windows KEYDOWN and KEYUP messages */
#define REPEATED_KEYMASK (1<<30)
#define EXTENDED_KEYMASK (1<<24)
53
54
/* DJM: If the user setup the window for us, we want to save his window proc,
and give him a chance to handle some messages. */
55
56
57
58
59
60
#ifdef STRICT
#define WNDPROCTYPE WNDPROC
#else
#define WNDPROCTYPE FARPROC
#endif
static WNDPROCTYPE userWindowProc = NULL;
61
62
63
64
#ifdef _WIN32_WCE
65
66
WPARAM
rotateKey (WPARAM key, SDL_ScreenOrientation direction)
67
{
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
if (direction != SDL_ORIENTATION_LEFT)
return key;
switch (key) {
case 0x26: /* up */
return 0x27;
case 0x27: /* right */
return 0x28;
case 0x28: /* down */
return 0x25;
case 0x25: /* left */
return 0x26;
}
return key;
83
84
}
85
#endif
86
87
88
/* The main Win32 event handler */
89
90
LRESULT
DIB_HandleMessage (_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
91
{
92
extern int posted;
93
94
95
96
97
98
switch (msg) {
case WM_SYSKEYDOWN:
case WM_KEYDOWN:
{
SDL_keysym keysym;
99
100
#ifdef _WIN32_WCE
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
// Drop GAPI artefacts
if (wParam == 0x84 || wParam == 0x5B)
return 0;
// Rotate key if necessary
if (this->hidden->orientation != SDL_ORIENTATION_UP)
wParam = rotateKey (wParam, this->hidden->orientation);
#endif
/* Ignore repeated keys */
if (lParam & REPEATED_KEYMASK) {
return (0);
}
switch (wParam) {
case VK_CONTROL:
if (lParam & EXTENDED_KEYMASK)
wParam = VK_RCONTROL;
else
wParam = VK_LCONTROL;
break;
case VK_SHIFT:
/* EXTENDED trick doesn't work here */
{
Uint8 *state = SDL_GetKeyState (NULL);
if (state[SDLK_LSHIFT] == SDL_RELEASED
&& (GetKeyState (VK_LSHIFT) & 0x8000)) {
wParam = VK_LSHIFT;
} else if (state[SDLK_RSHIFT] == SDL_RELEASED
&& (GetKeyState (VK_RSHIFT) & 0x8000)) {
wParam = VK_RSHIFT;
} else {
/* Probably a key repeat */
return (0);
}
}
break;
case VK_MENU:
if (lParam & EXTENDED_KEYMASK)
wParam = VK_RMENU;
else
wParam = VK_LMENU;
break;
}
143
#ifdef NO_GETKEYBOARDSTATE
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* this is the workaround for the missing ToAscii() and ToUnicode() in CE (not necessary at KEYUP!) */
if (SDL_TranslateUNICODE) {
MSG m;
m.hwnd = hwnd;
m.message = msg;
m.wParam = wParam;
m.lParam = lParam;
m.time = 0;
if (TranslateMessage (&m)
&& PeekMessage (&m, hwnd, 0, WM_USER, PM_NOREMOVE)
&& (m.message == WM_CHAR)) {
GetMessage (&m, hwnd, 0, WM_USER);
wParam = m.wParam;
}
}
160
#endif /* NO_GETKEYBOARDSTATE */
161
162
163
164
165
166
167
168
169
170
171
posted = SDL_PrivateKeyboard (SDL_PRESSED,
TranslateKey (wParam,
HIWORD (lParam),
&keysym, 1));
}
return (0);
case WM_SYSKEYUP:
case WM_KEYUP:
{
SDL_keysym keysym;
172
173
#ifdef _WIN32_WCE
174
175
176
// Drop GAPI artifacts
if (wParam == 0x84 || wParam == 0x5B)
return 0;
177
178
179
180
// Rotate key if necessary
if (this->hidden->orientation != SDL_ORIENTATION_UP)
wParam = rotateKey (wParam, this->hidden->orientation);
181
182
#endif
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
switch (wParam) {
case VK_CONTROL:
if (lParam & EXTENDED_KEYMASK)
wParam = VK_RCONTROL;
else
wParam = VK_LCONTROL;
break;
case VK_SHIFT:
/* EXTENDED trick doesn't work here */
{
Uint8 *state = SDL_GetKeyState (NULL);
if (state[SDLK_LSHIFT] == SDL_PRESSED
&& !(GetKeyState (VK_LSHIFT) & 0x8000)) {
wParam = VK_LSHIFT;
} else if (state[SDLK_RSHIFT] == SDL_PRESSED
&& !(GetKeyState (VK_RSHIFT) & 0x8000)) {
wParam = VK_RSHIFT;
} else {
/* Probably a key repeat */
return (0);
}
}
break;
case VK_MENU:
if (lParam & EXTENDED_KEYMASK)
wParam = VK_RMENU;
else
wParam = VK_LMENU;
break;
}
/* Windows only reports keyup for print screen */
if (wParam == VK_SNAPSHOT
&& SDL_GetKeyState (NULL)[SDLK_PRINT] == SDL_RELEASED) {
posted = SDL_PrivateKeyboard (SDL_PRESSED,
TranslateKey (wParam,
HIWORD
(lParam),
&keysym, 1));
}
posted = SDL_PrivateKeyboard (SDL_RELEASED,
TranslateKey (wParam,
HIWORD (lParam),
&keysym, 0));
}
return (0);
228
229
#if defined(SC_SCREENSAVE) && defined(SC_MONITORPOWER)
230
231
232
233
234
235
236
case WM_SYSCOMMAND:
{
if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
(wParam & 0xFFF0) == SC_MONITORPOWER)
return (0);
}
/* Fall through to default processing */
237
238
#endif /* SC_SCREENSAVE && SC_MONITORPOWER */
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
default:
{
/* Only post the event if we're watching for it */
if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
SDL_SysWMmsg wmmsg;
SDL_VERSION (&wmmsg.version);
wmmsg.hwnd = hwnd;
wmmsg.msg = msg;
wmmsg.wParam = wParam;
wmmsg.lParam = lParam;
posted = SDL_PrivateSysWMEvent (&wmmsg);
/* DJM: If the user isn't watching for private
messages in her SDL event loop, then pass it
along to any win32 specific window proc.
*/
} else if (userWindowProc) {
return CallWindowProc (userWindowProc, hwnd, msg, wParam,
lParam);
}
}
break;
}
return (DefWindowProc (hwnd, msg, wParam, lParam));
264
265
}
266
267
void
DIB_PumpEvents (_THIS)
268
{
269
MSG msg;
270
271
272
273
274
275
while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)) {
if (GetMessage (&msg, NULL, 0, 0) > 0) {
DispatchMessage (&msg);
}
}
276
277
}
278
279
void
DIB_InitOSKeymap (_THIS)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
int i;
/* Map the VK keysyms */
for (i = 0; i < SDL_arraysize (VK_keymap); ++i)
VK_keymap[i] = SDLK_UNKNOWN;
VK_keymap[VK_BACK] = SDLK_BACKSPACE;
VK_keymap[VK_TAB] = SDLK_TAB;
VK_keymap[VK_CLEAR] = SDLK_CLEAR;
VK_keymap[VK_RETURN] = SDLK_RETURN;
VK_keymap[VK_PAUSE] = SDLK_PAUSE;
VK_keymap[VK_ESCAPE] = SDLK_ESCAPE;
VK_keymap[VK_SPACE] = SDLK_SPACE;
VK_keymap[VK_APOSTROPHE] = SDLK_QUOTE;
VK_keymap[VK_COMMA] = SDLK_COMMA;
VK_keymap[VK_MINUS] = SDLK_MINUS;
VK_keymap[VK_PERIOD] = SDLK_PERIOD;
VK_keymap[VK_SLASH] = SDLK_SLASH;
VK_keymap[VK_0] = SDLK_0;
VK_keymap[VK_1] = SDLK_1;
VK_keymap[VK_2] = SDLK_2;
VK_keymap[VK_3] = SDLK_3;
VK_keymap[VK_4] = SDLK_4;
VK_keymap[VK_5] = SDLK_5;
VK_keymap[VK_6] = SDLK_6;
VK_keymap[VK_7] = SDLK_7;
VK_keymap[VK_8] = SDLK_8;
VK_keymap[VK_9] = SDLK_9;
VK_keymap[VK_SEMICOLON] = SDLK_SEMICOLON;
VK_keymap[VK_EQUALS] = SDLK_EQUALS;
VK_keymap[VK_LBRACKET] = SDLK_LEFTBRACKET;
VK_keymap[VK_BACKSLASH] = SDLK_BACKSLASH;
VK_keymap[VK_OEM_102] = SDLK_LESS;
VK_keymap[VK_RBRACKET] = SDLK_RIGHTBRACKET;
VK_keymap[VK_GRAVE] = SDLK_BACKQUOTE;
VK_keymap[VK_BACKTICK] = SDLK_BACKQUOTE;
VK_keymap[VK_A] = SDLK_a;
VK_keymap[VK_B] = SDLK_b;
VK_keymap[VK_C] = SDLK_c;
VK_keymap[VK_D] = SDLK_d;
VK_keymap[VK_E] = SDLK_e;
VK_keymap[VK_F] = SDLK_f;
VK_keymap[VK_G] = SDLK_g;
VK_keymap[VK_H] = SDLK_h;
VK_keymap[VK_I] = SDLK_i;
VK_keymap[VK_J] = SDLK_j;
VK_keymap[VK_K] = SDLK_k;
VK_keymap[VK_L] = SDLK_l;
VK_keymap[VK_M] = SDLK_m;
VK_keymap[VK_N] = SDLK_n;
VK_keymap[VK_O] = SDLK_o;
VK_keymap[VK_P] = SDLK_p;
VK_keymap[VK_Q] = SDLK_q;
VK_keymap[VK_R] = SDLK_r;
VK_keymap[VK_S] = SDLK_s;
VK_keymap[VK_T] = SDLK_t;
VK_keymap[VK_U] = SDLK_u;
VK_keymap[VK_V] = SDLK_v;
VK_keymap[VK_W] = SDLK_w;
VK_keymap[VK_X] = SDLK_x;
VK_keymap[VK_Y] = SDLK_y;
VK_keymap[VK_Z] = SDLK_z;
VK_keymap[VK_DELETE] = SDLK_DELETE;
VK_keymap[VK_NUMPAD0] = SDLK_KP0;
VK_keymap[VK_NUMPAD1] = SDLK_KP1;
VK_keymap[VK_NUMPAD2] = SDLK_KP2;
VK_keymap[VK_NUMPAD3] = SDLK_KP3;
VK_keymap[VK_NUMPAD4] = SDLK_KP4;
VK_keymap[VK_NUMPAD5] = SDLK_KP5;
VK_keymap[VK_NUMPAD6] = SDLK_KP6;
VK_keymap[VK_NUMPAD7] = SDLK_KP7;
VK_keymap[VK_NUMPAD8] = SDLK_KP8;
VK_keymap[VK_NUMPAD9] = SDLK_KP9;
VK_keymap[VK_DECIMAL] = SDLK_KP_PERIOD;
VK_keymap[VK_DIVIDE] = SDLK_KP_DIVIDE;
VK_keymap[VK_MULTIPLY] = SDLK_KP_MULTIPLY;
VK_keymap[VK_SUBTRACT] = SDLK_KP_MINUS;
VK_keymap[VK_ADD] = SDLK_KP_PLUS;
VK_keymap[VK_UP] = SDLK_UP;
VK_keymap[VK_DOWN] = SDLK_DOWN;
VK_keymap[VK_RIGHT] = SDLK_RIGHT;
VK_keymap[VK_LEFT] = SDLK_LEFT;
VK_keymap[VK_INSERT] = SDLK_INSERT;
VK_keymap[VK_HOME] = SDLK_HOME;
VK_keymap[VK_END] = SDLK_END;
VK_keymap[VK_PRIOR] = SDLK_PAGEUP;
VK_keymap[VK_NEXT] = SDLK_PAGEDOWN;
VK_keymap[VK_F1] = SDLK_F1;
VK_keymap[VK_F2] = SDLK_F2;
VK_keymap[VK_F3] = SDLK_F3;
VK_keymap[VK_F4] = SDLK_F4;
VK_keymap[VK_F5] = SDLK_F5;
VK_keymap[VK_F6] = SDLK_F6;
VK_keymap[VK_F7] = SDLK_F7;
VK_keymap[VK_F8] = SDLK_F8;
VK_keymap[VK_F9] = SDLK_F9;
VK_keymap[VK_F10] = SDLK_F10;
VK_keymap[VK_F11] = SDLK_F11;
VK_keymap[VK_F12] = SDLK_F12;
VK_keymap[VK_F13] = SDLK_F13;
VK_keymap[VK_F14] = SDLK_F14;
VK_keymap[VK_F15] = SDLK_F15;
VK_keymap[VK_NUMLOCK] = SDLK_NUMLOCK;
VK_keymap[VK_CAPITAL] = SDLK_CAPSLOCK;
VK_keymap[VK_SCROLL] = SDLK_SCROLLOCK;
VK_keymap[VK_RSHIFT] = SDLK_RSHIFT;
VK_keymap[VK_LSHIFT] = SDLK_LSHIFT;
VK_keymap[VK_RCONTROL] = SDLK_RCTRL;
VK_keymap[VK_LCONTROL] = SDLK_LCTRL;
VK_keymap[VK_RMENU] = SDLK_RALT;
VK_keymap[VK_LMENU] = SDLK_LALT;
VK_keymap[VK_RWIN] = SDLK_RSUPER;
VK_keymap[VK_LWIN] = SDLK_LSUPER;
VK_keymap[VK_HELP] = SDLK_HELP;
400
#ifdef VK_PRINT
401
VK_keymap[VK_PRINT] = SDLK_PRINT;
402
#endif
403
404
405
VK_keymap[VK_SNAPSHOT] = SDLK_PRINT;
VK_keymap[VK_CANCEL] = SDLK_BREAK;
VK_keymap[VK_APPS] = SDLK_MENU;
406
407
}
408
409
static SDL_keysym *
TranslateKey (WPARAM vkey, UINT scancode, SDL_keysym * keysym, int pressed)
410
{
411
412
413
414
415
416
/* Set the keysym information */
keysym->scancode = (unsigned char) scancode;
keysym->sym = VK_keymap[vkey];
keysym->mod = KMOD_NONE;
keysym->unicode = 0;
if (pressed && SDL_TranslateUNICODE) {
417
#ifdef NO_GETKEYBOARDSTATE
418
419
/* Uh oh, better hope the vkey is close enough.. */
keysym->unicode = vkey;
420
#else
421
422
423
424
425
426
427
428
429
BYTE keystate[256];
Uint16 wchars[2];
GetKeyboardState (keystate);
if (SDL_ToUnicode
((UINT) vkey, scancode, keystate, wchars,
sizeof (wchars) / sizeof (wchars[0]), 0) == 1) {
keysym->unicode = wchars[0];
}
430
#endif /* NO_GETKEYBOARDSTATE */
431
432
}
return (keysym);
433
434
}
435
436
int
DIB_CreateWindow (_THIS)
437
{
438
char *windowid = SDL_getenv ("SDL_WINDOWID");
439
440
SDL_RegisterApp (NULL, 0, 0);
441
442
443
SDL_windowid = (windowid != NULL);
if (SDL_windowid) {
444
#if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
445
446
447
448
449
450
451
/* wince 2.1 does not have strtol */
wchar_t *windowid_t =
SDL_malloc ((SDL_strlen (windowid) + 1) * sizeof (wchar_t));
MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, windowid, -1,
windowid_t, SDL_strlen (windowid) + 1);
SDL_Window = (HWND) wcstol (windowid_t, NULL, 0);
SDL_free (windowid_t);
452
#else
453
SDL_Window = (HWND) SDL_strtoull (windowid, NULL, 0);
454
#endif
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
if (SDL_Window == NULL) {
SDL_SetError ("Couldn't get user specified window");
return (-1);
}
/* DJM: we want all event's for the user specified
window to be handled by SDL.
*/
userWindowProc =
(WNDPROCTYPE) GetWindowLongPtr (SDL_Window, GWLP_WNDPROC);
SetWindowLongPtr (SDL_Window, GWLP_WNDPROC, (LONG_PTR) WinMessage);
} else {
SDL_Window = CreateWindow (SDL_Appname, SDL_Appname,
(WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU
| WS_MINIMIZEBOX), CW_USEDEFAULT,
CW_USEDEFAULT, 0, 0, NULL, NULL,
SDL_Instance, NULL);
if (SDL_Window == NULL) {
SDL_SetError ("Couldn't create window");
return (-1);
}
ShowWindow (SDL_Window, SW_HIDE);
}
/* JC 14 Mar 2006
Flush the message loop or this can cause big problems later
Especially if the user decides to use dialog boxes or assert()!
*/
WIN_FlushMessageQueue ();
return (0);
486
487
}
488
489
void
DIB_DestroyWindow (_THIS)
490
{
491
492
493
494
495
496
497
498
499
500
501
502
503
if (SDL_windowid) {
SetWindowLongPtr (SDL_Window, GWLP_WNDPROC,
(LONG_PTR) userWindowProc);
} else {
DestroyWindow (SDL_Window);
}
SDL_UnregisterApp ();
/* JC 14 Mar 2006
Flush the message loop or this can cause big problems later
Especially if the user decides to use dialog boxes or assert()!
*/
WIN_FlushMessageQueue ();
504
}
505
506
/* vi: set ts=4 sw=4 expandtab: */