Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
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 if (QGuiApplication::platformName().startsWith("xcb"_L1)) {
60 // In addition try if there is an xatom exposing the bus address, this allows applications run as root to work
61 QString address = getAddressFromXCB();
62 if (!address.isEmpty()) {
63 m_enabled = true;
64 connectA11yBus(address);
65 }
66 }
67}
68
69QString DBusConnection::getAddressFromXCB()
70{
71 QGuiApplication *app = qobject_cast<QGuiApplication *>(QCoreApplication::instance());
72 if (!app)
73 return QString();
74 QPlatformNativeInterface *platformNativeInterface = app->platformNativeInterface();
75 QByteArray *addressByteArray = reinterpret_cast<QByteArray*>(
76 platformNativeInterface->nativeResourceForIntegration(QByteArrayLiteral("AtspiBus")));
77 if (addressByteArray) {
78 QString address = QString::fromLatin1(*addressByteArray);
79 delete addressByteArray;
80 return address;
81 }
82 return QString();
83}
84
85// We have the a11y registry on the session bus.
86// Subscribe to updates about a11y enabled state.
87// Find out the bus address
88void DBusConnection::serviceRegistered()
89{
90 // listen to enabled changes
92 OrgA11yStatusInterface *a11yStatus = new OrgA11yStatusInterface(A11Y_SERVICE, A11Y_PATH, c, this);
93
94 //The variable was introduced because on some embedded platforms there are custom accessibility
95 //clients which don't set Status.ScreenReaderEnabled to true. The variable is also useful for
96 //debugging.
97 static const bool a11yAlwaysOn = qEnvironmentVariableIsSet("QT_LINUX_ACCESSIBILITY_ALWAYS_ON");
98
99 bool enabled = a11yAlwaysOn || a11yStatus->screenReaderEnabled() || a11yStatus->isEnabled();
100
101 if (enabled != m_enabled) {
102 m_enabled = enabled;
103 if (m_a11yConnection.isConnected()) {
104 emit enabledChanged(m_enabled);
105 } else {
108 "GetAddress"_L1);
109 c.callWithCallback(m, this, SLOT(connectA11yBus(QString)), SLOT(dbusError(QDBusError)));
110 }
111 }
112
113 // connect(a11yStatus, ); QtDbus doesn't support notifications for property changes yet
114}
115
116void DBusConnection::serviceUnregistered()
117{
118 emit enabledChanged(false);
119}
120
121void DBusConnection::connectA11yBus(const QString &address)
122{
123 if (address.isEmpty()) {
124 qWarning("Could not find Accessibility DBus address.");
125 return;
126 }
127 m_a11yConnection = QDBusConnection(QDBusConnection::connectToBus(address, "a11y"_L1));
128
129 if (m_enabled)
130 emit enabledChanged(true);
131}
132
133void DBusConnection::dbusError(const QDBusError &error)
134{
135 qWarning() << "Accessibility encountered a DBus error:" << error;
136}
137
143{
144 return m_a11yConnection;
145}
146
148
149#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
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()
QString platformName
The name of the underlying platform plugin.
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
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:129
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
static QString fromLocal8Bit(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5949
#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:166
#define SLOT(a)
Definition qobjectdefs.h:52
#define SIGNAL(a)
Definition qobjectdefs.h:53
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]