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