src/video/windib/SDL_dibevents.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
    41 #define NO_GETKEYBOARDSTATE
    41 #define NO_GETKEYBOARDSTATE
    42 #endif
    42 #endif
    43 
    43 
    44 /* The translation table from a Microsoft VK keysym to a SDL keysym */
    44 /* The translation table from a Microsoft VK keysym to a SDL keysym */
    45 static SDLKey VK_keymap[SDLK_LAST];
    45 static SDLKey VK_keymap[SDLK_LAST];
    46 static SDL_keysym *TranslateKey (WPARAM vkey, UINT scancode,
    46 static SDL_keysym *TranslateKey(WPARAM vkey, UINT scancode,
    47                                  SDL_keysym * keysym, int pressed);
    47                                 SDL_keysym * keysym, int pressed);
    48 
    48 
    49 /* Masks for processing the windows KEYDOWN and KEYUP messages */
    49 /* Masks for processing the windows KEYDOWN and KEYUP messages */
    50 #define REPEATED_KEYMASK	(1<<30)
    50 #define REPEATED_KEYMASK	(1<<30)
    51 #define EXTENDED_KEYMASK	(1<<24)
    51 #define EXTENDED_KEYMASK	(1<<24)
    52 
    52 
    61 
    61 
    62 
    62 
    63 #ifdef _WIN32_WCE
    63 #ifdef _WIN32_WCE
    64 
    64 
    65 WPARAM
    65 WPARAM
    66 rotateKey (WPARAM key, SDL_ScreenOrientation direction)
    66 rotateKey(WPARAM key, SDL_ScreenOrientation direction)
    67 {
    67 {
    68     if (direction != SDL_ORIENTATION_LEFT)
    68     if (direction != SDL_ORIENTATION_LEFT)
    69         return key;
    69         return key;
    70 
    70 
    71     switch (key) {
    71     switch (key) {
    85 #endif
    85 #endif
    86 
    86 
    87 
    87 
    88 /* The main Win32 event handler */
    88 /* The main Win32 event handler */
    89 LRESULT
    89 LRESULT
    90 DIB_HandleMessage (_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    90 DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    91 {
    91 {
    92     extern int posted;
    92     extern int posted;
    93 
    93 
    94     switch (msg) {
    94     switch (msg) {
    95     case WM_SYSKEYDOWN:
    95     case WM_SYSKEYDOWN:
   102             if (wParam == 0x84 || wParam == 0x5B)
   102             if (wParam == 0x84 || wParam == 0x5B)
   103                 return 0;
   103                 return 0;
   104 
   104 
   105             // Rotate key if necessary
   105             // Rotate key if necessary
   106             if (this->hidden->orientation != SDL_ORIENTATION_UP)
   106             if (this->hidden->orientation != SDL_ORIENTATION_UP)
   107                 wParam = rotateKey (wParam, this->hidden->orientation);
   107                 wParam = rotateKey(wParam, this->hidden->orientation);
   108 #endif
   108 #endif
   109             /* Ignore repeated keys */
   109             /* Ignore repeated keys */
   110             if (lParam & REPEATED_KEYMASK) {
   110             if (lParam & REPEATED_KEYMASK) {
   111                 return (0);
   111                 return (0);
   112             }
   112             }
   118                     wParam = VK_LCONTROL;
   118                     wParam = VK_LCONTROL;
   119                 break;
   119                 break;
   120             case VK_SHIFT:
   120             case VK_SHIFT:
   121                 /* EXTENDED trick doesn't work here */
   121                 /* EXTENDED trick doesn't work here */
   122                 {
   122                 {
   123                     Uint8 *state = SDL_GetKeyState (NULL);
   123                     Uint8 *state = SDL_GetKeyState(NULL);
   124                     if (state[SDLK_LSHIFT] == SDL_RELEASED
   124                     if (state[SDLK_LSHIFT] == SDL_RELEASED
   125                         && (GetKeyState (VK_LSHIFT) & 0x8000)) {
   125                         && (GetKeyState(VK_LSHIFT) & 0x8000)) {
   126                         wParam = VK_LSHIFT;
   126                         wParam = VK_LSHIFT;
   127                     } else if (state[SDLK_RSHIFT] == SDL_RELEASED
   127                     } else if (state[SDLK_RSHIFT] == SDL_RELEASED
   128                                && (GetKeyState (VK_RSHIFT) & 0x8000)) {
   128                                && (GetKeyState(VK_RSHIFT) & 0x8000)) {
   129                         wParam = VK_RSHIFT;
   129                         wParam = VK_RSHIFT;
   130                     } else {
   130                     } else {
   131                         /* Probably a key repeat */
   131                         /* Probably a key repeat */
   132                         return (0);
   132                         return (0);
   133                     }
   133                     }
   148                 m.hwnd = hwnd;
   148                 m.hwnd = hwnd;
   149                 m.message = msg;
   149                 m.message = msg;
   150                 m.wParam = wParam;
   150                 m.wParam = wParam;
   151                 m.lParam = lParam;
   151                 m.lParam = lParam;
   152                 m.time = 0;
   152                 m.time = 0;
   153                 if (TranslateMessage (&m)
   153                 if (TranslateMessage(&m)
   154                     && PeekMessage (&m, hwnd, 0, WM_USER, PM_NOREMOVE)
   154                     && PeekMessage(&m, hwnd, 0, WM_USER, PM_NOREMOVE)
   155                     && (m.message == WM_CHAR)) {
   155                     && (m.message == WM_CHAR)) {
   156                     GetMessage (&m, hwnd, 0, WM_USER);
   156                     GetMessage(&m, hwnd, 0, WM_USER);
   157                     wParam = m.wParam;
   157                     wParam = m.wParam;
   158                 }
   158                 }
   159             }
   159             }
   160 #endif /* NO_GETKEYBOARDSTATE */
   160 #endif /* NO_GETKEYBOARDSTATE */
   161             posted = SDL_PrivateKeyboard (SDL_PRESSED,
   161             posted = SDL_PrivateKeyboard(SDL_PRESSED,
   162                                           TranslateKey (wParam,
   162                                          TranslateKey(wParam,
   163                                                         HIWORD (lParam),
   163                                                       HIWORD(lParam),
   164                                                         &keysym, 1));
   164                                                       &keysym, 1));
   165         }
   165         }
   166         return (0);
   166         return (0);
   167 
   167 
   168     case WM_SYSKEYUP:
   168     case WM_SYSKEYUP:
   169     case WM_KEYUP:
   169     case WM_KEYUP:
   175             if (wParam == 0x84 || wParam == 0x5B)
   175             if (wParam == 0x84 || wParam == 0x5B)
   176                 return 0;
   176                 return 0;
   177 
   177 
   178             // Rotate key if necessary
   178             // Rotate key if necessary
   179             if (this->hidden->orientation != SDL_ORIENTATION_UP)
   179             if (this->hidden->orientation != SDL_ORIENTATION_UP)
   180                 wParam = rotateKey (wParam, this->hidden->orientation);
   180                 wParam = rotateKey(wParam, this->hidden->orientation);
   181 #endif
   181 #endif
   182 
   182 
   183             switch (wParam) {
   183             switch (wParam) {
   184             case VK_CONTROL:
   184             case VK_CONTROL:
   185                 if (lParam & EXTENDED_KEYMASK)
   185                 if (lParam & EXTENDED_KEYMASK)
   188                     wParam = VK_LCONTROL;
   188                     wParam = VK_LCONTROL;
   189                 break;
   189                 break;
   190             case VK_SHIFT:
   190             case VK_SHIFT:
   191                 /* EXTENDED trick doesn't work here */
   191                 /* EXTENDED trick doesn't work here */
   192                 {
   192                 {
   193                     Uint8 *state = SDL_GetKeyState (NULL);
   193                     Uint8 *state = SDL_GetKeyState(NULL);
   194                     if (state[SDLK_LSHIFT] == SDL_PRESSED
   194                     if (state[SDLK_LSHIFT] == SDL_PRESSED
   195                         && !(GetKeyState (VK_LSHIFT) & 0x8000)) {
   195                         && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
   196                         wParam = VK_LSHIFT;
   196                         wParam = VK_LSHIFT;
   197                     } else if (state[SDLK_RSHIFT] == SDL_PRESSED
   197                     } else if (state[SDLK_RSHIFT] == SDL_PRESSED
   198                                && !(GetKeyState (VK_RSHIFT) & 0x8000)) {
   198                                && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
   199                         wParam = VK_RSHIFT;
   199                         wParam = VK_RSHIFT;
   200                     } else {
   200                     } else {
   201                         /* Probably a key repeat */
   201                         /* Probably a key repeat */
   202                         return (0);
   202                         return (0);
   203                     }
   203                     }
   210                     wParam = VK_LMENU;
   210                     wParam = VK_LMENU;
   211                 break;
   211                 break;
   212             }
   212             }
   213             /* Windows only reports keyup for print screen */
   213             /* Windows only reports keyup for print screen */
   214             if (wParam == VK_SNAPSHOT
   214             if (wParam == VK_SNAPSHOT
   215                 && SDL_GetKeyState (NULL)[SDLK_PRINT] == SDL_RELEASED) {
   215                 && SDL_GetKeyState(NULL)[SDLK_PRINT] == SDL_RELEASED) {
   216                 posted = SDL_PrivateKeyboard (SDL_PRESSED,
   216                 posted = SDL_PrivateKeyboard(SDL_PRESSED,
   217                                               TranslateKey (wParam,
   217                                              TranslateKey(wParam,
   218                                                             HIWORD
   218                                                           HIWORD
   219                                                             (lParam),
   219                                                           (lParam),
   220                                                             &keysym, 1));
   220                                                           &keysym, 1));
   221             }
   221             }
   222             posted = SDL_PrivateKeyboard (SDL_RELEASED,
   222             posted = SDL_PrivateKeyboard(SDL_RELEASED,
   223                                           TranslateKey (wParam,
   223                                          TranslateKey(wParam,
   224                                                         HIWORD (lParam),
   224                                                       HIWORD(lParam),
   225                                                         &keysym, 0));
   225                                                       &keysym, 0));
   226         }
   226         }
   227         return (0);
   227         return (0);
   228 
   228 
   229 #if defined(SC_SCREENSAVE) && defined(SC_MONITORPOWER)
   229 #if defined(SC_SCREENSAVE) && defined(SC_MONITORPOWER)
   230     case WM_SYSCOMMAND:
   230     case WM_SYSCOMMAND:
   240         {
   240         {
   241             /* Only post the event if we're watching for it */
   241             /* Only post the event if we're watching for it */
   242             if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
   242             if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
   243                 SDL_SysWMmsg wmmsg;
   243                 SDL_SysWMmsg wmmsg;
   244 
   244 
   245                 SDL_VERSION (&wmmsg.version);
   245                 SDL_VERSION(&wmmsg.version);
   246                 wmmsg.hwnd = hwnd;
   246                 wmmsg.hwnd = hwnd;
   247                 wmmsg.msg = msg;
   247                 wmmsg.msg = msg;
   248                 wmmsg.wParam = wParam;
   248                 wmmsg.wParam = wParam;
   249                 wmmsg.lParam = lParam;
   249                 wmmsg.lParam = lParam;
   250                 posted = SDL_PrivateSysWMEvent (&wmmsg);
   250                 posted = SDL_PrivateSysWMEvent(&wmmsg);
   251 
   251 
   252                 /* DJM: If the user isn't watching for private
   252                 /* DJM: If the user isn't watching for private
   253                    messages in her SDL event loop, then pass it
   253                    messages in her SDL event loop, then pass it
   254                    along to any win32 specific window proc.
   254                    along to any win32 specific window proc.
   255                  */
   255                  */
   256             } else if (userWindowProc) {
   256             } else if (userWindowProc) {
   257                 return CallWindowProc (userWindowProc, hwnd, msg, wParam,
   257                 return CallWindowProc(userWindowProc, hwnd, msg, wParam,
   258                                        lParam);
   258                                       lParam);
   259             }
   259             }
   260         }
   260         }
   261         break;
   261         break;
   262     }
   262     }
   263     return (DefWindowProc (hwnd, msg, wParam, lParam));
   263     return (DefWindowProc(hwnd, msg, wParam, lParam));
   264 }
   264 }
   265 
   265 
   266 void
   266 void
   267 DIB_PumpEvents (_THIS)
   267 DIB_PumpEvents(_THIS)
   268 {
   268 {
   269     MSG msg;
   269     MSG msg;
   270 
   270 
   271     while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)) {
   271     while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
   272         if (GetMessage (&msg, NULL, 0, 0) > 0) {
   272         if (GetMessage(&msg, NULL, 0, 0) > 0) {
   273             DispatchMessage (&msg);
   273             DispatchMessage(&msg);
   274         }
   274         }
   275     }
   275     }
   276 }
   276 }
   277 
   277 
   278 void
   278 void
   279 DIB_InitOSKeymap (_THIS)
   279 DIB_InitOSKeymap(_THIS)
   280 {
   280 {
   281     int i;
   281     int i;
   282 
   282 
   283     /* Map the VK keysyms */
   283     /* Map the VK keysyms */
   284     for (i = 0; i < SDL_arraysize (VK_keymap); ++i)
   284     for (i = 0; i < SDL_arraysize(VK_keymap); ++i)
   285         VK_keymap[i] = SDLK_UNKNOWN;
   285         VK_keymap[i] = SDLK_UNKNOWN;
   286 
   286 
   287     VK_keymap[VK_BACK] = SDLK_BACKSPACE;
   287     VK_keymap[VK_BACK] = SDLK_BACKSPACE;
   288     VK_keymap[VK_TAB] = SDLK_TAB;
   288     VK_keymap[VK_TAB] = SDLK_TAB;
   289     VK_keymap[VK_CLEAR] = SDLK_CLEAR;
   289     VK_keymap[VK_CLEAR] = SDLK_CLEAR;
   404     VK_keymap[VK_CANCEL] = SDLK_BREAK;
   404     VK_keymap[VK_CANCEL] = SDLK_BREAK;
   405     VK_keymap[VK_APPS] = SDLK_MENU;
   405     VK_keymap[VK_APPS] = SDLK_MENU;
   406 }
   406 }
   407 
   407 
   408 static SDL_keysym *
   408 static SDL_keysym *
   409 TranslateKey (WPARAM vkey, UINT scancode, SDL_keysym * keysym, int pressed)
   409 TranslateKey(WPARAM vkey, UINT scancode, SDL_keysym * keysym, int pressed)
   410 {
   410 {
   411     /* Set the keysym information */
   411     /* Set the keysym information */
   412     keysym->scancode = (unsigned char) scancode;
   412     keysym->scancode = (unsigned char) scancode;
   413     keysym->sym = VK_keymap[vkey];
   413     keysym->sym = VK_keymap[vkey];
   414     keysym->mod = KMOD_NONE;
   414     keysym->mod = KMOD_NONE;
   419         keysym->unicode = vkey;
   419         keysym->unicode = vkey;
   420 #else
   420 #else
   421         BYTE keystate[256];
   421         BYTE keystate[256];
   422         Uint16 wchars[2];
   422         Uint16 wchars[2];
   423 
   423 
   424         GetKeyboardState (keystate);
   424         GetKeyboardState(keystate);
   425         if (SDL_ToUnicode
   425         if (SDL_ToUnicode
   426             ((UINT) vkey, scancode, keystate, wchars,
   426             ((UINT) vkey, scancode, keystate, wchars,
   427              sizeof (wchars) / sizeof (wchars[0]), 0) == 1) {
   427              sizeof(wchars) / sizeof(wchars[0]), 0) == 1) {
   428             keysym->unicode = wchars[0];
   428             keysym->unicode = wchars[0];
   429         }
   429         }
   430 #endif /* NO_GETKEYBOARDSTATE */
   430 #endif /* NO_GETKEYBOARDSTATE */
   431     }
   431     }
   432     return (keysym);
   432     return (keysym);
   433 }
   433 }
   434 
   434 
   435 int
   435 int
   436 DIB_CreateWindow (_THIS)
   436 DIB_CreateWindow(_THIS)
   437 {
   437 {
   438     char *windowid = SDL_getenv ("SDL_WINDOWID");
   438     char *windowid = SDL_getenv("SDL_WINDOWID");
   439 
   439 
   440     SDL_RegisterApp (NULL, 0, 0);
   440     SDL_RegisterApp(NULL, 0, 0);
   441 
   441 
   442     SDL_windowid = (windowid != NULL);
   442     SDL_windowid = (windowid != NULL);
   443     if (SDL_windowid) {
   443     if (SDL_windowid) {
   444 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   444 #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
   445         /* wince 2.1 does not have strtol */
   445         /* wince 2.1 does not have strtol */
   446         wchar_t *windowid_t =
   446         wchar_t *windowid_t =
   447             SDL_malloc ((SDL_strlen (windowid) + 1) * sizeof (wchar_t));
   447             SDL_malloc((SDL_strlen(windowid) + 1) * sizeof(wchar_t));
   448         MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, windowid, -1,
   448         MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, windowid, -1,
   449                              windowid_t, SDL_strlen (windowid) + 1);
   449                             windowid_t, SDL_strlen(windowid) + 1);
   450         SDL_Window = (HWND) wcstol (windowid_t, NULL, 0);
   450         SDL_Window = (HWND) wcstol(windowid_t, NULL, 0);
   451         SDL_free (windowid_t);
   451         SDL_free(windowid_t);
   452 #else
   452 #else
   453         SDL_Window = (HWND) SDL_strtoull (windowid, NULL, 0);
   453         SDL_Window = (HWND) SDL_strtoull(windowid, NULL, 0);
   454 #endif
   454 #endif
   455         if (SDL_Window == NULL) {
   455         if (SDL_Window == NULL) {
   456             SDL_SetError ("Couldn't get user specified window");
   456             SDL_SetError("Couldn't get user specified window");
   457             return (-1);
   457             return (-1);
   458         }
   458         }
   459 
   459 
   460         /* DJM: we want all event's for the user specified
   460         /* DJM: we want all event's for the user specified
   461            window to be handled by SDL.
   461            window to be handled by SDL.
   462          */
   462          */
   463         userWindowProc =
   463         userWindowProc =
   464             (WNDPROCTYPE) GetWindowLongPtr (SDL_Window, GWLP_WNDPROC);
   464             (WNDPROCTYPE) GetWindowLongPtr(SDL_Window, GWLP_WNDPROC);
   465         SetWindowLongPtr (SDL_Window, GWLP_WNDPROC, (LONG_PTR) WinMessage);
   465         SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR) WinMessage);
   466     } else {
   466     } else {
   467         SDL_Window = CreateWindow (SDL_Appname, SDL_Appname,
   467         SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
   468                                    (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU
   468                                   (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU
   469                                     | WS_MINIMIZEBOX), CW_USEDEFAULT,
   469                                    | WS_MINIMIZEBOX), CW_USEDEFAULT,
   470                                    CW_USEDEFAULT, 0, 0, NULL, NULL,
   470                                   CW_USEDEFAULT, 0, 0, NULL, NULL,
   471                                    SDL_Instance, NULL);
   471                                   SDL_Instance, NULL);
   472         if (SDL_Window == NULL) {
   472         if (SDL_Window == NULL) {
   473             SDL_SetError ("Couldn't create window");
   473             SDL_SetError("Couldn't create window");
   474             return (-1);
   474             return (-1);
   475         }
   475         }
   476         ShowWindow (SDL_Window, SW_HIDE);
   476         ShowWindow(SDL_Window, SW_HIDE);
   477     }
   477     }
   478 
   478 
   479     /* JC 14 Mar 2006
   479     /* JC 14 Mar 2006
   480        Flush the message loop or this can cause big problems later
   480        Flush the message loop or this can cause big problems later
   481        Especially if the user decides to use dialog boxes or assert()!
   481        Especially if the user decides to use dialog boxes or assert()!
   482      */
   482      */
   483     WIN_FlushMessageQueue ();
   483     WIN_FlushMessageQueue();
   484 
   484 
   485     return (0);
   485     return (0);
   486 }
   486 }
   487 
   487 
   488 void
   488 void
   489 DIB_DestroyWindow (_THIS)
   489 DIB_DestroyWindow(_THIS)
   490 {
   490 {
   491     if (SDL_windowid) {
   491     if (SDL_windowid) {
   492         SetWindowLongPtr (SDL_Window, GWLP_WNDPROC,
   492         SetWindowLongPtr(SDL_Window, GWLP_WNDPROC, (LONG_PTR) userWindowProc);
   493                           (LONG_PTR) userWindowProc);
       
   494     } else {
   493     } else {
   495         DestroyWindow (SDL_Window);
   494         DestroyWindow(SDL_Window);
   496     }
   495     }
   497     SDL_UnregisterApp ();
   496     SDL_UnregisterApp();
   498 
   497 
   499     /* JC 14 Mar 2006
   498     /* JC 14 Mar 2006
   500        Flush the message loop or this can cause big problems later
   499        Flush the message loop or this can cause big problems later
   501        Especially if the user decides to use dialog boxes or assert()!
   500        Especially if the user decides to use dialog boxes or assert()!
   502      */
   501      */
   503     WIN_FlushMessageQueue ();
   502     WIN_FlushMessageQueue();
   504 }
   503 }
   505 
   504 
   506 /* vi: set ts=4 sw=4 expandtab: */
   505 /* vi: set ts=4 sw=4 expandtab: */