You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tdelibs/tdecore/tdehw/hwlibdaemons/tdedbus/HardwareControl.cpp

113 lines
3.6 KiB
C++

/*
* HardwareControl.cpp
*
* Created on: Feb 2, 2021
* Author: emanoil
*
* hardwarecontrol Copyright (C) 2021 trinity desktop development team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <tqtimer.h>
#include <tqdbusmessage.h>
#include "common.h"
#include "HardwareControl.h"
#define DBUS_HWCTRL_SERVICE_NAME "org.trinitydesktop.hardwarecontrol"
#define DBUS_CONNECTION_TIMEOUT 4000
#define DBUS_CONNECTION_RETRY 3
HardwareControl::HardwareControl(int &argc, char **argv, bool GUIenabled, bool SMenabled)
: TQApplication(argc, argv, GUIenabled, SMenabled),
retryCount(0)
{
// init session connection to dbus
if (!initDBUS()) {
tqDebug("Failed to initialize the connection to DBus");
TQTimer::singleShot(DBUS_CONNECTION_TIMEOUT, this, TQ_SLOT(slotReconnect()));
}
}
HardwareControl::~HardwareControl()
{
// close D-Bus connection
dbusConnectionClose();
}
bool HardwareControl::initDBUS(){
m_connection = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus, DBUS_HWCTRL_SERVICE_NAME);
if ( !m_connection.isConnected() ) {
tqDebug("Failed to open connection to system message bus: "
+ m_connection.lastError().message());
return false;
}
m_connection.connect(this, TQ_SLOT(slotDbusSignal(const TQT_DBusMessage&)));
// try to get a specific service name
if (!m_connection.requestName(DBUS_HWCTRL_SERVICE_NAME, TQT_DBusConnection::NoReplace))
return false;
// make sure we get a reply
m_connection.scheduleDispatch();
return true;
}
void HardwareControl::slotDbusSignal(const TQT_DBusMessage& message) {
TQString serviceName = message[0].toString();
if ( message.interface() == TQString("org.freedesktop.DBus") &&
message.member() == TQString("NameAcquired") &&
serviceName == DBUS_HWCTRL_SERVICE_NAME )
{
tqDebug("TDEHW unique DBus name acquired: " + serviceName);
rootService = new RootNodeService(m_connection);
orgService = new OrgNodeService(m_connection);
trinitydesktopService = new TrinityDesktopNodeService(m_connection);
hardwarecontrolService = new HardwareControlNodeService(m_connection);
tqDebug("TDEHW service setup done.");
}
}
void HardwareControl::dbusConnectionClose() {
DEL(rootService)
DEL(orgService)
DEL(trinitydesktopService)
DEL(hardwarecontrolService)
if(m_connection.isConnected()) {
m_connection.disconnect(this, TQ_SLOT(slotDbusSignal(const TQT_DBusMessage&)));
m_connection.closeConnection(DBUS_HWCTRL_SERVICE_NAME);
}
retryCount=0;
}
void HardwareControl::slotReconnect() {
dbusConnectionClose();
if (!initDBUS()) {
if (DBUS_CONNECTION_RETRY > retryCount) {
tqDebug("Failed to initialize the connection to DBus");
}
TQTimer::singleShot(DBUS_CONNECTION_TIMEOUT, this, TQ_SLOT(slotReconnect()));
retryCount++;
}
}
#include "HardwareControl.moc"