1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 
41 
42 #include "dbusconnection_p.h"
43 
44 #include <QtDBus/QDBusMessage>
45 #include <QtDBus/QDBusServiceWatcher>
46 #include <qdebug.h>
47 
48 #include <QDBusConnectionInterface>
49 #include "bus_interface.h"
50 
51 #include <QtGui/qguiapplication.h>
52 #include <qpa/qplatformnativeinterface.h>
53 
54 QT_BEGIN_NAMESPACE
55 
56 /* note: do not change these to QStringLiteral;
57    we are unloaded before QtDBus is done using the strings.
58  */
59 #define A11Y_SERVICE QLatin1String("org.a11y.Bus")
60 #define A11Y_PATH QLatin1String("/org/a11y/bus")
61 
62 /*!
63     \class DBusConnection
64     \internal
65     \brief Connects to the accessibility dbus.
66 
67     This is usually a different bus from the session bus.
68 */
DBusConnection(QObject * parent)69 DBusConnection::DBusConnection(QObject *parent)
70     : QObject(parent), m_a11yConnection(QString()), m_enabled(false)
71 {
72     // Start monitoring if "org.a11y.Bus" is registered as DBus service.
73     QDBusConnection c = QDBusConnection::sessionBus();
74     if (!c.isConnected()) {
75         return;
76     }
77 
78     dbusWatcher = new QDBusServiceWatcher(A11Y_SERVICE, c, QDBusServiceWatcher::WatchForRegistration, this);
79     connect(dbusWatcher, SIGNAL(serviceRegistered(QString)), this, SLOT(serviceRegistered()));
80 
81     // If it is registered already, setup a11y right away
82     if (c.interface()->isServiceRegistered(A11Y_SERVICE))
83         serviceRegistered();
84 
85     // In addition try if there is an xatom exposing the bus address, this allows applications run as root to work
86     QString address = getAddressFromXCB();
87     if (!address.isEmpty()) {
88         m_enabled = true;
89         connectA11yBus(address);
90     }
91 }
92 
getAddressFromXCB()93 QString DBusConnection::getAddressFromXCB()
94 {
95     QGuiApplication *app = qobject_cast<QGuiApplication *>(QCoreApplication::instance());
96     if (!app)
97         return QString();
98     QPlatformNativeInterface *platformNativeInterface = app->platformNativeInterface();
99     QByteArray *addressByteArray = reinterpret_cast<QByteArray*>(
100                 platformNativeInterface->nativeResourceForIntegration(QByteArrayLiteral("AtspiBus")));
101     if (addressByteArray) {
102         QString address = QString::fromLatin1(*addressByteArray);
103         delete addressByteArray;
104         return address;
105     }
106     return QString();
107 }
108 
109 // We have the a11y registry on the session bus.
110 // Subscribe to updates about a11y enabled state.
111 // Find out the bus address
serviceRegistered()112 void DBusConnection::serviceRegistered()
113 {
114     // listen to enabled changes
115     QDBusConnection c = QDBusConnection::sessionBus();
116     OrgA11yStatusInterface *a11yStatus = new OrgA11yStatusInterface(A11Y_SERVICE, A11Y_PATH, c, this);
117 
118     //The variable was introduced because on some embedded platforms there are custom accessibility
119     //clients which don't set Status.ScreenReaderEnabled to true. The variable is also useful for
120     //debugging.
121     static const bool a11yAlwaysOn = qEnvironmentVariableIsSet("QT_LINUX_ACCESSIBILITY_ALWAYS_ON");
122 
123     bool enabled = a11yAlwaysOn || a11yStatus->screenReaderEnabled() || a11yStatus->isEnabled();
124 
125     if (enabled != m_enabled) {
126         m_enabled = enabled;
127         if (m_a11yConnection.isConnected()) {
128             emit enabledChanged(m_enabled);
129         } else {
130             QDBusConnection c = QDBusConnection::sessionBus();
131             QDBusMessage m = QDBusMessage::createMethodCall(QLatin1String("org.a11y.Bus"),
132                                                             QLatin1String("/org/a11y/bus"),
133                                                             QLatin1String("org.a11y.Bus"), QLatin1String("GetAddress"));
134             c.callWithCallback(m, this, SLOT(connectA11yBus(QString)), SLOT(dbusError(QDBusError)));
135         }
136     }
137 
138     //    connect(a11yStatus, ); QtDbus doesn't support notifications for property changes yet
139 }
140 
serviceUnregistered()141 void DBusConnection::serviceUnregistered()
142 {
143     emit enabledChanged(false);
144 }
145 
connectA11yBus(const QString & address)146 void DBusConnection::connectA11yBus(const QString &address)
147 {
148     if (address.isEmpty()) {
149         qWarning("Could not find Accessibility DBus address.");
150         return;
151     }
152     m_a11yConnection = QDBusConnection(QDBusConnection::connectToBus(address, QLatin1String("a11y")));
153 
154     if (m_enabled)
155         emit enabledChanged(true);
156 }
157 
dbusError(const QDBusError & error)158 void DBusConnection::dbusError(const QDBusError &error)
159 {
160     qWarning() << "Accessibility encountered a DBus error:" << error;
161 }
162 
163 /*!
164   Returns the DBus connection that got established.
165   Or an invalid connection if not yet connected.
166 */
connection() const167 QDBusConnection DBusConnection::connection() const
168 {
169     return m_a11yConnection;
170 }
171 
172 QT_END_NAMESPACE
173