Update minictrl and assistive_light
[apps/core/preloaded/quickpanel.git] / daemon / quickpanel-ui.c
1 /*
2  * Copyright (c) 2009-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
18
19 #include <stdio.h>
20 #include <signal.h>
21 #include <sys/utsname.h>
22 #include <Elementary.h>
23 #include <Ecore_Input.h>
24 #include <unistd.h>
25 #include <malloc.h>
26
27 #include <app.h>
28 #include <vconf.h>
29 #include <E_DBus.h>
30 #include <tapi_common.h>
31 #include <ITapiSim.h>
32 #include <tzsh_quickpanel_service.h>
33 #include <notification.h>
34 #include <sound_manager.h>
35 #include <media.h>
36 #include <system_settings.h>
37
38 #if defined(WINSYS_X11)
39 #include <X11/Xlib.h>
40 #include <utilX.h>
41 #include <Ecore_X.h>
42 #include <privilege-control.h>
43 #endif
44
45 /* quickpanel basics */
46 #include "common.h"
47 #include "common_uic.h"
48 #include "quickpanel-ui.h"
49 #include "modules.h"
50 #include "quickpanel_def.h"
51 #include "list_util.h"
52 #include "noti_node.h"
53 #include "vi_manager.h"
54 #include "pager.h"
55 #include "page_base.h"
56 #ifdef QP_ENABLE_PAGE_EDIT
57 #include "page_edit.h"
58 #else
59 #include "page_setting_all.h"
60 #endif
61
62 #include "sim_controller.h"
63 #include "noti.h"
64
65 /* services */
66 #include "keyboard.h"
67 #include "keyboard_x.h"
68 #include "uninstall.h"
69 #ifdef QP_REMINDER_ENABLE
70 #include "reminder.h"
71 #endif
72 #ifdef QP_EMERGENCY_MODE_ENABLE
73 #include "emergency_mode.h"
74 #endif
75 #include "minictrl.h"
76 #include "util-time.h"
77
78 #define QP_WINDOW_PRIO 300
79
80 static struct appdata *g_app_data = NULL;
81
82 #if defined(WINSYS_X11)
83 static Ecore_X_Atom E_ILLUME_ATOM_MV_QUICKPANEL_STATE;
84 #endif
85
86 static void _ui_rotate(void *data, int new_angle);
87 static void _ui_geometry_info_set(void *data);
88 static void _ui_handler_info_set(void *data);
89 static void _ui_efl_cache_flush(void *evas);
90
91 HAPI void *quickpanel_get_app_data(void)
92 {
93         return g_app_data;
94 }
95
96 /******************************************************************************
97  *
98  * UI
99  *
100  ****************************************************************************/
101 static void _ui_efl_cache_flush(void *evas)
102 {
103         int file_cache;
104         int collection_cache;
105         int image_cache;
106         int font_cache;
107
108         retif(evas == NULL, , "Evas is NULL\n");
109         file_cache = edje_file_cache_get();
110         collection_cache = edje_collection_cache_get();
111         image_cache = evas_image_cache_get(evas);
112         font_cache = evas_font_cache_get(evas);
113
114         edje_file_cache_set(file_cache);
115         edje_collection_cache_set(collection_cache);
116         evas_image_cache_set(evas, 0);
117         evas_font_cache_set(evas, 0);
118
119         evas_image_cache_flush(evas);
120         evas_render_idle_flush(evas);
121         evas_font_cache_flush(evas);
122
123         edje_file_cache_flush();
124         edje_collection_cache_flush();
125
126         edje_file_cache_set(file_cache);
127         edje_collection_cache_set(collection_cache);
128         evas_image_cache_set(evas, image_cache);
129         evas_font_cache_set(evas, font_cache);
130
131         elm_cache_all_flush();
132         malloc_trim(0);
133 }
134
135 static void _ui_handler_input_region_set(void *data, int contents_height)
136 {
137         struct appdata *ad = NULL;
138 #if defined(WINSYS_X11)
139         Ecore_X_Window xwin;
140         Ecore_X_Atom atom_window_input_region = 0;
141 #else
142         tzsh_region_h region;
143 #endif
144         unsigned int window_input_region[4] = {0,};
145
146         retif(data == NULL,  , "Invialid parameter!");
147         ad = data;
148         
149 #if defined(WINSYS_X11)
150         xwin = elm_win_xwindow_get(ad->win);
151 #else
152         region = tzsh_region_create(ad->tzsh);
153 #endif
154
155         switch (ad->angle) {
156         case 0:
157                 window_input_region[0] = 0; //X
158                 window_input_region[1] = contents_height; // Y
159                 window_input_region[2] = ad->win_width; // Width
160                 window_input_region[3] = ELM_SCALE_SIZE(QP_HANDLE_H); // height
161                 break;
162         case 90:
163                 window_input_region[0] = contents_height; //X
164                 window_input_region[1] = 0; // Y
165                 window_input_region[2] = ELM_SCALE_SIZE(QP_HANDLE_H); // Width
166                 window_input_region[3] = ad->win_height; // height
167                 break;
168         case 180:
169                 window_input_region[0] = 0; //X
170                 window_input_region[1] = ad->win_height - contents_height - ELM_SCALE_SIZE(QP_HANDLE_H); // Y
171                 window_input_region[2] = ad->win_width; // Width
172                 window_input_region[3] = ELM_SCALE_SIZE(QP_HANDLE_H); // height
173                 break;
174         case 270:
175                 window_input_region[0] = ad->win_width - contents_height - ELM_SCALE_SIZE(QP_HANDLE_H); //X
176                 window_input_region[1] = 0; // Y
177                 window_input_region[2] = ELM_SCALE_SIZE(QP_HANDLE_H); // Width
178                 window_input_region[3] = ad->win_height; // height
179                 break;
180         }
181
182         INFO("win_input_0:%d\nwin_input_1:%d\nwin_input_2:%d\nwin_input_3:%d\n"
183                         ,window_input_region[0]
184                         ,window_input_region[1]
185                         ,window_input_region[2]
186                         ,window_input_region[3]
187                 );
188
189 #if defined(WINSYS_X11)
190         atom_window_input_region = ecore_x_atom_get(STR_ATOM_WINDOW_INPUT_REGION);
191         ecore_x_window_prop_card32_set(xwin, atom_window_input_region, window_input_region, 4);
192 #else
193         tzsh_region_add(region, 0, contents_height, ad->win_width, ELM_SCALE_SIZE(QP_HANDLE_H));
194         tzsh_quickpanel_service_handler_region_set(ad->quickpanel_service, ad->angle, region);
195         tzsh_region_destroy(region);
196 #endif
197 }
198
199 static void _ui_handler_content_region_set(void *data, int contents_height)
200 {
201         struct appdata *ad = NULL;
202
203 #if defined(WINSYS_X11)
204         Ecore_X_Window xwin;
205         Ecore_X_Atom atom_window_contents_region = 0;
206 #else
207         tzsh_region_h region;
208 #endif
209         unsigned int window_contents_region[4] = {0,};
210
211         retif(data == NULL,  , "Invialid parameter!");
212         ad = data;
213
214 #if defined(WINSYS_X11)
215         xwin = elm_win_xwindow_get(ad->win);
216 #else
217         region = tzsh_region_create(ad->tzsh);
218 #endif
219
220         switch (ad->angle) {
221         case 0:
222                 window_contents_region[0] = 0; //X
223                 window_contents_region[1] = 0; // Y
224                 window_contents_region[2] = ad->win_width; // Width
225                 window_contents_region[3] = contents_height; // height
226                 break;
227         case 90:
228                 window_contents_region[0] = 0; //X
229                 window_contents_region[1] = 0; // Y
230                 window_contents_region[2] = contents_height; // Width
231                 window_contents_region[3] = ad->win_height; // height
232                 break;
233         case 180:
234                 window_contents_region[0] = 0; //X
235                 window_contents_region[1] = ad->win_height - contents_height; // Y
236                 window_contents_region[2] = ad->win_width; // Width
237                 window_contents_region[3] = contents_height; // height
238                 break;
239         case 270:
240                 window_contents_region[0] = ad->win_width - contents_height ; //X
241                 window_contents_region[1] = 0; // Y
242                 window_contents_region[2] = contents_height; // Width
243                 window_contents_region[3] = ad->win_height; // height
244                 break;
245         }
246
247         DBG("win_contents_0:%d\nwin_contents_1:%d\nwin_contents_2:%d\nwin_contents_3:%d\n"
248                         ,window_contents_region[0]
249                         ,window_contents_region[1]
250                         ,window_contents_region[2]
251                         ,window_contents_region[3]
252            );
253
254 #if defined(WINSYS_X11)
255         atom_window_contents_region = ecore_x_atom_get(STR_ATOM_WINDOW_CONTENTS_REGION);
256         ecore_x_window_prop_card32_set(xwin, atom_window_contents_region, window_contents_region, 4);
257 #else
258         tzsh_region_add(region, 0, contents_height, ad->win_width, ELM_SCALE_SIZE(QP_HANDLE_H));
259         tzsh_quickpanel_service_content_region_set(ad->quickpanel_service, ad->angle, region);
260         tzsh_region_destroy(region);
261 #endif
262 }
263
264 static void _ui_handler_info_set(void *data)
265 {
266         int contents_height = 0;
267         struct appdata *ad = NULL;
268
269         retif(data == NULL, , "data is NULL");
270         ad = data;
271
272         contents_height = ad->gl_distance_from_top + ad->gl_limit_height;
273
274         _ui_handler_input_region_set(ad, contents_height);
275         _ui_handler_content_region_set(ad, contents_height);
276 }
277
278 static void _ui_geometry_info_set(void *data)
279 {
280         struct appdata *ad = NULL;
281         int max_height_window = 0;
282         Evas_Coord genlist_y = 0;
283
284         retif(data == NULL, , "data is NULL");
285         ad = data;
286
287         if (ad->angle == 90 || ad->angle == 270 ) {
288                 max_height_window = ad->win_width;
289         } else {
290                 max_height_window = ad->win_height;
291         }
292
293         edje_object_part_geometry_get(_EDJ(ad->ly), "qp.base.list.swallow", NULL, &genlist_y, NULL, NULL);
294
295         ad->gl_distance_from_top = genlist_y;
296         ad->gl_distance_to_bottom = ELM_SCALE_SIZE(QP_HANDLE_H);
297         ad->gl_limit_height = max_height_window - ad->gl_distance_from_top - ad->gl_distance_to_bottom;
298 }
299
300 /*****************************************************************************
301  *
302  * ui rotation functions
303  *
304  ****************************************************************************/
305 static void _ui_rotation_wm_cb(void *data, Evas_Object *obj, void *event)
306 {
307         int angle = 0;
308         struct appdata *ad = data;
309         retif(ad == NULL, , "Invalid parameter!");
310
311         angle = elm_win_rotation_get((Evas_Object *)obj);
312
313         DBG("ROTATE:%d", angle);
314
315         quickpanel_minictrl_rotation_report(angle);
316
317         _ui_rotate(ad, angle);
318 }
319
320 static int _ui_rotation_angle_get(void *data)
321 {
322         struct appdata *ad = (struct appdata *)data;
323         retif(ad == NULL, 0, "Invalid parameter!");
324         retif(ad->win == NULL, 0, "Invalid parameter!");
325
326         return elm_win_rotation_get(ad->win);
327 }
328
329 static void _ui_handler_enable_set(Eina_Bool is_enable)
330 {
331         const char *signal = NULL;
332         struct appdata *ad = quickpanel_get_app_data();
333         retif(ad == NULL, , "invalid data.");
334         retif(ad->view_root == NULL, , "data is NULL");
335
336         if (is_enable == EINA_TRUE) {
337                 signal = "mouse,down,1";
338         } else {
339                 signal = "mouse,up,1";
340         }
341
342         elm_object_signal_emit(ad->view_root, signal, "qp.handler.bg");
343 }
344
345 static void _ui_rotation_handler(struct appdata *ad, int angle)
346 {
347         const char *signal = NULL;
348
349         retif(ad == NULL, , "data is NULL");
350         retif(ad->view_root == NULL, , "data is NULL");
351
352
353         if (angle == 90 || angle == 270) {
354                 signal = "quickpanel.landscape";
355         } else {
356                 signal = "quickpanel.portrait";
357         }
358
359         elm_object_signal_emit(ad->view_root, signal, "quickpanel.prog");
360 }
361
362 static void _ui_rotate(void *data, int new_angle)
363 {
364         struct appdata *ad = data;
365         retif(data == NULL, , "Invalid parameter!");
366
367         DBG("ROTATION: new:%d old:%d", new_angle, ad->angle);
368
369         if (new_angle == 0 || new_angle == 90 || new_angle == 180 || new_angle == 270) {
370                 if (new_angle != ad->angle) {
371                         ad->angle = new_angle;
372                         quickpanel_modules_refresh(ad);
373                         _ui_geometry_info_set(ad);
374                         _ui_handler_info_set(ad);
375                         _ui_rotation_handler(ad, ad->angle);
376                 }
377         }
378 }
379 #if 1
380 static int _tzsh_set(Evas_Object *win)
381 {
382         tzsh_h tzsh = NULL;
383         tzsh_quickpanel_service_h quickpanel_service = NULL;
384         tzsh_window tz_win;
385
386         retif(!win, QP_FAIL, "Invialid parameter!");
387
388         tzsh = tzsh_create(TZSH_TOOLKIT_TYPE_EFL);
389         retif(!tzsh, QP_FAIL, "tzsh_create ERROR!");
390
391         struct appdata *ad = quickpanel_get_app_data();
392
393         ad->tzsh = tzsh;
394
395         tz_win = elm_win_window_id_get(win);
396         if (!tz_win) {
397                 tzsh_destroy(tzsh);
398                 return QP_FAIL;
399         }
400
401         quickpanel_service = tzsh_quickpanel_service_create(tzsh, tz_win);
402         if (!quickpanel_service) {
403                 tzsh_destroy(tzsh);
404                 return QP_FAIL;
405         }
406         ad->quickpanel_service = quickpanel_service;
407
408         return QP_OK;
409 }
410
411 static void _tzsh_unset(void)
412 {
413         struct appdata *ad = quickpanel_get_app_data();
414
415         if (ad->quickpanel_service) {
416                 tzsh_quickpanel_service_destroy(ad->quickpanel_service);
417                 ad->quickpanel_service = NULL;
418         }
419
420         if (ad->tzsh) {
421                 tzsh_destroy(ad->tzsh);
422                 ad->tzsh = NULL;
423         }
424 }
425 #endif
426
427 /*****************************************************************************
428  *
429  * ui creation/deletion functions
430  *
431  ****************************************************************************/
432 static Evas_Object *_ui_window_add(const char *name, int prio)
433 {
434         Evas_Object *eo = NULL;
435
436         eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
437
438         if (eo != NULL) {
439                 elm_win_alpha_set(eo, EINA_TRUE);
440                 elm_win_indicator_mode_set(eo, ELM_WIN_INDICATOR_HIDE);
441
442                 elm_win_title_set(eo, name);
443                 elm_win_borderless_set(eo, EINA_TRUE);
444                 elm_win_autodel_set(eo, EINA_TRUE);
445
446                 /* set this window as a quickpanel */
447                 elm_win_quickpanel_set(eo, 1);
448                 elm_win_quickpanel_priority_major_set(eo, prio);
449
450                 if (elm_win_wm_rotation_supported_get(eo)) {
451                         int rots[4] = { 0, 90, 180, 270 };
452                         elm_win_wm_rotation_available_rotations_set(eo, rots, 4);
453                 }
454
455                 evas_object_show(eo);
456
457                 if( QP_OK != _tzsh_set(eo)) {
458                         ERR("Failed to set tzsh");
459                 }
460         }
461
462         return eo;
463 }
464
465 static int _ui_gui_create(void *data)
466 {
467         struct appdata *ad = data;
468         int w = 0, h = 0;
469         int initial_angle = 0;
470         Evas_Object *page_base = NULL;
471
472         retif(data == NULL, QP_FAIL, "Invialid parameter!");
473
474         ad->win = _ui_window_add("Quickpanel Window",
475                         QP_WINDOW_PRIO);
476         retif(ad->win == NULL, QP_FAIL, "Failed to create main window");
477         //build error
478         //elm_win_focus_allow_set(ad->win, EINA_TRUE);
479
480         evas_object_smart_callback_add(ad->win, "wm,rotation,changed",
481                         _ui_rotation_wm_cb, ad);
482
483         ad->background = elm_bg_add(ad->win);
484         if (ad->background  != NULL) {
485                 evas_object_size_hint_weight_set(ad->background,
486                                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
487                 elm_win_resize_object_add(ad->win, ad->background);
488                 evas_object_show(ad->background);
489         } else {
490                 ERR("failed to create background");
491         }
492
493         ad->view_root = quickpanel_uic_load_edj(ad->background,
494                         DEFAULT_EDJ, "quickpanel/root", 0);
495         retif(ad->view_root == NULL, QP_FAIL, "Failed to create main page");
496
497         Evas_Object *pager_scroller = quickpanel_pager_new(ad->view_root, NULL);
498         Evas_Object *pager_box = quickpanel_pager_view_get("BOX");
499
500         page_base = quickpanel_page_base_create(pager_box, NULL);
501         retif(page_base == NULL, QP_FAIL, "Failed to create main page");
502         ad->ly = quickpanel_page_base_view_get("LAYOUT");
503         retif(ad->ly == NULL, QP_FAIL, "Failed to create main page");
504
505         elm_box_pack_end(pager_box, page_base);
506         elm_win_resize_object_add(ad->win, ad->view_root);
507         elm_object_part_content_set(ad->view_root, "qp.root.swallow", pager_scroller);
508
509         /* get noti evas */
510         ad->evas = evas_object_evas_get(ad->win);
511         ad->list = quickpanel_page_base_view_get("BOX");
512         ad->scroller = quickpanel_page_base_view_get("SCROLLER");
513
514         //ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
515         elm_win_screen_size_get(ad->win, NULL, NULL, &w, &h);
516         evas_object_resize(ad->win, w, h);
517
518         ad->win_width = w;
519         ad->win_height = h;
520
521         _ui_geometry_info_set(ad);
522
523         initial_angle = _ui_rotation_angle_get(ad);
524         _ui_rotate(ad, initial_angle);
525
526         quickpanel_pager_page_set(PAGE_IDX_MAIN, 1);
527
528         sim_controller_init(ad->ly);
529
530         quickpanel_noti_init_noti_section();
531
532         return 0;
533 }
534
535 static int _ui_gui_destroy(void *data)
536 {
537         struct appdata *ad = data;
538         retif(data == NULL, QP_FAIL, "Invialid parameter!");
539
540         if (ad->list != NULL) {
541                 evas_object_del(ad->list);
542                 ad->list = NULL;
543         }
544         if (ad->scroller != NULL) {
545                 evas_object_del(ad->scroller);
546                 ad->scroller = NULL;
547         }
548         if (ad->ly != NULL) {
549                 evas_object_del(ad->ly);
550                 ad->ly = NULL;
551         }
552         if (ad->win != NULL) {
553                 evas_object_del(ad->win);
554                 ad->win = NULL;
555         }
556
557         _tzsh_unset();
558
559         return QP_OK;
560 }
561
562 static void _ui_setting_visibility_set(struct appdata *ad, int show)
563 {
564         retif(ad == NULL, , "data is NULL");
565         retif(ad->ly == NULL, , "data is NULL");
566
567         elm_object_signal_emit(ad->ly, "quickpanel.setting.show",
568                         "quickpanel.prog");
569 }
570
571 /*****************************************************************************
572  *
573  * event handler initialization functions
574  *
575  ****************************************************************************/
576 static void _vconf_event_powerff_cb(keynode_t *node,
577                 void *data)
578 {
579         int val;
580         if (vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &val) == 0 &&
581                         (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || val == VCONFKEY_SYSMAN_POWER_OFF_RESTART)) {
582                 ui_app_exit();
583         }
584 }
585
586 static void _vconf_event_lcdoff_cb(keynode_t *node,
587                 void *data)
588 {
589         int ret = 0;
590         int pm_state = VCONFKEY_PM_STATE_NORMAL;
591
592         ret = vconf_get_int(VCONFKEY_PM_STATE, &pm_state);
593
594         if (ret == 0 && pm_state == VCONFKEY_PM_STATE_LCDOFF) {
595                 quickpanel_uic_close_quickpanel(false, 0);
596         }
597 }
598
599 #if defined(WINSYS_X11)
600 static Eina_Bool _ecore_event_client_message_cb(void *data, int type,
601                 void *event)
602 {
603         struct appdata *ad = data;
604
605         Ecore_X_Event_Client_Message *ev = event;
606
607         retif(data == NULL || event == NULL,
608                         ECORE_CALLBACK_RENEW, "Invalid parameter!");
609
610         if (ev->message_type == ECORE_X_ATOM_E_ILLUME_QUICKPANEL_STATE) {
611                 if (ev->data.l[0] == ECORE_X_ATOM_E_ILLUME_QUICKPANEL_OFF) {
612                         SERR("quickpanel is closed");
613                         ad->is_opened = 0;
614                         quickpanel_util_time_timer_enable_set(0);
615                         quickpanel_keyboard_closing_fini(ad);
616                         quickpanel_keyboard_x_closing_fini(ad);
617                         quickpanel_modules_closed(data);
618                         quickpanel_media_player_stop();
619                 }
620         } else if (ev->message_type == E_ILLUME_ATOM_MV_QUICKPANEL_STATE) {
621                 if (ev->data.l[0] == 1) {
622                         // pressed
623                         if (ad->is_opened == 0) {
624                                 quickpanel_util_time_timer_enable_set(1);
625                                 quickpanel_keyboard_openning_init(ad);
626                                 quickpanel_keyboard_x_openning_init(ad);
627                                 if (quickpanel_uic_opened_reason_get() != OPENED_BY_CMD_SHOW_SETTINGS) {
628                                         quickpanel_pager_page_set(PAGE_IDX_MAIN, 0);
629                                 }
630                         }
631                         _ui_handler_enable_set(EINA_TRUE);
632                 }
633                 if (ev->data.l[0] == 0) {
634                         // released
635                         if (ad->is_opened == 0) {
636                                 SERR("quickpanel is opened");
637                                 ad->is_opened = 1;
638                                 quickpanel_modules_opened(data);
639                                 quickpanel_media_player_stop();
640                                 quickpanel_uic_opened_reason_set(OPENED_NO_REASON);
641                         }
642                         _ui_handler_enable_set(EINA_FALSE);
643                 }
644         }
645         return ECORE_CALLBACK_RENEW;
646 }
647 #else
648 void _event_message_cb(void *data, Evas_Object *obj, void *event_info)
649  {
650         bool visiblity = (bool)event_info;
651         struct appdata *ad = data;
652
653         if(visiblity == 1) { // show
654                 DBG("quickpanel is opened");
655
656                 ad->is_opened = 1;
657                 quickpanel_util_time_timer_enable_set(1);
658                 quickpanel_keyboard_openning_init(ad);
659                 quickpanel_modules_opened(data);
660                 quickpanel_uic_opened_reason_set(OPENED_NO_REASON);
661         } else {
662                 DBG("quickpanel is closed");
663
664                 ad->is_opened = 0;
665                 quickpanel_util_time_timer_enable_set(0);
666                 quickpanel_keyboard_closing_fini(ad);
667                 quickpanel_modules_closed(data);
668         }
669         quickpanel_media_player_stop();
670  }
671 #endif
672
673 static void _vconf_init(struct appdata *ad)
674 {
675         int ret = 0;
676
677         ret = vconf_notify_key_changed(VCONFKEY_PM_STATE,
678                         _vconf_event_lcdoff_cb, ad);
679         if (ret != 0) {
680                 ERR("VCONFKEY_PM_STATE: %d", ret);
681         }
682
683         ret = vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
684                         _vconf_event_powerff_cb, ad);
685         if (ret != 0) {
686                 ERR("VCONFKEY_PM_STATE: %d", ret);
687         }
688 }
689
690 static void _vconf_fini(struct appdata *ad)
691 {
692         int ret = 0;
693
694         ret = vconf_ignore_key_changed(VCONFKEY_PM_STATE,
695                         _vconf_event_lcdoff_cb);
696         if (ret != 0) {
697                 ERR("VCONFKEY_PM_STATE: %d", ret);
698         }
699
700         ret = vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS,
701                         _vconf_event_powerff_cb);
702         if (ret != 0) {
703                 ERR("VCONFKEY_PM_STATE: %d", ret);
704         }
705 }
706
707 static void _edbus_init(struct appdata *ad)
708 {
709         e_dbus_init();
710         ad->dbus_connection = e_dbus_bus_get(DBUS_BUS_SYSTEM);
711         if (ad->dbus_connection == NULL) {
712                 ERR("noti register : failed to get dbus bus");
713         }
714 }
715
716 static void _edbus_fini(struct appdata *ad)
717 {
718         if (ad->dbus_connection != NULL) {
719                 e_dbus_connection_close(ad->dbus_connection);
720                 ad->dbus_connection = NULL;
721                 e_dbus_shutdown();
722         }
723 }
724
725 static void _ecore_event_init(struct appdata *ad)
726 {
727 #if defined(WINSYS_X11)
728         Ecore_Event_Handler *hdl = NULL;
729
730         /* Register window rotate event */
731         hdl = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE,
732                         _ecore_event_client_message_cb, ad);
733         if (hdl == NULL) {
734                 ERR("failed to add handler(ECORE_X_EVENT_CLIENT_MESSAGE)");
735         }
736
737         ad->hdl_client_message = hdl;
738 #else
739         DBG("");
740         evas_object_smart_callback_add(ad->win, "visibility,changed", _event_message_cb, ad);
741 #endif
742 }
743
744 static void _ecore_event_fini(struct appdata *ad)
745 {
746         if (ad->hdl_client_message != NULL) {
747                 ecore_event_handler_del(ad->hdl_client_message);
748                 ad->hdl_client_message = NULL;
749         }
750 }
751
752 static void _x_atom_init(void)
753 {
754 #if defined(WINSYS_X11)
755         E_ILLUME_ATOM_MV_QUICKPANEL_STATE = ecore_x_atom_get("_E_MOVE_QUICKPANEL_STATE");
756 #endif
757 }
758 /*****************************************************************************
759  *
760  * App efl main interface
761  *
762  ****************************************************************************/
763 static void _sigaction_terminate_handler(int signum, siginfo_t *info, void *unused)
764 {
765         ERR("quickpanel going to be terminated");
766         ui_app_exit();
767 }
768
769 static void _service_request_process(app_control_h service, void *data)
770 {
771         char *value = NULL;
772         retif(service == NULL, , "Invialid parameter!");
773
774         if (!app_control_get_extra_data(service, "HIDE_LAUNCH", &value))
775         {
776                 if (value != NULL) {
777                         ERR("HIDE_LAUNCH: %s", value);
778                         if (!strcmp(value, "1")) {
779                                 quickpanel_uic_close_quickpanel(false, 0);
780                         } else {
781                                 quickpanel_uic_open_quickpanel(OPENED_BY_CMD_HIDE_LAUNCH);
782                         }
783
784                         free(value);
785                 }
786         } else if (!app_control_get_extra_data(service, "SHOW_SETTINGS", &value)) {
787                 if (value != NULL) {
788                         ERR("SHOW_SETTINGS: %s", value);
789                         if (!strcmp(value, "1")) {
790                                 quickpanel_pager_page_set(PAGE_IDX_EDITING, 0);
791                                 quickpanel_uic_open_quickpanel(OPENED_BY_CMD_SHOW_SETTINGS);
792                         }
793
794                         free(value);
795                 }
796         }
797 #ifdef QP_EMERGENCY_MODE_ENABLE
798         else if (!app_control_get_extra_data(service, "EMERGENCY_MODE_LAUNCH", &value)) {
799                 if (value != NULL) {
800                         ERR("EMERGENCY_MODE_LAUNCH: %s", value);
801                         if (!strcmp(value, "1")) {
802                                 if (quickpanel_emergency_mode_syspopup_launch() == QP_FAIL) {
803                                         ERR("failed to launch emergency mode syspopup");
804                                 } else {
805                                         quickpanel_uic_close_quickpanel(true, 0);
806                                 }
807                         }
808
809                         free(value);
810                 }
811         }
812 #endif
813 }
814
815 static Eina_Bool _appcore_cache_flush_timer_cb(void *data)
816 {
817         if (!quickpanel_uic_is_suspended()) {
818                 return ECORE_CALLBACK_CANCEL;
819         }
820
821         return ECORE_CALLBACK_CANCEL;
822 }
823
824 static Eina_Bool _ui_refresh_idler_cb(void *data)
825 {
826         DBG("");
827         struct appdata *ad = data;
828         retif(ad == NULL, QP_FAIL, "Invalid parameter!");
829
830         quickpanel_modules_refresh(ad);
831         _ui_geometry_info_set(ad);
832         _ui_handler_info_set(ad);
833
834         /*      Cache memory is cleared when the application paused (every time, after 5 seconds (in appcore)),
835          *      but after running in a minimized mode application have status AS_RUNNING.
836          *      Application have status AS_PAUSED only after change of visibility to hidden condition by user (on hiding window)
837          *      Cleaning must be performed only once after application loading in hidden condition
838          *      (and stay in a hidden condition at time of cleaning).
839          */
840         ecore_timer_add(10, _appcore_cache_flush_timer_cb, NULL);
841
842         return EINA_FALSE;
843 }
844
845 static void _quickpanel_initialize(void *data)
846 {
847         int ret = 0;
848         struct appdata *ad = data;
849         retif(ad == NULL, , "Invialid parameter!");
850
851         INFO(">> Creating Quickpanel");
852         /* Check emulator */
853         ad->is_emul = quickpanel_uic_is_emul();
854         INFO("quickpanel run in %s", ad->is_emul ? "Emul" : "Device");
855
856         int w, h;
857         elm_win_screen_size_get(ad->win, NULL, NULL, &w, &h);
858         ad->scale = elm_config_scale_get();
859         if (ad->scale < 0) {
860                 ad->scale = 1.0;
861         }
862
863         INFO("quickpanel scale %f", ad->scale);
864
865         ad->is_suspended = 1;
866
867         /* Get theme */
868         elm_theme_extension_add(NULL, DEFAULT_THEME_EDJ);
869         /* create quickpanel window */
870         ret = _ui_gui_create(ad);
871         retif(ret != QP_OK, , "Failed to create window!");
872
873         quickpanel_media_init();
874
875         _x_atom_init();
876         _ecore_event_init(ad);
877         _vconf_init(ad);
878         _edbus_init(ad);
879
880         quickpanel_uninstall_init(ad);
881 #ifdef QP_EMERGENCY_MODE_ENABLE
882         quickpanel_emergency_mode_init(ad);
883 #endif
884         quickpanel_keyboard_init(ad);
885 #if defined(WINSYS_X11)
886         quickpanel_keyboard_x_init(ad);
887 #endif
888 #ifdef QP_REMINDER_ENABLE
889         quickpanel_reminder_init(ad);
890 #endif
891
892 #ifdef QP_SETTING_ENABLE
893         _ui_setting_visibility_set(ad, 1);
894 #else /* QP_SETTING_ENABLE */
895         _ui_setting_visibility_set(ad, 0);
896 #endif /* QP_SETTING_ENABLE */
897
898         /* init quickpanel modules */
899         quickpanel_modules_init(ad);
900         ecore_idler_add(_ui_refresh_idler_cb, ad);
901 }
902
903 static bool _app_create_cb(void *data)
904 {
905         ERR("");
906 #if defined(WINSYS_X11)
907         elm_config_engine_set("opengl_x11");
908 #endif
909         elm_app_base_scale_set(1.8);
910
911         pid_t pid;
912         int r;
913
914         // signal handler
915         struct sigaction act;
916         act.sa_sigaction = _sigaction_terminate_handler;
917         act.sa_flags = SA_SIGINFO;
918
919         int ret = sigemptyset(&act.sa_mask);
920         if (ret < 0) {
921                 ERR("Failed to sigemptyset[%s]", strerror(errno));
922         }
923         ret = sigaddset(&act.sa_mask, SIGTERM);
924         if (ret < 0) {
925                 ERR("Failed to sigaddset[%s]", strerror(errno));
926         }
927         ret = sigaction(SIGTERM, &act, NULL);
928         if (ret < 0) {
929                 ERR("Failed to sigaction[%s]", strerror(errno));
930         }
931
932         pid = setsid();
933         if (pid < 0) {
934                 WARN("Failed to set session id!");
935         }
936
937         r = nice(2);
938         if (r == -1) {
939                 WARN("Failed to set nice value!");
940         }
941
942         return TRUE;
943 }
944
945 static void _app_service_cb(app_control_h service, void *data)
946 {
947         struct appdata *ad = data;
948         retif(ad == NULL, , "Invialid parameter!");
949
950         if (ad->win == NULL && ad->ly == NULL) {
951                 _quickpanel_initialize(data);
952         } else {
953                 _service_request_process(service, data);
954         }
955 }
956
957 static void _app_terminate_cb(void *data)
958 {
959         ERR("");
960
961         struct appdata *ad = data;
962         retif(ad == NULL, , "invalid data.");
963
964         quickpanel_media_fini();
965
966         /* fini quickpanel modules */
967         quickpanel_modules_fini(ad);
968         _edbus_fini(ad);
969         _vconf_fini(ad);
970         _ecore_event_fini(ad);
971
972         quickpanel_keyboard_fini(ad);
973 #if defined(WINSYS_X11)
974         quickpanel_keyboard_x_fini(ad);
975 #endif
976         quickpanel_uninstall_fini(ad);
977 #ifdef QP_REMINDER_ENABLE
978         quickpanel_reminder_fini(ad);
979 #endif
980 #ifdef QP_EMERGENCY_MODE_ENABLE
981         quickpanel_emergency_mode_fini(ad);
982 #endif
983
984         /* delete quickpanel window */
985         _ui_gui_destroy(ad);
986
987         INFO("Quickpanel is terminated");
988 }
989
990 static void _app_resume_cb(void *data)
991 {
992         DBG("");
993         struct appdata *ad = data;
994         retif(ad == NULL,, "invalid data.");
995
996         ad->is_suspended = 0;
997         _ui_handler_enable_set(EINA_FALSE);
998
999         quickpanel_modules_resume(data);
1000
1001         sim_controller_resume();
1002 }
1003
1004 static void _app_pause_cb(void *data)
1005 {
1006         DBG("");
1007         struct appdata *ad = data;
1008         retif(ad == NULL,, "invalid data.");
1009
1010
1011         quickpanel_modules_suspend(ad);
1012
1013         ad->is_suspended = 1;
1014
1015         if (ad->evas != NULL) {
1016                 _ui_efl_cache_flush(ad->evas);
1017                 evas_event_feed_mouse_cancel(ad->evas, ecore_time_get(), NULL);
1018         }
1019 }
1020
1021 static void _app_language_changed_cb(app_event_info_h event_info, void *data)
1022 {
1023         DBG("");
1024         quickpanel_modules_lang_change(data);
1025
1026         sim_controller_on_language_change();
1027 }
1028
1029 static void _app_region_format_changed_cb(app_event_info_h event_info, void *data)
1030 {
1031         DBG("");
1032         quickpanel_modules_lang_change(data);
1033 }
1034
1035 int main(int argc, char *argv[])
1036 {
1037         INFO("BUILD START: %s %s", __DATE__, __TIME__);
1038         ERR("BUILD START: %s %s", __DATE__, __TIME__);
1039
1040         int ret = 0;
1041         struct appdata ad;
1042         ui_app_lifecycle_callback_s event_callback = {0,};
1043         app_event_handler_h handlers[5] = {NULL, };
1044
1045         ERR("quickpanel is forked");
1046
1047 #if defined(WINSYS_X11)
1048         ret = perm_app_set_privilege("org.tizen.", NULL, NULL);
1049         if (ret != 0) {
1050                 WARN("Failed to control privilege!");
1051         }
1052 #endif
1053
1054         event_callback.create = _app_create_cb;
1055         event_callback.terminate = _app_terminate_cb;
1056         event_callback.pause = _app_pause_cb;
1057         event_callback.resume = _app_resume_cb;
1058         event_callback.app_control = _app_service_cb;
1059
1060 //      ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, NULL, NULL);
1061 //      ui_app_add_event_handler(&handlers[APP_EVENT_LOW_MEMORY], APP_EVENT_LOW_MEMORY, NULL, NULL);
1062 //      ui_app_add_event_handler(&handlers[APP_EVENT_DEVICE_ORIENTATION_CHANGED], APP_EVENT_DEVICE_ORIENTATION_CHANGED, NULL, NULL);
1063         ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, _app_language_changed_cb, &ad);
1064         ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, _app_region_format_changed_cb, &ad);
1065
1066         memset(&ad, 0x0, sizeof(struct appdata));
1067
1068         g_app_data = &ad;
1069
1070         ret = ui_app_main(argc, argv, &event_callback, (void *)&ad);
1071         if (ret != APP_ERROR_NONE) {
1072                 ERR("ui_app_main() is failed. err = %d", ret);
1073         }
1074
1075         return ret;
1076
1077 }