summaryrefslogtreecommitdiffstats
path: root/tdecore/tdehw/hwlibdaemons/tdedbus/HardwareControl.cpp
blob: 8a36807e5a05de97e4dec5eff87ff900f1a1f46c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * 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 "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, TQT_SLOT(slotReconnect()));
        retryCount++;
    }
}

HardwareControl::~HardwareControl()
{
    // close D-Bus connection
    close();

    delete hardwarecontrolService;
    delete trinitydesktopService;
    delete orgService;
    delete rootService;
}

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;
    }

    // try to get a specific service name
    if (!m_connection.requestName(DBUS_HWCTRL_SERVICE_NAME, TQT_DBusConnection::NoReplace))
        return false;

    m_connection.scheduleDispatch();
    m_connection.connect(this, TQT_SLOT(slotDbusSignal(const TQT_DBusMessage&)));

    TQTimer::singleShot(10, this, TQT_SLOT(slotConnectionCheck()));

    return true;
}

void HardwareControl::close() {
    if(m_connection.isConnected()) {
        m_connection.disconnect(this, TQT_SLOT(slotDbusSignal(const TQT_DBusMessage&)));
        m_connection.closeConnection(DBUS_HWCTRL_SERVICE_NAME);
    }
}

void HardwareControl::slotReconnect() {

    close();

    if (!initDBUS()) {
        if (DBUS_CONNECTION_RETRY > retryCount) {
            tqFatal("Failed to initialize the connection to DBus");
        }
        TQTimer::singleShot(DBUS_CONNECTION_TIMEOUT, this, TQT_SLOT(slotReconnect()));
        retryCount++;
    }
}

void HardwareControl::slotDbusSignal(const TQT_DBusMessage& message) {
    if (message.interface() != TQString("org.freedesktop.DBus"))
        return;
    if (message.member() != TQString("NameAcquired"))
        return;
    tqDebug("Name acquired: " + message[0].toString());
    serviceName = message[0].toString();
}

void HardwareControl::slotConnectionCheck() {

    if (serviceName != DBUS_HWCTRL_SERVICE_NAME) {
        tqFatal("TDEHW service already running or no unique name possible.");
    }

    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.");
}

#include "HardwareControl.moc"