27e814c3073f62e89320ce09bbd6cf0cf90ae9d2
[apps/native/starter.git] / src / mobile / hw_key.c
1 /*
2  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifdef HAVE_X11
18
19 #include <app.h>
20 #include <bundle.h>
21 #include <Elementary.h>
22 #include <Ecore_X.h>
23 #include <Ecore_Input.h>
24 #include <dd-deviced.h>
25 #include <syspopup_caller.h>
26 #include <utilX.h>
27 #include <vconf.h>
28 #include <system/media_key.h>
29 #include <aul.h>
30 #include <feedback.h>
31 #include <system_settings.h>
32
33 #include "hw_key.h"
34 #include "home_mgr.h"
35 #include "util.h"
36 #include "dbus_util.h"
37 #include "lock_mgr.h"
38 #include "status.h"
39 #include "process_mgr.h"
40
41 #define APPID_CAMERA "org.tizen.camera-app"
42 #define APPID_CALLLOG "org.tizen.calllog"
43 #define APPID_MUSIC_PLAYER "org.tizen.music-player"
44 #define APPID_TASKMGR "org.tizen.task-mgr"
45 #define APPID_BROWSER "org.tizen.browser"
46 #define APPID_EMAIL "org.tizen.email"
47 #define APPID_DIALER "org.tizen.phone"
48
49 #define STR_ATOM_XKEY_COMPOSITION "_XKEY_COMPOSITION"
50 #define STR_ATOM_KEYROUTER_NOTIWINDOW "_KEYROUTER_NOTIWINDOW"
51
52 #define LONG_PRESS_TIMER_SEC 0.4
53 #define HOMEKEY_TIMER_SEC 0.2
54 #define CANCEL_KEY_TIMER_SEC 0.3
55
56 static struct {
57         Ecore_X_Window win;
58         Ecore_Event_Handler *key_up;
59         Ecore_Event_Handler *key_down;
60         Ecore_Timer *home_long_press_timer;
61         Ecore_Timer *home_multi_press_timer;
62         Eina_Bool cancel;
63         Ecore_X_Window keyrouter_notiwindow;
64         int homekey_count;
65 } key_info = {
66         .win = 0x0,
67         .key_up = NULL,
68         .key_down = NULL,
69         .home_long_press_timer = NULL,
70         .home_multi_press_timer = NULL,
71         .cancel = EINA_FALSE,
72         .keyrouter_notiwindow = 0x0,
73         .homekey_count = 0,
74 };
75
76
77
78 static void _after_launch_taskmgr(int pid)
79 {
80         if(0 < pid) {
81                 if(dbus_util_send_oomadj(pid, OOM_ADJ_VALUE_DEFAULT) < 0){
82                         _E("failed to send oom dbus signal");
83                 }
84         }
85 }
86
87
88
89 static Eina_Bool _launch_taskmgr_cb(void* data)
90 {
91         int val = -1;
92
93         _D("Launch TASKMGR");
94
95         key_info.home_long_press_timer = NULL;
96
97         if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &val) < 0) {
98                 _E("Cannot get VCONFKEY for lock state");
99         } else if (VCONFKEY_IDLE_LOCK == val) {
100                 _E("lock state, ignore home key long press..!!");
101                 return ECORE_CALLBACK_CANCEL;
102         }
103
104         process_mgr_must_launch(APPID_TASKMGR, NULL, NULL, NULL, _after_launch_taskmgr);
105
106         return ECORE_CALLBACK_CANCEL;
107 }
108
109
110
111 static void _release_multimedia_key(const char *value)
112 {
113         ret_if(NULL == value);
114         _D("Multimedia key is released with %s", value);
115         process_mgr_must_launch(APPID_MUSIC_PLAYER, "multimedia_key", value, NULL, NULL);
116 }
117
118
119
120 static Eina_Bool _launch_by_home_key(void *data)
121 {
122         int ret = 0;
123
124         if (status_passive_get()->idle_lock_state > VCONFKEY_IDLE_UNLOCK) {
125                 return ECORE_CALLBACK_CANCEL;
126         }
127
128         ret = home_mgr_open_home(NULL);
129         if(ret > 0) {
130                 dbus_util_send_home_raise_signal();
131         }
132
133         return ECORE_CALLBACK_CANCEL;
134 }
135
136
137
138 static Eina_Bool _home_multi_press_timer_cb(void *data)
139 {
140         _W("homekey count[%d]", key_info.homekey_count);
141
142         key_info.home_multi_press_timer = NULL;
143
144         if(0 == key_info.homekey_count % 2) {
145                 key_info.homekey_count = 0;
146                 return ECORE_CALLBACK_CANCEL;
147         } else if(key_info.homekey_count >= 3) {
148                 key_info.homekey_count = 0;
149                 return ECORE_CALLBACK_CANCEL;
150         }
151
152         /* Single homekey operation */
153         key_info.homekey_count = 0;
154         _launch_by_home_key(data);
155
156         return ECORE_CALLBACK_CANCEL;
157
158 }
159
160
161
162 #define SERVICE_OPERATION_POPUP_SEARCH "http://samsung.com/appcontrol/operation/search"
163 #define SEARCH_PKG_NAME "org.tizen.sfinder"
164 static int _launch_search(void)
165 {
166         app_control_h app_control;
167         int ret = APP_CONTROL_ERROR_NONE;
168
169         app_control_create(&app_control);
170         app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
171         app_control_set_app_id(app_control, SEARCH_PKG_NAME);
172
173         ret = app_control_send_launch_request(app_control, NULL, NULL);
174
175         if(ret != APP_CONTROL_ERROR_NONE) {
176                 _E("Cannot launch search!! err[%d]", ret);
177         }
178
179         app_control_destroy(app_control);
180         return ret;
181 }
182
183
184
185 static void _cancel_key_events(void)
186 {
187         key_info.homekey_count = 0;
188
189         if (key_info.home_long_press_timer) {
190                 ecore_timer_del(key_info.home_long_press_timer);
191                 key_info.home_long_press_timer = NULL;
192         }
193
194         if(key_info.home_multi_press_timer) {
195                 ecore_timer_del(key_info.home_multi_press_timer);
196                 key_info.home_multi_press_timer = NULL;
197         }
198 }
199
200
201
202 static Eina_Bool _key_release_cb(void *data, int type, void *event)
203 {
204         Evas_Event_Key_Up *ev = event;
205
206         retv_if(!ev, ECORE_CALLBACK_RENEW);
207         retv_if(!ev->keyname, ECORE_CALLBACK_RENEW);
208
209         _D("_key_release_cb : %s Released", ev->keyname);
210
211         /* Priority 1 : Cancel event */
212         if (!strcmp(ev->keyname, KEY_CANCEL)) {
213                 _D("CANCEL Key is released");
214                 key_info.cancel = EINA_FALSE;
215                 return ECORE_CALLBACK_RENEW;
216         }
217
218         if (EINA_TRUE == key_info.cancel) {
219                 _D("CANCEL is on");
220                 return ECORE_CALLBACK_RENEW;
221         }
222
223         /* Priority 2 : Execute before checking the lock status */
224         if (!strcmp(ev->keyname, KEY_MEDIA)) {
225                 _release_multimedia_key("KEY_PLAYCD");
226                 return ECORE_CALLBACK_RENEW;
227         }
228
229         /* Priority 3 : Check the lock status */
230         if ((status_passive_get()->idle_lock_state  == VCONFKEY_IDLE_LOCK)
231                 && (status_active_get()->setappl_screen_lock_type_int > SETTING_SCREEN_LOCK_TYPE_NONE)) {
232                 _D("phone lock state, ignore home key.");
233                 return ECORE_CALLBACK_RENEW;
234         }
235
236         /* Priority 4 : These keys are only activated after checking the lock state */
237         if (!strcmp(ev->keyname, KEY_END)) {
238         } else if (!strcmp(ev->keyname, KEY_CONFIG)) {
239         } else if (!strcmp(ev->keyname, KEY_SEND)) {
240         } else if (!strcmp(ev->keyname, KEY_HOME)) {
241                 _W("Home Key is released");
242
243                 syspopup_destroy_all();
244
245                 if(key_info.home_multi_press_timer) {
246                         _D("delete homekey timer");
247                         ecore_timer_del(key_info.home_multi_press_timer);
248                         key_info.home_multi_press_timer = NULL;
249                 }
250
251                 if (key_info.home_long_press_timer) {
252                         ecore_timer_del(key_info.home_long_press_timer);
253                         key_info.home_long_press_timer = NULL;
254                 } else {
255                         key_info.homekey_count = 0;
256                         return ECORE_CALLBACK_RENEW;
257                 }
258
259                 key_info.home_multi_press_timer = ecore_timer_add(HOMEKEY_TIMER_SEC, _home_multi_press_timer_cb, NULL);
260                 if (!key_info.home_multi_press_timer) {
261                         _E("Critical! cannot add a timer for home multi press");
262                 }
263                 return ECORE_CALLBACK_RENEW;
264         } else if (!strcmp(ev->keyname, KEY_PAUSE)) {
265         } else if (!strcmp(ev->keyname, KEY_APPS)) {
266                 _D("App tray key is released");
267         } else if (!strcmp(ev->keyname, KEY_TASKSWITCH)) {
268                 _D("Task switch key is released");
269                 _launch_taskmgr_cb(NULL);
270         } else if (!strcmp(ev->keyname, KEY_WEBPAGE)) {
271                 _D("Web page key is released");
272                 process_mgr_must_open(APPID_BROWSER, NULL, NULL);
273         } else if (!strcmp(ev->keyname, KEY_MAIL)) {
274                 _D("Mail key is released");
275                 process_mgr_must_open(APPID_EMAIL, NULL, NULL);
276         } else if (!strcmp(ev->keyname, KEY_CONNECT)) {
277                 _D("Connect key is released");
278                 process_mgr_must_open(APPID_DIALER, NULL, NULL);
279         } else if (!strcmp(ev->keyname, KEY_SEARCH)) {
280                 _D("Search key is released");
281                 if (_launch_search() < 0) {
282                         _E("Failed to launch the search");
283                 }
284         } else if (!strcmp(ev->keyname, KEY_VOICE)) {
285                 _D("Voice key is released");
286         }
287
288         return ECORE_CALLBACK_RENEW;
289 }
290
291
292
293 static Eina_Bool _key_press_cb(void *data, int type, void *event)
294 {
295         Evas_Event_Key_Down *ev = event;
296
297         retv_if(!ev, ECORE_CALLBACK_RENEW);
298         retv_if(!ev->keyname, ECORE_CALLBACK_RENEW);
299
300         _D("_key_press_cb : %s Pressed", ev->keyname);
301
302         /* Priority 1 : Cancel */
303         /* every reserved events have to be canceld when cancel key is pressed */
304         if (!strcmp(ev->keyname, KEY_CANCEL)) {
305                 _D("Cancel button is pressed");
306                 key_info.cancel = EINA_TRUE;
307                 _cancel_key_events();
308                 return ECORE_CALLBACK_RENEW;
309         }
310
311         if (EINA_TRUE == key_info.cancel) {
312                 _D("CANCEL is on");
313                 return ECORE_CALLBACK_RENEW;
314         }
315
316         /* Priority 2 : Check the lock status */
317         if ((status_passive_get()->idle_lock_state == VCONFKEY_IDLE_LOCK)
318                 && (status_active_get()->setappl_screen_lock_type_int > SETTING_SCREEN_LOCK_TYPE_NONE)) {
319                 _D("phone lock state, ignore key events.");
320                 _cancel_key_events();
321                 return ECORE_CALLBACK_RENEW;
322         }
323
324         /* Priority 3 : other keys */
325         if (!strcmp(ev->keyname, KEY_SEND)) {
326                 _D("Launch calllog");
327                 process_mgr_must_open(APPID_CALLLOG, NULL, NULL);
328         } else if(!strcmp(ev->keyname, KEY_CONFIG)) {
329                 _D("Launch camera");
330                 process_mgr_must_open(APPID_CAMERA, NULL, NULL);
331         } else if (!strcmp(ev->keyname, KEY_HOME)) {
332                 _W("Home Key is pressed");
333                 if (key_info.home_long_press_timer) {
334                         ecore_timer_del(key_info.home_long_press_timer);
335                         key_info.home_long_press_timer = NULL;
336                 }
337
338                 key_info.homekey_count++;
339                 _W("homekey count : %d", key_info.homekey_count);
340
341                 if(key_info.home_multi_press_timer) {
342                         ecore_timer_del(key_info.home_multi_press_timer);
343                         key_info.home_multi_press_timer = NULL;
344                         _D("delete homekey timer");
345                 }
346
347                 _D("create long press timer");
348                 key_info.home_long_press_timer = ecore_timer_add(LONG_PRESS_TIMER_SEC, _launch_taskmgr_cb, NULL);
349                 if (!key_info.home_long_press_timer) {
350                         _E("Failed to add timer for long press detection");
351                 }
352         } else if (!strcmp(ev->keyname, KEY_MEDIA)) {
353                 _D("Media key is pressed");
354         } else if (!strcmp(ev->keyname, KEY_APPS)) {
355                 _D("App tray key is pressed");
356         } else if (!strcmp(ev->keyname, KEY_TASKSWITCH)) {
357                 _D("Task switch key is pressed");
358         } else if (!strcmp(ev->keyname, KEY_WEBPAGE)) {
359                 _D("Web page key is pressed");
360         } else if (!strcmp(ev->keyname, KEY_MAIL)) {
361                 _D("Mail key is pressed");
362         } else if (!strcmp(ev->keyname, KEY_SEARCH)) {
363                 _D("Search key is pressed");
364         } else if (!strcmp(ev->keyname, KEY_VOICE)) {
365                 _D("Voice key is pressed");
366         } else if (!strcmp(ev->keyname, KEY_CONNECT)) {
367                 _D("Connect key is pressed");
368         }
369
370         return ECORE_CALLBACK_RENEW;
371 }
372
373
374
375 void _media_key_event_cb(media_key_e key, media_key_event_e status, void *user_data)
376 {
377         _D("MEDIA KEY EVENT : %d", key);
378         if (MEDIA_KEY_STATUS_PRESSED == status) return;
379
380         switch (key) {
381         case MEDIA_KEY_PAUSE:
382                 _release_multimedia_key("KEY_PAUSECD");
383                 break;
384         case MEDIA_KEY_PLAY:
385                 _release_multimedia_key("KEY_PLAYCD");
386                 break;
387         case MEDIA_KEY_PLAYPAUSE:
388                 _release_multimedia_key("KEY_PLAYPAUSECD");
389                 break;
390         default:
391                 _E("cannot reach here, key[%d]", key);
392                 break;
393         }
394 }
395
396
397
398 void hw_key_create_window(void)
399 {
400         int ret;
401         Ecore_X_Atom atomNotiWindow;
402         Ecore_X_Window keyrouter_notiwindow;
403
404         key_info.win = ecore_x_window_input_new(0, 0, 0, 1, 1);
405         if (!key_info.win) {
406                 _D("Failed to create hidden window");
407                 return;
408         }
409         ecore_x_event_mask_unset(key_info.win, ECORE_X_EVENT_MASK_NONE);
410         ecore_x_icccm_title_set(key_info.win, "menudaemon,key,receiver");
411         ecore_x_netwm_name_set(key_info.win, "menudaemon,key,receiver");
412         ecore_x_netwm_pid_set(key_info.win, getpid());
413
414         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_HOME, SHARED_GRAB);
415         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_VOLUMEDOWN, SHARED_GRAB);
416         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_VOLUMEUP, SHARED_GRAB);
417         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_CONFIG, SHARED_GRAB);
418         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_MEDIA, SHARED_GRAB);
419         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_APPS, SHARED_GRAB);
420         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_TASKSWITCH, SHARED_GRAB);
421         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_WEBPAGE, SHARED_GRAB);
422         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_MAIL, SHARED_GRAB);
423         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_SEARCH, SHARED_GRAB);
424         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_VOICE, SHARED_GRAB);
425         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_CONNECT, SHARED_GRAB);
426         utilx_grab_key(ecore_x_display_get(), key_info.win, KEY_POWER, SHARED_GRAB);
427
428         key_info.key_up = ecore_event_handler_add(ECORE_EVENT_KEY_UP, _key_release_cb, NULL);
429         if (!key_info.key_up)
430                 _E("Failed to register a key up event handler");
431
432         key_info.key_down = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, _key_press_cb, NULL);
433         if (!key_info.key_down)
434                 _E("Failed to register a key down event handler");
435
436         /* Get notifwindow */
437         atomNotiWindow = ecore_x_atom_get(STR_ATOM_KEYROUTER_NOTIWINDOW);
438         ret = ecore_x_window_prop_window_get(ecore_x_window_root_first_get(), atomNotiWindow, &keyrouter_notiwindow, 1);
439         if (ret > 0) {
440                 _D("Succeed to get keyrouter notiwindow ! ret = %d (win=0x%x)\n"
441                                 , ret, keyrouter_notiwindow);
442                 ecore_x_window_sniff(keyrouter_notiwindow);
443                 key_info.keyrouter_notiwindow = keyrouter_notiwindow;
444         } else {
445                 _E("Failed to get keyrouter notiwindow! ret = %d, atomNotiWindow = 0x%x, keyrouter_notiwindow = 0x%x"
446                                 , ret, atomNotiWindow, keyrouter_notiwindow);
447         }
448
449         media_key_reserve(_media_key_event_cb, NULL);
450 }
451
452
453
454 void hw_key_destroy_window(void)
455 {
456         utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_HOME);
457         utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_VOLUMEDOWN);
458         utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_VOLUMEUP);
459         utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_CONFIG);
460         utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_MEDIA);
461         utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_APPS);
462         utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_TASKSWITCH);
463         utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_WEBPAGE);
464         utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_MAIL);
465         utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_SEARCH);
466         utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_VOICE);
467         utilx_ungrab_key(ecore_x_display_get(), key_info.win, KEY_CONNECT);
468
469         if (key_info.key_up) {
470                 ecore_event_handler_del(key_info.key_up);
471                 key_info.key_up = NULL;
472         }
473
474         if (key_info.key_down) {
475                 ecore_event_handler_del(key_info.key_down);
476                 key_info.key_down = NULL;
477         }
478
479         ecore_x_window_delete_request_send(key_info.win);
480         key_info.win = 0x0;
481
482         media_key_release();
483 }
484
485 #elif HAVE_WAYLAND
486
487 #include <app.h>
488 #include <bundle.h>
489 #include <Elementary.h>
490 #include <Ecore.h>
491 #include <Ecore_Wayland.h>
492 #include <Ecore_Input.h>
493 #include <dd-deviced.h>
494 #include <syspopup_caller.h>
495 #include <vconf.h>
496 #include <system/media_key.h>
497 #include <aul.h>
498 #include <feedback.h>
499 #include <system_settings.h>
500
501 #include "hw_key.h"
502 #include "home_mgr.h"
503 #include "util.h"
504 #include "dbus_util.h"
505 #include "lock_mgr.h"
506 #include "status.h"
507 #include "process_mgr.h"
508
509 #define APPID_CAMERA "org.tizen.camera-app"
510 #define APPID_CALLLOG "org.tizen.calllog"
511 #define APPID_MUSIC_PLAYER "org.tizen.music-player"
512 #define APPID_TASKMGR "org.tizen.task-mgr"
513 #define APPID_BROWSER "org.tizen.browser"
514 #define APPID_EMAIL "org.tizen.email"
515 #define APPID_DIALER "org.tizen.phone"
516
517 #define STR_ATOM_KEYROUTER_NOTIWINDOW "_KEYROUTER_NOTIWINDOW"
518
519 #define LONG_PRESS_TIMER_SEC 0.4
520 #define HOMEKEY_TIMER_SEC 0.2
521 #define CANCEL_KEY_TIMER_SEC 0.3
522
523
524 const char *key_name[KEY_NAME_MAX] = {
525         "XF86AudioRaiseVolume",
526         "XF86AudioLowerVolume",
527         "XF86PowerOff",
528         "XF86Menu",
529         "XF86Home",
530         "XF86Back",
531         "XF86Camera",
532         "XF86Camera_Full",
533         "XF86Search",
534         "XF86AudioPlay",
535         "XF86AudioPause",
536         "XF86AudioStop",
537         "XF86AudioNext",
538         "XF86AudioPrev",
539         "XF86AudioRewind",
540         "XF86AudioForward",
541         "XF86AudioMedia",
542         "XF86AudioPlayPause",
543         "XF86AudioMute",
544         "XF86AudioRecord",
545         "Cancel",
546         "XF86SoftKBD",
547         "XF86QuickPanel",
548         "XF86TaskPane",
549         "XF86HomePage",
550         "XF86WWW",
551         "XF86Mail",
552         "XF86ScreenSaver",
553         "XF86MonBrightnessDown",
554         "XF86MonBrightnessUp",
555         "XF86Voice",
556         "Hangul",
557         "XF86Apps",
558         "XF86Call",
559         "XF86Game",
560         "XF86VoiceWakeUp_LPSD",
561         "XF86VoiceWakeUp",
562 };
563
564
565 static struct {
566         Ecore_Event_Handler *key_up;
567         Ecore_Event_Handler *key_down;
568         Ecore_Timer *home_long_press_timer;
569         Ecore_Timer *home_multi_press_timer;
570         Ecore_Timer *keygrab_timer;
571         Eina_Bool cancel;
572         int homekey_count;
573 } key_info = {
574         .key_up = NULL,
575         .key_down = NULL,
576         .home_long_press_timer = NULL,
577         .home_multi_press_timer = NULL,
578         .keygrab_timer = NULL,
579         .cancel = EINA_FALSE,
580         .homekey_count = 0,
581 };
582
583
584
585
586 static void _cancel_key_events(void)
587 {
588         key_info.homekey_count = 0;
589
590         if (key_info.home_long_press_timer) {
591                 ecore_timer_del(key_info.home_long_press_timer);
592                 key_info.home_long_press_timer = NULL;
593         }
594
595         if(key_info.home_multi_press_timer) {
596                 ecore_timer_del(key_info.home_multi_press_timer);
597                 key_info.home_multi_press_timer = NULL;
598         }
599 }
600
601
602
603 #define SERVICE_OPERATION_POPUP_SEARCH "http://samsung.com/appcontrol/operation/search"
604 #define SEARCH_PKG_NAME "org.tizen.sfinder"
605 static int _launch_search(void)
606 {
607         app_control_h app_control;
608         int ret = APP_CONTROL_ERROR_NONE;
609
610         app_control_create(&app_control);
611         app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
612         app_control_set_app_id(app_control, SEARCH_PKG_NAME);
613
614         ret = app_control_send_launch_request(app_control, NULL, NULL);
615
616         if(ret != APP_CONTROL_ERROR_NONE) {
617                 _E("Cannot launch search!! err[%d]", ret);
618         }
619
620         app_control_destroy(app_control);
621         return ret;
622 }
623
624
625
626 static void _after_launch_taskmgr(int pid)
627 {
628         if(0 < pid) {
629                 if(dbus_util_send_oomadj(pid, OOM_ADJ_VALUE_DEFAULT) < 0){
630                         _E("failed to send oom dbus signal");
631                 }
632         }
633 }
634
635
636
637 static Eina_Bool _launch_taskmgr_cb(void* data)
638 {
639         int val = -1;
640
641         _D("Launch TASKMGR");
642
643         key_info.home_long_press_timer = NULL;
644
645         if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &val) < 0) {
646                 _E("Cannot get VCONFKEY for lock state");
647         } else if (VCONFKEY_IDLE_LOCK == val) {
648                 _E("lock state, ignore home key long press..!!");
649                 return ECORE_CALLBACK_CANCEL;
650         }
651
652         process_mgr_must_launch(APPID_TASKMGR, NULL, NULL, NULL, _after_launch_taskmgr);
653
654         return ECORE_CALLBACK_CANCEL;
655 }
656
657
658
659 static Eina_Bool _launch_by_home_key(void *data)
660 {
661         int ret = 0;
662
663         if (status_passive_get()->idle_lock_state > VCONFKEY_IDLE_UNLOCK) {
664                 return ECORE_CALLBACK_CANCEL;
665         }
666
667         ret = home_mgr_open_home(NULL);
668         if(ret > 0) {
669                 dbus_util_send_home_raise_signal();
670         }
671
672         return ECORE_CALLBACK_CANCEL;
673 }
674
675
676
677 static Eina_Bool _home_multi_press_timer_cb(void *data)
678 {
679         _W("homekey count[%d]", key_info.homekey_count);
680
681         key_info.home_multi_press_timer = NULL;
682
683         if(0 == key_info.homekey_count % 2) {
684                 key_info.homekey_count = 0;
685                 return ECORE_CALLBACK_CANCEL;
686         } else if(key_info.homekey_count >= 3) {
687                 key_info.homekey_count = 0;
688                 return ECORE_CALLBACK_CANCEL;
689         }
690
691         /* Single homekey operation */
692         key_info.homekey_count = 0;
693         _launch_by_home_key(data);
694
695         return ECORE_CALLBACK_CANCEL;
696
697 }
698
699
700
701 static void _release_multimedia_key(const char *value)
702 {
703         ret_if(NULL == value);
704         _D("Multimedia key is released with %s", value);
705         process_mgr_must_launch(APPID_MUSIC_PLAYER, "multimedia_key", value, NULL, NULL);
706 }
707
708
709
710 static Eina_Bool _key_release_cb(void *data, int type, void *event)
711 {
712         Evas_Event_Key_Up *ev = event;
713
714         retv_if(!ev, ECORE_CALLBACK_RENEW);
715         retv_if(!ev->keyname, ECORE_CALLBACK_RENEW);
716
717         _D("_key_release_cb : %s Released", ev->keyname);
718
719         /* Priority 1 : Cancel event */
720         if (!strcmp(ev->keyname, key_name[KEY_CANCEL])) {
721                 _D("CANCEL Key is released");
722                 key_info.cancel = EINA_FALSE;
723                 return ECORE_CALLBACK_RENEW;
724         }
725
726         if (EINA_TRUE == key_info.cancel) {
727                 _D("CANCEL is on");
728                 return ECORE_CALLBACK_RENEW;
729         }
730
731         /* Priority 2 : Execute before checking the lock status */
732         if (!strcmp(ev->keyname, key_name[KEY_MEDIA])) {
733                 _release_multimedia_key("KEY_PLAYCD");
734                 return ECORE_CALLBACK_RENEW;
735         }
736
737         /* Priority 3 : Check the lock status */
738         if ((status_passive_get()->idle_lock_state  == VCONFKEY_IDLE_LOCK)
739                 && (status_active_get()->setappl_screen_lock_type_int > SETTING_SCREEN_LOCK_TYPE_NONE)) {
740                         _D("phone lock state, ignore home key.");
741                 return ECORE_CALLBACK_RENEW;
742         }
743
744         /* Priority 4 : These keys are only activated after checking the lock state */
745 #if 0
746         if (!strcmp(ev->keyname, key_name[KEY_END])) {
747         } else
748 #endif
749         if (!strcmp(ev->keyname, key_name[KEY_CONFIG])) {
750         //} else if (!strcmp(ev->keyname, key_name[KEY_SEND])) {
751         } else if (!strcmp(ev->keyname, key_name[KEY_HOME])) {
752                 _W("Home Key is released");
753
754                 syspopup_destroy_all();
755
756                 if(key_info.home_multi_press_timer) {
757                         _D("delete homekey timer");
758                         ecore_timer_del(key_info.home_multi_press_timer);
759                         key_info.home_multi_press_timer = NULL;
760                 }
761
762                 if (key_info.home_long_press_timer) {
763                         ecore_timer_del(key_info.home_long_press_timer);
764                         key_info.home_long_press_timer = NULL;
765                 } else {
766                         key_info.homekey_count = 0;
767                         return ECORE_CALLBACK_RENEW;
768                 }
769
770                 key_info.home_multi_press_timer = ecore_timer_add(HOMEKEY_TIMER_SEC, _home_multi_press_timer_cb, NULL);
771                 if (!key_info.home_multi_press_timer) {
772                         _E("Critical! cannot add a timer for home multi press");
773                 }
774                 return ECORE_CALLBACK_RENEW;
775         //} else if (!strcmp(ev->keyname, key_name[KEY_PAUSE])) {
776         } else if (!strcmp(ev->keyname, key_name[KEY_APPS])) {
777                 _D("App tray key is released");
778         } else if (!strcmp(ev->keyname, key_name[KEY_TASKSWITCH])) {
779                 _D("Task switch key is released");
780                 _launch_taskmgr_cb(NULL);
781         } else if (!strcmp(ev->keyname, key_name[KEY_WEBPAGE])) {
782                 _D("Web page key is released");
783                 process_mgr_must_open(APPID_BROWSER, NULL, NULL);
784         } else if (!strcmp(ev->keyname, key_name[KEY_MAIL])) {
785                 _D("Mail key is released");
786                 process_mgr_must_open(APPID_EMAIL, NULL, NULL);
787         } else if (!strcmp(ev->keyname, key_name[KEY_CONNECT])) {
788                 _D("Connect key is released");
789                 process_mgr_must_open(APPID_DIALER, NULL, NULL);
790         } else if (!strcmp(ev->keyname, key_name[KEY_SEARCH])) {
791                 _D("Search key is released");
792                 if (_launch_search() < 0) {
793                         _E("Failed to launch the search");
794                 }
795         } else if (!strcmp(ev->keyname, key_name[KEY_VOICE])) {
796                 _D("Voice key is released");
797         }
798
799         return ECORE_CALLBACK_RENEW;
800 }
801
802
803
804 static Eina_Bool _key_press_cb(void *data, int type, void *event)
805 {
806         Evas_Event_Key_Down *ev = event;
807
808         retv_if(!ev, ECORE_CALLBACK_RENEW);
809         retv_if(!ev->keyname, ECORE_CALLBACK_RENEW);
810
811         _D("_key_press_cb : %s Pressed", ev->keyname);
812
813         /* Priority 1 : Cancel */
814         /*              every reserved events have to be canceld when cancel key is pressed */
815         if (!strcmp(ev->keyname, key_name[KEY_CANCEL])) {
816                 _D("Cancel button is pressed");
817                 key_info.cancel = EINA_TRUE;
818                 _cancel_key_events();
819                 return ECORE_CALLBACK_RENEW;
820         }
821
822         if (EINA_TRUE == key_info.cancel) {
823                 _D("CANCEL is on");
824                 return ECORE_CALLBACK_RENEW;
825         }
826
827         /* Priority 2 : Check the lock status */
828         if ((status_passive_get()->idle_lock_state == VCONFKEY_IDLE_LOCK)
829                 && (status_active_get()->setappl_screen_lock_type_int > SETTING_SCREEN_LOCK_TYPE_NONE)) {
830                 _D("phone lock state, ignore key events.");
831                 _cancel_key_events();
832                 return ECORE_CALLBACK_RENEW;
833         }
834
835         /* Priority 3 : other keys */
836 #if 0
837         if (!strcmp(ev->keyname, key_name[KEY_SEND])) {
838                 _D("Launch calllog");
839                 process_mgr_must_open(APPID_CALLLOG, NULL, NULL);
840         } else
841 #endif
842         if(!strcmp(ev->keyname, key_name[KEY_CONFIG])) {
843                 _D("Launch camera");
844                 process_mgr_must_open(APPID_CAMERA, NULL, NULL);
845         } else if (!strcmp(ev->keyname, key_name[KEY_HOME])) {
846                 _W("Home Key is pressed");
847                 if (key_info.home_long_press_timer) {
848                         ecore_timer_del(key_info.home_long_press_timer);
849                         key_info.home_long_press_timer = NULL;
850                 }
851
852                 key_info.homekey_count++;
853                 _W("homekey count : %d", key_info.homekey_count);
854
855                 if(key_info.home_multi_press_timer) {
856                         ecore_timer_del(key_info.home_multi_press_timer);
857                         key_info.home_multi_press_timer = NULL;
858                         _D("delete homekey timer");
859                 }
860
861                 _D("create long press timer");
862                 key_info.home_long_press_timer = ecore_timer_add(LONG_PRESS_TIMER_SEC, _launch_taskmgr_cb, NULL);
863                 if (!key_info.home_long_press_timer) {
864                         _E("Failed to add timer for long press detection");
865                 }
866         } else if (!strcmp(ev->keyname, key_name[KEY_MEDIA])) {
867                 _D("Media key is pressed");
868         } else if (!strcmp(ev->keyname, key_name[KEY_APPS])) {
869                 _D("App tray key is pressed");
870         } else if (!strcmp(ev->keyname, key_name[KEY_TASKSWITCH])) {
871                 _D("Task switch key is pressed");
872         } else if (!strcmp(ev->keyname, key_name[KEY_WEBPAGE])) {
873                 _D("Web page key is pressed");
874         } else if (!strcmp(ev->keyname, key_name[KEY_MAIL])) {
875                 _D("Mail key is pressed");
876         } else if (!strcmp(ev->keyname, key_name[KEY_SEARCH])) {
877                 _D("Search key is pressed");
878         } else if (!strcmp(ev->keyname, key_name[KEY_VOICE])) {
879                 _D("Voice key is pressed");
880         } else if (!strcmp(ev->keyname, key_name[KEY_CONNECT])) {
881                 _D("Connect key is pressed");
882         }
883
884         return ECORE_CALLBACK_RENEW;
885 }
886
887
888
889 static Eina_Bool __keygrab_timer_cb(void *data)
890 {
891         int i = 0;
892         int ret = 0;
893
894         for (i = 0; i < KEY_NAME_MAX; i++) {
895                 ret = ecore_wl_window_keygrab_set(NULL, key_name[i], 0, 0, 0, ECORE_WL_WINDOW_KEYGRAB_SHARED);
896                 _D("key grab : %s / ret : %d", key_name[i], ret);
897         }
898
899         key_info.key_up = ecore_event_handler_add(ECORE_EVENT_KEY_UP, _key_release_cb, NULL);
900         if (!key_info.key_up) {
901                 _E("Failed to register a key up event handler");
902         }
903
904         key_info.key_down = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, _key_press_cb, NULL);
905         if (!key_info.key_down) {
906                 _E("Failed to register a key down event handler");
907         }
908
909         return ECORE_CALLBACK_CANCEL;
910 }
911
912
913
914 void hw_key_create_window(void)
915 {
916         if (key_info.keygrab_timer) {
917                 ecore_timer_del(key_info.keygrab_timer);
918                 key_info.keygrab_timer = NULL;
919         }
920
921         key_info.keygrab_timer = ecore_timer_add(1.0f, __keygrab_timer_cb, NULL);
922         if (!key_info.keygrab_timer) {
923                 _E("Failed to add timer for keygrab");
924         }
925 }
926
927
928
929 void hw_key_destroy_window(void)
930 {
931         int i = 0;
932
933         for (i = 0; i < KEY_NAME_MAX; i++) {
934                 ecore_wl_window_keygrab_unset(NULL, key_name[i], 0, 0);
935         }
936
937         if (key_info.keygrab_timer) {
938                 ecore_timer_del(key_info.keygrab_timer);
939                 key_info.keygrab_timer = NULL;
940         }
941
942         if (key_info.key_up) {
943                 ecore_event_handler_del(key_info.key_up);
944                 key_info.key_up = NULL;
945         }
946
947         if (key_info.key_down) {
948                 ecore_event_handler_del(key_info.key_down);
949                 key_info.key_down = NULL;
950         }
951 }
952
953 #endif
954
955 // End of a file