• 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 (BIT_IS_SET(switches, SW_RADIO)) {
124  supportedSwitches = supportedSwitches | TDESwitchType::Radio;
125  }
126  if (BIT_IS_SET(switches, SW_MICROPHONE_INSERT)) {
127  supportedSwitches = supportedSwitches | TDESwitchType::MicrophoneInsert;
128  }
129  if (BIT_IS_SET(switches, SW_DOCK)) {
130  supportedSwitches = supportedSwitches | TDESwitchType::Dock;
131  }
132  if (BIT_IS_SET(switches, SW_LINEOUT_INSERT)) {
133  supportedSwitches = supportedSwitches | TDESwitchType::LineOutInsert;
134  }
135  if (BIT_IS_SET(switches, SW_JACK_PHYSICAL_INSERT)) {
136  supportedSwitches = supportedSwitches | TDESwitchType::JackPhysicalInsert;
137  }
138  if (BIT_IS_SET(switches, SW_VIDEOOUT_INSERT)) {
139  supportedSwitches = supportedSwitches | TDESwitchType::VideoOutInsert;
140  }
141 # ifdef SW_CAMERA_LENS_COVER
142  if (BIT_IS_SET(switches, SW_CAMERA_LENS_COVER)) {
143  supportedSwitches = supportedSwitches | TDESwitchType::CameraLensCover;
144  }
145 # endif
146 # ifdef SW_KEYPAD_SLIDE
147  if (BIT_IS_SET(switches, SW_KEYPAD_SLIDE)) {
148  supportedSwitches = supportedSwitches | TDESwitchType::KeypadSlide;
149  }
150 # endif
151 # ifdef SW_FRONT_PROXIMITY
152  if (BIT_IS_SET(switches, SW_FRONT_PROXIMITY)) {
153  supportedSwitches = supportedSwitches | TDESwitchType::FrontProximity;
154  }
155 # endif
156 # ifdef SW_ROTATE_LOCK
157  if (BIT_IS_SET(switches, SW_ROTATE_LOCK)) {
158  supportedSwitches = supportedSwitches | TDESwitchType::RotateLock;
159  }
160 # endif
161 # ifdef SW_LINEIN_INSERT
162  if (BIT_IS_SET(switches, SW_LINEIN_INSERT)) {
163  supportedSwitches = supportedSwitches | TDESwitchType::LineInInsert;
164  }
165 # endif
166  // Keep in sync with ACPI Event/Input identification routines above
167  if (systemPath().contains("PNP0C0D")) {
168  supportedSwitches = supportedSwitches | TDESwitchType::Lid;
169  }
170  if (systemPath().contains("PNP0C0E") || systemPath().contains("/LNXSLPBN")) {
171  supportedSwitches = supportedSwitches | TDESwitchType::SleepButton;
172  }
173  if (systemPath().contains("PNP0C0C") || systemPath().contains("/LNXPWRBN")) {
174  supportedSwitches = supportedSwitches | TDESwitchType::PowerButton;
175  }
176  }
177  m_providedSwitches = supportedSwitches;
178 }
179 
180 void TDEEventDevice::internalSetProvidedSwitches(TDESwitchType::TDESwitchType sl) {
181  m_providedSwitches = sl;
182 }
183 
184 TDESwitchType::TDESwitchType TDEEventDevice::activeSwitches() {
185  if (!m_monitorActive) {
186  internalReadActiveSwitches();
187  }
188  return m_switchActive;
189 }
190 
191 void TDEEventDevice::internalReadActiveSwitches() {
192  unsigned long switches[NUM_BITS(EV_CNT)];
193  int r = 0;
194 
195  TDESwitchType::TDESwitchType activeSwitches = TDESwitchType::Null;
196  if (m_fd >= 0) {
197  r = ioctl(m_fd, EVIOCGSW(sizeof(switches)), switches);
198  }
199 #ifdef WITH_TDEHWLIB_DAEMONS
200  if( r < 1 ) {
201  TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
202  if (dbusConn.isConnected()) {
203  TQT_DBusProxy switchesProxy("org.trinitydesktop.hardwarecontrol",
204  "/org/trinitydesktop/hardwarecontrol",
205  "org.trinitydesktop.hardwarecontrol.InputEvents",
206  dbusConn);
207  if (switchesProxy.canSend()) {
208  TQValueList<TQT_DBusData> params;
209  params << TQT_DBusData::fromString(deviceNode().ascii());
210  TQT_DBusMessage reply = switchesProxy.sendWithReply("GetActiveSwitches", params);
211  if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
212  TQValueList<TQ_UINT32> list = reply[0].toList().toUInt32List();
213  TQValueList<TQ_UINT32>::const_iterator it = list.begin();
214  for (r = 0; it != list.end(); ++it, r++) {
215  switches[r] = (*it);
216  }
217  }
218  }
219  }
220  }
221 #endif
222  if (r > 0) {
223  if (BIT_IS_SET(switches, SW_LID)) {
224  activeSwitches = activeSwitches | TDESwitchType::Lid;
225  }
226  if (BIT_IS_SET(switches, SW_TABLET_MODE)) {
227  activeSwitches = activeSwitches | TDESwitchType::TabletMode;
228  }
229  if (BIT_IS_SET(switches, SW_RFKILL_ALL)) {
230  activeSwitches = activeSwitches | TDESwitchType::RFKill;
231  }
232  if (BIT_IS_SET(switches, SW_RADIO)) {
233  activeSwitches = activeSwitches | TDESwitchType::Radio;
234  }
235  if (BIT_IS_SET(switches, SW_MICROPHONE_INSERT)) {
236  activeSwitches = activeSwitches | TDESwitchType::MicrophoneInsert;
237  }
238  if (BIT_IS_SET(switches, SW_DOCK)) {
239  activeSwitches = activeSwitches | TDESwitchType::Dock;
240  }
241  if (BIT_IS_SET(switches, SW_LINEOUT_INSERT)) {
242  activeSwitches = activeSwitches | TDESwitchType::LineOutInsert;
243  }
244  if (BIT_IS_SET(switches, SW_JACK_PHYSICAL_INSERT)) {
245  activeSwitches = activeSwitches | TDESwitchType::JackPhysicalInsert;
246  }
247  if (BIT_IS_SET(switches, SW_VIDEOOUT_INSERT)) {
248  activeSwitches = activeSwitches | TDESwitchType::VideoOutInsert;
249  }
250 # ifdef SW_CAMERA_LENS_COVER
251  if (BIT_IS_SET(switches, SW_CAMERA_LENS_COVER)) {
252  activeSwitches = activeSwitches | TDESwitchType::CameraLensCover;
253  }
254 # endif
255 # ifdef SW_KEYPAD_SLIDE
256  if (BIT_IS_SET(switches, SW_KEYPAD_SLIDE)) {
257  activeSwitches = activeSwitches | TDESwitchType::KeypadSlide;
258  }
259 # endif
260 # ifdef SW_FRONT_PROXIMITY
261  if (BIT_IS_SET(switches, SW_FRONT_PROXIMITY)) {
262  activeSwitches = activeSwitches | TDESwitchType::FrontProximity;
263  }
264 # endif
265 # ifdef SW_ROTATE_LOCK
266  if (BIT_IS_SET(switches, SW_ROTATE_LOCK)) {
267  activeSwitches = activeSwitches | TDESwitchType::RotateLock;
268  }
269 # endif
270 # ifdef SW_LINEIN_INSERT
271  if (BIT_IS_SET(switches, SW_LINEIN_INSERT)) {
272  activeSwitches = activeSwitches | TDESwitchType::LineInInsert;
273  }
274 # endif
275  }
276  m_switchActive = activeSwitches;
277 }
278 
279 void TDEEventDevice::internalSetActiveSwitches(TDESwitchType::TDESwitchType sl) {
280  m_switchActive = sl;
281 }
282 
283 // Keep this in sync with the TDESwitchType definition in the header
284 TQStringList TDEEventDevice::friendlySwitchList(TDESwitchType::TDESwitchType switches) {
285  TQStringList ret;
286 
287  if (switches & TDESwitchType::Lid) {
288  ret.append(i18n("Lid Switch"));
289  }
290  if (switches & TDESwitchType::TabletMode) {
291  ret.append(i18n("Tablet Mode"));
292  }
293  if (switches & TDESwitchType::HeadphoneInsert) {
294  ret.append(i18n("Headphone Inserted"));
295  }
296  if (switches & TDESwitchType::RFKill) {
297  ret.append(i18n("Radio Frequency Device Kill Switch"));
298  }
299  if (switches & TDESwitchType::Radio) {
300  ret.append(i18n("Enable Radio"));
301  }
302  if (switches & TDESwitchType::MicrophoneInsert) {
303  ret.append(i18n("Microphone Inserted"));
304  }
305  if (switches & TDESwitchType::Dock) {
306  ret.append(i18n("Docked"));
307  }
308  if (switches & TDESwitchType::LineOutInsert) {
309  ret.append(i18n("Line Out Inserted"));
310  }
311  if (switches & TDESwitchType::JackPhysicalInsert) {
312  ret.append(i18n("Physical Jack Inserted"));
313  }
314  if (switches & TDESwitchType::VideoOutInsert) {
315  ret.append(i18n("Video Out Inserted"));
316  }
317  if (switches & TDESwitchType::CameraLensCover) {
318  ret.append(i18n("Camera Lens Cover"));
319  }
320  if (switches & TDESwitchType::KeypadSlide) {
321  ret.append(i18n("Keypad Slide"));
322  }
323  if (switches & TDESwitchType::FrontProximity) {
324  ret.append(i18n("Front Proximity"));
325  }
326  if (switches & TDESwitchType::RotateLock) {
327  ret.append(i18n("Rotate Lock"));
328  }
329  if (switches & TDESwitchType::LineInInsert) {
330  ret.append(i18n("Line In Inserted"));
331  }
332  if (switches & TDESwitchType::PowerButton) {
333  ret.append(i18n("Power Button"));
334  }
335  if (switches & TDESwitchType::SleepButton) {
336  ret.append(i18n("Sleep Button"));
337  }
338 
339  return ret;
340 }
341 
342 void TDEEventDevice::internalStartMonitoring(TDEHardwareDevices* hwmanager) {
343  if (!m_monitorActive) {
344  // For security and performance reasons, only monitor known ACPI buttons
345  if (eventType() != TDEEventDeviceType::Unknown) {
346  if (m_fd >= 0) {
347  m_eventNotifier = new TQSocketNotifier(m_fd, TQSocketNotifier::Read, this);
348  connect( m_eventNotifier, TQT_SIGNAL(activated(int)), this, TQT_SLOT(eventReceived()) );
349  m_monitorActive = true;
350  }
351  }
352  if (m_monitorActive == true) {
353  // get initial state of switches
354  internalReadProvidedSwitches();
355  internalReadActiveSwitches();
356  connect( this, TQT_SIGNAL(keyPressed(unsigned int, TDEEventDevice*)), hwmanager, TQT_SLOT(processEventDeviceKeyPressed(unsigned int, TDEEventDevice*)) );
357  }
358  }
359 }
360 
361 void TDEEventDevice::eventReceived() {
362  struct input_event ev;
363  int r;
364  r = read(m_fd, &ev, sizeof(struct input_event));
365  if (r > 0) {
366  if ((ev.type == EV_KEY) && (ev.value == 1)) { // Only detect keypress events (value == 1)
367  emit keyPressed(ev.code, this);
368  }
369  if (ev.type == EV_SW) {
370  emit switchChanged();
371  }
372  }
373 }
374 
375 void TDEEventDevice::processActiveSwitches() {
376  TDESwitchType::TDESwitchType previousSwitches = m_switchActive;
377  internalReadActiveSwitches();
378 
379  if (previousSwitches != m_switchActive) {
380  emit switchChanged();
381  }
382 }
383 
384 void TDEEventDevice::connectNotify( const char* signal ) {
385  if( !m_monitorActive && qstrcmp( signal, TQT_SIGNAL(switchChanged())) == 0 ) {
386  m_watchTimer = new TQTimer(this);
387  connect( m_watchTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(processActiveSwitches()) );
388  m_watchTimer->start( 2500, FALSE );
389  m_monitorActive = true;
390 
391  // get initial state of switches
392  internalReadProvidedSwitches();
393  internalReadActiveSwitches();
394  }
395  TQObject::connectNotify( signal );
396 }
397 
398 #include "tdeeventdevice.moc"
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
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.8.11
This website is maintained by Timothy Pearson.