summaryrefslogtreecommitdiff
path: root/src/hw_key.c
blob: 9d9093d223c7dd511bbe8ebc00b5e00f8297b4e2 (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
 /*
  * Copyright 2012  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.
  */



#include <ail.h>
#include <aul.h>
#include <bundle.h>
#include <Elementary.h>
#include <Ecore_X.h>
#include <Ecore_Input.h>
#include <sysman.h>
#include <syspopup_caller.h>
#include <utilX.h>
#include <vconf.h>
#include <system/media_key.h>

#include "hw_key.h"
#include "util.h"

#define TASKMGR_PKG_NAME "org.tizen.taskmgr"
#define CAMERA_PKG_NAME "org.tizen.camera-app"
#define CALLLOG_PKG_NAME "org.tizen.calllog"
#define MUSIC_PLAYER_PKG_NAME "org.tizen.music-player"



static struct {
	Ecore_X_Window win;
	Ecore_Event_Handler *key_up;
	Ecore_Event_Handler *key_down;
	Ecore_Timer *long_press;
	Ecore_Timer *single_timer;
	Ecore_Timer *volume_up_long_press;
	Ecore_Timer *volume_down_long_press;
	Eina_Bool cancel;
} key_info = {
	.win = 0x0,
	.key_up = NULL,
	.key_down = NULL,
	.long_press = NULL,
	.single_timer = NULL,
	.volume_up_long_press = NULL,
	.volume_down_long_press = NULL,
	.cancel = EINA_FALSE,
};



static Eina_Bool _launch_taskmgr_cb(void* data)
{
	int val1 = -1;
	int val2 = -1;
	_D("Launch TASKMGR");

	key_info.long_press = NULL;

	if (vconf_get_int(VCONFKEY_PM_STATE, &val1) < 0) {
		_E("Cannot get VCONFKEY_PM_STATE");
		return ECORE_CALLBACK_CANCEL;
	}
	if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &val2) < 0) {
		_E("Cannot get VCONFKEY_IDLE_LOCK_STATE");
		return ECORE_CALLBACK_CANCEL;
	}

	if ((val1 == VCONFKEY_PM_STATE_NORMAL) && (val2 == VCONFKEY_IDLE_UNLOCK)) {
		_D("LCD ON, UNLOCK state => launch taskmgr");
		if (aul_open_app(TASKMGR_PKG_NAME) < 0)
			_E("Failed to launch the taskmgr");
	} else {
		_D("Can't launch TASKMGR pm state : %d lock state : %d", val1, val2);
	}

	return ECORE_CALLBACK_CANCEL;
}



static Eina_Bool _launch_home_screen(void *data)
{
	char *package;
	int ret;

	syspopup_destroy_all();
	key_info.single_timer = NULL;

	package = vconf_get_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME);
	if (package) {
		ret = aul_open_app(package);
		if (ret < 0) {
			_E("cannot launch package %s(err:%d)", package, ret);

			if (-1 == ret) {
				ret = aul_open_app(HOME_SCREEN_PKG_NAME);
				if (ret < 0) {
					_E("Failed to open a default home, %s(err:%d)", HOME_SCREEN_PKG_NAME, ret);
				}
			}
		}

		free(package);
	} else {
		ret = aul_open_app(HOME_SCREEN_PKG_NAME);
		if (ret < 0) _E("Cannot open default home");
	}

	if (ret > 0) {
		if (-1 == sysconf_set_mempolicy_bypid(ret, OOM_IGNORE)) {
			_E("Cannot set the memory policy for Home-screen(%d)", ret);
		} else {
			_E("Set the memory policy for Home-screen(%d)", ret);
		}
	}

	return ECORE_CALLBACK_CANCEL;
}



inline static int _release_home_key(void)
{
	retv_if(NULL == key_info.long_press, EXIT_SUCCESS);
	ecore_timer_del(key_info.long_press);
	key_info.long_press = NULL;

	if (NULL == key_info.single_timer) {
		key_info.single_timer = ecore_timer_add(0.3, _launch_home_screen, NULL);
		return EXIT_SUCCESS;
	}
	ecore_timer_del(key_info.single_timer);
	key_info.single_timer = NULL;

	syspopup_destroy_all();

	return EXIT_SUCCESS;
}



inline static void _release_multimedia_key(const char *value)
{
	bundle *b;
	int ret;

	_D("Multimedia key is released with %s", value);
	ret_if(NULL == value);

	b = bundle_create();
	if (!b) {
		_E("Cannot create bundle");
		return;
	}

	bundle_add(b, "multimedia_key", value);
	ret = aul_launch_app(MUSIC_PLAYER_PKG_NAME, b);
	bundle_free(b);

	if (ret < 0)
		_E("Failed to launch the running apps, ret : %d", ret);
}



static Eina_Bool _key_release_cb(void *data, int type, void *event)
{
	Evas_Event_Key_Up *ev = event;
	int val = -1;

	_D("Released");

	if (!ev) {
		_D("Invalid event object");
		return ECORE_CALLBACK_RENEW;
	}

	if (!strcmp(ev->keyname, KEY_END)) {
	} else if (!strcmp(ev->keyname, KEY_CONFIG)) {
	} else if (!strcmp(ev->keyname, KEY_SEND)) {
	} else if (!strcmp(ev->keyname, KEY_HOME)) {

		if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &val) < 0) {
			_D("Cannot get VCONFKEY_IDLE_LOCK_STATE");
		}
		if (val == VCONFKEY_IDLE_LOCK) {
			_D("lock state, ignore home key..!!");
			return ECORE_CALLBACK_RENEW;
		}

		if (EINA_TRUE == key_info.cancel) {
			_D("Cancel key is activated");
			if (key_info.long_press) {
				ecore_timer_del(key_info.long_press);
				key_info.long_press = NULL;
			}

			if (key_info.single_timer) {
				ecore_timer_del(key_info.single_timer);
				key_info.single_timer = NULL;
			}

			return ECORE_CALLBACK_RENEW;
		}

		_release_home_key();
	} else if (!strcmp(ev->keyname, KEY_PAUSE)) {
	} else if (!strcmp(ev->keyname, KEY_CANCEL)) {
		_D("CANCEL Key is released");
		key_info.cancel = EINA_FALSE;
	} else if (!strcmp(ev->keyname, KEY_MEDIA)) {
		_release_multimedia_key("KEY_PLAYCD");
	}

	return ECORE_CALLBACK_RENEW;
}



static Eina_Bool _key_press_cb(void *data, int type, void *event)
{
	Evas_Event_Key_Down *ev = event;
	int val = -1;

	_D("Pressed");

	if (!ev) {
		_D("Invalid event object");
		return ECORE_CALLBACK_RENEW;
	}

	if (!strcmp(ev->keyname, KEY_SEND)) {
		_D("Launch calllog");
		if (aul_open_app(CALLLOG_PKG_NAME) < 0)
			_E("Failed to launch %s", CALLLOG_PKG_NAME);
	} else if(!strcmp(ev->keyname, KEY_CONFIG)) {
		_D("Launch camera");
		if (aul_open_app(CAMERA_PKG_NAME) < 0)
			_E("Failed to launch %s", CAMERA_PKG_NAME);
	} else if (!strcmp(ev->keyname, KEY_HOME)) {
		if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &val) < 0) {
			_D("Cannot get VCONFKEY_IDLE_LOCK_STATE");
		}
		if (val == VCONFKEY_IDLE_LOCK) {
			_D("lock state, ignore home key..!!");
			return ECORE_CALLBACK_RENEW;
		}
		if (key_info.long_press) {
			ecore_timer_del(key_info.long_press);
			key_info.long_press = NULL;
		}

		key_info.long_press = ecore_timer_add(0.6, _launch_taskmgr_cb, NULL);
		if (!key_info.long_press)
			_E("Failed to add timer for long press detection");
	} else if (!strcmp(ev->keyname, KEY_CANCEL)) {
		_D("Cancel button is pressed");
		key_info.cancel = EINA_TRUE;
	} else if (!strcmp(ev->keyname, KEY_MEDIA)) {
		_D("Media key is pressed");
	}

	return ECORE_CALLBACK_RENEW;
}



void _media_key_event_cb(media_key_e key, media_key_event_e status, void *user_data)
{
	_D("MEDIA KEY EVENT");
	if (MEDIA_KEY_STATUS_PRESSED == status) return;

	if (MEDIA_KEY_PAUSE == key) {
		_release_multimedia_key("KEY_PAUSECD");
	} else if (MEDIA_KEY_PLAY == key) {
		_release_multimedia_key("KEY_PLAYCD");
	}
}



void create_key_window(void)
{
	key_info.win = ecore_x_window_input_new(0, 0, 0, 1, 1);
	if (!key_info.win) {
		_D("Failed to create hidden window");
		return;
	}
	ecore_x_event_mask_unset(key_info.win, ECORE_X_EVENT_MASK_NONE);
	ecore_x_icccm_title_set(key_info.win, "menudaemon,key,receiver");
	ecore_x_netwm_name_set(key_info.win, "menudaemon,key,receiver");
	ecore_x_netwm_pid_set(key_info.win, getpid());

	utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_HOME, SHARED_GRAB);
	utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_VOLUMEDOWN, SHARED_GRAB);
	utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_VOLUMEUP, SHARED_GRAB);
	utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_CONFIG, SHARED_GRAB);
	utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_MEDIA, SHARED_GRAB);

	key_info.key_up = ecore_event_handler_add(ECORE_EVENT_KEY_UP, _key_release_cb, NULL);
	if (!key_info.key_up)
		_D("Failed to register a key up event handler");

	key_info.key_down = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, _key_press_cb, NULL);
	if (!key_info.key_down)
		_D("Failed to register a key down event handler");

	media_key_reserve(_media_key_event_cb, NULL);
}



void destroy_key_window(void)
{
	utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_HOME);
	utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_VOLUMEDOWN);
	utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_VOLUMEUP);
	utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_CONFIG);
	utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_MEDIA);

	if (key_info.key_up) {
		ecore_event_handler_del(key_info.key_up);
		key_info.key_up = NULL;
	}

	if (key_info.key_down) {
		ecore_event_handler_del(key_info.key_down);
		key_info.key_down = NULL;
	}

	ecore_x_window_delete_request_send(key_info.win);
	key_info.win = 0x0;

	media_key_release();
}