summaryrefslogtreecommitdiff
path: root/src/event.c
blob: 818e66fcaa8307d1f0bb1571412652f6041f9b00 (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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * Copyright 2013  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <linux/input.h>

#include <Eina.h>
#include <Ecore.h>
#include <dlog.h>
#include <livebox-errno.h>

#include "util.h"
#include "debug.h"
#include "conf.h"
#include "event.h"

#define CRITICAL_SECTION_BEGIN(lock) do { \
	int ret; \
	ret = pthread_mutex_lock(&lock); \
	if (ret != 0) { \
		ErrPrint("Unable to get lock: %s\n", strerror(ret)); \
	} \
} while (0)

#define CRITICAL_SECTION_END(lock) do { \
	int ret; \
	ret = pthread_mutex_unlock(&lock); \
	if (ret != 0) { \
		ErrPrint("Unable to unlock: %s\n", strerror(ret)); \
	} \
} while (0)

#define CANCEL_SECTION_BEGIN() do { \
	int ret; \
	ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); \
	if (ret != 0) \
		ErrPrint("Unable to set cancelate state: %s\n", strerror(ret)); \
} while (0)

#define CANCEL_SECTION_END() do { \
	int ret; \
	ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); \
	if (ret != 0) \
		ErrPrint("Unable to set cancelate state: %s\n", strerror(ret)); \
} while (0)

#define PIPE_READ	0
#define PIPE_WRITE	1
#define EVENT_CH	'e'

int errno;

static struct info {
	pthread_t tid;
	Eina_List *event_list;
	int handle;
	pthread_mutex_t event_list_lock;
	int evt_pipe[2];
	Ecore_Fd_Handler *event_handler;

	int (*event_cb)(enum event_state state, struct event_data *event, void *data);
	void *cbdata;

	enum event_state event_state;
	struct event_data event_data;

	int x;
	int y;
} s_info = {
	.event_list = NULL,
	.handle = -1,
	.event_handler = NULL,

	.event_cb = NULL,
	.cbdata = NULL,

	.event_state = EVENT_STATE_DEACTIVATE,

	.event_data = {
		.x = 0,
		.y = 0,
		.device = -1,
	},
};

HAPI int event_init(void)
{
	int ret;
	ret = pthread_mutex_init(&s_info.event_list_lock, NULL);
	if (ret != 0) {
		ErrPrint("Mutex: %s\n", strerror(ret));
		return LB_STATUS_ERROR_FAULT;
	}
	return LB_STATUS_SUCCESS;
}

HAPI int event_fini(void)
{
	int ret;
	ret = pthread_mutex_destroy(&s_info.event_list_lock);
	if (ret != 0)
		ErrPrint("Mutex destroy failed: %s\n", strerror(ret));
	return LB_STATUS_SUCCESS;
}

static inline int processing_input_event(struct input_event *event)
{
	struct event_data *item;

	switch (event->type) {
	case EV_SYN:
		switch (event->code) {
		case SYN_REPORT:
			if (s_info.event_data.x < 0 || s_info.event_data.y < 0) {
				/* Waiting full event packet */
				break;
			}

			item = malloc(sizeof(*item));
			if (item) {
				char event_ch;

				memcpy(item, &s_info.event_data, sizeof(*item));

				CRITICAL_SECTION_BEGIN(s_info.event_list_lock);
				s_info.event_list = eina_list_append(s_info.event_list, item);
				CRITICAL_SECTION_END(s_info.event_list_lock);

				event_ch = EVENT_CH;
				if (write(s_info.evt_pipe[PIPE_WRITE], &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
					ErrPrint("Unable to send an event: %s\n", strerror(errno));
					return LB_STATUS_ERROR_IO;
				}
			}
			break;
		case SYN_CONFIG:
			break;
		case SYN_MT_REPORT:
			break;
		/*
		case SYN_DROPPED:
			DbgPrint("EV_SYN, SYN_DROPPED\n");
			break;
		*/
		default:
			DbgPrint("EV_SYN, 0x%x\n", event->code);
			break;
		}
		break;
	case EV_KEY:
		break;
	case EV_REL:
		break;
	case EV_ABS:
		switch (event->code) {
		case ABS_DISTANCE:
			break;
		case ABS_MT_POSITION_X:
			s_info.event_data.x = event->value - s_info.x;
			break;
		case ABS_MT_POSITION_Y:
			s_info.event_data.y = event->value - s_info.y;
			break;
		case ABS_MT_SLOT:
			break;
		case ABS_MT_TRACKING_ID:
			s_info.event_data.device = event->value;
			break;
		case ABS_MT_TOUCH_MAJOR:
			break;
		case ABS_MT_TOUCH_MINOR:
			break;
		case ABS_MT_WIDTH_MAJOR:
			break;
		case ABS_MT_WIDTH_MINOR:
			break;
		default:
			DbgPrint("EV_ABS, 0x%x\n", event->code);
			break;
		}
		break;
	case EV_MSC:
		break;
	case EV_SW:
		break;
	case EV_LED:
		break;
	case EV_SND:
		break;
	case EV_REP:
		break;
	case EV_FF:
		break;
	case EV_PWR:
		break;
	case EV_FF_STATUS:
		break;
	default:
		DbgPrint("0x%X, 0x%X\n", event->type, event->code);
		break;
	}

	return LB_STATUS_SUCCESS;
}

static void *event_main(void *data)
{
	fd_set set;
	int ret = 0;
	struct input_event input_event;
	char *ptr = (char *)&input_event;
	int offset = 0;
	int readsize = 0;

	DbgPrint("event_main initiated\n");

	while (1) {
		CANCEL_SECTION_BEGIN();
		FD_ZERO(&set);
		FD_SET(s_info.handle, &set);
		ret = select(s_info.handle + 1, &set, NULL, NULL, NULL);
		if (ret < 0) {
			ret = -errno;
			if (errno == EINTR) {
				DbgPrint("Select receives INTR\n");
				CANCEL_SECTION_END();
				continue;
			}
			ErrPrint("Error: %s\n", strerror(errno));
			CANCEL_SECTION_END();
			return (void *)ret;
		} else if (ret == 0) {
			ErrPrint("Timeout expired\n");
			CANCEL_SECTION_END();
			return (void *)LB_STATUS_ERROR_TIMEOUT;
		}
		CANCEL_SECTION_END();

		if (!FD_ISSET(s_info.handle, &set)) {
			ErrPrint("Unexpected handle is toggled\n");
			ret = LB_STATUS_ERROR_INVALID;
			break;
		}

		readsize = read(s_info.handle, ptr + offset, sizeof(input_event) - offset);
		if (readsize < 0) {
			ErrPrint("Unable to read device: %s / fd: %d / offset: %d / size: %d - %d\n", strerror(errno), s_info.handle, offset, sizeof(input_event), readsize);
			ret = LB_STATUS_ERROR_FAULT;
			break;
		}

		offset += readsize;
		if (offset == sizeof(input_event)) {
			offset = 0;
			if (processing_input_event(&input_event) < 0) {
				ret = LB_STATUS_ERROR_FAULT;
				break;
			}
		}
	}

	return (void *)ret;
}

static Eina_Bool event_read_cb(void *data, Ecore_Fd_Handler *handler)
{
	int fd;
	struct event_data *item;
	char event_ch;

	fd = ecore_main_fd_handler_fd_get(handler);
	if (fd < 0) {
		ErrPrint("Invalid fd\n");
		return ECORE_CALLBACK_CANCEL;
	}

	if (read(fd, &event_ch, sizeof(event_ch)) != sizeof(event_ch)) {
		ErrPrint("Unable to read event ch: %s\n", strerror(errno));
		return ECORE_CALLBACK_CANCEL;
	}

	CRITICAL_SECTION_BEGIN(s_info.event_list_lock);
	item = eina_list_nth(s_info.event_list, 0);
	if (item)
		s_info.event_list = eina_list_remove(s_info.event_list, item);
	else
		ErrPrint("Unable to get event\n");
	CRITICAL_SECTION_END(s_info.event_list_lock);

	if (item && s_info.event_cb) {
		switch (s_info.event_state) {
		case EVENT_STATE_DEACTIVATE:
			s_info.event_state = EVENT_STATE_ACTIVATE;
			break;
		case EVENT_STATE_ACTIVATE:
			s_info.event_state = EVENT_STATE_ACTIVATED;
			break;
		case EVENT_STATE_ACTIVATED:
		default:
			break;
		}
		s_info.event_cb(s_info.event_state, item, s_info.cbdata);
	}

	free(item);
	return ECORE_CALLBACK_RENEW;
}

HAPI int event_activate(int x, int y, int (*event_cb)(enum event_state state, struct event_data *event, void *data), void *data)
{
	int status;

	if (s_info.handle >= 0) {
		DbgPrint("Already activated\n");
		return LB_STATUS_SUCCESS;
	}

	s_info.handle = open(INPUT_PATH, O_RDONLY);
	if (s_info.handle < 0) {
		ErrPrint("Unable to access the device: %s\n", strerror(errno));
		return LB_STATUS_ERROR_IO;
	}

	if (fcntl(s_info.handle, F_SETFD, FD_CLOEXEC) < 0)
		ErrPrint("Error: %s\n", strerror(errno));

	if (fcntl(s_info.handle, F_SETFL, O_NONBLOCK) < 0)
		ErrPrint("Error: %s\n", strerror(errno));

	status = pipe2(s_info.evt_pipe, O_NONBLOCK | O_CLOEXEC);
	if (status < 0) {
		ErrPrint("Unable to prepare evt pipe: %s\n", strerror(errno));
		if (close(s_info.handle) < 0)
			ErrPrint("Failed to close handle: %s\n", strerror(errno));
		s_info.handle = -1;
		return LB_STATUS_ERROR_FAULT;
	}

	s_info.event_handler = ecore_main_fd_handler_add(s_info.evt_pipe[PIPE_READ], ECORE_FD_READ, event_read_cb, NULL, NULL, NULL);
	if (!s_info.event_handler) {
		if (close(s_info.handle) < 0)
			ErrPrint("Failed to close handle: %s\n", strerror(errno));

		if (close(s_info.evt_pipe[PIPE_READ]) < 0)
			ErrPrint("Failed to close handle: %s\n", strerror(errno));

		if (close(s_info.evt_pipe[PIPE_WRITE]) < 0)
			ErrPrint("Failed to close handle: %s\n", strerror(errno));

		s_info.handle = -1;
		return LB_STATUS_ERROR_FAULT;
	}

	status = pthread_create(&s_info.tid, NULL, event_main, NULL);
	if (status != 0) {
		ErrPrint("Failed to initiate the thread: %s\n", strerror(status));
		ecore_main_fd_handler_del(s_info.event_handler);
		s_info.event_handler = NULL;

		if (close(s_info.handle) < 0)
			ErrPrint("close: %s\n", strerror(errno));
		s_info.handle = -1;

		if (close(s_info.evt_pipe[PIPE_READ]) < 0)
			ErrPrint("close: %s\n", strerror(errno));

		if (close(s_info.evt_pipe[PIPE_WRITE]) < 0)
			ErrPrint("close: %s\n", strerror(errno));

		return LB_STATUS_ERROR_FAULT;
	}

	s_info.event_cb = event_cb;
	s_info.cbdata = data;
	s_info.x = x;
	s_info.y = y;

	DbgPrint("Event handler activated\n");
	return LB_STATUS_SUCCESS;
}

HAPI int event_deactivate(void)
{
	int status;
	struct event_data *event;
	void *ret;

	if (s_info.handle < 0) {
		ErrPrint("Event handler is not actiavated\n");
		return LB_STATUS_SUCCESS;
	}

	status = pthread_cancel(s_info.tid);
	if (status != 0)
		ErrPrint("Failed to cacnel a thread: %s\n", strerror(errno));

	status = pthread_join(s_info.tid, &ret);
	if (status != 0)
		ErrPrint("Failed to join a thread: %s\n", strerror(errno));
	else if (ret == PTHREAD_CANCELED)
		DbgPrint("Thread is canceled\n");

	ecore_main_fd_handler_del(s_info.event_handler);
	s_info.event_handler = NULL;

	if (close(s_info.evt_pipe[PIPE_READ]) < 0)
		ErrPrint("Failed to close: %s\n", strerror(errno));

	if (close(s_info.evt_pipe[PIPE_WRITE]) < 0)
		ErrPrint("Failed to close: %s\n", strerror(errno));

	if (close(s_info.handle) < 0)
		ErrPrint("Unable to release the fd: %s\n", strerror(errno));

	s_info.handle = -1;
	DbgPrint("Event handler deactivated\n");

	EINA_LIST_FREE(s_info.event_list, event) {
		if (s_info.event_cb) {
			if (s_info.event_state == EVENT_STATE_DEACTIVATE) {
				s_info.event_state = EVENT_STATE_ACTIVATE;
			} else if (s_info.event_state == EVENT_STATE_ACTIVATE) {
				s_info.event_state = EVENT_STATE_ACTIVATED;
			}
			s_info.event_cb(s_info.event_state, event, s_info.cbdata);
		}
		free(event);
	}

	if (s_info.event_state != EVENT_STATE_DEACTIVATE) {
		s_info.event_state = EVENT_STATE_DEACTIVATE;

		if (s_info.event_cb)
			s_info.event_cb(s_info.event_state, &s_info.event_data, s_info.cbdata);
	}

	s_info.event_data.x = -1;
	s_info.event_data.y = -1;

	return LB_STATUS_SUCCESS;
}

HAPI int event_is_activated(void)
{
	return s_info.handle >= 0;
}

/* End of a file */