Commit 5af50961 authored by Sam Lantinga's avatar Sam Lantinga

Applied David MacCormack's patch to fix SDL_WINDOWID on Windows,

and added a function to cache the application handle so DirectInput
still works properly.

--HG--
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/trunk%40146
parent dac38725
...@@ -64,7 +64,9 @@ extern "C" { ...@@ -64,7 +64,9 @@ extern "C" {
#endif #endif
/* This should be called from your WinMain() function, if any */ /* This should be called from your WinMain() function, if any */
extern DECLSPEC int SDL_RegisterApp(char *name, Uint32 style, void *hInst); extern DECLSPEC void SDL_SetModuleHandle(HMODULE hInst);
/* This can also be called, but is no longer necessary */
extern DECLSPEC int SDL_RegisterApp(char *name, Uint32 style, HMODULE hInst);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
...@@ -199,12 +199,16 @@ int console_main(int argc, char *argv[]) ...@@ -199,12 +199,16 @@ int console_main(int argc, char *argv[])
atexit(SDL_Quit); atexit(SDL_Quit);
#ifndef DISABLE_VIDEO #ifndef DISABLE_VIDEO
/* Create and register our class */ /* Create and register our class *
DJM: If we do this here, the user nevers gets a chance to
putenv(SDL_WINDOWID). This is already called later by
the (DIB|DX5)_CreateWindow function, so it should be
safe to comment it out here.
if ( SDL_RegisterApp(appname, CS_BYTEALIGNCLIENT, if ( SDL_RegisterApp(appname, CS_BYTEALIGNCLIENT,
GetModuleHandle(NULL)) < 0 ) { GetModuleHandle(NULL)) < 0 ) {
ShowError("WinMain() error", SDL_GetError()); ShowError("WinMain() error", SDL_GetError());
exit(1); exit(1);
} }*/
#endif /* !DISABLE_VIDEO */ #endif /* !DISABLE_VIDEO */
/* Run the application main() code */ /* Run the application main() code */
......
...@@ -174,4 +174,6 @@ ...@@ -174,4 +174,6 @@
SDL_WM_IconifyWindow SDL_WM_IconifyWindow
SDL_WM_ToggleFullScreen SDL_WM_ToggleFullScreen
SDL_WM_GrabInput SDL_WM_GrabInput
SDL_SoftStretch
SDL_RegisterApp SDL_RegisterApp
SDL_SetModuleHandle
...@@ -20,3 +20,4 @@ while ( ($file = shift(@ARGV)) ) { ...@@ -20,3 +20,4 @@ while ( ($file = shift(@ARGV)) ) {
} }
# Special exports not in the header files # Special exports not in the header files
print "\tSDL_RegisterApp\n"; print "\tSDL_RegisterApp\n";
print "\tSDL_SetModuleHandle\n";
...@@ -87,4 +87,8 @@ extern int mouse_relative; ...@@ -87,4 +87,8 @@ extern int mouse_relative;
/* This is really from SDL_dx5audio.c */ /* This is really from SDL_dx5audio.c */
extern void DX5_SoundFocus(HWND window); extern void DX5_SoundFocus(HWND window);
/* DJM: This is really from SDL_sysevents.c, we need it in
GDL_CreateWindow as well */
LONG CALLBACK WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
#endif /* SDL_lowvideo_h */ #endif /* SDL_lowvideo_h */
...@@ -145,8 +145,10 @@ static void WIN_GetKeyboardState(void) ...@@ -145,8 +145,10 @@ static void WIN_GetKeyboardState(void)
#endif /* !NO_GETKEYBOARDSTATE */ #endif /* !NO_GETKEYBOARDSTATE */
} }
/* The main Win32 event handler */ /* The main Win32 event handler
static LONG CALLBACK WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) DJM: This is no longer static as (DX5/DIB)_CreateWindow needs it
*/
LONG CALLBACK WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ {
SDL_VideoDevice *this = current_video; SDL_VideoDevice *this = current_video;
static int mouse_pressed = 0; static int mouse_pressed = 0;
...@@ -265,6 +267,13 @@ static LONG CALLBACK WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara ...@@ -265,6 +267,13 @@ static LONG CALLBACK WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
Sint16 x, y; Sint16 x, y;
Uint8 button, state; Uint8 button, state;
/* DJM:
We want the SDL window to take focus so that
it acts like a normal windows "component"
(e.g. gains keyboard focus on a mouse click).
*/
SetFocus(SDL_Window);
/* Figure out which button to use */ /* Figure out which button to use */
switch (msg) { switch (msg) {
case WM_LBUTTONDOWN: case WM_LBUTTONDOWN:
...@@ -465,10 +474,11 @@ static LONG CALLBACK WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara ...@@ -465,10 +474,11 @@ static LONG CALLBACK WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
} }
return(0); return(0);
/* DJM: Send an expose event in this case */
case WM_ERASEBKGND: { case WM_ERASEBKGND: {
/* Just do nothing */ ; posted = SDL_PrivateExpose();
} }
return(1); return(0);
case WM_CLOSE: { case WM_CLOSE: {
if ( (posted = SDL_PrivateQuit()) ) if ( (posted = SDL_PrivateQuit()) )
...@@ -493,11 +503,35 @@ static LONG CALLBACK WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara ...@@ -493,11 +503,35 @@ static LONG CALLBACK WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
return(DefWindowProc(hwnd, msg, wParam, lParam)); return(DefWindowProc(hwnd, msg, wParam, lParam));
} }
/* Allow the application handle to be stored and retrieved later */
static HMODULE SDL_handle = NULL;
void SDL_SetModuleHandle(HMODULE handle)
{
SDL_handle = handle;
}
HMODULE SDL_GetModuleHandle(void)
{
void *handle;
if ( SDL_handle ) {
handle = SDL_handle;
} else {
/* Warning:
If SDL is built as a DLL, this will return a handle to
the DLL, not the application, and DirectInput may fail
to initialize.
*/
handle = GetModuleHandle(NULL);
}
return(handle);
}
/* This allows the SDL_WINDOWID hack */ /* This allows the SDL_WINDOWID hack */
const char *SDL_windowid = NULL; const char *SDL_windowid = NULL;
/* Register the class for this application -- exported for winmain.c */ /* Register the class for this application -- exported for winmain.c */
int SDL_RegisterApp(char *name, Uint32 style, void *hInst) int SDL_RegisterApp(char *name, Uint32 style, HMODULE hInst)
{ {
static int initialized = 0; static int initialized = 0;
WNDCLASS class; WNDCLASS class;
...@@ -511,12 +545,10 @@ int SDL_RegisterApp(char *name, Uint32 style, void *hInst) ...@@ -511,12 +545,10 @@ int SDL_RegisterApp(char *name, Uint32 style, void *hInst)
} }
/* This function needs to be passed the correct process handle /* This function needs to be passed the correct process handle
by the application. The following call just returns a handle by the application.
to the SDL DLL, which is useless for our purposes and causes
DirectInput to fail to initialize.
*/ */
if ( ! hInst ) { if ( ! hInst ) {
hInst = GetModuleHandle(NULL); hInst = SDL_GetModuleHandle();
} }
/* Register the application class */ /* Register the application class */
......
...@@ -54,6 +54,10 @@ static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, in ...@@ -54,6 +54,10 @@ static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, in
#define REPEATED_KEYMASK (1<<30) #define REPEATED_KEYMASK (1<<30)
#define EXTENDED_KEYMASK (1<<24) #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;
/* The main Win32 event handler */ /* The main Win32 event handler */
LONG LONG
DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) DIB_HandleMessage(_THIS, HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
...@@ -150,6 +154,13 @@ LONG ...@@ -150,6 +154,13 @@ LONG
wmmsg.wParam = wParam; wmmsg.wParam = wParam;
wmmsg.lParam = lParam; wmmsg.lParam = lParam;
posted = SDL_PrivateSysWMEvent(&wmmsg); 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 userWindowProc(hwnd, msg, wParam, lParam);
} }
} }
break; break;
...@@ -339,6 +350,14 @@ int DIB_CreateWindow(_THIS) ...@@ -339,6 +350,14 @@ int DIB_CreateWindow(_THIS)
SDL_RegisterApp("SDL_app", CS_BYTEALIGNCLIENT, 0); SDL_RegisterApp("SDL_app", CS_BYTEALIGNCLIENT, 0);
if ( SDL_windowid ) { if ( SDL_windowid ) {
SDL_Window = (HWND)strtol(SDL_windowid, NULL, 0); SDL_Window = (HWND)strtol(SDL_windowid, NULL, 0);
/* DJM: we want all event's for the user specified
window to be handled by SDL.
*/
if (SDL_Window) {
userWindowProc = (WNDPROC)GetWindowLong(SDL_Window, GWL_WNDPROC);
SetWindowLong(SDL_Window, GWL_WNDPROC, (LONG)WinMessage);
}
} else { } else {
SDL_Window = CreateWindow(SDL_Appname, SDL_Appname, SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
(WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX), (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
......
...@@ -574,7 +574,10 @@ SDL_Surface *DIB_SetVideoMode(_THIS, SDL_Surface *current, ...@@ -574,7 +574,10 @@ SDL_Surface *DIB_SetVideoMode(_THIS, SDL_Surface *current,
if (IsZoomed(SDL_Window)) style |= WS_MAXIMIZE; if (IsZoomed(SDL_Window)) style |= WS_MAXIMIZE;
#endif #endif
} }
SetWindowLong(SDL_Window, GWL_STYLE, style);
/* DJM: Don't piss of anyone who has setup his own window */
if (!SDL_windowid)
SetWindowLong(SDL_Window, GWL_STYLE, style);
/* Delete the old bitmap if necessary */ /* Delete the old bitmap if necessary */
if ( screen_bmp != NULL ) { if ( screen_bmp != NULL ) {
......
...@@ -59,6 +59,10 @@ static int mouse_pressed; ...@@ -59,6 +59,10 @@ static int mouse_pressed;
static SDLKey DIK_keymap[256]; static SDLKey DIK_keymap[256];
static SDL_keysym *TranslateKey(UINT scancode, SDL_keysym *keysym, int pressed); static SDL_keysym *TranslateKey(UINT scancode, SDL_keysym *keysym, int pressed);
/* 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;
/* Convert a DirectInput return code to a text message */ /* Convert a DirectInput return code to a text message */
static void SetDIerror(char *function, int code) static void SetDIerror(char *function, int code)
{ {
...@@ -509,7 +513,14 @@ LONG ...@@ -509,7 +513,14 @@ LONG
wmmsg.wParam = wParam; wmmsg.wParam = wParam;
wmmsg.lParam = lParam; wmmsg.lParam = lParam;
posted = SDL_PrivateSysWMEvent(&wmmsg); 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 userWindowProc(hwnd, msg, wParam, lParam);
}
} }
break; break;
} }
...@@ -764,6 +775,14 @@ int DX5_CreateWindow(_THIS) ...@@ -764,6 +775,14 @@ int DX5_CreateWindow(_THIS)
SDL_RegisterApp("SDL_app", CS_BYTEALIGNCLIENT, 0); SDL_RegisterApp("SDL_app", CS_BYTEALIGNCLIENT, 0);
if ( SDL_windowid ) { if ( SDL_windowid ) {
SDL_Window = (HWND)strtol(SDL_windowid, NULL, 0); SDL_Window = (HWND)strtol(SDL_windowid, NULL, 0);
/* DJM: we want all event's for the user specified
window to be handled by SDL.
*/
if (SDL_Window) {
userWindowProc = (WNDPROC)GetWindowLong(SDL_Window, GWL_WNDPROC);
SetWindowLong(SDL_Window, GWL_WNDPROC, (LONG)WinMessage);
}
} else { } else {
SDL_Window = CreateWindow(SDL_Appname, SDL_Appname, SDL_Window = CreateWindow(SDL_Appname, SDL_Appname,
(WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX), (WS_OVERLAPPED|WS_CAPTION|WS_SYSMENU|WS_MINIMIZEBOX),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment