• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
  • tdehw
tdeeventdevice.cpp
1 /* This file is part of the TDE libraries
2  Copyright (C) 2012 Timothy Pearson <kb9vqf@pearsoncomputing.net>
3  (C) 2013 Golubev Alexander <fatzer2@gmail.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "tdeeventdevice.h"
21 
22 #include <unistd.h>
23 #include <linux/input.h>
24 
25 #include <tqsocketnotifier.h>
26 #include <tqtimer.h>
27 
28 #include "tdelocale.h"
29 
30 #include "tdehardwaredevices.h"
31 
32 #include "config.h"
33 
34 #define BITS_PER_LONG (sizeof(long) * 8)
35 #define NUM_BITS(x) ((((x) - 1) / BITS_PER_LONG) + 1)
36 #define OFF(x) ((x) % BITS_PER_LONG)
37 #define BIT(x) (1UL << OFF(x))
38 #define LONG(x) ((x) / BITS_PER_LONG)
39 #define BIT_IS_SET(array, bit) ((array[LONG(bit)] >> OFF(bit)) & 1)
40 
41 #if defined(WITH_TDEHWLIB_DAEMONS)
42 #include <tqdbusconnection.h>
43 #include <tqdbusproxy.h>
44 #include <tqdbusmessage.h>
45 #include <tqdbusvariant.h>
46 #include <tqdbusdata.h>
47 #include <tqdbusdatalist.h>
48 #endif
49 
50 TDEEventDevice::TDEEventDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
51  m_fd = -1;
52  m_watchTimer = 0;
53  m_monitorActive = false;
54 }
55 
56 TDEEventDevice::~TDEEventDevice() {
57  if (m_fd >= 0) {
58  close(m_fd);
59  }
60  if (m_watchTimer) {
61  m_watchTimer->stop();
62  delete m_watchTimer;
63  }
64 }
65 
66 TDEEventDeviceType::TDEEventDeviceType TDEEventDevice::eventType() {
67  return m_eventType;
68 }
69 
70 void TDEEventDevice::internalSetEventType(TDEEventDeviceType::TDEEventDeviceType et) {
71  m_eventType = et;
72 }
73 
74 TDESwitchType::TDESwitchType TDEEventDevice::providedSwitches() {
75  if (!m_monitorActive) {
76  internalReadProvidedSwitches();
77  }
78  return m_providedSwitches;
79 }
80 
81 void TDEEventDevice::internalReadProvidedSwitches() {
82  unsigned long switches[NUM_BITS(EV_CNT)];
83  int r = 0;
84 
85  // Figure out which switch types are supported, if any
86  TDESwitchType::TDESwitchType supportedSwitches = TDESwitchType::Null;
87  if (m_fd >= 0) {
88  r = ioctl(m_fd, EVIOCGBIT(EV_SW, EV_CNT), switches);
89  }
90 #ifdef WITH_TDEHWLIB_DAEMONS
91  if( r < 1 ) {
92  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
93  if (dbusConn.isConnected()) {
94  TQT_DBusProxy switchesProxy("org.trinitydesktop.hardwarecontrol",
95  "/org/trinitydesktop/hardwarecontrol",
96  "org.trinitydesktop.hardwarecontrol.InputEvents",
97  dbusConn);
98  if (switchesProxy.canSend()) {
99  TQValueList<TQT_DBusData> params;
100  params << TQT_DBusData::fromString(deviceNode().ascii());
101  TQT_DBusMessage reply = switchesProxy.sendWithReply("GetProvidedSwitches", params);
102  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
103  TQValueList<TQ_UINT32> list = reply[0].toList().toUInt32List();
104  TQValueList<TQ_UINT32>::const_iterator it = list.begin();
105  for (r = 0; it != list.end(); ++it, r++) {
106  switches[r] = (*it);
107  }
108  }
109  }
110  }
111  }
112 #endif
113  if (r > 0) {
114  if (BIT_IS_SET(switches, SW_LID)) {
115  supportedSwitches = supportedSwitches | TDESwitchType::Lid;
116  }
117  if (BIT_IS_SET(switches, SW_TABLET_MODE)) {
118  supportedSwitches = supportedSwitches | TDESwitchType::TabletMode;
119  }
120  if (BIT_IS_SET(switches, SW_RFKILL_ALL)) {
121  supportedSwitches = supportedSwitches | TDESwitchType::RFKill;
122  }
123 # if SW_RADIO != SW_RFKILL_ALL // SW_RADIO is a deprecated name for SW_RFKILL_ALL
124  if (BIT_IS_SET(switches, SW_RADIO)) {
125  supportedSwitches = supportedSwitches | TDESwitchType::Radio;
126  }
127 # endif
128  if (BIT_IS_SET(switches, SW_MICROPHONE_INSERT)) {
129  supportedSwitches = supportedSwitches | TDESwitchType::MicrophoneInsert;
130  }
131  if (BIT_IS_SET(switches, SW_DOCK)) {
132  supportedSwitches = supportedSwitches | TDESwitchType::Dock;
133  }
134  if (BIT_IS_SET(switches, SW_LINEOUT_INSERT)) {
135  supportedSwitches = supportedSwitches | TDESwitchType::LineOutInsert;
136  }
137  if (BIT_IS_SET(switches, SW_JACK_PHYSICAL_INSERT)) {
138  supportedSwitches = supportedSwitches | TDESwitchType::JackPhysicalInsert;
139  }
140  if (BIT_IS_SET(switches, SW_VIDEOOUT_INSERT)) {
141  supportedSwitches = supportedSwitches | TDESwitchType::VideoOutInsert;
142  }
143 # ifdef SW_CAMERA_LENS_COVER
144  if (BIT_IS_SET(switches, SW_CAMERA_LENS_COVER)) {
145  supportedSwitches = supportedSwitches | TDESwitchType::CameraLensCover;
146  }
147 # endif
148 # ifdef SW_KEYPAD_SLIDE
149  if (BIT_IS_SET(switches, SW_KEYPAD_SLIDE)) {
150  supportedSwitches = supportedSwitches | TDESwitchType::KeypadSlide;
151  }
152 # endif
153 # ifdef SW_FRONT_PROXIMITY
154  if (BIT_IS_SET(switches, SW_FRONT_PROXIMITY)) {
155  supportedSwitches = supportedSwitches | TDESwitchType::FrontProximity;
156  }
157 # endif
158 # ifdef SW_ROTATE_LOCK
159  if (BIT_IS_SET(switches, SW_ROTATE_LOCK)) {
160  supportedSwitches = supportedSwitches | TDESwitchType::RotateLock;
161  }
162 # endif
163 # ifdef SW_LINEIN_INSERT
164  if (BIT_IS_SET(switches, SW_LINEIN_INSERT)) {
165  supportedSwitches = supportedSwitches | TDESwitchType::LineInInsert;
166  }
167 # endif
168  // Keep in sync with ACPI Event/Input identification routines above
169  if (systemPath().contains("PNP0C0D")) {
170  supportedSwitches = supportedSwitches | TDESwitchType::Lid;
171  }
172  if (systemPath().contains("PNP0C0E") || systemPath().contains("/LNXSLPBN")) {
173  supportedSwitches = supportedSwitches | TDESwitchType::SleepButton;
174  }
175  if (systemPath().contains("PNP0C0C") || systemPath().contains("/LNXPWRBN")) {
176  supportedSwitches = supportedSwitches | TDESwitchType::PowerButton;
177  }
178  }
179  m_providedSwitches = supportedSwitches;
180 }
181 
182 void TDEEventDevice::internalSetProvidedSwitches(TDESwitchType::TDESwitchType sl) {
183  m_providedSwitches = sl;
184 }
185 
186 TDESwitchType::TDESwitchType TDEEventDevice::activeSwitches() {
187  if (!m_monitorActive) {
188  internalReadActiveSwitches();
189  }
190  return m_switchActive;
191 }
192 
193 void TDEEventDevice::internalReadActiveSwitches() {
194  unsigned long switches[NUM_BITS(EV_CNT)];
195  int r = 0;
196 
197  TDESwitchType::TDESwitchType activeSwitches = TDESwitchType::Null;
198  if (m_fd >= 0) {
199  r = ioctl(m_fd, EVIOCGSW(sizeof(switches)), switches);
200  }
201 #ifdef WITH_TDEHWLIB_DAEMONS
202  if( r < 1 ) {
203  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
204  if (dbusConn.isConnected()) {
205  TQT_DBusProxy switchesProxy("org.trinitydesktop.hardwarecontrol",
206  "/org/trinitydesktop/hardwarecontrol",
207  "org.trinitydesktop.hardwarecontrol.InputEvents",
208  dbusConn);
209  if (switchesProxy.canSend()) {
210  TQValueList<TQT_DBusData> params;
211  params << TQT_DBusData::fromString(deviceNode().ascii());
212  TQT_DBusMessage reply = switchesProxy.sendWithReply("GetActiveSwitches", params);
213  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
214  TQValueList<TQ_UINT32> list = reply[0].toList().toUInt32List();
215  TQValueList<TQ_UINT32>::const_iterator it = list.begin();
216  for (r = 0; it != list.end(); ++it, r++) {
217  switches[r] = (*it);
218  }
219  }
220  }
221  }
222  }
223 #endif
224  if (r > 0) {
225  if (BIT_IS_SET(switches, SW_LID)) {
226  activeSwitches = activeSwitches | TDESwitchType::Lid;
227  }
228  if (BIT_IS_SET(switches, SW_TABLET_MODE)) {
229  activeSwitches = activeSwitches | TDESwitchType::TabletMode;
230  }
231  if (BIT_IS_SET(switches, SW_RFKILL_ALL)) {
232  activeSwitches = activeSwitches | TDESwitchType::RFKill;
233  }
234 # if SW_RADIO != SW_RFKILL_ALL // SW_RADIO is a deprecated name for SW_RFKILL_ALL
235  if (BIT_IS_SET(switches, SW_RADIO)) {
236  activeSwitches = activeSwitches | TDESwitchType::Radio;
237  }
238 # endif
239  if (BIT_IS_SET(switches, SW_MICROPHONE_INSERT)) {
240  activeSwitches = activeSwitches | TDESwitchType::MicrophoneInsert;
241  }
242  if (BIT_IS_SET(switches, SW_DOCK)) {
243  activeSwitches = activeSwitches | TDESwitchType::Dock;
244  }
245  if (BIT_IS_SET(switches, SW_LINEOUT_INSERT)) {
246  activeSwitches = activeSwitches | TDESwitchType::LineOutInsert;
247  }
248  if (BIT_IS_SET(switches, SW_JACK_PHYSICAL_INSERT)) {
249  activeSwitches = activeSwitches | TDESwitchType::JackPhysicalInsert;
250  }
251  if (BIT_IS_SET(switches, SW_VIDEOOUT_INSERT)) {
252  activeSwitches = activeSwitches | TDESwitchType::VideoOutInsert;
253  }
254 # ifdef SW_CAMERA_LENS_COVER
255  if (BIT_IS_SET(switches, SW_CAMERA_LENS_COVER)) {
256  activeSwitches = activeSwitches | TDESwitchType::CameraLensCover;
257  }
258 # endif
259 # ifdef SW_KEYPAD_SLIDE
260  if (BIT_IS_SET(switches, SW_KEYPAD_SLIDE)) {
261  activeSwitches = activeSwitches | TDESwitchType::KeypadSlide;
262  }
263 # endif
264 # ifdef SW_FRONT_PROXIMITY
265  if (BIT_IS_SET(switches, SW_FRONT_PROXIMITY)) {
266  activeSwitches = activeSwitches | TDESwitchType::FrontProximity;
267  }
268 # endif
269 # ifdef SW_ROTATE_LOCK
270  if (BIT_IS_SET(switches, SW_ROTATE_LOCK)) {
271  activeSwitches = activeSwitches | TDESwitchType::RotateLock;
272  }
273 # endif
274 # ifdef SW_LINEIN_INSERT
275  if (BIT_IS_SET(switches, SW_LINEIN_INSERT)) {
276  activeSwitches = activeSwitches | TDESwitchType::LineInInsert;
277  }
278 # endif
279  }
280  m_switchActive = activeSwitches;
281 }
282 
283 void TDEEventDevice::internalSetActiveSwitches(TDESwitchType::TDESwitchType sl) {
284  m_switchActive = sl;
285 }
286 
287 // Keep this in sync with the TDESwitchType definition in the header
288 TQStringList TDEEventDevice::friendlySwitchList(TDESwitchType::TDESwitchType switches) {
289  TQStringList ret;
290 
291  if (switches & TDESwitchType::Lid) {
292  ret.append(i18n("Lid Switch"));
293  }
294  if (switches & TDESwitchType::TabletMode) {
295  ret.append(i18n("Tablet Mode"));
296  }
297  if (switches & TDESwitchType::HeadphoneInsert) {
298  ret.append(i18n("Headphone Inserted"));
299  }
300  if (switches & TDESwitchType::RFKill) {
301  ret.append(i18n("Radio Frequency Device Kill Switch"));
302  }
303  if (switches & TDESwitchType::Radio) {
304  ret.append(i18n("Enable Radio"));
305  }
306  if (switches & TDESwitchType::MicrophoneInsert) {
307  ret.append(i18n("Microphone Inserted"));
308  }
309  if (switches & TDESwitchType::Dock) {
310  ret.append(i18n("Docked"));
311  }
312  if (switches & TDESwitchType::LineOutInsert) {
313  ret.append(i18n("Line Out Inserted"));
314  }
315  if (switches & TDESwitchType::JackPhysicalInsert) {
316  ret.append(i18n("Physical Jack Inserted"));
317  }
318  if (switches & TDESwitchType::VideoOutInsert) {
319  ret.append(i18n("Video Out Inserted"));
320  }
321  if (switches & TDESwitchType::CameraLensCover) {
322  ret.append(i18n("Camera Lens Cover"));
323  }
324  if (switches & TDESwitchType::KeypadSlide) {
325  ret.append(i18n("Keypad Slide"));
326  }
327  if (switches & TDESwitchType::FrontProximity) {
328  ret.append(i18n("Front Proximity"));
329  }
330  if (switches & TDESwitchType::RotateLock) {
331  ret.append(i18n("Rotate Lock"));
332  }
333  if (switches & TDESwitchType::LineInInsert) {
334  ret.append(i18n("Line In Inserted"));
335  }
336  if (switches & TDESwitchType::PowerButton) {
337  ret.append(i18n("Power Button"));
338  }
339  if (switches & TDESwitchType::SleepButton) {
340  ret.append(i18n("Sleep Button"));
341  }
342 
343  return ret;
344 }
345 
346 void TDEEventDevice::internalStartMonitoring(TDEHardwareDevices* hwmanager) {
347  if (!m_monitorActive) {
348  // For security and performance reasons, only monitor known ACPI buttons
349  if (eventType() != TDEEventDeviceType::Unknown) {
350  if (m_fd >= 0) {
351  m_eventNotifier = new TQSocketNotifier(m_fd, TQSocketNotifier::Read, this);
352  connect( m_eventNotifier, TQT_SIGNAL(activated(int)), this, TQT_SLOT(eventReceived()) );
353  m_monitorActive = true;
354  }
355  }
356  if (m_monitorActive == true) {
357  // get initial state of switches
358  internalReadProvidedSwitches();
359  internalReadActiveSwitches();
360  connect( this, TQT_SIGNAL(keyPressed(unsigned int, TDEEventDevice*)), hwmanager, TQT_SLOT(processEventDeviceKeyPressed(unsigned int, TDEEventDevice*)) );
361  }
362  }
363 }
364 
365 void TDEEventDevice::eventReceived() {
366  struct input_event ev;
367  int r;
368  r = read(m_fd, &ev, sizeof(struct input_event));
369  if (r > 0) {
370  if ((ev.type == EV_KEY) && (ev.value == 1)) { // Only detect keypress events (value == 1)
371  emit keyPressed(ev.code, this);
372  }
373  if (ev.type == EV_SW) {
374  internalReadActiveSwitches();
375  emit switchChanged();
376  }
377  }
378 }
379 
380 void TDEEventDevice::processActiveSwitches() {
381  TDESwitchType::TDESwitchType previousSwitches = m_switchActive;
382  internalReadActiveSwitches();
383 
384  if (previousSwitches != m_switchActive) {
385  emit switchChanged();
386  }
387 }
388 
389 void TDEEventDevice::connectNotify( const char* signal ) {
390  if( !m_monitorActive && qstrcmp( signal, TQT_SIGNAL(switchChanged())) == 0 ) {
391  m_watchTimer = new TQTimer(this);
392  connect( m_watchTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(processActiveSwitches()) );
393  m_watchTimer->start( 2500, FALSE );
394  m_monitorActive = true;
395 
396  // get initial state of switches
397  internalReadProvidedSwitches();
398  internalReadActiveSwitches();
399  }
400  TQObject::connectNotify( signal );
401 }
402 
403 #include "tdeeventdevice.moc"
TDELocale::i18n
TQString i18n(const char *text)
Definition: tdelocale.cpp:1976
tdelocale.h
KStdAction::close
TDEAction * close(const TQObject *recvr, const char *slot, TDEActionCollection *parent, const char *name=0)

tdecore

Skip menu "tdecore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.8
This website is maintained by Timothy Pearson.