From 368ba2a0132616f915aaa6001dc9354d95f13d4a Mon Sep 17 00:00:00 2001 From: Falcury Date: Sat, 17 Aug 2019 13:59:50 +0200 Subject: [PATCH] Miscellaneous compatibility tweaks * CMakeLists.txt: Retrieve SDL2 location using the sdl2-config utility * CMakeLists.txt: Build for Mac OS 10.4 minimum * .gitignore: ignore .DS_Store (macOS folder metadata) * Supply strnlen() if necessary (missing on older macOS versions) * Allow running without joystick support if SDL was built without it --- .gitignore | 3 +++ src/CMakeLists.txt | 18 +++++++++++++----- src/common.h | 9 +++++++++ src/data.h | 1 + src/seg009.c | 32 +++++++++++++------------------- 5 files changed, 39 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 7294d3f4..92fa3ee7 100644 --- a/.gitignore +++ b/.gitignore @@ -66,6 +66,9 @@ build/ # Windows thumbnail cache Thumbs.db +# macOS folder metadata +.DS_Store + # The desktop file has to be recreated from the template. src/SDLPoP.desktop diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cea39526..b4444153 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -16,10 +16,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${SDLPoP_SOURCE_DIR}/..") #set(SDL2 "/usr/local/Cellar/sdl2/2.0.5") -if (NOT(WIN32) AND (DEFINED SDL2)) - include_directories(${SDL2}/include) - link_directories(${SDL2}/lib) -endif() +# If you don't specify the location of SDL2, this script will try to guess the location, +# or retrieve it using the sdl2-config utility. if (WIN32) if (MSVC) @@ -49,6 +47,16 @@ if (WIN32) include_directories(${SDL2}/${SDL2_ARCH}/include) link_directories(${SDL2}/${SDL2_ARCH}/lib) +else() + if (DEFINED SDL2) + include_directories(${SDL2}/include) + link_directories(${SDL2}/lib) + else() + execute_process(COMMAND sdl2-config --prefix OUTPUT_VARIABLE SDL2_PREFIX) + string(STRIP ${SDL2_PREFIX} SDL2_PREFIX) + include_directories(${SDL2_PREFIX}/include) + link_directories(${SDL2_PREFIX}/lib) + endif() endif() set(SOURCE_FILES @@ -89,7 +97,7 @@ else() set(MACOSX_BUNDLE_ICON_FILE "icon.icns") set_source_files_properties(${MACOSX_BUNDLE_ICON_FILE} PROPERTIES MACOSX_PACKAGE_LOCATION Resources) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmacosx-version-min=10.6") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mmacosx-version-min=10.4") set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -headerpad_max_install_names") add_executable(prince MACOSX_BUNDLE ${SOURCE_FILES} ${MACOSX_BUNDLE_ICON_FILE}) set_target_properties(prince PROPERTIES MACOSX_BUNDLE_INFO_PLIST ${CMAKE_SOURCE_DIR}/Info.plist) diff --git a/src/common.h b/src/common.h index 8e19d882..2018a1b7 100644 --- a/src/common.h +++ b/src/common.h @@ -67,6 +67,15 @@ extern "C" { #define ABS(x) ((x)<0?-(x):(x)) #endif +#if defined(__APPLE__) && !defined(strnlen) +// Use this if strnlen is missing. +static inline size_t strnlen(const char *str, size_t max) +{ + const char *end = memchr (str, 0, max); + return end ? (size_t)(end - str) : max; +} +#endif + #ifdef __cplusplus } #endif diff --git a/src/data.h b/src/data.h index 747ed4c4..14455a42 100644 --- a/src/data.h +++ b/src/data.h @@ -558,6 +558,7 @@ extern SDL_Texture* texture_fuzzy; extern SDL_Texture* texture_blurry; extern SDL_Texture* target_texture; +extern bool is_joyst_supported; extern SDL_GameController* sdl_controller_ INIT( = 0 ); extern SDL_Joystick* sdl_joystick_; // in case our joystick is not compatible with SDL_GameController extern byte using_sdl_joystick_interface; diff --git a/src/seg009.c b/src/seg009.c index 0a0e3173..e2386e0c 100644 --- a/src/seg009.c +++ b/src/seg009.c @@ -671,6 +671,7 @@ void __pascal far draw_image_transp(image_type far *image,image_type far *mask,i // seg009:157E int __pascal far set_joy_mode() { // stub + if (!is_joyst_supported) return; if (SDL_NumJoysticks() < 1) { is_joyst_mode = 0; } else { @@ -696,7 +697,6 @@ int __pascal far set_joy_mode() { } else { sdl_haptic = NULL; } - is_keyboard_mode = !is_joyst_mode; return is_joyst_mode; } @@ -1738,25 +1738,11 @@ void init_digi() { // Open the audio device. Called once. //printf("init_digi(): called\n"); - SDL_AudioFormat desired_audioformat; - SDL_version version; - SDL_GetVersion(&version); - //printf("SDL Version = %d.%d.%d\n", version.major, version.minor, version.patch); - if (version.major <= 2 && version.minor <= 0 && version.patch <= 3) { - // In versions before 2.0.4, 16-bit audio samples don't work properly (the sound becomes garbled). - // See: https://bugzilla.libsdl.org/show_bug.cgi?id=2389 - // Workaround: set the audio format to 8-bit, if we are linking against an older SDL2 version. - desired_audioformat = AUDIO_U8; - printf("Your SDL.dll is older than 2.0.4. Using 8-bit audio format to work around resampling bug."); - } else { - desired_audioformat = AUDIO_S16SYS; - } - SDL_AudioSpec *desired; desired = (SDL_AudioSpec *)malloc(sizeof(SDL_AudioSpec)); memset(desired, 0, sizeof(SDL_AudioSpec)); desired->freq = digi_samplerate; //buffer->digi.sample_rate; - desired->format = desired_audioformat; + desired->format = AUDIO_S16SYS; desired->channels = 2; desired->samples = 1024; desired->callback = audio_callback; @@ -2128,11 +2114,16 @@ void __pascal far set_gr_mode(byte grmode) { #ifdef SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING SDL_SetHint(SDL_HINT_WINDOWS_DISABLE_THREAD_NAMING, "1"); #endif - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE | - SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC ) != 0) { + Uint32 init_flags = SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_NOPARACHUTE; + if (SDL_Init(init_flags) != 0) { sdlperror("SDL_Init"); quit(1); } + + is_joyst_supported = (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER | SDL_INIT_HAPTIC) == 0); + if (!is_joyst_supported) { + sdlperror("SDL_InitSubSystem"); + } //SDL_EnableUNICODE(1); //deprecated Uint32 flags = 0; @@ -2940,6 +2931,7 @@ void process_events() { #endif break; case SDL_CONTROLLERAXISMOTION: + if (!is_joyst_supported) break; if (event.caxis.axis < 6) { joy_axis[event.caxis.axis] = event.caxis.value; @@ -2952,6 +2944,7 @@ void process_events() { } break; case SDL_CONTROLLERBUTTONDOWN: + if (!is_joyst_supported) break; #ifdef USE_AUTO_INPUT_MODE if (!is_joyst_mode) { is_joyst_mode = 1; @@ -2983,6 +2976,7 @@ void process_events() { } break; case SDL_CONTROLLERBUTTONUP: + if (!is_joyst_supported) break; switch (event.cbutton.button) { case SDL_CONTROLLER_BUTTON_DPAD_LEFT: joy_hat_states[0] = 0; break; // left @@ -3001,6 +2995,7 @@ void process_events() { case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONUP: case SDL_JOYAXISMOTION: + if (!is_joyst_supported) break; // Only handle the event if the joystick is incompatible with the SDL_GameController interface. // (Otherwise it will interfere with the normal action of the SDL_GameController API.) if (!using_sdl_joystick_interface) { @@ -3035,7 +3030,6 @@ void process_events() { else if (event.jbutton.button == SDL_JOYSTICK_BUTTON_X) joy_X_button_state = 0; // X (shift) } break; - case SDL_TEXTINPUT: last_text_input = event.text.text[0]; // UTF-8 formatted char text input break;