Skip to content

Commit

Permalink
Platform: query DPI awareness on Apple and warn if not set.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mosra committed Aug 22, 2018
1 parent ae31c3c commit 769bc0d
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 6 deletions.
10 changes: 8 additions & 2 deletions modules/FindMagnum.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 13 additions & 1 deletion src/Magnum/Platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions src/Magnum/Platform/Implementation/dpiScaling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
#include <emscripten/html5.h>
#endif

namespace Magnum { namespace Platform { namespace Implementation { namespace {
namespace Magnum { namespace Platform { namespace Implementation {

namespace {

inline Utility::Arguments windowScalingArguments() {
Utility::Arguments args{"magnum"};
Expand Down Expand Up @@ -126,6 +128,12 @@ inline Float emscriptenDpiScaling() {
}
#endif

}}}}
}

#ifdef CORRADE_TARGET_APPLE
bool isAppleBundleHiDpiEnabled();
#endif

}}}

#endif
40 changes: 40 additions & 0 deletions src/Magnum/Platform/Implementation/dpiScaling.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018
Vladimír Vondruš <mosra@centrum.cz>
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"]);
}

}}}
9 changes: 9 additions & 0 deletions src/Magnum/Platform/Sdl2Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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{};
Expand Down
4 changes: 3 additions & 1 deletion src/Magnum/Platform/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
20 changes: 20 additions & 0 deletions src/Magnum/Platform/Test/Sdl2ApplicationTest.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en-US</string>
<key>CFBundleExecutable</key>
<string>${MACOSX_BUNDLE_EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>cz.mosra.magnum.Sdl2ApplicationTest</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>Magnum Triangle</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>NSHighResolutionCapable</key>
<true/>
</dict>
</plist>

0 comments on commit 769bc0d

Please sign in to comment.