src/video/windib/SDL_dibevents.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1852 eb2d5480ae95
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
     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     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     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 #define WIN32_LEAN_AND_MEAN
    25 #include <windows.h>
    26 
    27 #include "SDL_main.h"
    28 #include "SDL_events.h"
    29 #include "SDL_syswm.h"
    30 #include "../../events/SDL_sysevents.h"
    31 #include "../../events/SDL_events_c.h"
    32 #include "../wincommon/SDL_lowvideo.h"
    33 #include "SDL_dibvideo.h"
    34 #include "SDL_vkeys.h"
    35 
    36 #ifndef WM_APP
    37 #define WM_APP	0x8000
    38 #endif
    39 
    40 #ifdef _WIN32_WCE
    41 #define NO_GETKEYBOARDSTATE
    42 #endif
    43 
    44 /* The translation table from a Microsoft VK keysym to a SDL keysym */
    45 static SDLKey VK_keymap[SDLK_LAST];
    46 static SDL_keysym *TranslateKey(WPARAM vkey, UINT scancode,
    47                                 SDL_keysym * keysym, int pressed);
    48 
    49 /* Masks for processing the windows KEYDOWN and KEYUP messages */
    50 #define REPEATED_KEYMASK	(1<<30)
    51 #define EXTENDED_KEYMASK	(1<<24)
    52 
    53 /* DJM: If the user setup the window for us, we want to save his window proc,
    54    and give him a chance to handle some messages. */
    55 #ifdef STRICT
    56 #define WNDPROCTYPE	WNDPROC
    57 #else
    58 #define WNDPROCTYPE	FARPROC
    59 #endif
    60 static WNDPROCTYPE userWindowProc = NULL;
    61 
    62 
    63 #ifdef _WIN32_WCE
    64 
    65 WPARAM
    66 rotateKey(WPARAM key, SDL_ScreenOrientation direction)
    67 {
    68     if (direction != SDL_ORIENTATION_LEFT)
    69         return key;
    70 
    71     switch (key) {
    72     case 0x26:                 /* up */
    73         return 0x27;
    74     case 0x27:                 /* right */
    75         return 0x28;
    76     case 0x28:                 /* down */
    77         return 0x25;
    78     case 0x25:                 /* left */
    79         return 0x26;
    80     }
    81 
    82     return key;
    83 }
    84 
    85 #endif
    86 
    87 
    88 /* The main Win32 event handler */
    89 LRESULT
    90 DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    91 {
    92     extern int posted;
    93 
    94     switch (msg) {
    95     case WM_SYSKEYDOWN:
    96     case WM_KEYDOWN:
    97         {
    98             SDL_keysym keysym;
    99 
   100 #ifdef _WIN32_WCE
   101             // Drop GAPI artefacts
   102             if (wParam == 0x84 || wParam == 0x5B)
   103                 return 0;
   104 
   105             // Rotate key if necessary
   106             if (this->hidden->orientation != SDL_ORIENTATION_UP)
   107                 wParam = rotateKey(wParam, this->hidden->orientation);
   108 #endif
   109             /* Ignore repeated keys */
   110             if (lParam & REPEATED_KEYMASK) {
   111                 return (0);
   112             }
   113             switch (wParam) {
   114             case VK_CONTROL:
   115                 if (lParam & EXTENDED_KEYMASK)
   116                     wParam = VK_RCONTROL;
   117                 else
   118                     wParam = VK_LCONTROL;
   119                 break;
   120             case VK_SHIFT:
   121                 /* EXTENDED trick doesn't work here */
   122                 {
   123                     Uint8 *state = SDL_GetKeyState(NULL);
   124                     if (state[SDLK_LSHIFT] == SDL_RELEASED
   125                         && (GetKeyState(VK_LSHIFT) & 0x8000)) {
   126                         wParam = VK_LSHIFT;
   127                     } else if (state[SDLK_RSHIFT] == SDL_RELEASED
   128                                && (GetKeyState(VK_RSHIFT) & 0x8000)) {
   129                         wParam = VK_RSHIFT;
   130                     } else {
   131                         /* Probably a key repeat */
   132                         return (0);
   133                     }
   134                 }
   135                 break;
   136             case VK_MENU:
   137                 if (lParam & EXTENDED_KEYMASK)
   138                     wParam = VK_RMENU;
   139                 else
   140                     wParam = VK_LMENU;
   141                 break;
   142             }
   143 #ifdef NO_GETKEYBOARDSTATE
   144             /* this is the workaround for the missing ToAscii() and ToUnicode() in CE (not necessary at KEYUP!) */
   145             if (SDL_TranslateUNICODE) {
   146                 MSG m;
   147 
   148                 m.hwnd = hwnd;
   149                 m.message = msg;
   150                 m.wParam = wParam;
   151                 m.lParam = lParam;
   152                 m.time = 0;
   153                 if (TranslateMessage(&m)
   154                     && PeekMessage(&m, hwnd, 0, WM_USER, PM_NOREMOVE)
   155                     && (m.message == WM_CHAR)) {
   156                     GetMessage(&m, hwnd, 0, WM_USER);
   157                     wParam = m.wParam;
   158                 }
   159             }
   160 #endif /* NO_GETKEYBOARDSTATE */
   161             posted = SDL_PrivateKeyboard(SDL_PRESSED,
   162                                          TranslateKey(wParam,
   163                                                       HIWORD(lParam),
   164                                                       &keysym, 1));
   165         }
   166         return (0);
   167 
   168     case WM_SYSKEYUP:
   169     case WM_KEYUP:
   170         {
   171             SDL_keysym keysym;
   172 
   173 #ifdef _WIN32_WCE
   174             // Drop GAPI artifacts
   175             if (wParam == 0x84 || wParam == 0x5B)
   176                 return 0;
   177 
   178             // Rotate key if necessary
   179             if (this->hidden->orientation != SDL_ORIENTATION_UP)
   180                 wParam = rotateKey(wParam, this->hidden->orientation);
   181 #endif
   182 
   183             switch (wParam) {
   184             case VK_CONTROL:
   185                 if (lParam & EXTENDED_KEYMASK)
   186                     wParam = VK_RCONTROL;
   187                 else
   188                     wParam = VK_LCONTROL;
   189                 break;
   190             case VK_SHIFT:
   191                 /* EXTENDED trick doesn't work here */
   192                 {
   193                     Uint8 *state = SDL_GetKeyState(NULL);
   194                     if (state[SDLK_LSHIFT] == SDL_PRESSED
   195                         && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
   196                         wParam = VK_LSHIFT;
   197                     } else if (state[SDLK_RSHIFT] == SDL_PRESSED
   198                                && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
   199                         wParam = VK_RSHIFT;
   200                     } else {
   201                         /* Probably a key repeat */
   202                         return (0);
   203                     }
   204                 }
   205                 break;
   206             case VK_MENU:
   207                 if (lParam & EXTENDED_KEYMASK)
   208                     wParam = VK_RMENU;
   209                 else
   210                     wParam = VK_LMENU;
   211                 break;
   212             }
   213             /* Windows only reports keyup for print screen */
   214             if (wParam == VK_SNAPSHOT
   215                 && SDL_GetKeyState(NULL)[SDLK_PRINT] == SDL_RELEASED) {
   216                 posted = SDL_PrivateKeyboard(SDL_PRESSED,
   217                                              TranslateKey(wParam,
   218                                                           HIWORD
   219                                                           (lParam),
   220                                                           &keysym, 1));
   221             }
   222             posted = SDL_PrivateKeyboard(SDL_RELEASED,
   223                                          TranslateKey(wParam,
   224                                                       HIWORD(lParam),
   225                                                       &keysym, 0));
   226         }
   227         return (0);
   228 
   229 #if defined(SC_SCREENSAVE) && defined(SC_MONITORPOWER)
   230     case WM_SYSCOMMAND:
   231         {
   232             if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
   233                 (wParam & 0xFFF0) == SC_MONITORPOWER)
   234                 return (0);
   235         }
   236         /* Fall through to default processing */
   237 #endif /* SC_SCREENSAVE && SC_MONITORPOWER */
   238 
   239     default:
   240         {
   241             /* Only post the event if we're watching for it */
   242             if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
   243                 SDL_SysWMmsg wmmsg;
   244 
   245                 SDL_VERSION(&wmmsg.version);
   246                 wmmsg.hwnd = hwnd;
   247                 wmmsg.msg = msg;
   248                 wmmsg.wParam = wParam;
   249                 wmmsg.lParam = lParam;
   250                 posted = SDL_PrivateSysWMEvent(&wmmsg);
   251 
   252                 /* DJM: If the user isn't watching for private
   253                    messages in her SDL event loop, then pass it
   254                    along to any win32 specific window proc.
   255                  */
   256             } else if (userWindowProc) {
   257                 return CallWindowProc(userWindowProc, hwnd, msg, wParam,
   258                                       lParam);
   259             }
   260         }
   261         break;
   262     }
   263     return (DefWindowProc(hwnd, msg, wParam, lParam));
   264 }
   265 
   266 void
   267 DIB_PumpEvents(_THIS)
   268 {
   269     MSG msg;
   270 
   271     while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
   272         if (GetMessage(&msg, NULL, 0, 0) > 0) {
   273             DispatchMessage(&msg);
   274         }
   275     }
   276 }
   277 
   278 void
   279 DIB_InitOSKeymap(_THIS)
   280 {
   281     int i;
   282 
   283     /* Map the VK keysyms */
   284     for (i = 0; i < SDL_arraysize(VK_keymap); ++i)
   285         VK_keymap[i] = SDLK_UNKNOWN;
   286 
   287     VK_keymap[VK_BACK] = SDLK_BACKSPACE;
   288     VK_keymap[VK_TAB] = SDLK_TAB;
   289     VK_keymap[VK_CLEAR] = SDLK_CLEAR;
   290     VK_keymap[VK_RETURN] = SDLK_RETURN;
   291     VK_keymap[VK_PAUSE] = SDLK_PAUSE;
   292     VK_keymap[VK_ESCAPE] = SDLK_ESCAPE;
   293     VK_keymap[VK_SPACE] = SDLK_SPACE;
   294     VK_keymap[VK_APOSTROPHE] = SDLK_QUOTE;
   295     VK_keymap[VK_COMMA] = SDLK_COMMA;
   296     VK_keymap[VK_MINUS] = SDLK_MINUS;
   297     VK_keymap[VK_PERIOD] = SDLK_PERIOD;
   298     VK_keymap[VK_SLASH] = SDLK_SLASH;
   299     VK_keymap[VK_0] = SDLK_0;
   300     VK_keymap[VK_1] = SDLK_1;
   301     VK_keymap[VK_2] = SDLK_2;
   302     VK_keymap[VK_3] = SDLK_3;
   303     VK_keymap[VK_4] = SDLK_4;
   304     VK_keymap[VK_5] = SDLK_5;
   305     VK_keymap[VK_6] = SDLK_6;
   306     VK_keymap[VK_7] = SDLK_7;
   307     VK_keymap[VK_8] = SDLK_8;
   308     VK_keymap[VK_9] = SDLK_9;
   309     VK_keymap[VK_SEMICOLON] = SDLK_SEMICOLON;
   310     VK_keymap[VK_EQUALS] = SDLK_EQUALS;
   311     VK_keymap[VK_LBRACKET] = SDLK_LEFTBRACKET;
   312     VK_keymap[VK_BACKSLASH] = SDLK_BACKSLASH;
   313     VK_keymap[VK_OEM_102] = SDLK_LESS;
   314     VK_keymap[VK_RBRACKET] = SDLK_RIGHTBRACKET;
   315     VK_keymap[VK_GRAVE] = SDLK_BACKQUOTE;
   316     VK_keymap[VK_BACKTICK] = SDLK_BACKQUOTE;
   317     VK_keymap[VK_A] = SDLK_a;
   318     VK_keymap[VK_B] = SDLK_b;
   319     VK_keymap[VK_C] = SDLK_c;
   320     VK_keymap[VK_D] = SDLK_d;
   321     VK_keymap[VK_E] = SDLK_e;
   322     VK_keymap[VK_F] = SDLK_f;
   323     VK_keymap[VK_G] = SDLK_g;
   324     VK_keymap[VK_H] = SDLK_h;
   325     VK_keymap[VK_I] = SDLK_i;
   326     VK_keymap[VK_J] = SDLK_j;
   327     VK_keymap[VK_K] = SDLK_k;
   328     VK_keymap[VK_L] = SDLK_l;
   329     VK_keymap[VK_M] = SDLK_m;
   330     VK_keymap[VK_N] = SDLK_n;
   331     VK_keymap[VK_O] = SDLK_o;
   332     VK_keymap[VK_P] = SDLK_p;
   333     VK_keymap[VK_Q] = SDLK_q;
   334     VK_keymap[VK_R] = SDLK_r;
   335     VK_keymap[VK_S] = SDLK_s;
   336     VK_keymap[VK_T] = SDLK_t;
   337     VK_keymap[VK_U] = SDLK_u;
   338     VK_keymap[VK_V] = SDLK_v;
   339     VK_keymap[VK_W] = SDLK_w;
   340     VK_keymap[VK_X] = SDLK_x;
   341     VK_keymap[VK_Y] = SDLK_y;
   342     VK_keymap[VK_Z] = SDLK_z;
   343     VK_keymap[VK_DELETE] = SDLK_DELETE;
   344 
   345     VK_keymap[VK_NUMPAD0] = SDLK_KP0;
   346     VK_keymap[VK_NUMPAD1] = SDLK_KP1;
   347     VK_keymap[VK_NUMPAD2] = SDLK_KP2;
   348     VK_keymap[VK_NUMPAD3] = SDLK_KP3;
   349     VK_keymap[VK_NUMPAD4] = SDLK_KP4;
   350     VK_keymap[VK_NUMPAD5] = SDLK_KP5;
   351     VK_keymap[VK_NUMPAD6] = SDLK_KP6;
   352     VK_keymap[VK_NUMPAD7] = SDLK_KP7;
   353     VK_keymap[VK_NUMPAD8] = SDLK_KP8;
   354     VK_keymap[VK_NUMPAD9] = SDLK_KP9;
   355     VK_keymap[VK_DECIMAL] = SDLK_KP_PERIOD;
   356     VK_keymap[VK_DIVIDE] = SDLK_KP_DIVIDE;
   357     VK_keymap[VK_MULTIPLY] = SDLK_KP_MULTIPLY;
   358     VK_keymap[VK_SUBTRACT] = SDLK_KP_MINUS;
   359     VK_keymap[VK_ADD] = SDLK_KP_PLUS;
   360 
   361     VK_keymap[VK_UP] = SDLK_UP;
   362     VK_keymap[VK_DOWN] = SDLK_DOWN;
   363     VK_keymap[VK_RIGHT] = SDLK_RIGHT;
   364     VK_keymap[VK_LEFT] = SDLK_LEFT;
   365     VK_keymap[VK_INSERT] = SDLK_INSERT;
   366     VK_keymap[VK_HOME] = SDLK_HOME;
   367     VK_keymap[VK_END] = SDLK_END;
   368     VK_keymap[VK_PRIOR] = SDLK_PAGEUP;
   369     VK_keymap[VK_NEXT] = SDLK_PAGEDOWN;
   370 
   371     VK_keymap[VK_F1] = SDLK_F1;
   372     VK_keymap[VK_F2] = SDLK_F2;
   373     VK_keymap[VK_F3] = SDLK_F3;
   374     VK_keymap[VK_F4] = SDLK_F4;
   375     VK_keymap[VK_F5] = SDLK_F5;
   376     VK_keymap[VK_F6] = SDLK_F6;
   377     VK_keymap[VK_F7] = SDLK_F7;
   378     VK_keymap[VK_F8] = SDLK_F8;
   379     VK_keymap[VK_F9] = SDLK_F9;
   380     VK_keymap[VK_F10] = SDLK_F10;
   381     VK_keymap[VK_F11] = SDLK_F11;
   382     VK_keymap[VK_F12] = SDLK_F12;
   383     VK_keymap[VK_F13] = SDLK_F13;
   384     VK_keymap[VK_F14] = SDLK_F14;
   385     VK_keymap[VK_F15] = SDLK_F15;
   386 
   387     VK_keymap[VK_NUMLOCK] = SDLK_NUMLOCK;
   388     VK_keymap[VK_CAPITAL] = SDLK_CAPSLOCK;
   389     VK_keymap[VK_SCROLL] = SDLK_SCROLLOCK;
   390     VK_keymap[VK_RSHIFT] = SDLK_RSHIFT;
   391     VK_keymap[VK_LSHIFT] = SDLK_LSHIFT;
   392     VK_keymap[VK_RCONTROL] = SDLK_RCTRL;
   393     VK_keymap[VK_LCONTROL] = SDLK_LCTRL;
   394     VK_keymap[VK_RMENU] = SDLK_RALT;
   395     VK_keymap[VK_LMENU] = SDLK_LALT;
   396     VK_keymap[VK_RWIN] = SDLK_RSUPER;
   397     VK_keymap[VK_LWIN] = SDLK_LSUPER;
   398 
   399     VK_keymap[VK_HELP] = SDLK_HELP;
   400 #ifdef VK_PRINT
   401     VK_keymap[VK_PRINT] = SDLK_PRINT;
   402 #endif
   403     VK_keymap[VK_SNAPSHOT] = SDLK_PRINT;
   404     VK_keymap[VK_CANCEL] = SDLK_BREAK;
   405     VK_keymap[VK_APPS] = SDLK_MENU;
   406 }
   407 
   408 static SDL_keysym *
   409 TranslateKey(WPARAM vkey, UINT scancode, SDL_keysym * keysym, int pressed)
   410 {
   411     /* Set the keysym information */
   412     keysym->scancode = (unsigned char) scancode;
   413     keysym->sym = VK_keymap[vkey];
   414     keysym->mod = KMOD_NONE;
   415     keysym->unicode = 0;
   416     if (pressed && SDL_TranslateUNICODE) {
   417 #ifdef NO_GETKEYBOARDSTATE
   418         /* Uh oh, better hope the vkey is close enough.. */
   419         keysym->unicode = vkey;
   420 #else
   421         BYTE keystate[256];
   422         Uint16 wchars[2];
   423 
   424         GetKeyboardState(keystate);
   425         if (SDL_ToUnicode
   426             ((UINT) vkey, scancode, keystate, wchars,
   427              sizeof(wchars) / sizeof(wchars[0]), 0) == 1) {
   428             keysym->unicode = wchars[0];
   429         }
   430 #endif /* NO_GETKEYBOARDSTATE */
   431     }
   432     return (keysym);
   433 }
   434 
   435 int
   436 DIB_CreateWindow(_THIS)
   437 {
   438     char *windowid = SDL_getenv("SDL_WINDOWID");
   439 
   440     SDL_RegisterApp(NULL, 0, 0);
   441 
   442     SDL_windowid = (windowid != NULL);
   443     if (SDL_windowid) {
   444 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   445         /* wince 2.1 does not have strtol */
   446         wchar_t *windowid_t =
   447             SDL_malloc((SDL_strlen(windowid) + 1) * sizeof(wchar_t));
   448         MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, windowid, -1,
   449                             windowid_t, SDL_strlen(windowid) + 1);
   450         SDL_Window = (HWND) wcstol(windowid_t, NULL, 0);
   451         SDL_free(windowid_t);
   452 #else
   453         SDL_Window = (HWND) SDL_strtoull(windowid, NULL, 0);
   454 #endif
   455         if (SDL_Window == NULL) {
   456             SDL_SetError("Couldn't get user specified window");
   457             return (-1);
   458         }
   459 
   460         /* DJM: we want all event's for the user specified
   461            window to be handled by SDL.
   462          */
   463         userWindowProc =
   464             (WNDPROCTYPE) GetWindowLongPtr(SDL_Window, GWLP_WNDPROC);
   465         SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR) WinMessage);
   466     } else {
   467         SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
   468                                   (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU
   469                                    | WS_MINIMIZEBOX), CW_USEDEFAULT,
   470                                   CW_USEDEFAULT, 0, 0, NULL, NULL,
   471                                   SDL_Instance, NULL);
   472         if (SDL_Window == NULL) {
   473             SDL_SetError("Couldn't create window");
   474             return (-1);
   475         }
   476         ShowWindow(SDL_Window, SW_HIDE);
   477     }
   478 
   479     /* JC 14 Mar 2006
   480        Flush the message loop or this can cause big problems later
   481        Especially if the user decides to use dialog boxes or assert()!
   482      */
   483     WIN_FlushMessageQueue();
   484 
   485     return (0);
   486 }
   487 
   488 void
   489 DIB_DestroyWindow(_THIS)
   490 {
   491     if (SDL_windowid) {
   492         SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR) userWindowProc);
   493     } else {
   494         DestroyWindow(SDL_Window);
   495     }
   496     SDL_UnregisterApp();
   497 
   498     /* JC 14 Mar 2006
   499        Flush the message loop or this can cause big problems later
   500        Especially if the user decides to use dialog boxes or assert()!
   501      */
   502     WIN_FlushMessageQueue();
   503 }
   504 
   505 /* vi: set ts=4 sw=4 expandtab: */