summaryrefslogtreecommitdiffstats
path: root/tdecore/tdehw/tdeeventdevice.cpp
blob: 88509b3aca2a63b9b4a7d22741b0353da48993b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* This file is part of the TDE libraries
   Copyright (C) 2012 Timothy Pearson <kb9vqf@pearsoncomputing.net>
             (C) 2013 Golubev Alexander <fatzer2@gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "tdeeventdevice.h"

#include <unistd.h>
#include <linux/input.h>

#include <tqsocketnotifier.h>
#include <tqtimer.h>

#include "tdelocale.h"

#include "tdehardwaredevices.h"

#include "config.h"

#define BITS_PER_LONG (sizeof(long) * 8)
#define NUM_BITS(x) ((((x) - 1) / BITS_PER_LONG) + 1)
#define OFF(x)  ((x) % BITS_PER_LONG)
#define BIT(x)  (1UL << OFF(x))
#define LONG(x) ((x) / BITS_PER_LONG)
#define BIT_IS_SET(array, bit)  ((array[LONG(bit)] >> OFF(bit)) & 1)

#if defined(WITH_TDEHWLIB_DAEMONS)
#include <tqdbusconnection.h>
#include <tqdbusproxy.h>
#include <tqdbusmessage.h>
#include <tqdbusvariant.h>
#include <tqdbusdata.h>
#include <tqdbusdatalist.h>
#endif

TDEEventDevice::TDEEventDevice(TDEGenericDeviceType::TDEGenericDeviceType dt, TQString dn) : TDEGenericDevice(dt, dn) {
	m_fd = -1;
	m_watchTimer = 0;
	m_monitorActive = false;
}

TDEEventDevice::~TDEEventDevice() {
	if (m_fd >= 0) {
		close(m_fd);
	}
	if (m_watchTimer) {
		m_watchTimer->stop();
		delete m_watchTimer;
	}
}

TDEEventDeviceType::TDEEventDeviceType TDEEventDevice::eventType() {
	return m_eventType;
}

void TDEEventDevice::internalSetEventType(TDEEventDeviceType::TDEEventDeviceType et) {
	m_eventType = et;
}

TDESwitchType::TDESwitchType TDEEventDevice::providedSwitches() {
	if (!m_monitorActive) {
		internalReadProvidedSwitches();
	}
	return m_providedSwitches;
}

void TDEEventDevice::internalReadProvidedSwitches() {
	unsigned long switches[NUM_BITS(EV_CNT)];
	int r = 0;

	// Figure out which switch types are supported, if any
	TDESwitchType::TDESwitchType supportedSwitches = TDESwitchType::Null;
	if (m_fd >= 0) {
		r = ioctl(m_fd, EVIOCGBIT(EV_SW, EV_CNT), switches);
	}
#ifdef WITH_TDEHWLIB_DAEMONS
	if( r < 1 ) {
		TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
		if (dbusConn.isConnected()) {
			TQT_DBusProxy switchesProxy("org.trinitydesktop.hardwarecontrol",
				"/org/trinitydesktop/hardwarecontrol",
				"org.trinitydesktop.hardwarecontrol.InputEvents",
				dbusConn);
			if (switchesProxy.canSend()) {
				TQValueList<TQT_DBusData> params;
				params << TQT_DBusData::fromString(deviceNode().ascii());
				TQT_DBusMessage reply = switchesProxy.sendWithReply("GetProvidedSwitches", params);
				if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
					TQValueList<TQ_UINT32> list = reply[0].toList().toUInt32List();
					TQValueList<TQ_UINT32>::const_iterator it = list.begin();
					for (r = 0; it != list.end(); ++it, r++) {
						switches[r] = (*it);
					}
				}
			}
		}
	}
#endif
	if (r > 0) {
		if (BIT_IS_SET(switches, SW_LID)) {
			supportedSwitches = supportedSwitches | TDESwitchType::Lid;
		}
		if (BIT_IS_SET(switches, SW_TABLET_MODE)) {
			supportedSwitches = supportedSwitches | TDESwitchType::TabletMode;
		}
		if (BIT_IS_SET(switches, SW_RFKILL_ALL)) {
			supportedSwitches = supportedSwitches | TDESwitchType::RFKill;
		}
		if (BIT_IS_SET(switches, SW_RADIO)) {
			supportedSwitches = supportedSwitches | TDESwitchType::Radio;
		}
		if (BIT_IS_SET(switches, SW_MICROPHONE_INSERT)) {
			supportedSwitches = supportedSwitches | TDESwitchType::MicrophoneInsert;
		}
		if (BIT_IS_SET(switches, SW_DOCK)) {
			supportedSwitches = supportedSwitches | TDESwitchType::Dock;
		}
		if (BIT_IS_SET(switches, SW_LINEOUT_INSERT)) {
			supportedSwitches = supportedSwitches | TDESwitchType::LineOutInsert;
		}
		if (BIT_IS_SET(switches, SW_JACK_PHYSICAL_INSERT)) {
			supportedSwitches = supportedSwitches | TDESwitchType::JackPhysicalInsert;
		}
		if (BIT_IS_SET(switches, SW_VIDEOOUT_INSERT)) {
			supportedSwitches = supportedSwitches | TDESwitchType::VideoOutInsert;
		}
#		ifdef SW_CAMERA_LENS_COVER
		if (BIT_IS_SET(switches, SW_CAMERA_LENS_COVER)) {
			supportedSwitches = supportedSwitches | TDESwitchType::CameraLensCover;
		}
#		endif
#		ifdef SW_KEYPAD_SLIDE
		if (BIT_IS_SET(switches, SW_KEYPAD_SLIDE)) {
			supportedSwitches = supportedSwitches | TDESwitchType::KeypadSlide;
		}
#		endif
#		ifdef SW_FRONT_PROXIMITY
		if (BIT_IS_SET(switches, SW_FRONT_PROXIMITY)) {
			supportedSwitches = supportedSwitches | TDESwitchType::FrontProximity;
		}
#		endif
#		ifdef SW_ROTATE_LOCK
		if (BIT_IS_SET(switches, SW_ROTATE_LOCK)) {
			supportedSwitches = supportedSwitches | TDESwitchType::RotateLock;
		}
#		endif
#		ifdef SW_LINEIN_INSERT
		if (BIT_IS_SET(switches, SW_LINEIN_INSERT)) {
			supportedSwitches = supportedSwitches | TDESwitchType::LineInInsert;
		}
#		endif
		// Keep in sync with ACPI Event/Input identification routines above
		if (systemPath().contains("PNP0C0D")) {
			supportedSwitches = supportedSwitches | TDESwitchType::Lid;
		}
		if (systemPath().contains("PNP0C0E") || systemPath().contains("/LNXSLPBN")) {
			supportedSwitches = supportedSwitches | TDESwitchType::SleepButton;
		}
		if (systemPath().contains("PNP0C0C") || systemPath().contains("/LNXPWRBN")) {
			supportedSwitches = supportedSwitches | TDESwitchType::PowerButton;
		}
	}
	m_providedSwitches = supportedSwitches;
}

void TDEEventDevice::internalSetProvidedSwitches(TDESwitchType::TDESwitchType sl) {
	m_providedSwitches = sl;
}

TDESwitchType::TDESwitchType TDEEventDevice::activeSwitches() {
	if (!m_monitorActive) {
		internalReadActiveSwitches();
	}
	return m_switchActive;
}

void TDEEventDevice::internalReadActiveSwitches() {
	unsigned long switches[NUM_BITS(EV_CNT)];
	int r = 0;

	TDESwitchType::TDESwitchType activeSwitches = TDESwitchType::Null;
	if (m_fd >= 0) {
		r = ioctl(m_fd, EVIOCGSW(sizeof(switches)), switches);
	}
#ifdef WITH_TDEHWLIB_DAEMONS
	if( r < 1 ) {
		TQT_DBusConnection dbusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SystemBus);
		if (dbusConn.isConnected()) {
			TQT_DBusProxy switchesProxy("org.trinitydesktop.hardwarecontrol",
				"/org/trinitydesktop/hardwarecontrol",
				"org.trinitydesktop.hardwarecontrol.InputEvents",
				dbusConn);
			if (switchesProxy.canSend()) {
				TQValueList<TQT_DBusData> params;
				params << TQT_DBusData::fromString(deviceNode().ascii());
				TQT_DBusMessage reply = switchesProxy.sendWithReply("GetActiveSwitches", params);
				if (reply.type() == TQT_DBusMessage::ReplyMessage && reply.count() == 1) {
					TQValueList<TQ_UINT32> list = reply[0].toList().toUInt32List();
					TQValueList<TQ_UINT32>::const_iterator it = list.begin();
					for (r = 0; it != list.end(); ++it, r++) {
						switches[r] = (*it);
					}
				}
			}
		}
	}
#endif
	if (r > 0) {
		if (BIT_IS_SET(switches, SW_LID)) {
			activeSwitches = activeSwitches | TDESwitchType::Lid;
		}
		if (BIT_IS_SET(switches, SW_TABLET_MODE)) {
			activeSwitches = activeSwitches | TDESwitchType::TabletMode;
		}
		if (BIT_IS_SET(switches, SW_RFKILL_ALL)) {
			activeSwitches = activeSwitches | TDESwitchType::RFKill;
		}
		if (BIT_IS_SET(switches, SW_RADIO)) {
			activeSwitches = activeSwitches | TDESwitchType::Radio;
		}
		if (BIT_IS_SET(switches, SW_MICROPHONE_INSERT)) {
			activeSwitches = activeSwitches | TDESwitchType::MicrophoneInsert;
		}
		if (BIT_IS_SET(switches, SW_DOCK)) {
			activeSwitches = activeSwitches | TDESwitchType::Dock;
		}
		if (BIT_IS_SET(switches, SW_LINEOUT_INSERT)) {
			activeSwitches = activeSwitches | TDESwitchType::LineOutInsert;
		}
		if (BIT_IS_SET(switches, SW_JACK_PHYSICAL_INSERT)) {
			activeSwitches = activeSwitches | TDESwitchType::JackPhysicalInsert;
		}
		if (BIT_IS_SET(switches, SW_VIDEOOUT_INSERT)) {
			activeSwitches = activeSwitches | TDESwitchType::VideoOutInsert;
		}
#		ifdef SW_CAMERA_LENS_COVER
		if (BIT_IS_SET(switches, SW_CAMERA_LENS_COVER)) {
			activeSwitches = activeSwitches | TDESwitchType::CameraLensCover;
		}
#		endif
#		ifdef SW_KEYPAD_SLIDE
		if (BIT_IS_SET(switches, SW_KEYPAD_SLIDE)) {
			activeSwitches = activeSwitches | TDESwitchType::KeypadSlide;
		}
#		endif
#		ifdef SW_FRONT_PROXIMITY
		if (BIT_IS_SET(switches, SW_FRONT_PROXIMITY)) {
			activeSwitches = activeSwitches | TDESwitchType::FrontProximity;
		}
#		endif
#		ifdef SW_ROTATE_LOCK
		if (BIT_IS_SET(switches, SW_ROTATE_LOCK)) {
			activeSwitches = activeSwitches | TDESwitchType::RotateLock;
		}
#		endif
#		ifdef SW_LINEIN_INSERT
		if (BIT_IS_SET(switches, SW_LINEIN_INSERT)) {
			activeSwitches = activeSwitches | TDESwitchType::LineInInsert;
		}
#		endif
	}
	m_switchActive = activeSwitches;
}

void TDEEventDevice::internalSetActiveSwitches(TDESwitchType::TDESwitchType sl) {
	m_switchActive = sl;
}

// Keep this in sync with the TDESwitchType definition in the header
TQStringList TDEEventDevice::friendlySwitchList(TDESwitchType::TDESwitchType switches) {
	TQStringList ret;

	if (switches & TDESwitchType::Lid) {
		ret.append(i18n("Lid Switch"));
	}
	if (switches & TDESwitchType::TabletMode) {
		ret.append(i18n("Tablet Mode"));
	}
	if (switches & TDESwitchType::HeadphoneInsert) {
		ret.append(i18n("Headphone Inserted"));
	}
	if (switches & TDESwitchType::RFKill) {
		ret.append(i18n("Radio Frequency Device Kill Switch"));
	}
	if (switches & TDESwitchType::Radio) {
		ret.append(i18n("Enable Radio"));
	}
	if (switches & TDESwitchType::MicrophoneInsert) {
		ret.append(i18n("Microphone Inserted"));
	}
	if (switches & TDESwitchType::Dock) {
		ret.append(i18n("Docked"));
	}
	if (switches & TDESwitchType::LineOutInsert) {
		ret.append(i18n("Line Out Inserted"));
	}
	if (switches & TDESwitchType::JackPhysicalInsert) {
		ret.append(i18n("Physical Jack Inserted"));
	}
	if (switches & TDESwitchType::VideoOutInsert) {
		ret.append(i18n("Video Out Inserted"));
	}
	if (switches & TDESwitchType::CameraLensCover) {
		ret.append(i18n("Camera Lens Cover"));
	}
	if (switches & TDESwitchType::KeypadSlide) {
		ret.append(i18n("Keypad Slide"));
	}
	if (switches & TDESwitchType::FrontProximity) {
		ret.append(i18n("Front Proximity"));
	}
	if (switches & TDESwitchType::RotateLock) {
		ret.append(i18n("Rotate Lock"));
	}
	if (switches & TDESwitchType::LineInInsert) {
		ret.append(i18n("Line In Inserted"));
	}
	if (switches & TDESwitchType::PowerButton) {
		ret.append(i18n("Power Button"));
	}
	if (switches & TDESwitchType::SleepButton) {
		ret.append(i18n("Sleep Button"));
	}

	return ret;
}

void TDEEventDevice::internalStartMonitoring(TDEHardwareDevices* hwmanager) {
	if (!m_monitorActive) {
		// For security and performance reasons, only monitor known ACPI buttons
		if (eventType() != TDEEventDeviceType::Unknown) {
			if (m_fd >= 0) {
				m_eventNotifier = new TQSocketNotifier(m_fd, TQSocketNotifier::Read, this);
				connect( m_eventNotifier, TQT_SIGNAL(activated(int)), this, TQT_SLOT(eventReceived()) );
				m_monitorActive = true;
			}
		}
		if (m_monitorActive == true) {
			// get initial state of switches
			internalReadProvidedSwitches();
			internalReadActiveSwitches();
			connect( this, TQT_SIGNAL(keyPressed(unsigned int, TDEEventDevice*)), hwmanager, TQT_SLOT(processEventDeviceKeyPressed(unsigned int, TDEEventDevice*)) );
		}
	}
}

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) && (ev.value == 1)) {	// Only detect keypress events (value == 1)
			emit keyPressed(ev.code, this);
		}
		if (ev.type == EV_SW) {
			internalReadActiveSwitches();
			emit switchChanged();
		}
	}
}

void TDEEventDevice::processActiveSwitches() {
	TDESwitchType::TDESwitchType previousSwitches = m_switchActive;
	internalReadActiveSwitches();

	if (previousSwitches != m_switchActive) {
		emit switchChanged();
	}
}

void TDEEventDevice::connectNotify( const char* signal ) {
	if( !m_monitorActive && qstrcmp( signal, TQT_SIGNAL(switchChanged())) == 0 ) {
		m_watchTimer = new TQTimer(this);
		connect( m_watchTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(processActiveSwitches()) );
		m_watchTimer->start( 2500, FALSE );
		m_monitorActive = true;

		// get initial state of switches
		internalReadProvidedSwitches();
		internalReadActiveSwitches();
	}
	TQObject::connectNotify( signal );
}

#include "tdeeventdevice.moc"