/* SDL - Simple DirectMedia Layer Copyright (C) 1997-2004 Sam Lantinga This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Sam Lantinga slouken@libsdl.org */ #ifdef SAVE_RCSID static char rcsid = "@(#) $Id$"; #endif #include #include #include #include "SDL_events.h" #include "SDL_error.h" #include "SDL_syswm.h" #include "SDL_sysevents.h" #include "SDL_events_c.h" #include "SDL_lowvideo.h" #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]; static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, int pressed); static BOOL prev_shiftstates[2]; /* Masks for processing the windows KEYDOWN and KEYUP messages */ #define REPEATED_KEYMASK (1<<30) #define EXTENDED_KEYMASK (1<<24) /* 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. */ static WNDPROC userWindowProc = NULL; #ifdef _WIN32_WCE WPARAM rotateKey(WPARAM key,SDL_ScreenOrientation direction) { 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; } #endif /* The main Win32 event handler */ LONG DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { extern int posted; switch (msg) { case WM_SYSKEYDOWN: case WM_KEYDOWN: { SDL_keysym keysym; #ifdef _WIN32_WCE // 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 */ if (!prev_shiftstates[0] && (GetKeyState(VK_LSHIFT) & 0x8000)) { wParam = VK_LSHIFT; prev_shiftstates[0] = TRUE; } else if (!prev_shiftstates[1] && (GetKeyState(VK_RSHIFT) & 0x8000)) { wParam = VK_RSHIFT; prev_shiftstates[1] = TRUE; } else { /* Huh? */ } break; case VK_MENU: if ( lParam&EXTENDED_KEYMASK ) wParam = VK_RMENU; else wParam = VK_LMENU; break; } #ifdef NO_GETKEYBOARDSTATE /* 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; } } #endif /* NO_GETKEYBOARDSTATE */ posted = SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(wParam,HIWORD(lParam),&keysym,1)); } return(0); case WM_SYSKEYUP: case WM_KEYUP: { SDL_keysym keysym; #ifdef _WIN32_WCE // 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 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 */ if (prev_shiftstates[0] && !(GetKeyState(VK_LSHIFT) & 0x8000)) { wParam = VK_LSHIFT; prev_shiftstates[0] = FALSE; } else if (prev_shiftstates[1] && !(GetKeyState(VK_RSHIFT) & 0x8000)) { wParam = VK_RSHIFT; prev_shiftstates[1] = FALSE; } else { /* Huh? */ } break; case VK_MENU: if ( lParam&EXTENDED_KEYMASK ) wParam = VK_RMENU; else wParam = VK_LMENU; break; } posted = SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(wParam,HIWORD(lParam),&keysym,0)); } return(0); #if defined(SC_SCREENSAVE) && defined(SC_MONITORPOWER) case WM_SYSCOMMAND: { if ((wParam&0xFFF0)==SC_SCREENSAVE || (wParam&0xFFF0)==SC_MONITORPOWER) return(0); } /* Fall through to default processing */ #endif /* SC_SCREENSAVE && SC_MONITORPOWER */ 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)); } void DIB_PumpEvents(_THIS) { MSG msg; while ( PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) ) { if ( GetMessage(&msg, NULL, 0, 0) > 0 ) { DispatchMessage(&msg); } } } void DIB_InitOSKeymap(_THIS) { int i; /* Map the VK keysyms */ for ( i=0; iscancode = (unsigned char) scancode; keysym->sym = VK_keymap[vkey]; keysym->mod = KMOD_NONE; keysym->unicode = 0; if ( pressed && SDL_TranslateUNICODE ) { #ifdef NO_GETKEYBOARDSTATE /* Uh oh, better hope the vkey is close enough.. */ keysym->unicode = vkey; #else BYTE keystate[256]; Uint16 wchars[2]; GetKeyboardState(keystate); if (SDL_ToUnicode(vkey, scancode, keystate, wchars, sizeof(wchars)/sizeof(wchars[0]), 0) == 1) { keysym->unicode = wchars[0]; } #endif /* NO_GETKEYBOARDSTATE */ } return(keysym); } int DIB_CreateWindow(_THIS) { char *windowid = getenv("SDL_WINDOWID"); #ifndef CS_BYTEALIGNCLIENT #define CS_BYTEALIGNCLIENT 0 #endif SDL_RegisterApp("SDL_app", CS_BYTEALIGNCLIENT, 0); SDL_windowid = (windowid != NULL); if ( SDL_windowid ) { #if defined(_WIN32_WCE) && (_WIN32_WCE < 300) /* wince 2.1 does not have strtol */ wchar_t *windowid_t = malloc((strlen(windowid) + 1) * sizeof(wchar_t)); MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, windowid, -1, windowid_t, strlen(windowid) + 1); SDL_Window = (HWND)wcstol(windowid_t, NULL, 0); free(windowid_t); #else SDL_Window = (HWND)strtol(windowid, NULL, 0); #endif 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 = (WNDPROC)GetWindowLong(SDL_Window, GWL_WNDPROC); SetWindowLong(SDL_Window, GWL_WNDPROC, (LONG)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); } return(0); } void DIB_DestroyWindow(_THIS) { if ( SDL_windowid ) { SetWindowLong(SDL_Window, GWL_WNDPROC, (LONG)userWindowProc); } else { DestroyWindow(SDL_Window); } }