From 2bc6d2f0c26ba2e9a393af46383add3378a86695 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Tue, 27 Jun 2006 07:46:36 +0000 Subject: [PATCH] Further progress on the new Windows video driver: * SDL_SetModuleHandle() is obsolete, I hope. * SDL 1.3 uses the UNICODE API * I'm ignoring Windows CE for the moment, we'll reevaluate what needs to be different for Windows CE later. * Pulled the stdio redirection from WinMain() --- include/SDL_compat.h | 1 + include/SDL_main.h | 6 +- src/SDL.c | 6 - src/main/win32/SDL_win32_main.c | 197 +----------------------------- src/video/win32/SDL_win32events.c | 84 ++++++++++++- src/video/win32/SDL_win32events.h | 6 +- src/video/win32/SDL_win32video.c | 67 +++++----- src/video/win32/SDL_win32video.h | 1 + src/video/win32/SDL_win32window.c | 28 ++--- src/video/win32/SDL_win32window.h | 31 +++-- 10 files changed, 158 insertions(+), 269 deletions(-) diff --git a/include/SDL_compat.h b/include/SDL_compat.h index c3288017d..1a2a0246a 100644 --- a/include/SDL_compat.h +++ b/include/SDL_compat.h @@ -117,6 +117,7 @@ typedef enum struct SDL_SysWMinfo; +#define SDL_SetModuleHandle(x) #define SDL_AllocSurface SDL_CreateRGBSurface extern DECLSPEC const SDL_version *SDLCALL SDL_Linked_Version(void); diff --git a/include/SDL_main.h b/include/SDL_main.h index b7a9c4794..13b677d3a 100644 --- a/include/SDL_main.h +++ b/include/SDL_main.h @@ -63,13 +63,11 @@ extern "C" { /* *INDENT-ON* */ #endif -/* This should be called from your WinMain() function, if any */ -extern DECLSPEC void SDLCALL SDL_SetModuleHandle(void *hInst); -/* This can also be called, but is no longer necessary */ +/* This can be called to set the application class at startup */ extern DECLSPEC int SDLCALL SDL_RegisterApp(char *name, Uint32 style, void *hInst); -/* This can also be called, but is no longer necessary (SDL_Quit calls it) */ extern DECLSPEC void SDLCALL SDL_UnregisterApp(void); + #ifdef __cplusplus /* *INDENT-OFF* */ } diff --git a/src/SDL.c b/src/SDL.c index 3acd4780c..05076c924 100644 --- a/src/SDL.c +++ b/src/SDL.c @@ -348,12 +348,6 @@ _DllMainCRTStartup(HANDLE hModule, } #endif /* building DLL with Watcom C */ -void -SDL_SetModuleHandle(void *hInst) -{ - /* FIXME: Do we still need this? */ -} - #endif /* OS/2 elif __WIN32__ */ /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/main/win32/SDL_win32_main.c b/src/main/win32/SDL_win32_main.c index 00ce12556..16858a633 100644 --- a/src/main/win32/SDL_win32_main.c +++ b/src/main/win32/SDL_win32_main.c @@ -10,20 +10,6 @@ #define WIN32_LEAN_AND_MEAN #include -#ifdef _WIN32_WCE -# define DIR_SEPERATOR TEXT("\\") -# undef _getcwd -# define _getcwd(str,len) wcscpy(str,TEXT("")) -# define setbuf(f,b) -# define setvbuf(w,x,y,z) -# define fopen _wfopen -# define freopen _wfreopen -# define remove(x) DeleteFile(x) -#else -# define DIR_SEPERATOR TEXT("/") -# include -#endif - /* Include the SDL main definition header */ #include "SDL.h" #include "SDL_main.h" @@ -34,20 +20,6 @@ # endif /* _WIN32_WCE_EMULATION */ #endif /* main */ -/* The standard output files */ -#define STDOUT_FILE TEXT("stdout.txt") -#define STDERR_FILE TEXT("stderr.txt") - -#ifndef NO_STDIO_REDIRECT -# ifdef _WIN32_WCE -static wchar_t stdoutPath[MAX_PATH]; -static wchar_t stderrPath[MAX_PATH]; -# else -static char stdoutPath[MAX_PATH]; -static char stderrPath[MAX_PATH]; -# endif -#endif - #if defined(_WIN32_WCE) && _WIN32_WCE < 300 /* seems to be undefined in Win CE although in online help */ #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t')) @@ -124,52 +96,6 @@ OutOfMemory(void) return FALSE; } -/* SDL_Quit() shouldn't be used with atexit() directly because - calling conventions may differ... */ -static void -cleanup(void) -{ - SDL_Quit(); -} - -/* Remove the output files if there was no output written */ -static void -cleanup_output(void) -{ -#ifndef NO_STDIO_REDIRECT - FILE *file; - int empty; -#endif - - /* Flush the output in case anything is queued */ - fclose(stdout); - fclose(stderr); - -#ifndef NO_STDIO_REDIRECT - /* See if the files have any output in them */ - if (stdoutPath[0]) { - file = fopen(stdoutPath, TEXT("rb")); - if (file) { - empty = (fgetc(file) == EOF) ? 1 : 0; - fclose(file); - if (empty) { - remove(stdoutPath); - } - } - } - if (stderrPath[0]) { - file = fopen(stderrPath, TEXT("rb")); - if (file) { - empty = (fgetc(file) == EOF) ? 1 : 0; - fclose(file); - if (empty) { - remove(stderrPath); - } - } - } -#endif -} - #if defined(_MSC_VER) && !defined(_WIN32_WCE) /* The VC++ compiler needs main defined */ #define console_main main @@ -179,45 +105,8 @@ cleanup_output(void) int console_main(int argc, char *argv[]) { - size_t n; - char *bufp, *appname; int status; - /* Get the class name from argv[0] */ - appname = argv[0]; - if ((bufp = SDL_strrchr(argv[0], '\\')) != NULL) { - appname = bufp + 1; - } else if ((bufp = SDL_strrchr(argv[0], '/')) != NULL) { - appname = bufp + 1; - } - - if ((bufp = SDL_strrchr(appname, '.')) == NULL) - n = SDL_strlen(appname); - else - n = (bufp - appname); - - bufp = SDL_stack_alloc(char, n + 1); - if (bufp == NULL) { - return OutOfMemory(); - } - SDL_strlcpy(bufp, appname, n + 1); - appname = bufp; - - /* Load SDL dynamic link library */ - if (SDL_Init(SDL_INIT_NOPARACHUTE) < 0) { - ShowError("WinMain() error", SDL_GetError()); - return (FALSE); - } - atexit(cleanup_output); - atexit(cleanup); - - /* Sam: - We still need to pass in the application handle so that - DirectInput will initialize properly when SDL_RegisterApp() - is called later in the video initialization. - */ - SDL_SetModuleHandle(GetModuleHandle(NULL)); - /* Run the application main() code */ status = SDL_main(argc, argv); @@ -229,24 +118,12 @@ console_main(int argc, char *argv[]) } /* This is where execution begins [windowed apps] */ -#ifdef _WIN32_WCE -int WINAPI -WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw) -#else int WINAPI -WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) -#endif +WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int sw) { - HINSTANCE handle; char **argv; int argc; char *cmdline; - DWORD pathlen; -#ifdef _WIN32_WCE - wchar_t path[MAX_PATH]; -#else - char path[MAX_PATH]; -#endif #ifdef _WIN32_WCE wchar_t *bufp; int nLen; @@ -254,76 +131,6 @@ WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) char *bufp; size_t nLen; #endif -#ifndef NO_STDIO_REDIRECT - FILE *newfp; -#endif - - /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't - keep them open. This is a hack.. hopefully it will be fixed - someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded. - */ - handle = LoadLibrary(TEXT("DDRAW.DLL")); - if (handle != NULL) { - FreeLibrary(handle); - } -#ifndef NO_STDIO_REDIRECT - pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path)); - while (pathlen > 0 && path[pathlen] != '\\') { - --pathlen; - } - path[pathlen] = '\0'; - -#ifdef _WIN32_WCE - wcsncpy(stdoutPath, path, SDL_arraysize(stdoutPath)); - wcsncat(stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath)); -#else - SDL_strlcpy(stdoutPath, path, SDL_arraysize(stdoutPath)); - SDL_strlcat(stdoutPath, DIR_SEPERATOR STDOUT_FILE, - SDL_arraysize(stdoutPath)); -#endif - - /* Redirect standard input and standard output */ - newfp = freopen(stdoutPath, TEXT("w"), stdout); - -#ifndef _WIN32_WCE - if (newfp == NULL) { /* This happens on NT */ -#if !defined(stdout) - stdout = fopen(stdoutPath, TEXT("w")); -#else - newfp = fopen(stdoutPath, TEXT("w")); - if (newfp) { - *stdout = *newfp; - } -#endif - } -#endif /* _WIN32_WCE */ - -#ifdef _WIN32_WCE - wcsncpy(stderrPath, path, SDL_arraysize(stdoutPath)); - wcsncat(stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath)); -#else - SDL_strlcpy(stderrPath, path, SDL_arraysize(stderrPath)); - SDL_strlcat(stderrPath, DIR_SEPERATOR STDERR_FILE, - SDL_arraysize(stderrPath)); -#endif - - newfp = freopen(stderrPath, TEXT("w"), stderr); -#ifndef _WIN32_WCE - if (newfp == NULL) { /* This happens on NT */ -#if !defined(stderr) - stderr = fopen(stderrPath, TEXT("w")); -#else - newfp = fopen(stderrPath, TEXT("w")); - if (newfp) { - *stderr = *newfp; - } -#endif - } -#endif /* _WIN32_WCE */ - - setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */ - setbuf(stderr, NULL); /* No buffering */ -#endif /* !NO_STDIO_REDIRECT */ #ifdef _WIN32_WCE nLen = wcslen(szCmdLine) + 128 + 1; @@ -357,7 +164,7 @@ WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) } ParseCommandLine(cmdline, argv); - /* Run the main program (after a little SDL initialization) */ + /* Run the main program */ console_main(argc, argv); /* Hush little compiler, don't you cry... */ diff --git a/src/video/win32/SDL_win32events.c b/src/video/win32/SDL_win32events.c index b22494dc7..309694e9c 100644 --- a/src/video/win32/SDL_win32events.c +++ b/src/video/win32/SDL_win32events.c @@ -23,9 +23,91 @@ #include "SDL_win32video.h" + +static LRESULT CALLBACK +WinMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) +{ + return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam); +} + void -WIN32_PumpEvents(_THIS) +WIN_PumpEvents(_THIS) { + MSG msg; + while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } +} + +static int app_registered = 0; +LPTSTR SDL_Appname = NULL; +Uint32 SDL_Appstyle = 0; +HINSTANCE SDL_Instance = NULL; + +/* Register the class for this application */ +int +SDL_RegisterApp(char *name, Uint32 style, void *hInst) +{ + WNDCLASS class; + + /* Only do this once... */ + if (app_registered) { + ++app_registered; + return (0); + } + if (!name && !SDL_Appname) { + name = "SDL_app"; + SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC); + SDL_Instance = hInst ? hInst : GetModuleHandle(NULL); + } + + if (name) { + SDL_Appname = SDL_iconv_utf8_ucs2(name); + SDL_Appstyle = style; + SDL_Instance = hInst ? hInst : GetModuleHandle(NULL); + } + + /* Register the application class */ + class.hCursor = NULL; + class.hIcon = LoadImage(SDL_Instance, SDL_Appname, + IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR); + class.lpszMenuName = NULL; + class.lpszClassName = SDL_Appname; + class.hbrBackground = NULL; + class.hInstance = SDL_Instance; + class.style = SDL_Appstyle; + class.lpfnWndProc = WinMessage; + class.cbWndExtra = 0; + class.cbClsExtra = 0; + if (!RegisterClass(&class)) { + SDL_SetError("Couldn't register application class"); + return (-1); + } + + app_registered = 1; + return (0); +} + +/* Unregisters the windowclass registered in SDL_RegisterApp above. */ +void +SDL_UnregisterApp() +{ + WNDCLASS class; + + /* SDL_RegisterApp might not have been called before */ + if (!app_registered) { + return; + } + --app_registered; + if (app_registered == 0) { + /* Check for any registered window classes. */ + if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) { + UnregisterClass(SDL_Appname, SDL_Instance); + } + SDL_free(SDL_Appname); + SDL_Appname = NULL; + } } /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/win32/SDL_win32events.h b/src/video/win32/SDL_win32events.h index fb9565178..e89f18d98 100644 --- a/src/video/win32/SDL_win32events.h +++ b/src/video/win32/SDL_win32events.h @@ -26,7 +26,11 @@ #include "../SDL_sysvideo.h" -extern void WIN32_PumpEvents(_THIS); +extern LPTSTR SDL_Appname; +extern Uint32 SDL_Appstyle; +extern HINSTANCE SDL_Instance; + +extern void WIN_PumpEvents(_THIS); #endif /* _SDL_win32events_h */ diff --git a/src/video/win32/SDL_win32video.c b/src/video/win32/SDL_win32video.c index 91393c150..a230fb5b0 100644 --- a/src/video/win32/SDL_win32video.c +++ b/src/video/win32/SDL_win32video.c @@ -31,30 +31,33 @@ #include "SDL_win32window.h" /* Initialization/Query functions */ -static int WIN32_VideoInit(_THIS); -static int WIN32_SetDisplayMode(_THIS, const SDL_DisplayMode * mode); -static void WIN32_VideoQuit(_THIS); +static int WIN_VideoInit(_THIS); +static int WIN_SetDisplayMode(_THIS, const SDL_DisplayMode * mode); +static void WIN_VideoQuit(_THIS); /* WIN32 driver bootstrap functions */ static int -WIN32_Available(void) +WIN_Available(void) { return (1); } static void -WIN32_DeleteDevice(SDL_VideoDevice * device) +WIN_DeleteDevice(SDL_VideoDevice * device) { + SDL_UnregisterApp(); SDL_free(device->hidden); SDL_free(device); } static SDL_VideoDevice * -WIN32_CreateDevice(int devindex) +WIN_CreateDevice(int devindex) { SDL_VideoDevice *device; + SDL_RegisterApp(NULL, 0, NULL); + /* Initialize all variables that we clean on shutdown */ device = (SDL_VideoDevice *) SDL_malloc(sizeof(SDL_VideoDevice)); if (device) { @@ -67,50 +70,50 @@ WIN32_CreateDevice(int devindex) if (device) { SDL_free(device); } - return (0); + return NULL; } SDL_memset(device->hidden, 0, (sizeof *device->hidden)); /* Set the function pointers */ - device->VideoInit = WIN32_VideoInit; - device->SetDisplayMode = WIN32_SetDisplayMode; - device->VideoQuit = WIN32_VideoQuit; - device->PumpEvents = WIN32_PumpEvents; + device->VideoInit = WIN_VideoInit; + device->SetDisplayMode = WIN_SetDisplayMode; + device->VideoQuit = WIN_VideoQuit; + device->PumpEvents = WIN_PumpEvents; #undef CreateWindow - device->CreateWindow = WIN32_CreateWindow; - device->CreateWindowFrom = WIN32_CreateWindowFrom; - device->SetWindowTitle = WIN32_SetWindowTitle; - device->SetWindowPosition = WIN32_SetWindowPosition; - device->SetWindowSize = WIN32_SetWindowSize; - device->ShowWindow = WIN32_ShowWindow; - device->HideWindow = WIN32_HideWindow; - device->RaiseWindow = WIN32_RaiseWindow; - device->MaximizeWindow = WIN32_MaximizeWindow; - device->MinimizeWindow = WIN32_MinimizeWindow; - device->RestoreWindow = WIN32_RestoreWindow; - device->SetWindowGrab = WIN32_SetWindowGrab; - device->DestroyWindow = WIN32_DestroyWindow; - device->GetWindowWMInfo = WIN32_GetWindowWMInfo; - - device->free = WIN32_DeleteDevice; + device->CreateWindow = WIN_CreateWindow; + device->CreateWindowFrom = WIN_CreateWindowFrom; + device->SetWindowTitle = WIN_SetWindowTitle; + device->SetWindowPosition = WIN_SetWindowPosition; + device->SetWindowSize = WIN_SetWindowSize; + device->ShowWindow = WIN_ShowWindow; + device->HideWindow = WIN_HideWindow; + device->RaiseWindow = WIN_RaiseWindow; + device->MaximizeWindow = WIN_MaximizeWindow; + device->MinimizeWindow = WIN_MinimizeWindow; + device->RestoreWindow = WIN_RestoreWindow; + device->SetWindowGrab = WIN_SetWindowGrab; + device->DestroyWindow = WIN_DestroyWindow; + device->GetWindowWMInfo = WIN_GetWindowWMInfo; + + device->free = WIN_DeleteDevice; return device; } VideoBootStrap WIN32_bootstrap = { "win32", "SDL Win32/64 video driver", - WIN32_Available, WIN32_CreateDevice + WIN_Available, WIN_CreateDevice }; int -WIN32_VideoInit(_THIS) +WIN_VideoInit(_THIS) { SDL_DisplayMode mode; SDL_AddBasicVideoDisplay(NULL); - //SDL_AddRenderDriver(0, &SDL_WIN32_RenderDriver); + //SDL_AddRenderDriver(0, &SDL_WIN_RenderDriver); SDL_zero(mode); SDL_AddDisplayMode(0, &mode); @@ -120,14 +123,14 @@ WIN32_VideoInit(_THIS) } static int -WIN32_SetDisplayMode(_THIS, const SDL_DisplayMode * mode) +WIN_SetDisplayMode(_THIS, const SDL_DisplayMode * mode) { SDL_CurrentDisplay.current_mode = *mode; return 0; } void -WIN32_VideoQuit(_THIS) +WIN_VideoQuit(_THIS) { } diff --git a/src/video/win32/SDL_win32video.h b/src/video/win32/SDL_win32video.h index ce5518436..1042a7af5 100644 --- a/src/video/win32/SDL_win32video.h +++ b/src/video/win32/SDL_win32video.h @@ -27,6 +27,7 @@ #include "../SDL_sysvideo.h" #define WIN32_LEAN_AND_MEAN +#define UNICODE #include #include "SDL_win32events.h" diff --git a/src/video/win32/SDL_win32window.c b/src/video/win32/SDL_win32window.c index ee3da90cc..6fa16f6e7 100644 --- a/src/video/win32/SDL_win32window.c +++ b/src/video/win32/SDL_win32window.c @@ -27,72 +27,72 @@ int -WIN32_CreateWindow(_THIS, SDL_Window * window) +WIN_CreateWindow(_THIS, SDL_Window * window) { } int -WIN32_CreateWindowFrom(_THIS, SDL_Window * window, const void *data) +WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data) { } void -WIN32_SetWindowTitle(_THIS, SDL_Window * window) +WIN_SetWindowTitle(_THIS, SDL_Window * window) { } void -WIN32_SetWindowPosition(_THIS, SDL_Window * window) +WIN_SetWindowPosition(_THIS, SDL_Window * window) { } void -WIN32_SetWindowSize(_THIS, SDL_Window * window) +WIN_SetWindowSize(_THIS, SDL_Window * window) { } void -WIN32_ShowWindow(_THIS, SDL_Window * window) +WIN_ShowWindow(_THIS, SDL_Window * window) { } void -WIN32_HideWindow(_THIS, SDL_Window * window) +WIN_HideWindow(_THIS, SDL_Window * window) { } void -WIN32_RaiseWindow(_THIS, SDL_Window * window) +WIN_RaiseWindow(_THIS, SDL_Window * window) { } void -WIN32_MaximizeWindow(_THIS, SDL_Window * window) +WIN_MaximizeWindow(_THIS, SDL_Window * window) { } void -WIN32_MinimizeWindow(_THIS, SDL_Window * window) +WIN_MinimizeWindow(_THIS, SDL_Window * window) { } void -WIN32_RestoreWindow(_THIS, SDL_Window * window) +WIN_RestoreWindow(_THIS, SDL_Window * window) { } void -WIN32_SetWindowGrab(_THIS, SDL_Window * window) +WIN_SetWindowGrab(_THIS, SDL_Window * window) { } void -WIN32_DestroyWindow(_THIS, SDL_Window * window) +WIN_DestroyWindow(_THIS, SDL_Window * window) { } SDL_bool -WIN32_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info) +WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info) { } diff --git a/src/video/win32/SDL_win32window.h b/src/video/win32/SDL_win32window.h index d0a021068..35b326f61 100644 --- a/src/video/win32/SDL_win32window.h +++ b/src/video/win32/SDL_win32window.h @@ -27,22 +27,21 @@ #include "../SDL_sysvideo.h" #include "SDL_win32video.h" -extern int WIN32_CreateWindow(_THIS, SDL_Window * window); -extern int WIN32_CreateWindowFrom(_THIS, SDL_Window * window, - const void *data); -extern void WIN32_SetWindowTitle(_THIS, SDL_Window * window); -extern void WIN32_SetWindowPosition(_THIS, SDL_Window * window); -extern void WIN32_SetWindowSize(_THIS, SDL_Window * window); -extern void WIN32_ShowWindow(_THIS, SDL_Window * window); -extern void WIN32_HideWindow(_THIS, SDL_Window * window); -extern void WIN32_RaiseWindow(_THIS, SDL_Window * window); -extern void WIN32_MaximizeWindow(_THIS, SDL_Window * window); -extern void WIN32_MinimizeWindow(_THIS, SDL_Window * window); -extern void WIN32_RestoreWindow(_THIS, SDL_Window * window); -extern void WIN32_SetWindowGrab(_THIS, SDL_Window * window); -extern void WIN32_DestroyWindow(_THIS, SDL_Window * window); -extern SDL_bool WIN32_GetWindowWMInfo(_THIS, SDL_Window * window, - SDL_SysWMinfo * info); +extern int WIN_CreateWindow(_THIS, SDL_Window * window); +extern int WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data); +extern void WIN_SetWindowTitle(_THIS, SDL_Window * window); +extern void WIN_SetWindowPosition(_THIS, SDL_Window * window); +extern void WIN_SetWindowSize(_THIS, SDL_Window * window); +extern void WIN_ShowWindow(_THIS, SDL_Window * window); +extern void WIN_HideWindow(_THIS, SDL_Window * window); +extern void WIN_RaiseWindow(_THIS, SDL_Window * window); +extern void WIN_MaximizeWindow(_THIS, SDL_Window * window); +extern void WIN_MinimizeWindow(_THIS, SDL_Window * window); +extern void WIN_RestoreWindow(_THIS, SDL_Window * window); +extern void WIN_SetWindowGrab(_THIS, SDL_Window * window); +extern void WIN_DestroyWindow(_THIS, SDL_Window * window); +extern SDL_bool WIN_GetWindowWMInfo(_THIS, SDL_Window * window, + SDL_SysWMinfo * info); #endif /* _SDL_win32window_h */