Mercurial > sdl-ios-xcode
changeset 5092:327f181542f1
Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.
I think this also fixes the bug relating to non-latin characters in filenames, since UNICODE wasn't defined in SDL_rwops.c
line wrap: on
line diff
--- a/Android.mk Mon Jan 24 17:47:18 2011 -0800 +++ b/Android.mk Mon Jan 24 21:20:30 2011 -0800 @@ -12,30 +12,31 @@ LOCAL_C_INCLUDES := $(LOCAL_PATH)/include -LOCAL_SRC_FILES := src/SDL_android.cpp \ +LOCAL_SRC_FILES := \ $(subst $(LOCAL_PATH)/,, \ $(wildcard $(LOCAL_PATH)/src/*.c) \ $(wildcard $(LOCAL_PATH)/src/audio/*.c) \ + $(wildcard $(LOCAL_PATH)/src/audio/android/*.c) \ + $(wildcard $(LOCAL_PATH)/src/audio/dummy/*.c) \ $(LOCAL_PATH)/src/atomic/SDL_atomic.c \ $(LOCAL_PATH)/src/atomic/SDL_spinlock.c.arm \ + $(wildcard $(LOCAL_PATH)/src/core/android/*.cpp) \ $(wildcard $(LOCAL_PATH)/src/cpuinfo/*.c) \ $(wildcard $(LOCAL_PATH)/src/events/*.c) \ $(wildcard $(LOCAL_PATH)/src/file/*.c) \ + $(wildcard $(LOCAL_PATH)/src/haptic/*.c) \ + $(wildcard $(LOCAL_PATH)/src/haptic/dummy/*.c) \ $(wildcard $(LOCAL_PATH)/src/joystick/*.c) \ - $(wildcard $(LOCAL_PATH)/src/haptic/*.c) \ + $(wildcard $(LOCAL_PATH)/src/joystick/android/*.c) \ + $(wildcard $(LOCAL_PATH)/src/loadso/dlopen/*.c)) \ + $(wildcard $(LOCAL_PATH)/src/power/*.c) \ $(wildcard $(LOCAL_PATH)/src/stdlib/*.c) \ $(wildcard $(LOCAL_PATH)/src/thread/*.c) \ + $(wildcard $(LOCAL_PATH)/src/thread/pthread/*.c) \ $(wildcard $(LOCAL_PATH)/src/timer/*.c) \ + $(wildcard $(LOCAL_PATH)/src/timer/unix/*.c) \ $(wildcard $(LOCAL_PATH)/src/video/*.c) \ - $(wildcard $(LOCAL_PATH)/src/power/*.c) \ - $(wildcard $(LOCAL_PATH)/src/audio/android/*.c) \ - $(wildcard $(LOCAL_PATH)/src/audio/dummy/*.c) \ - $(wildcard $(LOCAL_PATH)/src/video/android/*.c) \ - $(wildcard $(LOCAL_PATH)/src/joystick/android/*.c) \ - $(wildcard $(LOCAL_PATH)/src/haptic/dummy/*.c) \ - $(wildcard $(LOCAL_PATH)/src/thread/pthread/*.c) \ - $(wildcard $(LOCAL_PATH)/src/timer/unix/*.c) \ - $(wildcard $(LOCAL_PATH)/src/loadso/dlopen/*.c)) + $(wildcard $(LOCAL_PATH)/src/video/android/*.c) LOCAL_LDLIBS := -ldl -lGLESv1_CM -llog
--- a/VisualC/SDL/SDL_VS2008.vcproj Mon Jan 24 17:47:18 2011 -0800 +++ b/VisualC/SDL/SDL_VS2008.vcproj Mon Jan 24 21:20:30 2011 -0800 @@ -1151,6 +1151,22 @@ > </File> <File + RelativePath="..\..\src\events\SDL_windowevents.c" + > + </File> + <File + RelativePath="..\..\src\events\SDL_windowevents_c.h" + > + </File> + <File + RelativePath="..\..\src\core\windows\SDL_windows.c" + > + </File> + <File + RelativePath="..\..\src\core\windows\SDL_windows.h" + > + </File> + <File RelativePath="..\..\src\video\windows\SDL_windowsclipboard.c" > </File> @@ -1231,14 +1247,6 @@ > </File> <File - RelativePath="..\..\src\events\SDL_windowevents.c" - > - </File> - <File - RelativePath="..\..\src\events\SDL_windowevents_c.h" - > - </File> - <File RelativePath="..\..\src\video\SDL_yuv_mmx.c" > </File>
--- a/VisualC/SDL/SDL_VS2010.vcxproj Mon Jan 24 17:47:18 2011 -0800 +++ b/VisualC/SDL/SDL_VS2010.vcxproj Mon Jan 24 21:20:30 2011 -0800 @@ -272,6 +272,7 @@ <ClInclude Include="..\..\include\SDL_types.h" /> <ClInclude Include="..\..\include\SDL_version.h" /> <ClInclude Include="..\..\include\SDL_video.h" /> + <ClInclude Include="..\..\src\core\windows\SDL_windows.h" /> <ClInclude Include="..\..\src\events\blank_cursor.h" /> <ClInclude Include="..\..\src\events\default_cursor.h" /> <ClInclude Include="..\..\src\audio\windx5\directx.h" /> @@ -345,6 +346,7 @@ <ClInclude Include="..\..\src\video\windows\wmmsg.h" /> </ItemGroup> <ItemGroup> + <ClCompile Include="..\..\src\core\windows\SDL_windows.c" /> <ClCompile Include="..\..\src\events\SDL_clipboardevents.c" /> <ClCompile Include="..\..\src\events\SDL_gesture.c" /> <ClCompile Include="..\..\src\events\SDL_touch.c" /> @@ -460,4 +462,4 @@ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> </ImportGroup> -</Project> +</Project> \ No newline at end of file
--- a/VisualCE/SDL/SDL.vcproj Mon Jan 24 17:47:18 2011 -0800 +++ b/VisualCE/SDL/SDL.vcproj Mon Jan 24 21:20:30 2011 -0800 @@ -1054,6 +1054,10 @@ > </File> <File + RelativePath="..\..\src\core\windows\SDL_windows.c" + > + </File> + <File RelativePath="..\..\src\video\windows\SDL_windowsclipboard.c" > </File> @@ -1359,6 +1363,10 @@ > </File> <File + RelativePath="..\..\src\core\windows\SDL_windows.h" + > + </File> + <File RelativePath="..\..\src\video\windows\SDL_windowsclipboard.h" > </File>
--- a/src/SDL.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/SDL.c Mon Jan 24 21:20:30 2011 -0800 @@ -329,8 +329,7 @@ #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL)) /* Need to include DllMain() on Watcom C for some reason.. */ -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "core/windows/SDL_windows.h" BOOL APIENTRY _DllMainCRTStartup(HANDLE hModule,
--- a/src/SDL_android.cpp Mon Jan 24 17:47:18 2011 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,258 +0,0 @@ -/* - SDL - Simple DirectMedia Layer - Copyright (C) 1997-2010 Sam Lantinga - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - - Sam Lantinga - slouken@libsdl.org -*/ -#include "SDL_config.h" - -#include "SDL_android.h" - -extern "C" { -#include "events/SDL_events_c.h" -#include "video/android/SDL_androidkeyboard.h" -#include "video/android/SDL_androidtouch.h" -#include "video/android/SDL_androidvideo.h" - -/* Impelemented in audio/android/SDL_androidaudio.c */ -extern void Android_RunAudioThread(); -} // C - -/******************************************************************************* - This file links the Java side of Android with libsdl -*******************************************************************************/ -#include <jni.h> -#include <android/log.h> - - -/******************************************************************************* - Globals -*******************************************************************************/ -static JNIEnv* mEnv = NULL; -static JNIEnv* mAudioEnv = NULL; - -// Main activity -static jclass mActivityClass; - -// method signatures -static jmethodID midCreateGLContext; -static jmethodID midFlipBuffers; -static jmethodID midAudioInit; -static jmethodID midAudioWriteShortBuffer; -static jmethodID midAudioWriteByteBuffer; -static jmethodID midAudioQuit; - -// Accelerometer data storage -static float fLastAccelerometer[3]; - - -/******************************************************************************* - Functions called by JNI -*******************************************************************************/ - -// Library init -extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) -{ - return JNI_VERSION_1_4; -} - -// Called before SDL_main() to initialize JNI bindings -extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls) -{ - __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()"); - - mEnv = env; - mActivityClass = cls; - - midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass, - "createGLContext","()V"); - midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass, - "flipBuffers","()V"); - midAudioInit = mEnv->GetStaticMethodID(mActivityClass, - "audioInit", "(IZZI)Ljava/lang/Object;"); - midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass, - "audioWriteShortBuffer", "([S)V"); - midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass, - "audioWriteByteBuffer", "([B)V"); - midAudioQuit = mEnv->GetStaticMethodID(mActivityClass, - "audioQuit", "()V"); - - if(!midCreateGLContext || !midFlipBuffers || !midAudioInit || - !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) { - __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly"); - } -} - -// Resize -extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize( - JNIEnv* env, jclass jcls, - jint width, jint height, jint format) -{ - Android_SetScreenResolution(width, height, format); -} - -// Keydown -extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown( - JNIEnv* env, jclass jcls, jint keycode) -{ - Android_OnKeyDown(keycode); -} - -// Keyup -extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp( - JNIEnv* env, jclass jcls, jint keycode) -{ - Android_OnKeyUp(keycode); -} - -// Touch -extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch( - JNIEnv* env, jclass jcls, - jint action, jfloat x, jfloat y, jfloat p) -{ - Android_OnTouch(action, x, y, p); -} - -// Accelerometer -extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel( - JNIEnv* env, jclass jcls, - jfloat x, jfloat y, jfloat z) -{ - fLastAccelerometer[0] = x; - fLastAccelerometer[1] = y; - fLastAccelerometer[2] = z; -} - -// Quit -extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( - JNIEnv* env, jclass cls) -{ - // Inject a SDL_QUIT event - SDL_SendQuit(); -} - -extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread( - JNIEnv* env, jclass cls) -{ - /* This is the audio thread, with a different environment */ - mAudioEnv = env; - - Android_RunAudioThread(); -} - - -/******************************************************************************* - Functions called by SDL into Java -*******************************************************************************/ -extern "C" void Android_JNI_CreateContext() -{ - mEnv->CallStaticVoidMethod(mActivityClass, midCreateGLContext); -} - -extern "C" void Android_JNI_SwapWindow() -{ - mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers); -} - -extern "C" void Android_JNI_SetActivityTitle(const char *title) -{ - jmethodID mid; - - mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V"); - if (mid) { - mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title)); - } -} - -extern "C" void Android_JNI_GetAccelerometerValues(float values[3]) -{ - int i; - for (i = 0; i < 3; ++i) { - values[i] = fLastAccelerometer[i]; - } -} - -// -// Audio support -// -static jboolean audioBuffer16Bit = JNI_FALSE; -static jboolean audioBufferStereo = JNI_FALSE; -static jobject audioBuffer = NULL; -static void* audioBufferPinned = NULL; - -extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames) -{ - int audioBufferFrames; - - __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device"); - audioBuffer16Bit = is16Bit; - audioBufferStereo = channelCount > 1; - - audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames); - - if (audioBuffer == NULL) { - __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!"); - return 0; - } - audioBuffer = mEnv->NewGlobalRef(audioBuffer); - - jboolean isCopy = JNI_FALSE; - if (audioBuffer16Bit) { - audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy); - audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer); - } else { - audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy); - audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer); - } - if (audioBufferStereo) { - audioBufferFrames /= 2; - } - - return audioBufferFrames; -} - -extern "C" void * Android_JNI_GetAudioBuffer() -{ - return audioBufferPinned; -} - -extern "C" void Android_JNI_WriteAudioBuffer() -{ - if (audioBuffer16Bit) { - mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT); - mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer); - } else { - mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT); - mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer); - } - - /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */ -} - -extern "C" void Android_JNI_CloseAudioDevice() -{ - mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit); - - if (audioBuffer) { - mEnv->DeleteGlobalRef(audioBuffer); - audioBuffer = NULL; - audioBufferPinned = NULL; - } -} - -/* vi: set ts=4 sw=4 expandtab: */
--- a/src/SDL_android.h Mon Jan 24 17:47:18 2011 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,50 +0,0 @@ -/* - SDL - Simple DirectMedia Layer - Copyright (C) 1997-2010 Sam Lantinga - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - - Sam Lantinga - slouken@libsdl.org -*/ -#include "SDL_config.h" - -/* Set up for C function definitions, even when using C++ */ -#ifdef __cplusplus -/* *INDENT-OFF* */ -extern "C" { -/* *INDENT-ON* */ -#endif - -/* Interface from the SDL library into the Android Java activity */ -extern void Android_JNI_CreateContext(); -extern void Android_JNI_SwapWindow(); -extern void Android_JNI_SetActivityTitle(const char *title); -extern void Android_JNI_GetAccelerometerValues(float values[3]); - -// Audio support -extern int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames); -extern void* Android_JNI_GetAudioBuffer(); -extern void Android_JNI_WriteAudioBuffer(); -extern void Android_JNI_CloseAudioDevice(); - -/* Ends C function definitions when using C++ */ -#ifdef __cplusplus -/* *INDENT-OFF* */ -} -/* *INDENT-ON* */ -#endif - -/* vi: set ts=4 sw=4 expandtab: */
--- a/src/SDL_assert.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/SDL_assert.c Mon Jan 24 21:20:30 2011 -0800 @@ -27,18 +27,11 @@ #include "video/SDL_sysvideo.h" #ifdef __WIN32__ -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "core/windows/SDL_windows.h" #ifndef WS_OVERLAPPEDWINDOW #define WS_OVERLAPPEDWINDOW 0 #endif - -#ifdef UNICODE -#define WIN_UTF8ToString(S) (WCHAR *)SDL_iconv_string("UCS-2", "UTF-8", (char *)S, SDL_strlen(S)+1) -#else -#define WIN_UTF8ToString(S) SDL_iconv_string("ASCII", "UTF-8", (char *)S, SDL_strlen(S)+1) -#endif #else /* fprintf, _exit(), etc. */ #include <stdio.h> #include <stdlib.h>
--- a/src/atomic/SDL_spinlock.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/atomic/SDL_spinlock.c Mon Jan 24 21:20:30 2011 -0800 @@ -25,14 +25,7 @@ #include "SDL_timer.h" /* Don't do the check for Visual Studio 2005, it's safe here */ -#if defined(_MSC_VER) -#ifdef _WIN32_WCE -#define WIN32_LEAN_AND_MEAN -#include <windows.h> -#else -#include <intrin.h> -#endif -#endif /* _MSC_VER */ +#include "../core/windows/SDL_windows.h" /* This function is where all the magic happens... */ SDL_bool @@ -40,7 +33,7 @@ { #if defined(_MSC_VER) SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long)); - return (_InterlockedExchange((long*)lock, 1) == 0); + return (InterlockedExchange((long*)lock, 1) == 0); #elif defined(__MACOSX__) return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
--- a/src/audio/android/SDL_androidaudio.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/audio/android/SDL_androidaudio.c Mon Jan 24 21:20:30 2011 -0800 @@ -27,7 +27,7 @@ #include "../SDL_audio_c.h" #include "SDL_androidaudio.h" -#include "../../SDL_android.h" +#include "../../core/android/SDL_android.h" #include <android/log.h>
--- a/src/audio/windib/SDL_dibaudio.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/audio/windib/SDL_dibaudio.c Mon Jan 24 21:20:30 2011 -0800 @@ -23,8 +23,7 @@ /* Allow access to a raw mixing buffer */ -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../../core/windows/SDL_windows.h" #include <mmsystem.h> #include "SDL_timer.h" @@ -67,21 +66,14 @@ { size_t len; char errbuf[MAXERRORLENGTH]; -#ifdef _WIN32_WCE wchar_t werrbuf[MAXERRORLENGTH]; -#endif SDL_snprintf(errbuf, SDL_arraysize(errbuf), "%s: ", function); len = SDL_strlen(errbuf); -#ifdef _WIN32_WCE - /* UNICODE version */ waveOutGetErrorText(code, werrbuf, MAXERRORLENGTH - len); WideCharToMultiByte(CP_ACP, 0, werrbuf, -1, errbuf + len, MAXERRORLENGTH - len, NULL, NULL); -#else - waveOutGetErrorText(code, errbuf + len, (UINT) (MAXERRORLENGTH - len)); -#endif SDL_SetError("%s", errbuf); }
--- a/src/audio/windx5/SDL_dx5audio.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/audio/windx5/SDL_dx5audio.c Mon Jan 24 21:20:30 2011 -0800 @@ -24,6 +24,7 @@ /* Allow access to a raw mixing buffer */ #include "SDL_timer.h" +#include "SDL_loadso.h" #include "SDL_audio.h" #include "../SDL_audio_c.h" #include "SDL_dx5audio.h" @@ -38,19 +39,19 @@ #endif /* DirectX function pointers for audio */ -static HINSTANCE DSoundDLL = NULL; +static void* DSoundDLL = NULL; static HRESULT(WINAPI * DSoundCreate) (LPGUID, LPDIRECTSOUND *, LPUNKNOWN) = NULL; static void DSOUND_Unload(void) { + DSoundCreate = NULL; + if (DSoundDLL != NULL) { - FreeLibrary(DSoundDLL); + SDL_UnloadObject(DSoundDLL); + DSoundDLL = NULL; } - - DSoundCreate = NULL; - DSoundDLL = NULL; } @@ -61,17 +62,16 @@ DSOUND_Unload(); - DSoundDLL = LoadLibrary(TEXT("DSOUND.DLL")); + DSoundDLL = SDL_LoadObject("DSOUND.DLL"); if (DSoundDLL == NULL) { SDL_SetError("DirectSound: failed to load DSOUND.DLL"); } else { /* Now make sure we have DirectX 5 or better... */ /* (DirectSoundCaptureCreate was added in DX5) */ - if (!GetProcAddress(DSoundDLL, TEXT("DirectSoundCaptureCreate"))) { + if (!SDL_LoadFunction(DSoundDLL, "DirectSoundCaptureCreate")) { SDL_SetError("DirectSound: System doesn't appear to have DX5."); } else { - DSoundCreate = (void *) GetProcAddress(DSoundDLL, - TEXT("DirectSoundCreate")); + DSoundCreate = SDL_LoadFunction(DSoundDLL, "DirectSoundCreate"); } if (!DSoundCreate) {
--- a/src/audio/windx5/directx.h Mon Jan 24 17:47:18 2011 -0800 +++ b/src/audio/windx5/directx.h Mon Jan 24 21:20:30 2011 -0800 @@ -4,8 +4,7 @@ /* Include all of the DirectX 5.0 headers and adds any necessary tweaks */ -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../../core/windows/SDL_windows.h" #include <mmsystem.h> #ifndef WIN32 #define WIN32
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/core/android/SDL_android.cpp Mon Jan 24 21:20:30 2011 -0800 @@ -0,0 +1,258 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2010 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ +#include "SDL_config.h" + +#include "SDL_android.h" + +extern "C" { +#include "events/SDL_events_c.h" +#include "video/android/SDL_androidkeyboard.h" +#include "video/android/SDL_androidtouch.h" +#include "video/android/SDL_androidvideo.h" + +/* Impelemented in audio/android/SDL_androidaudio.c */ +extern void Android_RunAudioThread(); +} // C + +/******************************************************************************* + This file links the Java side of Android with libsdl +*******************************************************************************/ +#include <jni.h> +#include <android/log.h> + + +/******************************************************************************* + Globals +*******************************************************************************/ +static JNIEnv* mEnv = NULL; +static JNIEnv* mAudioEnv = NULL; + +// Main activity +static jclass mActivityClass; + +// method signatures +static jmethodID midCreateGLContext; +static jmethodID midFlipBuffers; +static jmethodID midAudioInit; +static jmethodID midAudioWriteShortBuffer; +static jmethodID midAudioWriteByteBuffer; +static jmethodID midAudioQuit; + +// Accelerometer data storage +static float fLastAccelerometer[3]; + + +/******************************************************************************* + Functions called by JNI +*******************************************************************************/ + +// Library init +extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved) +{ + return JNI_VERSION_1_4; +} + +// Called before SDL_main() to initialize JNI bindings +extern "C" void SDL_Android_Init(JNIEnv* env, jclass cls) +{ + __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL_Android_Init()"); + + mEnv = env; + mActivityClass = cls; + + midCreateGLContext = mEnv->GetStaticMethodID(mActivityClass, + "createGLContext","()V"); + midFlipBuffers = mEnv->GetStaticMethodID(mActivityClass, + "flipBuffers","()V"); + midAudioInit = mEnv->GetStaticMethodID(mActivityClass, + "audioInit", "(IZZI)Ljava/lang/Object;"); + midAudioWriteShortBuffer = mEnv->GetStaticMethodID(mActivityClass, + "audioWriteShortBuffer", "([S)V"); + midAudioWriteByteBuffer = mEnv->GetStaticMethodID(mActivityClass, + "audioWriteByteBuffer", "([B)V"); + midAudioQuit = mEnv->GetStaticMethodID(mActivityClass, + "audioQuit", "()V"); + + if(!midCreateGLContext || !midFlipBuffers || !midAudioInit || + !midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) { + __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL: Couldn't locate Java callbacks, check that they're named and typed correctly"); + } +} + +// Resize +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeResize( + JNIEnv* env, jclass jcls, + jint width, jint height, jint format) +{ + Android_SetScreenResolution(width, height, format); +} + +// Keydown +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown( + JNIEnv* env, jclass jcls, jint keycode) +{ + Android_OnKeyDown(keycode); +} + +// Keyup +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyUp( + JNIEnv* env, jclass jcls, jint keycode) +{ + Android_OnKeyUp(keycode); +} + +// Touch +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeTouch( + JNIEnv* env, jclass jcls, + jint action, jfloat x, jfloat y, jfloat p) +{ + Android_OnTouch(action, x, y, p); +} + +// Accelerometer +extern "C" void Java_org_libsdl_app_SDLActivity_onNativeAccel( + JNIEnv* env, jclass jcls, + jfloat x, jfloat y, jfloat z) +{ + fLastAccelerometer[0] = x; + fLastAccelerometer[1] = y; + fLastAccelerometer[2] = z; +} + +// Quit +extern "C" void Java_org_libsdl_app_SDLActivity_nativeQuit( + JNIEnv* env, jclass cls) +{ + // Inject a SDL_QUIT event + SDL_SendQuit(); +} + +extern "C" void Java_org_libsdl_app_SDLActivity_nativeRunAudioThread( + JNIEnv* env, jclass cls) +{ + /* This is the audio thread, with a different environment */ + mAudioEnv = env; + + Android_RunAudioThread(); +} + + +/******************************************************************************* + Functions called by SDL into Java +*******************************************************************************/ +extern "C" void Android_JNI_CreateContext() +{ + mEnv->CallStaticVoidMethod(mActivityClass, midCreateGLContext); +} + +extern "C" void Android_JNI_SwapWindow() +{ + mEnv->CallStaticVoidMethod(mActivityClass, midFlipBuffers); +} + +extern "C" void Android_JNI_SetActivityTitle(const char *title) +{ + jmethodID mid; + + mid = mEnv->GetStaticMethodID(mActivityClass,"setActivityTitle","(Ljava/lang/String;)V"); + if (mid) { + mEnv->CallStaticVoidMethod(mActivityClass, mid, mEnv->NewStringUTF(title)); + } +} + +extern "C" void Android_JNI_GetAccelerometerValues(float values[3]) +{ + int i; + for (i = 0; i < 3; ++i) { + values[i] = fLastAccelerometer[i]; + } +} + +// +// Audio support +// +static jboolean audioBuffer16Bit = JNI_FALSE; +static jboolean audioBufferStereo = JNI_FALSE; +static jobject audioBuffer = NULL; +static void* audioBufferPinned = NULL; + +extern "C" int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames) +{ + int audioBufferFrames; + + __android_log_print(ANDROID_LOG_VERBOSE, "SDL", "SDL audio: opening device"); + audioBuffer16Bit = is16Bit; + audioBufferStereo = channelCount > 1; + + audioBuffer = mEnv->CallStaticObjectMethod(mActivityClass, midAudioInit, sampleRate, audioBuffer16Bit, audioBufferStereo, desiredBufferFrames); + + if (audioBuffer == NULL) { + __android_log_print(ANDROID_LOG_WARN, "SDL", "SDL audio: didn't get back a good audio buffer!"); + return 0; + } + audioBuffer = mEnv->NewGlobalRef(audioBuffer); + + jboolean isCopy = JNI_FALSE; + if (audioBuffer16Bit) { + audioBufferPinned = mEnv->GetShortArrayElements((jshortArray)audioBuffer, &isCopy); + audioBufferFrames = mEnv->GetArrayLength((jshortArray)audioBuffer); + } else { + audioBufferPinned = mEnv->GetByteArrayElements((jbyteArray)audioBuffer, &isCopy); + audioBufferFrames = mEnv->GetArrayLength((jbyteArray)audioBuffer); + } + if (audioBufferStereo) { + audioBufferFrames /= 2; + } + + return audioBufferFrames; +} + +extern "C" void * Android_JNI_GetAudioBuffer() +{ + return audioBufferPinned; +} + +extern "C" void Android_JNI_WriteAudioBuffer() +{ + if (audioBuffer16Bit) { + mAudioEnv->ReleaseShortArrayElements((jshortArray)audioBuffer, (jshort *)audioBufferPinned, JNI_COMMIT); + mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteShortBuffer, (jshortArray)audioBuffer); + } else { + mAudioEnv->ReleaseByteArrayElements((jbyteArray)audioBuffer, (jbyte *)audioBufferPinned, JNI_COMMIT); + mAudioEnv->CallStaticVoidMethod(mActivityClass, midAudioWriteByteBuffer, (jbyteArray)audioBuffer); + } + + /* JNI_COMMIT means the changes are committed to the VM but the buffer remains pinned */ +} + +extern "C" void Android_JNI_CloseAudioDevice() +{ + mEnv->CallStaticVoidMethod(mActivityClass, midAudioQuit); + + if (audioBuffer) { + mEnv->DeleteGlobalRef(audioBuffer); + audioBuffer = NULL; + audioBufferPinned = NULL; + } +} + +/* vi: set ts=4 sw=4 expandtab: */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/core/android/SDL_android.h Mon Jan 24 21:20:30 2011 -0800 @@ -0,0 +1,50 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2010 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ +#include "SDL_config.h" + +/* Set up for C function definitions, even when using C++ */ +#ifdef __cplusplus +/* *INDENT-OFF* */ +extern "C" { +/* *INDENT-ON* */ +#endif + +/* Interface from the SDL library into the Android Java activity */ +extern void Android_JNI_CreateContext(); +extern void Android_JNI_SwapWindow(); +extern void Android_JNI_SetActivityTitle(const char *title); +extern void Android_JNI_GetAccelerometerValues(float values[3]); + +// Audio support +extern int Android_JNI_OpenAudioDevice(int sampleRate, int is16Bit, int channelCount, int desiredBufferFrames); +extern void* Android_JNI_GetAudioBuffer(); +extern void Android_JNI_WriteAudioBuffer(); +extern void Android_JNI_CloseAudioDevice(); + +/* Ends C function definitions when using C++ */ +#ifdef __cplusplus +/* *INDENT-OFF* */ +} +/* *INDENT-ON* */ +#endif + +/* vi: set ts=4 sw=4 expandtab: */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/core/windows/SDL_windows.c Mon Jan 24 21:20:30 2011 -0800 @@ -0,0 +1,41 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2010 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ +#include "SDL_config.h" + +#include "SDL_error.h" +#include "SDL_windows.h" + + +/* Sets an error message based on GetLastError() */ +void +WIN_SetError(const char *prefix) +{ + TCHAR buffer[1024]; + char *message; + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, + buffer, SDL_arraysize(buffer), NULL); + message = WIN_StringToUTF8(buffer); + SDL_SetError("%s%s%s", prefix ? prefix : "", prefix ? ": " : "", message); + SDL_free(message); +} + +/* vi: set ts=4 sw=4 expandtab: */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/core/windows/SDL_windows.h Mon Jan 24 21:20:30 2011 -0800 @@ -0,0 +1,53 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2010 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ + +/* This is an include file for windows.h with the SDL build settings */ + +#ifndef _INCLUDED_WINDOWS_H +#define _INCLUDED_WINDOWS_H + +#define WIN32_LEAN_AND_MEAN +#define STRICT +#ifndef UNICODE +#define UNICODE 1 +#endif +#undef WINVER +#define WINVER 0x500 /* Need 0x410 for AlphaBlend() and 0x500 for EnumDisplayDevices() */ + +#include <windows.h> + + +/* Routines to convert from UTF8 to native Windows text */ +#if UNICODE +#define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "UCS-2", (char *)(S), (SDL_wcslen(S)+1)*sizeof(WCHAR)) +#define WIN_UTF8ToString(S) (WCHAR *)SDL_iconv_string("UCS-2", "UTF-8", (char *)(S), SDL_strlen(S)+1) +#else +#define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "ASCII", (char *)(S), (SDL_strlen(S)+1)) +#define WIN_UTF8ToString(S) SDL_iconv_string("ASCII", "UTF-8", (char *)(S), SDL_strlen(S)+1) +#endif + +/* Sets an error message based on GetLastError() */ +extern void WIN_SetError(const char *prefix); + +#endif /* _INCLUDED_WINDOWS_H */ + +/* vi: set ts=4 sw=4 expandtab: */
--- a/src/cpuinfo/SDL_cpuinfo.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/cpuinfo/SDL_cpuinfo.c Mon Jan 24 21:20:30 2011 -0800 @@ -39,8 +39,7 @@ #include <setjmp.h> #endif #ifdef __WIN32__ -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../core/windows/SDL_windows.h" #endif #define CPU_HAS_RDTSC 0x00000001
--- a/src/file/SDL_rwops.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/file/SDL_rwops.c Mon Jan 24 21:20:30 2011 -0800 @@ -44,8 +44,7 @@ faster, and all stdio functions anyway are embedded in coredll.dll - the main wince dll*/ -#define WINDOWS_LEAN_AND_MEAN -#include <windows.h> +#include "../core/windows/SDL_windows.h" #ifndef INVALID_SET_FILE_POINTER #define INVALID_SET_FILE_POINTER 0xFFFFFFFF @@ -98,34 +97,28 @@ } #ifdef _WIN32_WCE { - size_t size = SDL_strlen(filename) + 1; - wchar_t *filenameW = SDL_stack_alloc(wchar_t, size); - - if (MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, size) == - 0) { - SDL_stack_free(filenameW); - SDL_free(context->hidden.windowsio.buffer.data); - context->hidden.windowsio.buffer.data = NULL; - SDL_SetError("Unable to convert filename to Unicode"); - return -1; - } - h = CreateFile(filenameW, (w_right | r_right), + LPTSTR tstr = WIN_UTF8ToString(filename); + h = CreateFile(tstr, (w_right | r_right), (w_right) ? 0 : FILE_SHARE_READ, NULL, (must_exist | truncate | a_mode), FILE_ATTRIBUTE_NORMAL, NULL); - SDL_stack_free(filenameW); + SDL_free(tstr); } #else /* Do not open a dialog box if failure */ old_error_mode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS); - h = CreateFile(filename, (w_right | r_right), - (w_right) ? 0 : FILE_SHARE_READ, NULL, - (must_exist | truncate | a_mode), FILE_ATTRIBUTE_NORMAL, - NULL); + { + LPTSTR tstr = WIN_UTF8ToString(filename); + h = CreateFile(tstr, (w_right | r_right), + (w_right) ? 0 : FILE_SHARE_READ, NULL, + (must_exist | truncate | a_mode), + FILE_ATTRIBUTE_NORMAL, NULL); + SDL_free(tstr); + } - /* restore old behaviour */ + /* restore old behavior */ SetErrorMode(old_error_mode); #endif /* _WIN32_WCE */
--- a/src/haptic/windows/SDL_syshaptic.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/haptic/windows/SDL_syshaptic.c Mon Jan 24 21:20:30 2011 -0800 @@ -39,6 +39,7 @@ static struct { DIDEVICEINSTANCE instance; + char *name; SDL_Haptic *haptic; DIDEVCAPS capabilities; } SDL_hapticlist[MAX_HAPTICS]; @@ -220,6 +221,9 @@ return DIENUM_CONTINUE; } + /* Copy the name */ + SDL_hapticlist[SDL_numhaptics].name = WIN_StringToUTF8(SDL_hapticlist[SDL_numhaptics].instance.tszProductName); + /* Close up device and count it. */ IDirectInputDevice_Release(device); SDL_numhaptics++; @@ -238,7 +242,7 @@ const char * SDL_SYS_HapticName(int index) { - return SDL_hapticlist[index].instance.tszProductName; + return SDL_hapticlist[index].name; } @@ -630,6 +634,15 @@ void SDL_SYS_HapticQuit(void) { + int i; + + for (i = 0; i < SDL_arraysize(SDL_hapticlist); ++i) { + if (SDL_hapticlist[i].name) { + SDL_free(SDL_hapticlist[i].name); + SDL_hapticlist[i].name = NULL; + } + } + IDirectInput_Release(dinput); dinput = NULL; }
--- a/src/joystick/android/SDL_sysjoystick.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/joystick/android/SDL_sysjoystick.c Mon Jan 24 21:20:30 2011 -0800 @@ -32,7 +32,7 @@ #include "SDL_joystick.h" #include "../SDL_sysjoystick.h" #include "../SDL_joystick_c.h" -#include "../../SDL_android.h" +#include "../../core/android/SDL_android.h" static const char *accelerometerName = "Android accelerometer";
--- a/src/joystick/windows/SDL_dxjoystick.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/joystick/windows/SDL_dxjoystick.c Mon Jan 24 21:20:30 2011 -0800 @@ -63,6 +63,7 @@ LPDIRECTINPUT * ppDI, LPUNKNOWN punkOuter); static DIDEVICEINSTANCE SYS_Joystick[MAX_JOYSTICKS]; /* array to hold joystick ID values */ +static char *SYS_JoystickNames[MAX_JOYSTICKS]; static int SYS_NumJoysticks; static HINSTANCE DInputDLL = NULL; @@ -326,6 +327,7 @@ { SDL_memcpy(&SYS_Joystick[SYS_NumJoysticks], pdidInstance, sizeof(DIDEVICEINSTANCE)); + SYS_JoystickNames[SYS_NumJoysticks] = WIN_StringToUTF8(pdidInstance->tszProductName); SYS_NumJoysticks++; if (SYS_NumJoysticks >= MAX_JOYSTICKS) @@ -338,8 +340,7 @@ const char * SDL_SYS_JoystickName(int index) { - /***-> test for invalid index ? */ - return (SYS_Joystick[index].tszProductName); + return SYS_JoystickNames[index]; } /* Function to open a joystick for use. @@ -793,6 +794,15 @@ void SDL_SYS_JoystickQuit(void) { + int i; + + for (i = 0; i < SDL_arraysize(SYS_JoystickNames); ++i) { + if (SYS_JoystickNames[i]) { + SDL_free(SYS_JoystickNames[i]); + SYS_JoystickNames[i] = NULL; + } + } + IDirectInput_Release(dinput); dinput = NULL; }
--- a/src/joystick/windows/SDL_dxjoystick_c.h Mon Jan 24 17:47:18 2011 -0800 +++ b/src/joystick/windows/SDL_dxjoystick_c.h Mon Jan 24 21:20:30 2011 -0800 @@ -33,8 +33,7 @@ * with polled devices, and it's fine to call IDirectInputDevice2_GetDeviceData and * let it return 0 events. */ -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../../core/windows/SDL_windows.h" #define DIRECTINPUT_VERSION 0x0700 /* Need version 7 for force feedback. */ #include <dinput.h>
--- a/src/joystick/windows/SDL_mmjoystick.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/joystick/windows/SDL_mmjoystick.c Mon Jan 24 21:20:30 2011 -0800 @@ -25,8 +25,7 @@ /* Win32 MultiMedia Joystick driver, contributed by Andrei de A. Formiga */ -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../../core/windows/SDL_windows.h" #include <mmsystem.h> #include <regstr.h>
--- a/src/loadso/windows/SDL_sysloadso.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/loadso/windows/SDL_sysloadso.c Mon Jan 24 21:20:30 2011 -0800 @@ -26,110 +26,45 @@ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* System dependent library loading routines */ -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../../core/windows/SDL_windows.h" #include "SDL_loadso.h" void * SDL_LoadObject(const char *sofile) { - void *handle = NULL; - const char *loaderror = "Unknown error"; - -#if defined(_WIN32_WCE) - char errbuf[512]; - - wchar_t *errbuf_t = SDL_malloc(512 * sizeof(wchar_t)); - wchar_t *sofile_t = SDL_malloc((MAX_PATH + 1) * sizeof(wchar_t)); - - MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, sofile, -1, sofile_t, - MAX_PATH); - handle = (void *) LoadLibrary(sofile_t); + LPTSTR tstr = WIN_UTF8ToString(sofile); + void *handle = (void *) LoadLibrary(tstr); + SDL_free(tstr); /* Generate an error message if all loads failed */ if (handle == NULL) { - FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS | - FORMAT_MESSAGE_FROM_SYSTEM), - NULL, GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - errbuf_t, SDL_arraysize(errbuf), NULL); - WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL); - loaderror = errbuf; + char errbuf[512]; + SDL_strlcpy(errbuf, "Failed loading ", SDL_arraysize(errbuf)); + SDL_strlcat(errbuf, sofile, SDL_arraysize(errbuf)); + WIN_SetError(errbuf); } - - SDL_free(sofile_t); - SDL_free(errbuf_t); - -#else /*if defined(__WIN32__) */ - char errbuf[512]; - - handle = (void *) LoadLibrary(sofile); - - /* Generate an error message if all loads failed */ - if (handle == NULL) { - FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS | - FORMAT_MESSAGE_FROM_SYSTEM), - NULL, GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - errbuf, SDL_arraysize(errbuf), NULL); - loaderror = errbuf; - } -#endif - - if (handle == NULL) { - SDL_SetError("Failed loading %s: %s", sofile, loaderror); - } - return (handle); + return handle; } void * SDL_LoadFunction(void *handle, const char *name) { - void *symbol = NULL; - const char *loaderror = "Unknown error"; - -#if defined(_WIN32_WCE) - char errbuf[512]; - int length = SDL_strlen(name); - - wchar_t *name_t = SDL_malloc((length + 1) * sizeof(wchar_t)); - wchar_t *errbuf_t = SDL_malloc(512 * sizeof(wchar_t)); - - MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, name, -1, name_t, length + 1); - - symbol = (void *) GetProcAddress((HMODULE) handle, name_t); - if (symbol == NULL) { - FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS | - FORMAT_MESSAGE_FROM_SYSTEM), - NULL, GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - errbuf_t, SDL_arraysize(errbuf), NULL); - WideCharToMultiByte(CP_ACP, 0, errbuf_t, -1, errbuf, 511, NULL, NULL); - loaderror = errbuf; - } - - SDL_free(name_t); - SDL_free(errbuf_t); - -#else /*if defined(WIN32) */ - char errbuf[512]; - - symbol = (void *) GetProcAddress((HMODULE) handle, name); - if (symbol == NULL) { - FormatMessage((FORMAT_MESSAGE_IGNORE_INSERTS | - FORMAT_MESSAGE_FROM_SYSTEM), - NULL, GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - errbuf, SDL_arraysize(errbuf), NULL); - loaderror = errbuf; - } +#ifdef _WIN32_WCE + LPTSTR tstr = WIN_UTF8ToString(name); + void *symbol = (void *) GetProcAddress((HMODULE) handle, tstr); + SDL_free(tstr); +#else + void *symbol = (void *) GetProcAddress((HMODULE) handle, name); #endif if (symbol == NULL) { - SDL_SetError("Failed loading %s: %s", name, loaderror); + char errbuf[512]; + SDL_strlcpy(errbuf, "Failed loading ", SDL_arraysize(errbuf)); + SDL_strlcat(errbuf, name, SDL_arraysize(errbuf)); + WIN_SetError(errbuf); } - return (symbol); + return symbol; } void
--- a/src/power/windows/SDL_syspower.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/power/windows/SDL_syspower.c Mon Jan 24 21:20:30 2011 -0800 @@ -24,8 +24,7 @@ #ifndef SDL_POWER_DISABLED #ifdef SDL_POWER_WINDOWS -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../../core/windows/SDL_windows.h" #include "SDL_power.h"
--- a/src/stdlib/SDL_getenv.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/stdlib/SDL_getenv.c Mon Jan 24 21:20:30 2011 -0800 @@ -27,8 +27,7 @@ #if defined(__WIN32__) && !defined(_WIN32_WCE) -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../core/windows/SDL_windows.h" /* Note this isn't thread-safe! */ @@ -41,12 +40,12 @@ { if (!overwrite) { char ch = 0; - const size_t len = GetEnvironmentVariable(name, &ch, sizeof (ch)); + const size_t len = GetEnvironmentVariableA(name, &ch, sizeof (ch)); if (len > 0) { return 0; /* asked not to overwrite existing value. */ } } - if (!SetEnvironmentVariable(name, *value ? value : NULL)) { + if (!SetEnvironmentVariableA(name, *value ? value : NULL)) { return -1; } return 0; @@ -59,7 +58,7 @@ size_t bufferlen; bufferlen = - GetEnvironmentVariable(name, SDL_envmem, (DWORD) SDL_envmemlen); + GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen); if (bufferlen == 0) { return NULL; } @@ -70,7 +69,7 @@ } SDL_envmem = newmem; SDL_envmemlen = bufferlen; - GetEnvironmentVariable(name, SDL_envmem, (DWORD) SDL_envmemlen); + GetEnvironmentVariableA(name, SDL_envmem, (DWORD) SDL_envmemlen); } return SDL_envmem; }
--- a/src/thread/windows/SDL_sysmutex.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/thread/windows/SDL_sysmutex.c Mon Jan 24 21:20:30 2011 -0800 @@ -23,8 +23,7 @@ /* Mutex functions using the Win32 API */ -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../../core/windows/SDL_windows.h" #include "SDL_mutex.h"
--- a/src/thread/windows/SDL_syssem.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/thread/windows/SDL_syssem.c Mon Jan 24 21:20:30 2011 -0800 @@ -23,8 +23,7 @@ /* Semaphore functions using the Win32 API */ -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../../core/windows/SDL_windows.h" #include "SDL_thread.h" #if defined(_WIN32_WCE) && (_WIN32_WCE < 300)
--- a/src/thread/windows/SDL_systhread_c.h Mon Jan 24 17:47:18 2011 -0800 +++ b/src/thread/windows/SDL_systhread_c.h Mon Jan 24 21:20:30 2011 -0800 @@ -21,8 +21,7 @@ */ #include "SDL_config.h" -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../../core/windows/SDL_windows.h" typedef HANDLE SYS_ThreadHandle;
--- a/src/thread/windows/win_ce_semaphore.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/thread/windows/win_ce_semaphore.c Mon Jan 24 21:20:30 2011 -0800 @@ -28,8 +28,7 @@ and it is not clear how to handle a mixture of WCE semaphores and normal events and mutexes. */ -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../../core/windows/SDL_windows.h" #include "win_ce_semaphore.h"
--- a/src/timer/wince/SDL_systimer.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/timer/wince/SDL_systimer.c Mon Jan 24 21:20:30 2011 -0800 @@ -23,8 +23,7 @@ #ifdef SDL_TIMER_WINCE -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../../core/windows/SDL_windows.h" #include <mmsystem.h> #include "SDL_thread.h"
--- a/src/timer/windows/SDL_systimer.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/timer/windows/SDL_systimer.c Mon Jan 24 21:20:30 2011 -0800 @@ -23,8 +23,7 @@ #ifdef SDL_TIMER_WINDOWS -#define WIN32_LEAN_AND_MEAN -#include <windows.h> +#include "../../core/windows/SDL_windows.h" #include <mmsystem.h> #include "SDL_timer.h"
--- a/src/video/android/SDL_androidgl.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/video/android/SDL_androidgl.c Mon Jan 24 21:20:30 2011 -0800 @@ -26,7 +26,7 @@ #include "SDL_video.h" #include "SDL_androidvideo.h" -#include "../../SDL_android.h" +#include "../../core/android/SDL_android.h" #include <android/log.h>
--- a/src/video/windows/SDL_gapirender.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/video/windows/SDL_gapirender.c Mon Jan 24 21:20:30 2011 -0800 @@ -78,7 +78,7 @@ typedef struct { // gx.dll - HMODULE hGapiLib; + void* hGapiLib; PFNGXOpenDisplay GXOpenDisplay; PFNGXCloseDisplay GXCloseDisplay; PFNGXBeginDraw GXBeginDraw; @@ -251,7 +251,7 @@ int WINCE_Available(void) { - HMODULE render_gapi; + void* render_gapi; const char* preferably = SDL_getenv("SDL_VIDEO_RENDERER"); // raw check @@ -267,10 +267,10 @@ if(preferably && 0 == SDL_strcasecmp(preferably, RAW_RENDER_NAME)) return 0 != render_raw; // gapi check - render_gapi = LoadLibrary(TEXT("\\Windows\\gx.dll")); + render_gapi = SDL_LoadObject("\\Windows\\gx.dll"); if(0 == render_gapi) - render_gapi = LoadLibrary(TEXT("gx.dll")); - FreeLibrary(render_gapi); + render_gapi = SDL_LoadObject("gx.dll"); + SDL_UnloadObject(render_gapi); if(preferably && 0 == SDL_strcasecmp(preferably, GAPI_RENDER_NAME)) return 0 != render_gapi; @@ -280,7 +280,7 @@ void WINCE_AddRenderDriver(_THIS) { HDC hdc; - HMODULE render_gapi; + void* render_gapi; int render_raw, ii; const char* preferably = SDL_getenv("SDL_VIDEO_RENDERER"); @@ -295,12 +295,12 @@ render_raw = 1; // gapi check - render_gapi = LoadLibrary(TEXT("\\Windows\\gx.dll")); + render_gapi = SDL_LoadObject("\\Windows\\gx.dll"); if(0 == render_gapi) - render_gapi = LoadLibrary(TEXT("gx.dll")); + render_gapi = SDL_LoadObject("gx.dll"); if(render_gapi) - FreeLibrary(render_gapi); + SDL_UnloadObject(render_gapi); for(ii = 0; ii < _this->num_displays; ++ii) { @@ -885,15 +885,15 @@ return 0; } - data->gapi->hGapiLib = LoadLibrary(TEXT("\\Windows\\gx.dll")); + data->gapi->hGapiLib = SDL_LoadObject("\\Windows\\gx.dll"); if(0 == data->gapi->hGapiLib) { - data->gapi->hGapiLib = LoadLibrary(TEXT("gx.dll")); + data->gapi->hGapiLib = SDL_LoadObject("gx.dll"); if(0 == data->gapi->hGapiLib) return 0; } // load gapi library -#define LINK(type,name,import) name=(PFN##type)GetProcAddress(data->gapi->hGapiLib,TEXT(import)) +#define LINK(type,name,import) name=(PFN##type)SDL_LoadFunction(data->gapi->hGapiLib,import) LINK(GXOpenDisplay, data->gapi->GXOpenDisplay, "?GXOpenDisplay@@YAHPAUHWND__@@K@Z"); LINK(GXCloseDisplay, data->gapi->GXCloseDisplay, "?GXCloseDisplay@@YAHXZ"); LINK(GXBeginDraw, data->gapi->GXBeginDraw, "?GXBeginDraw@@YAPAXXZ"); @@ -1005,7 +1005,7 @@ if(data->gapi) { if(data->gapi->GXCloseDisplay) data->gapi->GXCloseDisplay(); - if(data->gapi->hGapiLib) FreeLibrary(data->gapi->hGapiLib); + if(data->gapi->hGapiLib) SDL_UnloadObject(data->gapi->hGapiLib); SDL_free(data->gapi); data->gapi = NULL;
--- a/src/video/windows/SDL_windowskeyboard.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/video/windows/SDL_windowskeyboard.c Mon Jan 24 21:20:30 2011 -0800 @@ -312,15 +312,15 @@ CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, (LPVOID *)&videodata->ime_threadmgr); } videodata->ime_initialized = SDL_TRUE; - videodata->ime_himm32 = LoadLibraryA("imm32.dll"); + videodata->ime_himm32 = SDL_LoadObject("imm32.dll"); if (!videodata->ime_himm32) { videodata->ime_available = SDL_FALSE; return; } - videodata->ImmLockIMC = (LPINPUTCONTEXT2 (WINAPI *)(HIMC))GetProcAddress(videodata->ime_himm32, "ImmLockIMC"); - videodata->ImmUnlockIMC = (BOOL (WINAPI *)(HIMC))GetProcAddress(videodata->ime_himm32, "ImmUnlockIMC"); - videodata->ImmLockIMCC = (LPVOID (WINAPI *)(HIMCC))GetProcAddress(videodata->ime_himm32, "ImmLockIMCC"); - videodata->ImmUnlockIMCC = (BOOL (WINAPI *)(HIMCC))GetProcAddress(videodata->ime_himm32, "ImmUnlockIMCC"); + videodata->ImmLockIMC = (LPINPUTCONTEXT2 (WINAPI *)(HIMC))SDL_LoadFunction(videodata->ime_himm32, "ImmLockIMC"); + videodata->ImmUnlockIMC = (BOOL (WINAPI *)(HIMC))SDL_LoadFunction(videodata->ime_himm32, "ImmUnlockIMC"); + videodata->ImmLockIMCC = (LPVOID (WINAPI *)(HIMCC))SDL_LoadFunction(videodata->ime_himm32, "ImmLockIMCC"); + videodata->ImmUnlockIMCC = (BOOL (WINAPI *)(HIMCC))SDL_LoadFunction(videodata->ime_himm32, "ImmUnlockIMCC"); IME_SetWindow(videodata, hwnd); videodata->ime_himc = ImmGetContext(hwnd); @@ -383,7 +383,7 @@ videodata->ime_hwnd_main = 0; videodata->ime_himc = 0; if (videodata->ime_himm32) { - FreeLibrary(videodata->ime_himm32); + SDL_UnloadObject(videodata->ime_himm32); videodata->ime_himm32 = 0; } if (videodata->ime_threadmgr) { @@ -606,7 +606,7 @@ IME_SetupAPI(SDL_VideoData *videodata) { char ime_file[MAX_PATH + 1]; - HMODULE hime = 0; + void* hime = 0; HKL hkl = 0; videodata->GetReadingString = 0; videodata->ShowReadingWindow = 0; @@ -617,14 +617,14 @@ if (ImmGetIMEFileNameA(hkl, ime_file, sizeof(ime_file) - 1) <= 0) return; - hime = LoadLibraryA(ime_file); + hime = SDL_LoadObject(ime_file); if (!hime) return; videodata->GetReadingString = (UINT (WINAPI *)(HIMC, UINT, LPWSTR, PINT, BOOL*, PUINT)) - GetProcAddress(hime, "GetReadingString"); + SDL_LoadFunction(hime, "GetReadingString"); videodata->ShowReadingWindow = (BOOL (WINAPI *)(HIMC, BOOL)) - GetProcAddress(hime, "ShowReadingWindow"); + SDL_LoadFunction(hime, "ShowReadingWindow"); if (videodata->ShowReadingWindow) { HIMC himc = ImmGetContext(videodata->ime_hwnd_current);
--- a/src/video/windows/SDL_windowsopengl.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/video/windows/SDL_windowsopengl.c Mon Jan 24 21:20:30 2011 -0800 @@ -101,7 +101,7 @@ !_this->gl_data->wglDeleteContext || !_this->gl_data->wglMakeCurrent) { SDL_SetError("Could not retrieve OpenGL functions"); - FreeLibrary(handle); + SDL_UnloadObject(handle); return -1; }
--- a/src/video/windows/SDL_windowsvideo.c Mon Jan 24 17:47:18 2011 -0800 +++ b/src/video/windows/SDL_windowsvideo.c Mon Jan 24 21:20:30 2011 -0800 @@ -37,19 +37,6 @@ static int WIN_VideoInit(_THIS); static void WIN_VideoQuit(_THIS); -/* Sets an error message based on GetLastError() */ -void -WIN_SetError(const char *prefix) -{ - TCHAR buffer[1024]; - char *message; - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, - buffer, SDL_arraysize(buffer), NULL); - message = WIN_StringToUTF8(buffer); - SDL_SetError("%s%s%s", prefix ? prefix : "", prefix ? ": " : "", message); - SDL_free(message); -} - /* Windows driver bootstrap functions */ @@ -68,22 +55,22 @@ #if SDL_VIDEO_RENDER_D3D if (data->d3d) { IDirect3D9_Release(data->d3d); - FreeLibrary(data->d3dDLL); + SDL_UnloadObject(data->d3dDLL); } #endif #if SDL_VIDEO_RENDER_DDRAW if (data->ddraw) { data->ddraw->lpVtbl->Release(data->ddraw); - FreeLibrary(data->ddrawDLL); + SDL_UnloadObject(data->ddrawDLL); } #endif #ifdef _WIN32_WCE if(data->hAygShell) { - FreeLibrary(data->hAygShell); + SDL_UnloadObject(data->hAygShell); } #endif if (data->userDLL) { - FreeLibrary(data->userDLL); + SDL_UnloadObject(data->userDLL); } SDL_free(device->driverdata); @@ -115,24 +102,24 @@ device->driverdata = data; #if SDL_VIDEO_RENDER_D3D - data->d3dDLL = LoadLibrary(TEXT("D3D9.DLL")); + data->d3dDLL = SDL_LoadObject("D3D9.DLL"); if (data->d3dDLL) { IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion); D3DCreate = - (IDirect3D9 * (WINAPI *) (UINT)) GetProcAddress(data->d3dDLL, + (IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(data->d3dDLL, "Direct3DCreate9"); if (D3DCreate) { data->d3d = D3DCreate(D3D_SDK_VERSION); } if (!data->d3d) { - FreeLibrary(data->d3dDLL); + SDL_UnloadObject(data->d3dDLL); data->d3dDLL = NULL; } } #endif /* SDL_VIDEO_RENDER_D3D */ #if SDL_VIDEO_RENDER_DDRAW - data->ddrawDLL = LoadLibrary(TEXT("ddraw.dll")); + data->ddrawDLL = SDL_LoadObject("ddraw.dll"); if (data->ddrawDLL) { IDirectDraw *(WINAPI * DDCreate) (GUID FAR * lpGUID, LPDIRECTDRAW FAR * lplpDD, @@ -141,9 +128,9 @@ DDCreate = (IDirectDraw * (WINAPI *) (GUID FAR *, LPDIRECTDRAW FAR *, IUnknown FAR *)) - GetProcAddress(data->ddrawDLL, TEXT("DirectDrawCreate")); + SDL_LoadFunction(data->ddrawDLL, "DirectDrawCreate"); if (!DDCreate || DDCreate(NULL, &data->ddraw, NULL) != DD_OK) { - FreeLibrary(data->ddrawDLL); + SDL_UnloadObject(data->ddrawDLL); data->ddrawDLL = NULL; data->ddraw = NULL; } @@ -151,19 +138,19 @@ #endif /* SDL_VIDEO_RENDER_DDRAW */ #ifdef _WIN32_WCE - data->hAygShell = LoadLibrary(TEXT("\\windows\\aygshell.dll")); + data->hAygShell = SDL_LoadObject("\\windows\\aygshell.dll"); if(0 == data->hAygShell) - data->hAygShell = LoadLibrary(TEXT("aygshell.dll")); + data->hAygShell = SDL_LoadObject("aygshell.dll"); data->SHFullScreen = (0 != data->hAygShell ? - (PFNSHFullScreen) GetProcAddress(data->hAygShell, TEXT("SHFullScreen")) : 0); + (PFNSHFullScreen) SDL_LoadFunction(data->hAygShell, "SHFullScreen") : 0); data->CoordTransform = NULL; #endif - data->userDLL = LoadLibrary(TEXT("USER32.DLL")); + data->userDLL = SDL_LoadObject("USER32.DLL"); if (data->userDLL) { - data->CloseTouchInputHandle = (BOOL (WINAPI *)( HTOUCHINPUT )) GetProcAddress(data->userDLL, TEXT("CloseTouchInputHandle")); - data->GetTouchInputInfo = (BOOL (WINAPI *)( HTOUCHINPUT, UINT, PTOUCHINPUT, int )) GetProcAddress(data->userDLL, TEXT("GetTouchInputInfo")); - data->RegisterTouchWindow = (BOOL (WINAPI *)( HWND, ULONG )) GetProcAddress(data->userDLL, TEXT("RegisterTouchWindow")); + data->CloseTouchInputHandle = (BOOL (WINAPI *)( HTOUCHINPUT )) SDL_LoadFunction(data->userDLL, "CloseTouchInputHandle"); + data->GetTouchInputInfo = (BOOL (WINAPI *)( HTOUCHINPUT, UINT, PTOUCHINPUT, int )) SDL_LoadFunction(data->userDLL, "GetTouchInputInfo"); + data->RegisterTouchWindow = (BOOL (WINAPI *)( HWND, ULONG )) SDL_LoadFunction(data->userDLL, "RegisterTouchWindow"); } /* Set the function pointers */
--- a/src/video/windows/SDL_windowsvideo.h Mon Jan 24 17:47:18 2011 -0800 +++ b/src/video/windows/SDL_windowsvideo.h Mon Jan 24 21:20:30 2011 -0800 @@ -26,15 +26,7 @@ #include "../SDL_sysvideo.h" -#define WIN32_LEAN_AND_MEAN -#define STRICT -#ifndef UNICODE -#define UNICODE -#endif -#undef WINVER -#define WINVER 0x500 /* Need 0x410 for AlphaBlend() and 0x500 for EnumDisplayDevices() */ - -#include <windows.h> +#include "../../core/windows/SDL_windows.h" #if defined(_MSC_VER) && !defined(_WIN32_WCE) #include <msctf.h> @@ -68,15 +60,8 @@ #include "SDL_windowsopengl.h" #include "SDL_windowswindow.h" #include "SDL_events.h" +#include "SDL_loadso.h" -#ifdef UNICODE -#define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "UCS-2", (char *)S, (SDL_wcslen(S)+1)*sizeof(WCHAR)) -#define WIN_UTF8ToString(S) (WCHAR *)SDL_iconv_string("UCS-2", "UTF-8", (char *)S, SDL_strlen(S)+1) -#else -#define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "ASCII", (char *)S, (SDL_strlen(S)+1)) -#define WIN_UTF8ToString(S) SDL_iconv_string("ASCII", "UTF-8", (char *)S, SDL_strlen(S)+1) -#endif -extern void WIN_SetError(const char *prefix); enum { RENDER_NONE, RENDER_D3D, RENDER_DDRAW, RENDER_GDI, RENDER_GAPI, RENDER_RAW }; @@ -147,15 +132,15 @@ int render; #if SDL_VIDEO_RENDER_D3D - HANDLE d3dDLL; + void* d3dDLL; IDirect3D9 *d3d; #endif #if SDL_VIDEO_RENDER_DDRAW - HANDLE ddrawDLL; + void* ddrawDLL; IDirectDraw *ddraw; #endif #ifdef _WIN32_WCE - HMODULE hAygShell; + void* hAygShell; PFNSHFullScreen SHFullScreen; PFCoordTransform CoordTransform; #endif @@ -164,7 +149,7 @@ DWORD clipboard_count; /* Touch input functions */ - HANDLE userDLL; + void* userDLL; BOOL (WINAPI *CloseTouchInputHandle)( HTOUCHINPUT ); BOOL (WINAPI *GetTouchInputInfo)( HTOUCHINPUT, UINT, PTOUCHINPUT, int ); BOOL (WINAPI *RegisterTouchWindow)( HWND, ULONG ); @@ -199,7 +184,7 @@ int ime_winheight; HKL ime_hkl; - HMODULE ime_himm32; + void* ime_himm32; UINT (WINAPI *GetReadingString)(HIMC himc, UINT uReadingBufLen, LPWSTR lpwReadingBuf, PINT pnErrorIndex, BOOL *pfIsVertical, PUINT puMaxReadingLen); BOOL (WINAPI *ShowReadingWindow)(HIMC himc, BOOL bShow); LPINPUTCONTEXT2 (WINAPI *ImmLockIMC)(HIMC himc);