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