Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
dbusconnection.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
5
6#include "dbusconnection_p.h"
7
8#include <QtDBus/QDBusMessage>
9#include <QtDBus/QDBusServiceWatcher>
10#include <qdebug.h>
11
12#include <QDBusConnectionInterface>
13#include "bus_interface.h"
14
15#include <QtGui/qguiapplication.h>
16#include <qpa/qplatformnativeinterface.h>
17
19
20using namespace Qt::StringLiterals;
21
22/* note: do not change these to QStringLiteral;
23 we are unloaded before QtDBus is done using the strings.
24 */
25#define A11Y_SERVICE "org.a11y.Bus"_L1
26#define A11Y_PATH "/org/a11y/bus"_L1
27
36 : QObject(parent), m_a11yConnection(QString()), m_enabled(false)
37{
38 // If the bus is explicitly set via env var it overrides everything else.
39 QByteArray addressEnv = qgetenv("AT_SPI_BUS_ADDRESS");
40 if (!addressEnv.isEmpty()) {
41 m_enabled = true;
42 connectA11yBus(QString::fromLocal8Bit(addressEnv));
43 return;
44 }
45
46 // Start monitoring if "org.a11y.Bus" is registered as DBus service.
48 if (!c.isConnected()) {
49 return;
50 }
51
53 connect(dbusWatcher, SIGNAL(serviceRegistered(QString)), this, SLOT(serviceRegistered()));
54
55 // If it is registered already, setup a11y right away
56 if (c.interface()->isServiceRegistered(A11Y_SERVICE))
57 serviceRegistered();
58
59 // In addition try if there is an xatom exposing the bus address, this allows applications run as root to work
60 QString address = getAddressFromXCB();
61 if (!address.isEmpty()) {
62 m_enabled = true;
63 connectA11yBus(address);
64 }
65}
66
67QString DBusConnection::getAddressFromXCB()
68{
69 QGuiApplication *app = qobject_cast<QGuiApplication *>(QCoreApplication::instance());
70 if (!app)
71 return QString();
72 QPlatformNativeInterface *platformNativeInterface = app->platformNativeInterface();
73 QByteArray *addressByteArray = reinterpret_cast<QByteArray*>(
74 platformNativeInterface->nativeResourceForIntegration(QByteArrayLiteral("AtspiBus")));
75 if (addressByteArray) {
76 QString address = QString::fromLatin1(*addressByteArray);
77 delete addressByteArray;
78 return address;
79 }
80 return QString();
81}
82
83// We have the a11y registry on the session bus.
84// Subscribe to updates about a11y enabled state.
85// Find out the bus address
86void DBusConnection::serviceRegistered()
87{
88 // listen to enabled changes
90 OrgA11yStatusInterface *a11yStatus = new OrgA11yStatusInterface(A11Y_SERVICE, A11Y_PATH, c, this);
91
92 //The variable was introduced because on some embedded platforms there are custom accessibility
93 //clients which don't set Status.ScreenReaderEnabled to true. The variable is also useful for
94 //debugging.
95 static const bool a11yAlwaysOn = qEnvironmentVariableIsSet("QT_LINUX_ACCESSIBILITY_ALWAYS_ON");
96
97 bool enabled = a11yAlwaysOn || a11yStatus->screenReaderEnabled() || a11yStatus->isEnabled();
98
99 if (enabled != m_enabled) {
100 m_enabled = enabled;
101 if (m_a11yConnection.isConnected()) {
102 emit enabledChanged(m_enabled);
103 } else {
106 "GetAddress"_L1);
107 c.callWithCallback(m, this, SLOT(connectA11yBus(QString)), SLOT(dbusError(QDBusError)));
108 }
109 }
110
111 // connect(a11yStatus, ); QtDbus doesn't support notifications for property changes yet
112}
113
114void DBusConnection::serviceUnregistered()
115{
116 emit enabledChanged(false);
117}
118
119void DBusConnection::connectA11yBus(const QString &address)
120{
121 if (address.isEmpty()) {
122 qWarning("Could not find Accessibility DBus address.");
123 return;
124 }
125 m_a11yConnection = QDBusConnection(QDBusConnection::connectToBus(address, "a11y"_L1));
126
127 if (m_enabled)
128 emit enabledChanged(true);
129}
130
131void DBusConnection::dbusError(const QDBusError &error)
132{
133 qWarning() << "Accessibility encountered a DBus error:" << error;
134}
135
141{
142 return m_a11yConnection;
143}
144
146
147#include "moc_dbusconnection_p.cpp"
void enabledChanged(bool enabled)
DBusConnection(QObject *parent=nullptr)
QDBusConnection connection() const
Returns the DBus connection that got established.
\inmodule QtCore
Definition qbytearray.h:57
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:106
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
\inmodule QtDBus
bool isConnected() const
Returns true if this QDBusConnection object is connected.
static QDBusConnection connectToBus(BusType type, const QString &name)
Opens a connection of type type to one of the known buses and associate with it the connection name n...
static QDBusConnection sessionBus()
Returns a QDBusConnection object opened with the session bus.
\inmodule QtDBus
Definition qdbuserror.h:21
\inmodule QtDBus
static QDBusMessage createMethodCall(const QString &destination, const QString &path, const QString &interface, const QString &method)
Constructs a new DBus message representing a method call.
The QDBusServiceWatcher class allows the user to watch for a bus service change.
\macro qGuiApp
static QPlatformNativeInterface * platformNativeInterface()
\inmodule QtCore
Definition qobject.h:90
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
The QPlatformNativeInterface class provides an abstraction for retrieving native resource handles.
virtual void * nativeResourceForIntegration(const QByteArray &resource)
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5710
static QString fromLocal8Bit(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5788
#define A11Y_PATH
#define A11Y_SERVICE
Combined button and popup list for selecting options.
#define QByteArrayLiteral(str)
Definition qbytearray.h:52
DBusConnection const char DBusError * error
#define qWarning
Definition qlogging.h:162
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
const GLfloat * m
GLenum GLenum GLsizei const GLuint GLboolean enabled
const GLubyte * c
GLuint GLuint64EXT address
Q_CORE_EXPORT QByteArray qgetenv(const char *varName)
Q_CORE_EXPORT bool qEnvironmentVariableIsSet(const char *varName) noexcept
#define emit
#define enabled
QApplication app(argc, argv)
[0]
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent