summaryrefslogtreecommitdiffstats
path: root/tdecore/tdehw/hwlibdaemons/tdedbus/InputEventsService.cpp
blob: 458df9467a194756f1813312ebc6e2f0bb4e2281 (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
/*
 * InputEventsService.cpp
 *
 *  Created on: Feb 1, 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 <unistd.h>
#include <tqfile.h>
#include <tqtextstream.h>

// Input devices
#include <linux/input.h>

#include <tqvaluelist.h>

#include "InputEventsService.h"

#define BITS_PER_LONG (sizeof(long) * 8)
#define NUM_BITS(x) ((((x) - 1) / BITS_PER_LONG) + 1)

InputEventsService::InputEventsService(TQT_DBusConnection &conn)
: m_connection(&conn)
{
    // TODO Auto-generated constructor stub

}

InputEventsService::~InputEventsService()
{
    if (m_connection)
        delete m_connection;
}

/*!
 * Implement virtual methods
 *
 */

TQValueList< TQ_UINT32 > InputEventsService::getSwitches(const TQString& device, bool active, TQT_DBusError& error) {
    int r;
    unsigned long switches[NUM_BITS(EV_CNT)];
    TQValueList< TQ_UINT32 > value = TQValueList< TQ_UINT32 >();

    if (!device.isEmpty() && (device.find("/dev/input/event") == 0) ) {

        TQFile file( device );
        if ( !file.open( IO_ReadOnly ) ) {
            error = TQT_DBusError::stdFailed(TQString ("Could not open device %1").arg(device));
            return value;
        }

        if( active ) {
            r = ioctl(file.handle(), EVIOCGSW(sizeof(switches)), switches);
        }
        else {
            r = ioctl(file.handle(), EVIOCGBIT(EV_SW, EV_CNT), switches);
        }
        if( r > 0 ) {
            // add the arguments to the reply
            for( int i = 0; i < sizeof(switches)/sizeof(switches[0]); i++ ) {
                value.append( switches[i] );
            }
        }
        else {
            error = TQT_DBusError::stdFailed(TQString ("Failed to handle IOCTL for device: " + device));
        }
        file.close();
    } else {
        error = TQT_DBusError::stdInvalidArgs(TQString ("Invalid argument for device: " + device));
    }

    return value;
}

bool InputEventsService::GetProvidedSwitches(const TQString& device, TQValueList< TQ_UINT32 >& value, TQT_DBusError& error) {
    value = getSwitches(device, false, error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

bool InputEventsService::GetActiveSwitches(const TQString& device, TQValueList< TQ_UINT32 >& value, TQT_DBusError& error) {
    value = getSwitches(device, true, error);
    if (error.isValid()) {
        tqDebug(error.message().local8Bit());
        return false;
    }
    return true;
}

void InputEventsService::handleMethodReply(const TQT_DBusMessage& reply) {
    m_connection->send(reply);
}