summaryrefslogtreecommitdiff
path: root/src/util/timeout_handler.c
blob: 244b24a4f3a334956ded3c215e096ffb6582f242 (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
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
*/
#include <stdbool.h>
#include <Elementary.h>
#include <app_debug.h>
#include <key_define.h>

#include "define.h"
#include "util/timeout_handler.h"

struct timeout_handler {
	Eina_List *list;
	Ecore_Timer *timer;

	timeout_event_cb event_cb;
	void *event_data;

	timeout_event_cb timeout_cb;
	void *timeout_data;

	double timeout;
	bool enable;
};

static Eina_Bool _timer_cb(void *data)
{
	struct timeout_handler *handle;
	Eina_Bool r;

	if (!data)
		return ECORE_CALLBACK_CANCEL;

	handle = data;

	handle->timer = NULL;

	if (!handle->enable)
		return ECORE_CALLBACK_CANCEL;

	r = handle->timeout_cb(handle->timeout_data, 0, NULL);

	return r;
}

void _timer_reset(struct timeout_handler *handle)
{
	if (handle->timer)
		ecore_timer_reset(handle->timer);
	else
		handle->timer = ecore_timer_add(handle->timeout,
				_timer_cb, handle);
}

static Eina_Bool _event_occured(void *data, int type, void *event)
{
	struct timeout_handler *handle;
	Eina_Bool r;

	if (!data)
		return ECORE_CALLBACK_PASS_ON;

	handle = data;

	if (!handle->enable)
		return ECORE_CALLBACK_PASS_ON;

	/* Don't handle Volume and Channel keys */
	if (type == ECORE_EVENT_KEY_DOWN) {
		Evas_Event_Key_Down *ev;

		if (!event)
			return ECORE_CALLBACK_PASS_ON;

		ev = event;

		if (!strcmp(ev->keyname, KEY_VOLUMEUP) ||
				!strcmp(ev->keyname, KEY_VOLUMEDOWN) ||
				!strcmp(ev->keyname, KEY_MUTE) ||
				!strcmp(ev->keyname, KEY_CHANNELUP) ||
				!strcmp(ev->keyname, KEY_CHANNELDOWN))
			return ECORE_CALLBACK_PASS_ON;
	}

	r = handle->event_cb(handle->event_data, type, event);

	_timer_reset(handle);

	return r;
}

void timeout_handler_pause(struct timeout_handler *handle)
{
	ecore_timer_del(handle->timer);
	handle->timer = NULL;
}

void timeout_handler_resume(struct timeout_handler *handle)
{
	_timer_reset(handle);
}

void timeout_handler_enable(struct timeout_handler *handle, bool enable)
{
	handle->enable = enable;

	if (enable)
		timeout_handler_resume(handle);
	else
		timeout_handler_pause(handle);
}

struct timeout_handler *timeout_handler_init(double timeout,
		timeout_event_cb timeout_cb, void *timeout_data,
		timeout_event_cb event_cb, void *event_data)
{
	struct timeout_handler *handle;
	Ecore_Event_Handler *ev;
	int i;
	int event_type[] = {
		ECORE_EVENT_KEY_DOWN,
		ECORE_EVENT_MOUSE_BUTTON_DOWN,
		ECORE_EVENT_MOUSE_BUTTON_UP,
		ECORE_EVENT_MOUSE_MOVE,
		ECORE_EVENT_MOUSE_WHEEL
	};

	handle = calloc(1, sizeof(*handle));
	if (!handle) {
		_ERR("failed to allocate");
		return NULL;
	}

	for (i = 0; i < sizeof(event_type) / sizeof(*event_type); i++) {
		ev = ecore_event_handler_add(event_type[i],
				_event_occured, handle);
		if (!ev)
			goto error;

		handle->list = eina_list_append(handle->list, ev);
	}

	handle->event_cb = event_cb;
	handle->event_data = event_data;
	handle->timeout_cb = timeout_cb;
	handle->timeout_data = timeout_data;
	handle->timeout = timeout;
	handle->enable = false;

	return handle;

error:
	timeout_handler_fini(handle);

	return NULL;
}

void timeout_handler_fini(struct timeout_handler *handle)
{
	Ecore_Event_Handler *ev;

	if (!handle)
		return;

	EINA_LIST_FREE(handle->list, ev)
		ecore_event_handler_del(ev);

	ecore_timer_del(handle->timer);
	handle->timer = NULL;

	free(handle);
}