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