diff options
Diffstat (limited to 'src/corelib/kernel/qcore_mac_p.h')
-rw-r--r-- | src/corelib/kernel/qcore_mac_p.h | 162 |
1 files changed, 92 insertions, 70 deletions
diff --git a/src/corelib/kernel/qcore_mac_p.h b/src/corelib/kernel/qcore_mac_p.h index 538ade3466..5ecf8072f4 100644 --- a/src/corelib/kernel/qcore_mac_p.h +++ b/src/corelib/kernel/qcore_mac_p.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QCORE_MAC_P_H #define QCORE_MAC_P_H @@ -54,7 +18,19 @@ #include "private/qglobal_p.h" #include <QtCore/qoperatingsystemversion.h> + +#include <optional> + +#ifdef Q_OS_MACOS +#include <mach/port.h> struct mach_header; +typedef int kern_return_t; +typedef mach_port_t io_object_t; +extern "C" { +kern_return_t IOObjectRetain(io_object_t object); +kern_return_t IOObjectRelease(io_object_t object); +} +#endif #ifndef __IMAGECAPTURE__ # define __IMAGECAPTURE__ @@ -74,7 +50,6 @@ struct mach_header; #endif #include "qstring.h" -#include "qscopedpointer.h" #include "qpair.h" #if defined( __OBJC__) && defined(QT_NAMESPACE) @@ -103,20 +78,26 @@ struct mach_header; Q_FORWARD_DECLARE_OBJC_CLASS(NSObject); Q_FORWARD_DECLARE_OBJC_CLASS(NSString); +// @compatibility_alias doesn't work with categories or their methods +#define QtExtras QT_MANGLE_NAMESPACE(QtExtras) + QT_BEGIN_NAMESPACE -template <typename T, typename U, U (*RetainFunction)(U), void (*ReleaseFunction)(U)> +template <typename T, typename U, auto RetainFunction, auto ReleaseFunction> class QAppleRefCounted { public: - QAppleRefCounted() : value() {} - QAppleRefCounted(const T &t) : value(t) {} - QAppleRefCounted(T &&t) noexcept(std::is_nothrow_move_constructible<T>::value) + Q_NODISCARD_CTOR QAppleRefCounted() : value() {} + Q_NODISCARD_CTOR QAppleRefCounted(const T &t) : value(t) {} + Q_NODISCARD_CTOR QAppleRefCounted(T &&t) + noexcept(std::is_nothrow_move_constructible<T>::value) : value(std::move(t)) {} - QAppleRefCounted(QAppleRefCounted &&other) + Q_NODISCARD_CTOR QAppleRefCounted(QAppleRefCounted &&other) noexcept(std::is_nothrow_move_assignable<T>::value && std::is_nothrow_move_constructible<T>::value) - : value(qExchange(other.value, T())) {} - QAppleRefCounted(const QAppleRefCounted &other) : value(other.value) { if (value) RetainFunction(value); } + : value(std::exchange(other.value, T())) {} + Q_NODISCARD_CTOR QAppleRefCounted(const QAppleRefCounted &other) + : value(other.value) + { if (value) RetainFunction(value); } ~QAppleRefCounted() { if (value) ReleaseFunction(value); } operator T() const { return value; } void swap(QAppleRefCounted &other) noexcept(noexcept(qSwap(value, other.value))) @@ -132,20 +113,29 @@ protected: T value; }; +class QMacAutoReleasePool +{ +public: + Q_NODISCARD_CTOR Q_CORE_EXPORT QMacAutoReleasePool(); + Q_CORE_EXPORT ~QMacAutoReleasePool(); +private: + Q_DISABLE_COPY(QMacAutoReleasePool) + void *pool; +}; #ifdef Q_OS_MACOS class QMacRootLevelAutoReleasePool { public: - QMacRootLevelAutoReleasePool(); + Q_NODISCARD_CTOR QMacRootLevelAutoReleasePool(); ~QMacRootLevelAutoReleasePool(); private: - QScopedPointer<QMacAutoReleasePool> pool; + std::optional<QMacAutoReleasePool> pool = std::nullopt; }; #endif /* - Helper class that automates refernce counting for CFtypes. + Helper class that automates reference counting for CFtypes. After constructing the QCFType object, it can be copied like a value-based type. @@ -162,7 +152,7 @@ class QCFType : public QAppleRefCounted<T, CFTypeRef, CFRetain, CFRelease> using Base = QAppleRefCounted<T, CFTypeRef, CFRetain, CFRelease>; public: using Base::Base; - explicit QCFType(CFTypeRef r) : Base(static_cast<T>(r)) {} + Q_NODISCARD_CTOR explicit QCFType(CFTypeRef r) : Base(static_cast<T>(r)) {} template <typename X> X as() const { return reinterpret_cast<X>(this->value); } static QCFType constructFromGet(const T &t) { @@ -172,15 +162,23 @@ public: } }; -class Q_CORE_EXPORT QCFString : public QCFType<CFStringRef> +#ifdef Q_OS_MACOS +template <typename T> +class QIOType : public QAppleRefCounted<T, io_object_t, IOObjectRetain, IOObjectRelease> +{ + using QAppleRefCounted<T, io_object_t, IOObjectRetain, IOObjectRelease>::QAppleRefCounted; +}; +#endif + +class QCFString : public QCFType<CFStringRef> { public: using QCFType<CFStringRef>::QCFType; - inline QCFString(const QString &str) : QCFType<CFStringRef>(0), string(str) {} - inline QCFString(const CFStringRef cfstr = 0) : QCFType<CFStringRef>(cfstr) {} - inline QCFString(const QCFType<CFStringRef> &other) : QCFType<CFStringRef>(other) {} - operator QString() const; - operator CFStringRef() const; + Q_NODISCARD_CTOR QCFString(const QString &str) : QCFType<CFStringRef>(0), string(str) {} + Q_NODISCARD_CTOR QCFString(const CFStringRef cfstr = 0) : QCFType<CFStringRef>(cfstr) {} + Q_NODISCARD_CTOR QCFString(const QCFType<CFStringRef> &other) : QCFType<CFStringRef>(other) {} + Q_CORE_EXPORT operator QString() const; + Q_CORE_EXPORT operator CFStringRef() const; private: QString string; @@ -189,6 +187,10 @@ private: #ifdef Q_OS_MACOS Q_CORE_EXPORT bool qt_mac_applicationIsInDarkMode(); Q_CORE_EXPORT bool qt_mac_runningUnderRosetta(); +Q_CORE_EXPORT std::optional<uint32_t> qt_mac_sipConfiguration(); +#ifdef QT_BUILD_INTERNAL +Q_AUTOTEST_EXPORT void qt_mac_ensureResponsible(); +#endif #endif #ifndef QT_NO_DEBUG_STREAM @@ -197,15 +199,18 @@ Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QCFString &string); #endif Q_CORE_EXPORT bool qt_apple_isApplicationExtension(); + +#if !defined(QT_BOOTSTRAPPED) Q_CORE_EXPORT bool qt_apple_isSandboxed(); -#if !defined(QT_BOOTSTRAPPED) && defined(__OBJC__) +#if defined(__OBJC__) QT_END_NAMESPACE -@interface NSObject (QtSandboxHelpers) +@interface NSObject (QtExtras) - (id)qt_valueForPrivateKey:(NSString *)key; @end QT_BEGIN_NAMESPACE #endif +#endif // !QT_BOOTSTRAPPED #if !defined(QT_BOOTSTRAPPED) && !defined(Q_OS_WATCHOS) QT_END_NAMESPACE @@ -232,9 +237,12 @@ QT_BEGIN_NAMESPACE class Q_CORE_EXPORT AppleUnifiedLogger { public: - static bool messageHandler(QtMsgType msgType, const QMessageLogContext &context, const QString &message, - const QString &subsystem = QString()); - static bool willMirrorToStderr(); + static bool messageHandler(QtMsgType msgType, const QMessageLogContext &context, + const QString &message) + { return messageHandler(msgType, context, message, QString()); } + static bool messageHandler(QtMsgType msgType, const QMessageLogContext &context, + const QString &message, const QString &subsystem); + static bool preventsStderrLogging(); private: static os_log_type_t logTypeForMessageType(QtMsgType msgType); static os_log_t cachedLog(const QString &subsystem, const QString &category); @@ -262,7 +270,7 @@ public: Q_DISABLE_COPY(QAppleLogActivity) QAppleLogActivity(QAppleLogActivity &&other) - : activity(qExchange(other.activity, nullptr)), state(other.state) + : activity(std::exchange(other.activity, nullptr)), state(other.state) { } @@ -288,8 +296,8 @@ public: void swap(QAppleLogActivity &other) { - qSwap(activity, other.activity); - qSwap(state, other.state); + activity.swap(other.activity); + std::swap(state, other.state); } private: @@ -337,7 +345,7 @@ public: QMacNotificationObserver(const QMacNotificationObserver &other) = delete; QMacNotificationObserver(QMacNotificationObserver &&other) - : observer(qExchange(other.observer, nullptr)) + : observer(std::exchange(other.observer, nullptr)) { } @@ -346,7 +354,7 @@ public: void swap(QMacNotificationObserver &other) noexcept { - qSwap(observer, other.observer); + qt_ptr_swap(observer, other.observer); } void remove(); @@ -396,9 +404,9 @@ public: void swap(QMacKeyValueObserver &other) noexcept { - qSwap(object, other.object); - qSwap(keyPath, other.keyPath); - qSwap(callback, other.callback); + qt_ptr_swap(object, other.object); + qt_ptr_swap(keyPath, other.keyPath); + callback.swap(other.callback); } private: @@ -429,7 +437,7 @@ public: private: QMacVersion() = default; - using VersionTuple = QPair<QOperatingSystemVersion, QOperatingSystemVersion>; + using VersionTuple = std::pair<QOperatingSystemVersion, QOperatingSystemVersion>; static VersionTuple versionsForImage(const mach_header *machHeader); static VersionTuple applicationVersion(); static VersionTuple libraryVersion(); @@ -437,6 +445,20 @@ private: // ------------------------------------------------------------------------- +#ifdef __OBJC__ +template <typename T> +typename std::enable_if<std::is_pointer<T>::value, T>::type +qt_objc_cast(id object) +{ + if ([object isKindOfClass:[typename std::remove_pointer<T>::type class]]) + return static_cast<T>(object); + + return nil; +} +#endif + +// ------------------------------------------------------------------------- + QT_END_NAMESPACE #endif // QCORE_MAC_P_H |