You can not 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/InputEventsService.cpp

113 lines
3.2 KiB

/*
* 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);
}