Get rid of OPACITY_MODE.
[apps/core/preloaded/indicator-win.git] / src / main.c
1 /*
2  *  Indicator
3  *
4  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdio.h>
21 #include <app.h>
22 #include <vconf.h>
23 #include <unistd.h>
24 #include <app_manager.h>
25 #include <signal.h>
26 #include <minicontrol-monitor.h>
27 #include <feedback.h>
28 #include <notification.h>
29 #include <app_preference.h>
30 #include <wifi.h>
31 //FIXME
32 #if 0
33 #include <tzsh_indicator_service.h>
34 #endif
35 #if 0
36 #include <app_manager_product.h>
37 #endif
38
39 #include "common.h"
40 #include "box.h"
41 #include "icon.h"
42 #include "main.h"
43 #include "indicator_gui.h"
44 #include "modules.h"
45 #include "util.h"
46 #include "plmn.h"
47 #include "message.h"
48 #include "tts.h"
49 #include "log.h"
50 #include "indicator.h"
51 #include "ticker.h"
52
53 #define GRP_NAME "indicator"
54 #define WIN_TITLE "Illume Indicator"
55 #define VCONF_PHONE_STATUS "memory/startapps/sequence"
56
57 #define BUS_NAME       "org.tizen.system.deviced"
58 #define PATH_NAME    "/Org/Tizen/System/DeviceD/Display"
59 #define INTERFACE_NAME BUS_NAME".display"
60 #define MEMBER_NAME     "LCDOn"
61
62 #define MP_APP_ID "org.tizen.music-player-lite"
63 #define FMRADIO_APP_ID "org.tizen.fm-radio-lite"
64 #define VR_APP_ID "org.tizen.voicerecorder-lite"
65
66 #define STR_ATOM_MV_INDICATOR_GEOMETRY          "_E_MOVE_INDICATOR_GEOMETRY"
67
68 #define HIBERNATION_ENTER_NOTI  "HIBERNATION_ENTER"
69 #define HIBERNATION_LEAVE_NOTI  "HIBERNATION_LEAVE"
70
71 #define UNLOCK_ENABLED  0
72 #define TIMEOUT                 5
73
74 #ifdef HOME_KEY_EMULATION
75 /* Predefine string */
76 #define PROP_HWKEY_EMULATION "_HWKEY_EMULATION"
77 #define KEY_MSG_PREFIX_PRESS "P:"
78 #define KEY_MSG_PREFIX_RELEASE "R:"
79 #define KEY_MSG_PREFIX_PRESS_C "PC"
80 #define KEY_MSG_PREFIX_RELEASE_C "RC"
81
82 #ifndef KEY_HOME
83 #define KEY_HOME "XF86Phone"
84 #endif /* KEY_HOME */
85 #endif /* HOME_KEY_EMULATION */
86
87 //static E_DBus_Connection *edbus_conn=NULL;
88 //static E_DBus_Signal_Handler *edbus_handler=NULL;
89 static Eina_Bool home_button_pressed = EINA_FALSE;
90 static Eina_Bool show_hide_pressed = EINA_FALSE;
91 Evas_Coord_Point indicator_press_coord = {0,0};
92 Ecore_Timer *clock_timer;
93 int is_transparent = 0;
94 int current_angle = 0;
95 int current_state = 0;
96 static int bFirst_opacity = 1;
97
98 static struct _s_info {
99         Ecore_Timer *listen_timer;
100 } s_info = {
101         .listen_timer = NULL,
102 };
103
104
105 static indicator_error_e _start_indicator(void *data);
106 static indicator_error_e _terminate_indicator(void *data);
107
108 static void _indicator_low_bat_cb(app_event_info_h event_info, void *data);
109 static void _indicator_lang_changed_cb(app_event_info_h event_info, void *data);
110 static void _indicator_region_changed_cb(app_event_info_h event_info, void *data);
111 static void _indicator_window_delete_cb(void *data, Evas_Object * obj, void *event);
112 //static Eina_Bool _indicator_client_message_cb(void *data, int type, void *event);
113 static void _indicator_mouse_down_cb(void *data, Evas * e, Evas_Object * obj, void *event);
114 static void _indicator_mouse_move_cb(void *data, Evas * e, Evas_Object * obj, void *event);
115 static void _indicator_mouse_up_cb(void *data, Evas * e, Evas_Object * obj, void *event);
116
117
118 static void _indicator_low_bat_cb(app_event_info_h event_info, void *data)
119 {
120 }
121
122 static void _indicator_lang_changed_cb(app_event_info_h event_info, void *data)
123 {
124         modules_lang_changed(data);
125 }
126
127 static void _indicator_region_changed_cb(app_event_info_h event_info, void *data)
128 {
129         modules_region_changed(data);
130 }
131
132 static void _indicator_window_delete_cb(void *data, Evas_Object * obj, void *event)
133 {
134         ret_if(!data);
135
136         _terminate_indicator((struct appdata *)data);
137 }
138
139 static void _indicator_notify_pm_state_cb(keynode_t * node, void *data)
140 {
141         static int nMove = 0;
142         static int nIndex = 1;
143         int val = -1;
144
145         ret_if(!data);
146
147         if (vconf_get_int(VCONFKEY_PM_STATE, &val) < 0) return;
148
149         switch (val) {
150         case VCONFKEY_PM_STATE_LCDOFF:
151                 if (clock_timer != NULL) {
152                         ecore_timer_del(clock_timer);
153                         clock_timer = NULL;
154                 }
155         case VCONFKEY_PM_STATE_SLEEP: // lcd off 2
156                 /* FIXME */
157                 nMove = nMove+nIndex;
158                 if(nMove>=4)
159                         nIndex = -1;
160                 else if(nMove<=0)
161                         nIndex = 1;
162                 {
163                         char temp[30] = {0,};
164                         sprintf(temp,"indicator.padding.resize.%d",nMove);
165                         util_signal_emit(data,temp,"indicator.prog");
166                 }
167                 icon_set_update_flag(0);
168                 box_noti_ani_handle(0);
169                 break;
170         case VCONFKEY_PM_STATE_NORMAL:
171                 if (!icon_get_update_flag()) {
172                         icon_set_update_flag(1);
173                         box_noti_ani_handle(1);
174                         modules_wake_up(data);
175                 }
176                 break;
177         case VCONFKEY_PM_STATE_LCDDIM:
178         default:
179                 break;
180         }
181 }
182
183 static void _indicator_power_off_status_cb(keynode_t * node, void *data)
184 {
185         int val = -1;
186
187         ret_if(!data);
188
189         if (vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &val) < 0) return;
190
191         switch (val) {
192         case VCONFKEY_SYSMAN_POWER_OFF_DIRECT:
193         case VCONFKEY_SYSMAN_POWER_OFF_RESTART:
194                 ui_app_exit();
195                 break;
196         default:
197                 break;
198         }
199
200 }
201
202 static void _indicator_lock_status_cb(keynode_t * node, void *data)
203 {
204         static int lockstate = 0;
205         extern int clock_mode;
206         int val = -1;
207
208         ret_if(!data);
209
210         if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &val) < 0) return;
211         if (val == lockstate) return;
212
213         lockstate = val;
214
215         switch (val) {
216         case VCONFKEY_IDLE_UNLOCK:
217                 if (!clock_mode) util_signal_emit(data,"clock.font.12","indicator.prog");
218                 else util_signal_emit(data,"clock.font.24","indicator.prog");
219                 break;
220         case VCONFKEY_IDLE_LOCK:
221         case VCONFKEY_IDLE_LAUNCHING_LOCK:
222                 util_signal_emit(data,"clock.invisible","indicator.prog");
223                 break;
224         default:
225                 break;
226         }
227
228 }
229
230 static void _rotate_window(struct appdata *ad, int new_angle)
231 {
232         ret_if(!ad);
233
234         _D("Indicator angle is %d degree", new_angle);
235
236         current_angle = new_angle;
237
238         switch (new_angle) {
239                 case 0:
240                 case 180:
241                         evas_object_resize(ad->win.win, ad->win.port_w, ad->win.h);
242                         break;
243                 case 90:
244                 case 270:
245                         evas_object_resize(ad->win.win, ad->win.land_w, ad->win.h);
246                         break;
247                 default:
248                         break;
249         }
250 }
251
252 static void _change_opacity(void *data, enum indicator_opacity_mode mode)
253 {
254         struct appdata *ad = NULL;
255         const char *signal = NULL;
256         retif(data == NULL, , "Invalid parameter!");
257
258         ad = data;
259
260         if (bFirst_opacity==1) bFirst_opacity = 0;
261
262         switch (mode) {
263         case INDICATOR_OPACITY_OPAQUE:
264                 signal = "bg.opaque";
265                 ad->opacity_mode = mode;
266                 break;
267         case INDICATOR_OPACITY_TRANSLUCENT:
268                 signal = "bg.translucent";
269                 ad->opacity_mode = mode;
270                 break;
271         case INDICATOR_OPACITY_TRANSPARENT:
272                 signal = "bg.transparent";
273                 ad->opacity_mode = mode;
274                 break;
275         default:
276                 _E("unknown mode : %d", mode);
277                 signal = "bg.opaque";
278                 ad->opacity_mode = INDICATOR_OPACITY_OPAQUE;
279                 break;
280
281         }
282         util_signal_emit_by_win(&(ad->win),signal, "indicator.prog");
283 }
284 #if 0
285 static void _indicator_quickpanel_changed(void *data, int is_open)
286 {
287         int val = 0;
288
289         ret_if(!data);
290
291         if (vconf_get_int(VCONFKEY_IDLE_LOCK_STATE, &val) < 0) return;
292         if (val == VCONFKEY_IDLE_LOCK) return;
293 }
294 #endif
295
296 #if 0
297 static Eina_Bool _indicator_client_message_cb(void *data, int type, void *event)
298 {
299         Ecore_X_Event_Client_Message *ev = (Ecore_X_Event_Client_Message *) event;
300         struct appdata *ad = NULL;
301         ad = data;
302
303         retv_if(data == NULL || event == NULL, ECORE_CALLBACK_RENEW);
304         if (ev->message_type == ECORE_X_ATOM_E_ILLUME_QUICKPANEL_STATE) {
305                 if (ev->data.l[0] == ECORE_X_ATOM_E_ILLUME_QUICKPANEL_ON) {
306                         _indicator_quickpanel_changed(data, 1);
307                 } else if (ev->data.l[0] == ECORE_X_ATOM_E_ILLUME_QUICKPANEL_OFF) {
308                         _indicator_quickpanel_changed(data, 0);
309                 }
310         }
311
312         if (ev->message_type == ECORE_X_ATOM_E_ILLUME_ROTATE_WINDOW_ANGLE) {
313                 int new_angle = 0;
314                 if(ev->win != ad->active_indi_win) {
315                         return ECORE_CALLBACK_RENEW;
316                 }
317                 new_angle = ev->data.l[0];
318                 _rotate_window(ad, new_angle);
319         }
320         return EINA_TRUE;
321 }
322 #endif
323
324 static Eina_Bool _active_indicator_handle(void* data,int type)
325 {
326         int trans_mode = 0;
327         int angle = 0;
328 //      Ecore_X_Illume_Indicator_Opacity_Mode illume_opacity = 0;
329
330         retv_if(!data, EINA_FALSE);
331
332         struct appdata *ad = (struct appdata *)data;
333         switch (type) {
334         /* Opacity */
335         case 1:
336 #if 0
337                 illume_opacity = ecore_x_e_illume_indicator_opacity_get(ad->active_indi_win);
338
339                 switch(illume_opacity) {
340                 case ECORE_X_ILLUME_INDICATOR_OPAQUE:
341                         trans_mode = INDICATOR_OPACITY_OPAQUE;
342                         break;
343                 case ECORE_X_ILLUME_INDICATOR_TRANSLUCENT:
344                         trans_mode = INDICATOR_OPACITY_TRANSLUCENT;
345                         break;
346                 case ECORE_X_ILLUME_INDICATOR_TRANSPARENT:
347                         trans_mode = INDICATOR_OPACITY_TRANSPARENT;
348                         break;
349                 default:
350                         trans_mode = INDICATOR_OPACITY_OPAQUE;
351                         break;
352                 }
353 #endif
354                 _change_opacity(ad, trans_mode);
355                 break;
356         /* Rotate */
357         case 2:
358                 util_win_prop_angle_get(ad->active_indi_win, &angle);
359                 _rotate_window(ad, angle);
360                 break;
361         default :
362                 break;
363         }
364
365         return EINA_TRUE;
366 }
367
368 static Eina_Bool _property_changed_cb(void *data, int type, void *event)
369 {
370 #if 0
371         struct appdata *ad = data;
372         Ecore_X_Event_Window_Property *ev = event;
373
374         retv_if(!ad, ECORE_CALLBACK_PASS_ON);
375         retv_if(!ev, ECORE_CALLBACK_PASS_ON);
376
377         if (ev->atom == ECORE_X_ATOM_E_ILLUME_ROTATE_WINDOW_ANGLE) {
378                 if (ev->win == ad->active_indi_win) {
379                         _active_indicator_handle(data, 2);
380                 }
381         } else if (ev->atom == ECORE_X_ATOM_E_ILLUME_INDICATOR_OPACITY_MODE) {
382                 if (ev->win == ad->active_indi_win) {
383                         _active_indicator_handle(data, 1);
384                 }
385         } else if (ev->atom == ad->atom_active) {
386                 int ret = 0;
387
388                 Ecore_X_Window active_win;
389
390                 ret = ecore_x_window_prop_window_get(ecore_x_window_root_first_get(), ad->atom_active, &(active_win), 1);
391                 if (ret <= -1) {
392                         _E("Count of fetched items : %d", ret);
393                         return ECORE_CALLBACK_PASS_ON;
394                 }
395
396                 if (active_win != ad->active_indi_win) {
397                         if (ad->active_indi_win != -1) {
398                                 ecore_x_window_unsniff(ad->active_indi_win);
399                                 _D("UNSNIFF API %x", ad->active_indi_win);
400                         }
401                         ad->active_indi_win = active_win;
402
403                         ecore_x_window_sniff(ad->active_indi_win);
404                 }
405
406                 _active_indicator_handle(data, 1);
407                 _active_indicator_handle(data, 2);
408         }
409 #endif
410         return ECORE_CALLBACK_PASS_ON;
411 }
412
413 #if 0
414 static void _mctrl_monitor_cb(minicontrol_action_e action, const char *name, unsigned int width, unsigned int height, minicontrol_priority_e priority, void *data)
415 {
416         ret_if(!data);
417         ret_if(!name);
418
419         modules_minictrl_control(action,name,data);
420 }
421 #endif
422
423 static void _indicator_ecore_evas_msg_parent_handle(Ecore_Evas *ee, int msg_domain, int msg_id, void *data, int size)
424 {
425         ret_if(!data);
426
427 #ifdef _SUPPORT_SCREEN_READER
428         if (msg_domain == MSG_DOMAIN_CONTROL_ACCESS) {
429                 struct appdata *ad = (struct appdata *)ecore_evas_data_get(ee,"indicator_app_data");
430
431                 ret_if(!ad);
432
433                 Elm_Access_Action_Info *action_info;
434                 Evas_Object* win = NULL;
435                 action_info = data;
436
437                 win = ad->win.win;
438
439                 if (msg_id == ELM_ACCESS_ACTION_ACTIVATE) {
440                         elm_access_action(win, action_info->action_type,action_info);
441                 } else if (msg_id == ELM_ACCESS_ACTION_HIGHLIGHT_NEXT) {
442                         action_info->highlight_cycle = EINA_TRUE;
443                         elm_access_action(win,action_info->action_type,action_info);
444                 } else if (msg_id == ELM_ACCESS_ACTION_HIGHLIGHT_PREV) {
445                         action_info->highlight_cycle = EINA_TRUE;
446                         elm_access_action(win,action_info->action_type,action_info);
447                 } else if (msg_id == ELM_ACCESS_ACTION_UNHIGHLIGHT) {
448                         elm_access_action(win,action_info->action_type,action_info);
449                 } else if (msg_id == ELM_ACCESS_ACTION_READ) {
450                         elm_access_action(win,action_info->action_type,action_info);
451                 }
452         }
453 #endif /* _SUPPORT_SCREEN_READER */
454 }
455
456 #if 0
457 static void on_changed_receive(void *data, DBusMessage *msg)
458 {
459         int r;
460
461         r = dbus_message_is_signal(msg, INTERFACE_NAME, MEMBER_NAME);
462         ret_if(!r);
463
464         _D("LCD On handling");
465
466         if (!icon_get_update_flag()) {
467                 icon_set_update_flag(1);
468                 box_noti_ani_handle(1);
469                 modules_wake_up(data);
470         }
471 }
472
473 static void edbus_cleaner(void)
474 {
475         if (!edbus_conn) {
476                 _D("already unregistered");
477                 return;
478         }
479
480         if (edbus_handler) {
481                 e_dbus_signal_handler_del(edbus_conn, edbus_handler);
482                 edbus_handler = NULL;
483         }
484
485         if (edbus_conn) {
486                 e_dbus_connection_close(edbus_conn);
487                 edbus_conn = NULL;
488         }
489
490         e_dbus_shutdown();
491 }
492
493 static int edbus_listener(void* data)
494 {
495         if (edbus_conn != NULL) {
496                 _D("alreay exist");
497                 return -1;
498         }
499
500         e_dbus_init();
501
502         edbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM);
503         if (!edbus_conn) {
504                 _E("e_dbus_bus_get error");
505                 return -1;
506         }
507
508         edbus_handler = e_dbus_signal_handler_add(edbus_conn, NULL, PATH_NAME, INTERFACE_NAME, MEMBER_NAME, on_changed_receive, data);
509         if (!edbus_handler) {
510                 _E("e_dbus_signal_handler_add error");
511                 return -1;
512         }
513
514         return 0;
515 }
516 #endif
517
518 static void _register_event_handler_both(win_info *win, void *data)
519 {
520         Ecore_Evas *ee;
521
522         ret_if(!win);
523
524         ee = ecore_evas_ecore_evas_get(evas_object_evas_get(win->win));
525
526         evas_object_smart_callback_add(win->win,"delete,request", _indicator_window_delete_cb, data);
527         evas_object_event_callback_add(win->layout, EVAS_CALLBACK_MOUSE_DOWN, _indicator_mouse_down_cb, win);
528         evas_object_event_callback_add(win->layout, EVAS_CALLBACK_MOUSE_MOVE, _indicator_mouse_move_cb, win);
529         evas_object_event_callback_add(win->layout, EVAS_CALLBACK_MOUSE_UP,_indicator_mouse_up_cb, win);
530         ecore_evas_callback_msg_parent_handle_set(ee, _indicator_ecore_evas_msg_parent_handle);
531         ecore_evas_data_set(ee,"indicator_app_data",data);
532 }
533
534 /* FIXME */
535 #if 0
536 static void _indicator_service_cb(void *data, tzsh_indicator_service_h service, int angle, int opacity)
537 {
538         _D("Indicator service callback");
539 }
540 #endif
541
542 static void register_event_handler(void *data)
543 {
544         struct appdata *ad = data;
545 //      Ecore_Event_Handler *hdl = NULL;
546         ret_if(!data);
547
548         ad->active_indi_win = -1;
549         //ad->atom_active = ecore_x_atom_get("_NET_ACTIVE_WINDOW");
550         //ecore_x_window_sniff(ecore_x_window_root_first_get());
551
552         _register_event_handler_both(&(ad->win),data);
553
554         /* FIXME */
555 #if 0
556         if (ad->indicator_service) {
557                 tzsh_indicator_service_property_change_cb_set(ad->indicator_service, _indicator_service_cb, NULL);
558         }
559 #endif
560
561 #if 0
562         hdl = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, _indicator_client_message_cb, (void *)ad);
563         ret_if(!hdl);
564         ad->evt_handlers = eina_list_append(ad->evt_handlers, hdl);
565
566         hdl = ecore_event_handler_add(ECORE_X_EVENT_WINDOW_PROPERTY, _property_changed_cb, data);
567         ret_if(!hdl);
568         ad->evt_handlers = eina_list_append(ad->evt_handlers, hdl);
569 #endif
570         if (vconf_notify_key_changed(VCONFKEY_PM_STATE, _indicator_notify_pm_state_cb, (void *)ad) != 0) {
571                 _E("Fail to set callback for VCONFKEY_PM_STATE");
572         }
573
574         if (vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, _indicator_power_off_status_cb, (void *)ad) < 0) {
575                 _E("Failed to set callback for VCONFKEY_SYSMAN_POWER_OFF_STATUS");
576         }
577
578         if (vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE, _indicator_lock_status_cb, (void *)ad) < 0) {
579                 _E("Failed to set callback for VCONFKEY_IDLE_LOCK_STATE");
580         }
581
582 //      edbus_listener(data);
583 }
584
585 static void _unregister_event_handler_both(win_info *win)
586 {
587         ret_if(!win);
588
589         evas_object_smart_callback_del(win->win, "delete-request", _indicator_window_delete_cb);
590         evas_object_event_callback_del(win->layout, EVAS_CALLBACK_MOUSE_DOWN, _indicator_mouse_down_cb);
591         evas_object_event_callback_del(win->layout, EVAS_CALLBACK_MOUSE_MOVE, _indicator_mouse_move_cb);
592         evas_object_event_callback_del(win->layout, EVAS_CALLBACK_MOUSE_UP, _indicator_mouse_up_cb);
593 }
594
595 static int unregister_event_handler(void *data)
596 {
597         struct appdata *ad = (struct appdata *)data;
598
599         retv_if(!data, 0);
600
601         _unregister_event_handler_both(&(ad->win));
602
603         vconf_ignore_key_changed(VCONFKEY_PM_STATE, _indicator_notify_pm_state_cb);
604         vconf_ignore_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, _indicator_power_off_status_cb);
605         vconf_ignore_key_changed(VCONFKEY_IDLE_LOCK_STATE, _indicator_lock_status_cb);
606
607         Ecore_Event_Handler *hdl = NULL;
608         EINA_LIST_FREE(ad->evt_handlers, hdl) {
609                 if (hdl) ecore_event_handler_del(hdl);
610         }
611
612 //      edbus_cleaner();
613
614         return OK;
615 }
616
617 static void _create_layout(struct appdata *ad, const char *file, const char *group)
618 {
619         ad->win.layout = elm_layout_add(ad->win.win);
620         ret_if(!ad->win.layout);
621
622         if (EINA_FALSE == elm_layout_file_set(ad->win.layout, file, group)) {
623                 _E("Failed to set file of layout");
624                 evas_object_del(ad->win.layout);
625                 return NULL;
626         }
627
628         evas_object_size_hint_min_set(ad->win.layout, ad->win.w, ad->win.h);
629         /* FIXME */
630         evas_object_size_hint_weight_set(ad->win.layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
631         elm_win_resize_object_add(ad->win.win, ad->win.layout);
632         evas_object_move(ad->win.layout, 0, 0);
633         evas_object_show(ad->win.layout);
634 }
635
636 static void _create_box(win_info *win)
637 {
638         ret_if(!win);
639
640         /* First, clear layout */
641         box_fini(win);
642
643         box_init(win);
644
645         return;
646 }
647
648 //FIXME
649 #if 0
650 static indicator_error_e _tzsh_set(struct appdata* ad)
651 {
652         tzsh_window tz_win;
653
654         retv_if(!ad, INDICATOR_ERROR_INVALID_PARAMETER);
655         retv_if(!ad->win.win, INDICATOR_ERROR_INVALID_PARAMETER);
656
657         ad->tzsh = tzsh_create(TZSH_TOOLKIT_TYPE_EFL);
658         retv_if(!ad->tzsh, INDICATOR_ERROR_FAIL);
659
660         tz_win = elm_win_window_id_get(ad->win.win);
661         if (!tz_win) {
662                 tzsh_destroy(ad->tzsh);
663                 _E("Failed to get Tizen window");
664                 /* FIXME */
665                 //return INDICATOR_ERROR_FAIL;
666                 return INDICATOR_ERROR_NONE;
667         }
668
669         ad->indicator_service = tzsh_indicator_service_create(ad->tzsh, tz_win);
670         if (!ad->indicator_service) {
671                 tzsh_destroy(ad->tzsh);
672                 _E("Failed to create Tizen window indicator service");
673                 return INDICATOR_ERROR_FAIL;
674         }
675
676         return INDICATOR_ERROR_NONE;
677 }
678
679 static void _tzsh_unset(struct appdata *ad)
680 {
681         ret_if(!ad);
682
683         if (ad->indicator_service) {
684                 tzsh_indicator_service_destroy(ad->indicator_service);
685                 ad->indicator_service = NULL;
686         }
687
688         if (ad->tzsh) {
689                 tzsh_destroy(ad->tzsh);
690                 ad->tzsh = NULL;
691         }
692 }
693 #endif
694
695 static Eina_Bool _indicator_listen_timer_cb(void* data)
696 {
697         win_info *win = data;
698
699         retv_if(!win, ECORE_CALLBACK_CANCEL);
700
701         //win = (win_info*)data;
702
703         if (!elm_win_socket_listen(win->win , INDICATOR_SERVICE_NAME, 0, EINA_FALSE)) {
704                 _E("failed to elm_win_socket_listen() %x", win->win);
705                 return ECORE_CALLBACK_RENEW;
706         } else {
707                 _D("listen success");
708                 s_info.listen_timer = NULL;
709                 return ECORE_CALLBACK_CANCEL;
710         }
711 }
712
713 #define INDICATOR_HEIGHT_TM1 52
714 static void _create_window(struct appdata *ad)
715 {
716         Evas_Object *dummy_win = NULL;
717
718         _D("Create window");
719
720         ad->win.win = elm_win_add(NULL, "indicator", ELM_WIN_SOCKET_IMAGE);
721         ret_if(!(ad->win.win));
722
723         elm_win_alpha_set(ad->win.win, EINA_TRUE);
724
725         dummy_win = elm_win_add(NULL, "indicator_dummy", ELM_WIN_BASIC);
726         if (dummy_win) {
727                 elm_win_screen_size_get(dummy_win, NULL, NULL, &ad->win.port_w, &ad->win.land_w);
728                 evas_object_del(dummy_win);
729                 _D("Dummy window w, h (%d, %d)", ad->win.port_w, ad->win.land_w);
730         } else {
731                 _E("Critical error. Cannot create dummy window");
732         }
733
734         if (!elm_win_socket_listen(ad->win.win , INDICATOR_SERVICE_NAME, 0, EINA_FALSE)) {
735                 _E("Failed 1st to elm_win_socket_listen() %x", ad->win.win);
736
737                 if (s_info.listen_timer != NULL) {
738                         ecore_timer_del(s_info.listen_timer);
739                         s_info.listen_timer = NULL;
740                 }
741                 s_info.listen_timer = ecore_timer_add(3, _indicator_listen_timer_cb, &(ad->win));
742         }
743
744         elm_win_alpha_set(ad->win.win , EINA_TRUE);
745         /* FIXME */
746         elm_win_borderless_set(ad->win.win , EINA_TRUE);
747         evas_object_size_hint_fill_set(ad->win.win , EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
748         evas_object_size_hint_align_set(ad->win.win , 1.0, 0.5);
749
750         evas_object_resize(ad->win.win, ad->win.port_w, INDICATOR_HEIGHT_TM1);
751         _D("w,h(%d,%d)", ad->win.port_w, INDICATOR_HEIGHT_TM1);
752
753         evas_object_show(ad->win.win);
754
755 }
756
757 static void _create_base_gui(void* data)
758 {
759         struct appdata *ad = data;
760         Evas_Object *dummy_win = NULL;
761
762         ret_if(!ad);
763
764         _D("Start to create base gui");
765
766         _create_window(ad);
767
768         //FIXME
769 #if 0
770         if (INDICATOR_ERROR_NONE != _tzsh_set(ad)) {
771                 _E("Failed to set tzsh");
772         }
773 #endif
774
775         /* FIXME */
776         ad->win.h = INDICATOR_HEIGHT_TM1;
777         ad->win.w = ad->win.port_w;
778         ad->win.evas = evas_object_evas_get(ad->win.win);
779
780         _D("win_size = Original(%d, %d), Scaled(%lf, %lf)", ad->win.port_w, ad->win.h, ELM_SCALE_SIZE(ad->win.port_w), ELM_SCALE_SIZE(ad->win.h));
781
782         _create_layout(ad, EDJ_FILE, GRP_NAME);
783         _create_box(&(ad->win));
784
785
786 #if 0 /* For test */
787         Evas_Object *rect = evas_object_rectangle_add(ad->win.evas);
788         ret_if(!rect);
789         evas_object_resize(rect, 720, 52);
790         evas_object_color_set(rect, 0, 0, 255, 255);
791         evas_object_show(rect);
792         evas_object_layer_set(rect, -256);
793 #endif
794         ad->win.data = data;
795
796         return;
797 }
798
799 static void _init_win_info(void * data)
800 {
801         struct appdata *ad = data;
802
803         ret_if(!ad);
804
805         memset(&(ad->win),0x00,sizeof(win_info));
806 }
807
808 static void _init_tel_info(void * data)
809 {
810         struct appdata *ad = data;
811
812         ret_if(!ad);
813
814         memset(&(ad->tel_info), 0x00, sizeof(telephony_info));
815 }
816
817 static indicator_error_e _start_indicator(void *data)
818 {
819         retv_if(!data, INDICATOR_ERROR_INVALID_PARAMETER);
820
821         _init_win_info(data);
822         _init_tel_info(data);
823
824         /* Create indicator window */
825         _create_base_gui(data);
826
827         return INDICATOR_ERROR_NONE;
828 }
829
830 static indicator_error_e _terminate_indicator(void *data)
831 {
832         struct appdata *ad = data;
833
834         retv_if(!ad, INDICATOR_ERROR_INVALID_PARAMETER);
835
836         modules_fini(data);
837         unregister_event_handler(ad);
838
839         box_fini(&(ad->win));
840
841         if (ad->win.evas)
842                 evas_image_cache_flush(ad->win.evas);
843
844         if (ad->win.layout) {
845                 evas_object_del(ad->win.layout);
846                 ad->win.layout = NULL;
847         }
848
849         if (ad->win.win) {
850                 evas_object_del(ad->win.win);
851                 ad->win.win = NULL;
852         }
853
854         //FIXME
855 #if 0
856         _tzsh_unset(ad);
857 #endif
858
859         if (ad)
860                 free(ad);
861
862         elm_exit();
863
864         return INDICATOR_ERROR_NONE;
865 }
866
867 static void __indicator_set_showhide_press(int value, int line)
868 {
869         show_hide_pressed = value;
870 }
871
872 static void _indicator_mouse_down_cb(void *data, Evas * e, Evas_Object * obj, void *event)
873 {
874         win_info *win = (win_info*)data;
875         Evas_Event_Mouse_Down *ev = NULL;
876
877         retif(data == NULL || event == NULL, , "Invalid parameter!");
878         ev = event;
879
880         win->mouse_event.x = ev->canvas.x;
881         win->mouse_event.y = ev->canvas.y;
882
883         if (ev->button != 1) {
884                 return;
885         }
886
887 #ifdef HOME_KEY_EMULATION
888         if (box_check_indicator_area(win, ev->canvas.x, ev->canvas.y)) {
889                 int lock_state = VCONFKEY_IDLE_UNLOCK;
890                 int ps_state = -1;
891                 int ret = -1;
892
893         /*      if (indicator_message_disp_check(win->type) == 1) {
894                         return;
895                 }*/
896                 ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE,&lock_state);
897
898                 if (ret != 0 || lock_state == VCONFKEY_IDLE_LOCK) {
899                         return;
900                 }
901                 ret = vconf_get_int(VCONFKEY_SETAPPL_PSMODE,&ps_state);
902
903                 if (ret != 0 || ps_state == SETTING_PSMODE_EMERGENCY) {
904                         return;
905                 }
906
907                 if (box_check_home_icon_area(win, ev->canvas.x, ev->canvas.y)) {
908
909                         if (util_check_system_status() == FAIL) {
910                                 _D("util_check_system_status failed");
911                                 return;
912                         }
913                         home_button_pressed = EINA_TRUE;
914                 }
915                 indicator_press_coord.x = ev->canvas.x;
916                 indicator_press_coord.y = ev->canvas.y;
917         }
918 #endif
919 }
920
921
922
923 static void _indicator_mouse_move_cb(void *data, Evas * e, Evas_Object * obj, void *event)
924 {
925         Evas_Event_Mouse_Move *ev = NULL;
926         win_info* win = (win_info*)data;
927
928         retif(data == NULL || event == NULL, , "Invalid parameter!");
929
930         ev = event;
931
932         if (home_button_pressed) {
933                 if (!box_check_home_icon_area(win,ev->cur.canvas.x,ev->cur.canvas.y)) {
934                         home_button_pressed = FALSE;
935                 }
936
937         }
938         if (show_hide_pressed == 1) {
939                         if (!box_check_more_icon_area(win,ev->cur.canvas.x,ev->cur.canvas.y)) {
940                                 __indicator_set_showhide_press(EINA_FALSE, __LINE__);
941                         }
942         }
943 }
944
945 static void _indicator_mouse_up_cb(void *data, Evas * e, Evas_Object * obj, void *event)
946 {
947         Evas_Event_Mouse_Up *ev = NULL;
948         win_info *win = (win_info *)data;
949
950         retif(data == NULL || event == NULL, , "Invalid parameter!");
951
952         ev = event;
953
954 #ifdef HOME_KEY_EMULATION
955         if (box_check_indicator_area(win, ev->canvas.x, ev->canvas.y)) {
956
957                 if (box_check_home_icon_area(win, ev->canvas.x, ev->canvas.y)) {
958                         if (home_button_pressed == EINA_TRUE) {
959                                 util_launch_search(win->data);
960                                 feedback_play_type(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TAP);
961                         }
962
963                 } else if (box_check_more_icon_area(win, ev->canvas.x, ev->canvas.y)) {
964                         if(show_hide_pressed == EINA_TRUE) {
965                                 DBG("pressed area");
966                                 feedback_play_type(FEEDBACK_TYPE_SOUND, FEEDBACK_PATTERN_TAP);
967                         }
968                 }
969         }
970
971         home_button_pressed = EINA_FALSE;
972         __indicator_set_showhide_press(EINA_FALSE, __LINE__);
973
974 #else /* HOME_REMOVE_LONGPRESS */
975         int mouse_up_prio = -1;
976         int mouse_down_prio = -1;
977
978         if (home_button_pressed == EINA_TRUE) {
979                 home_button_pressed = EINA_FALSE;
980         }
981
982         mouse_down_prio =
983                 box_get_priority_in_move_area(win,win->mouse_event.x,
984                                                         win->mouse_event.y);
985         mouse_up_prio = box_get_priority_in_move_area(win,ev->canvas.x,
986                                                         ev->canvas.y);
987
988         if (mouse_down_prio > -1 && mouse_up_prio > -1
989                 && mouse_down_prio == mouse_up_prio) {
990                 switch (mouse_down_prio) {
991                 case INDICATOR_PRIORITY_FIXED1:
992                         int lock_state = VCONFKEY_IDLE_UNLOCK;
993                         int lock_ret = -1;
994
995                         lock_ret = vconf_get_int(VCONFKEY_IDLE_LOCK_STATE,
996                                         &lock_state);
997
998                         /* In Lock Screen, home button don't have to do */
999                         if (lock_ret == 0 && lock_state == VCONFKEY_IDLE_LOCK)
1000                                 break;
1001
1002                         char *top_win_name = NULL;
1003
1004                         if (util_check_system_status() == FAIL)
1005                                 break;
1006                 break;
1007                 }
1008         }
1009 #endif /* HOME_KEY_EMULATION */
1010         win->mouse_event.x = 0;
1011         win->mouse_event.y = 0;
1012 }
1013
1014 #if 0
1015 static void _app_terminate_cb(app_context_h app_context, app_context_status_e status, void *data)
1016 {
1017         retif(data == NULL, , "Invalid parameter!");
1018         DBG("_app_terminate_cb");
1019         char *app_id = NULL;
1020         app_context_get_app_id(app_context, &app_id);
1021         if (app_id == NULL) {
1022                 ERR("app_id is null!!");
1023                 return;
1024         } else {
1025                 DBG("_app_terminate_cb %s",app_id);
1026         }
1027
1028         if (status == APP_CONTEXT_STATUS_TERMINATED) {
1029                 if (strcmp(MP_APP_ID,app_id) == 0) {
1030                         DBG("hide music icon");
1031                         hide_mp_icon();
1032                 } else if(strcmp(FMRADIO_APP_ID,app_id) == 0) {
1033                         DBG("hide fm radio icon");
1034                         hide_fm_radio_icon();
1035                 } else if(strcmp(VR_APP_ID,app_id) == 0) {
1036                         DBG("hide voice recorder icon");
1037                         hide_voice_recorder_icon();
1038                 }
1039         }
1040         if (app_id!=NULL) {
1041                 free(app_id);
1042                 app_id = NULL;
1043         }
1044 }
1045
1046 static void register_app_terminate_cb(void* data)
1047 {
1048         retif(data == NULL, , "Invalid parameter!");
1049         app_manager_set_app_context_status_cb(_app_terminate_cb, MP_APP_ID, data);
1050         app_manager_set_app_context_status_cb(_app_terminate_cb, FMRADIO_APP_ID, data);
1051         app_manager_set_app_context_status_cb(_app_terminate_cb, VR_APP_ID, data);
1052 }
1053 #endif
1054
1055 static void _signal_handler(int signum, siginfo_t *info, void *unused)
1056 {
1057     ui_app_exit();
1058 }
1059
1060 static bool app_create(void *data)
1061 {
1062         struct appdata *ad = data;
1063         int ret;
1064
1065         retv_if(!ad, false);
1066
1067         elm_app_base_scale_set(2.6);
1068
1069         /* Signal handler */
1070         struct sigaction act;
1071         memset(&act, 0x00, sizeof(struct sigaction));
1072         act.sa_sigaction = _signal_handler;
1073         act.sa_flags = SA_SIGINFO;
1074
1075         ret = sigemptyset(&act.sa_mask);
1076         if (ret < 0) {
1077                 _E("Failed to sigemptyset[%s]", strerror(errno));
1078         }
1079         ret = sigaddset(&act.sa_mask, SIGTERM);
1080         if (ret < 0) {
1081                 _E("Failed to sigaddset[%s]", strerror(errno));
1082         }
1083         ret = sigaction(SIGTERM, &act, NULL);
1084         if (ret < 0) {
1085                 _E("Failed to sigaction[%s]", strerror(errno));
1086         }
1087
1088         ret = _start_indicator(ad);
1089         if (ret != INDICATOR_ERROR_NONE) {
1090                 _D("Failed to create a new window!");
1091         }
1092
1093         /* Set nonfixed-list size for display */
1094         modules_init_first(ad);
1095
1096         if (ad->win.win) {
1097                 elm_win_activate(ad->win.win);
1098         }
1099         evas_object_show(ad->win.layout);
1100         evas_object_show(ad->win.win);
1101
1102         return true;
1103 }
1104
1105 static void app_terminate(void *data)
1106 {
1107         struct appdata *ad = data;
1108         modules_fini(data);
1109         ticker_fini(ad);
1110         indicator_message_fini();
1111 #ifdef _SUPPORT_SCREEN_READER2
1112         indicator_service_tts_fini(data);
1113 #endif
1114
1115         unregister_event_handler(ad);
1116
1117         feedback_deinitialize();
1118
1119         box_fini(&(ad->win));
1120         evas_image_cache_flush(ad->win.evas);
1121         evas_object_del(ad->win.layout);
1122         evas_object_del(ad->win.win);
1123
1124         _D("INDICATOR IS TERMINATED");
1125 }
1126
1127 static void app_pause(void *data)
1128 {
1129 }
1130
1131 static void app_resume(void *data)
1132 {
1133 }
1134
1135 static void app_service(app_control_h service, void *data)
1136 {
1137         struct appdata *ad = data;
1138
1139         _D("INDICATOR IS STARTED");
1140
1141         register_event_handler(ad);
1142         modules_init(data);
1143 #ifdef _SUPPORT_SCREEN_READER
1144         modules_register_tts(data);
1145 #endif
1146         feedback_initialize();
1147         indicator_message_init(data);
1148         if (INDICATOR_ERROR_NONE != ticker_init(ad)) {
1149                 _E("Ticker cannot initialize");
1150         }
1151 #ifdef _SUPPORT_SCREEN_READER2
1152         indicator_service_tts_init(data);
1153 #endif
1154         _indicator_lock_status_cb(NULL, data);
1155 #if 0
1156         register_app_terminate_cb(data);
1157 #endif
1158 }
1159
1160 int main(int argc, char *argv[])
1161 {
1162         struct appdata ad;
1163
1164         ui_app_lifecycle_callback_s event_callback = {0,};
1165         app_event_handler_h handlers[5] = {NULL, };
1166
1167         int ret = 0;
1168
1169         _D("Start indicator");
1170
1171         event_callback.create = app_create;
1172         event_callback.terminate = app_terminate;
1173         event_callback.pause = app_pause;
1174         event_callback.resume = app_resume;
1175         event_callback.app_control = app_service;
1176
1177         ui_app_add_event_handler(&handlers[APP_EVENT_LOW_BATTERY], APP_EVENT_LOW_BATTERY, _indicator_low_bat_cb, NULL);
1178         ui_app_add_event_handler(&handlers[APP_EVENT_LANGUAGE_CHANGED], APP_EVENT_LANGUAGE_CHANGED, _indicator_lang_changed_cb, &ad);
1179         ui_app_add_event_handler(&handlers[APP_EVENT_REGION_FORMAT_CHANGED], APP_EVENT_REGION_FORMAT_CHANGED, _indicator_region_changed_cb, NULL);
1180
1181         memset(&ad, 0x0, sizeof(struct appdata));
1182
1183         ret = ui_app_main(argc, argv, &event_callback, &ad);
1184         if (ret != APP_ERROR_NONE) {
1185                 _E("app_main() is failed. err = %d", ret);
1186         }
1187
1188         return ret;
1189 }
1190
1191 /* End of file */