From 632a87af39e5d4abc909f9a63624f77fc502aee5 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Sun, 15 Apr 2012 16:27:53 -0500 Subject: Add key device monitoring support --- tdecore/tdehardwaredevices.cpp | 43 ++++++++++++++++++++++++++++++++++++++---- tdecore/tdehardwaredevices.h | 38 ++++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 7 deletions(-) (limited to 'tdecore') diff --git a/tdecore/tdehardwaredevices.cpp b/tdecore/tdehardwaredevices.cpp index 424c7533c..c4cae020d 100644 --- a/tdecore/tdehardwaredevices.cpp +++ b/tdecore/tdehardwaredevices.cpp @@ -94,7 +94,7 @@ TDESensorCluster::TDESensorCluster() { critical = -1; } -TDEGenericDevice::TDEGenericDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) { +TDEGenericDevice::TDEGenericDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TQObject() { m_deviceType = dt; m_deviceName = dn; @@ -1554,6 +1554,7 @@ void TDEMonitorDevice::internalSetPowerLevel(TDEDisplayPowerLevel::TDEDisplayPow TDEEventDevice::TDEEventDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) { m_fd = -1; + m_fdMonitorActive = false; } TDEEventDevice::~TDEEventDevice() { @@ -1639,6 +1640,29 @@ TQStringList TDEEventDevice::friendlySwitchList(TDESwitchType::TDESwitchType swi return ret; } +void TDEEventDevice::internalStartFdMonitoring(TDEHardwareDevices* hwmanager) { + if (!m_fdMonitorActive) { + // For security and performance reasons, only monitor known ACPI buttons + if (eventType() != TDEEventDeviceType::Unknown) { + m_eventNotifier = new TQSocketNotifier(m_fd, TQSocketNotifier::Read, this); + connect( m_eventNotifier, TQT_SIGNAL(activated(int)), this, TQT_SLOT(eventReceived()) ); + connect( this, TQT_SIGNAL(keyPressed(unsigned int, TDEEventDevice*)), hwmanager, TQT_SLOT(processEventDeviceKeyPressed(unsigned int, TDEEventDevice*)) ); + } + m_fdMonitorActive = true; + } +} + +void TDEEventDevice::eventReceived() { + struct input_event ev; + int r; + r = read(m_fd, &ev, sizeof(struct input_event)); + if (r > 0) { + if (ev.type == EV_KEY) { + emit keyPressed(ev.code, this); + } + } +} + TDEInputDevice::TDEInputDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) { } @@ -2073,13 +2097,17 @@ void TDEHardwareDevices::processStatelessDevices() { // We can't use m_deviceList directly as m_deviceList can only have one iterator active against it at any given time TDEGenericHardwareList devList = listAllPhysicalDevices(); for ( hwdevice = devList.first(); hwdevice; hwdevice = devList.next() ) { - if ((hwdevice->type() == TDEGenericDeviceType::RootSystem) || (hwdevice->type() == TDEGenericDeviceType::Network) || (hwdevice->type() == TDEGenericDeviceType::OtherSensor) || (hwdevice->type() == TDEGenericDeviceType::Event)) { + if ((hwdevice->type() == TDEGenericDeviceType::RootSystem) || (hwdevice->type() == TDEGenericDeviceType::Network) || (hwdevice->type() == TDEGenericDeviceType::OtherSensor) || (hwdevice->type() == TDEGenericDeviceType::Event) || (hwdevice->type() == TDEGenericDeviceType::Battery) || (hwdevice->type() == TDEGenericDeviceType::PowerSupply)) { rescanDeviceInformation(hwdevice); emit hardwareUpdated(hwdevice); } } } +void TDEHardwareDevices::processEventDeviceKeyPressed(unsigned int keycode, TDEEventDevice* edevice) { + emit eventDeviceKeyPressed(keycode, edevice); +} + void TDEHardwareDevices::processModifiedMounts() { // Detect what changed between the old mount table and the new one, // and emit appropriate events @@ -2861,6 +2889,7 @@ TDEGenericDevice* TDEHardwareDevices::classifyUnknownDevice(udev_device* dev, TD // Pull out all event special devices and stuff them under Event TQString syspath_tail = systempath.lower(); + syspath_tail.truncate(syspath_tail.length()-1); syspath_tail.remove(0, syspath_tail.findRev("/")+1); if (syspath_tail.startsWith("event")) { if (!device) device = new TDEEventDevice(TDEGenericDeviceType::Event); @@ -3713,7 +3742,11 @@ TDEGenericDevice* TDEHardwareDevices::classifyUnknownDevice(udev_device* dev, TD } // Calculate time remaining - bdevice->internalSetTimeRemaining(bdevice->energy()*bdevice->dischargeRate()*60); + // Discharge rate is in watt-hours + // Energy is in watt-hours + // Therefore, energy/rate = time in hours + // Convert to seconds... + bdevice->internalSetTimeRemaining((bdevice->energy()/bdevice->dischargeRate())*60*60); } if (device->type() == TDEGenericDeviceType::PowerSupply) { @@ -3987,7 +4020,7 @@ TDEGenericDevice* TDEHardwareDevices::classifyUnknownDevice(udev_device* dev, TD else if (edevice->systemPath().contains("PNP0C0E")) { edevice->internalSetEventType(TDEEventDeviceType::ACPISleepButton); } - else if (edevice->systemPath().contains("PNP0C0C")) { + else if (edevice->systemPath().contains("PNP0C0C") || edevice->systemPath().contains("/LNXPWRBN")) { edevice->internalSetEventType(TDEEventDeviceType::ACPIPowerButton); } else { @@ -4122,6 +4155,8 @@ TDEGenericDevice* TDEHardwareDevices::classifyUnknownDevice(udev_device* dev, TD #endif } edevice->internalSetActiveSwitches(activeSwitches); + + edevice->internalStartFdMonitoring(this); } // Root devices are still special diff --git a/tdecore/tdehardwaredevices.h b/tdecore/tdehardwaredevices.h index 9061ad7fc..8eb21d92d 100644 --- a/tdecore/tdehardwaredevices.h +++ b/tdecore/tdehardwaredevices.h @@ -191,8 +191,10 @@ class TDECORE_EXPORT TDESensorCluster double critical; }; -class TDECORE_EXPORT TDEGenericDevice +class TDECORE_EXPORT TDEGenericDevice : public TQObject { + Q_OBJECT + public: /** * Constructor. @@ -916,7 +918,7 @@ class TDECORE_EXPORT TDEBatteryDevice : public TDEGenericDevice double maximumDesignEnergy(); /** - * @return a double with the current battery discharge rate in volt-hours, if available + * @return a double with the current battery discharge rate in watt-hours, if available */ double dischargeRate(); @@ -1693,8 +1695,12 @@ inline TDESwitchType operator~(TDESwitchType a) } }; +class TQSocketNotifier; + class TDECORE_EXPORT TDEEventDevice : public TDEGenericDevice { + Q_OBJECT + public: /** * Constructor. @@ -1747,12 +1753,31 @@ class TDECORE_EXPORT TDEEventDevice : public TDEGenericDevice */ void internalSetActiveSwitches(TDESwitchType::TDESwitchType sl); + /** + * @param hwmanager the master hardware manager + * @internal + */ + void internalStartFdMonitoring(TDEHardwareDevices* hwmanager); + + protected slots: + void eventReceived(); + + signals: + /** + * @param keycode the code of the key that was pressed/released + * See include/linux/input.h for a complete list of keycodes + * @param device a TDEEventDevice* with the device that received the event + */ + void keyPressed(unsigned int keycode, TDEEventDevice* device); + private: TDEEventDeviceType::TDEEventDeviceType m_eventType; TDESwitchType::TDESwitchType m_providedSwitches; TDESwitchType::TDESwitchType m_switchActive; int m_fd; + bool m_fdMonitorActive; + TQSocketNotifier* m_eventNotifier; friend class TDEHardwareDevices; }; @@ -1802,7 +1827,6 @@ class TDECORE_EXPORT TDEInputDevice : public TDEGenericDevice typedef TQPtrList TDEGenericHardwareList; typedef TQMap TDEDeviceIDMap; -class TQSocketNotifier; class KSimpleDirWatch; class TDECORE_EXPORT TDEHardwareDevices : public TQObject @@ -1944,11 +1968,19 @@ class TDECORE_EXPORT TDEHardwareDevices : public TQObject void hardwareUpdated(TDEGenericDevice*); void mountTableModified(); + /** + * @param keycode the code of the key that was pressed/released + * See include/linux/input.h for a complete list of keycodes + * @param device a TDEEventDevice* with the device that received the event + */ + void eventDeviceKeyPressed(unsigned int keycode, TDEEventDevice* device); + private slots: void processHotPluggedHardware(); void processModifiedMounts(); void processModifiedCPUs(); void processStatelessDevices(); + void processEventDeviceKeyPressed(unsigned int keycode, TDEEventDevice* edevice); private: void updateBlacklists(TDEGenericDevice* hwdevice, udev_device* dev); -- cgit v1.2.1