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