tizen 2.3 release
[apps/home/settings.git] / src / setting-support-shortcut / support_shortcut_developer.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <setting-debug.h>
22 #include <app.h>
23 #include <ui-gadget.h>
24 #include <Elementary.h>
25 #include <Evas.h>
26 #include <Ecore_X.h>
27 #include <utilX.h>
28 #include <appcore-efl.h>
29 #include <appcore-common.h>
30 #include <vconf.h>
31 #include <feedback.h>
32 #include <dd-led.h>
33 #include <device.h>
34 #include <efl_assist.h>
35 #include <setting-common-general-func.h>
36
37 #define EXPORT_PUBLIC __attribute__ ((visibility ("default")))
38
39
40 typedef struct _support_display_appdata {
41
42         ui_gadget_h ug;
43
44         Evas *evas;
45         Evas_Object *win_main;
46         Evas_Object *navibar_main;
47         Evas_Object *ly_main;                   /**< seting view main */
48         app_control_h service;
49         Ecore_Event_Handler *event_handler;
50 } support_display_appdata;
51
52 /**
53 * The function is called when Setting is terminated
54 */
55 static void support_display_app_terminate(void *data)
56 {
57         SETTING_TRACE_BEGIN;
58         support_display_appdata *ad = data;
59
60         SETTING_TRACE("support_display_terminate!");
61
62         if(ad->event_handler)
63         {
64                 ecore_event_handler_del(ad->event_handler);
65                 ad->event_handler = NULL;
66         }
67
68         if (ad->ly_main) {
69                 evas_object_del(ad->ly_main);
70                 ad->ly_main = NULL;
71         }
72
73         if (ad->win_main) {
74                 evas_object_del(ad->win_main);
75                 ad->win_main = NULL;
76         }
77         elm_exit();
78
79         SETTING_TRACE_END;
80 }
81
82 /**
83 * The event process when win object is destroyed
84 */
85 static void support_display_del_win(void *data, Evas_Object *obj, void *event)
86 {
87         //app_efl_exit();
88         elm_exit();
89 }
90
91 /**
92 * To create a win object, the win is shared between the App and all its UGs
93 */
94 static Evas_Object *support_display_create_win(const char *name)
95 {
96         SETTING_TRACE_BEGIN;
97         Evas_Object *eo;
98         int w, h;
99
100         eo = elm_win_add(NULL, name, ELM_WIN_BASIC);
101
102         if (eo)
103         {
104                 elm_win_title_set(eo, name);
105                 elm_win_borderless_set(eo, EINA_TRUE);
106                 evas_object_smart_callback_add(eo, "delete,request",
107                                                support_display_del_win, NULL);
108                 ecore_x_window_size_get(ecore_x_window_root_first_get(),
109                                         &w, &h);
110                 evas_object_resize(eo, w, h);
111         }
112
113         return eo;
114 }
115
116 static Eina_Bool __key_press_cb(void *data, int type, void *event)
117 {
118         SETTING_TRACE_BEGIN;
119         Evas_Event_Key_Down *ev = event;
120         if(!ev || !data)
121         {
122                 return ECORE_CALLBACK_RENEW;
123         }
124
125         support_display_appdata *ad = data;
126
127         if(strcmp(ev->keyname, KEY_HOME) == 0)
128         {
129                 setting_ug_destroy(ad->ug);
130         }
131         return ECORE_CALLBACK_RENEW;
132 }
133
134 /**
135 * The function is called to create Setting view widgets
136 */
137 static bool support_display_app_create(void *data)
138 {
139         SETTING_TRACE_BEGIN;
140
141         support_display_appdata *ad = data;
142
143         SETTING_TRACE("[TIME] 3. it taked %d msec from main to setting_display_app_create ", appcore_measure_time());
144         appcore_measure_start();
145
146         /* create window */
147         ad->win_main = support_display_create_win("org.tizen.setting.display");
148         if(ad->win_main == NULL)
149         {
150                 SETTING_TRACE("Can't create window");
151                 return 0;
152         }
153
154         if(elm_win_wm_rotation_supported_get(ad->win_main))
155         {
156                 int rots[4] = { 0, 90, 180, 270 };
157                 elm_win_wm_rotation_available_rotations_set(ad->win_main, rots, 4);
158         }
159
160         UG_INIT_EFL(ad->win_main, UG_OPT_INDICATOR_ENABLE);
161         ad->evas = evas_object_evas_get(ad->win_main);
162
163         elm_win_indicator_mode_set(ad->win_main, ELM_WIN_INDICATOR_SHOW);
164         elm_win_indicator_opacity_set(ad->win_main, ELM_WIN_INDICATOR_OPAQUE);
165
166         /* call ug */
167         Evas_Object *conform = elm_conformant_add(ad->win_main);
168         ad->ly_main = elm_layout_add(ad->win_main);
169         evas_object_size_hint_weight_set(ad->ly_main, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
170         elm_layout_theme_set(ad->ly_main, "layout", "application", "default");
171
172         /* add bg */
173         Evas_Object *bg = elm_bg_add(ad->ly_main);
174         elm_object_style_set(bg, "group_list");
175         evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
176         elm_object_part_content_set(ad->ly_main, "elm.swallow.bg", bg);
177         evas_object_show(bg);
178
179         /* Indicator bg */
180         Evas_Object *indicator_bg = elm_bg_add(conform);
181         elm_object_style_set(indicator_bg, "indicator/headerbg");
182         elm_object_part_content_set(conform, "elm.swallow.indicator_bg", indicator_bg);
183         evas_object_show(indicator_bg);
184
185         evas_object_size_hint_weight_set(conform, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
186         evas_object_size_hint_align_set(conform, EVAS_HINT_FILL, EVAS_HINT_FILL);
187         elm_win_resize_object_add(ad->win_main, conform);
188         elm_object_content_set(conform, ad->ly_main);
189         evas_object_show(conform);
190         elm_win_conformant_set(ad->win_main, EINA_TRUE);
191
192         SETTING_TRACE("[TIME] 4. setting_main_app_create taked %d msec ", appcore_measure_time());
193         appcore_measure_start();
194
195         /* add event handler */
196         ad->event_handler = ecore_event_handler_add(ECORE_EVENT_KEY_DOWN, __key_press_cb, ad);
197
198         return TRUE;
199 }
200
201 /**
202 * The function is called when Setting begins run in background from forground
203 */
204 static void support_display_app_pause(void *data)
205 {
206         SETTING_TRACE_BEGIN;
207         support_display_appdata *ad = data;
208
209         if (ad->ug) {
210                 ug_pause();
211                 //ad->ug = NULL;
212         }
213 }
214
215 /**
216 * The function is called when Setting begins run in forground from background
217 */
218 static void support_display_app_resume(void *data)
219 {
220         SETTING_TRACE_BEGIN;
221         support_display_appdata *ad = data;
222
223         if (ad->ug)
224                 ug_resume();
225 }
226
227 void support_display_app_destroy_ug_cb(ui_gadget_h ug, void *priv)
228 {
229         support_display_appdata *ad = (support_display_appdata *) priv;
230         if (!ad)
231                 return;         /*  do nothing if ad is NULL */
232
233         elm_win_lower(ad->win_main);
234
235         if (ug) {
236                 setting_ug_destroy(ug);
237                 ad->ug = NULL;
238         }
239 }
240
241 void support_display_app_end_ug_cb(ui_gadget_h ug, void *priv)
242 {
243         if(!ug)
244                 return;
245         support_display_appdata *ad = (support_display_appdata *) priv;
246         if (!ad)
247                 return;         /*  do nothing if ad is NULL */
248         support_display_app_terminate(ad);
249 }
250
251 void support_display_app_layout_ug_cb(ui_gadget_h ug, enum ug_mode mode,
252                                       void *priv)
253 {
254         Evas_Object *base;
255
256         if (!priv)
257                 return;
258
259         base = (Evas_Object *) ug_get_layout(ug);
260         if (!base)
261                 return;
262
263         switch (mode) {
264         case UG_MODE_FULLVIEW:
265                 evas_object_size_hint_weight_set(base, EVAS_HINT_EXPAND,
266                                                  EVAS_HINT_EXPAND);
267                 ug_disable_effect(ug);
268                 evas_object_show(base);
269                 break;
270         default:
271                 break;
272         }
273 }
274
275 static int _set;
276 EXPORT_PUBLIC void update_lang(void)
277 {
278     char *lang;
279     char *r;
280
281     lang = vconf_get_str(VCONFKEY_LANGSET);
282     if (lang) {
283         setenv("LANG", lang, 1);
284         setenv("LC_MESSAGES", lang, 1);
285         r = setlocale(LC_ALL, "");
286         if (r == NULL) {
287             setlocale(LC_ALL, vconf_get_str(VCONFKEY_LANGSET));
288         }
289         free(lang);
290     }
291 }
292
293 EXPORT_PUBLIC void update_region(void)
294 {
295     char *region;
296
297     region = vconf_get_str(VCONFKEY_REGIONFORMAT);
298     if (region) {
299         setenv("LC_CTYPE", region, 1);
300         setenv("LC_NUMERIC", region, 1);
301         setenv("LC_TIME", region, 1);
302         setenv("LC_COLLATE", region, 1);
303         setenv("LC_MONETARY", region, 1);
304         setenv("LC_PAPER", region, 1);
305         setenv("LC_NAME", region, 1);
306         setenv("LC_ADDRESS", region, 1);
307         setenv("LC_TELEPHONE", region, 1);
308         setenv("LC_MEASUREMENT", region, 1);
309         setenv("LC_IDENTIFICATION", region, 1);
310         free(region);
311     }
312 }
313
314 static int __set_i18n(const char *domain, const char *dir)
315 {
316     char *r;
317     if (domain == NULL) {
318         errno = EINVAL;
319         return -1;
320     }
321
322     r = setlocale(LC_ALL, "");
323     /* if locale is not set properly, try again to set as language base */
324     if (r == NULL) {
325         r = setlocale(LC_ALL, vconf_get_str(VCONFKEY_LANGSET));
326     }
327     bindtextdomain(domain, dir);
328     textdomain(domain);
329     return 0;
330 }
331
332 int set_i18n(char* pkgname, char* localedir)
333 //int set_i18n(const char *domainname, const char *dirname)
334 {
335         if (_set)
336                 return 0;
337     update_lang();
338     update_region();
339
340     return __set_i18n(pkgname, localedir);
341 }
342
343 /**
344 * The function is called by app-fwk after app_create. It always do the process which cost much time.
345 */
346 static void support_display_app_reset(app_control_h service, void *data)
347 {
348         SETTING_TRACE_BEGIN;
349         support_display_appdata *ad = data;
350
351         app_control_h svc;
352
353         struct ug_cbs *cbs = (struct ug_cbs *)calloc(1, sizeof(struct ug_cbs));
354         if(!cbs)
355         {
356                 support_display_app_terminate(ad);
357                 return;
358         }
359
360         cbs->layout_cb = support_display_app_layout_ug_cb;
361         cbs->result_cb = NULL;
362         cbs->destroy_cb = support_display_app_destroy_ug_cb;
363         cbs->end_cb = support_display_app_end_ug_cb;
364         cbs->priv = (void *)ad;
365
366         if(app_control_create(&svc))
367         {
368                 FREE(cbs);
369                 support_display_app_terminate(ad);
370                 return;
371         }
372         app_control_add_extra_data(svc, "viewtype", "usb");
373         ad->ug = setting_ug_create(NULL, "setting-developeroption-efl", UG_MODE_FULLVIEW, svc, cbs);
374
375         app_control_destroy(svc);
376
377         if (!ad->ug) {
378                 evas_object_show(ad->ly_main);
379                 if (cbs)
380                 {
381                         FREE(cbs);
382                         support_display_app_terminate(ad);
383                         return;
384                 }
385         }
386
387         if (ad->win_main)
388         {
389                 evas_object_show(ad->win_main);
390                 elm_win_activate(ad->win_main);
391         }
392         if (cbs)
393                 FREE(cbs);
394 }
395
396
397 static void support_display_app_lang_changed(void *data)
398 {
399         SETTING_TRACE_BEGIN;
400         support_display_appdata *ad = data;
401         char *lang = NULL;
402
403         lang = vconf_get_str(VCONFKEY_LANGSET);
404         if(lang)
405         {
406                 elm_language_set((const char *)lang);
407                 FREE(lang);
408         }
409
410         if(ad->ug)
411                 ug_send_event(UG_EVENT_LANG_CHANGE);
412 }
413
414 EXPORT_PUBLIC
415 int main(int argc, char *argv[])
416 {
417         SETTING_TRACE_BEGIN;
418         int r = 0;
419         support_display_appdata ad;
420
421         app_event_callback_s ops = {
422                 .create = support_display_app_create,
423                 .terminate = support_display_app_terminate,
424                 .pause = support_display_app_pause,
425                 .resume = support_display_app_resume,
426                 .app_control = support_display_app_reset,
427                 .low_battery = NULL,
428                 .language_changed = support_display_app_lang_changed,
429                 .region_format_changed = NULL,
430                 .device_orientation = NULL,
431         };
432
433         memset(&ad, 0x00, sizeof(support_display_appdata));
434
435         r = app_efl_main(&argc, &argv, &ops, &ad);
436         SETTING_TRACE_DEBUG("r = %d", r);
437
438         if(r == -1)
439         {
440                 SETTING_TRACE_ERROR("app_efl_main() returns -1");
441                 return -1;
442         }
443
444         return 0;
445 }