2 * Copyright 2012 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.tizenopensource.org/license
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <Elementary.h>
23 #include <Ecore_Input.h>
25 #include <syspopup_caller.h>
28 #include <system/media_key.h>
33 #define TASKMGR_PKG_NAME "org.tizen.taskmgr"
34 #define CAMERA_PKG_NAME "org.tizen.camera-app"
35 #define CALLLOG_PKG_NAME "org.tizen.calllog"
36 #define SEARCH_PKG_NAME "org.tizen.smartsearch"
37 #define MUSIC_PLAYER_PKG_NAME "org.tizen.music-player"
43 Ecore_Event_Handler *key_up;
44 Ecore_Event_Handler *key_down;
45 Ecore_Timer *long_press;
46 Ecore_Timer *single_timer;
47 Ecore_Timer *volume_up_long_press;
48 Ecore_Timer *volume_down_long_press;
56 .volume_up_long_press = NULL,
57 .volume_down_long_press = NULL,
63 static Eina_Bool _launch_taskmgr_cb(void* data)
67 key_info.long_press = NULL;
69 if (aul_open_app(TASKMGR_PKG_NAME) < 0)
70 _E("Failed to launch the taskmgr");
72 return ECORE_CALLBACK_CANCEL;
77 static Eina_Bool _launch_home_screen(void *data)
82 syspopup_destroy_all();
83 key_info.single_timer = NULL;
85 package = vconf_get_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME);
87 ret = aul_open_app(package);
89 _E("cannot launch package %s(err:%d)", package, ret);
91 if (-1 == ret) { // -1 : AUL returns '-1' when there is no package name in DB.
92 ret = aul_open_app(HOME_SCREEN_PKG_NAME);
94 _E("Failed to open a default home, %s(err:%d)", HOME_SCREEN_PKG_NAME, ret);
101 ret = aul_open_app(HOME_SCREEN_PKG_NAME);
102 if (ret < 0) _E("Cannot open default home");
106 if (-1 == sysconf_set_mempolicy_bypid(ret, OOM_IGNORE)) {
107 _E("Cannot set the memory policy for Home-screen(%d)", ret);
109 _E("Set the memory policy for Home-screen(%d)", ret);
113 return ECORE_CALLBACK_CANCEL;
118 inline static void _launch_svoice(void)
120 _D("Launch smartsearch");
121 if (aul_open_app(SEARCH_PKG_NAME) < 0)
122 _E("Cannot open Smart-search");
129 inline static int _release_home_key(void)
131 retv_if(NULL == key_info.long_press, EXIT_SUCCESS);
132 ecore_timer_del(key_info.long_press);
133 key_info.long_press = NULL;
135 if (NULL == key_info.single_timer) {
136 key_info.single_timer = ecore_timer_add(0.3, _launch_home_screen, NULL);
139 ecore_timer_del(key_info.single_timer);
140 key_info.single_timer = NULL;
142 syspopup_destroy_all();
150 inline static void _release_multimedia_key(const char *value)
155 _D("Multimedia key is released with %s", value);
156 ret_if(NULL == value);
160 _E("Cannot create bundle");
164 bundle_add(b, "multimedia_key", value);
165 ret = aul_launch_app(MUSIC_PLAYER_PKG_NAME, b);
169 _E("Failed to launch the running apps, ret : %d", ret);
174 static Eina_Bool _key_release_cb(void *data, int type, void *event)
176 Evas_Event_Key_Up *ev = event;
182 _D("Invalid event object");
183 return ECORE_CALLBACK_RENEW;
186 if (!strcmp(ev->keyname, KEY_END)) {
187 } else if (!strcmp(ev->keyname, KEY_CONFIG)) {
188 } else if (!strcmp(ev->keyname, KEY_SEND)) {
189 } else if (!strcmp(ev->keyname, KEY_HOME)) {
191 if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &val) < 0) {
192 _D("Cannot get VCONFKEY_IDLE_LOCK_STATE");
194 if (val == VCONFKEY_IDLE_LOCK) {
195 _D("lock state, ignore home key..!!");
196 return ECORE_CALLBACK_RENEW;
199 if (EINA_TRUE == key_info.cancel) {
200 _D("Cancel key is activated");
201 if (key_info.long_press) {
202 ecore_timer_del(key_info.long_press);
203 key_info.long_press = NULL;
206 if (key_info.single_timer) {
207 ecore_timer_del(key_info.single_timer);
208 key_info.single_timer = NULL;
211 return ECORE_CALLBACK_RENEW;
215 } else if (!strcmp(ev->keyname, KEY_PAUSE)) {
216 } else if (!strcmp(ev->keyname, KEY_CANCEL)) {
217 _D("CANCEL Key is released");
218 key_info.cancel = EINA_FALSE;
219 } else if (!strcmp(ev->keyname, KEY_MEDIA)) {
220 _release_multimedia_key("KEY_PLAYCD");
223 return ECORE_CALLBACK_RENEW;
228 static Eina_Bool _key_press_cb(void *data, int type, void *event)
230 Evas_Event_Key_Down *ev = event;
236 _D("Invalid event object");
237 return ECORE_CALLBACK_RENEW;
240 if (!strcmp(ev->keyname, KEY_SEND)) {
241 _D("Launch calllog");
242 if (aul_open_app(CALLLOG_PKG_NAME) < 0)
243 _E("Failed to launch %s", CALLLOG_PKG_NAME);
244 } else if(!strcmp(ev->keyname, KEY_CONFIG)) {
246 if (aul_open_app(CAMERA_PKG_NAME) < 0)
247 _E("Failed to launch %s", CAMERA_PKG_NAME);
248 } else if (!strcmp(ev->keyname, KEY_HOME)) {
249 if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &val) < 0) {
250 _D("Cannot get VCONFKEY_IDLE_LOCK_STATE");
252 if (val == VCONFKEY_IDLE_LOCK) {
253 _D("lock state, ignore home key..!!");
254 return ECORE_CALLBACK_RENEW;
256 if (key_info.long_press) {
257 ecore_timer_del(key_info.long_press);
258 key_info.long_press = NULL;
261 key_info.long_press = ecore_timer_add(0.5, _launch_taskmgr_cb, NULL);
262 if (!key_info.long_press)
263 _E("Failed to add timer for long press detection");
264 } else if (!strcmp(ev->keyname, KEY_CANCEL)) {
265 _D("Cancel button is pressed");
266 key_info.cancel = EINA_TRUE;
267 } else if (!strcmp(ev->keyname, KEY_MEDIA)) {
268 _D("Media key is pressed");
271 return ECORE_CALLBACK_RENEW;
276 void _media_key_event_cb(media_key_e key, media_key_event_e status, void *user_data)
278 _D("MEDIA KEY EVENT");
279 if (MEDIA_KEY_STATUS_PRESSED == status) return;
281 if (MEDIA_KEY_PAUSE == key) {
282 _release_multimedia_key("KEY_PAUSECD");
283 } else if (MEDIA_KEY_PLAY == key) {
284 _release_multimedia_key("KEY_PLAYCD");
290 void create_key_window(void)
292 key_info.win = ecore_x_window_input_new(0, 0, 0, 1, 1);
294 _D("Failed to create hidden window");
297 ecore_x_event_mask_unset(key_info.win, ECORE_X_EVENT_MASK_NONE);
298 ecore_x_icccm_title_set(key_info.win, "menudaemon,key,receiver");
299 ecore_x_netwm_name_set(key_info.win, "menudaemon,key,receiver");
300 ecore_x_netwm_pid_set(key_info.win, getpid());
302 utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_HOME, SHARED_GRAB);
303 utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_VOLUMEDOWN, SHARED_GRAB);
304 utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_VOLUMEUP, SHARED_GRAB);
305 utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_CONFIG, SHARED_GRAB);
306 utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_MEDIA, SHARED_GRAB);
308 key_info.key_up = ecore_event_handler_add(ECORE_EVENT_KEY_UP, _key_release_cb, NULL);
309 if (!key_info.key_up)
310 _D("Failed to register a key up event handler");
312 key_info.key_down = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, _key_press_cb, NULL);
313 if (!key_info.key_down)
314 _D("Failed to register a key down event handler");
316 media_key_reserve(_media_key_event_cb, NULL);
321 void destroy_key_window(void)
323 utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_HOME);
324 utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_VOLUMEDOWN);
325 utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_VOLUMEUP);
326 utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_CONFIG);
327 utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_MEDIA);
329 if (key_info.key_up) {
330 ecore_event_handler_del(key_info.key_up);
331 key_info.key_up = NULL;
334 if (key_info.key_down) {
335 ecore_event_handler_del(key_info.key_down);
336 key_info.key_down = NULL;
339 ecore_x_window_delete_request_send(key_info.win);