aboutsummaryrefslogtreecommitdiffstats
path: root/tests/manual/x11vkbwrapper/handleatspievents.cpp
blob: ed330e0c94c097a0902a20943b5a81bbff5f0e2a (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Virtual Keyboard module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QLoggingCategory>
#include <QTime>
#include <QGuiApplication>
#include <QMutableVectorIterator>

#include "handleatspievents.h"
#include "vkbhidetimer.h"
#include "atspi/atspi.h"

namespace  {
const QString KAtspiBusLauncher = "at-spi-bus-launcher";
const QString KAtspiRegistryd = "at-spi2-registryd";
const int KProsessIsRunning = 0;
}

Q_LOGGING_CATEGORY(lcHandleAtspiEvents, "qt.virtualkeyboard.tests.manual.x11vkbwrapper.handleatspievents")

/**
 * @brief focusEventFromInput Called when a widget is focused.
 * @param event
 * @param user_data
 */
 void focusEventFromInput(AtspiEvent *event, void *user_data)
{
    qCDebug(lcHandleAtspiEvents) << Q_FUNC_INFO;
    auto *handleATSPIEvents = static_cast<HandleATSPIEvents *>(user_data);
    handleATSPIEvents->gotFocusEventFromInput(event);
}

/**
 * @brief HandleATSPIEvents::HandleATSPIEvents
 * @param parent
 */
HandleATSPIEvents::HandleATSPIEvents(QObject *parent)
    : QObject(parent),
    m_keyboardVisible(false),
    m_focuses(0)
{

}

/**
 * @brief HandleATSPIEvents::~HandleATSPIEvents
 */
HandleATSPIEvents::~HandleATSPIEvents()
{
    qCDebug(lcHandleAtspiEvents) << Q_FUNC_INFO;

    m_focuses.clear();

    if (!atspi_event_listener_deregister_from_callback(focusEventFromInput, static_cast<void*>(this), "object:state-changed:focused", nullptr)) {
            qWarning() << "Error occurred: Problem deregistering focus listener";
     }
}

/**
 * @brief HandleATSPIEvents::init
 * @return false if at-spi is not running or callback regitering fail
 */

bool HandleATSPIEvents::init()
{
    qCDebug(lcHandleAtspiEvents) << Q_FUNC_INFO;
    /** Check that At-spi is running */
    if (KProsessIsRunning != system(QString("pidof -x %1 > /dev/null").arg(KAtspiBusLauncher).toLatin1().data()) ||
            KProsessIsRunning != system(QString("pidof -x %1 > /dev/null").arg(KAtspiRegistryd).toLatin1().data())) {
        qWarning() << "One or both of the At-Spi related processes are not running.";
        return false;
    }

    GError *error = nullptr;
    /** Registered the spi events to monitor focus and show on editable widgets. */
    if (!atspi_event_listener_register_from_callback(focusEventFromInput,
                                                     static_cast<void*>(this),
                                                     nullptr,
                                                     "object:state-changed:focused",
                                                     &error)){
        qWarning() << Q_FUNC_INFO << "Error occurred: ATSPI listener register failed. Error message:" << error->message;
        return false;
    }

    QObject::connect(QGuiApplication::inputMethod(), &QInputMethod::visibleChanged, [this] {
        this->setKeyboardVisible(QGuiApplication::inputMethod()->isVisible());
    });

    return true;
}

/**
 * @brief handleATSPIEvents::setKeyboardVisible
 * @param visible
 */
void HandleATSPIEvents::setKeyboardVisible(const bool visible)
{
    if (m_keyboardVisible != visible) {
        m_keyboardVisible = visible;
        qCDebug(lcHandleAtspiEvents) << "SET VKB visible " << visible;
        if (m_keyboardVisible && !QGuiApplication::inputMethod()->isVisible()) {
            QGuiApplication::inputMethod()->show();
        } else {
            QGuiApplication::inputMethod()->hide();
        }
    }
}

/**
 * @brief handleATSPIEvents::storeFocusElement
 * @param role
 * @param focus
 */
void HandleATSPIEvents::storeFocusElement(const qint8 role)
{
    m_focuses.append(role);
    qCDebug(lcHandleAtspiEvents) << "*****INSERTED FOCUS ELEMENT*****" << role << "TOTAL:"  << m_focuses.length();
}

/**
 * @brief handleATSPIEvents::isThereFocus
 * AT-SPI focus in/out events are received in random order and for some
 * objects AT-SPI doesn't send any focus OUT event at all.
 * This function keeps track if there's an accepted type of object in focus and
 * knows to release/ignore the objects that do not receive focus OUT event.
 * @param role
 */
bool HandleATSPIEvents::isThereFocus(const qint8 role)
{
    qCDebug(lcHandleAtspiEvents) << " FOCUS ELEMENT to EXAMINE: " << role;
    qint8 roleValue = ATSPI_ROLE_INVALID;
    for (auto iter = m_focuses.begin() ; iter != m_focuses.end() ; iter++) {
        roleValue = *iter;
        if (roleValue == role ||
            roleValue == ATSPI_ROLE_DOCUMENT_WEB ||
            roleValue == ATSPI_ROLE_ENTRY ||
            roleValue == ATSPI_ROLE_LINK) {
            qCDebug(lcHandleAtspiEvents) << "*****REMOVING FOCUS ELEMENT*****: " << *iter;
            m_focuses.erase(iter--);
        }
    }
    m_focuses.squeeze();
    return !m_focuses.isEmpty();
}

/**
 * @brief handleATSPIEvents::gotFocusEventFromInput
 * @param event
 */
void HandleATSPIEvents::gotFocusEventFromInput(const AtspiEvent *event)
{
    qCDebug(lcHandleAtspiEvents) << Q_FUNC_INFO << event->type << event->detail1 << event->detail2 << QTime::currentTime().toString();

    GError *error = nullptr;
    AtspiStateSet *state_set = atspi_accessible_get_state_set(event->source);
    AtspiRole role = atspi_accessible_get_role(event->source, &error);

    if (error) {
        qCDebug(lcHandleAtspiEvents) << Q_FUNC_INFO << "Event error message:" << error->message;
    }
    qCDebug(lcHandleAtspiEvents) << "ATSPI focus event received. Object role=" << role;

    if ((((role == ATSPI_ROLE_TERMINAL) || (role == ATSPI_ROLE_PANEL)  ||
          (role == ATSPI_ROLE_TABLE_CELL) ||
          (((role == ATSPI_ROLE_TEXT) ||
            (role == ATSPI_ROLE_PASSWORD_TEXT) ||
            (role == ATSPI_ROLE_SECTION) ||
            (role == ATSPI_ROLE_PARAGRAPH) ||
            (role = ATSPI_ROLE_ENTRY)) &&
           state_set &&
           atspi_state_set_contains(state_set, ATSPI_STATE_EDITABLE))))) {

        if (event->detail1) {
            qCDebug(lcHandleAtspiEvents) << "ACCEPTED FOCUS IN";
            VkbHideTimer::getInstance()->startTimer();
            this->storeFocusElement(role);
            if (!m_keyboardVisible) {
                setKeyboardVisible(true);
            }
        } else if (m_keyboardVisible && !isThereFocus(role)) {
            setKeyboardVisible(false);
        }

    } else {
        qCDebug(lcHandleAtspiEvents) << " ELSE: SET VKB visible FALSE";
        setKeyboardVisible(false);
        m_focuses.clear();
        m_focuses.squeeze();
    }
}