summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/windows/uiautomation/qwindowsuiaaccessibility.cpp
blob: 1abb412ccd29ec8e1e65858ca30571ac44b73011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// 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 <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()) {
            QWindowsUiaMainProvider *provider = QWindowsUiaMainProvider::providerForAccessible(accessible);
            *lResult = UiaReturnRawElementProvider(hwnd, wParam, lParam, provider);
            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;

    // No need to do anything when nobody is listening.
    if (!UiaClientsAreListening())
        return;

    switch (event->type()) {
    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)