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
qcgcapturablewindows.mm
Go to the documentation of this file.
1// Copyright (C) 2023 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#include <QtFFmpegMediaPluginImpl/private/qcgcapturablewindows_p.h>
5
6#include <QtCore/private/qcore_mac_p.h>
7
8#include <QtGui/qwindow.h>
9
10#include <QtMultimedia/private/qcapturablewindow_p.h>
11
12#import <AppKit/NSWindow.h>
13
14QT_BEGIN_NAMESPACE
15
16QList<QCapturableWindow> QCGCapturableWindows::windows() const
17{
18 QList<QCapturableWindow> result;
19 QCFType<CFArrayRef> windowList(
20 CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID));
21
22 // Iterate through the window dictionaries
23 CFIndex count = CFArrayGetCount(windowList);
24 for (CFIndex i = 0; i < count; ++i) {
25 auto windowInfo = (CFDictionaryRef)CFArrayGetValueAtIndex(windowList, i);
26 auto windowNumber = (CFNumberRef)CFDictionaryGetValue(windowInfo, kCGWindowNumber);
27 auto windowName = (CFStringRef)CFDictionaryGetValue(windowInfo, kCGWindowName);
28
29 CGWindowID windowId = 0;
30 static_assert(sizeof(windowId) == 4,
31 "CGWindowID size is not compatible with kCFNumberSInt32Type");
32 CFNumberGetValue(windowNumber, kCFNumberSInt32Type, &windowId);
33
34 QString windowDescription;
35 if (windowName)
36 windowDescription = QString::fromCFString(windowName);
37
38 result.push_back(QCapturableWindowPrivate::create(
39 static_cast<QCapturableWindowPrivate::Id>(windowId),
40 std::move(windowDescription)));
41 }
42
43 return result;
44}
45
46bool QCGCapturableWindows::isWindowValid(const QCapturableWindowPrivate &window) const
47{
48 QCFType<CFArrayRef> windowList(
49 CGWindowListCreate(kCGWindowListOptionIncludingWindow, window.id));
50 return CFArrayGetCount(windowList) > 0;
51}
52
53q23::expected<QCapturableWindow, QString> QCGCapturableWindows::fromQWindow(QWindow *window) const
54{
55 auto* nsView = reinterpret_cast<NSView*>(window->winId());
56
57 NSWindow* nsWindow = [nsView window];
58 if (nsWindow == nullptr)
59 return q23::unexpected{ QStringLiteral("NSView had no associated NSWindow") };
60
61 const auto cgWindowId = (CGWindowID)[nsWindow windowNumber];
62 if (cgWindowId == kCGNullWindowID)
63 return q23::unexpected{ QStringLiteral("NSWindow has no CGWindowID") };
64
65 return QCapturableWindowPrivate::create(
66 static_cast<QCapturableWindowPrivate::Id>(cgWindowId),
67 window->title());
68}
69
70QT_END_NAMESPACE