From 769bc0da9075a8ee77c51951a069d02011d76aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 19 Aug 2018 14:14:34 +0200 Subject: [PATCH] Platform: query DPI awareness on Apple and warn if not set. I'm not really sure if the extra work and link dependencies are worth the warning, but since I *need* to do something similar for Windows, why not have it here as well. --- modules/FindMagnum.cmake | 10 ++++- src/Magnum/Platform/CMakeLists.txt | 14 ++++++- .../Platform/Implementation/dpiScaling.hpp | 12 +++++- .../Platform/Implementation/dpiScaling.mm | 40 +++++++++++++++++++ src/Magnum/Platform/Sdl2Application.cpp | 9 +++++ src/Magnum/Platform/Test/CMakeLists.txt | 4 +- .../Platform/Test/Sdl2ApplicationTest.plist | 20 ++++++++++ 7 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 src/Magnum/Platform/Implementation/dpiScaling.mm create mode 100644 src/Magnum/Platform/Test/Sdl2ApplicationTest.plist diff --git a/modules/FindMagnum.cmake b/modules/FindMagnum.cmake index d0c7aec12..b4641b13b 100644 --- a/modules/FindMagnum.cmake +++ b/modules/FindMagnum.cmake @@ -639,8 +639,14 @@ foreach(_component ${Magnum_FIND_COMPONENTS}) find_package(SDL2) set_property(TARGET Magnum::${_component} APPEND PROPERTY INTERFACE_LINK_LIBRARIES SDL2::SDL2) - if(CORRADE_TARGET_UNIX AND NOT CORRADE_TARGET_APPLE) - # Needed for opt-in DPI queries + # Use the Foundation framework on Apple to query the DPI awareness + if(CORRADE_TARGET_APPLE) + find_library(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY Foundation) + mark_as_advanced(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY) + set_property(TARGET Magnum::${_component} APPEND PROPERTY + INTERFACE_LINK_LIBRARIES ${_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY}) + # Needed for opt-in DPI queries + elseif(CORRADE_TARGET_UNIX) set_property(TARGET Magnum::${_component} APPEND PROPERTY INTERFACE_LINK_LIBRARIES ${CMAKE_DL_LIBS}) endif() diff --git a/src/Magnum/Platform/CMakeLists.txt b/src/Magnum/Platform/CMakeLists.txt index ac5e8ec48..386be90dd 100644 --- a/src/Magnum/Platform/CMakeLists.txt +++ b/src/Magnum/Platform/CMakeLists.txt @@ -205,6 +205,9 @@ if(WITH_SDL2APPLICATION) if(TARGET_GL) list(APPEND MagnumSdl2Application_SRCS ${MagnumSomeContext_OBJECTS}) endif() + if(CORRADE_TARGET_APPLE) + list(APPEND MagnumSdl2Application_SRCS Implementation/dpiScaling.mm) + endif() add_library(MagnumSdl2Application STATIC ${MagnumSdl2Application_SRCS} @@ -222,8 +225,17 @@ if(WITH_SDL2APPLICATION) ${MagnumSomeContext_LIBRARY}) endif() + # Use the Foundation framework on Apple to query the DPI awareness + if(CORRADE_TARGET_APPLE) + find_library(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY Foundation) + mark_as_advanced(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY) + find_path(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_INCLUDE_DIR NAMES NSBundle.h) + mark_as_advanced(_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_INCLUDE_DIR) + target_link_libraries(MagnumSdl2Application PUBLIC ${_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_LIBRARY}) + target_include_directories(MagnumSdl2Application PRIVATE ${_MAGNUM_APPLE_FOUNDATION_FRAMEWORK_INCLUDE_DIR}) + # If there is X11, ask it for DPI - if(CORRADE_TARGET_UNIX AND NOT CORRADE_TARGET_APPLE) + elseif(CORRADE_TARGET_UNIX) find_package(X11) if(X11_FOUND) # Not linking to X11, we dlopen() instead diff --git a/src/Magnum/Platform/Implementation/dpiScaling.hpp b/src/Magnum/Platform/Implementation/dpiScaling.hpp index 3ca96aa41..53ccd0fb1 100644 --- a/src/Magnum/Platform/Implementation/dpiScaling.hpp +++ b/src/Magnum/Platform/Implementation/dpiScaling.hpp @@ -40,7 +40,9 @@ #include #endif -namespace Magnum { namespace Platform { namespace Implementation { namespace { +namespace Magnum { namespace Platform { namespace Implementation { + +namespace { inline Utility::Arguments windowScalingArguments() { Utility::Arguments args{"magnum"}; @@ -126,6 +128,12 @@ inline Float emscriptenDpiScaling() { } #endif -}}}} +} + +#ifdef CORRADE_TARGET_APPLE +bool isAppleBundleHiDpiEnabled(); +#endif + +}}} #endif diff --git a/src/Magnum/Platform/Implementation/dpiScaling.mm b/src/Magnum/Platform/Implementation/dpiScaling.mm new file mode 100644 index 000000000..1a8da6932 --- /dev/null +++ b/src/Magnum/Platform/Implementation/dpiScaling.mm @@ -0,0 +1,40 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 + Vladimír Vondruš + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included + in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. +*/ + +#import "NSBundle.h" + +#include "dpiScaling.hpp" + +namespace Magnum { namespace Platform { namespace Implementation { + +/* HiDPI is available only for bundles, so if the executable is not a bundle, + return false */ +bool isAppleBundleHiDpiEnabled() { + NSBundle* bundle = [NSBundle mainBundle]; + if(!bundle) return false; + return bool([bundle objectForInfoDictionaryKey:@"NSHighResolutionCapable"]); +} + +}}} diff --git a/src/Magnum/Platform/Sdl2Application.cpp b/src/Magnum/Platform/Sdl2Application.cpp index 24085574a..af7b37d22 100644 --- a/src/Magnum/Platform/Sdl2Application.cpp +++ b/src/Magnum/Platform/Sdl2Application.cpp @@ -135,6 +135,15 @@ void Sdl2Application::create(const Configuration& configuration, const GLConfigu Vector2 Sdl2Application::dpiScaling(const Configuration& configuration) const { std::ostream* verbose = _verboseLog ? Debug::output() : nullptr; + /* Print a helpful warning in case some extra steps are needed for HiDPI + support */ + #ifdef CORRADE_TARGET_APPLE + if(!Implementation::isAppleBundleHiDpiEnabled()) + Warning{} << "Platform::Sdl2Application: warning: the executable is not a HiDPI-enabled app bundle"; + #elif defined(CORRADE_TARGET_WINDOWS) + /** @todo */ + #endif + /* Use values from the configuration only if not overriden on command line. In any case explicit scaling has a precedence before the policy. */ Implementation::DpiScalingPolicy dpiScalingPolicy{}; diff --git a/src/Magnum/Platform/Test/CMakeLists.txt b/src/Magnum/Platform/Test/CMakeLists.txt index e06c6f4a9..c13c0bfef 100644 --- a/src/Magnum/Platform/Test/CMakeLists.txt +++ b/src/Magnum/Platform/Test/CMakeLists.txt @@ -58,8 +58,10 @@ if(WITH_SDL2APPLICATION) ../WebApplication.css Sdl2ApplicationTest.html DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) - elseif(CORRADE_TARGET_IOS) + elseif(CORRADE_TARGET_IOS OR CORRADE_TARGET_APPLE) + # The plist is needed in order to mark the app as DPI-aware set_target_properties(PlatformSdl2ApplicationTest PROPERTIES + MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/Sdl2ApplicationTest.plist MACOSX_BUNDLE ON XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "YES") endif() diff --git a/src/Magnum/Platform/Test/Sdl2ApplicationTest.plist b/src/Magnum/Platform/Test/Sdl2ApplicationTest.plist new file mode 100644 index 000000000..0ae5e0837 --- /dev/null +++ b/src/Magnum/Platform/Test/Sdl2ApplicationTest.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + en-US + CFBundleExecutable + ${MACOSX_BUNDLE_EXECUTABLE_NAME} + CFBundleIdentifier + cz.mosra.magnum.Sdl2ApplicationTest + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + Magnum Triangle + CFBundlePackageType + APPL + NSHighResolutionCapable + + +