Compare commits

...

33 Commits

Author SHA1 Message Date
kleuter
ff167a036d v6.8.1 2025-03-21 13:11:05 +01:00
kleuter
b149cc3eb2 one more change from Qt 6.8.2 2025-03-17 18:15:19 +01:00
kleuter
97ed722167 update to Qt 6.8.2 2025-03-17 18:04:27 +01:00
kleuter
15d5fb5382 turn off directwrite fonts on Windows prior to Windows 10 (issue https://github.com/crystalidea/qt6windows7/issues/33) 2025-03-17 17:22:31 +01:00
Kleuter
e92ba4dce4
Merge pull request #28 from wsxarcher/patch-1
Fix build for non-windows
2025-01-28 16:19:22 +01:00
Kleuter
50a1a7f6e9
Update README.md 2025-01-21 18:10:27 +01:00
kleuter
f56de58895 fixed #26 2025-01-21 17:27:49 +01:00
kleuter
b371361f55 qwindowsintegration.cpp original 2025-01-21 12:02:54 +01:00
Kleuter
aabe4f68c7
Merge pull request #29 from wsxarcher/patch-2
Fix mingw cross compilation on linux
2024-12-28 10:03:34 +01:00
Kleuter
263451546b
Merge pull request #30 from wsxarcher/patch-3
Fix gcc (mingw) compilation of pointer comparison in switch using ifs
2024-12-28 09:59:23 +01:00
wsxarcher
04ec1a740d fix gcc compilation of pointer comparison 2024-12-27 23:53:49 +01:00
Marco Bartoli
5a8a83f569 Fix mingw cross compilation 2024-12-16 02:02:54 +01:00
Marco Bartoli
d6525e7907
Fix build on non-windows 2024-12-15 23:00:30 +01:00
Kleuter
45d7e6576d
Update README.md 2024-12-13 15:50:31 +01:00
kleuter
8c8f9a4e87 Qt 6.8.1 2024-12-13 13:51:13 +01:00
Kleuter
a9966ab3ae
Update README.md 2024-11-21 14:34:17 +01:00
kleuter
4f4f7a7c46 fixed compilation when using non-microsoft compiler 2024-11-19 13:57:04 +01:00
Kleuter
6ba197bce4
Update README.md 2024-11-18 13:56:33 +01:00
Kleuter
b4475b8788
Update README.md 2024-11-18 11:47:41 +01:00
Kleuter
aa57feb61a
Update README.md 2024-11-18 11:38:58 +01:00
kleuter
09a199127f qt 6.8.0 support 2024-11-18 11:31:57 +01:00
Kleuter
c4707639b4
Update README.md 2024-11-12 15:59:00 +01:00
kleuter
b9947d93c0 udpate to qt 6.7.3 2024-11-12 15:55:53 +01:00
Kleuter
61d77a3278
Update README.md 2024-11-12 11:41:22 +01:00
kleuter
d759484c54 Update README.md 2024-09-19 14:50:42 +02:00
kleuter
68c7686c41 lf line endings 2024-07-01 12:48:35 +02:00
Kleuter
8fa19ffd8e
Update README.md 2024-06-30 14:01:00 +02:00
Kleuter
cb886bb693
Update README.md 2024-06-29 18:05:41 +02:00
Kleuter
69ab09a75c
Update README.md 2024-06-29 14:41:19 +02:00
kleuter
a88b5f1fbf Merge branch 'master' of https://github.com/crystalidea/qt6windows7 2024-06-29 14:34:12 +02:00
kleuter
36dc9b47f0 update to Qt 6.7.3 2024-06-29 14:33:57 +02:00
Kleuter
7669b4f494
Update README.md 2024-06-29 14:33:28 +02:00
kleuter
d8e4680eb6 Update README.md 2024-06-24 12:20:36 +02:00
29 changed files with 7498 additions and 1251 deletions

11
.editorconfig Normal file
View File

@ -0,0 +1,11 @@
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
charset = utf-8
indent_style = space
indent_size = 4

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
* text=auto

View File

@ -1,13 +1,38 @@
This is Qt 6.6.3 backport that runs on Windows 7 (what?). The repository contains patched source files from the qtbase module.
Approach is based on this [forum thread](https://forum.qt.io/topic/133002/qt-creator-6-0-1-and-qt-6-2-2-running-on-windows-7/60) but better: many improvements amongst important fallbacks to default Qt 6 behaviour when running on newer Windows.
You can use [our prebuild binaries](https://github.com/crystalidea/qt6windows7/releases) (we used Visual C++ 2019 with OpenSSL 3.0.13 statically linked, see [compile_win.pl](https://github.com/crystalidea/qt-build-tools/tree/master/6.6.2) script) or compile Qt yourself.
Known issues:
- QRhi using DirectX 11/12 is not ported
- setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::Round) is recommended
Qt 6.6 designer running on Windows 7:
![sshot-533](https://github.com/crystalidea/qt6windows7/assets/2600624/86b1763a-13d3-4b93-b4a9-654c70838ef2)
This repository provides a backport of the Qt 6.8.2 qtbase module, tailored for compatibility with Windows 7, 8 and 8.1. It contains patched source files from the qtbase module, along with some additional required files. To apply the backport, simply copy the contents of the src folder into your qtbase/src directory, replacing the existing files.
This approach builds upon the methodology discussed in this forum [thread](https://forum.qt.io/topic/133002/qt-creator-6-0-1-and-qt-6-2-2-running-on-windows-7/60) but offers significant enhancements, including important fallbacks to the default Qt 6 behavior when running on newer versions of Windows.
You can compile it yourself using your preferred compiler and build options or can use our [compile_win.pl](https://github.com/crystalidea/qt-build-tools/tree/master/6.8.1) build script, which utilizes Visual C++ 2022 and includes OpenSSL 3.0.13 statically linked. Alternatively, you can download our [prebuild Qt dlls](https://github.com/crystalidea/qt6windows7/releases), which also include the Qt Designer binary for demonstration purposes.
**Qt 6.8.2 designer running on Windows 7**:
![Qt Designer](designer.png)
**Other modules**:
Many of other Qt 6 modules are known to work fine on Windows 7 without modifications when compiled with patched qtbase. Verified modules:
- qt5compat
- qtimageformats
- qttools
- ... please let me know which work and which don't !
### Known issues:
- QRhi using DirectX 11/12 is not ported
### Older versions:
- [Qt 6.8.1](https://github.com/crystalidea/qt6windows7/releases/tag/v6.8.1)
- [Qt 6.8.0](https://github.com/crystalidea/qt6windows7/releases/tag/v6.8.0)
- [Qt 6.7.2](https://github.com/crystalidea/qt6windows7/releases/tag/v6.7.2)
- [Qt 6.6.3](https://github.com/crystalidea/qt6windows7/releases/tag/v6.6.3)
- [Qt 6.6.2](https://github.com/crystalidea/qt6windows7/releases/tag/v6.6.2)
- [Qt 6.6.1](https://github.com/crystalidea/qt6windows7/releases/tag/v6.6.1)
- [Qt 6.6.0](https://github.com/crystalidea/qt6windows7/releases/tag/v6.6.0)
- [Qt 6.5.3](https://github.com/crystalidea/qt6windows7/releases/tag/6.5.3-win7)
- [Qt 6.5.1](https://github.com/crystalidea/qt6windows7/releases/tag/6.5.1-win7)
### License
The repository shares Qt Community Edition terms which imply [Open-Source terms and conditions (GPL and LGPL)](https://www.qt.io/licensing/open-source-lgpl-obligations?hsLang=en).

BIN
designer.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

View File

@ -12,7 +12,7 @@
#include <qt_windows.h>
#include <shlobj.h>
#include <VersionHelpers.h>
#include <versionhelpers.h>
#include <intshcut.h>
#include <qvarlengtharray.h>
@ -112,8 +112,10 @@ static GUID writableSpecialFolderId(QStandardPaths::StandardLocation type)
FOLDERID_LocalAppData, // AppConfigLocation ("Local" path)
FOLDERID_Public, // PublicShareLocation
FOLDERID_Templates, // TemplatesLocation
GUID(), // StateLocation
GUID(), // GenericStateLocation
};
static_assert(sizeof(folderIds) / sizeof(folderIds[0]) == size_t(QStandardPaths::TemplatesLocation + 1));
static_assert(sizeof(folderIds) / sizeof(folderIds[0]) == size_t(QStandardPaths::GenericStateLocation + 1));
// folders for low integrity processes
static const GUID folderIds_li[] = {
@ -137,6 +139,8 @@ static GUID writableSpecialFolderId(QStandardPaths::StandardLocation type)
FOLDERID_LocalAppDataLow,// AppConfigLocation ("Local" path)
FOLDERID_Public, // PublicShareLocation
FOLDERID_Templates, // TemplatesLocation
GUID(), // StateLocation
GUID(), // GenericStateLocation
};
static_assert(sizeof(folderIds_li) == sizeof(folderIds));
@ -191,6 +195,23 @@ QString QStandardPaths::writableLocation(StandardLocation type)
result = QDir::tempPath();
break;
case StateLocation:
result = sHGetKnownFolderPath(writableSpecialFolderId(AppLocalDataLocation));
if (!result.isEmpty()) {
appendTestMode(result);
appendOrganizationAndApp(result);
result += "/State"_L1;
}
break;
case GenericStateLocation:
result = sHGetKnownFolderPath(writableSpecialFolderId(GenericDataLocation));
if (!result.isEmpty()) {
appendTestMode(result);
result += "/State"_L1;
}
break;
default:
result = sHGetKnownFolderPath(writableSpecialFolderId(type));
if (!result.isEmpty() && isConfigLocation(type)) {

View File

@ -7,7 +7,6 @@
#include "qcoreapplication.h"
#include <private/qsystemlibrary_p.h>
#include "qoperatingsystemversion.h"
#include "qpair.h"
#include "qset.h"
#include "qsocketnotifier.h"
#include "qvarlengtharray.h"
@ -136,7 +135,7 @@ LRESULT QT_WIN_CALLBACK qt_internal_proc(HWND hwnd, UINT message, WPARAM wp, LPA
QSNDict *sn_vec[4] = { &d->sn_read, &d->sn_write, &d->sn_except, &d->sn_read };
QSNDict *dict = sn_vec[type];
QSockNot *sn = dict ? dict->value(wp) : 0;
QSockNot *sn = dict ? dict->value(qintptr(wp)) : 0;
if (sn == nullptr) {
d->postActivateSocketNotifiers();
} else {
@ -414,7 +413,7 @@ void QEventDispatcherWin32Private::sendTimerEvent(int timerId)
}
}
void QEventDispatcherWin32Private::doWsaAsyncSelect(int socket, long event)
void QEventDispatcherWin32Private::doWsaAsyncSelect(qintptr socket, long event)
{
Q_ASSERT(internalHwnd);
// BoundsChecker may emit a warning for WSAAsyncSelect when event == 0
@ -563,7 +562,7 @@ bool QEventDispatcherWin32::processEvents(QEventLoop::ProcessEventsFlags flags)
void QEventDispatcherWin32::registerSocketNotifier(QSocketNotifier *notifier)
{
Q_ASSERT(notifier);
int sockfd = notifier->socket();
qintptr sockfd = notifier->socket();
int type = notifier->type();
#ifndef QT_NO_DEBUG
if (sockfd < 0) {
@ -587,7 +586,7 @@ void QEventDispatcherWin32::registerSocketNotifier(QSocketNotifier *notifier)
const char *t[] = { "Read", "Write", "Exception" };
/* Variable "socket" below is a function pointer. */
qWarning("QSocketNotifier: Multiple socket notifiers for "
"same socket %d and type %s", sockfd, t[type]);
"same socket %" PRIdQINTPTR " and type %s", sockfd, t[type]);
}
QSockNot *sn = new QSockNot;
@ -631,7 +630,7 @@ void QEventDispatcherWin32::unregisterSocketNotifier(QSocketNotifier *notifier)
{
Q_ASSERT(notifier);
#ifndef QT_NO_DEBUG
int sockfd = notifier->socket();
qintptr sockfd = notifier->socket();
if (sockfd < 0) {
qWarning("QEventDispatcherWin32::unregisterSocketNotifier: invalid socket identifier");
return;
@ -648,7 +647,7 @@ void QEventDispatcherWin32::doUnregisterSocketNotifier(QSocketNotifier *notifier
{
Q_D(QEventDispatcherWin32);
int type = notifier->type();
int sockfd = notifier->socket();
qintptr sockfd = notifier->socket();
Q_ASSERT(sockfd >= 0);
QSFDict::iterator it = d->active_fd.find(sockfd);
@ -911,3 +910,5 @@ HWND QEventDispatcherWin32::internalHwnd()
}
QT_END_NAMESPACE
#include "moc_qeventdispatcher_win_p.cpp"

View File

@ -28,6 +28,7 @@ QComHelper::QComHelper(COINIT concurrencyModel)
QComHelper::~QComHelper()
{
Q_ASSERT(m_threadId == GetCurrentThreadId());
if (SUCCEEDED(m_initResult))
CoUninitialize();
}
@ -51,21 +52,21 @@ bool qt_win_hasPackageIdentity()
{
#if defined(HAS_APPMODEL)
static const bool hasPackageIdentity = []() {
UINT32 length = 0;
switch (const auto result = myGetCurrentPackageFullName(&length, nullptr)) {
case ERROR_INSUFFICIENT_BUFFER:
return true;
case APPMODEL_ERROR_NO_PACKAGE:
return false;
default:
qWarning("Failed to resolve package identity (error code %ld)", result);
return false;
}
}();
return hasPackageIdentity;
static const bool hasPackageIdentity = []() {
UINT32 length = 0;
switch (const auto result = myGetCurrentPackageFullName(&length, nullptr)) {
case ERROR_INSUFFICIENT_BUFFER:
return true;
case APPMODEL_ERROR_NO_PACKAGE:
return false;
default:
qWarning("Failed to resolve package identity (error code %ld)", result);
return false;
}
}();
return hasPackageIdentity;
#else
return false;
return false;
#endif
}

View File

@ -15,15 +15,15 @@
// We mean it.
//
#include <qdeadlinetimer.h>
#include <private/qglobal_p.h>
#include <QtCore/qtsan_impl.h>
QT_BEGIN_NAMESPACE
namespace QtDummyFutex {
constexpr inline bool futexAvailable() { return false; }
template <typename Atomic>
inline bool futexWait(Atomic &, typename Atomic::Type, int = 0)
inline bool futexWait(Atomic &, typename Atomic::Type, QDeadlineTimer = {})
{ Q_UNREACHABLE_RETURN(false); }
template <typename Atomic> inline void futexWakeOne(Atomic &)
{ Q_UNREACHABLE(); }
@ -33,82 +33,16 @@ namespace QtDummyFutex {
QT_END_NAMESPACE
#if defined(Q_OS_LINUX) && !defined(QT_LINUXBASE)
#if defined(Q_OS_DARWIN)
# include "qfutex_mac_p.h"
#elif defined(Q_OS_FREEBSD)
# include "qfutex_freebsd_p.h"
#elif defined(Q_OS_LINUX) && !defined(QT_LINUXBASE)
// use Linux mutexes everywhere except for LSB builds
# include <sys/syscall.h>
# include <errno.h>
# include <limits.h>
# include <unistd.h>
# include <asm/unistd.h>
# include <linux/futex.h>
# define QT_ALWAYS_USE_FUTEX
// if not defined in linux/futex.h
# define FUTEX_PRIVATE_FLAG 128 // added in v2.6.22
// RISC-V does not supply __NR_futex
# ifndef __NR_futex
# define __NR_futex __NR_futex_time64
# endif
QT_BEGIN_NAMESPACE
namespace QtLinuxFutex {
constexpr inline bool futexAvailable() { return true; }
inline int _q_futex(int *addr, int op, int val, quintptr val2 = 0,
int *addr2 = nullptr, int val3 = 0) noexcept
{
QtTsan::futexRelease(addr, addr2);
// we use __NR_futex because some libcs (like Android's bionic) don't
// provide SYS_futex etc.
int result = syscall(__NR_futex, addr, op | FUTEX_PRIVATE_FLAG, val, val2, addr2, val3);
QtTsan::futexAcquire(addr, addr2);
return result;
}
template <typename T> int *addr(T *ptr)
{
int *int_addr = reinterpret_cast<int *>(ptr);
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
if (sizeof(T) > sizeof(int))
int_addr++; //We want a pointer to the least significant half
#endif
return int_addr;
}
template <typename Atomic>
inline void futexWait(Atomic &futex, typename Atomic::Type expectedValue)
{
_q_futex(addr(&futex), FUTEX_WAIT, qintptr(expectedValue));
}
template <typename Atomic>
inline bool futexWait(Atomic &futex, typename Atomic::Type expectedValue, qint64 nstimeout)
{
struct timespec ts;
ts.tv_sec = nstimeout / 1000 / 1000 / 1000;
ts.tv_nsec = nstimeout % (1000 * 1000 * 1000);
int r = _q_futex(addr(&futex), FUTEX_WAIT, qintptr(expectedValue), quintptr(&ts));
return r == 0 || errno != ETIMEDOUT;
}
template <typename Atomic> inline void futexWakeOne(Atomic &futex)
{
_q_futex(addr(&futex), FUTEX_WAKE, 1);
}
template <typename Atomic> inline void futexWakeAll(Atomic &futex)
{
_q_futex(addr(&futex), FUTEX_WAKE, INT_MAX);
}
template <typename Atomic> inline
void futexWakeOp(Atomic &futex1, int wake1, int wake2, Atomic &futex2, quint32 op)
{
_q_futex(addr(&futex1), FUTEX_WAKE_OP, wake1, wake2, addr(&futex2), op);
}
}
namespace QtFutex = QtLinuxFutex;
QT_END_NAMESPACE
# include "qfutex_linux_p.h"
//#elif defined(Q_OS_WIN)
//# include "qfutex_win_p.h"
#else
QT_BEGIN_NAMESPACE
namespace QtFutex = QtDummyFutex;
QT_END_NAMESPACE

View File

@ -8,7 +8,6 @@
#include "qmutex.h"
#include <qdebug.h>
#include "qatomic.h"
#include "qelapsedtimer.h"
#include "qfutex_p.h"
#include "qthread.h"
#include "qmutex_p.h"
@ -638,6 +637,7 @@ void QRecursiveMutex::unlock() noexcept
/*!
\internal helper for lock()
*/
Q_NEVER_INLINE
void QBasicMutex::lockInternal() QT_MUTEX_LOCK_NOEXCEPT
{
if (futexAvailable()) {
@ -671,14 +671,14 @@ bool QBasicMutex::lockInternal(int timeout) QT_MUTEX_LOCK_NOEXCEPT
/*!
\internal helper for tryLock(QDeadlineTimer)
*/
Q_NEVER_INLINE
bool QBasicMutex::lockInternal(QDeadlineTimer deadlineTimer) QT_MUTEX_LOCK_NOEXCEPT
{
qint64 remainingTime = deadlineTimer.remainingTimeNSecs();
if (remainingTime == 0)
if (deadlineTimer.hasExpired())
return false;
if (futexAvailable()) {
if (Q_UNLIKELY(remainingTime < 0)) { // deadlineTimer.isForever()
if (Q_UNLIKELY(deadlineTimer.isForever())) {
lockInternal();
return true;
}
@ -689,8 +689,8 @@ bool QBasicMutex::lockInternal(QDeadlineTimer deadlineTimer) QT_MUTEX_LOCK_NOEXC
if (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) == nullptr)
return true;
Q_FOREVER {
if (!futexWait(d_ptr, dummyFutexValue(), remainingTime))
for (;;) {
if (!futexWait(d_ptr, dummyFutexValue(), deadlineTimer))
return false;
// We got woken up, so must try to acquire the mutex. We must set
@ -699,9 +699,7 @@ bool QBasicMutex::lockInternal(QDeadlineTimer deadlineTimer) QT_MUTEX_LOCK_NOEXC
if (d_ptr.fetchAndStoreAcquire(dummyFutexValue()) == nullptr)
return true;
// calculate the remaining time
remainingTime = deadlineTimer.remainingTimeNSecs();
if (remainingTime <= 0)
if (deadlineTimer.hasExpired())
return false;
}
}
@ -713,7 +711,7 @@ bool QBasicMutex::lockInternal(QDeadlineTimer deadlineTimer) QT_MUTEX_LOCK_NOEXC
continue;
if (copy == dummyLocked()) {
if (remainingTime == 0)
if (deadlineTimer.hasExpired())
return false;
// The mutex is locked but does not have a QMutexPrivate yet.
// we need to allocate a QMutexPrivate
@ -728,7 +726,7 @@ bool QBasicMutex::lockInternal(QDeadlineTimer deadlineTimer) QT_MUTEX_LOCK_NOEXC
}
QMutexPrivate *d = static_cast<QMutexPrivate *>(copy);
if (remainingTime == 0 && !d->possiblyUnlocked.loadRelaxed())
if (deadlineTimer.hasExpired() && !d->possiblyUnlocked.loadRelaxed())
return false;
// At this point we have a pointer to a QMutexPrivate. But the other thread
@ -790,7 +788,6 @@ bool QBasicMutex::lockInternal(QDeadlineTimer deadlineTimer) QT_MUTEX_LOCK_NOEXC
Q_ASSERT(d == d_ptr.loadRelaxed());
return true;
} else {
Q_ASSERT(remainingTime >= 0);
// timed out
d->derefWaiters(1);
//There may be a race in which the mutex is unlocked right after we timed out,
@ -814,6 +811,7 @@ bool QBasicMutex::lockInternal(QDeadlineTimer deadlineTimer) QT_MUTEX_LOCK_NOEXC
/*!
\internal
*/
Q_NEVER_INLINE
void QBasicMutex::unlockInternal() noexcept
{
QMutexPrivate *copy = d_ptr.loadAcquire();
@ -915,10 +913,10 @@ QT_END_NAMESPACE
#if defined(QT_ALWAYS_USE_FUTEX)
// nothing
#elif defined(Q_OS_MAC)
#elif defined(Q_OS_DARWIN)
# include "qmutex_mac.cpp"
#elif defined(Q_OS_WIN)
# include "qmutex_win.cpp"
#else
# include "qmutex_unix.cpp"
#endif
#endif

View File

@ -0,0 +1,658 @@
// 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
#include "qthread.h"
#include "qthread_p.h"
#include "qthreadstorage.h"
#include "qmutex.h"
#include <qcoreapplication.h>
#include <qpointer.h>
#include <private/qcoreapplication_p.h>
#include <private/qeventdispatcher_win_p.h>
#include "qloggingcategory.h"
#include <qt_windows.h>
#ifndef _MT
# define _MT
#endif // _MT
#include <process.h>
extern "C" {
// MinGW is missing the declaration of SetThreadDescription:
WINBASEAPI
HRESULT
WINAPI
SetThreadDescription(
_In_ HANDLE hThread,
_In_ PCWSTR lpThreadDescription
);
}
QT_BEGIN_NAMESPACE
#if QT_CONFIG(thread)
void qt_watch_adopted_thread(const HANDLE adoptedThreadHandle, QThread *qthread);
DWORD WINAPI qt_adopted_thread_watcher_function(LPVOID);
static DWORD qt_current_thread_data_tls_index = TLS_OUT_OF_INDEXES;
void qt_create_tls()
{
if (qt_current_thread_data_tls_index != TLS_OUT_OF_INDEXES)
return;
Q_CONSTINIT static QBasicMutex mutex;
QMutexLocker locker(&mutex);
if (qt_current_thread_data_tls_index != TLS_OUT_OF_INDEXES)
return;
qt_current_thread_data_tls_index = TlsAlloc();
}
static void qt_free_tls()
{
if (qt_current_thread_data_tls_index != TLS_OUT_OF_INDEXES) {
TlsFree(qt_current_thread_data_tls_index);
qt_current_thread_data_tls_index = TLS_OUT_OF_INDEXES;
}
}
Q_DESTRUCTOR_FUNCTION(qt_free_tls)
/*
QThreadData
*/
void QThreadData::clearCurrentThreadData()
{
TlsSetValue(qt_current_thread_data_tls_index, 0);
}
QThreadData *QThreadData::current(bool createIfNecessary)
{
qt_create_tls();
QThreadData *threadData = reinterpret_cast<QThreadData *>(TlsGetValue(qt_current_thread_data_tls_index));
if (!threadData && createIfNecessary) {
threadData = new QThreadData;
// This needs to be called prior to new AdoptedThread() to
// avoid recursion.
TlsSetValue(qt_current_thread_data_tls_index, threadData);
QT_TRY {
threadData->thread.storeRelease(new QAdoptedThread(threadData));
} QT_CATCH(...) {
TlsSetValue(qt_current_thread_data_tls_index, 0);
threadData->deref();
threadData = 0;
QT_RETHROW;
}
threadData->deref();
threadData->isAdopted = true;
threadData->threadId.storeRelaxed(reinterpret_cast<Qt::HANDLE>(quintptr(GetCurrentThreadId())));
if (!QCoreApplicationPrivate::theMainThreadId) {
auto *mainThread = threadData->thread.loadRelaxed();
mainThread->setObjectName("Qt mainThread");
QCoreApplicationPrivate::theMainThread.storeRelease(mainThread);
QCoreApplicationPrivate::theMainThreadId.storeRelaxed(threadData->threadId.loadRelaxed());
} else {
HANDLE realHandle = INVALID_HANDLE_VALUE;
DuplicateHandle(GetCurrentProcess(),
GetCurrentThread(),
GetCurrentProcess(),
&realHandle,
0,
FALSE,
DUPLICATE_SAME_ACCESS);
qt_watch_adopted_thread(realHandle, threadData->thread.loadRelaxed());
}
}
return threadData;
}
void QAdoptedThread::init()
{
d_func()->handle = GetCurrentThread();
d_func()->id = GetCurrentThreadId();
}
static QList<HANDLE> qt_adopted_thread_handles;
static QList<QThread *> qt_adopted_qthreads;
Q_CONSTINIT static QBasicMutex qt_adopted_thread_watcher_mutex;
static DWORD qt_adopted_thread_watcher_id = 0;
static HANDLE qt_adopted_thread_wakeup = 0;
/*!
\internal
Adds an adopted thread to the list of threads that Qt watches to make sure
the thread data is properly cleaned up. This function starts the watcher
thread if necessary.
*/
void qt_watch_adopted_thread(const HANDLE adoptedThreadHandle, QThread *qthread)
{
QMutexLocker lock(&qt_adopted_thread_watcher_mutex);
if (GetCurrentThreadId() == qt_adopted_thread_watcher_id) {
CloseHandle(adoptedThreadHandle);
return;
}
qt_adopted_thread_handles.append(adoptedThreadHandle);
qt_adopted_qthreads.append(qthread);
// Start watcher thread if it is not already running.
if (qt_adopted_thread_watcher_id == 0) {
if (qt_adopted_thread_wakeup == 0) {
qt_adopted_thread_wakeup = CreateEvent(0, false, false, 0);
qt_adopted_thread_handles.prepend(qt_adopted_thread_wakeup);
}
CloseHandle(CreateThread(0, 0, qt_adopted_thread_watcher_function, 0, 0, &qt_adopted_thread_watcher_id));
} else {
SetEvent(qt_adopted_thread_wakeup);
}
}
/*
This function loops and waits for native adopted threads to finish.
When this happens it derefs the QThreadData for the adopted thread
to make sure it gets cleaned up properly.
*/
DWORD WINAPI qt_adopted_thread_watcher_function(LPVOID)
{
forever {
qt_adopted_thread_watcher_mutex.lock();
if (qt_adopted_thread_handles.count() == 1) {
qt_adopted_thread_watcher_id = 0;
qt_adopted_thread_watcher_mutex.unlock();
break;
}
QList<HANDLE> handlesCopy = qt_adopted_thread_handles;
qt_adopted_thread_watcher_mutex.unlock();
DWORD ret = WAIT_TIMEOUT;
int count;
int offset;
int loops = handlesCopy.size() / MAXIMUM_WAIT_OBJECTS;
if (handlesCopy.size() % MAXIMUM_WAIT_OBJECTS)
++loops;
if (loops == 1) {
// no need to loop, no timeout
offset = 0;
count = handlesCopy.count();
ret = WaitForMultipleObjects(handlesCopy.count(), handlesCopy.constData(), false, INFINITE);
} else {
int loop = 0;
do {
offset = loop * MAXIMUM_WAIT_OBJECTS;
count = qMin(handlesCopy.count() - offset, MAXIMUM_WAIT_OBJECTS);
ret = WaitForMultipleObjects(count, handlesCopy.constData() + offset, false, 100);
loop = (loop + 1) % loops;
} while (ret == WAIT_TIMEOUT);
}
if (ret == WAIT_FAILED || ret >= WAIT_OBJECT_0 + uint(count)) {
qWarning("QThread internal error while waiting for adopted threads: %d", int(GetLastError()));
continue;
}
const int handleIndex = offset + ret - WAIT_OBJECT_0;
if (handleIndex == 0) // New handle to watch was added.
continue;
const int qthreadIndex = handleIndex - 1;
qt_adopted_thread_watcher_mutex.lock();
QThreadData *data = QThreadData::get2(qt_adopted_qthreads.at(qthreadIndex));
qt_adopted_thread_watcher_mutex.unlock();
if (data->isAdopted) {
QThread *thread = data->thread;
Q_ASSERT(thread);
auto thread_p = static_cast<QThreadPrivate *>(QObjectPrivate::get(thread));
Q_UNUSED(thread_p);
Q_ASSERT(!thread_p->finished);
thread_p->finish();
}
data->deref();
QMutexLocker lock(&qt_adopted_thread_watcher_mutex);
CloseHandle(qt_adopted_thread_handles.at(handleIndex));
qt_adopted_thread_handles.remove(handleIndex);
qt_adopted_qthreads.remove(qthreadIndex);
}
QThreadData *threadData = reinterpret_cast<QThreadData *>(TlsGetValue(qt_current_thread_data_tls_index));
if (threadData)
threadData->deref();
return 0;
}
#ifndef Q_OS_WIN64
# define ULONG_PTR DWORD
#endif
typedef struct tagTHREADNAME_INFO
{
DWORD dwType; // must be 0x1000
LPCSTR szName; // pointer to name (in user addr space)
HANDLE dwThreadID; // thread ID (-1=caller thread)
DWORD dwFlags; // reserved for future use, must be zero
} THREADNAME_INFO;
typedef HRESULT(WINAPI* SetThreadDescriptionFunc)(HANDLE, PCWSTR);
#if defined(Q_CC_MSVC)
// Helper function to set the thread name using RaiseException and __try
static void setThreadNameUsingException(HANDLE threadId, LPCSTR threadName) {
THREADNAME_INFO info;
info.dwType = 0x1000;
info.szName = threadName;
info.dwThreadID = threadId;
info.dwFlags = 0;
__try {
RaiseException(0x406D1388, 0, sizeof(info) / sizeof(DWORD),
reinterpret_cast<const ULONG_PTR*>(&info));
}
__except (EXCEPTION_CONTINUE_EXECUTION) {
}
}
#endif // Q_CC_MSVC
void qt_set_thread_name(HANDLE threadId, const QString &name)
{
HMODULE hKernel32 = GetModuleHandleW(L"Kernel32.dll");
SetThreadDescriptionFunc pSetThreadDescription =
reinterpret_cast<SetThreadDescriptionFunc>(GetProcAddress(hKernel32, "SetThreadDescription"));
if (pSetThreadDescription) {
pSetThreadDescription(threadId, reinterpret_cast<const wchar_t *>(name.utf16()) );
}
else {
#if defined(Q_CC_MSVC)
std::string stdStr = name.toStdString();
LPCSTR threadName = stdStr.c_str();
setThreadNameUsingException(threadId, threadName);
#endif // Q_CC_MSVC
}
}
/**************************************************************************
** QThreadPrivate
*************************************************************************/
#endif // QT_CONFIG(thread)
QAbstractEventDispatcher *QThreadPrivate::createEventDispatcher(QThreadData *data)
{
Q_UNUSED(data);
return new QEventDispatcherWin32;
}
#if QT_CONFIG(thread)
unsigned int __stdcall QT_ENSURE_STACK_ALIGNED_FOR_SSE QThreadPrivate::start(void *arg) noexcept
{
QThread *thr = reinterpret_cast<QThread *>(arg);
QThreadData *data = QThreadData::get2(thr);
qt_create_tls();
TlsSetValue(qt_current_thread_data_tls_index, data);
data->threadId.storeRelaxed(reinterpret_cast<Qt::HANDLE>(quintptr(GetCurrentThreadId())));
QThread::setTerminationEnabled(false);
{
QMutexLocker locker(&thr->d_func()->mutex);
data->quitNow = thr->d_func()->exited;
}
data->ensureEventDispatcher();
data->eventDispatcher.loadRelaxed()->startingUp();
// sets the name of the current thread.
QString threadName = std::exchange(thr->d_func()->objectName, {});
if (Q_LIKELY(threadName.isEmpty()))
threadName = QString::fromUtf8(thr->metaObject()->className());
qt_set_thread_name(GetCurrentThread(), threadName);
emit thr->started(QThread::QPrivateSignal());
QThread::setTerminationEnabled(true);
thr->run();
thr->d_func()->finish();
return 0;
}
/*
For regularly terminating threads, this will be called and executed by the thread as the
last code before the thread exits. In that case, \a arg is the current QThread.
However, this function will also be called by QThread::terminate (as well as wait() and
setTerminationEnabled) to give Qt a chance to update the terminated thread's state and
process pending DeleteLater events for objects that live in the terminated thread. And for
adopted thread, this method is called by the thread watcher.
In those cases, \a arg will not be the current thread.
*/
void QThreadPrivate::finish(bool lockAnyway) noexcept
{
QThreadPrivate *d = this;
QThread *thr = q_func();
QMutexLocker locker(lockAnyway ? &d->mutex : nullptr);
d->isInFinish = true;
d->priority = QThread::InheritPriority;
void **tls_data = reinterpret_cast<void **>(&d->data->tls);
if (lockAnyway)
locker.unlock();
emit thr->finished(QThread::QPrivateSignal());
QCoreApplicationPrivate::sendPostedEvents(nullptr, QEvent::DeferredDelete, d->data);
QThreadStorageData::finish(tls_data);
if (lockAnyway)
locker.relock();
QAbstractEventDispatcher *eventDispatcher = d->data->eventDispatcher.loadRelaxed();
if (eventDispatcher) {
d->data->eventDispatcher = 0;
if (lockAnyway)
locker.unlock();
eventDispatcher->closingDown();
delete eventDispatcher;
if (lockAnyway)
locker.relock();
}
d->running = false;
d->finished = true;
d->isInFinish = false;
d->interruptionRequested.store(false, std::memory_order_relaxed);
if (!d->waiters) {
CloseHandle(d->handle);
d->handle = 0;
}
d->id = 0;
}
/**************************************************************************
** QThread
*************************************************************************/
Qt::HANDLE QThread::currentThreadIdImpl() noexcept
{
return reinterpret_cast<Qt::HANDLE>(quintptr(GetCurrentThreadId()));
}
int QThread::idealThreadCount() noexcept
{
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
}
void QThread::yieldCurrentThread()
{
SwitchToThread();
}
#endif // QT_CONFIG(thread)
void QThread::sleep(std::chrono::nanoseconds nsecs)
{
using namespace std::chrono;
::Sleep(DWORD(duration_cast<milliseconds>(nsecs).count()));
}
void QThread::sleep(unsigned long secs)
{
::Sleep(secs * 1000);
}
void QThread::msleep(unsigned long msecs)
{
::Sleep(msecs);
}
void QThread::usleep(unsigned long usecs)
{
::Sleep((usecs / 1000) + 1);
}
#if QT_CONFIG(thread)
void QThread::start(Priority priority)
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
if (d->isInFinish) {
locker.unlock();
wait();
locker.relock();
}
if (d->running)
return;
// avoid interacting with the binding system
d->objectName = d->extraData ? d->extraData->objectName.valueBypassingBindings()
: QString();
d->running = true;
d->finished = false;
d->exited = false;
d->returnCode = 0;
d->interruptionRequested.store(false, std::memory_order_relaxed);
/*
NOTE: we create the thread in the suspended state, set the
priority and then resume the thread.
since threads are created with normal priority by default, we
could get into a case where a thread (with priority less than
NormalPriority) tries to create a new thread (also with priority
less than NormalPriority), but the newly created thread preempts
its 'parent' and runs at normal priority.
*/
#if defined(Q_CC_MSVC) && !defined(_DLL)
// MSVC -MT or -MTd build
d->handle = (Qt::HANDLE) _beginthreadex(NULL, d->stackSize, QThreadPrivate::start,
this, CREATE_SUSPENDED, &(d->id));
#else
// MSVC -MD or -MDd or MinGW build
d->handle = CreateThread(nullptr, d->stackSize,
reinterpret_cast<LPTHREAD_START_ROUTINE>(QThreadPrivate::start),
this, CREATE_SUSPENDED, reinterpret_cast<LPDWORD>(&d->id));
#endif
if (!d->handle) {
qErrnoWarning("QThread::start: Failed to create thread");
d->running = false;
d->finished = true;
return;
}
int prio;
d->priority = priority;
switch (priority) {
case IdlePriority:
prio = THREAD_PRIORITY_IDLE;
break;
case LowestPriority:
prio = THREAD_PRIORITY_LOWEST;
break;
case LowPriority:
prio = THREAD_PRIORITY_BELOW_NORMAL;
break;
case NormalPriority:
prio = THREAD_PRIORITY_NORMAL;
break;
case HighPriority:
prio = THREAD_PRIORITY_ABOVE_NORMAL;
break;
case HighestPriority:
prio = THREAD_PRIORITY_HIGHEST;
break;
case TimeCriticalPriority:
prio = THREAD_PRIORITY_TIME_CRITICAL;
break;
case InheritPriority:
default:
prio = GetThreadPriority(GetCurrentThread());
break;
}
if (!SetThreadPriority(d->handle, prio)) {
qErrnoWarning("QThread::start: Failed to set thread priority");
}
if (ResumeThread(d->handle) == (DWORD) -1) {
qErrnoWarning("QThread::start: Failed to resume new thread");
}
}
void QThread::terminate()
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
if (!d->running)
return;
if (!d->terminationEnabled) {
d->terminatePending = true;
return;
}
TerminateThread(d->handle, 0);
d->finish(false);
}
bool QThread::wait(QDeadlineTimer deadline)
{
Q_D(QThread);
QMutexLocker locker(&d->mutex);
if (d->id == GetCurrentThreadId()) {
qWarning("QThread::wait: Thread tried to wait on itself");
return false;
}
if (d->finished || !d->running)
return true;
return d->wait(locker, deadline);
}
bool QThreadPrivate::wait(QMutexLocker<QMutex> &locker, QDeadlineTimer deadline)
{
Q_ASSERT(locker.isLocked());
QThreadPrivate *d = this;
++d->waiters;
locker.mutex()->unlock();
bool ret = false;
switch (WaitForSingleObject(d->handle, deadline.remainingTime())) {
case WAIT_OBJECT_0:
ret = true;
break;
case WAIT_FAILED:
qErrnoWarning("QThread::wait: Thread wait failure");
break;
case WAIT_ABANDONED:
case WAIT_TIMEOUT:
default:
break;
}
locker.mutex()->lock();
--d->waiters;
if (ret && !d->finished) {
// thread was terminated by someone else
d->finish(false);
}
if (d->finished && !d->waiters) {
CloseHandle(d->handle);
d->handle = 0;
}
return ret;
}
void QThread::setTerminationEnabled(bool enabled)
{
QThread *thr = currentThread();
Q_ASSERT_X(thr != 0, "QThread::setTerminationEnabled()",
"Current thread was not started with QThread.");
QThreadPrivate *d = thr->d_func();
QMutexLocker locker(&d->mutex);
d->terminationEnabled = enabled;
if (enabled && d->terminatePending) {
d->finish(false);
locker.unlock(); // don't leave the mutex locked!
_endthreadex(0);
}
}
// Caller must hold the mutex
void QThreadPrivate::setPriority(QThread::Priority threadPriority)
{
// copied from start() with a few modifications:
int prio;
priority = threadPriority;
switch (threadPriority) {
case QThread::IdlePriority:
prio = THREAD_PRIORITY_IDLE;
break;
case QThread::LowestPriority:
prio = THREAD_PRIORITY_LOWEST;
break;
case QThread::LowPriority:
prio = THREAD_PRIORITY_BELOW_NORMAL;
break;
case QThread::NormalPriority:
prio = THREAD_PRIORITY_NORMAL;
break;
case QThread::HighPriority:
prio = THREAD_PRIORITY_ABOVE_NORMAL;
break;
case QThread::HighestPriority:
prio = THREAD_PRIORITY_HIGHEST;
break;
case QThread::TimeCriticalPriority:
prio = THREAD_PRIORITY_TIME_CRITICAL;
break;
default:
return;
}
if (!SetThreadPriority(handle, prio)) {
qErrnoWarning("QThread::setPriority: Failed to set thread priority");
}
}
#endif // QT_CONFIG(thread)
QT_END_NAMESPACE

View File

@ -10,7 +10,9 @@
#include <QtCore/private/qsystemerror_p.h>
#include "qrhid3dhelpers_p.h"
#include <VersionHelpers.h>
#include <cstdio>
#include <versionhelpers.h>
QT_BEGIN_NAMESPACE
@ -28,7 +30,8 @@ using namespace Qt::StringLiterals;
/*!
\class QRhiD3D11InitParams
\inmodule QtGui
\inmodule QtGuiPrivate
\inheaderfile rhi/qrhi.h
\since 6.6
\brief Direct3D 11 specific initialization parameters.
@ -81,7 +84,8 @@ using namespace Qt::StringLiterals;
/*!
\class QRhiD3D11NativeHandles
\inmodule QtGui
\inmodule QtGuiPrivate
\inheaderfile rhi/qrhi.h
\since 6.6
\brief Holds the D3D device and device context used by the QRhi.
@ -95,22 +99,47 @@ using namespace Qt::StringLiterals;
/*!
\variable QRhiD3D11NativeHandles::dev
Points to a
\l{https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nn-d3d11-id3d11device}{ID3D11Device}
or left set to \nullptr if no existing device is to be imported.
\note When importing a device, both the device and the device context must be set to valid objects.
*/
/*!
\variable QRhiD3D11NativeHandles::context
Points to a \l{https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nn-d3d11-id3d11devicecontext}{ID3D11DeviceContext}
or left set to \nullptr if no existing device context is to be imported.
\note When importing a device, both the device and the device context must be set to valid objects.
*/
/*!
\variable QRhiD3D11NativeHandles::featureLevel
Specifies the feature level passed to
\l{https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-d3d11createdevice}{D3D11CreateDevice()}.
Relevant only when QRhi creates the device, ignored when importing a device
and device context. When not set, the default rules outlined in the D3D
documentation apply.
*/
/*!
\variable QRhiD3D11NativeHandles::adapterLuidLow
The low part of the local identifier (LUID) of the DXGI adapter to use.
Relevant only when QRhi creates the device, ignored when importing a device
and device context.
*/
/*!
\variable QRhiD3D11NativeHandles::adapterLuidHigh
The high part of the local identifier (LUID) of the DXGI adapter to use.
Relevant only when QRhi creates the device, ignored when importing a device
and device context.
*/
// help mingw with its ancient sdk headers
@ -208,9 +237,19 @@ bool QRhiD3D11::create(QRhi::Flags flags)
// there. (some features are not supported then, however)
useLegacySwapchainModel = qEnvironmentVariableIntValue("QT_D3D_NO_FLIP");
qCDebug(QRHI_LOG_INFO, "FLIP_* swapchain supported = true, ALLOW_TEARING supported = %s, use legacy (non-FLIP) model = %s",
if (!useLegacySwapchainModel) {
if (qEnvironmentVariableIsSet("QT_D3D_MAX_FRAME_LATENCY"))
maxFrameLatency = UINT(qMax(0, qEnvironmentVariableIntValue("QT_D3D_MAX_FRAME_LATENCY")));
} else {
maxFrameLatency = 0;
}
qCDebug(QRHI_LOG_INFO, "FLIP_* swapchain supported = true, ALLOW_TEARING supported = %s, use legacy (non-FLIP) model = %s, max frame latency = %u",
supportsAllowTearing ? "true" : "false",
useLegacySwapchainModel ? "true" : "false");
useLegacySwapchainModel ? "true" : "false",
maxFrameLatency);
if (maxFrameLatency == 0)
qCDebug(QRHI_LOG_INFO, "Disabling FRAME_LATENCY_WAITABLE_OBJECT usage");
if (!importedDeviceAndContext) {
IDXGIAdapter1 *adapter;
@ -259,9 +298,7 @@ bool QRhiD3D11::create(QRhi::Flags flags)
if (!activeAdapter && (requestedAdapterIndex < 0 || requestedAdapterIndex == adapterIndex)) {
activeAdapter = adapter;
adapterLuid = desc.AdapterLuid;
driverInfoStruct.deviceName = name.toUtf8();
driverInfoStruct.deviceId = desc.DeviceId;
driverInfoStruct.vendorId = desc.VendorId;
QRhiD3D::fillDriverInfo(&driverInfoStruct, desc);
qCDebug(QRHI_LOG_INFO, " using this adapter");
} else {
adapter->Release();
@ -304,21 +341,24 @@ bool QRhiD3D11::create(QRhi::Flags flags)
return false;
}
const bool supports11_1 = SUCCEEDED(ctx->QueryInterface(__uuidof(ID3D11DeviceContext1), reinterpret_cast<void **>(&context)));
ctx->Release();
if (!supports11_1) {
qWarning("ID3D11DeviceContext1 not supported");
return false;
}
// Test if creating a Shader Model 5.0 vertex shader works; we want to
// fail already in create() if that's not the case.
ID3D11VertexShader *testShader = nullptr;
if (SUCCEEDED(dev->CreateVertexShader(g_testVertexShader, sizeof(g_testVertexShader), nullptr, &testShader))) {
testShader->Release();
} else {
qWarning("D3D11 smoke test failed (failed to create vertex shader)");
ctx->Release();
return false;
}
const bool supports11_1 = SUCCEEDED(ctx->QueryInterface(__uuidof(ID3D11DeviceContext1), reinterpret_cast<void **>(&context)));
ctx->Release();
if (!supports11_1) {
qWarning("ID3D11DeviceContext1 not supported");
static const char *msg = "D3D11 smoke test: Failed to create vertex shader";
if (flags.testFlag(QRhi::SuppressSmokeTestWarnings))
qCDebug(QRHI_LOG_INFO, "%s", msg);
else
qWarning("%s", msg);
return false;
}
@ -328,11 +368,19 @@ bool QRhiD3D11::create(QRhi::Flags flags)
// still not support this D3D_FEATURE_LEVEL_11_1 feature. (e.g.
// because it only does 11_0)
if (!features.ConstantBufferOffsetting) {
qWarning("Constant buffer offsetting is not supported by the driver");
static const char *msg = "D3D11 smoke test: Constant buffer offsetting is not supported by the driver";
if (flags.testFlag(QRhi::SuppressSmokeTestWarnings))
qCDebug(QRHI_LOG_INFO, "%s", msg);
else
qWarning("%s", msg);
return false;
}
} else {
qWarning("Failed to query D3D11_FEATURE_D3D11_OPTIONS");
static const char *msg = "D3D11 smoke test: Failed to query D3D11_FEATURE_D3D11_OPTIONS";
if (flags.testFlag(QRhi::SuppressSmokeTestWarnings))
qCDebug(QRHI_LOG_INFO, "%s", msg);
else
qWarning("%s", msg);
return false;
}
} else {
@ -342,12 +390,14 @@ bool QRhiD3D11::create(QRhi::Flags flags)
if (SUCCEEDED(dev->QueryInterface(__uuidof(IDXGIDevice), reinterpret_cast<void **>(&dxgiDev)))) {
IDXGIAdapter *adapter = nullptr;
if (SUCCEEDED(dxgiDev->GetAdapter(&adapter))) {
DXGI_ADAPTER_DESC desc;
adapter->GetDesc(&desc);
adapterLuid = desc.AdapterLuid;
driverInfoStruct.deviceName = QString::fromUtf16(reinterpret_cast<char16_t *>(desc.Description)).toUtf8();
driverInfoStruct.deviceId = desc.DeviceId;
driverInfoStruct.vendorId = desc.VendorId;
IDXGIAdapter1 *adapter1 = nullptr;
if (SUCCEEDED(adapter->QueryInterface(__uuidof(IDXGIAdapter1), reinterpret_cast<void **>(&adapter1)))) {
DXGI_ADAPTER_DESC1 desc;
adapter1->GetDesc1(&desc);
adapterLuid = desc.AdapterLuid;
QRhiD3D::fillDriverInfo(&driverInfoStruct, desc);
adapter1->Release();
}
adapter->Release();
}
dxgiDev->Release();
@ -358,11 +408,6 @@ bool QRhiD3D11::create(QRhi::Flags flags)
if (FAILED(context->QueryInterface(__uuidof(ID3DUserDefinedAnnotation), reinterpret_cast<void **>(&annotations))))
annotations = nullptr;
if (flags.testFlag(QRhi::EnableTimestamps)) {
ofr.timestamps.prepare(2, this);
// timestamp queries are optional so we can go on even if they failed
}
deviceLost = false;
nativeHandlesStruct.dev = dev;
@ -388,7 +433,16 @@ void QRhiD3D11::destroy()
clearShaderCache();
ofr.timestamps.destroy();
if (ofr.tsDisjointQuery) {
ofr.tsDisjointQuery->Release();
ofr.tsDisjointQuery = nullptr;
}
for (int i = 0; i < 2; ++i) {
if (ofr.tsQueries[i]) {
ofr.tsQueries[i]->Release();
ofr.tsQueries[i] = nullptr;
}
}
if (annotations) {
annotations->Release();
@ -592,6 +646,12 @@ bool QRhiD3D11::isFeatureSupported(QRhi::Feature feature) const
return true;
case QRhi::ThreeDimensionalTextureMipmaps:
return true;
case QRhi::MultiView:
return false;
case QRhi::TextureViewFormat:
return false; // because we use fully typed formats for textures and relaxed casting is a D3D12 thing
case QRhi::ResolveDepthStencil:
return false;
default:
Q_UNREACHABLE();
return false;
@ -1262,7 +1322,6 @@ const QRhiNativeHandles *QRhiD3D11::nativeHandles(QRhiCommandBuffer *cb)
void QRhiD3D11::beginExternal(QRhiCommandBuffer *cb)
{
QD3D11CommandBuffer *cbD = QRHI_RES(QD3D11CommandBuffer, cb);
// no timestampSwapChain, in order to avoid timestamp mess
executeCommandBuffer(cbD);
cbD->resetCommands();
}
@ -1285,6 +1344,19 @@ double QRhiD3D11::lastCompletedGpuTime(QRhiCommandBuffer *cb)
return cbD->lastGpuTime;
}
static inline QD3D11RenderTargetData *rtData(QRhiRenderTarget *rt)
{
switch (rt->resourceType()) {
case QRhiResource::SwapChainRenderTarget:
return &QRHI_RES(QD3D11SwapChainRenderTarget, rt)->d;
case QRhiResource::TextureRenderTarget:
return &QRHI_RES(QD3D11TextureRenderTarget, rt)->d;
default:
Q_UNREACHABLE();
return nullptr;
}
}
QRhi::FrameOpResult QRhiD3D11::beginFrame(QRhiSwapChain *swapChain, QRhi::BeginFrameFlags flags)
{
Q_UNUSED(flags);
@ -1293,6 +1365,10 @@ QRhi::FrameOpResult QRhiD3D11::beginFrame(QRhiSwapChain *swapChain, QRhi::BeginF
contextState.currentSwapChain = swapChainD;
const int currentFrameSlot = swapChainD->currentFrameSlot;
// if we have a waitable object, now is the time to wait on it
if (swapChainD->frameLatencyWaitableObject)
WaitForSingleObjectEx(swapChainD->frameLatencyWaitableObject, 1000, true);
swapChainD->cb.resetState();
swapChainD->rt.d.rtv[0] = swapChainD->sampleDesc.Count > 1 ?
@ -1301,12 +1377,22 @@ QRhi::FrameOpResult QRhiD3D11::beginFrame(QRhiSwapChain *swapChain, QRhi::BeginF
finishActiveReadbacks();
if (swapChainD->timestamps.active[currentFrameSlot]) {
if (swapChainD->timestamps.active[swapChainD->currentTimestampPairIndex]) {
double elapsedSec = 0;
if (swapChainD->timestamps.tryQueryTimestamps(currentFrameSlot, context, &elapsedSec))
if (swapChainD->timestamps.tryQueryTimestamps(swapChainD->currentTimestampPairIndex, context, &elapsedSec))
swapChainD->cb.lastGpuTime = elapsedSec;
}
ID3D11Query *tsStart = swapChainD->timestamps.query[swapChainD->currentTimestampPairIndex * 2];
ID3D11Query *tsDisjoint = swapChainD->timestamps.disjointQuery[swapChainD->currentTimestampPairIndex];
const bool recordTimestamps = tsStart && tsDisjoint && !swapChainD->timestamps.active[swapChainD->currentTimestampPairIndex];
QD3D11CommandBuffer::Command &cmd(swapChainD->cb.commands.get());
cmd.cmd = QD3D11CommandBuffer::Command::BeginFrame;
cmd.args.beginFrame.tsQuery = recordTimestamps ? tsStart : nullptr;
cmd.args.beginFrame.tsDisjointQuery = recordTimestamps ? tsDisjoint : nullptr;
cmd.args.beginFrame.swapchainData = rtData(&swapChainD->rt);
return QRhi::FrameOpSuccess;
}
@ -1316,17 +1402,13 @@ QRhi::FrameOpResult QRhiD3D11::endFrame(QRhiSwapChain *swapChain, QRhi::EndFrame
Q_ASSERT(contextState.currentSwapChain = swapChainD);
const int currentFrameSlot = swapChainD->currentFrameSlot;
ID3D11Query *tsDisjoint = swapChainD->timestamps.disjointQuery[currentFrameSlot];
const int tsIdx = QD3D11SwapChain::BUFFER_COUNT * currentFrameSlot;
ID3D11Query *tsStart = swapChainD->timestamps.query[tsIdx];
ID3D11Query *tsEnd = swapChainD->timestamps.query[tsIdx + 1];
const bool recordTimestamps = tsDisjoint && tsStart && tsEnd && !swapChainD->timestamps.active[currentFrameSlot];
QD3D11CommandBuffer::Command &cmd(swapChainD->cb.commands.get());
cmd.cmd = QD3D11CommandBuffer::Command::EndFrame;
cmd.args.endFrame.tsQuery = nullptr; // done later manually, see below
cmd.args.endFrame.tsDisjointQuery = nullptr;
// send all commands to the context
if (recordTimestamps)
executeCommandBuffer(&swapChainD->cb, swapChainD);
else
executeCommandBuffer(&swapChainD->cb);
executeCommandBuffer(&swapChainD->cb);
if (swapChainD->sampleDesc.Count > 1) {
context->ResolveSubresource(swapChainD->backBufferTex, 0,
@ -1334,11 +1416,15 @@ QRhi::FrameOpResult QRhiD3D11::endFrame(QRhiSwapChain *swapChain, QRhi::EndFrame
swapChainD->colorFormat);
}
// this is here because we want to include the time spent on the resolve as well
// this is here because we want to include the time spent on the ResolveSubresource as well
ID3D11Query *tsEnd = swapChainD->timestamps.query[swapChainD->currentTimestampPairIndex * 2 + 1];
ID3D11Query *tsDisjoint = swapChainD->timestamps.disjointQuery[swapChainD->currentTimestampPairIndex];
const bool recordTimestamps = tsEnd && tsDisjoint && !swapChainD->timestamps.active[swapChainD->currentTimestampPairIndex];
if (recordTimestamps) {
context->End(tsEnd);
context->End(tsDisjoint);
swapChainD->timestamps.active[currentFrameSlot] = true;
swapChainD->timestamps.active[swapChainD->currentTimestampPairIndex] = true;
swapChainD->currentTimestampPairIndex = (swapChainD->currentTimestampPairIndex + 1) % QD3D11SwapChainTimestamps::TIMESTAMP_PAIRS;
}
if (!flags.testFlag(QRhi::SkipPresent)) {
@ -1383,12 +1469,36 @@ QRhi::FrameOpResult QRhiD3D11::beginOffscreenFrame(QRhiCommandBuffer **cb, QRhi:
ofr.cbWrapper.resetState();
*cb = &ofr.cbWrapper;
if (ofr.timestamps.active[ofr.timestampIdx]) {
double elapsedSec = 0;
if (ofr.timestamps.tryQueryTimestamps(ofr.timestampIdx, context, &elapsedSec))
ofr.cbWrapper.lastGpuTime = elapsedSec;
if (rhiFlags.testFlag(QRhi::EnableTimestamps)) {
D3D11_QUERY_DESC queryDesc = {};
if (!ofr.tsDisjointQuery) {
queryDesc.Query = D3D11_QUERY_TIMESTAMP_DISJOINT;
HRESULT hr = dev->CreateQuery(&queryDesc, &ofr.tsDisjointQuery);
if (FAILED(hr)) {
qWarning("Failed to create timestamp disjoint query: %s",
qPrintable(QSystemError::windowsComString(hr)));
return QRhi::FrameOpError;
}
}
queryDesc.Query = D3D11_QUERY_TIMESTAMP;
for (int i = 0; i < 2; ++i) {
if (!ofr.tsQueries[i]) {
HRESULT hr = dev->CreateQuery(&queryDesc, &ofr.tsQueries[i]);
if (FAILED(hr)) {
qWarning("Failed to create timestamp query: %s",
qPrintable(QSystemError::windowsComString(hr)));
return QRhi::FrameOpError;
}
}
}
}
QD3D11CommandBuffer::Command &cmd(ofr.cbWrapper.commands.get());
cmd.cmd = QD3D11CommandBuffer::Command::BeginFrame;
cmd.args.beginFrame.tsQuery = ofr.tsQueries[0] ? ofr.tsQueries[0] : nullptr;
cmd.args.beginFrame.tsDisjointQuery = ofr.tsDisjointQuery ? ofr.tsDisjointQuery : nullptr;
cmd.args.beginFrame.swapchainData = nullptr;
return QRhi::FrameOpSuccess;
}
@ -1397,25 +1507,39 @@ QRhi::FrameOpResult QRhiD3D11::endOffscreenFrame(QRhi::EndFrameFlags flags)
Q_UNUSED(flags);
ofr.active = false;
ID3D11Query *tsDisjoint = ofr.timestamps.disjointQuery[ofr.timestampIdx];
ID3D11Query *tsStart = ofr.timestamps.query[ofr.timestampIdx * 2];
ID3D11Query *tsEnd = ofr.timestamps.query[ofr.timestampIdx * 2 + 1];
const bool recordTimestamps = tsDisjoint && tsStart && tsEnd && !ofr.timestamps.active[ofr.timestampIdx];
if (recordTimestamps) {
context->Begin(tsDisjoint);
context->End(tsStart); // record timestamp; no Begin() for D3D11_QUERY_TIMESTAMP
}
QD3D11CommandBuffer::Command &cmd(ofr.cbWrapper.commands.get());
cmd.cmd = QD3D11CommandBuffer::Command::EndFrame;
cmd.args.endFrame.tsQuery = ofr.tsQueries[1] ? ofr.tsQueries[1] : nullptr;
cmd.args.endFrame.tsDisjointQuery = ofr.tsDisjointQuery ? ofr.tsDisjointQuery : nullptr;
executeCommandBuffer(&ofr.cbWrapper);
context->Flush();
finishActiveReadbacks();
if (recordTimestamps) {
context->End(tsEnd);
context->End(tsDisjoint);
ofr.timestamps.active[ofr.timestampIdx] = true;
ofr.timestampIdx = (ofr.timestampIdx + 1) % 2;
if (ofr.tsQueries[0]) {
quint64 timestamps[2];
D3D11_QUERY_DATA_TIMESTAMP_DISJOINT dj;
HRESULT hr;
bool ok = true;
do {
hr = context->GetData(ofr.tsDisjointQuery, &dj, sizeof(dj), 0);
} while (hr == S_FALSE);
ok &= hr == S_OK;
do {
hr = context->GetData(ofr.tsQueries[1], &timestamps[1], sizeof(quint64), 0);
} while (hr == S_FALSE);
ok &= hr == S_OK;
do {
hr = context->GetData(ofr.tsQueries[0], &timestamps[0], sizeof(quint64), 0);
} while (hr == S_FALSE);
ok &= hr == S_OK;
if (ok) {
if (!dj.Disjoint && dj.Frequency) {
const float elapsedMs = (timestamps[1] - timestamps[0]) / float(dj.Frequency) * 1000.0f;
ofr.cbWrapper.lastGpuTime = elapsedMs / 1000.0;
}
}
}
return QRhi::FrameOpSuccess;
@ -1558,7 +1682,7 @@ QRhi::FrameOpResult QRhiD3D11::finish()
} else {
Q_ASSERT(contextState.currentSwapChain);
Q_ASSERT(contextState.currentSwapChain->cb.recordingPass == QD3D11CommandBuffer::NoPass);
executeCommandBuffer(&contextState.currentSwapChain->cb); // no timestampSwapChain, in order to avoid timestamp mess
executeCommandBuffer(&contextState.currentSwapChain->cb);
contextState.currentSwapChain->cb.resetCommands();
}
}
@ -1933,19 +2057,6 @@ void QRhiD3D11::finishActiveReadbacks()
f();
}
static inline QD3D11RenderTargetData *rtData(QRhiRenderTarget *rt)
{
switch (rt->resourceType()) {
case QRhiResource::SwapChainRenderTarget:
return &QRHI_RES(QD3D11SwapChainRenderTarget, rt)->d;
case QRhiResource::TextureRenderTarget:
return &QRHI_RES(QD3D11TextureRenderTarget, rt)->d;
default:
Q_UNREACHABLE();
return nullptr;
}
}
void QRhiD3D11::resourceUpdate(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resourceUpdates)
{
Q_ASSERT(QRHI_RES(QD3D11CommandBuffer, cb)->recordingPass == QD3D11CommandBuffer::NoPass);
@ -2064,6 +2175,8 @@ void QRhiD3D11::endPass(QRhiCommandBuffer *cb, QRhiResourceUpdateBatch *resource
cmd.args.resolveSubRes.srcSubRes = D3D11CalcSubresource(0, UINT(colorAtt.layer()), 1);
cmd.args.resolveSubRes.format = dstTexD->dxgiFormat;
}
if (rtTex->m_desc.depthResolveTexture())
qWarning("Resolving multisample depth-stencil buffers is not supported with D3D");
}
cbD->recordingPass = QD3D11CommandBuffer::NoPass;
@ -2651,7 +2764,7 @@ void QRhiD3D11::resetShaderResources()
currentShaderMask &= ~StageU##MaskBit; \
}
void QRhiD3D11::executeCommandBuffer(QD3D11CommandBuffer *cbD, QD3D11SwapChain *timestampSwapChain)
void QRhiD3D11::executeCommandBuffer(QD3D11CommandBuffer *cbD)
{
quint32 stencilRef = 0;
float blendConstants[] = { 1, 1, 1, 1 };
@ -2664,26 +2777,30 @@ void QRhiD3D11::executeCommandBuffer(QD3D11CommandBuffer *cbD, QD3D11SwapChain *
};
int currentShaderMask = 0xFF;
if (timestampSwapChain) {
const int currentFrameSlot = timestampSwapChain->currentFrameSlot;
ID3D11Query *tsDisjoint = timestampSwapChain->timestamps.disjointQuery[currentFrameSlot];
const int tsIdx = QD3D11SwapChain::BUFFER_COUNT * currentFrameSlot;
ID3D11Query *tsStart = timestampSwapChain->timestamps.query[tsIdx];
if (tsDisjoint && tsStart && !timestampSwapChain->timestamps.active[currentFrameSlot]) {
// The timestamps seem to include vsync time with Present(1), except
// when running on a non-primary gpu. This is not ideal. So try working
// it around by issuing a semi-fake OMSetRenderTargets early and
// writing the first timestamp only afterwards.
context->Begin(tsDisjoint);
QD3D11RenderTargetData *rtD = rtData(&timestampSwapChain->rt);
context->OMSetRenderTargets(UINT(rtD->colorAttCount), rtD->colorAttCount ? rtD->rtv : nullptr, rtD->dsv);
context->End(tsStart); // just record a timestamp, no Begin needed
}
}
for (auto it = cbD->commands.cbegin(), end = cbD->commands.cend(); it != end; ++it) {
const QD3D11CommandBuffer::Command &cmd(*it);
switch (cmd.cmd) {
case QD3D11CommandBuffer::Command::BeginFrame:
if (cmd.args.beginFrame.tsDisjointQuery)
context->Begin(cmd.args.beginFrame.tsDisjointQuery);
if (cmd.args.beginFrame.tsQuery) {
if (cmd.args.beginFrame.swapchainData) {
// The timestamps seem to include vsync time with Present(1), except
// when running on a non-primary gpu. This is not ideal. So try working
// it around by issuing a semi-fake OMSetRenderTargets early and
// writing the first timestamp only afterwards.
QD3D11RenderTargetData *rtD = cmd.args.beginFrame.swapchainData;
context->OMSetRenderTargets(UINT(rtD->colorAttCount), rtD->colorAttCount ? rtD->rtv : nullptr, rtD->dsv);
}
context->End(cmd.args.beginFrame.tsQuery); // no Begin() for D3D11_QUERY_TIMESTAMP
}
break;
case QD3D11CommandBuffer::Command::EndFrame:
if (cmd.args.endFrame.tsQuery)
context->End(cmd.args.endFrame.tsQuery);
if (cmd.args.endFrame.tsDisjointQuery)
context->End(cmd.args.endFrame.tsDisjointQuery);
break;
case QD3D11CommandBuffer::Command::ResetShaderResources:
resetShaderResources();
break;
@ -2779,7 +2896,7 @@ void QRhiD3D11::executeCommandBuffer(QD3D11CommandBuffer *cbD, QD3D11SwapChain *
break;
case QD3D11CommandBuffer::Command::Draw:
if (cmd.args.draw.ps) {
if (cmd.args.draw.instanceCount == 1)
if (cmd.args.draw.instanceCount == 1 && cmd.args.draw.firstInstance == 0)
context->Draw(cmd.args.draw.vertexCount, cmd.args.draw.firstVertex);
else
context->DrawInstanced(cmd.args.draw.vertexCount, cmd.args.draw.instanceCount,
@ -2790,7 +2907,7 @@ void QRhiD3D11::executeCommandBuffer(QD3D11CommandBuffer *cbD, QD3D11SwapChain *
break;
case QD3D11CommandBuffer::Command::DrawIndexed:
if (cmd.args.drawIndexed.ps) {
if (cmd.args.drawIndexed.instanceCount == 1)
if (cmd.args.drawIndexed.instanceCount == 1 && cmd.args.drawIndexed.firstInstance == 0)
context->DrawIndexed(cmd.args.drawIndexed.indexCount, cmd.args.drawIndexed.firstIndex,
cmd.args.drawIndexed.vertexOffset);
else
@ -3195,6 +3312,10 @@ bool QD3D11Texture::prepareCreate(QSize *adjustedSize)
if (tex || tex3D || tex1D)
destroy();
QRHI_RES_RHI(QRhiD3D11);
if (!rhiD->isTextureFormatSupported(m_format, m_flags))
return false;
const bool isDepth = isDepthTextureFormat(m_format);
const bool isCube = m_flags.testFlag(CubeMap);
const bool is3D = m_flags.testFlag(ThreeDimensional);
@ -3205,7 +3326,6 @@ bool QD3D11Texture::prepareCreate(QSize *adjustedSize)
const QSize size = is1D ? QSize(qMax(1, m_pixelSize.width()), 1)
: (m_pixelSize.isEmpty() ? QSize(1, 1) : m_pixelSize);
QRHI_RES_RHI(QRhiD3D11);
dxgiFormat = toD3DTextureFormat(m_format, m_flags);
mipLevelCount = uint(hasMipMaps ? rhiD->q->mipLevelsForSize(size) : 1);
sampleDesc = rhiD->effectiveSampleDesc(m_sampleCount);
@ -4157,6 +4277,22 @@ static inline DXGI_FORMAT toD3DAttributeFormat(QRhiVertexInputAttribute::Format
return DXGI_FORMAT_R16G16_FLOAT;
case QRhiVertexInputAttribute::Half:
return DXGI_FORMAT_R16_FLOAT;
case QRhiVertexInputAttribute::UShort4:
// Note: D3D does not support UShort3. Pass through UShort3 as UShort4.
case QRhiVertexInputAttribute::UShort3:
return DXGI_FORMAT_R16G16B16A16_UINT;
case QRhiVertexInputAttribute::UShort2:
return DXGI_FORMAT_R16G16_UINT;
case QRhiVertexInputAttribute::UShort:
return DXGI_FORMAT_R16_UINT;
case QRhiVertexInputAttribute::SShort4:
// Note: D3D does not support SShort3. Pass through SShort3 as SShort4.
case QRhiVertexInputAttribute::SShort3:
return DXGI_FORMAT_R16G16B16A16_SINT;
case QRhiVertexInputAttribute::SShort2:
return DXGI_FORMAT_R16G16_SINT;
case QRhiVertexInputAttribute::SShort:
return DXGI_FORMAT_R16_SINT;
default:
Q_UNREACHABLE();
return DXGI_FORMAT_R32G32B32A32_FLOAT;
@ -4579,7 +4715,7 @@ bool QD3D11GraphicsPipeline::create()
} else {
QByteArray sem;
sem.resize(16);
qsnprintf(sem.data(), sem.size(), "TEXCOORD%d_", it->location() - matrixSlice);
std::snprintf(sem.data(), sem.size(), "TEXCOORD%d_", it->location() - matrixSlice);
matrixSliceSemantics.append(sem);
desc.SemanticName = matrixSliceSemantics.last().constData();
desc.SemanticIndex = UINT(matrixSlice);
@ -4702,14 +4838,13 @@ void QD3D11CommandBuffer::destroy()
// nothing to do here
}
bool QD3D11Timestamps::prepare(int pairCount, QRhiD3D11 *rhiD)
bool QD3D11SwapChainTimestamps::prepare(QRhiD3D11 *rhiD)
{
// Creates the query objects if not yet done, but otherwise calling this
// function is expected to be a no-op.
Q_ASSERT(pairCount <= MAX_TIMESTAMP_PAIRS);
D3D11_QUERY_DESC queryDesc = {};
for (int i = 0; i < pairCount; ++i) {
for (int i = 0; i < TIMESTAMP_PAIRS; ++i) {
if (!disjointQuery[i]) {
queryDesc.Query = D3D11_QUERY_TIMESTAMP_DISJOINT;
HRESULT hr = rhiD->dev->CreateQuery(&queryDesc, &disjointQuery[i]);
@ -4721,7 +4856,7 @@ bool QD3D11Timestamps::prepare(int pairCount, QRhiD3D11 *rhiD)
}
queryDesc.Query = D3D11_QUERY_TIMESTAMP;
for (int j = 0; j < 2; ++j) {
const int idx = pairCount * i + j;
const int idx = 2 * i + j;
if (!query[idx]) {
HRESULT hr = rhiD->dev->CreateQuery(&queryDesc, &query[idx]);
if (FAILED(hr)) {
@ -4732,20 +4867,19 @@ bool QD3D11Timestamps::prepare(int pairCount, QRhiD3D11 *rhiD)
}
}
}
this->pairCount = pairCount;
return true;
}
void QD3D11Timestamps::destroy()
void QD3D11SwapChainTimestamps::destroy()
{
for (int i = 0; i < MAX_TIMESTAMP_PAIRS; ++i) {
for (int i = 0; i < TIMESTAMP_PAIRS; ++i) {
active[i] = false;
if (disjointQuery[i]) {
disjointQuery[i]->Release();
disjointQuery[i] = nullptr;
}
for (int j = 0; j < 2; ++j) {
const int idx = MAX_TIMESTAMP_PAIRS * i + j;
const int idx = TIMESTAMP_PAIRS * i + j;
if (query[idx]) {
query[idx]->Release();
query[idx] = nullptr;
@ -4754,26 +4888,21 @@ void QD3D11Timestamps::destroy()
}
}
bool QD3D11Timestamps::tryQueryTimestamps(int idx, ID3D11DeviceContext *context, double *elapsedSec)
bool QD3D11SwapChainTimestamps::tryQueryTimestamps(int pairIndex, ID3D11DeviceContext *context, double *elapsedSec)
{
bool result = false;
if (!active[idx])
if (!active[pairIndex])
return result;
ID3D11Query *tsDisjoint = disjointQuery[idx];
const int tsIdx = pairCount * idx;
ID3D11Query *tsStart = query[tsIdx];
ID3D11Query *tsEnd = query[tsIdx + 1];
ID3D11Query *tsDisjoint = disjointQuery[pairIndex];
ID3D11Query *tsStart = query[pairIndex * 2];
ID3D11Query *tsEnd = query[pairIndex * 2 + 1];
quint64 timestamps[2];
D3D11_QUERY_DATA_TIMESTAMP_DISJOINT dj;
bool ok = true;
ok &= context->GetData(tsDisjoint, &dj, sizeof(dj), D3D11_ASYNC_GETDATA_DONOTFLUSH) == S_OK;
ok &= context->GetData(tsEnd, &timestamps[1], sizeof(quint64), D3D11_ASYNC_GETDATA_DONOTFLUSH) == S_OK;
// this above is often not ready, not even in frame_where_recorded+2,
// not clear why. so make the whole thing async and do not touch the
// queries until they are finally all available in frame this+2 or
// this+4 or ...
ok &= context->GetData(tsStart, &timestamps[0], sizeof(quint64), D3D11_ASYNC_GETDATA_DONOTFLUSH) == S_OK;
if (ok) {
@ -4782,16 +4911,14 @@ bool QD3D11Timestamps::tryQueryTimestamps(int idx, ID3D11DeviceContext *context,
*elapsedSec = elapsedMs / 1000.0;
result = true;
}
active[idx] = false;
} // else leave active set, will retry in a subsequent beginFrame or similar
active[pairIndex] = false;
} // else leave active set, will retry in a subsequent beginFrame
return result;
}
QD3D11SwapChain::QD3D11SwapChain(QRhiImplementation *rhi)
: QRhiSwapChain(rhi),
rt(rhi, this),
cb(rhi)
: QRhiSwapChain(rhi), rt(rhi, this), rtRight(rhi, this), cb(rhi)
{
backBufferTex = nullptr;
backBufferRtv = nullptr;
@ -4812,6 +4939,10 @@ void QD3D11SwapChain::releaseBuffers()
backBufferRtv->Release();
backBufferRtv = nullptr;
}
if (backBufferRtvRight) {
backBufferRtvRight->Release();
backBufferRtvRight = nullptr;
}
if (backBufferTex) {
backBufferTex->Release();
backBufferTex = nullptr;
@ -4850,6 +4981,11 @@ void QD3D11SwapChain::destroy()
dcompTarget = nullptr;
}
if (frameLatencyWaitableObject) {
CloseHandle(frameLatencyWaitableObject);
frameLatencyWaitableObject = nullptr;
}
QRHI_RES_RHI(QRhiD3D11);
if (rhiD) {
rhiD->unregisterResource(this);
@ -4869,50 +5005,17 @@ QRhiRenderTarget *QD3D11SwapChain::currentFrameRenderTarget()
return &rt;
}
QRhiRenderTarget *QD3D11SwapChain::currentFrameRenderTarget(StereoTargetBuffer targetBuffer)
{
return targetBuffer == StereoTargetBuffer::LeftBuffer? &rt: &rtRight;
}
QSize QD3D11SwapChain::surfacePixelSize()
{
Q_ASSERT(m_window);
return m_window->size() * m_window->devicePixelRatio();
}
static bool output6ForWindow(QWindow *w, IDXGIAdapter1 *adapter, IDXGIOutput6 **result)
{
bool ok = false;
QRect wr = w->geometry();
wr = QRect(wr.topLeft() * w->devicePixelRatio(), wr.size() * w->devicePixelRatio());
const QPoint center = wr.center();
IDXGIOutput *currentOutput = nullptr;
IDXGIOutput *output = nullptr;
for (UINT i = 0; adapter->EnumOutputs(i, &output) != DXGI_ERROR_NOT_FOUND; ++i) {
DXGI_OUTPUT_DESC desc;
output->GetDesc(&desc);
const RECT r = desc.DesktopCoordinates;
const QRect dr(QPoint(r.left, r.top), QPoint(r.right - 1, r.bottom - 1));
if (dr.contains(center)) {
currentOutput = output;
break;
} else {
output->Release();
}
}
if (currentOutput) {
ok = SUCCEEDED(currentOutput->QueryInterface(__uuidof(IDXGIOutput6), reinterpret_cast<void **>(result)));
currentOutput->Release();
}
return ok;
}
static bool outputDesc1ForWindow(QWindow *w, IDXGIAdapter1 *adapter, DXGI_OUTPUT_DESC1 *result)
{
bool ok = false;
IDXGIOutput6 *out6 = nullptr;
if (output6ForWindow(w, adapter, &out6)) {
ok = SUCCEEDED(out6->GetDesc1(result));
out6->Release();
}
return ok;
}
bool QD3D11SwapChain::isFormatSupported(Format f)
{
if (f == SDR)
@ -4925,7 +5028,7 @@ bool QD3D11SwapChain::isFormatSupported(Format f)
QRHI_RES_RHI(QRhiD3D11);
DXGI_OUTPUT_DESC1 desc1;
if (outputDesc1ForWindow(m_window, rhiD->activeAdapter, &desc1)) {
if (QRhiD3D::outputDesc1ForWindow(m_window, rhiD->activeAdapter, &desc1)) {
if (desc1.ColorSpace == DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020)
return f == QRhiSwapChain::HDRExtendedSrgbLinear || f == QRhiSwapChain::HDR10;
}
@ -4936,14 +5039,16 @@ bool QD3D11SwapChain::isFormatSupported(Format f)
QRhiSwapChainHdrInfo QD3D11SwapChain::hdrInfo()
{
QRhiSwapChainHdrInfo info = QRhiSwapChain::hdrInfo();
// Must use m_window, not window, given this may be called before createOrResize().
if (m_window) {
QRHI_RES_RHI(QRhiD3D11);
DXGI_OUTPUT_DESC1 hdrOutputDesc;
if (outputDesc1ForWindow(m_window, rhiD->activeAdapter, &hdrOutputDesc)) {
info.isHardCodedDefaults = false;
if (QRhiD3D::outputDesc1ForWindow(m_window, rhiD->activeAdapter, &hdrOutputDesc)) {
info.limitsType = QRhiSwapChainHdrInfo::LuminanceInNits;
info.limits.luminanceInNits.minLuminance = hdrOutputDesc.MinLuminance;
info.limits.luminanceInNits.maxLuminance = hdrOutputDesc.MaxLuminance;
info.luminanceBehavior = QRhiSwapChainHdrInfo::SceneReferred; // 1.0 = 80 nits
info.sdrWhiteLevel = QRhiD3D::sdrWhiteLevelInNits(hdrOutputDesc);
}
}
return info;
@ -5016,11 +5121,13 @@ bool QD3D11SwapChain::createOrResize()
{
return createOrResizeWin7();
}
// Can be called multiple times due to window resizes - that is not the
// same as a simple destroy+create (as with other resources). Just need to
// resize the buffers then.
const bool needsRegistration = !window || window != m_window;
const bool stereo = m_window->format().stereo();
// except if the window actually changes
if (window && window != m_window)
@ -5041,7 +5148,7 @@ bool QD3D11SwapChain::createOrResize()
if (m_flags.testFlag(SurfaceHasPreMulAlpha) || m_flags.testFlag(SurfaceHasNonPreMulAlpha)) {
if (!rhiD->useLegacySwapchainModel && rhiD->ensureDirectCompositionDevice()) {
if (!dcompTarget) {
hr = rhiD->dcompDevice->CreateTargetForHwnd(hwnd, true, &dcompTarget);
hr = rhiD->dcompDevice->CreateTargetForHwnd(hwnd, false, &dcompTarget);
if (FAILED(hr)) {
qWarning("Failed to create Direct Compsition target for the window: %s",
qPrintable(QSystemError::windowsComString(hr)));
@ -5071,6 +5178,17 @@ bool QD3D11SwapChain::createOrResize()
if (swapInterval == 0 && rhiD->supportsAllowTearing)
swapChainFlags |= DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;
// maxFrameLatency 0 means no waitable object usage.
// Ignore it also when NoVSync is on, and when using WARP.
const bool useFrameLatencyWaitableObject = rhiD->maxFrameLatency != 0
&& swapInterval != 0
&& rhiD->driverInfoStruct.deviceType != QRhiDriverInfo::CpuDevice;
if (useFrameLatencyWaitableObject) {
// the flag is not supported in real fullscreen on D3D11, but perhaps that's fine since we only do borderless
swapChainFlags |= DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT;
}
if (!swapChain) {
sampleDesc = rhiD->effectiveSampleDesc(m_sampleCount);
colorFormat = DEFAULT_FORMAT;
@ -5078,7 +5196,7 @@ bool QD3D11SwapChain::createOrResize()
DXGI_COLOR_SPACE_TYPE hdrColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709; // SDR
DXGI_OUTPUT_DESC1 hdrOutputDesc;
if (outputDesc1ForWindow(m_window, rhiD->activeAdapter, &hdrOutputDesc) && m_format != SDR) {
if (QRhiD3D::outputDesc1ForWindow(m_window, rhiD->activeAdapter, &hdrOutputDesc) && m_format != SDR) {
// https://docs.microsoft.com/en-us/windows/win32/direct3darticles/high-dynamic-range
if (hdrOutputDesc.ColorSpace == DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020) {
switch (m_format) {
@ -5120,6 +5238,7 @@ bool QD3D11SwapChain::createOrResize()
desc.Flags = swapChainFlags;
desc.Scaling = rhiD->useLegacySwapchainModel ? DXGI_SCALING_STRETCH : DXGI_SCALING_NONE;
desc.SwapEffect = rhiD->useLegacySwapchainModel ? DXGI_SWAP_EFFECT_DISCARD : DXGI_SWAP_EFFECT_FLIP_DISCARD;
desc.Stereo = stereo;
if (dcompVisual) {
// With DirectComposition setting AlphaMode to STRAIGHT fails the
@ -5155,16 +5274,31 @@ bool QD3D11SwapChain::createOrResize()
if (SUCCEEDED(hr)) {
swapChain = sc1;
if (m_format != SDR) {
IDXGISwapChain3 *sc3 = nullptr;
if (SUCCEEDED(sc1->QueryInterface(__uuidof(IDXGISwapChain3), reinterpret_cast<void **>(&sc3)))) {
IDXGISwapChain3 *sc3 = nullptr;
if (SUCCEEDED(sc1->QueryInterface(__uuidof(IDXGISwapChain3), reinterpret_cast<void **>(&sc3)))) {
if (m_format != SDR) {
hr = sc3->SetColorSpace1(hdrColorSpace);
if (FAILED(hr))
qWarning("Failed to set color space on swapchain: %s",
qPrintable(QSystemError::windowsComString(hr)));
sc3->Release();
} else {
qPrintable(QSystemError::windowsComString(hr)));
}
if (useFrameLatencyWaitableObject) {
sc3->SetMaximumFrameLatency(rhiD->maxFrameLatency);
frameLatencyWaitableObject = sc3->GetFrameLatencyWaitableObject();
}
sc3->Release();
} else {
if (m_format != SDR)
qWarning("IDXGISwapChain3 not available, HDR swapchain will not work as expected");
if (useFrameLatencyWaitableObject) {
IDXGISwapChain2 *sc2 = nullptr;
if (SUCCEEDED(sc1->QueryInterface(__uuidof(IDXGISwapChain2), reinterpret_cast<void **>(&sc2)))) {
sc2->SetMaximumFrameLatency(rhiD->maxFrameLatency);
frameLatencyWaitableObject = sc2->GetFrameLatencyWaitableObject();
sc2->Release();
} else { // this cannot really happen since we require DXGIFactory2
qWarning("IDXGISwapChain2 not available, FrameLatencyWaitableObject cannot be used");
}
}
}
if (dcompVisual) {
@ -5238,6 +5372,19 @@ bool QD3D11SwapChain::createOrResize()
return false;
}
if (stereo) {
// Create a second render target view for the right eye
rtvDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2DARRAY;
rtvDesc.Texture2DArray.FirstArraySlice = 1;
rtvDesc.Texture2DArray.ArraySize = 1;
hr = rhiD->dev->CreateRenderTargetView(backBufferTex, &rtvDesc, &backBufferRtvRight);
if (FAILED(hr)) {
qWarning("Failed to create rtv for swapchain backbuffer (right eye): %s",
qPrintable(QSystemError::windowsComString(hr)));
return false;
}
}
// Try to reduce stalls by having a dedicated MSAA texture per swapchain buffer.
for (int i = 0; i < BUFFER_COUNT; ++i) {
if (sampleDesc.Count > 1) {
@ -5276,8 +5423,20 @@ bool QD3D11SwapChain::createOrResize()
rtD->d.colorAttCount = 1;
rtD->d.dsAttCount = m_depthStencil ? 1 : 0;
if (stereo) {
rtD = QRHI_RES(QD3D11SwapChainRenderTarget, &rtRight);
rtD->d.rp = QRHI_RES(QD3D11RenderPassDescriptor, m_renderPassDesc);
rtD->d.pixelSize = pixelSize;
rtD->d.dpr = float(window->devicePixelRatio());
rtD->d.sampleCount = int(sampleDesc.Count);
rtD->d.colorAttCount = 1;
rtD->d.dsAttCount = m_depthStencil ? 1 : 0;
rtD->d.rtv[0] = backBufferRtvRight;
rtD->d.dsv = ds ? ds->dsv : nullptr;
}
if (rhiD->rhiFlags.testFlag(QRhi::EnableTimestamps)) {
timestamps.prepare(BUFFER_COUNT, rhiD);
timestamps.prepare(rhiD);
// timestamp queries are optional so we can go on even if they failed
}

View File

@ -356,6 +356,8 @@ struct QD3D11CommandBuffer : public QRhiCommandBuffer
struct Command {
enum Cmd {
BeginFrame,
EndFrame,
ResetShaderResources,
SetRenderTarget,
Clear,
@ -385,6 +387,15 @@ struct QD3D11CommandBuffer : public QRhiCommandBuffer
// QRhi*/QD3D11* references should be kept at minimum (so no
// QRhiTexture/Buffer/etc. pointers).
union Args {
struct {
ID3D11Query *tsQuery;
ID3D11Query *tsDisjointQuery;
QD3D11RenderTargetData *swapchainData;
} beginFrame;
struct {
ID3D11Query *tsQuery;
ID3D11Query *tsDisjointQuery;
} endFrame;
struct {
QRhiRenderTarget *rt;
} setRenderTarget;
@ -556,17 +567,15 @@ struct QD3D11CommandBuffer : public QRhiCommandBuffer
}
};
static const int QD3D11_SWAPCHAIN_BUFFER_COUNT = 2;
struct QD3D11Timestamps
struct QD3D11SwapChainTimestamps
{
static const int MAX_TIMESTAMP_PAIRS = QD3D11_SWAPCHAIN_BUFFER_COUNT;
bool active[MAX_TIMESTAMP_PAIRS] = {};
ID3D11Query *disjointQuery[MAX_TIMESTAMP_PAIRS] = {};
ID3D11Query *query[MAX_TIMESTAMP_PAIRS * 2] = {};
int pairCount = 0;
static const int TIMESTAMP_PAIRS = 2;
bool prepare(int pairCount, QRhiD3D11 *rhiD);
bool active[TIMESTAMP_PAIRS] = {};
ID3D11Query *disjointQuery[TIMESTAMP_PAIRS] = {};
ID3D11Query *query[TIMESTAMP_PAIRS * 2] = {};
bool prepare(QRhiD3D11 *rhiD);
void destroy();
bool tryQueryTimestamps(int idx, ID3D11DeviceContext *context, double *elapsedSec);
};
@ -579,6 +588,7 @@ struct QD3D11SwapChain : public QRhiSwapChain
QRhiCommandBuffer *currentFrameCommandBuffer() override;
QRhiRenderTarget *currentFrameRenderTarget() override;
QRhiRenderTarget *currentFrameRenderTarget(StereoTargetBuffer targetBuffer) override;
QSize surfacePixelSize() override;
bool isFormatSupported(Format f) override;
@ -587,7 +597,7 @@ struct QD3D11SwapChain : public QRhiSwapChain
QRhiRenderPassDescriptor *newCompatibleRenderPassDescriptor() override;
bool createOrResize() override;
bool createOrResizeWin7();
void releaseBuffers();
bool newColorBuffer(const QSize &size, DXGI_FORMAT format, DXGI_SAMPLE_DESC sampleDesc,
ID3D11Texture2D **tex, ID3D11RenderTargetView **rtv) const;
@ -595,6 +605,7 @@ struct QD3D11SwapChain : public QRhiSwapChain
QWindow *window = nullptr;
QSize pixelSize;
QD3D11SwapChainRenderTarget rt;
QD3D11SwapChainRenderTarget rtRight;
QD3D11CommandBuffer cb;
DXGI_FORMAT colorFormat;
DXGI_FORMAT srgbAdjustedColorFormat;
@ -602,7 +613,8 @@ struct QD3D11SwapChain : public QRhiSwapChain
UINT swapChainFlags = 0;
ID3D11Texture2D *backBufferTex;
ID3D11RenderTargetView *backBufferRtv;
static const int BUFFER_COUNT = QD3D11_SWAPCHAIN_BUFFER_COUNT;
ID3D11RenderTargetView *backBufferRtvRight = nullptr;
static const int BUFFER_COUNT = 2;
ID3D11Texture2D *msaaTex[BUFFER_COUNT];
ID3D11RenderTargetView *msaaRtv[BUFFER_COUNT];
DXGI_SAMPLE_DESC sampleDesc;
@ -612,7 +624,9 @@ struct QD3D11SwapChain : public QRhiSwapChain
UINT swapInterval = 1;
IDCompositionTarget *dcompTarget = nullptr;
IDCompositionVisual *dcompVisual = nullptr;
QD3D11Timestamps timestamps;
QD3D11SwapChainTimestamps timestamps;
int currentTimestampPairIndex = 0;
HANDLE frameLatencyWaitableObject = nullptr;
};
class QRhiD3D11 : public QRhiImplementation
@ -737,7 +751,7 @@ public:
const uint *dynOfsPairs, int dynOfsPairCount,
bool offsetOnlyChange);
void resetShaderResources();
void executeCommandBuffer(QD3D11CommandBuffer *cbD, QD3D11SwapChain *timestampSwapChain = nullptr);
void executeCommandBuffer(QD3D11CommandBuffer *cbD);
DXGI_SAMPLE_DESC effectiveSampleDesc(int sampleCount) const;
void finishActiveReadbacks();
void reportLiveObjects(ID3D11Device *device);
@ -748,6 +762,7 @@ public:
QRhi::Flags rhiFlags;
bool debugLayer = false;
UINT maxFrameLatency = 2; // 1-3, use 2 to keep CPU-GPU parallelism while reducing lag compared to tripple buffering
bool importedDeviceAndContext = false;
ID3D11Device *dev = nullptr;
ID3D11DeviceContext1 *context = nullptr;
@ -780,8 +795,8 @@ public:
OffscreenFrame(QRhiImplementation *rhi) : cbWrapper(rhi) { }
bool active = false;
QD3D11CommandBuffer cbWrapper;
QD3D11Timestamps timestamps;
int timestampIdx = 0;
ID3D11Query *tsQueries[2] = {};
ID3D11Query *tsDisjointQuery = nullptr;
} ofr;
struct TextureReadback {

File diff suppressed because it is too large Load Diff

View File

@ -359,10 +359,10 @@ namespace {
{
}
inline void addKey(const void *key, const QByteArray &fontData)
inline void addKey(const QByteArray &fontData)
{
Q_ASSERT(!m_fontDatas.contains(key));
m_fontDatas.insert(key, fontData);
if (!m_fontDatas.contains(fontData.data()))
m_fontDatas.insert(fontData.data(), fontData);
}
inline void removeKey(const void *key)
@ -378,6 +378,11 @@ namespace {
UINT32 fontFileReferenceKeySize,
OUT IDWriteFontFileStream **fontFileStream) override;
void clear()
{
m_fontDatas.clear();
}
private:
ULONG m_referenceCount;
QHash<const void *, QByteArray> m_fontDatas;
@ -435,53 +440,63 @@ namespace {
return S_OK;
}
class CustomFontFileLoader
{
public:
CustomFontFileLoader(IDWriteFactory *factory)
{
m_directWriteFactory = factory;
if (m_directWriteFactory) {
m_directWriteFactory->AddRef();
m_directWriteFontFileLoader = new DirectWriteFontFileLoader();
m_directWriteFactory->RegisterFontFileLoader(m_directWriteFontFileLoader);
}
}
~CustomFontFileLoader()
{
if (m_directWriteFactory != nullptr && m_directWriteFontFileLoader != nullptr)
m_directWriteFactory->UnregisterFontFileLoader(m_directWriteFontFileLoader);
if (m_directWriteFactory != nullptr)
m_directWriteFactory->Release();
}
void addKey(const void *key, const QByteArray &fontData)
{
if (m_directWriteFontFileLoader != nullptr)
m_directWriteFontFileLoader->addKey(key, fontData);
}
void removeKey(const void *key)
{
if (m_directWriteFontFileLoader != nullptr)
m_directWriteFontFileLoader->removeKey(key);
}
IDWriteFontFileLoader *loader() const
{
return m_directWriteFontFileLoader;
}
private:
IDWriteFactory *m_directWriteFactory = nullptr;
DirectWriteFontFileLoader *m_directWriteFontFileLoader = nullptr;
};
} // Anonymous namespace
class QCustomFontFileLoader
{
public:
QCustomFontFileLoader(IDWriteFactory *factory)
{
m_directWriteFactory = factory;
if (m_directWriteFactory) {
m_directWriteFactory->AddRef();
m_directWriteFontFileLoader = new DirectWriteFontFileLoader();
m_directWriteFactory->RegisterFontFileLoader(m_directWriteFontFileLoader);
}
}
~QCustomFontFileLoader()
{
clear();
if (m_directWriteFactory != nullptr && m_directWriteFontFileLoader != nullptr)
m_directWriteFactory->UnregisterFontFileLoader(m_directWriteFontFileLoader);
if (m_directWriteFactory != nullptr)
m_directWriteFactory->Release();
}
void addKey(const QByteArray &fontData)
{
if (m_directWriteFontFileLoader != nullptr)
m_directWriteFontFileLoader->addKey(fontData);
}
void removeKey(const void *key)
{
if (m_directWriteFontFileLoader != nullptr)
m_directWriteFontFileLoader->removeKey(key);
}
IDWriteFontFileLoader *loader() const
{
return m_directWriteFontFileLoader;
}
void clear()
{
if (m_directWriteFontFileLoader != nullptr)
m_directWriteFontFileLoader->clear();
}
private:
IDWriteFactory *m_directWriteFactory = nullptr;
DirectWriteFontFileLoader *m_directWriteFontFileLoader = nullptr;
};
#endif // directwrite && direct2d
@ -550,12 +565,27 @@ void QWindowsFontDatabaseBase::createDirectWriteFactory(IDWriteFactory **factory
IUnknown *result = nullptr;
# if QT_CONFIG(directwrite3)
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory3), &result);
# endif
if (result == nullptr)
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory2), &result);
qCDebug(lcQpaFonts) << "Trying to create IDWriteFactory6";
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory6), &result);
if (result == nullptr) {
qCDebug(lcQpaFonts) << "Trying to create IDWriteFactory5";
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory5), &result);
}
if (result == nullptr) {
qCDebug(lcQpaFonts) << "Trying to create IDWriteFactory3";
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory3), &result);
}
# endif
if (result == nullptr) {
qCDebug(lcQpaFonts) << "Trying to create IDWriteFactory2";
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory2), &result);
}
if (result == nullptr) {
qCDebug(lcQpaFonts) << "Trying to create plain IDWriteFactory";
if (FAILED(DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), &result))) {
qErrnoWarning("DWriteCreateFactory failed");
return;
@ -700,28 +730,47 @@ QFont QWindowsFontDatabaseBase::systemDefaultFont()
return systemFont;
}
#if QT_CONFIG(directwrite) && QT_CONFIG(direct2d)
IDWriteFontFace *QWindowsFontDatabaseBase::createDirectWriteFace(const QByteArray &fontData) const
void QWindowsFontDatabaseBase::invalidate()
{
#if QT_CONFIG(directwrite)
m_fontFileLoader.reset(nullptr);
#endif
}
#if QT_CONFIG(directwrite) && QT_CONFIG(direct2d)
IDWriteFontFace *QWindowsFontDatabaseBase::createDirectWriteFace(const QByteArray &fontData)
{
QList<IDWriteFontFace *> faces = createDirectWriteFaces(fontData, false);
Q_ASSERT(faces.size() <= 1);
return faces.isEmpty() ? nullptr : faces.first();
}
QList<IDWriteFontFace *> QWindowsFontDatabaseBase::createDirectWriteFaces(const QByteArray &fontData,
bool queryVariations) const
{
QList<IDWriteFontFace *> ret;
QSharedPointer<QWindowsFontEngineData> fontEngineData = data();
if (fontEngineData->directWriteFactory == nullptr) {
qCWarning(lcQpaFonts) << "DirectWrite factory not created in QWindowsFontDatabaseBase::createDirectWriteFace()";
return nullptr;
return ret;
}
CustomFontFileLoader fontFileLoader(fontEngineData->directWriteFactory);
fontFileLoader.addKey(this, fontData);
if (m_fontFileLoader == nullptr)
m_fontFileLoader.reset(new QCustomFontFileLoader(fontEngineData->directWriteFactory));
m_fontFileLoader->addKey(fontData);
IDWriteFontFile *fontFile = nullptr;
const void *key = this;
const void *key = fontData.data();
HRESULT hres = fontEngineData->directWriteFactory->CreateCustomFontFileReference(&key,
sizeof(void *),
fontFileLoader.loader(),
m_fontFileLoader->loader(),
&fontFile);
if (FAILED(hres)) {
qErrnoWarning(hres, "%s: CreateCustomFontFileReference failed", __FUNCTION__);
return nullptr;
return ret;
}
BOOL isSupportedFontType;
@ -731,25 +780,65 @@ IDWriteFontFace *QWindowsFontDatabaseBase::createDirectWriteFace(const QByteArra
fontFile->Analyze(&isSupportedFontType, &fontFileType, &fontFaceType, &numberOfFaces);
if (!isSupportedFontType) {
fontFile->Release();
return nullptr;
return ret;
}
#if QT_CONFIG(directwrite3)
IDWriteFactory5 *factory5 = nullptr;
if (queryVariations && SUCCEEDED(fontEngineData->directWriteFactory->QueryInterface(__uuidof(IDWriteFactory5),
reinterpret_cast<void **>(&factory5)))) {
IDWriteFontSetBuilder1 *builder;
if (SUCCEEDED(factory5->CreateFontSetBuilder(&builder))) {
if (SUCCEEDED(builder->AddFontFile(fontFile))) {
IDWriteFontSet *fontSet;
if (SUCCEEDED(builder->CreateFontSet(&fontSet))) {
int count = fontSet->GetFontCount();
qCDebug(lcQpaFonts) << "Found" << count << "variations in font file";
for (int i = 0; i < count; ++i) {
IDWriteFontFaceReference *ref;
if (SUCCEEDED(fontSet->GetFontFaceReference(i, &ref))) {
IDWriteFontFace3 *face;
if (SUCCEEDED(ref->CreateFontFace(&face))) {
ret.append(face);
}
ref->Release();
}
}
fontSet->Release();
}
}
builder->Release();
}
factory5->Release();
}
#else
Q_UNUSED(queryVariations);
#endif
// ### Currently no support for .ttc, but we could easily return a list here.
IDWriteFontFace *directWriteFontFace = nullptr;
hres = fontEngineData->directWriteFactory->CreateFontFace(fontFaceType,
1,
&fontFile,
0,
DWRITE_FONT_SIMULATIONS_NONE,
&directWriteFontFace);
if (FAILED(hres)) {
qErrnoWarning(hres, "%s: CreateFontFace failed", __FUNCTION__);
fontFile->Release();
return nullptr;
if (ret.isEmpty()) {
IDWriteFontFace *directWriteFontFace = nullptr;
hres = fontEngineData->directWriteFactory->CreateFontFace(fontFaceType,
1,
&fontFile,
0,
DWRITE_FONT_SIMULATIONS_NONE,
&directWriteFontFace);
if (FAILED(hres)) {
qErrnoWarning(hres, "%s: CreateFontFace failed", __FUNCTION__);
fontFile->Release();
return ret;
} else {
ret.append(directWriteFontFace);
}
}
fontFile->Release();
return directWriteFontFace;
return ret;
}
#endif // directwrite && direct2d
@ -769,7 +858,10 @@ QFontEngine *QWindowsFontDatabaseBase::fontEngine(const QByteArray &fontData, qr
if (fontEngineData->directWriteFactory == nullptr)
return nullptr;
IDWriteFontFace *directWriteFontFace = createDirectWriteFace(fontData);
IDWriteFontFace * directWriteFontFace = createDirectWriteFace(fontData);
if (directWriteFontFace == nullptr)
return nullptr;
fontEngine = new QWindowsFontEngineDirectWrite(directWriteFontFace,
pixelSize,
fontEngineData);

View File

@ -7,7 +7,6 @@
#include "qwindowswindow.h"
#include "qwindowskeymapper.h"
#include "qwindowsnativeinterface.h"
#include "qwindowsmousehandler.h"
#include "qwindowspointerhandler.h"
#include "qtwindowsglobal.h"
#include "qwindowsmenu.h"
@ -46,10 +45,13 @@
#include <QtCore/quuid.h>
#include <QtCore/private/qsystemlibrary_p.h>
#include <QtCore/private/qwinregistry_p.h>
#include <QtCore/private/qfactorycacheregistration_p.h>
#if QT_CONFIG(cpp_winrt)
# include <QtCore/private/qfactorycacheregistration_p.h>
#endif
#include <QtCore/private/qsystemerror_p.h>
#include <QtGui/private/qwindowsguieventdispatcher_p.h>
#include <QtGui/private/qwindowsthemecache_p.h>
#include <stdlib.h>
#include <stdio.h>
@ -58,6 +60,8 @@
#include <wtsapi32.h>
#include <shellscalingapi.h>
#include "vxkex.h"
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
@ -210,7 +214,6 @@ struct QWindowsContextPrivate {
HDC m_displayContext = nullptr;
int m_defaultDPI = 96;
QWindowsKeyMapper m_keyMapper;
QWindowsMouseHandler m_mouseHandler;
QWindowsPointerHandler m_pointerHandler;
QWindowsMimeRegistry m_mimeConverter;
QWindowsScreenManager m_screenManager;
@ -223,11 +226,9 @@ struct QWindowsContextPrivate {
bool m_asyncExpose = false;
HPOWERNOTIFY m_powerNotification = nullptr;
HWND m_powerDummyWindow = nullptr;
static bool m_darkMode;
static bool m_v2DpiAware;
};
bool QWindowsContextPrivate::m_darkMode = false;
bool QWindowsContextPrivate::m_v2DpiAware = false;
QWindowsContextPrivate::QWindowsContextPrivate()
@ -236,7 +237,7 @@ QWindowsContextPrivate::QWindowsContextPrivate()
QWindowsContext::user32dll.init();
QWindowsContext::shcoredll.init();
if (m_pointerHandler.touchDevice() || m_mouseHandler.touchDevice())
if (m_pointerHandler.touchDevice())
m_systemInfo |= QWindowsContext::SI_SupportsTouch;
m_displayContext = GetDC(nullptr);
m_defaultDPI = GetDeviceCaps(m_displayContext, LOGPIXELSY);
@ -244,7 +245,6 @@ QWindowsContextPrivate::QWindowsContextPrivate()
m_systemInfo |= QWindowsContext::SI_RTL_Extensions;
m_keyMapper.setUseRTLExtensions(true);
}
m_darkMode = QWindowsTheme::queryDarkMode();
if (FAILED(m_oleInitializeResult)) {
qWarning() << "QWindowsContext: OleInitialize() failed: "
<< QSystemError::windowsComString(m_oleInitializeResult);
@ -276,6 +276,8 @@ QWindowsContext::~QWindowsContext()
if (d->m_powerDummyWindow)
DestroyWindow(d->m_powerDummyWindow);
d->m_screenManager.destroyWindow();
unregisterWindowClasses();
if (d->m_oleInitializeResult == S_OK || d->m_oleInitializeResult == S_FALSE) {
#ifdef QT_USE_FACTORY_CACHE_REGISTRATION
@ -299,8 +301,7 @@ bool QWindowsContext::initTouch(unsigned integrationOptions)
{
if (d->m_systemInfo & QWindowsContext::SI_SupportsTouch)
return true;
const bool usePointerHandler = (d->m_systemInfo & QWindowsContext::SI_SupportsPointer) != 0;
auto touchDevice = usePointerHandler ? d->m_pointerHandler.touchDevice() : d->m_mouseHandler.touchDevice();
auto touchDevice = d->m_pointerHandler.touchDevice();
if (touchDevice.isNull()) {
const bool mouseEmulation =
(integrationOptions & QWindowsIntegration::DontPassOsMouseEventsSynthesizedFromTouch) == 0;
@ -309,7 +310,6 @@ bool QWindowsContext::initTouch(unsigned integrationOptions)
if (touchDevice.isNull())
return false;
d->m_pointerHandler.setTouchDevice(touchDevice);
d->m_mouseHandler.setTouchDevice(touchDevice);
QWindowSystemInterface::registerInputDevice(touchDevice.data());
d->m_systemInfo |= QWindowsContext::SI_SupportsTouch;
@ -349,21 +349,6 @@ bool QWindowsContext::disposeTablet()
#endif
}
bool QWindowsContext::initPointer(unsigned integrationOptions)
{
if (integrationOptions & QWindowsIntegration::DontUseWMPointer)
return false;
if (QOperatingSystemVersion::current() < QOperatingSystemVersion::Windows8)
return false;
if (!QWindowsContext::user32dll.supportsPointerApi())
return false;
d->m_systemInfo |= QWindowsContext::SI_SupportsPointer;
return true;
}
extern "C" LRESULT QT_WIN_CALLBACK qWindowsPowerWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message != WM_POWERBROADCAST || wParam != PBT_POWERSETTINGCHANGE)
@ -445,11 +430,25 @@ void QWindowsContext::setDetectAltGrModifier(bool a)
return QtWindows::DpiAwareness::System;
if (QWindowsContext::user32dll.areDpiAwarenessContextsEqual(context, DPI_AWARENESS_CONTEXT_UNAWARE))
return QtWindows::DpiAwareness::Unaware;
return QtWindows::DpiAwareness::Invalid;
}
else
{
// IsValidDpiAwarenessContext() will handle the NULL pointer case.
if (!vxkex::IsValidDpiAwarenessContext(context))
return QtWindows::DpiAwareness::Invalid;
if (vxkex::AreDpiAwarenessContextsEqual(context, DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED))
return QtWindows::DpiAwareness::Unaware_GdiScaled;
if (vxkex::AreDpiAwarenessContextsEqual(context, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2))
return QtWindows::DpiAwareness::PerMonitorVersion2;
if (vxkex::AreDpiAwarenessContextsEqual(context, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE))
return QtWindows::DpiAwareness::PerMonitor;
if (vxkex::AreDpiAwarenessContextsEqual(context, DPI_AWARENESS_CONTEXT_SYSTEM_AWARE))
return QtWindows::DpiAwareness::System;
if (vxkex::AreDpiAwarenessContextsEqual(context, DPI_AWARENESS_CONTEXT_UNAWARE))
return QtWindows::DpiAwareness::Unaware;
}
return QtWindows::DpiAwareness::Unaware; // Windows 7
return QtWindows::DpiAwareness::Invalid;
}
QtWindows::DpiAwareness QWindowsContext::windowDpiAwareness(HWND hwnd)
@ -457,31 +456,26 @@ QtWindows::DpiAwareness QWindowsContext::windowDpiAwareness(HWND hwnd)
if (!hwnd)
return QtWindows::DpiAwareness::Invalid;
if (QWindowsContext::user32dll.getWindowDpiAwarenessContext)
{
const auto context = QWindowsContext::user32dll.getWindowDpiAwarenessContext(hwnd);
return dpiAwarenessContextToQtDpiAwareness(context);
}
const auto context = QWindowsContext::user32dll.getWindowDpiAwarenessContext ?
QWindowsContext::user32dll.getWindowDpiAwarenessContext(hwnd) :
vxkex::GetWindowDpiAwarenessContext(hwnd);
return dpiAwarenessContextToQtDpiAwareness(DPI_AWARENESS_CONTEXT_UNAWARE);
return dpiAwarenessContextToQtDpiAwareness(context);
}
QtWindows::DpiAwareness QWindowsContext::processDpiAwareness()
{
if (QWindowsContext::user32dll.getThreadDpiAwarenessContext)
{
// Although we have GetDpiAwarenessContextForProcess(), however,
// it's only available on Win10 1903+, which is a little higher
// than Qt's minimum supported version (1809), so we can't use it.
// Luckily, MS docs said GetThreadDpiAwarenessContext() will also
// return the default DPI_AWARENESS_CONTEXT for the process if
// SetThreadDpiAwarenessContext() was never called. So we can use
// it as an equivalent.
const auto context = QWindowsContext::user32dll.getThreadDpiAwarenessContext();
return dpiAwarenessContextToQtDpiAwareness(context);
}
return dpiAwarenessContextToQtDpiAwareness(DPI_AWARENESS_CONTEXT_UNAWARE);
// Although we have GetDpiAwarenessContextForProcess(), however,
// it's only available on Win10 1903+, which is a little higher
// than Qt's minimum supported version (1809), so we can't use it.
// Luckily, MS docs said GetThreadDpiAwarenessContext() will also
// return the default DPI_AWARENESS_CONTEXT for the process if
// SetThreadDpiAwarenessContext() was never called. So we can use
// it as an equivalent.
const DPI_AWARENESS_CONTEXT context = QWindowsContext::user32dll.getThreadDpiAwarenessContext ?
QWindowsContext::user32dll.getThreadDpiAwarenessContext() :
vxkex::GetThreadDpiAwarenessContext();
return dpiAwarenessContextToQtDpiAwareness(context);
}
[[nodiscard]] static inline DPI_AWARENESS_CONTEXT
@ -541,33 +535,32 @@ bool QWindowsContext::setProcessDpiAwareness(QtWindows::DpiAwareness dpiAwarenes
return true;
const auto context = qtDpiAwarenessToDpiAwarenessContext(dpiAwareness);
if (QWindowsContext::user32dll.isValidDpiAwarenessContext && QWindowsContext::user32dll.setProcessDpiAwarenessContext)
{
if (!QWindowsContext::user32dll.isValidDpiAwarenessContext(context)) {
qCWarning(lcQpaWindow) << dpiAwareness << "is not supported by current system.";
return false;
}
if (!QWindowsContext::user32dll.setProcessDpiAwarenessContext(context)) {
qCWarning(lcQpaWindow).noquote().nospace()
<< "SetProcessDpiAwarenessContext() failed: "
<< QSystemError::windowsString()
<< "\nQt's default DPI awareness context is "
<< "DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2. If you know what you "
<< "are doing, you can overwrite this default using qt.conf "
<< "(https://doc.qt.io/qt-6/highdpi.html#configuring-windows).";
return false;
}
QWindowsContextPrivate::m_v2DpiAware
= processDpiAwareness() == QtWindows::DpiAwareness::PerMonitorVersion2;
return true;
BOOL bResultIsValid = QWindowsContext::user32dll.isValidDpiAwarenessContext ?
QWindowsContext::user32dll.isValidDpiAwarenessContext(context) :
vxkex::IsValidDpiAwarenessContext(context);
if (!bResultIsValid) {
qCWarning(lcQpaWindow) << dpiAwareness << "is not supported by current system.";
return false;
}
return false; // Windows 7
}
BOOL bResultSet = QWindowsContext::user32dll.setProcessDpiAwarenessContext ?
QWindowsContext::user32dll.setProcessDpiAwarenessContext(context) :
vxkex::SetProcessDpiAwarenessContext(context);
bool QWindowsContext::isDarkMode()
{
return QWindowsContextPrivate::m_darkMode;
if (!bResultSet) {
qCWarning(lcQpaWindow).noquote().nospace()
<< "SetProcessDpiAwarenessContext() failed: "
<< QSystemError::windowsString()
<< "\nQt's default DPI awareness context is "
<< "DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2. If you know what you "
<< "are doing, you can overwrite this default using qt.conf "
<< "(https://doc.qt.io/qt-6/highdpi.html#configuring-windows).";
return false;
}
QWindowsContextPrivate::m_v2DpiAware
= processDpiAwareness() == QtWindows::DpiAwareness::PerMonitorVersion2;
return true;
}
QWindowsContext *QWindowsContext::instance()
@ -585,9 +578,9 @@ bool QWindowsContext::useRTLExtensions() const
return d->m_keyMapper.useRTLExtensions();
}
QList<int> QWindowsContext::possibleKeys(const QKeyEvent *e) const
QPlatformKeyMapper *QWindowsContext::keyMapper() const
{
return d->m_keyMapper.possibleKeys(e);
return &d->m_keyMapper;
}
QWindowsContext::HandleBaseWindowHash &QWindowsContext::windows()
@ -837,16 +830,12 @@ QWindow *QWindowsContext::findWindow(HWND hwnd) const
QWindow *QWindowsContext::windowUnderMouse() const
{
return (d->m_systemInfo & QWindowsContext::SI_SupportsPointer) ?
d->m_pointerHandler.windowUnderMouse() : d->m_mouseHandler.windowUnderMouse();
return d->m_pointerHandler.windowUnderMouse();
}
void QWindowsContext::clearWindowUnderMouse()
{
if (d->m_systemInfo & QWindowsContext::SI_SupportsPointer)
d->m_pointerHandler.clearWindowUnderMouse();
else
d->m_mouseHandler.clearWindowUnderMouse();
d->m_pointerHandler.clearWindowUnderMouse();
}
/*!
@ -994,7 +983,7 @@ bool QWindowsContext::systemParametersInfo(unsigned action, unsigned param, void
{
const BOOL result = (QWindowsContext::user32dll.systemParametersInfoForDpi != nullptr && dpi != 0)
? QWindowsContext::user32dll.systemParametersInfoForDpi(action, param, out, 0, dpi)
: SystemParametersInfo(action, param, out, 0);
: vxkex::SystemParametersInfoForDpi(action, param, out, 0, dpi);
return result == TRUE;
}
@ -1080,8 +1069,11 @@ static inline bool isInputMessage(UINT m)
static bool enableNonClientDpiScaling(HWND hwnd)
{
bool result = false;
if (QWindowsContext::user32dll.enableNonClientDpiScaling && QWindowsContext::windowDpiAwareness(hwnd) == QtWindows::DpiAwareness::PerMonitor) {
result = QWindowsContext::user32dll.enableNonClientDpiScaling(hwnd) != FALSE;
if (QWindowsContext::windowDpiAwareness(hwnd) == QtWindows::DpiAwareness::PerMonitor) {
result = QWindowsContext::user32dll.enableNonClientDpiScaling ?
(QWindowsContext::user32dll.enableNonClientDpiScaling(hwnd) != FALSE) :
(vxkex::EnableNonClientDpiScaling(hwnd) != FALSE);
if (!result) {
const DWORD errorCode = GetLastError();
qErrnoWarning(int(errorCode), "EnableNonClientDpiScaling() failed for HWND %p (%lu)",
@ -1107,9 +1099,10 @@ bool QWindowsContext::windowsProc(HWND hwnd, UINT message,
MSG msg;
msg.hwnd = hwnd; // re-create MSG structure
msg.message = message; // time and pt fields ignored
msg.message = message;
msg.wParam = wParam;
msg.lParam = lParam;
msg.time = GetMessageTime();
msg.pt.x = msg.pt.y = 0;
if (et != QtWindows::CursorEvent && (et & (QtWindows::MouseEventFlag | QtWindows::NonClientEventFlag))) {
msg.pt.x = GET_X_LPARAM(lParam);
@ -1158,8 +1151,7 @@ bool QWindowsContext::windowsProc(HWND hwnd, UINT message,
switch (et) {
case QtWindows::GestureEvent:
if (!(d->m_systemInfo & QWindowsContext::SI_SupportsPointer))
return sessionManagerInteractionBlocked() || d->m_mouseHandler.translateGestureEvent(platformWindow->window(), hwnd, et, msg, result);
// TODO???
break;
case QtWindows::InputMethodOpenCandidateWindowEvent:
case QtWindows::InputMethodCloseCandidateWindowEvent:
@ -1193,21 +1185,7 @@ bool QWindowsContext::windowsProc(HWND hwnd, UINT message,
// Only refresh the window theme if the user changes the personalize settings.
if ((wParam == 0) && (lParam != 0) // lParam sometimes may be NULL.
&& (wcscmp(reinterpret_cast<LPCWSTR>(lParam), L"ImmersiveColorSet") == 0)) {
const bool darkMode = QWindowsTheme::queryDarkMode();
const bool darkModeChanged = darkMode != QWindowsContextPrivate::m_darkMode;
QWindowsContextPrivate::m_darkMode = darkMode;
auto integration = QWindowsIntegration::instance();
integration->updateApplicationBadge();
if (integration->darkModeHandling().testFlag(QWindowsApplication::DarkModeStyle)) {
QWindowsTheme::instance()->refresh();
QWindowSystemInterface::handleThemeChange();
}
if (darkModeChanged) {
if (integration->darkModeHandling().testFlag(QWindowsApplication::DarkModeWindowFrames)) {
for (QWindowsWindow *w : d->m_windows)
w->setDarkBorder(QWindowsContextPrivate::m_darkMode);
}
}
QWindowsTheme::handleSettingsChanged();
}
return d->m_screenManager.handleScreenChanges();
}
@ -1267,7 +1245,7 @@ bool QWindowsContext::windowsProc(HWND hwnd, UINT message,
if (wParam == DBT_DEVNODES_CHANGED)
initTouch();
break;
case QtWindows::KeyboardLayoutChangeEvent:
case QtWindows::InputLanguageChangeEvent:
if (QWindowsInputContext *wic = windowsInputContext())
wic->handleInputLanguageChanged(wParam, lParam);
Q_FALLTHROUGH();
@ -1313,16 +1291,11 @@ bool QWindowsContext::windowsProc(HWND hwnd, UINT message,
case QtWindows::NonClientMouseEvent:
if (!platformWindow->frameStrutEventsEnabled())
break;
if ((d->m_systemInfo & QWindowsContext::SI_SupportsPointer))
return sessionManagerInteractionBlocked() || d->m_pointerHandler.translateMouseEvent(platformWindow->window(), hwnd, et, msg, result);
else
return sessionManagerInteractionBlocked() || d->m_mouseHandler.translateMouseEvent(platformWindow->window(), hwnd, et, msg, result);
return sessionManagerInteractionBlocked() || d->m_pointerHandler.translateMouseEvent(platformWindow->window(), hwnd, et, msg, result);
case QtWindows::NonClientPointerEvent:
if (!platformWindow->frameStrutEventsEnabled())
break;
if ((d->m_systemInfo & QWindowsContext::SI_SupportsPointer))
return sessionManagerInteractionBlocked() || d->m_pointerHandler.translatePointerEvent(platformWindow->window(), hwnd, et, msg, result);
break;
return sessionManagerInteractionBlocked() || d->m_pointerHandler.translatePointerEvent(platformWindow->window(), hwnd, et, msg, result);
case QtWindows::EnterSizeMoveEvent:
platformWindow->setFlag(QWindowsWindow::ResizeMoveActive);
if (!IsZoomed(hwnd))
@ -1336,8 +1309,7 @@ bool QWindowsContext::windowsProc(HWND hwnd, UINT message,
platformWindow->updateRestoreGeometry();
return true;
case QtWindows::ScrollEvent:
if (!(d->m_systemInfo & QWindowsContext::SI_SupportsPointer))
return sessionManagerInteractionBlocked() || d->m_mouseHandler.translateScrollEvent(platformWindow->window(), hwnd, msg, result);
// TODO???
break;
case QtWindows::MouseWheelEvent:
case QtWindows::MouseEvent:
@ -1348,20 +1320,14 @@ bool QWindowsContext::windowsProc(HWND hwnd, UINT message,
window = window->parent();
if (!window)
return false;
if (d->m_systemInfo & QWindowsContext::SI_SupportsPointer)
return sessionManagerInteractionBlocked() || d->m_pointerHandler.translateMouseEvent(window, hwnd, et, msg, result);
else
return sessionManagerInteractionBlocked() || d->m_mouseHandler.translateMouseEvent(window, hwnd, et, msg, result);
return sessionManagerInteractionBlocked() || d->m_pointerHandler.translateMouseEvent(window, hwnd, et, msg, result);
}
break;
case QtWindows::TouchEvent:
if (!(d->m_systemInfo & QWindowsContext::SI_SupportsPointer))
return sessionManagerInteractionBlocked() || d->m_mouseHandler.translateTouchEvent(platformWindow->window(), hwnd, et, msg, result);
// TODO???
break;
case QtWindows::PointerEvent:
if (d->m_systemInfo & QWindowsContext::SI_SupportsPointer)
return sessionManagerInteractionBlocked() || d->m_pointerHandler.translatePointerEvent(platformWindow->window(), hwnd, et, msg, result);
break;
return sessionManagerInteractionBlocked() || d->m_pointerHandler.translatePointerEvent(platformWindow->window(), hwnd, et, msg, result);
case QtWindows::FocusInEvent: // see QWindowsWindow::requestActivateWindow().
case QtWindows::FocusOutEvent:
handleFocusEvent(et, platformWindow);
@ -1379,6 +1345,7 @@ bool QWindowsContext::windowsProc(HWND hwnd, UINT message,
QWindowSystemInterface::handleCloseEvent(platformWindow->window());
return true;
case QtWindows::ThemeChanged: {
QWindowsThemeCache::clearThemeCache(platformWindow->handle());
// Switch from Aero to Classic changes margins.
if (QWindowsTheme *theme = QWindowsTheme::instance())
theme->windowsThemeChanged(platformWindow->window());
@ -1517,7 +1484,7 @@ void QWindowsContext::handleFocusEvent(QtWindows::WindowsEventType et,
}
if (nextActiveWindow != d->m_lastActiveWindow) {
d->m_lastActiveWindow = nextActiveWindow;
QWindowSystemInterface::handleWindowActivated(nextActiveWindow, Qt::ActiveWindowFocusReason);
QWindowSystemInterface::handleFocusWindowChanged(nextActiveWindow, Qt::ActiveWindowFocusReason);
}
}
@ -1547,7 +1514,7 @@ bool QWindowsContext::handleContextMenuEvent(QWindow *window, const MSG &msg)
}
QWindowSystemInterface::handleContextMenuEvent(window, mouseTriggered, pos, globalPos,
QWindowsKeyMapper::queryKeyboardModifiers());
keyMapper()->queryKeyboardModifiers());
return true;
}
#endif
@ -1564,11 +1531,11 @@ void QWindowsContext::handleExitSizeMove(QWindow *window)
// Mouse is left in pressed state after press on size grip (inside window),
// no further mouse events are received
// For cases 1,3, intercept WM_EXITSIZEMOVE to sync the buttons.
const Qt::MouseButtons currentButtons = QWindowsMouseHandler::queryMouseButtons();
const Qt::MouseButtons currentButtons = QWindowsPointerHandler::queryMouseButtons();
const Qt::MouseButtons appButtons = QGuiApplication::mouseButtons();
if (currentButtons == appButtons)
return;
const Qt::KeyboardModifiers keyboardModifiers = QWindowsKeyMapper::queryKeyboardModifiers();
const Qt::KeyboardModifiers keyboardModifiers = keyMapper()->queryKeyboardModifiers();
const QPoint globalPos = QWindowsCursor::mousePosition();
const QPlatformWindow *platWin = window->handle();
const QPoint localPos = platWin->mapFromGlobal(globalPos);
@ -1580,10 +1547,7 @@ void QWindowsContext::handleExitSizeMove(QWindow *window)
currentButtons, button, type, keyboardModifiers);
}
}
if (d->m_systemInfo & QWindowsContext::SI_SupportsPointer)
d->m_pointerHandler.clearEvents();
else
d->m_mouseHandler.clearEvents();
d->m_pointerHandler.clearEvents();
}
bool QWindowsContext::asyncExpose() const
@ -1598,11 +1562,9 @@ void QWindowsContext::setAsyncExpose(bool value)
DWORD QWindowsContext::readAdvancedExplorerSettings(const wchar_t *subKey, DWORD defaultValue)
{
const auto value =
QWinRegistryKey(HKEY_CURRENT_USER,
LR"(Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced)")
.dwordValue(subKey);
return value.second ? value.first : defaultValue;
const auto advancedSettings = QWinRegistryKey(
HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced)");
return advancedSettings.value<DWORD>(subKey).value_or(defaultValue);
}
static inline bool isEmptyRect(const RECT &rect)

View File

@ -34,6 +34,7 @@ Q_DECLARE_LOGGING_CATEGORY(lcQpaScreen)
class QWindow;
class QPlatformScreen;
class QPlatformWindow;
class QPlatformKeyMapper;
class QWindowsMenuBar;
class QWindowsScreenManager;
class QWindowsTabletSupport;
@ -142,8 +143,7 @@ public:
enum SystemInfoFlags
{
SI_RTL_Extensions = 0x1,
SI_SupportsTouch = 0x2,
SI_SupportsPointer = 0x4,
SI_SupportsTouch = 0x2
};
// Verbose flag set by environment variable QT_QPA_VERBOSE
@ -156,7 +156,6 @@ public:
bool initTouch(unsigned integrationOptions); // For calls from QWindowsIntegration::QWindowsIntegration() only.
void registerTouchWindows();
bool initTablet();
bool initPointer(unsigned integrationOptions);
bool disposeTablet();
bool initPowerNotificationHandler();
@ -209,15 +208,13 @@ public:
static QtWindows::DpiAwareness processDpiAwareness();
static QtWindows::DpiAwareness windowDpiAwareness(HWND hwnd);
static bool isDarkMode();
void setDetectAltGrModifier(bool a);
// Returns a combination of SystemInfoFlags
unsigned systemInfo() const;
bool useRTLExtensions() const;
QList<int> possibleKeys(const QKeyEvent *e) const;
QPlatformKeyMapper *keyMapper() const;
HandleBaseWindowHash &windows();

View File

@ -11,7 +11,7 @@
#include "qwindowsintegration.h"
#include "qwindowsdropdataobject.h"
#include "qwindowswindow.h"
#include "qwindowsmousehandler.h"
#include "qwindowspointerhandler.h"
#include "qwindowscursor.h"
#include "qwindowskeymapper.h"
@ -28,9 +28,13 @@
#include <QtCore/qdebug.h>
#include <QtCore/qbuffer.h>
#include <QtCore/qpoint.h>
#include <QtCore/qpointer.h>
#include <QtCore/private/qcomobject_p.h>
#include <shlobj.h>
#include "vxkex.h"
QT_BEGIN_NAMESPACE
/*!
@ -167,7 +171,7 @@ static Qt::MouseButtons lastButtons = Qt::NoButton;
\internal
*/
class QWindowsOleDropSource : public QWindowsComBase<IDropSource>
class QWindowsOleDropSource : public QComObject<IDropSource>
{
public:
enum Mode {
@ -252,8 +256,9 @@ void QWindowsOleDropSource::createCursors()
if (const QScreen *primaryScreen = QGuiApplication::primaryScreen())
platformScreen = primaryScreen->handle();
}
Q_ASSERT(platformScreen);
QPlatformCursor *platformCursor = platformScreen->cursor();
QPlatformCursor *platformCursor = nullptr;
if (platformScreen)
platformCursor = platformScreen->cursor();
if (GetSystemMetrics (SM_REMOTESESSION) != 0) {
/* Workaround for RDP issues with large cursors.
@ -270,7 +275,7 @@ void QWindowsOleDropSource::createCursors()
hotSpotScaleFactor = QHighDpiScaling::factor(platformScreen);
pixmapScaleFactor = hotSpotScaleFactor / pixmap.devicePixelRatio();
}
QPixmap scaledPixmap = qFuzzyCompare(pixmapScaleFactor, 1.0)
QPixmap scaledPixmap = (!hasPixmap || qFuzzyCompare(pixmapScaleFactor, 1.0))
? pixmap
: pixmap.scaled((QSizeF(pixmap.size()) * pixmapScaleFactor).toSize(),
Qt::KeepAspectRatio, Qt::SmoothTransformation);
@ -339,7 +344,7 @@ QWindowsOleDropSource::QueryContinueDrag(BOOL fEscapePressed, DWORD grfKeyState)
// In some rare cases, when a mouse button is released but the mouse is static,
// grfKeyState will not be updated with these released buttons until the mouse
// is moved. So we use the async key state given by queryMouseButtons() instead.
Qt::MouseButtons buttons = QWindowsMouseHandler::queryMouseButtons();
Qt::MouseButtons buttons = QWindowsPointerHandler::queryMouseButtons();
SCODE result = S_OK;
if (fEscapePressed || QWindowsDrag::isCanceled()) {
@ -364,7 +369,7 @@ QWindowsOleDropSource::QueryContinueDrag(BOOL fEscapePressed, DWORD grfKeyState)
const QPoint localPos = m_windowUnderMouse->handle()->mapFromGlobal(globalPos);
QWindowSystemInterface::handleMouseEvent(m_windowUnderMouse.data(),
QPointF(localPos), QPointF(globalPos),
QWindowsMouseHandler::queryMouseButtons(),
QWindowsPointerHandler::queryMouseButtons(),
Qt::LeftButton, QEvent::MouseButtonRelease);
}
m_currentButtons = Qt::NoButton;
@ -458,7 +463,7 @@ void QWindowsOleDropTarget::handleDrag(QWindow *window, DWORD grfKeyState,
const Qt::DropActions actions = translateToQDragDropActions(*pdwEffect);
lastModifiers = toQtKeyboardModifiers(grfKeyState);
lastButtons = QWindowsMouseHandler::queryMouseButtons();
lastButtons = QWindowsPointerHandler::queryMouseButtons();
const QPlatformDragQtResponse response =
QWindowSystemInterface::handleDrag(window, windowsDrag->dropData(),
@ -526,8 +531,9 @@ QWindowsOleDropTarget::DragLeave()
qCDebug(lcQpaMime) << __FUNCTION__ << ' ' << m_window;
lastModifiers = QWindowsKeyMapper::queryKeyboardModifiers();
lastButtons = QWindowsMouseHandler::queryMouseButtons();
const auto *keyMapper = QWindowsContext::instance()->keyMapper();
lastModifiers = keyMapper->queryKeyboardModifiers();
lastButtons = QWindowsPointerHandler::queryMouseButtons();
QWindowSystemInterface::handleDrag(m_window, nullptr, QPoint(), Qt::IgnoreAction,
Qt::NoButton, Qt::NoModifier);
@ -556,7 +562,7 @@ QWindowsOleDropTarget::Drop(LPDATAOBJECT pDataObj, DWORD grfKeyState,
QWindowsDrag *windowsDrag = QWindowsDrag::instance();
lastModifiers = toQtKeyboardModifiers(grfKeyState);
lastButtons = QWindowsMouseHandler::queryMouseButtons();
lastButtons = QWindowsPointerHandler::queryMouseButtons();
const QPlatformDropQtResponse response =
QWindowSystemInterface::handleDrop(m_window, windowsDrag->dropData(),
@ -611,7 +617,6 @@ QWindowsOleDropTarget::Drop(LPDATAOBJECT pDataObj, DWORD grfKeyState,
*/
bool QWindowsDrag::m_canceled = false;
bool QWindowsDrag::m_dragging = false;
QWindowsDrag::QWindowsDrag() = default;
@ -671,7 +676,11 @@ static HRESULT startDoDragDrop(LPDATAOBJECT pDataObj, LPDROPSOURCE pDropSource,
const quint32 pointerId = GET_POINTERID_WPARAM(msg.wParam);
POINTER_INFO pointerInfo{};
if (!QWindowsContext::user32dll.getPointerInfo || !QWindowsContext::user32dll.getPointerInfo(pointerId, &pointerInfo))
BOOL bResultPointerInfo = QWindowsContext::user32dll.getPointerInfo ?
QWindowsContext::user32dll.getPointerInfo(pointerId, &pointerInfo) :
vxkex::GetPointerInfo(pointerId, &pointerInfo);
if (!bResultPointerInfo)
return E_FAIL;
if (pointerInfo.pointerFlags & POINTER_FLAG_PRIMARY) {
@ -739,10 +748,7 @@ Qt::DropAction QWindowsDrag::drag(QDrag *drag)
const DWORD allowedEffects = translateToWinDragEffects(possibleActions);
qCDebug(lcQpaMime) << '>' << __FUNCTION__ << "possible Actions=0x"
<< Qt::hex << int(possibleActions) << "effects=0x" << allowedEffects << Qt::dec;
// Indicate message handlers we are in DoDragDrop() event loop.
QWindowsDrag::m_dragging = true;
const HRESULT r = startDoDragDrop(dropDataObject, windowDropSource, allowedEffects, &resultEffect);
QWindowsDrag::m_dragging = false;
const DWORD reportedPerformedEffect = dropDataObject->reportedPerformedEffect();
if (r == DRAGDROP_S_DROP) {
if (reportedPerformedEffect == DROPEFFECT_MOVE && resultEffect != DROPEFFECT_MOVE) {

View File

@ -0,0 +1,779 @@
// Copyright (C) 2016 The Qt Company Ltd.
// Copyright (C) 2013 Samuel Gaist <samuel.gaist@edeltech.ch>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qwindowsintegration.h"
#include "qwindowswindow.h"
#include "qwindowscontext.h"
#include "qwin10helpers.h"
#include "qwindowsmenu.h"
#include "qwindowsopenglcontext.h"
#include "qwindowsscreen.h"
#include "qwindowstheme.h"
#include "qwindowsservices.h"
#include <QtGui/private/qtgui-config_p.h>
#if QT_CONFIG(directwrite3)
#include <QtGui/private/qwindowsdirectwritefontdatabase_p.h>
#endif
#ifndef QT_NO_FREETYPE
# include <QtGui/private/qwindowsfontdatabase_ft_p.h>
#endif
#include <QtGui/private/qwindowsfontdatabase_p.h>
#if QT_CONFIG(clipboard)
# include "qwindowsclipboard.h"
# if QT_CONFIG(draganddrop)
# include "qwindowsdrag.h"
# endif
#endif
#include "qwindowsinputcontext.h"
#include "qwindowskeymapper.h"
#if QT_CONFIG(accessibility)
# include "uiautomation/qwindowsuiaaccessibility.h"
#endif
#include <qpa/qplatformnativeinterface.h>
#include <qpa/qwindowsysteminterface.h>
#if QT_CONFIG(sessionmanager)
# include "qwindowssessionmanager.h"
#endif
#include <QtGui/qpointingdevice.h>
#include <QtGui/private/qguiapplication_p.h>
#include <QtGui/private/qhighdpiscaling_p.h>
#include <QtGui/qpa/qplatforminputcontextfactory_p.h>
#include <QtGui/qpa/qplatformcursor.h>
#include <QtGui/private/qwindowsguieventdispatcher_p.h>
#include <QtCore/qdebug.h>
#include <QtCore/qvariant.h>
#include <QtCore/qoperatingsystemversion.h>
#include <QtCore/private/qfunctions_win_p.h>
#include <wrl.h>
#include <limits.h>
#if !defined(QT_NO_OPENGL)
# include "qwindowsglcontext.h"
#endif
#include "qwindowsopengltester.h"
#if QT_CONFIG(cpp_winrt)
# include <QtCore/private/qt_winrtbase_p.h>
# include <winrt/Windows.UI.Notifications.h>
# include <winrt/Windows.Data.Xml.Dom.h>
# include <winrt/Windows.Foundation.h>
# include <winrt/Windows.UI.ViewManagement.h>
#endif
#include <memory>
static inline void initOpenGlBlacklistResources()
{
Q_INIT_RESOURCE(openglblacklists);
}
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
struct QWindowsIntegrationPrivate
{
Q_DISABLE_COPY_MOVE(QWindowsIntegrationPrivate)
explicit QWindowsIntegrationPrivate() = default;
~QWindowsIntegrationPrivate();
void parseOptions(QWindowsIntegration *q, const QStringList &paramList);
unsigned m_options = 0;
QWindowsContext m_context;
QPlatformFontDatabase *m_fontDatabase = nullptr;
#if QT_CONFIG(clipboard)
QWindowsClipboard m_clipboard;
# if QT_CONFIG(draganddrop)
QWindowsDrag m_drag;
# endif
#endif
#ifndef QT_NO_OPENGL
QMutex m_staticContextLock;
QScopedPointer<QWindowsStaticOpenGLContext> m_staticOpenGLContext;
#endif // QT_NO_OPENGL
QScopedPointer<QPlatformInputContext> m_inputContext;
#if QT_CONFIG(accessibility)
QWindowsUiaAccessibility m_accessibility;
#endif
QWindowsServices m_services;
};
template <typename IntType>
bool parseIntOption(const QString &parameter,const QLatin1StringView &option,
IntType minimumValue, IntType maximumValue, IntType *target)
{
const int valueLength = parameter.size() - option.size() - 1;
if (valueLength < 1 || !parameter.startsWith(option) || parameter.at(option.size()) != u'=')
return false;
bool ok;
const auto valueRef = QStringView{parameter}.right(valueLength);
const int value = valueRef.toInt(&ok);
if (ok) {
if (value >= int(minimumValue) && value <= int(maximumValue))
*target = static_cast<IntType>(value);
else {
qWarning() << "Value" << value << "for option" << option << "out of range"
<< minimumValue << ".." << maximumValue;
}
} else {
qWarning() << "Invalid value" << valueRef << "for option" << option;
}
return true;
}
using DarkModeHandlingFlag = QNativeInterface::Private::QWindowsApplication::DarkModeHandlingFlag;
using DarkModeHandling = QNativeInterface::Private::QWindowsApplication::DarkModeHandling;
static inline unsigned parseOptions(const QStringList &paramList,
int *tabletAbsoluteRange,
QtWindows::DpiAwareness *dpiAwareness,
DarkModeHandling *darkModeHandling)
{
unsigned options = 0;
for (const QString &param : paramList) {
if (param.startsWith(u"fontengine=")) {
if (param.endsWith(u"gdi")) {
options |= QWindowsIntegration::FontDatabaseGDI;
} else if (param.endsWith(u"freetype")) {
options |= QWindowsIntegration::FontDatabaseFreeType;
} else if (param.endsWith(u"native")) {
options |= QWindowsIntegration::FontDatabaseNative;
}
} else if (param.startsWith(u"dialogs=")) {
if (param.endsWith(u"xp")) {
options |= QWindowsIntegration::XpNativeDialogs;
} else if (param.endsWith(u"none")) {
options |= QWindowsIntegration::NoNativeDialogs;
}
} else if (param == u"altgr") {
options |= QWindowsIntegration::DetectAltGrModifier;
} else if (param == u"gl=gdi") {
options |= QWindowsIntegration::DisableArb;
} else if (param == u"nodirectwrite") {
options |= QWindowsIntegration::DontUseDirectWriteFonts;
} else if (param == u"nocolorfonts") {
options |= QWindowsIntegration::DontUseColorFonts;
} else if (param == u"nomousefromtouch") {
options |= QWindowsIntegration::DontPassOsMouseEventsSynthesizedFromTouch;
} else if (parseIntOption(param, "verbose"_L1, 0, INT_MAX, &QWindowsContext::verbose)
|| parseIntOption(param, "tabletabsoluterange"_L1, 0, INT_MAX, tabletAbsoluteRange)
|| parseIntOption(param, "dpiawareness"_L1, QtWindows::DpiAwareness::Invalid,
QtWindows::DpiAwareness::PerMonitorVersion2, dpiAwareness)) {
} else if (param == u"menus=native") {
options |= QWindowsIntegration::AlwaysUseNativeMenus;
} else if (param == u"menus=none") {
options |= QWindowsIntegration::NoNativeMenus;
} else if (param == u"reverse") {
options |= QWindowsIntegration::RtlEnabled;
} else if (param == u"darkmode=0") {
*darkModeHandling = {};
} else if (param == u"darkmode=1") {
darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeWindowFrames);
darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeStyle, false);
} else if (param == u"darkmode=2") {
darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeWindowFrames);
darkModeHandling->setFlag(DarkModeHandlingFlag::DarkModeStyle);
} else {
qWarning() << "Unknown option" << param;
}
}
return options;
}
void QWindowsIntegrationPrivate::parseOptions(QWindowsIntegration *q, const QStringList &paramList)
{
initOpenGlBlacklistResources();
static bool dpiAwarenessSet = false;
// Default to per-monitor-v2 awareness (if available)
QtWindows::DpiAwareness dpiAwareness = QtWindows::DpiAwareness::PerMonitorVersion2;
int tabletAbsoluteRange = -1;
DarkModeHandling darkModeHandling = DarkModeHandlingFlag::DarkModeWindowFrames
| DarkModeHandlingFlag::DarkModeStyle;
m_options = ::parseOptions(paramList, &tabletAbsoluteRange, &dpiAwareness, &darkModeHandling);
q->setDarkModeHandling(darkModeHandling);
QWindowsFontDatabase::setFontOptions(m_options);
if (tabletAbsoluteRange >= 0)
QWindowsContext::setTabletAbsoluteRange(tabletAbsoluteRange);
QCoreApplication::setAttribute(Qt::AA_CompressHighFrequencyEvents);
QWindowSystemInterfacePrivate::TabletEvent::setPlatformSynthesizesMouse(false);
if (!dpiAwarenessSet) { // Set only once in case of repeated instantiations of QGuiApplication.
if (!QCoreApplication::testAttribute(Qt::AA_PluginApplication)) {
m_context.setProcessDpiAwareness(dpiAwareness);
qCDebug(lcQpaWindow) << "DpiAwareness=" << dpiAwareness
<< "effective process DPI awareness=" << QWindowsContext::processDpiAwareness();
}
dpiAwarenessSet = true;
}
m_context.initTouch(m_options);
QPlatformCursor::setCapability(QPlatformCursor::OverrideCursor);
m_context.initPowerNotificationHandler();
}
QWindowsIntegrationPrivate::~QWindowsIntegrationPrivate()
{
delete m_fontDatabase;
}
QWindowsIntegration *QWindowsIntegration::m_instance = nullptr;
QWindowsIntegration::QWindowsIntegration(const QStringList &paramList) :
d(new QWindowsIntegrationPrivate)
{
m_instance = this;
d->parseOptions(this, paramList);
#if QT_CONFIG(clipboard)
d->m_clipboard.registerViewer();
#endif
d->m_context.screenManager().initialize();
d->m_context.setDetectAltGrModifier((d->m_options & DetectAltGrModifier) != 0);
}
QWindowsIntegration::~QWindowsIntegration()
{
m_instance = nullptr;
}
void QWindowsIntegration::initialize()
{
auto icStrs = QPlatformInputContextFactory::requested();
icStrs.isEmpty() ? d->m_inputContext.reset(new QWindowsInputContext)
: d->m_inputContext.reset(QPlatformInputContextFactory::create(icStrs));
}
bool QWindowsIntegration::hasCapability(QPlatformIntegration::Capability cap) const
{
switch (cap) {
case ThreadedPixmaps:
return true;
#ifndef QT_NO_OPENGL
case OpenGL:
return true;
case ThreadedOpenGL:
if (const QWindowsStaticOpenGLContext *glContext = QWindowsIntegration::staticOpenGLContext())
return glContext->supportsThreadedOpenGL();
return false;
#endif // !QT_NO_OPENGL
case WindowMasks:
return true;
case MultipleWindows:
return true;
case ForeignWindows:
return true;
case RasterGLSurface:
return true;
case AllGLFunctionsQueryable:
return true;
case SwitchableWidgetComposition:
return false; // QTBUG-68329 QTBUG-53515 QTBUG-54734
case BackingStoreStaticContents:
return true;
default:
return QPlatformIntegration::hasCapability(cap);
}
return false;
}
QPlatformWindow *QWindowsIntegration::createPlatformWindow(QWindow *window) const
{
if (window->type() == Qt::Desktop) {
auto *result = new QWindowsDesktopWindow(window);
qCDebug(lcQpaWindow) << "Desktop window:" << window
<< Qt::showbase << Qt::hex << result->winId() << Qt::noshowbase << Qt::dec << result->geometry();
return result;
}
QWindowsWindowData requested;
requested.flags = window->flags();
requested.geometry = window->isTopLevel()
? QHighDpi::toNativePixels(window->geometry(), window)
: QHighDpi::toNativeLocalPosition(window->geometry(), window);
if (!(requested.flags & Qt::FramelessWindowHint)) {
// Apply custom margins (see QWindowsWindow::setCustomMargins())).
const QVariant customMarginsV = window->property("_q_windowsCustomMargins");
if (customMarginsV.isValid())
requested.customMargins = qvariant_cast<QMargins>(customMarginsV);
}
QWindowsWindowData obtained =
QWindowsWindowData::create(window, requested,
QWindowsWindow::formatWindowTitle(window->title()));
qCDebug(lcQpaWindow).nospace()
<< __FUNCTION__ << ' ' << window
<< "\n Requested: " << requested.geometry << " frame incl.="
<< QWindowsGeometryHint::positionIncludesFrame(window)
<< ' ' << requested.flags
<< "\n Obtained : " << obtained.geometry << " margins=" << obtained.fullFrameMargins
<< " handle=" << obtained.hwnd << ' ' << obtained.flags << '\n';
if (Q_UNLIKELY(!obtained.hwnd))
return nullptr;
QWindowsWindow *result = createPlatformWindowHelper(window, obtained);
Q_ASSERT(result);
if (window->isTopLevel() && !QWindowsContext::shouldHaveNonClientDpiScaling(window))
result->setFlag(QWindowsWindow::DisableNonClientScaling);
if (QWindowsMenuBar *menuBarToBeInstalled = QWindowsMenuBar::menuBarOf(window))
menuBarToBeInstalled->install(result);
return result;
}
QPlatformWindow *QWindowsIntegration::createForeignWindow(QWindow *window, WId nativeHandle) const
{
const HWND hwnd = reinterpret_cast<HWND>(nativeHandle);
if (!IsWindow(hwnd)) {
qWarning("Windows QPA: Invalid foreign window ID %p.", hwnd);
return nullptr;
}
auto *result = new QWindowsForeignWindow(window, hwnd);
const QRect obtainedGeometry = result->geometry();
QScreen *screen = nullptr;
if (const QPlatformScreen *pScreen = result->screenForGeometry(obtainedGeometry))
screen = pScreen->screen();
if (screen && screen != window->screen())
window->setScreen(screen);
qCDebug(lcQpaWindow) << "Foreign window:" << window << Qt::showbase << Qt::hex
<< result->winId() << Qt::noshowbase << Qt::dec << obtainedGeometry << screen;
return result;
}
// Overridden to return a QWindowsDirect2DWindow in Direct2D plugin.
QWindowsWindow *QWindowsIntegration::createPlatformWindowHelper(QWindow *window, const QWindowsWindowData &data) const
{
return new QWindowsWindow(window, data);
}
#ifndef QT_NO_OPENGL
QWindowsStaticOpenGLContext *QWindowsStaticOpenGLContext::doCreate()
{
#if defined(QT_OPENGL_DYNAMIC)
QWindowsOpenGLTester::Renderer requestedRenderer = QWindowsOpenGLTester::requestedRenderer();
switch (requestedRenderer) {
case QWindowsOpenGLTester::DesktopGl:
if (QWindowsStaticOpenGLContext *glCtx = QOpenGLStaticContext::create()) {
if ((QWindowsOpenGLTester::supportedRenderers(requestedRenderer) & QWindowsOpenGLTester::DisableRotationFlag)
&& !QWindowsScreen::setOrientationPreference(Qt::LandscapeOrientation)) {
qCWarning(lcQpaGl, "Unable to disable rotation.");
}
return glCtx;
}
qCWarning(lcQpaGl, "System OpenGL failed. Falling back to Software OpenGL.");
return QOpenGLStaticContext::create(true);
case QWindowsOpenGLTester::SoftwareRasterizer:
if (QWindowsStaticOpenGLContext *swCtx = QOpenGLStaticContext::create(true))
return swCtx;
qCWarning(lcQpaGl, "Software OpenGL failed. Falling back to system OpenGL.");
if (QWindowsOpenGLTester::supportedRenderers(requestedRenderer) & QWindowsOpenGLTester::DesktopGl)
return QOpenGLStaticContext::create();
return nullptr;
default:
break;
}
const QWindowsOpenGLTester::Renderers supportedRenderers = QWindowsOpenGLTester::supportedRenderers(requestedRenderer);
if (supportedRenderers.testFlag(QWindowsOpenGLTester::DisableProgramCacheFlag)
&& !QCoreApplication::testAttribute(Qt::AA_DisableShaderDiskCache)) {
QCoreApplication::setAttribute(Qt::AA_DisableShaderDiskCache);
}
if (supportedRenderers & QWindowsOpenGLTester::DesktopGl) {
if (QWindowsStaticOpenGLContext *glCtx = QOpenGLStaticContext::create()) {
if ((supportedRenderers & QWindowsOpenGLTester::DisableRotationFlag)
&& !QWindowsScreen::setOrientationPreference(Qt::LandscapeOrientation)) {
qCWarning(lcQpaGl, "Unable to disable rotation.");
}
return glCtx;
}
}
return QOpenGLStaticContext::create(true);
#else
return QOpenGLStaticContext::create();
#endif
}
QWindowsStaticOpenGLContext *QWindowsStaticOpenGLContext::create()
{
return QWindowsStaticOpenGLContext::doCreate();
}
QPlatformOpenGLContext *QWindowsIntegration::createPlatformOpenGLContext(QOpenGLContext *context) const
{
qCDebug(lcQpaGl) << __FUNCTION__ << context->format();
if (QWindowsStaticOpenGLContext *staticOpenGLContext = QWindowsIntegration::staticOpenGLContext()) {
std::unique_ptr<QWindowsOpenGLContext> result(staticOpenGLContext->createContext(context));
if (result->isValid())
return result.release();
}
return nullptr;
}
QOpenGLContext::OpenGLModuleType QWindowsIntegration::openGLModuleType()
{
#if !defined(QT_OPENGL_DYNAMIC)
return QOpenGLContext::LibGL;
#else
if (const QWindowsStaticOpenGLContext *staticOpenGLContext = QWindowsIntegration::staticOpenGLContext())
return staticOpenGLContext->moduleType();
return QOpenGLContext::LibGL;
#endif
}
HMODULE QWindowsIntegration::openGLModuleHandle() const
{
if (QWindowsStaticOpenGLContext *staticOpenGLContext = QWindowsIntegration::staticOpenGLContext())
return static_cast<HMODULE>(staticOpenGLContext->moduleHandle());
return nullptr;
}
QOpenGLContext *QWindowsIntegration::createOpenGLContext(HGLRC ctx, HWND window, QOpenGLContext *shareContext) const
{
if (!ctx || !window)
return nullptr;
if (QWindowsStaticOpenGLContext *staticOpenGLContext = QWindowsIntegration::staticOpenGLContext()) {
std::unique_ptr<QWindowsOpenGLContext> result(staticOpenGLContext->createContext(ctx, window));
if (result->isValid()) {
auto *context = new QOpenGLContext;
context->setShareContext(shareContext);
auto *contextPrivate = QOpenGLContextPrivate::get(context);
contextPrivate->adopt(result.release());
return context;
}
}
return nullptr;
}
QWindowsStaticOpenGLContext *QWindowsIntegration::staticOpenGLContext()
{
QWindowsIntegration *integration = QWindowsIntegration::instance();
if (!integration)
return nullptr;
QWindowsIntegrationPrivate *d = integration->d.data();
QMutexLocker lock(&d->m_staticContextLock);
if (d->m_staticOpenGLContext.isNull())
d->m_staticOpenGLContext.reset(QWindowsStaticOpenGLContext::create());
return d->m_staticOpenGLContext.data();
}
#endif // !QT_NO_OPENGL
QPlatformFontDatabase *QWindowsIntegration::fontDatabase() const
{
if (!d->m_fontDatabase) {
#ifndef QT_NO_FREETYPE
if (d->m_options & QWindowsIntegration::FontDatabaseFreeType)
d->m_fontDatabase = new QWindowsFontDatabaseFT;
else
#endif // QT_NO_FREETYPE
#if QT_CONFIG(directwrite3)
/* IDWriteFontFace3 is only reportedly available starting with Windows 10. This change is necessary starting
with Qt 6.8, where DirectWrite is used by default to populate the font database.
More info: https://github.com/videolan/vlc/blob/master/contrib/src/qt/0001-Use-DirectWrite-font-database-only-with-Windows-10-a.patch
*/
if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows10 &&
!(d->m_options & (QWindowsIntegration::FontDatabaseGDI | QWindowsIntegration::DontUseDirectWriteFonts)))
d->m_fontDatabase = new QWindowsDirectWriteFontDatabase;
else
#endif
d->m_fontDatabase = new QWindowsFontDatabase;
}
return d->m_fontDatabase;
}
#ifdef SPI_GETKEYBOARDSPEED
static inline int keyBoardAutoRepeatRateMS()
{
DWORD time = 0;
if (SystemParametersInfo(SPI_GETKEYBOARDSPEED, 0, &time, 0))
return time ? 1000 / static_cast<int>(time) : 500;
return 30;
}
#endif
QVariant QWindowsIntegration::styleHint(QPlatformIntegration::StyleHint hint) const
{
switch (hint) {
case QPlatformIntegration::CursorFlashTime:
if (const unsigned timeMS = GetCaretBlinkTime())
return QVariant(timeMS != INFINITE ? int(timeMS) * 2 : 0);
break;
#ifdef SPI_GETKEYBOARDSPEED
case KeyboardAutoRepeatRate:
return QVariant(keyBoardAutoRepeatRateMS());
#endif
case QPlatformIntegration::ShowIsMaximized:
case QPlatformIntegration::StartDragTime:
case QPlatformIntegration::StartDragDistance:
case QPlatformIntegration::KeyboardInputInterval:
case QPlatformIntegration::ShowIsFullScreen:
case QPlatformIntegration::PasswordMaskDelay:
case QPlatformIntegration::StartDragVelocity:
break; // Not implemented
case QPlatformIntegration::FontSmoothingGamma:
return QVariant(QWindowsFontDatabase::fontSmoothingGamma());
case QPlatformIntegration::MouseDoubleClickInterval:
if (const UINT ms = GetDoubleClickTime())
return QVariant(int(ms));
break;
case QPlatformIntegration::UseRtlExtensions:
return QVariant(d->m_context.useRTLExtensions());
default:
break;
}
return QPlatformIntegration::styleHint(hint);
}
QPlatformKeyMapper *QWindowsIntegration::keyMapper() const
{
return d->m_context.keyMapper();
}
#if QT_CONFIG(clipboard)
QPlatformClipboard * QWindowsIntegration::clipboard() const
{
return &d->m_clipboard;
}
# if QT_CONFIG(draganddrop)
QPlatformDrag *QWindowsIntegration::drag() const
{
return &d->m_drag;
}
# endif // QT_CONFIG(draganddrop)
#endif // !QT_NO_CLIPBOARD
QPlatformInputContext * QWindowsIntegration::inputContext() const
{
return d->m_inputContext.data();
}
#if QT_CONFIG(accessibility)
QPlatformAccessibility *QWindowsIntegration::accessibility() const
{
return &d->m_accessibility;
}
#endif
unsigned QWindowsIntegration::options() const
{
return d->m_options;
}
#if QT_CONFIG(sessionmanager)
QPlatformSessionManager *QWindowsIntegration::createPlatformSessionManager(const QString &id, const QString &key) const
{
return new QWindowsSessionManager(id, key);
}
#endif
QAbstractEventDispatcher * QWindowsIntegration::createEventDispatcher() const
{
return new QWindowsGuiEventDispatcher;
}
QStringList QWindowsIntegration::themeNames() const
{
return QStringList(QLatin1StringView(QWindowsTheme::name));
}
QPlatformTheme *QWindowsIntegration::createPlatformTheme(const QString &name) const
{
if (name == QLatin1StringView(QWindowsTheme::name))
return new QWindowsTheme;
return QPlatformIntegration::createPlatformTheme(name);
}
QPlatformServices *QWindowsIntegration::services() const
{
return &d->m_services;
}
void QWindowsIntegration::beep() const
{
MessageBeep(MB_OK); // For QApplication
}
void QWindowsIntegration::setApplicationBadge(qint64 number)
{
// Clamp to positive numbers, as the Windows API doesn't support negative numbers
number = qMax(0, number);
// Persist, so we can re-apply it on setting changes and Explorer restart
m_applicationBadgeNumber = number;
static const bool isWindows11 = QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows11;
#if QT_CONFIG(cpp_winrt)
// We prefer the native BadgeUpdater API, that allows us to set a number directly,
// but it requires that the application has a package identity, and also doesn't
// seem to work in all cases on < Windows 11.
QT_TRY {
if (isWindows11 && qt_win_hasPackageIdentity()) {
using namespace winrt::Windows::UI::Notifications;
auto badgeXml = BadgeUpdateManager::GetTemplateContent(BadgeTemplateType::BadgeNumber);
badgeXml.SelectSingleNode(L"//badge/@value").NodeValue(winrt::box_value(winrt::to_hstring(number)));
BadgeUpdateManager::CreateBadgeUpdaterForApplication().Update(BadgeNotification(badgeXml));
return;
}
} QT_CATCH(...) {
// fall back to win32 implementation
}
#endif
// Fallback for non-packaged apps, Windows 10, or Qt builds without WinRT/C++ support
if (!number) {
// Clear badge
setApplicationBadge(QImage());
return;
}
const bool isDarkMode = QWindowsTheme::instance()->colorScheme()
== Qt::ColorScheme::Dark;
QColor badgeColor;
QColor textColor;
#if QT_CONFIG(cpp_winrt)
if (isWindows11) {
// Match colors used by BadgeUpdater
static const auto fromUIColor = [](winrt::Windows::UI::Color &&color) {
return QColor(color.R, color.G, color.B, color.A);
};
using namespace winrt::Windows::UI::ViewManagement;
const auto settings = UISettings();
badgeColor = fromUIColor(settings.GetColorValue(isDarkMode ?
UIColorType::AccentLight2 : UIColorType::Accent));
textColor = fromUIColor(settings.GetColorValue(UIColorType::Background));
}
#endif
if (!badgeColor.isValid()) {
// Fall back to basic badge colors, based on Windows 10 look
badgeColor = isDarkMode ? Qt::black : QColor(220, 220, 220);
badgeColor.setAlphaF(0.5f);
textColor = isDarkMode ? Qt::white : Qt::black;
}
const auto devicePixelRatio = qApp->devicePixelRatio();
static const QSize iconBaseSize(16, 16);
QImage image(iconBaseSize * devicePixelRatio,
QImage::Format_ARGB32_Premultiplied);
image.fill(Qt::transparent);
QPainter painter(&image);
QRect badgeRect = image.rect();
QPen badgeBorderPen = Qt::NoPen;
if (!isWindows11) {
QColor badgeBorderColor = textColor;
badgeBorderColor.setAlphaF(0.5f);
badgeBorderPen = badgeBorderColor;
badgeRect.adjust(1, 1, -1, -1);
}
painter.setBrush(badgeColor);
painter.setPen(badgeBorderPen);
painter.setRenderHint(QPainter::Antialiasing);
painter.drawEllipse(badgeRect);
auto pixelSize = qCeil(10.5 * devicePixelRatio);
// Unlike the BadgeUpdater API we're limited by a square
// badge, so adjust the font size when above two digits.
const bool textOverflow = number > 99;
if (textOverflow)
pixelSize *= 0.8;
QFont font = painter.font();
font.setPixelSize(pixelSize);
font.setWeight(isWindows11 ? QFont::Medium : QFont::DemiBold);
painter.setFont(font);
painter.setRenderHint(QPainter::TextAntialiasing, devicePixelRatio > 1);
painter.setPen(textColor);
auto text = textOverflow ? u"99+"_s : QString::number(number);
painter.translate(textOverflow ? 1 : 0, textOverflow ? 0 : -1);
painter.drawText(image.rect(), Qt::AlignCenter, text);
painter.end();
setApplicationBadge(image);
}
void QWindowsIntegration::setApplicationBadge(const QImage &image)
{
QComHelper comHelper;
using Microsoft::WRL::ComPtr;
ComPtr<ITaskbarList3> taskbarList;
CoCreateInstance(CLSID_TaskbarList, nullptr,
CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&taskbarList));
if (!taskbarList) {
// There may not be any windows with a task bar button yet,
// in which case we'll apply the badge once a window with
// a button has been created.
return;
}
const auto hIcon = image.toHICON();
// Apply the icon to all top level windows, since the badge is
// set on an application level. If one of the windows go away
// the other windows will take over in showing the badge.
const auto topLevelWindows = QGuiApplication::topLevelWindows();
for (auto *topLevelWindow : topLevelWindows) {
if (!topLevelWindow->handle())
continue;
auto hwnd = reinterpret_cast<HWND>(topLevelWindow->winId());
taskbarList->SetOverlayIcon(hwnd, hIcon, L"");
}
DestroyIcon(hIcon);
// FIXME: Update icon when the application scale factor changes.
// Doing so in response to screen DPI changes is too soon, as the
// task bar is not yet ready for an updated icon, and will just
// result in a blurred icon even if our icon is high-DPI.
}
void QWindowsIntegration::updateApplicationBadge()
{
// The system color settings have changed, or we are reacting
// to a task bar button being created for the fist time or after
// Explorer had crashed and re-started. In any case, re-apply the
// badge so that everything is up to date.
if (m_applicationBadgeNumber)
setApplicationBadge(m_applicationBadgeNumber);
}
#if QT_CONFIG(vulkan)
QPlatformVulkanInstance *QWindowsIntegration::createPlatformVulkanInstance(QVulkanInstance *instance) const
{
return new QWindowsVulkanInstance(instance);
}
#endif
QT_END_NAMESPACE

View File

@ -17,6 +17,8 @@
#include <QtCore/private/qdebug_p.h>
#include <QtCore/private/qtools_p.h>
#include "vxkex.h"
#if defined(WM_APPCOMMAND)
# ifndef FAPPCOMMAND_MOUSE
# define FAPPCOMMAND_MOUSE 0x8000
@ -88,9 +90,17 @@ QWindowsKeyMapper::~QWindowsKeyMapper()= default;
#define VK_OEM_3 0xC0
#endif
// We not only need the scancode itself but also the extended bit of key messages. Thus we need
// the additional bit when masking the scancode.
enum { scancodeBitmask = 0x1ff };
// Get scancode from the given message
static constexpr quint32 getScancode(const MSG &msg)
{
const auto keyFlags = HIWORD(msg.lParam);
quint32 scancode = LOBYTE(keyFlags);
// if extended-key flag is on, the scan code consists of a sequence of two bytes,
// where the first byte has a value of 0xe0.
if ((keyFlags & KF_EXTENDED) != 0)
scancode |= 0xE000;
return scancode;
}
// Key recorder ------------------------------------------------------------------------[ start ] --
struct KeyRecord {
@ -532,33 +542,6 @@ QDebug operator<<(QDebug d, const KeyboardLayoutItem &k)
d << ')';
return d;
}
// Helpers to format a list of int as Qt key sequence
class formatKeys
{
public:
explicit formatKeys(const QList<int> &keys) : m_keys(keys) {}
private:
friend QDebug operator<<(QDebug d, const formatKeys &keys);
const QList<int> &m_keys;
};
QDebug operator<<(QDebug d, const formatKeys &k)
{
QDebugStateSaver saver(d);
d.nospace();
d << '(';
for (int i =0, size = k.m_keys.size(); i < size; ++i) {
if (i)
d << ", ";
d << QKeySequence(k.m_keys.at(i));
}
d << ')';
return d;
}
#else // !QT_NO_DEBUG_STREAM
static int formatKeys(const QList<int> &) { return 0; }
#endif // QT_NO_DEBUG_STREAM
/**
@ -656,7 +639,7 @@ void QWindowsKeyMapper::updateKeyMap(const MSG &msg)
{
unsigned char kbdBuffer[256]; // Will hold the complete keyboard state
GetKeyboardState(kbdBuffer);
const quint32 scancode = (msg.lParam >> 16) & scancodeBitmask;
const quint32 scancode = getScancode(msg);
updatePossibleKeyCodes(kbdBuffer, scancode, quint32(msg.wParam));
}
@ -751,28 +734,18 @@ static inline QString messageKeyText(const MSG &msg)
[[nodiscard]] static inline int getTitleBarHeight(const HWND hwnd)
{
if (QWindowsContext::user32dll.getDpiForWindow && QWindowsContext::user32dll.getSystemMetricsForDpi)
{
const UINT dpi = QWindowsContext::user32dll.getDpiForWindow(hwnd);
const int captionHeight = QWindowsContext::user32dll.getSystemMetricsForDpi(SM_CYCAPTION, dpi);
if (IsZoomed(hwnd))
return captionHeight;
// The frame height should also be taken into account if the window
// is not maximized.
const int frameHeight = QWindowsContext::user32dll.getSystemMetricsForDpi(SM_CYSIZEFRAME, dpi)
+ QWindowsContext::user32dll.getSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
return captionHeight + frameHeight;
}
else
{
const int captionHeight = GetSystemMetrics(SM_CYCAPTION);
if (IsZoomed(hwnd))
return captionHeight;
// The frame height should also be taken into account if the window
// is not maximized.
const int frameHeight = GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER);
return captionHeight + frameHeight;
}
const BOOL bNewAPI = (QWindowsContext::user32dll.getSystemMetricsForDpi != nullptr);
const UINT dpi = bNewAPI ? QWindowsContext::user32dll.getDpiForWindow(hwnd) : vxkex::GetDpiForWindow(hwnd);
const int captionHeight = bNewAPI ? QWindowsContext::user32dll.getSystemMetricsForDpi(SM_CYCAPTION, dpi) : vxkex::GetSystemMetricsForDpi(SM_CYCAPTION, dpi);
if (IsZoomed(hwnd))
return captionHeight;
// The frame height should also be taken into account if the window
// is not maximized.
const int frameHeight = bNewAPI ?
(QWindowsContext::user32dll.getSystemMetricsForDpi(SM_CYSIZEFRAME, dpi) + QWindowsContext::user32dll.getSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi))
: (vxkex::GetSystemMetricsForDpi(SM_CYSIZEFRAME, dpi) + vxkex::GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi));
return captionHeight + frameHeight;
}
[[nodiscard]] static inline bool isSystemMenuOffsetNeeded(const Qt::WindowFlags flags)
@ -833,7 +806,7 @@ static void showSystemMenu(QWindow* w)
qWindowsWndProc(topLevelHwnd, WM_SYSCOMMAND, WPARAM(ret), 0);
}
static inline void sendExtendedPressRelease(QWindow *w, int k,
static inline void sendExtendedPressRelease(QWindow *w, unsigned long timestamp, int k,
Qt::KeyboardModifiers mods,
quint32 nativeScanCode,
quint32 nativeVirtualKey,
@ -842,8 +815,8 @@ static inline void sendExtendedPressRelease(QWindow *w, int k,
bool autorep = false,
ushort count = 1)
{
QWindowSystemInterface::handleExtendedKeyEvent(w, QEvent::KeyPress, k, mods, nativeScanCode, nativeVirtualKey, nativeModifiers, text, autorep, count);
QWindowSystemInterface::handleExtendedKeyEvent(w, QEvent::KeyRelease, k, mods, nativeScanCode, nativeVirtualKey, nativeModifiers, text, autorep, count);
QWindowSystemInterface::handleExtendedKeyEvent(w, timestamp, QEvent::KeyPress, k, mods, nativeScanCode, nativeVirtualKey, nativeModifiers, text, autorep, count);
QWindowSystemInterface::handleExtendedKeyEvent(w, timestamp, QEvent::KeyRelease, k, mods, nativeScanCode, nativeVirtualKey, nativeModifiers, text, autorep, count);
}
/*!
@ -910,7 +883,7 @@ bool QWindowsKeyMapper::translateMultimediaKeyEventInternal(QWindow *window, con
const int qtKey = int(CmdTbl[cmd]);
if (!skipPressRelease)
sendExtendedPressRelease(receiver, qtKey, Qt::KeyboardModifier(state), 0, 0, 0);
sendExtendedPressRelease(receiver, msg.time, qtKey, Qt::KeyboardModifier(state), 0, 0, 0);
// QTBUG-43343: Make sure to return false if Qt does not handle the key, otherwise,
// the keys are not passed to the active media player.
# if QT_CONFIG(shortcut)
@ -955,7 +928,7 @@ bool QWindowsKeyMapper::translateKeyEventInternal(QWindow *window, MSG msg,
m_seenAltGr = true;
const UINT msgType = msg.message;
const quint32 scancode = (msg.lParam >> 16) & scancodeBitmask;
const quint32 scancode = getScancode(msg);
auto vk_key = quint32(msg.wParam);
quint32 nModifiers = 0;
@ -990,7 +963,7 @@ bool QWindowsKeyMapper::translateKeyEventInternal(QWindow *window, MSG msg,
// A multi-character key or a Input method character
// not found by our look-ahead
if (msgType == WM_CHAR || msgType == WM_IME_CHAR) {
sendExtendedPressRelease(receiver, 0, Qt::KeyboardModifier(state), scancode, 0, nModifiers, messageKeyText(msg), false);
sendExtendedPressRelease(receiver, msg.time, 0, Qt::KeyboardModifier(state), scancode, 0, nModifiers, messageKeyText(msg), false);
return true;
}
@ -1025,14 +998,14 @@ bool QWindowsKeyMapper::translateKeyEventInternal(QWindow *window, MSG msg,
if (dirStatus == VK_LSHIFT
&& ((msg.wParam == VK_SHIFT && GetKeyState(VK_LCONTROL))
|| (msg.wParam == VK_CONTROL && GetKeyState(VK_LSHIFT)))) {
sendExtendedPressRelease(receiver, Qt::Key_Direction_L, {},
sendExtendedPressRelease(receiver, msg.time, Qt::Key_Direction_L, {},
scancode, vk_key, nModifiers, QString(), false);
result = true;
dirStatus = 0;
} else if (dirStatus == VK_RSHIFT
&& ( (msg.wParam == VK_SHIFT && GetKeyState(VK_RCONTROL))
|| (msg.wParam == VK_CONTROL && GetKeyState(VK_RSHIFT)))) {
sendExtendedPressRelease(receiver, Qt::Key_Direction_R, {},
sendExtendedPressRelease(receiver, msg.time, Qt::Key_Direction_R, {},
scancode, vk_key, nModifiers, QString(), false);
result = true;
dirStatus = 0;
@ -1245,9 +1218,9 @@ bool QWindowsKeyMapper::translateKeyEventInternal(QWindow *window, MSG msg,
// so, we have an auto-repeating key
if (rec) {
if (code < Qt::Key_Shift || code > Qt::Key_ScrollLock) {
QWindowSystemInterface::handleExtendedKeyEvent(receiver, QEvent::KeyRelease, code,
QWindowSystemInterface::handleExtendedKeyEvent(receiver, msg.time, QEvent::KeyRelease, code,
Qt::KeyboardModifier(state), scancode, quint32(msg.wParam), nModifiers, rec->text, true);
QWindowSystemInterface::handleExtendedKeyEvent(receiver, QEvent::KeyPress, code,
QWindowSystemInterface::handleExtendedKeyEvent(receiver, msg.time, QEvent::KeyPress, code,
Qt::KeyboardModifier(state), scancode, quint32(msg.wParam), nModifiers, rec->text, true);
result = true;
}
@ -1273,7 +1246,7 @@ bool QWindowsKeyMapper::translateKeyEventInternal(QWindow *window, MSG msg,
if (msg.wParam == VK_PACKET)
code = asciiToKeycode(char(uch.cell()), state);
QWindowSystemInterface::handleExtendedKeyEvent(receiver, QEvent::KeyPress, code,
QWindowSystemInterface::handleExtendedKeyEvent(receiver, msg.time, QEvent::KeyPress, code,
modifiers, scancode, quint32(msg.wParam), nModifiers, text, false);
result =true;
bool store = true;
@ -1315,10 +1288,10 @@ bool QWindowsKeyMapper::translateKeyEventInternal(QWindow *window, MSG msg,
if ((msg.lParam & 0x40000000) == 0 &&
Qt::KeyboardModifier(state) == Qt::NoModifier &&
((code == Qt::Key_F18) || (code == Qt::Key_F19) || (code == Qt::Key_F20))) {
QWindowSystemInterface::handleExtendedKeyEvent(receiver, QEvent::KeyPress, code,
QWindowSystemInterface::handleExtendedKeyEvent(receiver, msg.time, QEvent::KeyPress, code,
Qt::MetaModifier, scancode,
quint32(msg.wParam), MetaLeft);
QWindowSystemInterface::handleExtendedKeyEvent(receiver, QEvent::KeyRelease, code,
QWindowSystemInterface::handleExtendedKeyEvent(receiver, msg.time, QEvent::KeyRelease, code,
Qt::NoModifier, scancode,
quint32(msg.wParam), 0);
result = true;
@ -1330,7 +1303,7 @@ bool QWindowsKeyMapper::translateKeyEventInternal(QWindow *window, MSG msg,
// Map SHIFT + Tab to SHIFT + BackTab, QShortcutMap knows about this translation
if (code == Qt::Key_Tab && (state & Qt::ShiftModifier) == Qt::ShiftModifier)
code = Qt::Key_Backtab;
QWindowSystemInterface::handleExtendedKeyEvent(receiver, QEvent::KeyRelease, code,
QWindowSystemInterface::handleExtendedKeyEvent(receiver, msg.time, QEvent::KeyRelease, code,
Qt::KeyboardModifier(state), scancode, quint32(msg.wParam),
nModifiers,
(rec ? rec->text : QString()), false);
@ -1352,7 +1325,7 @@ bool QWindowsKeyMapper::translateKeyEventInternal(QWindow *window, MSG msg,
return result;
}
Qt::KeyboardModifiers QWindowsKeyMapper::queryKeyboardModifiers()
Qt::KeyboardModifiers QWindowsKeyMapper::queryKeyboardModifiers() const
{
Qt::KeyboardModifiers modifiers = Qt::NoModifier;
if (GetKeyState(VK_SHIFT) < 0)
@ -1366,9 +1339,9 @@ Qt::KeyboardModifiers QWindowsKeyMapper::queryKeyboardModifiers()
return modifiers;
}
QList<int> QWindowsKeyMapper::possibleKeys(const QKeyEvent *e) const
QList<QKeyCombination> QWindowsKeyMapper::possibleKeyCombinations(const QKeyEvent *e) const
{
QList<int> result;
QList<QKeyCombination> result;
const quint32 nativeVirtualKey = e->nativeVirtualKey();
@ -1382,31 +1355,39 @@ QList<int> QWindowsKeyMapper::possibleKeys(const QKeyEvent *e) const
quint32 baseKey = kbItem.qtKey[0];
Qt::KeyboardModifiers keyMods = e->modifiers();
if (baseKey == Qt::Key_Return && (e->nativeModifiers() & ExtendedKey)) {
result << (Qt::Key_Enter | keyMods).toCombined();
result << (Qt::Key_Enter | keyMods);
return result;
}
result << int(baseKey) + int(keyMods); // The base key is _always_ valid, of course
// If Key_Tab+Shift is pressed we add Key_Backtab without
// shift modifier as a possible combination too
if (baseKey == Qt::Key_Tab && (keyMods & Qt::ShiftModifier))
result << (Qt::Key_Backtab | (keyMods & ~Qt::ShiftModifier));
// The base key is _always_ valid, of course
result << QKeyCombination::fromCombined(int(baseKey) + int(keyMods));
for (size_t i = 1; i < NumMods; ++i) {
Qt::KeyboardModifiers neededMods = ModsTbl[i];
quint32 key = kbItem.qtKey[i];
if (key && key != baseKey && ((keyMods & neededMods) == neededMods)) {
const Qt::KeyboardModifiers missingMods = keyMods & ~neededMods;
const int matchedKey = int(key) + int(missingMods);
const auto it =
std::find_if(result.begin(), result.end(),
[key] (int k) { return (k & ~Qt::KeyboardModifierMask) == key; });
const auto matchedKey = QKeyCombination::fromCombined(int(key) + int(missingMods));
const auto it = std::find_if(result.begin(), result.end(),
[key](auto keyCombination) {
return keyCombination.key() == key;
});
// QTBUG-67200: Use the match with the least modifiers (prefer
// Shift+9 over Alt + Shift + 9) resulting in more missing modifiers.
if (it == result.end())
result << matchedKey;
else if (missingMods > Qt::KeyboardModifiers(*it & Qt::KeyboardModifierMask))
else if (missingMods > it->keyboardModifiers())
*it = matchedKey;
}
}
qCDebug(lcQpaEvents) << __FUNCTION__ << e << "nativeVirtualKey="
<< Qt::showbase << Qt::hex << e->nativeVirtualKey() << Qt::dec << Qt::noshowbase
<< e->modifiers() << kbItem << "\n returns" << formatKeys(result);
<< e->modifiers() << kbItem << "\n returns" << result;
return result;
}

View File

@ -4,7 +4,6 @@
#include <QtCore/qt_windows.h>
#include "qwindowspointerhandler.h"
#include "qwindowsmousehandler.h"
#if QT_CONFIG(tabletevent)
# include "qwindowstabletsupport.h"
#endif
@ -27,6 +26,8 @@
#include <windowsx.h>
#include "vxkex.h"
QT_BEGIN_NAMESPACE
enum {
@ -39,6 +40,14 @@ enum {
qint64 QWindowsPointerHandler::m_nextInputDeviceId = 1;
const QPointingDevice *primaryMouse()
{
static QPointer<const QPointingDevice> result;
if (!result)
result = QPointingDevice::primaryPointingDevice();
return result;
}
QWindowsPointerHandler::~QWindowsPointerHandler()
{
}
@ -48,7 +57,11 @@ bool QWindowsPointerHandler::translatePointerEvent(QWindow *window, HWND hwnd, Q
*result = 0;
const quint32 pointerId = GET_POINTERID_WPARAM(msg.wParam);
if (!QWindowsContext::user32dll.getPointerType(pointerId, &m_pointerType)) {
BOOL bResultPt = QWindowsContext::user32dll.getPointerType ?
QWindowsContext::user32dll.getPointerType(pointerId, &m_pointerType) :
vxkex::GetPointerType(pointerId, &m_pointerType);
if (!bResultPt) {
qWarning() << "GetPointerType() failed:" << qt_error_string();
return false;
}
@ -62,12 +75,21 @@ bool QWindowsPointerHandler::translatePointerEvent(QWindow *window, HWND hwnd, Q
}
case QT_PT_TOUCH: {
quint32 pointerCount = 0;
if (!QWindowsContext::user32dll.getPointerFrameTouchInfo(pointerId, &pointerCount, nullptr)) {
BOOL bResultPointerTouchInfo = QWindowsContext::user32dll.getPointerFrameTouchInfo ?
QWindowsContext::user32dll.getPointerFrameTouchInfo(pointerId, &pointerCount, nullptr) :
vxkex::GetPointerFrameTouchInfo(pointerId, &pointerCount, nullptr);
if (!bResultPointerTouchInfo) {
qWarning() << "GetPointerFrameTouchInfo() failed:" << qt_error_string();
return false;
}
QVarLengthArray<POINTER_TOUCH_INFO, 10> touchInfo(pointerCount);
if (!QWindowsContext::user32dll.getPointerFrameTouchInfo(pointerId, &pointerCount, touchInfo.data())) {
bResultPointerTouchInfo = QWindowsContext::user32dll.getPointerFrameTouchInfo ?
QWindowsContext::user32dll.getPointerFrameTouchInfo(pointerId, &pointerCount, touchInfo.data()) :
vxkex::GetPointerFrameTouchInfo(pointerId, &pointerCount, touchInfo.data());
if (!bResultPointerTouchInfo) {
qWarning() << "GetPointerFrameTouchInfo() failed:" << qt_error_string();
return false;
}
@ -80,10 +102,12 @@ bool QWindowsPointerHandler::translatePointerEvent(QWindow *window, HWND hwnd, Q
// dispatch any skipped frames if event compression is disabled by the app
if (historyCount > 1 && !QCoreApplication::testAttribute(Qt::AA_CompressHighFrequencyEvents)) {
touchInfo.resize(pointerCount * historyCount);
if (!QWindowsContext::user32dll.getPointerFrameTouchInfoHistory(pointerId,
&historyCount,
&pointerCount,
touchInfo.data())) {
BOOL bResultTouchHistory = QWindowsContext::user32dll.getPointerFrameTouchInfoHistory ?
QWindowsContext::user32dll.getPointerFrameTouchInfoHistory(pointerId, &historyCount, &pointerCount, touchInfo.data()) :
vxkex::GetPointerFrameTouchInfoHistory(pointerId, &historyCount, &pointerCount, touchInfo.data());
if (!bResultTouchHistory) {
qWarning() << "GetPointerFrameTouchInfoHistory() failed:" << qt_error_string();
return false;
}
@ -101,7 +125,11 @@ bool QWindowsPointerHandler::translatePointerEvent(QWindow *window, HWND hwnd, Q
}
case QT_PT_PEN: {
POINTER_PEN_INFO penInfo;
if (!QWindowsContext::user32dll.getPointerPenInfo(pointerId, &penInfo)) {
BOOL bResultPenInfo = QWindowsContext::user32dll.getPointerPenInfo ?
QWindowsContext::user32dll.getPointerPenInfo(pointerId, &penInfo) : vxkex::GetPointerPenInfo(pointerId, &penInfo);
if (!bResultPenInfo) {
qWarning() << "GetPointerPenInfo() failed:" << qt_error_string();
return false;
}
@ -113,7 +141,11 @@ bool QWindowsPointerHandler::translatePointerEvent(QWindow *window, HWND hwnd, Q
|| !QCoreApplication::testAttribute(Qt::AA_CompressTabletEvents))) {
QVarLengthArray<POINTER_PEN_INFO, 10> penInfoHistory(historyCount);
if (!QWindowsContext::user32dll.getPointerPenInfoHistory(pointerId, &historyCount, penInfoHistory.data())) {
BOOL bResultPenInfoHistory = QWindowsContext::user32dll.getPointerPenInfoHistory ?
QWindowsContext::user32dll.getPointerPenInfoHistory(pointerId, &historyCount, penInfoHistory.data()) :
vxkex::GetPointerPenInfoHistory(pointerId, &historyCount, penInfoHistory.data());
if (!bResultPenInfoHistory) {
qWarning() << "GetPointerPenInfoHistory() failed:" << qt_error_string();
return false;
}
@ -215,7 +247,7 @@ static Qt::MouseButtons mouseButtonsFromKeyState(WPARAM keyState)
return result;
}
static Qt::MouseButtons queryMouseButtons()
Qt::MouseButtons QWindowsPointerHandler::queryMouseButtons()
{
Qt::MouseButtons result = Qt::NoButton;
const bool mouseSwapped = GetSystemMetrics(SM_SWAPBUTTON);
@ -428,8 +460,9 @@ bool QWindowsPointerHandler::translateTouchEvent(QWindow *window, HWND hwnd,
return false;
if (msg.message == WM_POINTERCAPTURECHANGED) {
QWindowSystemInterface::handleTouchCancelEvent(window, m_touchDevice.data(),
QWindowsKeyMapper::queryKeyboardModifiers());
const auto *keyMapper = QWindowsContext::instance()->keyMapper();
QWindowSystemInterface::handleTouchCancelEvent(window, msg.time, m_touchDevice.data(),
keyMapper->queryKeyboardModifiers());
m_lastTouchPoints.clear();
return true;
}
@ -519,7 +552,10 @@ bool QWindowsPointerHandler::translateTouchEvent(QWindow *window, HWND hwnd,
inputIds.insert(touchPoint.id);
// Avoid getting repeated messages for this frame if there are multiple pointerIds
QWindowsContext::user32dll.skipPointerFrameMessages(touchInfo[i].pointerInfo.pointerId);
if (QWindowsContext::user32dll.skipPointerFrameMessages)
QWindowsContext::user32dll.skipPointerFrameMessages(touchInfo[i].pointerInfo.pointerId);
else
vxkex::SkipPointerFrameMessages(touchInfo[i].pointerInfo.pointerId);
}
// Some devices send touches for each finger in a different message/frame, instead of consolidating
@ -539,8 +575,9 @@ bool QWindowsPointerHandler::translateTouchEvent(QWindow *window, HWND hwnd,
if (allStates == QEventPoint::State::Released)
m_touchInputIDToTouchPointID.clear();
QWindowSystemInterface::handleTouchEvent(window, m_touchDevice.data(), touchPoints,
QWindowsKeyMapper::queryKeyboardModifiers());
const auto *keyMapper = QWindowsContext::instance()->keyMapper();
QWindowSystemInterface::handleTouchEvent(window, msg.time, m_touchDevice.data(), touchPoints,
keyMapper->queryKeyboardModifiers());
return false; // Allow mouse messages to be generated.
}
@ -565,7 +602,12 @@ bool QWindowsPointerHandler::translatePenEvent(QWindow *window, HWND hwnd, QtWin
auto *penInfo = static_cast<POINTER_PEN_INFO *>(vPenInfo);
RECT pRect, dRect;
if (!QWindowsContext::user32dll.getPointerDeviceRects(penInfo->pointerInfo.sourceDevice, &pRect, &dRect))
BOOL bResultDeviceRects = QWindowsContext::user32dll.getPointerDeviceRects ?
QWindowsContext::user32dll.getPointerDeviceRects(penInfo->pointerInfo.sourceDevice, &pRect, &dRect) :
vxkex::GetPointerDeviceRects(penInfo->pointerInfo.sourceDevice, &pRect, &dRect);
if (!bResultDeviceRects)
return false;
const auto systemId = (qint64)penInfo->pointerInfo.sourceDevice;
@ -637,7 +679,7 @@ bool QWindowsPointerHandler::translatePenEvent(QWindow *window, HWND hwnd, QtWin
switch (msg.message) {
case WM_POINTERENTER: {
QWindowSystemInterface::handleTabletEnterLeaveProximityEvent(window, device.data(), true);
QWindowSystemInterface::handleTabletEnterLeaveProximityEvent(window, msg.time, device.data(), true);
m_windowUnderPointer = window;
// The local coordinates may fall outside the window.
// Wait until the next update to send the enter event.
@ -650,7 +692,7 @@ bool QWindowsPointerHandler::translatePenEvent(QWindow *window, HWND hwnd, QtWin
m_windowUnderPointer = nullptr;
m_currentWindow = nullptr;
}
QWindowSystemInterface::handleTabletEnterLeaveProximityEvent(window, device.data(), false);
QWindowSystemInterface::handleTabletEnterLeaveProximityEvent(window, msg.time, device.data(), false);
break;
case WM_POINTERDOWN:
case WM_POINTERUP:
@ -673,9 +715,10 @@ bool QWindowsPointerHandler::translatePenEvent(QWindow *window, HWND hwnd, QtWin
wumPlatformWindow->applyCursor();
}
}
const Qt::KeyboardModifiers keyModifiers = QWindowsKeyMapper::queryKeyboardModifiers();
const auto *keyMapper = QWindowsContext::instance()->keyMapper();
const Qt::KeyboardModifiers keyModifiers = keyMapper->queryKeyboardModifiers();
QWindowSystemInterface::handleTabletEvent(target, device.data(),
QWindowSystemInterface::handleTabletEvent(target, msg.time, device.data(),
localPos, hiResGlobalPos, mouseButtons,
pressure, xTilt, yTilt, tangentialPressure,
rotation, z, keyModifiers);
@ -726,7 +769,7 @@ bool QWindowsPointerHandler::translateMouseWheelEvent(QWindow *window,
QPoint localPos = QWindowsGeometryHint::mapFromGlobal(receiver, globalPos);
QWindowSystemInterface::handleWheelEvent(receiver, localPos, globalPos, QPoint(), angleDelta, keyModifiers);
QWindowSystemInterface::handleWheelEvent(receiver, msg.time, localPos, globalPos, QPoint(), angleDelta, keyModifiers);
return true;
}
@ -762,7 +805,8 @@ bool QWindowsPointerHandler::translateMouseEvent(QWindow *window,
: QWindowsGeometryHint::mapFromGlobal(targetHwnd, globalPos);
}
const Qt::KeyboardModifiers keyModifiers = QWindowsKeyMapper::queryKeyboardModifiers();
const auto *keyMapper = QWindowsContext::instance()->keyMapper();
const Qt::KeyboardModifiers keyModifiers = keyMapper->queryKeyboardModifiers();
QWindow *currentWindowUnderPointer = getWindowUnderPointer(window, globalPos);
if (et == QtWindows::MouseWheelEvent)
@ -781,7 +825,7 @@ bool QWindowsPointerHandler::translateMouseEvent(QWindow *window,
}
Qt::MouseEventSource source = Qt::MouseEventNotSynthesized;
const QPointingDevice *device = QWindowsMouseHandler::primaryMouse();
const QPointingDevice *device = primaryMouse();
// Following the logic of the old mouse handler, only events synthesized
// for touch screen are marked as such. On some systems, using the bit 7 of
@ -824,14 +868,14 @@ bool QWindowsPointerHandler::translateMouseEvent(QWindow *window,
&& (m_lastEventButton & mouseButtons) == 0) {
auto releaseType = mouseEvent.type == QEvent::NonClientAreaMouseMove ?
QEvent::NonClientAreaMouseButtonRelease : QEvent::MouseButtonRelease;
QWindowSystemInterface::handleMouseEvent(window, device, localPos, globalPos, mouseButtons, m_lastEventButton,
QWindowSystemInterface::handleMouseEvent(window, msg.time, device, localPos, globalPos, mouseButtons, m_lastEventButton,
releaseType, keyModifiers, source);
}
m_lastEventType = mouseEvent.type;
m_lastEventButton = mouseEvent.button;
if (mouseEvent.type >= QEvent::NonClientAreaMouseMove && mouseEvent.type <= QEvent::NonClientAreaMouseButtonDblClick) {
QWindowSystemInterface::handleMouseEvent(window, device, localPos, globalPos, mouseButtons,
QWindowSystemInterface::handleMouseEvent(window, msg.time, device, localPos, globalPos, mouseButtons,
mouseEvent.button, mouseEvent.type, keyModifiers, source);
return false; // Allow further event processing
}
@ -851,7 +895,7 @@ bool QWindowsPointerHandler::translateMouseEvent(QWindow *window,
handleEnterLeave(window, currentWindowUnderPointer, globalPos);
if (!discardEvent && mouseEvent.type != QEvent::None) {
QWindowSystemInterface::handleMouseEvent(window, device, localPos, globalPos, mouseButtons,
QWindowSystemInterface::handleMouseEvent(window, msg.time, device, localPos, globalPos, mouseButtons,
mouseEvent.button, mouseEvent.type, keyModifiers, source);
}

View File

@ -32,6 +32,8 @@
#include <setupapi.h>
#include <shellscalingapi.h>
#include "vxkex.h"
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
@ -43,12 +45,18 @@ static inline QDpi deviceDPI(HDC hdc)
static inline QDpi monitorDPI(HMONITOR hMonitor)
{
if (QWindowsContext::shcoredll.isValid()) {
UINT dpiX;
UINT dpiY;
if (SUCCEEDED(QWindowsContext::shcoredll.getDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY)))
return QDpi(dpiX, dpiY);
}
UINT dpiX;
UINT dpiY;
HRESULT hr = S_OK;
if (QWindowsContext::shcoredll.isValid())
hr = QWindowsContext::shcoredll.getDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
else
hr = vxkex::GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
if (SUCCEEDED(hr))
return QDpi(dpiX, dpiY);
return {0, 0};
}
@ -124,16 +132,28 @@ namespace {
struct DiRegKeyHandleTraits
{
using Type = HKEY;
static Type invalidValue()
static Type invalidValue() noexcept
{
// The setupapi.h functions return INVALID_HANDLE_VALUE when failing to open a registry key
return reinterpret_cast<HKEY>(INVALID_HANDLE_VALUE);
}
static bool close(Type handle) { return RegCloseKey(handle) == ERROR_SUCCESS; }
static bool close(Type handle) noexcept { return RegCloseKey(handle) == ERROR_SUCCESS; }
};
using DiRegKeyHandle = QUniqueHandle<DiRegKeyHandleTraits>;
struct DevInfoHandleTraits
{
using Type = HDEVINFO;
static Type invalidValue() noexcept
{
return reinterpret_cast<HDEVINFO>(INVALID_HANDLE_VALUE);
}
static bool close(Type handle) noexcept { return SetupDiDestroyDeviceInfoList(handle) == TRUE; }
};
using DevInfoHandle = QUniqueHandle<DevInfoHandleTraits>;
}
static void setMonitorDataFromSetupApi(QWindowsScreenData &data,
@ -183,13 +203,16 @@ static void setMonitorDataFromSetupApi(QWindowsScreenData &data,
constexpr GUID GUID_DEVINTERFACE_MONITOR = {
0xe6f07b5f, 0xee97, 0x4a90, { 0xb0, 0x76, 0x33, 0xf5, 0x7b, 0xf4, 0xea, 0xa7 }
};
const HDEVINFO devInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_MONITOR, nullptr, nullptr,
DIGCF_DEVICEINTERFACE);
const DevInfoHandle devInfo{ SetupDiGetClassDevs(
&GUID_DEVINTERFACE_MONITOR, nullptr, nullptr, DIGCF_DEVICEINTERFACE) };
if (!devInfo.isValid())
continue;
SP_DEVICE_INTERFACE_DATA deviceInterfaceData{};
deviceInterfaceData.cbSize = sizeof(deviceInterfaceData);
if (!SetupDiOpenDeviceInterfaceW(devInfo, deviceName.monitorDevicePath, DIODI_NO_ADD,
if (!SetupDiOpenDeviceInterfaceW(devInfo.get(), deviceName.monitorDevicePath, DIODI_NO_ADD,
&deviceInterfaceData)) {
qCWarning(lcQpaScreen)
<< u"Unable to open monitor interface to %1:"_s.arg(data.deviceName)
@ -198,7 +221,7 @@ static void setMonitorDataFromSetupApi(QWindowsScreenData &data,
}
DWORD requiredSize{ 0 };
if (SetupDiGetDeviceInterfaceDetailW(devInfo, &deviceInterfaceData, nullptr, 0,
if (SetupDiGetDeviceInterfaceDetailW(devInfo.get(), &deviceInterfaceData, nullptr, 0,
&requiredSize, nullptr)
|| GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
continue;
@ -209,7 +232,7 @@ static void setMonitorDataFromSetupApi(QWindowsScreenData &data,
devicePath->cbSize = sizeof(std::remove_pointer_t<decltype(devicePath)>);
SP_DEVINFO_DATA deviceInfoData{};
deviceInfoData.cbSize = sizeof(deviceInfoData);
if (!SetupDiGetDeviceInterfaceDetailW(devInfo, &deviceInterfaceData, devicePath,
if (!SetupDiGetDeviceInterfaceDetailW(devInfo.get(), &deviceInterfaceData, devicePath,
requiredSize, nullptr, &deviceInfoData)) {
qCDebug(lcQpaScreen) << u"Unable to get monitor metadata for %1:"_s.arg(data.deviceName)
<< QSystemError::windowsString();
@ -217,7 +240,7 @@ static void setMonitorDataFromSetupApi(QWindowsScreenData &data,
}
const DiRegKeyHandle edidRegistryKey{ SetupDiOpenDevRegKey(
devInfo, &deviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ) };
devInfo.get(), &deviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_READ) };
if (!edidRegistryKey.isValid())
continue;
@ -579,51 +602,58 @@ QRect QWindowsScreen::virtualGeometry(const QPlatformScreen *screen) // cf QScre
bool QWindowsScreen::setOrientationPreference(Qt::ScreenOrientation o)
{
bool result = false;
if (QWindowsContext::user32dll.setDisplayAutoRotationPreferences) {
ORIENTATION_PREFERENCE orientationPreference = ORIENTATION_PREFERENCE_NONE;
switch (o) {
case Qt::PrimaryOrientation:
break;
case Qt::PortraitOrientation:
orientationPreference = ORIENTATION_PREFERENCE_PORTRAIT;
break;
case Qt::LandscapeOrientation:
orientationPreference = ORIENTATION_PREFERENCE_LANDSCAPE;
break;
case Qt::InvertedPortraitOrientation:
orientationPreference = ORIENTATION_PREFERENCE_PORTRAIT_FLIPPED;
break;
case Qt::InvertedLandscapeOrientation:
orientationPreference = ORIENTATION_PREFERENCE_LANDSCAPE_FLIPPED;
break;
}
result = QWindowsContext::user32dll.setDisplayAutoRotationPreferences(orientationPreference);
ORIENTATION_PREFERENCE orientationPreference = ORIENTATION_PREFERENCE_NONE;
switch (o) {
case Qt::PrimaryOrientation:
break;
case Qt::PortraitOrientation:
orientationPreference = ORIENTATION_PREFERENCE_PORTRAIT;
break;
case Qt::LandscapeOrientation:
orientationPreference = ORIENTATION_PREFERENCE_LANDSCAPE;
break;
case Qt::InvertedPortraitOrientation:
orientationPreference = ORIENTATION_PREFERENCE_PORTRAIT_FLIPPED;
break;
case Qt::InvertedLandscapeOrientation:
orientationPreference = ORIENTATION_PREFERENCE_LANDSCAPE_FLIPPED;
break;
}
if (QWindowsContext::user32dll.setDisplayAutoRotationPreferences)
result = QWindowsContext::user32dll.setDisplayAutoRotationPreferences(orientationPreference);
else
result = vxkex::SetDisplayAutoRotationPreferences(orientationPreference);
return result;
}
Qt::ScreenOrientation QWindowsScreen::orientationPreference()
{
Qt::ScreenOrientation result = Qt::PrimaryOrientation;
if (QWindowsContext::user32dll.getDisplayAutoRotationPreferences) {
DWORD orientationPreference = ORIENTATION_PREFERENCE_NONE;
if (QWindowsContext::user32dll.getDisplayAutoRotationPreferences(&orientationPreference)) {
switch (orientationPreference) {
case ORIENTATION_PREFERENCE_NONE:
break;
case ORIENTATION_PREFERENCE_LANDSCAPE:
result = Qt::LandscapeOrientation;
break;
case ORIENTATION_PREFERENCE_PORTRAIT:
result = Qt::PortraitOrientation;
break;
case ORIENTATION_PREFERENCE_LANDSCAPE_FLIPPED:
result = Qt::InvertedLandscapeOrientation;
break;
case ORIENTATION_PREFERENCE_PORTRAIT_FLIPPED:
result = Qt::InvertedPortraitOrientation;
break;
}
ORIENTATION_PREFERENCE orientationPreference = ORIENTATION_PREFERENCE_NONE;
BOOL bResult = TRUE;
if (QWindowsContext::user32dll.getDisplayAutoRotationPreferences)
bResult = QWindowsContext::user32dll.getDisplayAutoRotationPreferences((DWORD *)&orientationPreference);
else
bResult = vxkex::GetDisplayAutoRotationPreferences(&orientationPreference);
if (bResult) {
switch (orientationPreference) {
case ORIENTATION_PREFERENCE_NONE:
break;
case ORIENTATION_PREFERENCE_LANDSCAPE:
result = Qt::LandscapeOrientation;
break;
case ORIENTATION_PREFERENCE_PORTRAIT:
result = Qt::PortraitOrientation;
break;
case ORIENTATION_PREFERENCE_LANDSCAPE_FLIPPED:
result = Qt::InvertedLandscapeOrientation;
break;
case ORIENTATION_PREFERENCE_PORTRAIT_FLIPPED:
result = Qt::InvertedPortraitOrientation;
break;
}
}
return result;
@ -704,11 +734,15 @@ void QWindowsScreenManager::initialize()
handleScreenChanges();
}
QWindowsScreenManager::~QWindowsScreenManager()
void QWindowsScreenManager::destroyWindow()
{
qCDebug(lcQpaScreen) << "Destroying display change observer" << m_displayChangeObserver;
DestroyWindow(m_displayChangeObserver);
m_displayChangeObserver = nullptr;
}
QWindowsScreenManager::~QWindowsScreenManager() = default;
bool QWindowsScreenManager::isSingleScreen()
{
return QWindowsContext::instance()->screenManager().screens().size() < 2;

View File

@ -7,26 +7,29 @@
#include "qwindowsmenu.h"
#include "qwindowsdialoghelpers.h"
#include "qwindowscontext.h"
#include "qwindowsiconengine.h"
#include "qwindowsintegration.h"
#if QT_CONFIG(systemtrayicon)
# include "qwindowssystemtrayicon.h"
#endif
#include "qwindowsscreen.h"
#include "qwindowswindow.h"
#include <commctrl.h>
#include <objbase.h>
#ifndef Q_CC_MINGW
# include <commoncontrols.h>
#endif
#include <commoncontrols.h>
#include <shellapi.h>
#include <QtCore/qapplicationstatic.h>
#include <QtCore/qvariant.h>
#include <QtCore/qcoreapplication.h>
#include <QtCore/qdebug.h>
#include <QtCore/qsysinfo.h>
#include <QtCore/qcache.h>
#include <QtCore/qthread.h>
#include <QtCore/qqueue.h>
#include <QtCore/qmutex.h>
#include <QtCore/qwaitcondition.h>
#include <QtCore/qoperatingsystemversion.h>
#include <QtGui/qcolor.h>
#include <QtGui/qpalette.h>
#include <QtGui/qguiapplication.h>
@ -42,7 +45,7 @@
#include <algorithm>
#include <VersionHelpers.h>
#include "vxkex.h"
#if QT_CONFIG(cpp_winrt)
# include <QtCore/private/qt_winrtbase_p.h>
@ -50,10 +53,6 @@
# include <winrt/Windows.UI.ViewManagement.h>
#endif // QT_CONFIG(cpp_winrt)
#if defined(__IImageList_INTERFACE_DEFINED__) && defined(__IID_DEFINED__)
# define USE_IIMAGELIST
#endif
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
@ -81,11 +80,15 @@ static inline QColor mixColors(const QColor &c1, const QColor &c2)
(c1.blue() + c2.blue()) / 2};
}
static inline QColor getSysColor(int index)
{
COLORREF cr = GetSysColor(index);
return QColor(GetRValue(cr), GetGValue(cr), GetBValue(cr));
}
enum AccentColorLevel {
AccentColorDarkest,
AccentColorDarker,
AccentColorDark,
AccentColorNormal,
AccentColorLight,
AccentColorLighter,
AccentColorLightest
};
#if QT_CONFIG(cpp_winrt)
static constexpr QColor getSysColor(winrt::Windows::UI::Color &&color)
@ -94,111 +97,182 @@ static constexpr QColor getSysColor(winrt::Windows::UI::Color &&color)
}
#endif
[[maybe_unused]] [[nodiscard]] static inline QColor qt_accentColor(AccentColorLevel level)
{
QColor accent;
QColor accentLight;
QColor accentLighter;
QColor accentLightest;
QColor accentDark;
QColor accentDarker;
QColor accentDarkest;
#if QT_CONFIG(cpp_winrt)
if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows10)
{
using namespace winrt::Windows::UI::ViewManagement;
const auto settings = UISettings();
accent = getSysColor(settings.GetColorValue(UIColorType::Accent));
accentLight = getSysColor(settings.GetColorValue(UIColorType::AccentLight1));
accentLighter = getSysColor(settings.GetColorValue(UIColorType::AccentLight2));
accentLightest = getSysColor(settings.GetColorValue(UIColorType::AccentLight3));
accentDark = getSysColor(settings.GetColorValue(UIColorType::AccentDark1));
accentDarker = getSysColor(settings.GetColorValue(UIColorType::AccentDark2));
accentDarkest = getSysColor(settings.GetColorValue(UIColorType::AccentDark3));
}
#endif
if (!accent.isValid())
{
const QWinRegistryKey registry(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\DWM)");
if (!registry.isValid())
return {};
const QVariant value = registry.value(L"AccentColor");
if (!value.isValid())
return {};
// The retrieved value is in the #AABBGGRR format, we need to
// convert it to the #AARRGGBB format which Qt expects.
const QColor abgr = QColor::fromRgba(qvariant_cast<DWORD>(value));
if (!abgr.isValid())
return {};
accent = QColor::fromRgb(abgr.blue(), abgr.green(), abgr.red(), abgr.alpha());
accentLight = accent.lighter(120);
accentLighter = accentLight.lighter(120);
accentLightest = accentLighter.lighter(120);
accentDarkest = accent.darker(120 * 120 * 120);
accentDark = accent.darker(120);
accentDarker = accentDark.darker(120);
accentDarkest = accentDarker.darker(120);
}
switch (level) {
case AccentColorDarkest:
return accentDarkest;
case AccentColorDarker:
return accentDarker;
case AccentColorDark:
return accentDark;
case AccentColorLight:
return accentLight;
case AccentColorLighter:
return accentLighter;
case AccentColorLightest:
return accentLightest;
default:
return accent;
}
}
static inline QColor getSysColor(int index)
{
COLORREF cr = GetSysColor(index);
return QColor(GetRValue(cr), GetGValue(cr), GetBValue(cr));
}
// QTBUG-48823/Windows 10: SHGetFileInfo() (as called by item views on file system
// models has been observed to trigger a WM_PAINT on the mainwindow. Suppress the
// behavior by running it in a thread.
struct QShGetFileInfoParams
{
QShGetFileInfoParams(const QString &fn, DWORD a, SHFILEINFO *i, UINT f, bool *r)
: fileName(fn), attributes(a), flags(f), info(i), result(r)
{ }
const QString &fileName;
const DWORD attributes;
const UINT flags;
SHFILEINFO *const info;
bool *const result;
};
class QShGetFileInfoThread : public QThread
{
public:
explicit QShGetFileInfoThread()
: QThread(), m_params(nullptr)
struct Task
{
connect(this, &QThread::finished, this, &QObject::deleteLater);
Task(const QString &fn, DWORD a, UINT f)
: fileName(fn), attributes(a), flags(f)
{}
Q_DISABLE_COPY(Task)
~Task()
{
DestroyIcon(hIcon);
hIcon = 0;
}
// Request
const QString fileName;
const DWORD attributes;
const UINT flags;
// Result
HICON hIcon = 0;
int iIcon = -1;
bool finished = false;
bool resultValid() const { return hIcon != 0 && iIcon >= 0 && finished; }
};
QShGetFileInfoThread()
: QThread()
{
start();
}
~QShGetFileInfoThread()
{
cancel();
wait();
}
QSharedPointer<Task> getNextTask()
{
QMutexLocker l(&m_waitForTaskMutex);
while (!isInterruptionRequested()) {
if (!m_taskQueue.isEmpty())
return m_taskQueue.dequeue();
m_waitForTaskCondition.wait(&m_waitForTaskMutex);
}
return nullptr;
}
void run() override
{
QComHelper comHelper(COINIT_MULTITHREADED);
QMutexLocker readyLocker(&m_readyMutex);
while (!m_cancelled.loadRelaxed()) {
if (!m_params && !m_cancelled.loadRelaxed()
&& !m_readyCondition.wait(&m_readyMutex, QDeadlineTimer(1000ll)))
continue;
if (m_params) {
const QString fileName = m_params->fileName;
while (!isInterruptionRequested()) {
auto task = getNextTask();
if (task) {
SHFILEINFO info;
const bool result = SHGetFileInfo(reinterpret_cast<const wchar_t *>(fileName.utf16()),
m_params->attributes, &info, sizeof(SHFILEINFO),
m_params->flags);
m_doneMutex.lock();
if (!m_cancelled.loadRelaxed()) {
*m_params->result = result;
memcpy(m_params->info, &info, sizeof(SHFILEINFO));
const bool result = SHGetFileInfo(reinterpret_cast<const wchar_t *>(task->fileName.utf16()),
task->attributes, &info, sizeof(SHFILEINFO),
task->flags);
if (result) {
task->hIcon = info.hIcon;
task->iIcon = info.iIcon;
}
m_params = nullptr;
task->finished = true;
m_doneCondition.wakeAll();
m_doneMutex.unlock();
}
}
}
bool runWithParams(QShGetFileInfoParams *params, qint64 timeOutMSecs)
void runWithParams(const QSharedPointer<Task> &task,
std::chrono::milliseconds timeout = std::chrono::milliseconds(5000))
{
{
QMutexLocker l(&m_waitForTaskMutex);
m_taskQueue.enqueue(task);
m_waitForTaskCondition.wakeAll();
}
QMutexLocker doneLocker(&m_doneMutex);
m_readyMutex.lock();
m_params = params;
m_readyCondition.wakeAll();
m_readyMutex.unlock();
return m_doneCondition.wait(&m_doneMutex, QDeadlineTimer(timeOutMSecs));
while (!task->finished && !isInterruptionRequested()) {
if (!m_doneCondition.wait(&m_doneMutex, QDeadlineTimer(timeout)))
return;
}
}
void cancel()
{
QMutexLocker doneLocker(&m_doneMutex);
m_cancelled.storeRelaxed(1);
m_readyCondition.wakeAll();
requestInterruption();
m_doneCondition.wakeAll();
m_waitForTaskCondition.wakeAll();
}
private:
QShGetFileInfoParams *m_params;
QAtomicInt m_cancelled;
QWaitCondition m_readyCondition;
QQueue<QSharedPointer<Task>> m_taskQueue;
QWaitCondition m_doneCondition;
QMutex m_readyMutex;
QWaitCondition m_waitForTaskCondition;
QMutex m_doneMutex;
QMutex m_waitForTaskMutex;
};
static bool shGetFileInfoBackground(const QString &fileName, DWORD attributes,
SHFILEINFO *info, UINT flags,
qint64 timeOutMSecs = 5000)
{
static QShGetFileInfoThread *getFileInfoThread = nullptr;
if (!getFileInfoThread) {
getFileInfoThread = new QShGetFileInfoThread;
getFileInfoThread->start();
}
bool result = false;
QShGetFileInfoParams params(fileName, attributes, info, flags, &result);
if (!getFileInfoThread->runWithParams(&params, timeOutMSecs)) {
// Cancel and reset getFileInfoThread. It'll
// be reinitialized the next time we get called.
getFileInfoThread->cancel();
getFileInfoThread = nullptr;
qWarning().noquote() << "SHGetFileInfo() timed out for " << fileName;
return false;
}
return result;
}
Q_APPLICATION_STATIC(QShGetFileInfoThread, s_shGetFileInfoThread)
// from QStyle::standardPalette
static inline QPalette standardPalette()
@ -223,46 +297,20 @@ static QColor placeHolderColor(QColor textColor)
return textColor;
}
[[maybe_unused]] [[nodiscard]] static inline QColor qt_accentColor()
{
const QWinRegistryKey registry(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\DWM)");
if (!registry.isValid())
return {};
const QVariant value = registry.value(L"AccentColor");
if (!value.isValid())
return {};
// The retrieved value is in the #AABBGGRR format, we need to
// convert it to the #AARRGGBB format which Qt expects.
const QColor abgr = QColor::fromRgba(qvariant_cast<DWORD>(value));
if (!abgr.isValid())
return {};
return QColor::fromRgb(abgr.blue(), abgr.green(), abgr.red(), abgr.alpha());
}
/*
This is used when the theme is light mode, and when the theme is dark but the
application doesn't support dark mode. In the latter case, we need to check.
*/
static void populateLightSystemBasePalette(QPalette &result)
void QWindowsTheme::populateLightSystemBasePalette(QPalette &result)
{
QColor background = getSysColor(COLOR_BTNFACE);
QColor textColor = getSysColor(COLOR_WINDOWTEXT);
QColor accent = qt_accentColor();
QColor accentDarkest = accent.darker(120 * 120 * 120);
const QColor background = getSysColor(COLOR_BTNFACE);
const QColor textColor = getSysColor(COLOR_WINDOWTEXT);
#if QT_CONFIG(cpp_winrt)
if (IsWindows10OrGreater())
{
// respect the Windows 11 accent color
using namespace winrt::Windows::UI::ViewManagement;
const auto settings = UISettings();
const QColor accentDark = qt_accentColor(AccentColorDark);
const QColor accentDarker = qt_accentColor(AccentColorDarker);
const QColor accentDarkest = qt_accentColor(AccentColorDarkest);
accent = getSysColor(settings.GetColorValue(UIColorType::Accent));
accentDarkest = getSysColor(settings.GetColorValue(UIColorType::AccentDark3));
}
#endif
const QColor linkColor = accent;
const QColor linkColor = accentDarker;
const QColor btnFace = background;
const QColor btnHighlight = getSysColor(COLOR_BTNHIGHLIGHT);
@ -281,7 +329,7 @@ static void populateLightSystemBasePalette(QPalette &result)
result.setColor(QPalette::Midlight, getSysColor(COLOR_3DLIGHT));
result.setColor(QPalette::Shadow, getSysColor(COLOR_3DDKSHADOW));
result.setColor(QPalette::HighlightedText, getSysColor(COLOR_HIGHLIGHTTEXT));
result.setColor(QPalette::Accent, accent);
result.setColor(QPalette::Accent, accentDark); // default accent color for controls on Light mode is AccentDark1
result.setColor(QPalette::Link, linkColor);
result.setColor(QPalette::LinkVisited, accentDarkest);
@ -294,11 +342,11 @@ static void populateLightSystemBasePalette(QPalette &result)
result.setColor(QPalette::Midlight, result.button().color().lighter(110));
}
static void populateDarkSystemBasePalette(QPalette &result)
void QWindowsTheme::populateDarkSystemBasePalette(QPalette &result)
{
QColor foreground = Qt::white;
QColor background = QColor(0x1E, 0x1E, 0x1E);
QColor accent = qt_accentColor();
QColor accent = qt_accentColor(AccentColorNormal);
QColor accentDark = accent.darker(120);
QColor accentDarker = accentDark.darker(120);
QColor accentDarkest = accentDarker.darker(120);
@ -307,7 +355,7 @@ static void populateDarkSystemBasePalette(QPalette &result)
QColor accentLightest = accentLighter.lighter(120);
#if QT_CONFIG(cpp_winrt)
if (IsWindows10OrGreater())
if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows10)
{
using namespace winrt::Windows::UI::ViewManagement;
const auto settings = UISettings();
@ -315,25 +363,27 @@ static void populateDarkSystemBasePalette(QPalette &result)
// We have to craft a palette from these colors. The settings.UIElementColor(UIElementType) API
// returns the old system colors, not the dark mode colors. If the background is black (which it
// usually), then override it with a dark gray instead so that we can go up and down the lightness.
foreground = getSysColor(settings.GetColorValue(UIColorType::Foreground));
background = [&settings]() -> QColor {
auto systemBackground = getSysColor(settings.GetColorValue(UIColorType::Background));
if (systemBackground == Qt::black)
systemBackground = QColor(0x1E, 0x1E, 0x1E);
return systemBackground;
}();
accent = getSysColor(settings.GetColorValue(UIColorType::Accent));
accentDark = getSysColor(settings.GetColorValue(UIColorType::AccentDark1));
accentDarker = getSysColor(settings.GetColorValue(UIColorType::AccentDark2));
accentDarkest = getSysColor(settings.GetColorValue(UIColorType::AccentDark3));
accentLight = getSysColor(settings.GetColorValue(UIColorType::AccentLight1));
accentLighter = getSysColor(settings.GetColorValue(UIColorType::AccentLight2));
accentLightest = getSysColor(settings.GetColorValue(UIColorType::AccentLight3));
if (QWindowsTheme::queryColorScheme() == Qt::ColorScheme::Dark) {
// the system is actually running in dark mode, so UISettings will give us dark colors
foreground = getSysColor(settings.GetColorValue(UIColorType::Foreground));
background = [&settings]() -> QColor {
auto systemBackground = getSysColor(settings.GetColorValue(UIColorType::Background));
if (systemBackground == Qt::black)
systemBackground = QColor(0x1E, 0x1E, 0x1E);
return systemBackground;
}();
accent = qt_accentColor(AccentColorNormal);
accentDark = qt_accentColor(AccentColorDark);
accentDarker = qt_accentColor(AccentColorDarker);
accentDarkest = qt_accentColor(AccentColorDarkest);
accentLight = qt_accentColor(AccentColorLight);
accentLighter = qt_accentColor(AccentColorLighter);
accentLightest = qt_accentColor(AccentColorLightest);
}
}
#endif
const QColor linkColor = accent;
const QColor linkColor = accentLightest;
const QColor buttonColor = background.lighter(200);
result.setColor(QPalette::All, QPalette::WindowText, foreground);
@ -354,12 +404,12 @@ static void populateDarkSystemBasePalette(QPalette &result)
result.setColor(QPalette::All, QPalette::Highlight, accent);
result.setColor(QPalette::All, QPalette::HighlightedText, accent.lightness() > 128 ? Qt::black : Qt::white);
result.setColor(QPalette::All, QPalette::Link, linkColor);
result.setColor(QPalette::All, QPalette::LinkVisited, accentDarkest);
result.setColor(QPalette::All, QPalette::LinkVisited, accentLighter);
result.setColor(QPalette::All, QPalette::AlternateBase, accentDarkest);
result.setColor(QPalette::All, QPalette::ToolTipBase, buttonColor);
result.setColor(QPalette::All, QPalette::ToolTipText, foreground.darker(120));
result.setColor(QPalette::All, QPalette::PlaceholderText, placeHolderColor(foreground));
result.setColor(QPalette::All, QPalette::Accent, accent);
result.setColor(QPalette::All, QPalette::Accent, accentLighter);
}
static inline QPalette toolTipPalette(const QPalette &systemPalette, bool light)
@ -451,6 +501,8 @@ QWindowsTheme *QWindowsTheme::m_instance = nullptr;
QWindowsTheme::QWindowsTheme()
{
m_instance = this;
s_colorScheme = Qt::ColorScheme::Unknown; // Used inside QWindowsTheme::effectiveColorScheme();
s_colorScheme = QWindowsTheme::effectiveColorScheme();
std::fill(m_fonts, m_fonts + NFonts, nullptr);
std::fill(m_palettes, m_palettes + NPalettes, nullptr);
refresh();
@ -472,7 +524,10 @@ static inline QStringList iconThemeSearchPaths()
static inline QStringList styleNames()
{
return { QStringLiteral("WindowsVista"), QStringLiteral("Windows") };
QStringList styles = { QStringLiteral("WindowsVista"), QStringLiteral("Windows") };
if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows11)
styles.prepend(QStringLiteral("Windows11"));
return styles;
}
static inline int uiEffects()
@ -537,7 +592,46 @@ QVariant QWindowsTheme::themeHint(ThemeHint hint) const
Qt::ColorScheme QWindowsTheme::colorScheme() const
{
return QWindowsContext::isDarkMode() ? Qt::ColorScheme::Dark : Qt::ColorScheme::Light;
return QWindowsTheme::effectiveColorScheme();
}
Qt::ColorScheme QWindowsTheme::effectiveColorScheme()
{
auto integration = QWindowsIntegration::instance();
if (queryHighContrast())
return Qt::ColorScheme::Unknown;
if (s_colorSchemeOverride != Qt::ColorScheme::Unknown)
return s_colorSchemeOverride;
if (s_colorScheme != Qt::ColorScheme::Unknown)
return s_colorScheme;
if (!integration->darkModeHandling().testFlag(QWindowsApplication::DarkModeStyle))
return Qt::ColorScheme::Light;
return queryColorScheme();
}
void QWindowsTheme::requestColorScheme(Qt::ColorScheme scheme)
{
s_colorSchemeOverride = scheme;
handleSettingsChanged();
}
void QWindowsTheme::handleSettingsChanged()
{
const auto oldColorScheme = s_colorScheme;
s_colorScheme = Qt::ColorScheme::Unknown; // make effectiveColorScheme() query registry
const auto newColorScheme = effectiveColorScheme();
const bool colorSchemeChanged = newColorScheme != oldColorScheme;
s_colorScheme = newColorScheme;
auto integration = QWindowsIntegration::instance();
integration->updateApplicationBadge();
if (integration->darkModeHandling().testFlag(QWindowsApplication::DarkModeStyle)) {
QWindowsTheme::instance()->refresh();
QWindowSystemInterface::handleThemeChange<QWindowSystemInterface::SynchronousDelivery>();
}
if (colorSchemeChanged) {
for (QWindowsWindow *w : std::as_const(QWindowsContext::instance()->windows()))
w->setDarkBorder(s_colorScheme == Qt::ColorScheme::Dark);
}
}
void QWindowsTheme::clearPalettes()
@ -551,32 +645,18 @@ void QWindowsTheme::refreshPalettes()
if (!QGuiApplication::desktopSettingsAware())
return;
const bool light =
!QWindowsContext::isDarkMode()
effectiveColorScheme() != Qt::ColorScheme::Dark
|| !QWindowsIntegration::instance()->darkModeHandling().testFlag(QWindowsApplication::DarkModeStyle);
clearPalettes();
m_palettes[SystemPalette] = new QPalette(QWindowsTheme::systemPalette(light ? Qt::ColorScheme::Light : Qt::ColorScheme::Dark));
m_palettes[SystemPalette] = new QPalette(QWindowsTheme::systemPalette(s_colorScheme));
m_palettes[ToolTipPalette] = new QPalette(toolTipPalette(*m_palettes[SystemPalette], light));
m_palettes[MenuPalette] = new QPalette(menuPalette(*m_palettes[SystemPalette], light));
m_palettes[MenuBarPalette] = menuBarPalette(*m_palettes[MenuPalette], light);
if (!light) {
QColor accent = qt_accentColor();
QColor accentLight = accent.lighter(120);
QColor accentDarkest = accent.darker(120 * 120 * 120);
#if QT_CONFIG(cpp_winrt)
if (IsWindows10OrGreater())
{
using namespace winrt::Windows::UI::ViewManagement;
const auto settings = UISettings();
accent = getSysColor(settings.GetColorValue(UIColorType::Accent));
accentLight = getSysColor(settings.GetColorValue(UIColorType::AccentLight1));
accentDarkest = getSysColor(settings.GetColorValue(UIColorType::AccentDark3));
}
#endif
m_palettes[CheckBoxPalette] = new QPalette(*m_palettes[SystemPalette]);
m_palettes[CheckBoxPalette]->setColor(QPalette::Active, QPalette::Base, accent);
m_palettes[CheckBoxPalette]->setColor(QPalette::Active, QPalette::Button, accentLight);
m_palettes[CheckBoxPalette]->setColor(QPalette::Inactive, QPalette::Base, accentDarkest);
m_palettes[CheckBoxPalette]->setColor(QPalette::Active, QPalette::Base, qt_accentColor(AccentColorNormal));
m_palettes[CheckBoxPalette]->setColor(QPalette::Active, QPalette::Button, qt_accentColor(AccentColorLighter));
m_palettes[CheckBoxPalette]->setColor(QPalette::Inactive, QPalette::Base, qt_accentColor(AccentColorDarkest));
m_palettes[RadioButtonPalette] = new QPalette(*m_palettes[CheckBoxPalette]);
}
}
@ -586,15 +666,15 @@ QPalette QWindowsTheme::systemPalette(Qt::ColorScheme colorScheme)
QPalette result = standardPalette();
switch (colorScheme) {
case Qt::ColorScheme::Light:
populateLightSystemBasePalette(result);
break;
case Qt::ColorScheme::Dark:
populateDarkSystemBasePalette(result);
break;
default:
qFatal("Unknown color scheme");
break;
case Qt::ColorScheme::Unknown:
// when a high-contrast theme is active or when we fail to read, assume light
Q_FALLTHROUGH();
case Qt::ColorScheme::Light:
populateLightSystemBasePalette(result);
break;
case Qt::ColorScheme::Dark:
populateDarkSystemBasePalette(result);
break;
}
if (result.window() != result.base()) {
@ -675,14 +755,11 @@ void QWindowsTheme::refreshFonts()
fixedFont.setStyleHint(QFont::TypeWriter);
LOGFONT lfIconTitleFont;
QFont iconTitleFont;
if (QWindowsContext::user32dll.systemParametersInfoForDpi) {
if (QWindowsContext::user32dll.systemParametersInfoForDpi)
QWindowsContext::user32dll.systemParametersInfoForDpi(SPI_GETICONTITLELOGFONT, sizeof(lfIconTitleFont), &lfIconTitleFont, 0, dpi);
iconTitleFont = QWindowsFontDatabase::LOGFONT_to_QFont(lfIconTitleFont, dpi);
} else {
SystemParametersInfo(SPI_GETICONTITLELOGFONT, sizeof(lfIconTitleFont), &lfIconTitleFont, 0);
iconTitleFont = QWindowsFontDatabase::LOGFONT_to_QFont(lfIconTitleFont);
}
else
vxkex::SystemParametersInfoForDpi(SPI_GETICONTITLELOGFONT, sizeof(lfIconTitleFont), &lfIconTitleFont, 0, dpi);
const QFont iconTitleFont = QWindowsFontDatabase::LOGFONT_to_QFont(lfIconTitleFont, dpi);
m_fonts[SystemFont] = new QFont(QWindowsFontDatabase::systemDefaultFont());
m_fonts[MenuFont] = new QFont(menuFont);
@ -739,11 +816,7 @@ void QWindowsTheme::refreshIconPixmapSizes()
fileIconSizes[LargeFileIcon] + fileIconSizes[LargeFileIcon] / 2;
fileIconSizes[JumboFileIcon] = 8 * fileIconSizes[LargeFileIcon]; // empirical, has not been observed to work
#ifdef USE_IIMAGELIST
int *availEnd = fileIconSizes + JumboFileIcon + 1;
#else
int *availEnd = fileIconSizes + LargeFileIcon + 1;
#endif // USE_IIMAGELIST
m_fileIconSizes = QAbstractFileIconEngine::toSizeList(fileIconSizes, availEnd);
qCDebug(lcQpaWindow) << __FUNCTION__ << m_fileIconSizes;
}
@ -770,7 +843,6 @@ QPixmap QWindowsTheme::standardPixmap(StandardPixmap sp, const QSizeF &pixmapSiz
{
int resourceId = -1;
SHSTOCKICONID stockId = SIID_INVALID;
UINT stockFlags = 0;
LPCTSTR iconName = nullptr;
switch (sp) {
case DriveCDIcon:
@ -794,14 +866,12 @@ QPixmap QWindowsTheme::standardPixmap(StandardPixmap sp, const QSizeF &pixmapSiz
resourceId = 7;
break;
case FileLinkIcon:
stockFlags = SHGSI_LINKOVERLAY;
Q_FALLTHROUGH();
case FileIcon:
stockId = SIID_DOCNOASSOC;
resourceId = 1;
break;
case DirLinkIcon:
stockFlags = SHGSI_LINKOVERLAY;
Q_FALLTHROUGH();
case DirClosedIcon:
case DirIcon:
@ -815,7 +885,6 @@ QPixmap QWindowsTheme::standardPixmap(StandardPixmap sp, const QSizeF &pixmapSiz
resourceId = 16;
break;
case DirLinkOpenIcon:
stockFlags = SHGSI_LINKOVERLAY;
Q_FALLTHROUGH();
case DirOpenIcon:
stockId = SIID_FOLDEROPEN;
@ -855,27 +924,42 @@ QPixmap QWindowsTheme::standardPixmap(StandardPixmap sp, const QSizeF &pixmapSiz
break;
}
// Even with SHGSI_LINKOVERLAY flag set, loaded Icon with SHDefExtractIcon doesn't have
// any overlay, so we avoid SHGSI_LINKOVERLAY flag and draw it manually (QTBUG-131843)
const auto drawLinkOverlayIconIfNeeded = [](StandardPixmap sp, QPixmap &pixmap, QSizeF pixmapSize) {
if (sp == FileLinkIcon || sp == DirLinkIcon || sp == DirLinkOpenIcon) {
QPainter painter(&pixmap);
const QSizeF linkSize = pixmapSize / (pixmapSize.height() >= 48 ? 3 : 2);
static constexpr auto LinkOverlayIconId = 16769;
const QPixmap link = loadIconFromShell32(LinkOverlayIconId, linkSize.toSize());
const int yPos = pixmap.height() - link.size().height();
painter.drawPixmap(0, yPos, int(linkSize.width()), int(linkSize.height()), link);
}
};
if (stockId != SIID_INVALID) {
QPixmap pixmap;
SHSTOCKICONINFO iconInfo;
memset(&iconInfo, 0, sizeof(iconInfo));
iconInfo.cbSize = sizeof(iconInfo);
stockFlags |= (pixmapSize.width() > 16 ? SHGFI_LARGEICON : SHGFI_SMALLICON);
if (SHGetStockIconInfo(stockId, SHGFI_ICON | stockFlags, &iconInfo) == S_OK) {
pixmap = qt_pixmapFromWinHICON(iconInfo.hIcon);
DestroyIcon(iconInfo.hIcon);
return pixmap;
constexpr UINT stockFlags = SHGSI_ICONLOCATION;
if (SHGetStockIconInfo(stockId, stockFlags, &iconInfo) == S_OK) {
const auto iconSize = pixmapSize.width();
HICON icon;
if (SHDefExtractIcon(iconInfo.szPath, iconInfo.iIcon, 0, &icon, nullptr, iconSize) == S_OK) {
QPixmap pixmap = qt_pixmapFromWinHICON(icon);
DestroyIcon(icon);
if (!pixmap.isNull()) {
drawLinkOverlayIconIfNeeded(sp, pixmap, pixmap.size());
return pixmap;
}
}
}
}
if (resourceId != -1) {
QPixmap pixmap = loadIconFromShell32(resourceId, pixmapSize);
if (!pixmap.isNull()) {
if (sp == FileLinkIcon || sp == DirLinkIcon || sp == DirLinkOpenIcon) {
QPainter painter(&pixmap);
QPixmap link = loadIconFromShell32(30, pixmapSize);
painter.drawPixmap(0, 0, int(pixmapSize.width()), int(pixmapSize.height()), link);
}
drawLinkOverlayIconIfNeeded(sp, pixmap, pixmapSize);
return pixmap;
}
}
@ -934,10 +1018,10 @@ public:
// Shell image list helper functions.
static QPixmap pixmapFromShellImageList(int iImageList, const SHFILEINFO &info)
static QPixmap pixmapFromShellImageList(int iImageList, int iIcon)
{
QPixmap result;
#ifdef USE_IIMAGELIST
// For MinGW:
static const IID iID_IImageList = {0x46eb5926, 0x582e, 0x4017, {0x9f, 0xdf, 0xe8, 0x99, 0x8d, 0xaa, 0x9, 0x50}};
@ -946,16 +1030,13 @@ static QPixmap pixmapFromShellImageList(int iImageList, const SHFILEINFO &info)
if (hr != S_OK)
return result;
HICON hIcon;
hr = imageList->GetIcon(info.iIcon, ILD_TRANSPARENT, &hIcon);
hr = imageList->GetIcon(iIcon, ILD_TRANSPARENT, &hIcon);
if (hr == S_OK) {
result = qt_pixmapFromWinHICON(hIcon);
DestroyIcon(hIcon);
}
imageList->Release();
#else
Q_UNUSED(iImageList);
Q_UNUSED(info);
#endif // USE_IIMAGELIST
return result;
}
@ -1007,13 +1088,9 @@ QPixmap QWindowsFileIconEngine::filePixmap(const QSize &size, QIcon::Mode, QIcon
const int width = int(size.width());
const int iconSize = width > fileIconSizes[SmallFileIcon] ? SHGFI_LARGEICON : SHGFI_SMALLICON;
const int requestedImageListSize =
#ifdef USE_IIMAGELIST
width > fileIconSizes[ExtraLargeFileIcon]
? sHIL_JUMBO
: (width > fileIconSizes[LargeFileIcon] ? sHIL_EXTRALARGE : 0);
#else
0;
#endif // !USE_IIMAGELIST
bool cacheableDirIcon = fileInfo().isDir() && !fileInfo().isRoot();
if (cacheableDirIcon) {
QMutexLocker locker(&mx);
@ -1029,7 +1106,6 @@ QPixmap QWindowsFileIconEngine::filePixmap(const QSize &size, QIcon::Mode, QIcon
}
}
SHFILEINFO info;
unsigned int flags = SHGFI_ICON | iconSize | SHGFI_SYSICONINDEX | SHGFI_ADDOVERLAYS | SHGFI_OVERLAYINDEX;
DWORD attributes = 0;
QString path = filePath;
@ -1041,43 +1117,43 @@ QPixmap QWindowsFileIconEngine::filePixmap(const QSize &size, QIcon::Mode, QIcon
flags |= SHGFI_USEFILEATTRIBUTES;
attributes |= FILE_ATTRIBUTE_NORMAL;
}
const bool val = shGetFileInfoBackground(path, attributes, &info, flags);
auto task = QSharedPointer<QShGetFileInfoThread::Task>(
new QShGetFileInfoThread::Task(path, attributes, flags));
s_shGetFileInfoThread()->runWithParams(task);
// Even if GetFileInfo returns a valid result, hIcon can be empty in some cases
if (val && info.hIcon) {
if (task->resultValid()) {
QString key;
if (cacheableDirIcon) {
if (useDefaultFolderIcon && defaultFolderIIcon < 0)
defaultFolderIIcon = info.iIcon;
defaultFolderIIcon = task->iIcon;
//using the unique icon index provided by windows save us from duplicate keys
key = dirIconPixmapCacheKey(info.iIcon, iconSize, requestedImageListSize);
key = dirIconPixmapCacheKey(task->iIcon, iconSize, requestedImageListSize);
QPixmapCache::find(key, &pixmap);
if (!pixmap.isNull()) {
QMutexLocker locker(&mx);
dirIconEntryCache.insert(filePath, FakePointer<int>::create(info.iIcon));
dirIconEntryCache.insert(filePath, FakePointer<int>::create(task->iIcon));
}
}
if (pixmap.isNull()) {
if (requestedImageListSize) {
pixmap = pixmapFromShellImageList(requestedImageListSize, info);
pixmap = pixmapFromShellImageList(requestedImageListSize, task->iIcon);
if (pixmap.isNull() && requestedImageListSize == sHIL_JUMBO)
pixmap = pixmapFromShellImageList(sHIL_EXTRALARGE, info);
pixmap = pixmapFromShellImageList(sHIL_EXTRALARGE, task->iIcon);
}
if (pixmap.isNull())
pixmap = qt_pixmapFromWinHICON(info.hIcon);
pixmap = qt_pixmapFromWinHICON(task->hIcon);
if (!pixmap.isNull()) {
if (cacheableDirIcon) {
QMutexLocker locker(&mx);
QPixmapCache::insert(key, pixmap);
dirIconEntryCache.insert(filePath, FakePointer<int>::create(info.iIcon));
dirIconEntryCache.insert(filePath, FakePointer<int>::create(task->iIcon));
}
} else {
qWarning("QWindowsTheme::fileIconPixmap() no icon found");
}
}
DestroyIcon(info.hIcon);
}
return pixmap;
@ -1088,6 +1164,11 @@ QIcon QWindowsTheme::fileIcon(const QFileInfo &fileInfo, QPlatformTheme::IconOpt
return QIcon(new QWindowsFileIconEngine(fileInfo, iconOptions));
}
QIconEngine *QWindowsTheme::createIconEngine(const QString &iconName) const
{
return new QWindowsIconEngine(iconName);
}
static inline bool doUseNativeMenus()
{
const unsigned options = QWindowsIntegration::instance()->options();
@ -1112,14 +1193,16 @@ bool QWindowsTheme::useNativeMenus()
return result;
}
bool QWindowsTheme::queryDarkMode()
Qt::ColorScheme QWindowsTheme::queryColorScheme()
{
if (queryHighContrast()) {
return false;
}
const auto setting = QWinRegistryKey(HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\CurrentVersion\Themes\Personalize)")
.dwordValue(L"AppsUseLightTheme");
return setting.second && setting.first == 0;
if (queryHighContrast())
return Qt::ColorScheme::Unknown;
QWinRegistryKey personalizeKey{
HKEY_CURRENT_USER, LR"(Software\Microsoft\Windows\CurrentVersion\Themes\Personalize)"
};
const bool useDarkTheme = personalizeKey.value<DWORD>(L"AppsUseLightTheme") == 0;
return useDarkTheme ? Qt::ColorScheme::Dark : Qt::ColorScheme::Light;
}
bool QWindowsTheme::queryHighContrast()

View File

@ -5,6 +5,7 @@
#include "qwindowswindow.h"
#include "qwindowscontext.h"
#include "qwindowstheme.h"
#if QT_CONFIG(draganddrop)
# include "qwindowsdrag.h"
#endif
@ -27,6 +28,7 @@
#include <QtGui/qwindow.h>
#include <QtGui/qregion.h>
#include <QtGui/qopenglcontext.h>
#include <QtGui/private/qwindowsthemecache_p.h>
#include <private/qwindow_p.h> // QWINDOWSIZE_MAX
#include <private/qguiapplication_p.h>
#include <private/qhighdpiscaling_p.h>
@ -34,6 +36,7 @@
#include <QtCore/qdebug.h>
#include <QtCore/qlibraryinfo.h>
#include <QtCore/qoperatingsystemversion.h>
#include <dwmapi.h>
@ -43,6 +46,8 @@
#include <shellscalingapi.h>
#include "vxkex.h"
QT_BEGIN_NAMESPACE
using QWindowCreationContextPtr = QSharedPointer<QWindowCreationContext>;
@ -526,8 +531,8 @@ static inline void updateGLWindowSettings(const QWindow *w, HWND hwnd, Qt::Windo
return QWindowsContext::user32dll.getSystemMetricsForDpi(SM_CXSIZEFRAME, dpi)
+ QWindowsContext::user32dll.getSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
}
return GetSystemMetrics(SM_CXSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER);
else
return vxkex::GetSystemMetricsForDpi(SM_CXSIZEFRAME, dpi) + vxkex::GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
}
/*!
@ -537,16 +542,22 @@ static inline void updateGLWindowSettings(const QWindow *w, HWND hwnd, Qt::Windo
static QMargins invisibleMargins(QPoint screenPoint)
{
if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows10) {
POINT pt = {screenPoint.x(), screenPoint.y()};
if (HMONITOR hMonitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONULL)) {
if (QWindowsContext::shcoredll.isValid()) {
UINT dpiX;
UINT dpiY;
if (SUCCEEDED(QWindowsContext::shcoredll.getDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY))) {
const int gap = getResizeBorderThickness(dpiX);
return QMargins(gap, 0, gap, gap);
}
POINT pt = {screenPoint.x(), screenPoint.y()};
if (HMONITOR hMonitor = MonitorFromPoint(pt, MONITOR_DEFAULTTONULL)) {
if (QWindowsContext::shcoredll.isValid()) {
UINT dpiX;
UINT dpiY;
HRESULT hr = S_OK;
if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows10)
hr = QWindowsContext::shcoredll.getDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
else
hr = vxkex::GetDpiForMonitor(hMonitor, MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
if (SUCCEEDED(hr)) {
const int gap = getResizeBorderThickness(dpiX);
return QMargins(gap, 0, gap, gap);
}
}
}
@ -555,12 +566,10 @@ static QMargins invisibleMargins(QPoint screenPoint)
[[nodiscard]] static inline QMargins invisibleMargins(const HWND hwnd)
{
if (QWindowsContext::user32dll.getDpiForWindow) {
const UINT dpi = QWindowsContext::user32dll.getDpiForWindow(hwnd);
const int gap = getResizeBorderThickness(dpi);
return QMargins(gap, 0, gap, gap);
}
return QMargins();
const UINT dpi = (QWindowsContext::user32dll.getDpiForWindow) ? QWindowsContext::user32dll.getDpiForWindow(hwnd) : vxkex::GetDpiForWindow(hwnd);
const int gap = getResizeBorderThickness(dpi);
return QMargins(gap, 0, gap, gap);
}
/*!
@ -848,6 +857,10 @@ void WindowCreationData::fromWindow(const QWindow *w, const Qt::WindowFlags flag
// NOTE: WS_EX_TRANSPARENT flag can make mouse inputs fall through a layered window
if (flagsIn & Qt::WindowTransparentForInput)
exStyle |= WS_EX_LAYERED | WS_EX_TRANSPARENT;
// Currently only compatible with D3D surfaces, use it with care.
if (qEnvironmentVariableIntValue("QT_QPA_DISABLE_REDIRECTION_SURFACE"))
exStyle |= WS_EX_NOREDIRECTIONBITMAP;
}
}
@ -855,7 +868,8 @@ static inline bool shouldApplyDarkFrame(const QWindow *w)
{
if (!w->isTopLevel() || w->flags().testFlag(Qt::FramelessWindowHint))
return false;
// the application has explicitly opted out of dark frames
// the user of the application has explicitly opted out of dark frames
if (!QWindowsIntegration::instance()->darkModeHandling().testFlag(QWindowsApplication::DarkModeWindowFrames))
return false;
@ -935,7 +949,7 @@ QWindowsWindowData
return result;
}
if (QWindowsContext::isDarkMode() && shouldApplyDarkFrame(w))
if (QWindowsTheme::instance()->colorScheme() == Qt::ColorScheme::Dark && shouldApplyDarkFrame(w))
QWindowsWindow::setDarkBorderToWindow(result.hwnd, true);
if (mirrorParentWidth != 0) {
@ -1074,10 +1088,17 @@ QMargins QWindowsGeometryHint::frame(const QWindow *w, DWORD style, DWORD exStyl
return {};
RECT rect = {0,0,0,0};
style &= ~DWORD(WS_OVERLAPPED); // Not permitted, see docs.
if (QWindowsContext::user32dll.adjustWindowRectExForDpi &&
QWindowsContext::user32dll.adjustWindowRectExForDpi(&rect, style, FALSE, exStyle, unsigned(qRound(dpi))) == FALSE) {
qErrnoWarning("%s: AdjustWindowRectExForDpi failed", __FUNCTION__);
if (QWindowsContext::user32dll.adjustWindowRectExForDpi)
{
if (QWindowsContext::user32dll.adjustWindowRectExForDpi(&rect, style, FALSE, exStyle, unsigned(qRound(dpi))) == FALSE)
qErrnoWarning("%s: AdjustWindowRectExForDpi failed", __FUNCTION__);
}
else
{
if (vxkex::AdjustWindowRectExForDpi(&rect, style, FALSE, exStyle, unsigned(qRound(dpi))) == FALSE)
qErrnoWarning("%s: vxkex::AdjustWindowRectExForDpi failed", __FUNCTION__);
}
const QMargins result(qAbs(rect.left), qAbs(rect.top),
qAbs(rect.right), qAbs(rect.bottom));
qCDebug(lcQpaWindow).nospace() << __FUNCTION__ << " style="
@ -1364,6 +1385,14 @@ QWindowsForeignWindow::QWindowsForeignWindow(QWindow *window, HWND hwnd)
, m_hwnd(hwnd)
, m_topLevelStyle(0)
{
if (QPlatformWindow::parent())
setParent(QPlatformWindow::parent());
}
QWindowsForeignWindow::~QWindowsForeignWindow()
{
if (QPlatformWindow::parent())
setParent(nullptr);
}
void QWindowsForeignWindow::setParent(const QPlatformWindow *newParentWindow)
@ -1372,10 +1401,12 @@ void QWindowsForeignWindow::setParent(const QPlatformWindow *newParentWindow)
const HWND newParent = newParentWindow ? reinterpret_cast<HWND>(newParentWindow->winId()) : HWND(nullptr);
const bool isTopLevel = !newParent;
const DWORD oldStyle = style();
qCDebug(lcQpaWindow) << __FUNCTION__ << window() << "newParent="
<< newParentWindow << newParent << "oldStyle=" << debugWinStyle(oldStyle);
SetParent(m_hwnd, newParent);
if (wasTopLevel != isTopLevel) { // Top level window flags need to be set/cleared manually.
auto updateWindowFlags = [&]{
// Top level window flags need to be set/cleared manually.
DWORD newStyle = oldStyle;
if (isTopLevel) {
newStyle = m_topLevelStyle;
@ -1385,6 +1416,20 @@ void QWindowsForeignWindow::setParent(const QPlatformWindow *newParentWindow)
newStyle |= WS_CHILD;
}
SetWindowLongPtr(m_hwnd, GWL_STYLE, newStyle);
};
if (wasTopLevel && !isTopLevel) {
// Becoming a child window requires the style
// flags to be updated before reparenting.
updateWindowFlags();
}
SetParent(m_hwnd, newParent);
if (!wasTopLevel && isTopLevel) {
// Becoming a top level window requires the style
// flags to be updated after reparenting.
updateWindowFlags();
}
}
@ -1539,6 +1584,7 @@ QWindowsWindow::QWindowsWindow(QWindow *aWindow, const QWindowsWindowData &data)
QWindowsWindow::~QWindowsWindow()
{
setFlag(WithinDestroy);
QWindowsThemeCache::clearThemeCache(m_data.hwnd);
if (testFlag(TouchRegistered))
UnregisterTouchWindow(m_data.hwnd);
destroyWindow();
@ -1568,7 +1614,7 @@ void QWindowsWindow::initialize()
}
}
QWindowsWindow::setSavedDpi(QWindowsContext::user32dll.getDpiForWindow ?
QWindowsContext::user32dll.getDpiForWindow(handle()) : 96);
QWindowsContext::user32dll.getDpiForWindow(handle()) : vxkex::GetDpiForWindow(handle()));
}
QSurfaceFormat QWindowsWindow::format() const
@ -2018,6 +2064,9 @@ void QWindowsWindow::handleDpiChanged(HWND hwnd, WPARAM wParam, LPARAM lParam)
const UINT dpi = HIWORD(wParam);
const qreal scale = dpiRelativeScale(dpi);
setSavedDpi(dpi);
QWindowsThemeCache::clearThemeCache(hwnd);
// Send screen change first, so that the new screen is set during any following resize
checkForScreenChanged(QWindowsWindow::FromDpiChange);
@ -2060,20 +2109,17 @@ void QWindowsWindow::handleDpiChanged(HWND hwnd, WPARAM wParam, LPARAM lParam)
void QWindowsWindow::handleDpiChangedAfterParent(HWND hwnd)
{
if (QWindowsContext::user32dll.getDpiForWindow)
{
const UINT dpi = QWindowsContext::user32dll.getDpiForWindow(hwnd);
const qreal scale = dpiRelativeScale(dpi);
setSavedDpi(dpi);
const UINT dpi = QWindowsContext::user32dll.getDpiForWindow ? QWindowsContext::user32dll.getDpiForWindow(hwnd) : vxkex::GetDpiForWindow(hwnd);
const qreal scale = dpiRelativeScale(dpi);
setSavedDpi(dpi);
checkForScreenChanged(QWindowsWindow::FromDpiChange);
checkForScreenChanged(QWindowsWindow::FromDpiChange);
// Child windows do not get WM_GETDPISCALEDSIZE messages to inform
// Windows about the new size, so we need to manually scale them.
QRect currentGeometry = geometry();
QRect scaledGeometry = QRect(currentGeometry.topLeft() * scale, currentGeometry.size() * scale);
setGeometry(scaledGeometry);
}
// Child windows do not get WM_GETDPISCALEDSIZE messages to inform
// Windows about the new size, so we need to manually scale them.
QRect currentGeometry = geometry();
QRect scaledGeometry = QRect(currentGeometry.topLeft() * scale, currentGeometry.size() * scale);
setGeometry(scaledGeometry);
}
static QRect normalFrameGeometry(HWND hwnd)
@ -2155,11 +2201,8 @@ void QWindowsWindow::setGeometry(const QRect &rectIn)
const QMargins margins = frameMargins();
rect.moveTopLeft(rect.topLeft() + QPoint(margins.left(), margins.top()));
}
if (m_windowState & Qt::WindowMinimized)
m_data.geometry = rect; // Otherwise set by handleGeometryChange() triggered by event.
else
setWindowState(Qt::WindowNoState);// Update window state to WindowNoState unless minimized
if (m_data.hwnd) {
// A ResizeEvent with resulting geometry will be sent. If we cannot
@ -2485,6 +2528,13 @@ void QWindowsWindow::handleWindowStateChange(Qt::WindowStates state)
GetWindowPlacement(m_data.hwnd, &windowPlacement);
const RECT geometry = RECTfromQRect(m_data.restoreGeometry);
windowPlacement.rcNormalPosition = geometry;
correctWindowPlacement(windowPlacement);
// Even if the window is hidden, windowPlacement's showCmd is not SW_HIDE, so change it
// manually to avoid unhiding a hidden window with the subsequent call to
// SetWindowPlacement().
if (!isVisible())
windowPlacement.showCmd = SW_HIDE;
SetWindowPlacement(m_data.hwnd, &windowPlacement);
}
// QTBUG-17548: We send expose events when receiving WM_Paint, but for
@ -2510,6 +2560,65 @@ void QWindowsWindow::handleWindowStateChange(Qt::WindowStates state)
}
}
// Apply corrections to window placement in Windows 10
// Related to task bar on top or left.
inline bool QWindowsBaseWindow::hasMaximumHeight() const
{
return window()->maximumHeight() != QWINDOWSIZE_MAX;
}
inline bool QWindowsBaseWindow::hasMaximumWidth() const
{
return window()->maximumWidth() != QWINDOWSIZE_MAX;
}
inline bool QWindowsBaseWindow::hasMaximumSize() const
{
return hasMaximumHeight() || hasMaximumWidth();
}
void QWindowsWindow::correctWindowPlacement(WINDOWPLACEMENT &windowPlacement)
{
static const auto windows11 = QOperatingSystemVersion::Windows11_21H2;
static const bool isWindows10 = QOperatingSystemVersion::current() < windows11;
if (!isWindows10)
return;
// Correct normal position by placement offset on Windows 10
// (where task bar can be on any side of the screen)
const QPoint offset = windowPlacementOffset(m_data.hwnd, m_data.restoreGeometry.topLeft());
windowPlacement.rcNormalPosition = RECTfromQRect(m_data.restoreGeometry.translated(-offset));
qCDebug(lcQpaWindow) << "Corrected normal position by" << -offset;
// A bug in windows 10 grows
// - ptMaxPosition.x by the task bar's width, if it's on the left
// - ptMaxPosition.y by the task bar's height, if it's on the top
// each time GetWindowPlacement() is called.
// The offset of the screen's left edge (as per frameMargins_sys().left()) is ignored.
// => Check for windows 10 and correct.
if (hasMaximumSize()) {
const QMargins margins = frameMargins_sys();
const QPoint topLeft = window()->screen()->geometry().topLeft();
windowPlacement.ptMaxPosition = POINT{ topLeft.x() - margins.left(), topLeft.y() };
qCDebug(lcQpaWindow) << "Window has maximum size. Corrected topLeft by"
<< -margins.left();
// If there is a placement offset correct width/height unless restricted,
// in order to fit window onto the screen.
if (offset.x() > 0 && !hasMaximumWidth()) {
const int adjust = offset.x() / window()->devicePixelRatio();
window()->setWidth(window()->width() - adjust);
qCDebug(lcQpaWindow) << "Width shortened by" << adjust << "logical pixels.";
}
if (offset.y() > 0 && !hasMaximumHeight()) {
const int adjust = offset.y() / window()->devicePixelRatio();
window()->setHeight(window()->height() - adjust);
qCDebug(lcQpaWindow) << "Height shortened by" << adjust << "logical pixels.";
}
}
}
void QWindowsWindow::updateRestoreGeometry()
{
m_data.restoreGeometry = normalFrameGeometry(m_data.hwnd);
@ -2654,8 +2763,24 @@ void QWindowsWindow::setWindowState_sys(Qt::WindowStates newState)
setFlag(WithinMaximize);
if (newState & Qt::WindowFullScreen)
setFlag(MaximizeToFullScreen);
ShowWindow(m_data.hwnd,
(newState & Qt::WindowMaximized) ? SW_MAXIMIZE : SW_SHOWNOACTIVATE);
if (m_data.flags & Qt::FramelessWindowHint) {
if (newState == Qt::WindowNoState) {
const QRect &rect = m_savedFrameGeometry;
MoveWindow(m_data.hwnd, rect.x(), rect.y(), rect.width(), rect.height(), true);
} else {
HMONITOR monitor = MonitorFromWindow(m_data.hwnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO monitorInfo = {};
monitorInfo.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(monitor, &monitorInfo);
const RECT &rect = monitorInfo.rcWork;
m_savedFrameGeometry = geometry();
MoveWindow(m_data.hwnd, rect.left, rect.top,
rect.right - rect.left, rect.bottom - rect.top, true);
}
} else {
ShowWindow(m_data.hwnd,
(newState & Qt::WindowMaximized) ? SW_MAXIMIZE : SW_SHOWNOACTIVATE);
}
clearFlag(WithinMaximize);
clearFlag(MaximizeToFullScreen);
} else if (visible && (oldState & newState & Qt::WindowMinimized)) {
@ -2694,7 +2819,7 @@ bool QWindowsWindow::windowEvent(QEvent *event)
{
switch (event->type()) {
case QEvent::ApplicationPaletteChange:
setDarkBorder(QWindowsContext::isDarkMode());
setDarkBorder(QWindowsTheme::instance()->colorScheme() == Qt::ColorScheme::Dark);
break;
case QEvent::WindowBlocked: // Blocked by another modal window.
setEnabled(false);
@ -3290,17 +3415,6 @@ enum : WORD {
DwmwaUseImmersiveDarkModeBefore20h1 = 19
};
static bool queryDarkBorder(HWND hwnd)
{
BOOL result = FALSE;
const bool ok =
SUCCEEDED(DwmGetWindowAttribute(hwnd, DwmwaUseImmersiveDarkMode, &result, sizeof(result)))
|| SUCCEEDED(DwmGetWindowAttribute(hwnd, DwmwaUseImmersiveDarkModeBefore20h1, &result, sizeof(result)));
if (!ok)
qCWarning(lcQpaWindow, "%s: Unable to retrieve dark window border setting.", __FUNCTION__);
return result == TRUE;
}
bool QWindowsWindow::setDarkBorderToWindow(HWND hwnd, bool d)
{
const BOOL darkBorder = d ? TRUE : FALSE;
@ -3316,8 +3430,6 @@ void QWindowsWindow::setDarkBorder(bool d)
{
// respect explicit opt-out and incompatible palettes or styles
d = d && shouldApplyDarkFrame(window());
if (queryDarkBorder(m_data.hwnd) == d)
return;
setDarkBorderToWindow(m_data.hwnd, d);
}

View File

@ -0,0 +1,170 @@
// Copyright (C) 2017 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
#include <QtGui/qtguiglobal.h>
#if QT_CONFIG(accessibility)
#include "qwindowsuiaaccessibility.h"
#include "qwindowsuiautomation.h"
#include "qwindowsuiamainprovider.h"
#include "qwindowsuiautils.h"
#include <QtGui/qaccessible.h>
#include <QtGui/qwindow.h>
#include <QtGui/qguiapplication.h>
#include <QtGui/private/qguiapplication_p.h>
#include <QtCore/qt_windows.h>
#include <qpa/qplatformintegration.h>
#include "qwindowsuiawrapper_p.h"
#include "qwindowsuiawrapper.cpp"
#include <QtCore/private/qwinregistry_p.h>
QT_BEGIN_NAMESPACE
using namespace QWindowsUiAutomation;
using namespace Qt::Literals::StringLiterals;
bool QWindowsUiaAccessibility::m_accessibleActive = false;
QWindowsUiaAccessibility::QWindowsUiaAccessibility()
{
}
QWindowsUiaAccessibility::~QWindowsUiaAccessibility()
{
}
// Handles UI Automation window messages.
bool QWindowsUiaAccessibility::handleWmGetObject(HWND hwnd, WPARAM wParam, LPARAM lParam, LRESULT *lResult)
{
// Start handling accessibility internally
QGuiApplicationPrivate::platformIntegration()->accessibility()->setActive(true);
m_accessibleActive = true;
// Ignoring all requests while starting up / shutting down
if (QCoreApplication::startingUp() || QCoreApplication::closingDown())
return false;
if (QWindow *window = QWindowsContext::instance()->findWindow(hwnd)) {
if (QAccessibleInterface *accessible = window->accessibleRoot()) {
auto provider = QWindowsUiaMainProvider::providerForAccessible(accessible);
*lResult = QWindowsUiaWrapper::instance()->returnRawElementProvider(hwnd, wParam, lParam, provider.Get());
return true;
}
}
return false;
}
// Retrieve sound name by checking the icon property of a message box
// should it be the event object.
static QString alertSound(const QObject *object)
{
if (object->inherits("QMessageBox")) {
enum MessageBoxIcon { // Keep in sync with QMessageBox::Icon
Information = 1,
Warning = 2,
Critical = 3
};
switch (object->property("icon").toInt()) {
case Information:
return QStringLiteral("SystemAsterisk");
case Warning:
return QStringLiteral("SystemExclamation");
case Critical:
return QStringLiteral("SystemHand");
}
return QString();
}
return QStringLiteral("SystemAsterisk");
}
static QString soundFileName(const QString &soundName)
{
const QString key = "AppEvents\\Schemes\\Apps\\.Default\\"_L1
+ soundName + "\\.Current"_L1;
return QWinRegistryKey(HKEY_CURRENT_USER, key).stringValue(L"");
}
static void playSystemSound(const QString &soundName)
{
if (!soundName.isEmpty() && !soundFileName(soundName).isEmpty()) {
PlaySound(reinterpret_cast<const wchar_t *>(soundName.utf16()), nullptr,
SND_ALIAS | SND_ASYNC | SND_NODEFAULT | SND_NOWAIT);
}
}
// Handles accessibility update notifications.
void QWindowsUiaAccessibility::notifyAccessibilityUpdate(QAccessibleEvent *event)
{
if (!event)
return;
// Always handle system sound events
switch (event->type()) {
case QAccessible::PopupMenuStart:
playSystemSound(QStringLiteral("MenuPopup"));
break;
case QAccessible::MenuCommand:
playSystemSound(QStringLiteral("MenuCommand"));
break;
case QAccessible::Alert:
playSystemSound(alertSound(event->object()));
break;
default:
break;
}
// Ignore events sent before the first UI Automation
// request or while QAccessible is being activated.
if (!m_accessibleActive)
return;
QAccessibleInterface *accessible = event->accessibleInterface();
if (!isActive() || !accessible || !accessible->isValid())
return;
// Ensures QWindowsUiaWrapper is properly initialized.
if (!QWindowsUiaWrapper::instance()->ready())
return;
// No need to do anything when nobody is listening.
if (!QWindowsUiaWrapper::instance()->clientsAreListening())
return;
switch (event->type()) {
case QAccessible::Announcement:
QWindowsUiaMainProvider::raiseNotification(static_cast<QAccessibleAnnouncementEvent *>(event));
break;
case QAccessible::Focus:
QWindowsUiaMainProvider::notifyFocusChange(event);
break;
case QAccessible::StateChanged:
QWindowsUiaMainProvider::notifyStateChange(static_cast<QAccessibleStateChangeEvent *>(event));
break;
case QAccessible::ValueChanged:
QWindowsUiaMainProvider::notifyValueChange(static_cast<QAccessibleValueChangeEvent *>(event));
break;
case QAccessible::NameChanged:
QWindowsUiaMainProvider::notifyNameChange(event);
break;
case QAccessible::SelectionAdd:
QWindowsUiaMainProvider::notifySelectionChange(event);
break;
case QAccessible::TextAttributeChanged:
case QAccessible::TextColumnChanged:
case QAccessible::TextInserted:
case QAccessible::TextRemoved:
case QAccessible::TextUpdated:
case QAccessible::TextSelectionChanged:
case QAccessible::TextCaretMoved:
QWindowsUiaMainProvider::notifyTextChange(event);
break;
default:
break;
}
}
QT_END_NAMESPACE
#endif // QT_CONFIG(accessibility)

View File

@ -0,0 +1,843 @@
// Copyright (C) 2017 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
#include <QtGui/qtguiglobal.h>
#if QT_CONFIG(accessibility)
#include "qwindowsuiamainprovider.h"
#include "qwindowsuiavalueprovider.h"
#include "qwindowsuiarangevalueprovider.h"
#include "qwindowsuiatextprovider.h"
#include "qwindowsuiatoggleprovider.h"
#include "qwindowsuiainvokeprovider.h"
#include "qwindowsuiaselectionprovider.h"
#include "qwindowsuiaselectionitemprovider.h"
#include "qwindowsuiatableprovider.h"
#include "qwindowsuiatableitemprovider.h"
#include "qwindowsuiagridprovider.h"
#include "qwindowsuiagriditemprovider.h"
#include "qwindowsuiawindowprovider.h"
#include "qwindowsuiaexpandcollapseprovider.h"
#include "qwindowscontext.h"
#include "qwindowsuiautils.h"
#include "qwindowsuiaprovidercache.h"
#include "qwindowsuiawrapper_p.h"
#include <QtCore/qloggingcategory.h>
#include <QtGui/private/qaccessiblebridgeutils_p.h>
#include <QtGui/qaccessible.h>
#include <QtGui/qguiapplication.h>
#include <QtGui/qwindow.h>
#include <qpa/qplatforminputcontextfactory_p.h>
#include <QtCore/private/qcomvariant_p.h>
#if !defined(Q_CC_BOR) && !defined (Q_CC_GNU)
#include <comdef.h>
#endif
#include <QtCore/qt_windows.h>
QT_BEGIN_NAMESPACE
using namespace QWindowsUiAutomation;
// Returns a cached instance of the provider for a specific accessible interface.
ComPtr<QWindowsUiaMainProvider> QWindowsUiaMainProvider::providerForAccessible(QAccessibleInterface *accessible)
{
if (!accessible)
return nullptr;
QAccessible::Id id = QAccessible::uniqueId(accessible);
QWindowsUiaProviderCache *providerCache = QWindowsUiaProviderCache::instance();
ComPtr<QWindowsUiaMainProvider> provider = providerCache->providerForId(id);
if (!provider) {
provider = makeComObject<QWindowsUiaMainProvider>(accessible);
providerCache->insert(id, provider.Get()); // Cache holds weak references
}
return provider;
}
QWindowsUiaMainProvider::QWindowsUiaMainProvider(QAccessibleInterface *a)
: QWindowsUiaBaseProvider(QAccessible::uniqueId(a))
{
}
QWindowsUiaMainProvider::~QWindowsUiaMainProvider()
{
}
void QWindowsUiaMainProvider::notifyFocusChange(QAccessibleEvent *event)
{
if (QAccessibleInterface *accessible = event->accessibleInterface()) {
// If this is a complex element, raise event for the focused child instead.
if (accessible->childCount()) {
if (QAccessibleInterface *child = accessible->focusChild())
accessible = child;
}
if (auto provider = providerForAccessible(accessible))
QWindowsUiaWrapper::instance()->raiseAutomationEvent(provider.Get(), UIA_AutomationFocusChangedEventId);
}
}
void QWindowsUiaMainProvider::notifyStateChange(QAccessibleStateChangeEvent *event)
{
if (QAccessibleInterface *accessible = event->accessibleInterface()) {
if (event->changedStates().checked || event->changedStates().checkStateMixed) {
// Notifies states changes in checkboxes.
if (accessible->role() == QAccessible::CheckBox) {
if (auto provider = providerForAccessible(accessible)) {
long toggleState = ToggleState_Off;
if (accessible->state().checked)
toggleState = accessible->state().checkStateMixed ? ToggleState_Indeterminate : ToggleState_On;
QComVariant oldVal;
QComVariant newVal{toggleState};
QWindowsUiaWrapper::instance()->raiseAutomationPropertyChangedEvent(
provider.Get(), UIA_ToggleToggleStatePropertyId, oldVal.get(), newVal.get());
}
}
}
if (event->changedStates().active) {
if (accessible->role() == QAccessible::Window) {
// Notifies window opened/closed.
if (auto provider = providerForAccessible(accessible)) {
if (accessible->state().active) {
QWindowsUiaWrapper::instance()->raiseAutomationEvent(provider.Get(), UIA_Window_WindowOpenedEventId);
if (QAccessibleInterface *focused = accessible->focusChild()) {
if (auto focusedProvider = providerForAccessible(focused)) {
QWindowsUiaWrapper::instance()->raiseAutomationEvent(focusedProvider.Get(),
UIA_AutomationFocusChangedEventId);
}
}
} else {
QWindowsUiaWrapper::instance()->raiseAutomationEvent(provider.Get(), UIA_Window_WindowClosedEventId);
}
}
}
}
}
}
void QWindowsUiaMainProvider::notifyValueChange(QAccessibleValueChangeEvent *event)
{
if (QAccessibleInterface *accessible = event->accessibleInterface()) {
if (accessible->role() == QAccessible::ComboBox && accessible->childCount() > 0) {
QAccessibleInterface *listacc = accessible->child(0);
if (listacc && listacc->role() == QAccessible::List) {
int count = listacc->childCount();
for (int i = 0; i < count; ++i) {
QAccessibleInterface *item = listacc->child(i);
if (item && item->isValid() && item->text(QAccessible::Name) == event->value()) {
if (!item->state().selected) {
if (QAccessibleActionInterface *actionInterface = item->actionInterface())
actionInterface->doAction(QAccessibleActionInterface::toggleAction());
}
break;
}
}
}
}
if (event->value().typeId() == QMetaType::QString) {
if (auto provider = providerForAccessible(accessible)) {
// Notifies changes in string values.
const QComVariant oldVal;
const QComVariant newVal{ event->value().toString() };
QWindowsUiaWrapper::instance()->raiseAutomationPropertyChangedEvent(provider.Get(), UIA_ValueValuePropertyId,
oldVal.get(), newVal.get());
}
} else if (QAccessibleValueInterface *valueInterface = accessible->valueInterface()) {
if (auto provider = providerForAccessible(accessible)) {
// Notifies changes in values of controls supporting the value interface.
const QComVariant oldVal;
const QComVariant newVal{ valueInterface->currentValue().toDouble() };
QWindowsUiaWrapper::instance()->raiseAutomationPropertyChangedEvent(
provider.Get(), UIA_RangeValueValuePropertyId, oldVal.get(), newVal.get());
}
}
}
}
void QWindowsUiaMainProvider::notifyNameChange(QAccessibleEvent *event)
{
if (QAccessibleInterface *accessible = event->accessibleInterface()) {
// Restrict notification to combo boxes, which need it for accessibility,
// in order to avoid slowdowns with unnecessary notifications.
if (accessible->role() == QAccessible::ComboBox) {
if (auto provider = providerForAccessible(accessible)) {
QComVariant oldVal;
QComVariant newVal{ accessible->text(QAccessible::Name) };
QWindowsUiaWrapper::instance()->raiseAutomationPropertyChangedEvent(provider.Get(), UIA_NamePropertyId,
oldVal.get(), newVal.get());
}
}
}
}
void QWindowsUiaMainProvider::notifySelectionChange(QAccessibleEvent *event)
{
if (QAccessibleInterface *accessible = event->accessibleInterface()) {
if (auto provider = providerForAccessible(accessible))
QWindowsUiaWrapper::instance()->raiseAutomationEvent(provider.Get(), UIA_SelectionItem_ElementSelectedEventId);
}
}
// Notifies changes in text content and selection state of text controls.
void QWindowsUiaMainProvider::notifyTextChange(QAccessibleEvent *event)
{
if (QAccessibleInterface *accessible = event->accessibleInterface()) {
if (accessible->textInterface()) {
if (auto provider = providerForAccessible(accessible)) {
if (event->type() == QAccessible::TextSelectionChanged) {
QWindowsUiaWrapper::instance()->raiseAutomationEvent(provider.Get(), UIA_Text_TextSelectionChangedEventId);
} else if (event->type() == QAccessible::TextCaretMoved) {
if (!accessible->state().readOnly) {
QWindowsUiaWrapper::instance()->raiseAutomationEvent(provider.Get(),
UIA_Text_TextSelectionChangedEventId);
}
} else {
QWindowsUiaWrapper::instance()->raiseAutomationEvent(provider.Get(), UIA_Text_TextChangedEventId);
}
}
}
}
}
void QWindowsUiaMainProvider::raiseNotification(QAccessibleAnnouncementEvent *event)
{
if (QAccessibleInterface *accessible = event->accessibleInterface()) {
if (auto provider = providerForAccessible(accessible)) {
QBStr message{ event->message() };
QAccessible::AnnouncementPoliteness prio = event->politeness();
NotificationProcessing processing = (prio == QAccessible::AnnouncementPoliteness::Assertive)
? NotificationProcessing_ImportantAll
: NotificationProcessing_All;
QBStr activityId{ QString::fromLatin1("") };
QWindowsUiaWrapper::instance()->raiseNotificationEvent(provider.Get(), NotificationKind_Other, processing, message.bstr(),
activityId.bstr());
}
}
}
HRESULT STDMETHODCALLTYPE QWindowsUiaMainProvider::QueryInterface(REFIID iid, LPVOID *iface)
{
HRESULT result = QComObject::QueryInterface(iid, iface);
if (SUCCEEDED(result) && iid == __uuidof(IRawElementProviderFragmentRoot)) {
QAccessibleInterface *accessible = accessibleInterface();
if (accessible && hwndForAccessible(accessible)) {
result = S_OK;
} else {
Release();
result = E_NOINTERFACE;
*iface = nullptr;
}
}
return result;
}
HRESULT QWindowsUiaMainProvider::get_ProviderOptions(ProviderOptions *pRetVal)
{
if (!pRetVal)
return E_INVALIDARG;
// We are STA, (OleInitialize()).
*pRetVal = static_cast<ProviderOptions>(ProviderOptions_ServerSideProvider | ProviderOptions_UseComThreading);
return S_OK;
}
// Return providers for specific control patterns
HRESULT QWindowsUiaMainProvider::GetPatternProvider(PATTERNID idPattern, IUnknown **pRetVal)
{
qCDebug(lcQpaUiAutomation) << __FUNCTION__ << idPattern;
if (!pRetVal)
return E_INVALIDARG;
*pRetVal = nullptr;
QAccessibleInterface *accessible = accessibleInterface();
if (!accessible)
return UIA_E_ELEMENTNOTAVAILABLE;
switch (idPattern) {
case UIA_WindowPatternId:
if (accessible->parent() && (accessible->parent()->role() == QAccessible::Application)) {
*pRetVal = makeComObject<QWindowsUiaWindowProvider>(id()).Detach();
}
break;
case UIA_TextPatternId:
case UIA_TextPattern2Id:
// All text controls.
if (accessible->textInterface()) {
*pRetVal = makeComObject<QWindowsUiaTextProvider>(id()).Detach();
}
break;
case UIA_ValuePatternId:
// All non-static controls support the Value pattern.
if (accessible->role() != QAccessible::StaticText)
*pRetVal = makeComObject<QWindowsUiaValueProvider>(id()).Detach();
break;
case UIA_RangeValuePatternId:
// Controls providing a numeric value within a range (e.g., sliders, scroll bars, dials).
if (accessible->valueInterface()) {
*pRetVal = makeComObject<QWindowsUiaRangeValueProvider>(id()).Detach();
}
break;
case UIA_TogglePatternId:
// Checkboxes and other checkable controls.
if (accessible->state().checkable)
*pRetVal = makeComObject<QWindowsUiaToggleProvider>(id()).Detach();
break;
case UIA_SelectionPatternId:
case UIA_SelectionPattern2Id:
// Selections via QAccessibleSelectionInterface or lists of items.
if (accessible->selectionInterface()
|| accessible->role() == QAccessible::List
|| accessible->role() == QAccessible::PageTabList) {
*pRetVal = makeComObject<QWindowsUiaSelectionProvider>(id()).Detach();
}
break;
case UIA_SelectionItemPatternId:
// Parent supports selection interface or items within a list and radio buttons.
if ((accessible->parent() && accessible->parent()->selectionInterface())
|| (accessible->role() == QAccessible::RadioButton)
|| (accessible->role() == QAccessible::ListItem)
|| (accessible->role() == QAccessible::PageTab)) {
*pRetVal = makeComObject<QWindowsUiaSelectionItemProvider>(id()).Detach();
}
break;
case UIA_TablePatternId:
// Table/tree.
if (accessible->tableInterface()
&& ((accessible->role() == QAccessible::Table) || (accessible->role() == QAccessible::Tree))) {
*pRetVal = makeComObject<QWindowsUiaTableProvider>(id()).Detach();
}
break;
case UIA_TableItemPatternId:
// Item within a table/tree.
if (accessible->tableCellInterface()
&& ((accessible->role() == QAccessible::Cell) || (accessible->role() == QAccessible::TreeItem))) {
*pRetVal = makeComObject<QWindowsUiaTableItemProvider>(id()).Detach();
}
break;
case UIA_GridPatternId:
// Table/tree.
if (accessible->tableInterface()
&& ((accessible->role() == QAccessible::Table) || (accessible->role() == QAccessible::Tree))) {
*pRetVal = makeComObject<QWindowsUiaGridProvider>(id()).Detach();
}
break;
case UIA_GridItemPatternId:
// Item within a table/tree.
if (accessible->tableCellInterface()
&& ((accessible->role() == QAccessible::Cell) || (accessible->role() == QAccessible::TreeItem))) {
*pRetVal = makeComObject<QWindowsUiaGridItemProvider>(id()).Detach();
}
break;
case UIA_InvokePatternId:
// Things that have an invokable action (e.g., simple buttons).
if (accessible->actionInterface()) {
*pRetVal = makeComObject<QWindowsUiaInvokeProvider>(id()).Detach();
}
break;
case UIA_ExpandCollapsePatternId:
// Menu items with submenus.
if ((accessible->role() == QAccessible::MenuItem
&& accessible->childCount() > 0
&& accessible->child(0)->role() == QAccessible::PopupMenu)
|| accessible->role() == QAccessible::ComboBox
|| (accessible->role() == QAccessible::TreeItem && accessible->state().expandable)) {
*pRetVal = makeComObject<QWindowsUiaExpandCollapseProvider>(id()).Detach();
}
break;
default:
break;
}
return S_OK;
}
void QWindowsUiaMainProvider::fillVariantArrayForRelation(QAccessibleInterface* accessible,
QAccessible::Relation relation, VARIANT *pRetVal)
{
Q_ASSERT(accessible);
typedef std::pair<QAccessibleInterface*, QAccessible::Relation> RelationPair;
const QList<RelationPair> relationInterfaces = accessible->relations(relation);
if (relationInterfaces.empty())
return;
SAFEARRAY *elements = SafeArrayCreateVector(VT_UNKNOWN, 0, relationInterfaces.size());
for (LONG i = 0; i < relationInterfaces.size(); ++i) {
if (ComPtr<IRawElementProviderSimple> provider =
providerForAccessible(relationInterfaces.at(i).first)) {
SafeArrayPutElement(elements, &i, provider.Get());
}
}
pRetVal->vt = VT_UNKNOWN | VT_ARRAY;
pRetVal->parray = elements;
}
void QWindowsUiaMainProvider::setAriaProperties(QAccessibleInterface *accessible, VARIANT *pRetVal)
{
Q_ASSERT(accessible);
QAccessibleAttributesInterface *attributesIface = accessible->attributesInterface();
if (!attributesIface)
return;
QString ariaString;
const QList<QAccessible::Attribute> attrKeys = attributesIface->attributeKeys();
for (qsizetype i = 0; i < attrKeys.size(); ++i) {
if (i != 0)
ariaString += QStringLiteral(";");
const QAccessible::Attribute key = attrKeys.at(i);
const QVariant value = attributesIface->attributeValue(key);
// see "Core Accessibility API Mappings" spec: https://www.w3.org/TR/core-aam-1.2/
switch (key) {
case QAccessible::Attribute::Custom:
{
// forward custom attributes as-is
Q_ASSERT((value.canConvert<QHash<QString, QString>>()));
const QHash<QString, QString> attrMap = value.value<QHash<QString, QString>>();
for (auto [name, val] : attrMap.asKeyValueRange()) {
if (name != *attrMap.keyBegin())
ariaString += QStringLiteral(";");
ariaString += name + QStringLiteral("=") + val;
}
break;
}
case QAccessible::Attribute::Level:
Q_ASSERT(value.canConvert<int>());
ariaString += QStringLiteral("level=") + QString::number(value.toInt());
break;
default:
break;
}
}
*pRetVal = QComVariant{ ariaString }.release();
}
void QWindowsUiaMainProvider::setStyle(QAccessibleInterface *accessible, VARIANT *pRetVal)
{
Q_ASSERT(accessible);
QAccessibleAttributesInterface *attributesIface = accessible->attributesInterface();
if (!attributesIface)
return;
// currently, only heading styles are implemented here
if (accessible->role() != QAccessible::Role::Heading)
return;
const QVariant levelVariant = attributesIface->attributeValue(QAccessible::Attribute::Level);
if (!levelVariant.isValid())
return;
Q_ASSERT(levelVariant.canConvert<int>());
// UIA only has styles for heading levels 1-9
const int level = levelVariant.toInt();
if (level < 1 || level > 9)
return;
const long styleId = styleIdForHeadingLevel(level);
*pRetVal = QComVariant{ styleId }.release();
}
int QWindowsUiaMainProvider::styleIdForHeadingLevel(int headingLevel)
{
// only heading levels 1-9 have a corresponding UIA style ID
Q_ASSERT(headingLevel > 0 && headingLevel <= 9);
static constexpr int styles[] = {
StyleId_Heading1,
StyleId_Heading2,
StyleId_Heading3,
StyleId_Heading4,
StyleId_Heading5,
StyleId_Heading6,
StyleId_Heading7,
StyleId_Heading8,
StyleId_Heading9,
};
return styles[headingLevel - 1];
}
HRESULT QWindowsUiaMainProvider::GetPropertyValue(PROPERTYID idProp, VARIANT *pRetVal)
{
qCDebug(lcQpaUiAutomation) << __FUNCTION__ << idProp;
if (!pRetVal)
return E_INVALIDARG;
clearVariant(pRetVal);
QAccessibleInterface *accessible = accessibleInterface();
if (!accessible)
return UIA_E_ELEMENTNOTAVAILABLE;
bool topLevelWindow = accessible->parent() && (accessible->parent()->role() == QAccessible::Application);
switch (idProp) {
case UIA_ProcessIdPropertyId:
// PID
*pRetVal = QComVariant{ static_cast<long>(GetCurrentProcessId()) }.release();
break;
case UIA_AccessKeyPropertyId:
// Accelerator key.
*pRetVal = QComVariant{ accessible->text(QAccessible::Accelerator) }.release();
break;
case UIA_AriaPropertiesPropertyId:
setAriaProperties(accessible, pRetVal);
break;
case UIA_AutomationIdPropertyId:
// Automation ID, which can be used by tools to select a specific control in the UI.
*pRetVal = QComVariant{ QAccessibleBridgeUtils::accessibleId(accessible) }.release();
break;
case UIA_ClassNamePropertyId:
// Class name.
if (QObject *o = accessible->object()) {
QString className = QLatin1StringView(o->metaObject()->className());
*pRetVal = QComVariant{ className }.release();
}
break;
case UIA_DescribedByPropertyId:
fillVariantArrayForRelation(accessible, QAccessible::DescriptionFor, pRetVal);
break;
case UIA_FlowsFromPropertyId:
fillVariantArrayForRelation(accessible, QAccessible::FlowsTo, pRetVal);
break;
case UIA_FlowsToPropertyId:
fillVariantArrayForRelation(accessible, QAccessible::FlowsFrom, pRetVal);
break;
case UIA_FrameworkIdPropertyId:
*pRetVal = QComVariant{ QStringLiteral("Qt") }.release();
break;
case UIA_ControlTypePropertyId:
if (topLevelWindow) {
// Reports a top-level widget as a window, instead of "custom".
*pRetVal = QComVariant{ UIA_WindowControlTypeId }.release();
} else {
// Control type converted from role.
auto controlType = roleToControlTypeId(accessible->role());
// The native OSK should be disabled if the Qt OSK is in use,
// or if disabled via application attribute.
static bool imModuleEmpty = QPlatformInputContextFactory::requested().isEmpty();
bool nativeVKDisabled = QCoreApplication::testAttribute(Qt::AA_DisableNativeVirtualKeyboard);
// If we want to disable the native OSK auto-showing
// we have to report text fields as non-editable.
if (controlType == UIA_EditControlTypeId && (!imModuleEmpty || nativeVKDisabled))
controlType = UIA_TextControlTypeId;
*pRetVal = QComVariant{ controlType }.release();
}
break;
case UIA_HelpTextPropertyId:
*pRetVal = QComVariant{ accessible->text(QAccessible::Help) }.release();
break;
case UIA_HasKeyboardFocusPropertyId:
if (topLevelWindow) {
// Windows set the active state to true when they are focused
*pRetVal = QComVariant{ accessible->state().active ? true : false }.release();
} else {
*pRetVal = QComVariant{ accessible->state().focused ? true : false }.release();
}
break;
case UIA_IsKeyboardFocusablePropertyId:
if (topLevelWindow) {
// Windows should always be focusable
*pRetVal = QComVariant{ true }.release();
} else {
*pRetVal = QComVariant{ accessible->state().focusable ? true : false }.release();
}
break;
case UIA_IsOffscreenPropertyId:
*pRetVal = QComVariant{ accessible->state().offscreen ? true : false }.release();
break;
case UIA_IsContentElementPropertyId:
*pRetVal = QComVariant{ true }.release();
break;
case UIA_IsControlElementPropertyId:
*pRetVal = QComVariant{ true }.release();
break;
case UIA_IsEnabledPropertyId:
*pRetVal = QComVariant{ !accessible->state().disabled }.release();
break;
case UIA_IsPasswordPropertyId:
*pRetVal = QComVariant{ accessible->role() == QAccessible::EditableText
&& accessible->state().passwordEdit }
.release();
break;
case UIA_IsPeripheralPropertyId:
// True for peripheral UIs.
if (QWindow *window = windowForAccessible(accessible)) {
const Qt::WindowType wt = window->type();
*pRetVal = QComVariant{ wt == Qt::Popup || wt == Qt::ToolTip || wt == Qt::SplashScreen }
.release();
}
break;
case UIA_IsDialogPropertyId:
*pRetVal = QComVariant{ accessible->role() == QAccessible::Dialog
|| accessible->role() == QAccessible::AlertMessage }
.release();
break;
case UIA_FullDescriptionPropertyId:
*pRetVal = QComVariant{ accessible->text(QAccessible::Description) }.release();
break;
case UIA_NamePropertyId: {
QString name = accessible->text(QAccessible::Name);
if (name.isEmpty() && topLevelWindow)
name = QCoreApplication::applicationName();
*pRetVal = QComVariant{ name }.release();
break;
}
case UIA_StyleIdAttributeId:
setStyle(accessible, pRetVal);
break;
default:
break;
}
return S_OK;
}
HRESULT QWindowsUiaMainProvider::get_HostRawElementProvider(IRawElementProviderSimple **pRetVal)
{
qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
if (!pRetVal)
return E_INVALIDARG;
*pRetVal = nullptr;
// Returns a host provider only for controls associated with a native window handle. Others should return NULL.
if (QAccessibleInterface *accessible = accessibleInterface()) {
if (HWND hwnd = hwndForAccessible(accessible)) {
return QWindowsUiaWrapper::instance()->hostProviderFromHwnd(hwnd, pRetVal);
}
}
return S_OK;
}
// Navigates within the tree of accessible controls.
HRESULT QWindowsUiaMainProvider::Navigate(NavigateDirection direction, IRawElementProviderFragment **pRetVal)
{
qCDebug(lcQpaUiAutomation) << __FUNCTION__ << direction << " this: " << this;
if (!pRetVal)
return E_INVALIDARG;
*pRetVal = nullptr;
QAccessibleInterface *accessible = accessibleInterface();
if (!accessible)
return UIA_E_ELEMENTNOTAVAILABLE;
QAccessibleInterface *targetacc = nullptr;
if (direction == NavigateDirection_Parent) {
if (QAccessibleInterface *parent = accessible->parent()) {
// The Application's children are considered top level objects.
if (parent->isValid() && parent->role() != QAccessible::Application) {
targetacc = parent;
}
}
} else {
QAccessibleInterface *parent = nullptr;
int index = 0;
int incr = 1;
switch (direction) {
case NavigateDirection_FirstChild:
parent = accessible;
index = 0;
incr = 1;
break;
case NavigateDirection_LastChild:
parent = accessible;
index = accessible->childCount() - 1;
incr = -1;
break;
case NavigateDirection_NextSibling:
if ((parent = accessible->parent()))
index = parent->indexOfChild(accessible) + 1;
incr = 1;
break;
case NavigateDirection_PreviousSibling:
if ((parent = accessible->parent()))
index = parent->indexOfChild(accessible) - 1;
incr = -1;
break;
default:
Q_UNREACHABLE();
break;
}
if (parent && parent->isValid()) {
for (int count = parent->childCount(); index >= 0 && index < count; index += incr) {
if (QAccessibleInterface *child = parent->child(index)) {
if (child->isValid() && !child->state().invisible) {
targetacc = child;
break;
}
}
}
}
}
if (targetacc)
*pRetVal = providerForAccessible(targetacc).Detach();
return S_OK;
}
// Returns a unique id assigned to the UI element, used as key by the UI Automation framework.
HRESULT QWindowsUiaMainProvider::GetRuntimeId(SAFEARRAY **pRetVal)
{
qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
if (!pRetVal)
return E_INVALIDARG;
*pRetVal = nullptr;
QAccessibleInterface *accessible = accessibleInterface();
if (!accessible)
return UIA_E_ELEMENTNOTAVAILABLE;
// The UiaAppendRuntimeId constant is used to make then ID unique
// among multiple instances running on the system.
int rtId[] = { UiaAppendRuntimeId, int(id()) };
if ((*pRetVal = SafeArrayCreateVector(VT_I4, 0, 2))) {
for (LONG i = 0; i < 2; ++i)
SafeArrayPutElement(*pRetVal, &i, &rtId[i]);
}
return S_OK;
}
// Returns the bounding rectangle for the accessible control.
HRESULT QWindowsUiaMainProvider::get_BoundingRectangle(UiaRect *pRetVal)
{
qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
if (!pRetVal)
return E_INVALIDARG;
QAccessibleInterface *accessible = accessibleInterface();
if (!accessible)
return UIA_E_ELEMENTNOTAVAILABLE;
QWindow *window = windowForAccessible(accessible);
if (!window)
return UIA_E_ELEMENTNOTAVAILABLE;
rectToNativeUiaRect(accessible->rect(), window, pRetVal);
return S_OK;
}
HRESULT QWindowsUiaMainProvider::GetEmbeddedFragmentRoots(SAFEARRAY **pRetVal)
{
qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
if (!pRetVal)
return E_INVALIDARG;
*pRetVal = nullptr;
// No embedded roots.
return S_OK;
}
// Sets focus to the control.
HRESULT QWindowsUiaMainProvider::SetFocus()
{
qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
QAccessibleInterface *accessible = accessibleInterface();
if (!accessible)
return UIA_E_ELEMENTNOTAVAILABLE;
QAccessibleActionInterface *actionInterface = accessible->actionInterface();
if (!actionInterface)
return UIA_E_ELEMENTNOTAVAILABLE;
actionInterface->doAction(QAccessibleActionInterface::setFocusAction());
return S_OK;
}
HRESULT QWindowsUiaMainProvider::get_FragmentRoot(IRawElementProviderFragmentRoot **pRetVal)
{
qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
if (!pRetVal)
return E_INVALIDARG;
*pRetVal = nullptr;
// Our UI Automation implementation considers the window as the root for
// non-native controls/fragments.
if (QAccessibleInterface *accessible = accessibleInterface()) {
if (QWindow *window = windowForAccessible(accessible)) {
if (QAccessibleInterface *rootacc = window->accessibleRoot())
*pRetVal = providerForAccessible(rootacc).Detach();
}
}
return S_OK;
}
// Returns a provider for the UI element present at the specified screen coordinates.
HRESULT QWindowsUiaMainProvider::ElementProviderFromPoint(double x, double y, IRawElementProviderFragment **pRetVal)
{
qCDebug(lcQpaUiAutomation) << __FUNCTION__ << x << y;
if (!pRetVal) {
return E_INVALIDARG;
}
*pRetVal = nullptr;
QAccessibleInterface *accessible = accessibleInterface();
if (!accessible)
return UIA_E_ELEMENTNOTAVAILABLE;
QWindow *window = windowForAccessible(accessible);
if (!window)
return UIA_E_ELEMENTNOTAVAILABLE;
// Scales coordinates from High DPI screens.
UiaPoint uiaPoint = {x, y};
QPoint point;
nativeUiaPointToPoint(uiaPoint, window, &point);
QAccessibleInterface *targetacc = accessible->childAt(point.x(), point.y());
if (targetacc) {
QAccessibleInterface *acc = targetacc;
// Controls can be embedded within grouping elements. By default returns the innermost control.
while (acc) {
targetacc = acc;
// For accessibility tools it may be better to return the text element instead of its subcomponents.
if (targetacc->textInterface()) break;
acc = acc->childAt(point.x(), point.y());
}
*pRetVal = providerForAccessible(targetacc).Detach();
}
return S_OK;
}
// Returns the fragment with focus.
HRESULT QWindowsUiaMainProvider::GetFocus(IRawElementProviderFragment **pRetVal)
{
qCDebug(lcQpaUiAutomation) << __FUNCTION__ << this;
if (!pRetVal)
return E_INVALIDARG;
*pRetVal = nullptr;
if (QAccessibleInterface *accessible = accessibleInterface()) {
if (QAccessibleInterface *focusacc = accessible->focusChild()) {
*pRetVal = providerForAccessible(focusacc).Detach();
}
}
return S_OK;
}
QT_END_NAMESPACE
#endif // QT_CONFIG(accessibility)

View File

@ -0,0 +1,89 @@
// Copyright (C) 2017 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
#include <initguid.h>
#include "qwindowsuiawrapper_p.h"
#include <QtCore/private/qsystemlibrary_p.h>
QT_BEGIN_NAMESPACE
// private constructor
QWindowsUiaWrapper::QWindowsUiaWrapper()
{
QSystemLibrary uiaLib(QStringLiteral("UIAutomationCore"));
if (uiaLib.load()) {
m_pUiaReturnRawElementProvider = reinterpret_cast<PtrUiaReturnRawElementProvider>(uiaLib.resolve("UiaReturnRawElementProvider"));
m_pUiaHostProviderFromHwnd = reinterpret_cast<PtrUiaHostProviderFromHwnd>(uiaLib.resolve("UiaHostProviderFromHwnd"));
m_pUiaRaiseAutomationPropertyChangedEvent = reinterpret_cast<PtrUiaRaiseAutomationPropertyChangedEvent>(uiaLib.resolve("UiaRaiseAutomationPropertyChangedEvent"));
m_pUiaRaiseAutomationEvent = reinterpret_cast<PtrUiaRaiseAutomationEvent>(uiaLib.resolve("UiaRaiseAutomationEvent"));
m_pUiaRaiseNotificationEvent = reinterpret_cast<PtrUiaRaiseNotificationEvent>(uiaLib.resolve("UiaRaiseNotificationEvent"));
m_pUiaClientsAreListening = reinterpret_cast<PtrUiaClientsAreListening>(uiaLib.resolve("UiaClientsAreListening"));
}
}
QWindowsUiaWrapper::~QWindowsUiaWrapper()
{
}
// shared instance
QWindowsUiaWrapper *QWindowsUiaWrapper::instance()
{
static QWindowsUiaWrapper wrapper;
return &wrapper;
}
// True if all symbols resolved.
BOOL QWindowsUiaWrapper::ready()
{
return m_pUiaReturnRawElementProvider
&& m_pUiaHostProviderFromHwnd
&& m_pUiaRaiseAutomationPropertyChangedEvent
&& m_pUiaRaiseAutomationEvent
&& m_pUiaClientsAreListening;
}
BOOL QWindowsUiaWrapper::clientsAreListening()
{
if (!m_pUiaClientsAreListening)
return FALSE;
return m_pUiaClientsAreListening();
}
LRESULT QWindowsUiaWrapper::returnRawElementProvider(HWND hwnd, WPARAM wParam, LPARAM lParam, IRawElementProviderSimple *el)
{
if (!m_pUiaReturnRawElementProvider)
return static_cast<LRESULT>(NULL);
return m_pUiaReturnRawElementProvider(hwnd, wParam, lParam, el);
}
HRESULT QWindowsUiaWrapper::hostProviderFromHwnd(HWND hwnd, IRawElementProviderSimple **ppProvider)
{
if (!m_pUiaHostProviderFromHwnd)
return UIA_E_NOTSUPPORTED;
return m_pUiaHostProviderFromHwnd(hwnd, ppProvider);
}
HRESULT QWindowsUiaWrapper::raiseAutomationPropertyChangedEvent(IRawElementProviderSimple *pProvider, PROPERTYID id, VARIANT oldValue, VARIANT newValue)
{
if (!m_pUiaRaiseAutomationPropertyChangedEvent)
return UIA_E_NOTSUPPORTED;
return m_pUiaRaiseAutomationPropertyChangedEvent(pProvider, id, oldValue, newValue);
}
HRESULT QWindowsUiaWrapper::raiseAutomationEvent(IRawElementProviderSimple *pProvider, EVENTID id)
{
if (!m_pUiaRaiseAutomationEvent)
return UIA_E_NOTSUPPORTED;
return m_pUiaRaiseAutomationEvent(pProvider, id);
}
HRESULT QWindowsUiaWrapper::raiseNotificationEvent(IRawElementProviderSimple *pProvider, NotificationKind notificationKind, NotificationProcessing notificationProcessing, BSTR displayString, BSTR activityId)
{
if (!m_pUiaRaiseNotificationEvent)
return UIA_E_NOTSUPPORTED;
return m_pUiaRaiseNotificationEvent(pProvider, notificationKind, notificationProcessing, displayString, activityId);
}
QT_END_NAMESPACE

View File

@ -0,0 +1,57 @@
// Copyright (C) 2017 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 QWINDOWSUIAWRAPPER_H
#define QWINDOWSUIAWRAPPER_H
//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of other Qt classes. This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//
#include <QtGui/private/qtguiglobal_p.h>
QT_REQUIRE_CONFIG(accessibility);
QT_BEGIN_NAMESPACE
class QWindowsUiaWrapper
{
QWindowsUiaWrapper();
virtual ~QWindowsUiaWrapper();
public:
static QWindowsUiaWrapper *instance();
BOOL ready();
BOOL clientsAreListening();
LRESULT returnRawElementProvider(HWND hwnd, WPARAM wParam, LPARAM lParam, IRawElementProviderSimple *el);
HRESULT hostProviderFromHwnd(HWND hwnd, IRawElementProviderSimple **ppProvider);
HRESULT raiseAutomationPropertyChangedEvent(IRawElementProviderSimple *pProvider, PROPERTYID id, VARIANT oldValue, VARIANT newValue);
HRESULT raiseAutomationEvent(IRawElementProviderSimple *pProvider, EVENTID id);
HRESULT raiseNotificationEvent(IRawElementProviderSimple *pProvider, NotificationKind notificationKind, NotificationProcessing notificationProcessing, BSTR displayString, BSTR activityId);
private:
typedef LRESULT (WINAPI *PtrUiaReturnRawElementProvider)(HWND, WPARAM, LPARAM, IRawElementProviderSimple *);
typedef HRESULT (WINAPI *PtrUiaHostProviderFromHwnd)(HWND, IRawElementProviderSimple **);
typedef HRESULT (WINAPI *PtrUiaRaiseAutomationPropertyChangedEvent)(IRawElementProviderSimple *, PROPERTYID, VARIANT, VARIANT);
typedef HRESULT (WINAPI *PtrUiaRaiseAutomationEvent)(IRawElementProviderSimple *, EVENTID);
typedef HRESULT (WINAPI *PtrUiaRaiseNotificationEvent)(IRawElementProviderSimple *, NotificationKind, NotificationProcessing, BSTR, BSTR);
typedef BOOL (WINAPI *PtrUiaClientsAreListening)();
PtrUiaReturnRawElementProvider m_pUiaReturnRawElementProvider = nullptr;
PtrUiaHostProviderFromHwnd m_pUiaHostProviderFromHwnd = nullptr;
PtrUiaRaiseAutomationPropertyChangedEvent m_pUiaRaiseAutomationPropertyChangedEvent = nullptr;
PtrUiaRaiseAutomationEvent m_pUiaRaiseAutomationEvent = nullptr;
PtrUiaRaiseNotificationEvent m_pUiaRaiseNotificationEvent = nullptr;
PtrUiaClientsAreListening m_pUiaClientsAreListening = nullptr;
};
QT_END_NAMESPACE
#endif //QWINDOWSUIAWRAPPER_H

View File

@ -0,0 +1,426 @@
#pragma once
#include <shtypes.h>
#include <winuser.h>
#include <windef.h>
#include <shellscalingapi.h>
#define MDT_MAXIMUM_DPI 3
namespace vxkex {
static INT GetSystemMetricsForDpi(
IN INT Index,
IN UINT Dpi)
{
INT Value;
Value = GetSystemMetrics(Index);
switch (Index) {
case SM_CXVSCROLL:
case SM_CYHSCROLL:
case SM_CYCAPTION:
case SM_CYVTHUMB:
case SM_CXHTHUMB:
case SM_CXICON:
case SM_CYICON:
case SM_CXCURSOR:
case SM_CYCURSOR:
case SM_CYMENU:
case SM_CYVSCROLL:
case SM_CXHSCROLL:
case SM_CXMIN:
case SM_CXMINTRACK:
case SM_CYMIN:
case SM_CYMINTRACK:
case SM_CXSIZE:
case SM_CXFRAME:
case SM_CYFRAME:
case SM_CXICONSPACING:
case SM_CYICONSPACING:
case SM_CXSMICON:
case SM_CYSMICON:
case SM_CYSMCAPTION:
case SM_CXSMSIZE:
case SM_CYSMSIZE:
case SM_CXMENUSIZE:
case SM_CYMENUSIZE:
case SM_CXMENUCHECK:
case SM_CYMENUCHECK:
// These are pixel values that have to be scaled according to DPI.
Value *= Dpi;
Value /= USER_DEFAULT_SCREEN_DPI;
break;
}
return Value;
}
static BOOL SystemParametersInfoForDpi(
IN UINT Action,
IN UINT Parameter,
IN OUT PVOID Data,
IN UINT WinIni,
IN UINT Dpi)
{
switch (Action) {
case SPI_GETICONTITLELOGFONT:
return SystemParametersInfo(Action, Parameter, Data, 0);
case SPI_GETICONMETRICS:
{
BOOL Success;
PICONMETRICS IconMetrics;
Success = SystemParametersInfo(Action, Parameter, Data, 0);
if (Success) {
IconMetrics = (PICONMETRICS) Data;
IconMetrics->iHorzSpacing *= Dpi;
IconMetrics->iVertSpacing *= Dpi;
IconMetrics->iHorzSpacing /= USER_DEFAULT_SCREEN_DPI;
IconMetrics->iVertSpacing /= USER_DEFAULT_SCREEN_DPI;
}
return Success;
}
case SPI_GETNONCLIENTMETRICS:
{
BOOL Success;
PNONCLIENTMETRICS NonClientMetrics;
Success = SystemParametersInfo(Action, Parameter, Data, 0);
if (Success) {
NonClientMetrics = (PNONCLIENTMETRICS) Data;
NonClientMetrics->iBorderWidth *= Dpi;
NonClientMetrics->iScrollWidth *= Dpi;
NonClientMetrics->iScrollHeight *= Dpi;
NonClientMetrics->iCaptionWidth *= Dpi;
NonClientMetrics->iCaptionHeight *= Dpi;
NonClientMetrics->iSmCaptionWidth *= Dpi;
NonClientMetrics->iSmCaptionHeight *= Dpi;
NonClientMetrics->iMenuWidth *= Dpi;
NonClientMetrics->iMenuHeight *= Dpi;
NonClientMetrics->iPaddedBorderWidth *= Dpi;
NonClientMetrics->iBorderWidth /= USER_DEFAULT_SCREEN_DPI;
NonClientMetrics->iScrollWidth /= USER_DEFAULT_SCREEN_DPI;
NonClientMetrics->iScrollHeight /= USER_DEFAULT_SCREEN_DPI;
NonClientMetrics->iCaptionWidth /= USER_DEFAULT_SCREEN_DPI;
NonClientMetrics->iCaptionHeight /= USER_DEFAULT_SCREEN_DPI;
NonClientMetrics->iSmCaptionWidth /= USER_DEFAULT_SCREEN_DPI;
NonClientMetrics->iSmCaptionHeight /= USER_DEFAULT_SCREEN_DPI;
NonClientMetrics->iMenuWidth /= USER_DEFAULT_SCREEN_DPI;
NonClientMetrics->iMenuHeight /= USER_DEFAULT_SCREEN_DPI;
NonClientMetrics->iPaddedBorderWidth /= USER_DEFAULT_SCREEN_DPI;
}
return Success;
}
default:
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
}
static HRESULT GetScaleFactorForMonitor(
IN HMONITOR Monitor,
OUT DEVICE_SCALE_FACTOR *ScaleFactor)
{
HDC DeviceContext;
ULONG LogPixelsX;
DeviceContext = GetDC(NULL);
if (!DeviceContext) {
*ScaleFactor = SCALE_100_PERCENT;
return S_OK;
}
LogPixelsX = GetDeviceCaps(DeviceContext, LOGPIXELSX);
ReleaseDC(NULL, DeviceContext);
*ScaleFactor = (DEVICE_SCALE_FACTOR) (9600 / LogPixelsX);
return S_OK;
}
static HRESULT GetDpiForMonitor(
IN HMONITOR Monitor,
IN MONITOR_DPI_TYPE DpiType,
OUT UINT * DpiX,
OUT UINT * DpiY)
{
HDC DeviceContext;
if (DpiType >= MDT_MAXIMUM_DPI) {
return E_INVALIDARG;
}
if (!DpiX || !DpiY) {
return E_INVALIDARG;
}
if (!IsProcessDPIAware()) {
*DpiX = USER_DEFAULT_SCREEN_DPI;
*DpiY = USER_DEFAULT_SCREEN_DPI;
return S_OK;
}
DeviceContext = GetDC(NULL);
if (!DeviceContext) {
*DpiX = USER_DEFAULT_SCREEN_DPI;
*DpiY = USER_DEFAULT_SCREEN_DPI;
return S_OK;
}
*DpiX = GetDeviceCaps(DeviceContext, LOGPIXELSX);
*DpiY = GetDeviceCaps(DeviceContext, LOGPIXELSY);
if (DpiType == MDT_EFFECTIVE_DPI) {
DEVICE_SCALE_FACTOR ScaleFactor;
// We have to multiply the DPI values by the scaling factor.
vxkex::GetScaleFactorForMonitor(Monitor, &ScaleFactor);
*DpiX *= ScaleFactor;
*DpiY *= ScaleFactor;
*DpiX /= 100;
*DpiY /= 100;
}
ReleaseDC(NULL, DeviceContext);
return S_OK;
}
static UINT GetDpiForSystem(
VOID)
{
HDC DeviceContext;
ULONG LogPixelsX;
if (!IsProcessDPIAware()) {
return 96;
}
DeviceContext = GetDC(NULL);
if (!DeviceContext) {
return 96;
}
LogPixelsX = GetDeviceCaps(DeviceContext, LOGPIXELSX);
ReleaseDC(NULL, DeviceContext);
return LogPixelsX;
}
static UINT GetDpiForWindow(
IN HWND Window)
{
if (!IsWindow(Window)) {
return 0;
}
return vxkex::GetDpiForSystem();
}
static BOOL AdjustWindowRectExForDpi(
IN OUT LPRECT Rect,
IN ULONG WindowStyle,
IN BOOL HasMenu,
IN ULONG WindowExStyle,
IN ULONG Dpi)
{
// I'm not sure how to implement this function properly.
// If it turns out to be important, I'll have to do some testing
// on a Win10 VM.
return AdjustWindowRectEx(
Rect,
WindowStyle,
HasMenu,
WindowExStyle);
}
static BOOL SetDisplayAutoRotationPreferences(
IN ORIENTATION_PREFERENCE Orientation)
{
return TRUE;
}
static BOOL GetDisplayAutoRotationPreferences(
OUT ORIENTATION_PREFERENCE * Orientation)
{
*Orientation = ORIENTATION_PREFERENCE_NONE;
return TRUE;
}
// scaling.c
static BOOL SetProcessDpiAwarenessContext(
IN DPI_AWARENESS_CONTEXT DpiContext)
{
if ((ULONG_PTR)DpiContext == (ULONG_PTR)DPI_AWARENESS_CONTEXT_UNAWARE) {
//NOTHING;
} else if ((ULONG_PTR)DpiContext == (ULONG_PTR)DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ||
(ULONG_PTR)DpiContext == (ULONG_PTR)DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ||
(ULONG_PTR)DpiContext == (ULONG_PTR)DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) {
SetProcessDPIAware();
} else {
return FALSE;
}
return TRUE;
}
static BOOL AreDpiAwarenessContextsEqual(
IN DPI_AWARENESS_CONTEXT Value1,
IN DPI_AWARENESS_CONTEXT Value2)
{
return (Value1 == Value2);
}
static BOOL IsValidDpiAwarenessContext(
IN DPI_AWARENESS_CONTEXT Value)
{
if ((ULONG_PTR)Value == (ULONG_PTR)DPI_AWARENESS_CONTEXT_UNAWARE ||
(ULONG_PTR)Value == (ULONG_PTR)DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED ||
(ULONG_PTR)Value == (ULONG_PTR)DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ||
(ULONG_PTR)Value == (ULONG_PTR)DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE ||
(ULONG_PTR)Value == (ULONG_PTR)DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) {
return TRUE;
} else {
return FALSE;
}
}
static BOOL EnableNonClientDpiScaling(
IN HWND Window)
{
return TRUE;
}
static DPI_AWARENESS_CONTEXT GetThreadDpiAwarenessContext(
VOID)
{
if (IsProcessDPIAware()) {
return DPI_AWARENESS_CONTEXT_SYSTEM_AWARE;
} else {
return DPI_AWARENESS_CONTEXT_UNAWARE;
}
}
static DPI_AWARENESS_CONTEXT GetWindowDpiAwarenessContext(
IN HWND Window)
{
ULONG WindowThreadId;
ULONG WindowProcessId;
WindowThreadId = GetWindowThreadProcessId(Window, &WindowProcessId);
if (!WindowThreadId) {
return 0;
}
// looks like there's a bug in vxkex, here should be == instead of =
// and if is always true
// anyway I don't want to deal with Windows kernel mode structures here
if (1) { //if (WindowProcessId = (ULONG) NtCurrentTeb()->ClientId.UniqueProcess) {
return vxkex::GetThreadDpiAwarenessContext();
}
return DPI_AWARENESS_CONTEXT_UNAWARE;
}
// pointer.c
static BOOL GetPointerType(
IN UINT32 PointerId,
OUT POINTER_INPUT_TYPE *PointerType)
{
*PointerType = PT_MOUSE;
return TRUE;
}
static BOOL GetPointerFrameTouchInfo(
IN UINT32 PointerId,
IN OUT UINT32 *PointerCount,
OUT LPVOID TouchInfo)
{
return FALSE;
}
static BOOL GetPointerFrameTouchInfoHistory(
IN UINT32 PointerId,
IN OUT UINT32 *EntriesCount,
IN OUT UINT32 *PointerCount,
OUT LPVOID TouchInfo)
{
return FALSE;
}
static BOOL GetPointerPenInfo(
IN UINT32 PointerId,
OUT LPVOID PenInfo)
{
return FALSE;
}
static BOOL GetPointerPenInfoHistory(
IN UINT32 PointerId,
IN OUT UINT32 *EntriesCount,
OUT LPVOID PenInfo)
{
return FALSE;
}
static BOOL SkipPointerFrameMessages(
IN UINT32 PointerId)
{
return TRUE;
}
static BOOL GetPointerDeviceRects(
IN HANDLE Device,
OUT LPRECT PointerDeviceRect,
OUT LPRECT DisplayRect)
{
PointerDeviceRect->top = 0;
PointerDeviceRect->left = 0;
PointerDeviceRect->bottom = GetSystemMetrics(SM_CYVIRTUALSCREEN);
PointerDeviceRect->right = GetSystemMetrics(SM_CXVIRTUALSCREEN);
DisplayRect->top = 0;
DisplayRect->left = 0;
DisplayRect->bottom = GetSystemMetrics(SM_CYVIRTUALSCREEN);
DisplayRect->right = GetSystemMetrics(SM_CXVIRTUALSCREEN);
return TRUE;
}
static BOOL GetPointerInfo(
IN DWORD PointerId,
OUT POINTER_INFO *PointerInfo)
{
PointerInfo->pointerType = PT_MOUSE;
PointerInfo->pointerId = PointerId;
PointerInfo->frameId = 0;
PointerInfo->pointerFlags = POINTER_FLAG_NONE;
PointerInfo->sourceDevice = NULL;
PointerInfo->hwndTarget = NULL;
GetCursorPos(&PointerInfo->ptPixelLocation);
GetCursorPos(&PointerInfo->ptHimetricLocation);
GetCursorPos(&PointerInfo->ptPixelLocationRaw);
GetCursorPos(&PointerInfo->ptHimetricLocationRaw);
PointerInfo->dwTime = 0;
PointerInfo->historyCount = 1;
PointerInfo->InputData = 0;
PointerInfo->dwKeyStates = 0;
PointerInfo->PerformanceCount = 0;
PointerInfo->ButtonChangeType = POINTER_CHANGE_NONE;
return TRUE;
}
} // namespace vxkex

File diff suppressed because it is too large Load Diff