Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcore_mac_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QCORE_MAC_P_H
5#define QCORE_MAC_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of other Qt classes. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "private/qglobal_p.h"
19
20#include <QtCore/qoperatingsystemversion.h>
21
22#include <optional>
23
24#ifdef Q_OS_MACOS
25#include <mach/port.h>
26struct mach_header;
27typedef int kern_return_t;
28typedef mach_port_t io_object_t;
29extern "C" {
30kern_return_t IOObjectRetain(io_object_t object);
31kern_return_t IOObjectRelease(io_object_t object);
32}
33#endif
34
35#ifndef __IMAGECAPTURE__
36# define __IMAGECAPTURE__
37#endif
38
39// --------------------------------------------------------------------------
40
41#if defined(QT_BOOTSTRAPPED)
42#include <ApplicationServices/ApplicationServices.h>
43#else
44#include <CoreFoundation/CoreFoundation.h>
45#endif
46
47#ifdef __OBJC__
48#include <Foundation/Foundation.h>
49#include <functional>
50#endif
51
52#include "qstring.h"
53#include "qpair.h"
54
55#if defined( __OBJC__) && defined(QT_NAMESPACE)
56#define QT_NAMESPACE_ALIAS_OBJC_CLASS(__KLASS__) @compatibility_alias __KLASS__ QT_MANGLE_NAMESPACE(__KLASS__)
57#else
58#define QT_NAMESPACE_ALIAS_OBJC_CLASS(__KLASS__)
59#endif
60
61#define QT_MAC_WEAK_IMPORT(symbol) extern "C" decltype(symbol) symbol __attribute__((weak_import));
62
63#if defined(__OBJC__)
64#define QT_DECLARE_NAMESPACED_OBJC_INTERFACE(classname, definition)
65 @interface QT_MANGLE_NAMESPACE(classname) :
66 definition
67 @end
68 QT_NAMESPACE_ALIAS_OBJC_CLASS(classname);
69#else
70#define QT_DECLARE_NAMESPACED_OBJC_INTERFACE(classname, definition)
71 Q_FORWARD_DECLARE_OBJC_CLASS(QT_MANGLE_NAMESPACE(classname));
72 using classname = QT_MANGLE_NAMESPACE(classname);
73#endif
74
75#define QT_FORWARD_DECLARE_OBJC_ENUM(name, type)
76 typedef type name;
77
80
81// @compatibility_alias doesn't work with categories or their methods
82#define QtExtras QT_MANGLE_NAMESPACE(QtExtras)
83
84QT_BEGIN_NAMESPACE
85template <typename T, typename U, auto RetainFunction, auto ReleaseFunction>
86class QAppleRefCounted
87{
88public:
89 Q_NODISCARD_CTOR QAppleRefCounted() : value() {}
90 Q_NODISCARD_CTOR QAppleRefCounted(const T &t) : value(t) {}
91 Q_NODISCARD_CTOR QAppleRefCounted(T &&t)
92 noexcept(std::is_nothrow_move_constructible<T>::value)
93 : value(std::move(t)) {}
94 Q_NODISCARD_CTOR QAppleRefCounted(QAppleRefCounted &&other)
95 noexcept(std::is_nothrow_move_assignable<T>::value &&
96 std::is_nothrow_move_constructible<T>::value)
97 : value(std::exchange(other.value, T())) {}
98 Q_NODISCARD_CTOR QAppleRefCounted(const QAppleRefCounted &other)
99 : value(other.value)
100 { if (value) RetainFunction(value); }
101 ~QAppleRefCounted() { if (value) ReleaseFunction(value); }
102 operator T() const { return value; }
103 void swap(QAppleRefCounted &other) noexcept(noexcept(qSwap(value, other.value)))
104 { qSwap(value, other.value); }
105 QAppleRefCounted &operator=(const QAppleRefCounted &other)
106 { QAppleRefCounted copy(other); swap(copy); return *this; }
107 QAppleRefCounted &operator=(QAppleRefCounted &&other)
108 noexcept(std::is_nothrow_move_assignable<T>::value &&
109 std::is_nothrow_move_constructible<T>::value)
110 { QAppleRefCounted moved(std::move(other)); swap(moved); return *this; }
111 T *operator&() { return &value; }
112protected:
113 T value;
114};
115
117{
118public:
119 Q_NODISCARD_CTOR Q_CORE_EXPORT QMacAutoReleasePool();
121private:
123 void *pool;
124};
125
126#ifdef Q_OS_MACOS
127class QMacRootLevelAutoReleasePool
128{
129public:
130 Q_NODISCARD_CTOR QMacRootLevelAutoReleasePool();
131 ~QMacRootLevelAutoReleasePool();
132private:
133 std::optional<QMacAutoReleasePool> pool = std::nullopt;
134};
135#endif
136
137/*
138 Helper class that automates reference counting for CFtypes.
139 After constructing the QCFType object, it can be copied like a
140 value-based type.
141
142 Note that you must own the object you are wrapping.
143 This is typically the case if you get the object from a Core
144 Foundation function with the word "Create" or "Copy" in it. If
145 you got the object from a "Get" function, either retain it or use
146 constructFromGet(). One exception to this rule is the
147 HIThemeGet*Shape functions, which in reality are "Copy" functions.
148*/
149template <typename T>
151{
153public:
154 using Base::Base;
155 Q_NODISCARD_CTOR explicit QCFType(CFTypeRef r) : Base(static_cast<T>(r)) {}
156 template <typename X> X as() const { return reinterpret_cast<X>(this->value); }
157 static QCFType constructFromGet(const T &t)
158 {
159 if (t)
160 CFRetain(t);
161 return QCFType<T>(t);
162 }
163};
164
165#ifdef Q_OS_MACOS
166template <typename T>
167class QIOType : public QAppleRefCounted<T, io_object_t, IOObjectRetain, IOObjectRelease>
168{
169 using QAppleRefCounted<T, io_object_t, IOObjectRetain, IOObjectRelease>::QAppleRefCounted;
170};
171#endif
172
186
187class Q_CORE_EXPORT QObjCWeakPointerBase
188{
189public:
190 QObjCWeakPointerBase(NSObject *object = nil);
191 QObjCWeakPointerBase(const QObjCWeakPointerBase &other);
192 QObjCWeakPointerBase &operator=(const QObjCWeakPointerBase &other);
193
194protected:
195 ~QObjCWeakPointerBase();
196 NSObject *get() const;
197 union {
198 NSObject *m_object = nil;
199#if __has_feature(objc_arc_weak) && __has_feature(objc_arc_fields)
200 // Used by qcore_mac.mm, built with -fobjc-weak, to track lifetime
201 __weak id m_weakReference;
202#endif
203 };
204};
205
206template <typename T>
208{
209public:
210 using QObjCWeakPointerBase::QObjCWeakPointerBase;
211 operator T*() const { return static_cast<T*>(get()); }
212};
213
214// -------------------------------------------------------------------------
215
216#ifdef Q_OS_MACOS
217Q_CORE_EXPORT bool qt_mac_runningUnderRosetta();
218Q_CORE_EXPORT std::optional<uint32_t> qt_mac_sipConfiguration();
219Q_CORE_EXPORT bool qt_mac_processHasEntitlement(const QString &entitlement);
220#ifdef QT_BUILD_INTERNAL
221Q_AUTOTEST_EXPORT void qt_mac_ensureResponsible();
222#endif
223#endif
224
225#ifndef QT_NO_DEBUG_STREAM
226Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QMacAutoReleasePool *pool);
227Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QCFString &string);
228#endif
229
230Q_CORE_EXPORT bool qt_apple_isApplicationExtension();
232
233#if !defined(QT_BOOTSTRAPPED)
234Q_CORE_EXPORT bool qt_apple_isSandboxed();
235
236#if defined(__OBJC__)
237QT_END_NAMESPACE
238@interface NSObject (QtExtras)
239- (id)qt_valueForPrivateKey:(NSString *)key;
240@end
241QT_BEGIN_NAMESPACE
242#endif
243#endif // !QT_BOOTSTRAPPED
244
245#if !defined(QT_BOOTSTRAPPED) && !defined(Q_OS_WATCHOS)
247# if defined(Q_OS_MACOS)
250# else
252using AppleApplication = UIApplication;
253# endif
254QT_BEGIN_NAMESPACE
255Q_CORE_EXPORT AppleApplication *qt_apple_sharedApplication();
256#endif
257
258// --------------------------------------------------------------------------
259
260#if !defined(QT_BOOTSTRAPPED)
261#define QT_USE_APPLE_UNIFIED_LOGGING
262
264#include <os/log.h>
266
267class Q_CORE_EXPORT AppleUnifiedLogger
268{
269public:
270 static bool messageHandler(QtMsgType msgType, const QMessageLogContext &context,
271 const QString &message)
272 { return messageHandler(msgType, context, message, QString()); }
273 static bool messageHandler(QtMsgType msgType, const QMessageLogContext &context,
274 const QString &message, const QString &subsystem);
275 static bool preventsStderrLogging();
276private:
277 static os_log_type_t logTypeForMessageType(QtMsgType msgType);
278 static os_log_t cachedLog(const QString &subsystem, const QString &category);
279};
280
281#endif
282
283// --------------------------------------------------------------------------
284
285#if !defined(QT_BOOTSTRAPPED) && !__has_feature(objc_arc)
286
288#include <os/activity.h>
290
291template <typename T> using QAppleOsType = QAppleRefCounted<T, void *, os_retain, os_release>;
292
293class Q_CORE_EXPORT QAppleLogActivity
294{
295public:
296 QAppleLogActivity() : activity(nullptr) {}
297 QAppleLogActivity(os_activity_t activity) : activity(activity) {}
298 ~QAppleLogActivity() { if (activity) leave(); }
299
300 Q_DISABLE_COPY(QAppleLogActivity)
301
302 QAppleLogActivity(QAppleLogActivity &&other)
303 : activity(std::exchange(other.activity, nullptr)), state(other.state)
304 {
305 }
306
307 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QAppleLogActivity)
308
309 QAppleLogActivity &&enter()
310 {
311 if (activity)
312 os_activity_scope_enter(static_cast<os_activity_t>(*this), &state);
313 return std::move(*this);
314 }
315
316 void leave()
317 {
318 if (activity)
319 os_activity_scope_leave(&state);
320 }
321
322 operator os_activity_t()
323 {
324 return reinterpret_cast<os_activity_t>(static_cast<void *>(activity));
325 }
326
327 void swap(QAppleLogActivity &other)
328 {
329 activity.swap(other.activity);
330 std::swap(state, other.state);
331 }
332
333private:
334 // Work around API_AVAILABLE not working for templates by using void*
335 QAppleOsType<void *> activity;
336 os_activity_scope_state_s state;
337};
338
339#define QT_APPLE_LOG_ACTIVITY_CREATE(condition, description, parent) []() {
340 if (!(condition))
341 return QAppleLogActivity();
342 return QAppleLogActivity(os_activity_create(description, parent, OS_ACTIVITY_FLAG_DEFAULT));
343 }()
344
345#define QT_APPLE_LOG_ACTIVITY_WITH_PARENT_3(condition, description, parent) QT_APPLE_LOG_ACTIVITY_CREATE(condition, description, parent)
346#define QT_APPLE_LOG_ACTIVITY_WITH_PARENT_2(description, parent) QT_APPLE_LOG_ACTIVITY_WITH_PARENT_3(true, description, parent)
347#define QT_APPLE_LOG_ACTIVITY_WITH_PARENT(...) QT_OVERLOADED_MACRO(QT_APPLE_LOG_ACTIVITY_WITH_PARENT, __VA_ARGS__)
348
349QT_MAC_WEAK_IMPORT(_os_activity_current);
350#define QT_APPLE_LOG_ACTIVITY_2(condition, description) QT_APPLE_LOG_ACTIVITY_CREATE(condition, description, OS_ACTIVITY_CURRENT)
351#define QT_APPLE_LOG_ACTIVITY_1(description) QT_APPLE_LOG_ACTIVITY_2(true, description)
352#define QT_APPLE_LOG_ACTIVITY(...) QT_OVERLOADED_MACRO(QT_APPLE_LOG_ACTIVITY, __VA_ARGS__)
353
354#define QT_APPLE_SCOPED_LOG_ACTIVITY(...) QAppleLogActivity scopedLogActivity = QT_APPLE_LOG_ACTIVITY(__VA_ARGS__).enter();
355
356#endif // !defined(QT_BOOTSTRAPPED) && !__has_feature(objc_arc)
357
358// -------------------------------------------------------------------------
359
360class Q_CORE_EXPORT QMacNotificationObserver
361{
362public:
363 QMacNotificationObserver() {}
364
365#if defined( __OBJC__)
366 template<typename Functor>
367 QMacNotificationObserver(NSObject *object, NSNotificationName name, Functor callback) {
368 observer = [[NSNotificationCenter defaultCenter] addObserverForName:name
369 object:object queue:nil usingBlock:^(NSNotification *notification) {
370 if constexpr (std::is_invocable_v<Functor, NSNotification *>)
371 callback(notification);
372 else
373 callback();
374 }
375 ];
376 }
377#endif
378
379 QMacNotificationObserver(const QMacNotificationObserver &other) = delete;
380 QMacNotificationObserver(QMacNotificationObserver &&other)
381 : observer(std::exchange(other.observer, nullptr))
382 {
383 }
384
385 QMacNotificationObserver &operator=(const QMacNotificationObserver &other) = delete;
386 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QMacNotificationObserver)
387
388 void swap(QMacNotificationObserver &other) noexcept
389 {
390 std::swap(observer, other.observer);
391 }
392
393 void remove();
394 ~QMacNotificationObserver() { remove(); }
395
396private:
397 NSObject *observer = nullptr;
398};
399
401QT_DECLARE_NAMESPACED_OBJC_INTERFACE(KeyValueObserver, NSObject)
403
405{
406public:
407 using Callback = std::function<void()>;
408
409 QMacKeyValueObserver() = default;
410
411#if defined( __OBJC__)
412 QMacKeyValueObserver(NSObject *object, NSString *keyPath, Callback callback,
413 NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew)
414 : object(object), keyPath(keyPath), callback(new Callback(callback))
415 {
416 addObserver(options);
417 }
418#endif
419
420 QMacKeyValueObserver(const QMacKeyValueObserver &other);
421
422 QMacKeyValueObserver(QMacKeyValueObserver &&other) noexcept { swap(other); }
423
424 ~QMacKeyValueObserver() { removeObserver(); }
425
426 QMacKeyValueObserver &operator=(const QMacKeyValueObserver &other)
427 {
428 QMacKeyValueObserver tmp(other);
429 swap(tmp);
430 return *this;
431 }
432
433 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QMacKeyValueObserver)
434
435 void removeObserver();
436
437 void swap(QMacKeyValueObserver &other) noexcept
438 {
439 std::swap(object, other.object);
440 std::swap(keyPath, other.keyPath);
441 callback.swap(other.callback);
442 }
443
444private:
445#if defined( __OBJC__)
446 void addObserver(NSKeyValueObservingOptions options);
447#endif
448
449 QObjCWeakPointer<NSObject> object;
450 NSString *keyPath = nullptr;
451 std::unique_ptr<Callback> callback;
452
453 static KeyValueObserver *observer;
454};
455
456// -------------------------------------------------------------------------
457
458class Q_CORE_EXPORT QMacVersion
459{
460public:
461 enum VersionTarget {
462 ApplicationBinary,
463 QtLibraries
464 };
465
466 static QOperatingSystemVersion buildSDK(VersionTarget target = ApplicationBinary);
467 static QOperatingSystemVersion deploymentTarget(VersionTarget target = ApplicationBinary);
468 static QOperatingSystemVersion currentRuntime();
469
470private:
471 QMacVersion() = default;
472 using VersionTuple = std::pair<QOperatingSystemVersion, QOperatingSystemVersion>;
473 static VersionTuple versionsForImage(const mach_header *machHeader);
474 static VersionTuple applicationVersion();
475 static VersionTuple libraryVersion();
476};
477
478// -------------------------------------------------------------------------
479
480#ifdef __OBJC__
481template <typename T>
482typename std::enable_if<std::is_pointer<T>::value, T>::type
483qt_objc_cast(id object)
484{
485 if ([object isKindOfClass:[typename std::remove_pointer<T>::type class]])
486 return static_cast<T>(object);
487
488 return nil;
489}
490#endif
491
492// -------------------------------------------------------------------------
493
494QT_END_NAMESPACE
495
496#endif // QCORE_MAC_P_H
static QCFType constructFromGet(const T &t)
X as() const
Q_CORE_EXPORT ~QMacAutoReleasePool()
Definition qcore_mac.mm:306
Combined button and popup list for selecting options.
const QString & asString(const QString &s)
Definition qstring.h:1678
#define __has_feature(x)
#define QT_APPLE_LOG_ACTIVITY_2(condition, description)
Q_CORE_EXPORT bool qt_apple_isApplicationExtension()
Definition qcore_mac.mm:464
#define QT_APPLE_LOG_ACTIVITY(...)
#define QtExtras
Definition qcore_mac_p.h:82
#define QT_MAC_WEAK_IMPORT(symbol)
Definition qcore_mac_p.h:61
#define QT_APPLE_LOG_ACTIVITY_WITH_PARENT_3(condition, description, parent)
Q_FORWARD_DECLARE_OBJC_CLASS(NSString)
Q_CORE_EXPORT bool qt_apple_isSandboxed()
Definition qcore_mac.mm:494
Q_CORE_EXPORT bool qt_apple_runningWithLiquidGlass()
#define QT_APPLE_LOG_ACTIVITY_CREATE(condition, description, parent)
#define QT_APPLE_LOG_ACTIVITY_WITH_PARENT(...)
#define QT_DECLARE_NAMESPACED_OBJC_INTERFACE(classname, definition)
Definition qcore_mac_p.h:70
Q_FORWARD_DECLARE_OBJC_CLASS(NSObject)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2568
Q_FORWARD_DECLARE_OBJC_CLASS(QT_MANGLE_NAMESPACE(RunLoopModeTracker))
QT_REQUIRE_CONFIG(filesystemwatcher)
static QT_BEGIN_NAMESPACE void callBackFunction(ConstFSEventStreamRef streamRef, void *clientCallBackInfo, size_t numEvents, void *eventPaths, const FSEventStreamEventFlags eventFlags[], const FSEventStreamEventId eventIds[])
#define qPrintable(string)
Definition qstring.h:1683