tdeeventdevice.cpp
00001 /* This file is part of the TDE libraries 00002 Copyright (C) 2012 Timothy Pearson <kb9vqf@pearsoncomputing.net> 00003 (C) 2013 Golubev Alexander <fatzer2@gmail.com> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2 as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "tdeeventdevice.h" 00021 00022 #include <unistd.h> 00023 #include <linux/input.h> 00024 00025 #include <tqsocketnotifier.h> 00026 #include <tqtimer.h> 00027 00028 #include "tdelocale.h" 00029 00030 #include "tdehardwaredevices.h" 00031 00032 #include "config.h" 00033 00034 #define BITS_PER_LONG (sizeof(long) * 8) 00035 #define NUM_BITS(x) ((((x) - 1) / BITS_PER_LONG) + 1) 00036 #define OFF(x) ((x) % BITS_PER_LONG) 00037 #define BIT(x) (1UL << OFF(x)) 00038 #define LONG(x) ((x) / BITS_PER_LONG) 00039 #define BIT_IS_SET(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1) 00040 00041 #if defined(WITH_TDEHWLIB_DAEMONS) 00042 #include <tqdbusconnection.h> 00043 #include <tqdbusproxy.h> 00044 #include <tqdbusmessage.h> 00045 #include <tqdbusvariant.h> 00046 #include <tqdbusdata.h> 00047 #include <tqdbusdatalist.h> 00048 #endif 00049 00050 TDEEventDevice::TDEEventDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) { 00051 m_fd = -1; 00052 m_watchTimer = 0; 00053 m_monitorActive = false; 00054 } 00055 00056 TDEEventDevice::~TDEEventDevice() { 00057 if (m_fd >= 0) { 00058 close(m_fd); 00059 } 00060 if (m_watchTimer) { 00061 m_watchTimer->stop(); 00062 delete m_watchTimer; 00063 } 00064 } 00065 00066 TDEEventDeviceType::TDEEventDeviceType TDEEventDevice::eventType() { 00067 return m_eventType; 00068 } 00069 00070 void TDEEventDevice::internalSetEventType(TDEEventDeviceType::TDEEventDeviceType et) { 00071 m_eventType = et; 00072 } 00073 00074 TDESwitchType::TDESwitchType TDEEventDevice::providedSwitches() { 00075 if (!m_monitorActive) { 00076 internalReadProvidedSwitches(); 00077 } 00078 return m_providedSwitches; 00079 } 00080 00081 void TDEEventDevice::internalReadProvidedSwitches() { 00082 unsigned long switches[NUM_BITS(EV_CNT)]; 00083 int r = 0; 00084 00085 // Figure out which switch types are supported, if any 00086 TDESwitchType::TDESwitchType supportedSwitches = TDESwitchType::Null; 00087 if (m_fd >= 0) { 00088 r = ioctl(m_fd, EVIOCGBIT(EV_SW, EV_CNT), switches); 00089 } 00090 #ifdef WITH_TDEHWLIB_DAEMONS 00091 if( r < 1 ) { 00092 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); 00093 if (dbusConn.isConnected()) { 00094 TQT_DBusProxy switchesProxy("org.trinitydesktop.hardwarecontrol", 00095 "/org/trinitydesktop/hardwarecontrol", 00096 "org.trinitydesktop.hardwarecontrol.InputEvents", 00097 dbusConn); 00098 if (switchesProxy.canSend()) { 00099 TQValueList<TQT_DBusData> params; 00100 params << TQT_DBusData::fromString(deviceNode().ascii()); 00101 TQT_DBusMessage reply = switchesProxy.sendWithReply("GetProvidedSwitches", params); 00102 if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) { 00103 TQValueList<TQ_UINT32> list = reply[0].toList().toUInt32List(); 00104 TQValueList<TQ_UINT32>::const_iterator it = list.begin(); 00105 for (r = 0; it != list.end(); ++it, r++) { 00106 switches[r] = (*it); 00107 } 00108 } 00109 } 00110 } 00111 } 00112 #endif 00113 if (r > 0) { 00114 if (BIT_IS_SET(switches, SW_LID)) { 00115 supportedSwitches = supportedSwitches | TDESwitchType::Lid; 00116 } 00117 if (BIT_IS_SET(switches, SW_TABLET_MODE)) { 00118 supportedSwitches = supportedSwitches | TDESwitchType::TabletMode; 00119 } 00120 if (BIT_IS_SET(switches, SW_RFKILL_ALL)) { 00121 supportedSwitches = supportedSwitches | TDESwitchType::RFKill; 00122 } 00123 if (BIT_IS_SET(switches, SW_RADIO)) { 00124 supportedSwitches = supportedSwitches | TDESwitchType::Radio; 00125 } 00126 if (BIT_IS_SET(switches, SW_MICROPHONE_INSERT)) { 00127 supportedSwitches = supportedSwitches | TDESwitchType::MicrophoneInsert; 00128 } 00129 if (BIT_IS_SET(switches, SW_DOCK)) { 00130 supportedSwitches = supportedSwitches | TDESwitchType::Dock; 00131 } 00132 if (BIT_IS_SET(switches, SW_LINEOUT_INSERT)) { 00133 supportedSwitches = supportedSwitches | TDESwitchType::LineOutInsert; 00134 } 00135 if (BIT_IS_SET(switches, SW_JACK_PHYSICAL_INSERT)) { 00136 supportedSwitches = supportedSwitches | TDESwitchType::JackPhysicalInsert; 00137 } 00138 if (BIT_IS_SET(switches, SW_VIDEOOUT_INSERT)) { 00139 supportedSwitches = supportedSwitches | TDESwitchType::VideoOutInsert; 00140 } 00141 # ifdef SW_CAMERA_LENS_COVER 00142 if (BIT_IS_SET(switches, SW_CAMERA_LENS_COVER)) { 00143 supportedSwitches = supportedSwitches | TDESwitchType::CameraLensCover; 00144 } 00145 # endif 00146 # ifdef SW_KEYPAD_SLIDE 00147 if (BIT_IS_SET(switches, SW_KEYPAD_SLIDE)) { 00148 supportedSwitches = supportedSwitches | TDESwitchType::KeypadSlide; 00149 } 00150 # endif 00151 # ifdef SW_FRONT_PROXIMITY 00152 if (BIT_IS_SET(switches, SW_FRONT_PROXIMITY)) { 00153 supportedSwitches = supportedSwitches | TDESwitchType::FrontProximity; 00154 } 00155 # endif 00156 # ifdef SW_ROTATE_LOCK 00157 if (BIT_IS_SET(switches, SW_ROTATE_LOCK)) { 00158 supportedSwitches = supportedSwitches | TDESwitchType::RotateLock; 00159 } 00160 # endif 00161 # ifdef SW_LINEIN_INSERT 00162 if (BIT_IS_SET(switches, SW_LINEIN_INSERT)) { 00163 supportedSwitches = supportedSwitches | TDESwitchType::LineInInsert; 00164 } 00165 # endif 00166 // Keep in sync with ACPI Event/Input identification routines above 00167 if (systemPath().contains("PNP0C0D")) { 00168 supportedSwitches = supportedSwitches | TDESwitchType::Lid; 00169 } 00170 if (systemPath().contains("PNP0C0E") || systemPath().contains("/LNXSLPBN")) { 00171 supportedSwitches = supportedSwitches | TDESwitchType::SleepButton; 00172 } 00173 if (systemPath().contains("PNP0C0C") || systemPath().contains("/LNXPWRBN")) { 00174 supportedSwitches = supportedSwitches | TDESwitchType::PowerButton; 00175 } 00176 } 00177 m_providedSwitches = supportedSwitches; 00178 } 00179 00180 void TDEEventDevice::internalSetProvidedSwitches(TDESwitchType::TDESwitchType sl) { 00181 m_providedSwitches = sl; 00182 } 00183 00184 TDESwitchType::TDESwitchType TDEEventDevice::activeSwitches() { 00185 if (!m_monitorActive) { 00186 internalReadActiveSwitches(); 00187 } 00188 return m_switchActive; 00189 } 00190 00191 void TDEEventDevice::internalReadActiveSwitches() { 00192 unsigned long switches[NUM_BITS(EV_CNT)]; 00193 int r = 0; 00194 00195 TDESwitchType::TDESwitchType activeSwitches = TDESwitchType::Null; 00196 if (m_fd >= 0) { 00197 r = ioctl(m_fd, EVIOCGSW(sizeof(switches)), switches); 00198 } 00199 #ifdef WITH_TDEHWLIB_DAEMONS 00200 if( r < 1 ) { 00201 TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus); 00202 if (dbusConn.isConnected()) { 00203 TQT_DBusProxy switchesProxy("org.trinitydesktop.hardwarecontrol", 00204 "/org/trinitydesktop/hardwarecontrol", 00205 "org.trinitydesktop.hardwarecontrol.InputEvents", 00206 dbusConn); 00207 if (switchesProxy.canSend()) { 00208 TQValueList<TQT_DBusData> params; 00209 params << TQT_DBusData::fromString(deviceNode().ascii()); 00210 TQT_DBusMessage reply = switchesProxy.sendWithReply("GetActiveSwitches", params); 00211 if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) { 00212 TQValueList<TQ_UINT32> list = reply[0].toList().toUInt32List(); 00213 TQValueList<TQ_UINT32>::const_iterator it = list.begin(); 00214 for (r = 0; it != list.end(); ++it, r++) { 00215 switches[r] = (*it); 00216 } 00217 } 00218 } 00219 } 00220 } 00221 #endif 00222 if (r > 0) { 00223 if (BIT_IS_SET(switches, SW_LID)) { 00224 activeSwitches = activeSwitches | TDESwitchType::Lid; 00225 } 00226 if (BIT_IS_SET(switches, SW_TABLET_MODE)) { 00227 activeSwitches = activeSwitches | TDESwitchType::TabletMode; 00228 } 00229 if (BIT_IS_SET(switches, SW_RFKILL_ALL)) { 00230 activeSwitches = activeSwitches | TDESwitchType::RFKill; 00231 } 00232 if (BIT_IS_SET(switches, SW_RADIO)) { 00233 activeSwitches = activeSwitches | TDESwitchType::Radio; 00234 } 00235 if (BIT_IS_SET(switches, SW_MICROPHONE_INSERT)) { 00236 activeSwitches = activeSwitches | TDESwitchType::MicrophoneInsert; 00237 } 00238 if (BIT_IS_SET(switches, SW_DOCK)) { 00239 activeSwitches = activeSwitches | TDESwitchType::Dock; 00240 } 00241 if (BIT_IS_SET(switches, SW_LINEOUT_INSERT)) { 00242 activeSwitches = activeSwitches | TDESwitchType::LineOutInsert; 00243 } 00244 if (BIT_IS_SET(switches, SW_JACK_PHYSICAL_INSERT)) { 00245 activeSwitches = activeSwitches | TDESwitchType::JackPhysicalInsert; 00246 } 00247 if (BIT_IS_SET(switches, SW_VIDEOOUT_INSERT)) { 00248 activeSwitches = activeSwitches | TDESwitchType::VideoOutInsert; 00249 } 00250 # ifdef SW_CAMERA_LENS_COVER 00251 if (BIT_IS_SET(switches, SW_CAMERA_LENS_COVER)) { 00252 activeSwitches = activeSwitches | TDESwitchType::CameraLensCover; 00253 } 00254 # endif 00255 # ifdef SW_KEYPAD_SLIDE 00256 if (BIT_IS_SET(switches, SW_KEYPAD_SLIDE)) { 00257 activeSwitches = activeSwitches | TDESwitchType::KeypadSlide; 00258 } 00259 # endif 00260 # ifdef SW_FRONT_PROXIMITY 00261 if (BIT_IS_SET(switches, SW_FRONT_PROXIMITY)) { 00262 activeSwitches = activeSwitches | TDESwitchType::FrontProximity; 00263 } 00264 # endif 00265 # ifdef SW_ROTATE_LOCK 00266 if (BIT_IS_SET(switches, SW_ROTATE_LOCK)) { 00267 activeSwitches = activeSwitches | TDESwitchType::RotateLock; 00268 } 00269 # endif 00270 # ifdef SW_LINEIN_INSERT 00271 if (BIT_IS_SET(switches, SW_LINEIN_INSERT)) { 00272 activeSwitches = activeSwitches | TDESwitchType::LineInInsert; 00273 } 00274 # endif 00275 } 00276 m_switchActive = activeSwitches; 00277 } 00278 00279 void TDEEventDevice::internalSetActiveSwitches(TDESwitchType::TDESwitchType sl) { 00280 m_switchActive = sl; 00281 } 00282 00283 // Keep this in sync with the TDESwitchType definition in the header 00284 TQStringList TDEEventDevice::friendlySwitchList(TDESwitchType::TDESwitchType switches) { 00285 TQStringList ret; 00286 00287 if (switches & TDESwitchType::Lid) { 00288 ret.append(i18n("Lid Switch")); 00289 } 00290 if (switches & TDESwitchType::TabletMode) { 00291 ret.append(i18n("Tablet Mode")); 00292 } 00293 if (switches & TDESwitchType::HeadphoneInsert) { 00294 ret.append(i18n("Headphone Inserted")); 00295 } 00296 if (switches & TDESwitchType::RFKill) { 00297 ret.append(i18n("Radio Frequency Device Kill Switch")); 00298 } 00299 if (switches & TDESwitchType::Radio) { 00300 ret.append(i18n("Enable Radio")); 00301 } 00302 if (switches & TDESwitchType::MicrophoneInsert) { 00303 ret.append(i18n("Microphone Inserted")); 00304 } 00305 if (switches & TDESwitchType::Dock) { 00306 ret.append(i18n("Docked")); 00307 } 00308 if (switches & TDESwitchType::LineOutInsert) { 00309 ret.append(i18n("Line Out Inserted")); 00310 } 00311 if (switches & TDESwitchType::JackPhysicalInsert) { 00312 ret.append(i18n("Physical Jack Inserted")); 00313 } 00314 if (switches & TDESwitchType::VideoOutInsert) { 00315 ret.append(i18n("Video Out Inserted")); 00316 } 00317 if (switches & TDESwitchType::CameraLensCover) { 00318 ret.append(i18n("Camera Lens Cover")); 00319 } 00320 if (switches & TDESwitchType::KeypadSlide) { 00321 ret.append(i18n("Keypad Slide")); 00322 } 00323 if (switches & TDESwitchType::FrontProximity) { 00324 ret.append(i18n("Front Proximity")); 00325 } 00326 if (switches & TDESwitchType::RotateLock) { 00327 ret.append(i18n("Rotate Lock")); 00328 } 00329 if (switches & TDESwitchType::LineInInsert) { 00330 ret.append(i18n("Line In Inserted")); 00331 } 00332 if (switches & TDESwitchType::PowerButton) { 00333 ret.append(i18n("Power Button")); 00334 } 00335 if (switches & TDESwitchType::SleepButton) { 00336 ret.append(i18n("Sleep Button")); 00337 } 00338 00339 return ret; 00340 } 00341 00342 void TDEEventDevice::internalStartMonitoring(TDEHardwareDevices* hwmanager) { 00343 if (!m_monitorActive) { 00344 // For security and performance reasons, only monitor known ACPI buttons 00345 if (eventType() != TDEEventDeviceType::Unknown) { 00346 if (m_fd >= 0) { 00347 m_eventNotifier = new TQSocketNotifier(m_fd, TQSocketNotifier::Read, this); 00348 connect( m_eventNotifier, TQT_SIGNAL(activated(int)), this, TQT_SLOT(eventReceived()) ); 00349 m_monitorActive = true; 00350 } 00351 } 00352 if (m_monitorActive == true) { 00353 // get initial state of switches 00354 internalReadProvidedSwitches(); 00355 internalReadActiveSwitches(); 00356 connect( this, TQT_SIGNAL(keyPressed(unsigned int, TDEEventDevice*)), hwmanager, TQT_SLOT(processEventDeviceKeyPressed(unsigned int, TDEEventDevice*)) ); 00357 } 00358 } 00359 } 00360 00361 void TDEEventDevice::eventReceived() { 00362 struct input_event ev; 00363 int r; 00364 r = read(m_fd, &ev, sizeof(struct input_event)); 00365 if (r > 0) { 00366 if ((ev.type == EV_KEY) && (ev.value == 1)) { // Only detect keypress events (value == 1) 00367 emit keyPressed(ev.code, this); 00368 } 00369 if (ev.type == EV_SW) { 00370 internalReadActiveSwitches(); 00371 emit switchChanged(); 00372 } 00373 } 00374 } 00375 00376 void TDEEventDevice::processActiveSwitches() { 00377 TDESwitchType::TDESwitchType previousSwitches = m_switchActive; 00378 internalReadActiveSwitches(); 00379 00380 if (previousSwitches != m_switchActive) { 00381 emit switchChanged(); 00382 } 00383 } 00384 00385 void TDEEventDevice::connectNotify( const char* signal ) { 00386 if( !m_monitorActive && qstrcmp( signal, TQT_SIGNAL(switchChanged())) == 0 ) { 00387 m_watchTimer = new TQTimer(this); 00388 connect( m_watchTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(processActiveSwitches()) ); 00389 m_watchTimer->start( 2500, FALSE ); 00390 m_monitorActive = true; 00391 00392 // get initial state of switches 00393 internalReadProvidedSwitches(); 00394 internalReadActiveSwitches(); 00395 } 00396 TQObject::connectNotify( signal ); 00397 } 00398 00399 #include "tdeeventdevice.moc"