Commit eccc792efd59fc3f2717c99b1a7d37e36df80bc5

  • avatar
  • swiesner <swiesner @283d02a7-25f6-0310…ecb5cbfe19da.>
  • Fri Apr 23 19:58:39 CEST 2010
Monitor the HAL service and handle (un)registration

git-svn-id: svn+ssh://svn.kde.org/home/kde/trunk/playground/utils/synaptiks@1118004 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
  
3030#include "mousedevicesmonitor.h"
3131#include <KDebug>
3232#include <QtDBus/QDBusInterface>
33#include <QtDBus/QDBusServiceWatcher>
3334#include <QtDBus/QDBusReply>
3435#include <QtCore/QStringList>
36#include <QtCore/QPointer>
3537
3638
3739using namespace synaptiks;
6161
6262 void _k_deviceAdded(const QString &udi);
6363 void _k_deviceRemoved(const QString &udi);
64 void _k_halRegistered();
65 void _k_halUnregistered();
6466 QStringList pluggedMouseDevices() const;
6567 bool queryCapability(const QString &udi,
6668 const QString &capability) const;
6769 QString getProperty(const QString &udi,
6870 const QString &property) const;
6971
70 QDBusInterface *hal_manager;
72 QPointer<QDBusInterface> hal_manager;
73 QDBusServiceWatcher *hal_watcher;
7174 QSet<QString> mouseDevices;
7275 MouseDevicesMonitor *q_ptr;
7376 };
8686 SLOT(_k_deviceAdded(const QString&)));
8787 q->connect(this->hal_manager, SIGNAL(DeviceRemoved(const QString&)),
8888 SLOT(_k_deviceRemoved(const QString&)));
89 // watch the HAL service for (un-)registration to re-initialize the
90 // connection to this service if required (e.g. HAL was restarted due to
91 // an update or HAL crashed or whatever)
92 this->hal_watcher = new QDBusServiceWatcher(q);
93 this->hal_watcher->addWatchedService(HAL_SERVICE);
94 q->connect(this->hal_watcher, SIGNAL(serviceRegistered(const QString&)),
95 SLOT(_k_halRegistered()));
96 q->connect(
97 this->hal_watcher, SIGNAL(serviceUnregistered(const QString&)),
98 SLOT(_k_halUnregistered()));
99 this->hal_watcher->setConnection(this->hal_manager->connection());
89100 this->mouseDevices = QSet<QString>::fromList(
90101 this->pluggedMouseDevices());
91102}
117117 }
118118}
119119
120void MouseDevicesMonitorPrivate::_k_halRegistered() {
121 // HAL is available now, create the interface and "connect" all mouses
122 Q_Q(MouseDevicesMonitor);
123 this->hal_manager = new QDBusInterface(
124 HAL_SERVICE, HAL_MANAGER_PATH, HAL_MANAGER_IFACE,
125 this->hal_watcher->connection(), q);
126 this->mouseDevices = QSet<QString>::fromList(
127 this->pluggedMouseDevices());
128 foreach (const QString &udi, this->mouseDevices) {
129 emit q->mousePlugged(udi);
130 }
131}
132
133void MouseDevicesMonitorPrivate::_k_halUnregistered() {
134 // HAL is gone, so we consider all mouses being disconnected now, just
135 // to be on the safe side, because we can't montor mouse devices
136 // anymore.
137 Q_Q(MouseDevicesMonitor);
138 foreach (const QString &udi, this->mouseDevices) {
139 emit q->mouseUnplugged(udi);
140 }
141 this->mouseDevices.clear();
142 // delete the now invalid interface
143 delete this->hal_manager;
144}
145
120146QStringList MouseDevicesMonitorPrivate::pluggedMouseDevices() const {
121147 kDebug() << "enumerating plugged devices";
148 if (!this->hal_manager)
149 return QStringList();
122150 QDBusReply<QStringList> devices = this->hal_manager->call(
123151 "FindDeviceByCapability", "input.mouse");
124152 if (!devices.isValid()) {
  
157157
158158 Q_PRIVATE_SLOT(d_func(), void _k_deviceAdded(const QString&))
159159 Q_PRIVATE_SLOT(d_func(), void _k_deviceRemoved(const QString&))
160 Q_PRIVATE_SLOT(d_func(), void _k_halRegistered())
161 Q_PRIVATE_SLOT(d_func(), void _k_halUnregistered())
160162
161163 MouseDevicesMonitorPrivate * const d_ptr;
162164 };