f1246dca4b9db494402529913238db931f74b12e
[framework/uifw/elementary.git] / src / lib / elm_config.c
1 #ifdef HAVE_CONFIG_H
2 # include "elementary_config.h"
3 #endif
4
5 #ifdef HAVE_EVIL
6 # include <Evil.h>
7 #endif
8
9 #include <Elementary.h>
10 #include "elm_priv.h"
11
12 EAPI int ELM_EVENT_CONFIG_ALL_CHANGED = 0;
13
14 Elm_Config *_elm_config = NULL;
15 char *_elm_profile = NULL;
16 static Eet_Data_Descriptor *_config_edd = NULL;
17 static Eet_Data_Descriptor *_config_font_overlay_edd = NULL;
18
19 static Ecore_Poller *_elm_cache_flush_poller = NULL;
20
21 const char *_elm_engines[] = {
22    "software_x11",
23    "fb",
24    "directfb",
25    "software_16_x11",
26    "software_8_x11",
27    "xrender_x11",
28    "opengl_x11",
29    "software_gdi",
30    "software_16_wince_gdi",
31    "sdl",
32    "software_16_sdl",
33    "opengl_sdl",
34    "buffer",
35    "ews",
36    NULL
37 };
38
39 /* whenever you want to add a new text class support into Elementary,
40    declare it both here and in the (default) theme */
41 static const Elm_Text_Class _elm_text_classes[] = {
42    {"button", "Button Labels"},
43    {"label", "Text Labels"},
44    {"entry", "Text Entries"},
45    {"title_bar", "Title Bar"},
46    {"list_item", "List Items"},
47    {"grid_item", "Grid Items"},
48    {"toolbar_item", "Toolbar Items"},
49    {"menu_item", "Menu Items"},
50    {NULL, NULL}
51 };
52
53 static void        _desc_init(void);
54 static void        _desc_shutdown(void);
55 static void        _profile_fetch_from_conf(void);
56 static void        _config_free(void);
57 static void        _config_apply(void);
58 static Elm_Config *_config_user_load(void);
59 static Elm_Config *_config_system_load(void);
60 static void        _config_load(void);
61 static void        _config_update(void);
62 static void        _env_get(void);
63 static size_t      _elm_data_dir_snprintf(char       *dst,
64                                           size_t      size,
65                                           const char *fmt, ...)
66                                           EINA_PRINTF(3, 4);
67 static size_t _elm_user_dir_snprintf(char       *dst,
68                                      size_t      size,
69                                      const char *fmt, ...)
70                                      EINA_PRINTF(3, 4);
71
72 #define ELM_CONFIG_VAL(edd, type, member, dtype) \
73   EET_DATA_DESCRIPTOR_ADD_BASIC(edd, type, #member, member, dtype)
74 #define ELM_CONFIG_LIST(edd, type, member, eddtype) \
75   EET_DATA_DESCRIPTOR_ADD_LIST(edd, type, #member, member, eddtype)
76
77 #ifdef HAVE_ELEMENTARY_X
78 static Ecore_Event_Handler *_prop_change_handler = NULL;
79 static Ecore_Timer *_prop_all_update_timer = NULL;
80 static Ecore_Timer *_prop_change_delay_timer = NULL;
81 static Ecore_X_Window _root_1st = 0;
82 #define ATOM_COUNT 2
83 static Ecore_X_Atom _atom[ATOM_COUNT];
84 static Ecore_X_Atom _atom_config = 0;
85 static const char *_atom_names[ATOM_COUNT] =
86 {
87    "ELM_PROFILE",
88    "ELM_CONFIG"
89 };
90 #define ATOM_E_PROFILE                              0
91 #define ATOM_E_CONFIG                               1
92
93 static Eina_Bool _prop_all_update_cb(void *data __UNUSED__);
94 static Eina_Bool _prop_config_get(void);
95 static void      _prop_config_set(void);
96 static Eina_Bool _prop_change(void *data  __UNUSED__,
97                               int ev_type __UNUSED__,
98                               void       *ev);
99
100 static Eina_Bool
101 _prop_all_update_cb(void *data __UNUSED__)
102 {
103    _prop_config_set();
104    ecore_x_window_prop_string_set(_root_1st, _atom[ATOM_E_PROFILE],
105                                   _elm_profile);
106    _prop_all_update_timer = NULL;
107    return EINA_FALSE;
108 }
109
110 static Eina_Bool
111 _prop_config_get(void)
112 {
113    int size = 0;
114    Ecore_X_Atom atom;
115    char buf[512];
116    unsigned char *data = NULL;
117    Elm_Config *config_data;
118
119    snprintf(buf, sizeof(buf), "ELM_CONFIG_%s", _elm_profile);
120    atom = ecore_x_atom_get(buf);
121    _atom_config = atom;
122    if (!ecore_x_window_prop_property_get(_root_1st,
123                                          atom, _atom[ATOM_E_CONFIG],
124                                          8, &data, &size))
125      {
126         if (!ecore_x_window_prop_property_get(_root_1st,
127                                               _atom[ATOM_E_CONFIG],
128                                               _atom[ATOM_E_CONFIG],
129                                               8, &data, &size))
130           return EINA_FALSE;
131         else
132           _atom_config = _atom[ATOM_E_CONFIG];
133      }
134    else
135      _atom_config = atom;
136    if (size < 1)
137      {
138         free(data);
139         return EINA_FALSE;
140      }
141    config_data = eet_data_descriptor_decode(_config_edd, data, size);
142    free(data);
143    if (!config_data) return EINA_FALSE;
144
145    /* What do we do on version mismatch when someone changes the
146     * config in the rootwindow? */
147    /* Most obvious case, new version and we are still linked to
148     * whatever was there before, we just ignore until user restarts us */
149    if (config_data->config_version > ELM_CONFIG_VERSION)
150      return EINA_TRUE;
151    /* What in the case the version is older? Do we even support those
152     * cases or we only check for equality above? */
153
154    _config_free();
155    _elm_config = config_data;
156    _config_apply();
157    _elm_config_font_overlay_apply();
158    _elm_rescale();
159    _elm_recache();
160    ecore_event_add(ELM_EVENT_CONFIG_ALL_CHANGED, NULL, NULL, NULL);
161    return EINA_TRUE;
162 }
163
164 static void
165 _prop_config_set(void)
166 {
167    unsigned char *config_data = NULL;
168    int size = 0;
169
170    config_data = eet_data_descriptor_encode(_config_edd, _elm_config, &size);
171    if (config_data)
172      {
173         Ecore_X_Atom atom;
174         char buf[512];
175
176         snprintf(buf, sizeof(buf), "ELM_CONFIG_%s", _elm_profile);
177         atom = ecore_x_atom_get(buf);
178         _atom_config = atom;
179
180         ecore_x_window_prop_property_set(_root_1st, _atom_config,
181                                          _atom[ATOM_E_CONFIG], 8,
182                                          config_data, size);
183         free(config_data);
184      }
185 }
186
187 static Eina_Bool
188 _prop_change_delay_cb(void *data __UNUSED__)
189 {
190    char *s;
191
192    s = ecore_x_window_prop_string_get(_root_1st, _atom[ATOM_E_PROFILE]);
193    if (s)
194      {
195         if (_elm_profile) free(_elm_profile);
196         _elm_profile = s;
197      }
198    _prop_config_get();
199    _prop_change_delay_timer = NULL;
200    return EINA_FALSE;
201 }
202
203 static Eina_Bool
204 _prop_change(void *data  __UNUSED__,
205              int ev_type __UNUSED__,
206              void       *ev)
207 {
208    Ecore_X_Event_Window_Property *event = ev;
209
210    if (event->win == _root_1st)
211      {
212         if (event->atom == _atom[ATOM_E_PROFILE])
213           {
214              if (_prop_change_delay_timer) ecore_timer_del(_prop_change_delay_timer);
215              _prop_change_delay_timer = ecore_timer_add(0.1, _prop_change_delay_cb, NULL);
216           }
217         else if (((_atom_config > 0) && (event->atom == _atom_config)) ||
218                  (event->atom == _atom[ATOM_E_CONFIG]))
219           {
220              if (_prop_change_delay_timer) ecore_timer_del(_prop_change_delay_timer);
221              _prop_change_delay_timer = ecore_timer_add(0.1, _prop_change_delay_cb, NULL);
222           }
223      }
224    return ECORE_CALLBACK_PASS_ON;
225 }
226
227 #endif
228
229 static void
230 _desc_init(void)
231 {
232    Eet_Data_Descriptor_Class eddc;
233
234    EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(&eddc, Elm_Config);
235    eddc.func.str_direct_alloc = NULL;
236    eddc.func.str_direct_free = NULL;
237
238    _config_edd = eet_data_descriptor_file_new(&eddc);
239    if (!_config_edd)
240      {
241         printf("EEEK! eet_data_descriptor_file_new() failed\n");
242         return;
243      }
244
245    memset(&eddc, 0, sizeof(eddc)); /* just in case... */
246    EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(&eddc, Elm_Font_Overlay);
247    eddc.func.str_direct_alloc = NULL;
248    eddc.func.str_direct_free = NULL;
249
250    _config_font_overlay_edd = eet_data_descriptor_stream_new(&eddc);
251    if (!_config_font_overlay_edd)
252      {
253         printf("EEEK! eet_data_descriptor_stream_new() failed\n");
254         eet_data_descriptor_free(_config_edd);
255         return;
256      }
257 #define T_INT    EET_T_INT
258 #define T_DOUBLE EET_T_DOUBLE
259 #define T_STRING EET_T_STRING
260 #define T_UCHAR  EET_T_UCHAR
261
262 #define T        Elm_Font_Overlay
263 #define D        _config_font_overlay_edd
264    ELM_CONFIG_VAL(D, T, text_class, EET_T_STRING);
265    ELM_CONFIG_VAL(D, T, font, EET_T_STRING);
266    ELM_CONFIG_VAL(D, T, size, EET_T_INT);
267 #undef T
268 #undef D
269
270 #define T Elm_Config
271 #define D _config_edd
272    ELM_CONFIG_VAL(D, T, config_version, T_INT);
273    ELM_CONFIG_VAL(D, T, engine, T_STRING);
274    ELM_CONFIG_VAL(D, T, vsync, T_UCHAR);
275    ELM_CONFIG_VAL(D, T, thumbscroll_enable, T_UCHAR);
276    ELM_CONFIG_VAL(D, T, thumbscroll_threshold, T_INT);
277    ELM_CONFIG_VAL(D, T, thumbscroll_momentum_threshold, T_DOUBLE);
278    ELM_CONFIG_VAL(D, T, thumbscroll_friction, T_DOUBLE);
279    ELM_CONFIG_VAL(D, T, thumbscroll_bounce_friction, T_DOUBLE);
280    ELM_CONFIG_VAL(D, T, thumbscroll_border_friction, T_DOUBLE);
281    ELM_CONFIG_VAL(D, T, thumbscroll_sensitivity_friction, T_DOUBLE);
282    ELM_CONFIG_VAL(D, T, page_scroll_friction, T_DOUBLE);
283    ELM_CONFIG_VAL(D, T, bring_in_scroll_friction, T_DOUBLE);
284    ELM_CONFIG_VAL(D, T, zoom_friction, T_DOUBLE);
285    ELM_CONFIG_VAL(D, T, thumbscroll_bounce_enable, T_UCHAR);
286    ELM_CONFIG_VAL(D, T, scroll_smooth_amount, T_DOUBLE);
287    ELM_CONFIG_VAL(D, T, scroll_smooth_history_weight, T_DOUBLE);
288    ELM_CONFIG_VAL(D, T, scroll_smooth_future_time, T_DOUBLE);
289    ELM_CONFIG_VAL(D, T, scroll_smooth_time_window, T_DOUBLE);
290    ELM_CONFIG_VAL(D, T, scale, T_DOUBLE);
291    ELM_CONFIG_VAL(D, T, bgpixmap, T_INT);
292    ELM_CONFIG_VAL(D, T, compositing, T_INT);
293    /* EET_DATA_DESCRIPTOR_ADD_LIST(D, T, "font_dirs", font_dirs, sub_edd); */
294    ELM_CONFIG_LIST(D, T, font_overlays, _config_font_overlay_edd);
295    ELM_CONFIG_VAL(D, T, font_hinting, T_INT);
296    ELM_CONFIG_VAL(D, T, cache_flush_poll_interval, T_INT);
297    ELM_CONFIG_VAL(D, T, cache_flush_enable, T_UCHAR);
298    ELM_CONFIG_VAL(D, T, image_cache, T_INT);
299    ELM_CONFIG_VAL(D, T, font_cache, T_INT);
300    ELM_CONFIG_VAL(D, T, edje_cache, T_INT);
301    ELM_CONFIG_VAL(D, T, edje_collection_cache, T_INT);
302    ELM_CONFIG_VAL(D, T, finger_size, T_INT);
303    ELM_CONFIG_VAL(D, T, fps, T_DOUBLE);
304    ELM_CONFIG_VAL(D, T, theme, T_STRING);
305    ELM_CONFIG_VAL(D, T, modules, T_STRING);
306    ELM_CONFIG_VAL(D, T, tooltip_delay, T_DOUBLE);
307    ELM_CONFIG_VAL(D, T, cursor_engine_only, T_UCHAR);
308    ELM_CONFIG_VAL(D, T, focus_highlight_enable, T_UCHAR);
309    ELM_CONFIG_VAL(D, T, focus_highlight_animate, T_UCHAR);
310    ELM_CONFIG_VAL(D, T, toolbar_shrink_mode, T_INT);
311    ELM_CONFIG_VAL(D, T, fileselector_expand_enable, T_UCHAR);
312    ELM_CONFIG_VAL(D, T, inwin_dialogs_enable, T_UCHAR);
313    ELM_CONFIG_VAL(D, T, icon_size, T_INT);
314    ELM_CONFIG_VAL(D, T, longpress_timeout, T_DOUBLE);
315    ELM_CONFIG_VAL(D, T, effect_enable, T_UCHAR);
316    ELM_CONFIG_VAL(D, T, desktop_entry, T_UCHAR);
317    ELM_CONFIG_VAL(D, T, password_show_last, T_UCHAR);
318    ELM_CONFIG_VAL(D, T, password_show_last_timeout, T_DOUBLE);
319    ELM_CONFIG_VAL(D, T, glayer_zoom_finger_factor, T_DOUBLE);
320    ELM_CONFIG_VAL(D, T, glayer_zoom_wheel_factor, T_DOUBLE);
321    ELM_CONFIG_VAL(D, T, glayer_zoom_distance_tolerance, T_DOUBLE);
322    ELM_CONFIG_VAL(D, T, glayer_rotate_angular_tolerance, T_DOUBLE);
323    ELM_CONFIG_VAL(D, T, glayer_line_min_length, T_DOUBLE);
324    ELM_CONFIG_VAL(D, T, glayer_line_distance_tolerance, T_DOUBLE);
325    ELM_CONFIG_VAL(D, T, glayer_line_angular_tolerance, T_DOUBLE);
326    ELM_CONFIG_VAL(D, T, glayer_flick_time_limit_ms, T_INT);
327    ELM_CONFIG_VAL(D, T, glayer_long_tap_start_timeout, T_DOUBLE);
328    ELM_CONFIG_VAL(D, T, access_mode, T_INT);
329    ELM_CONFIG_VAL(D, T, glayer_continues_enable, T_UCHAR);
330 #undef T
331 #undef D
332 #undef T_INT
333 #undef T_DOUBLE
334 #undef T_STRING
335 #undef T_UCHAR
336 }
337
338 static void
339 _desc_shutdown(void)
340 {
341    if (_config_edd)
342      {
343         eet_data_descriptor_free(_config_edd);
344         _config_edd = NULL;
345      }
346
347    if (_config_font_overlay_edd)
348      {
349         eet_data_descriptor_free(_config_font_overlay_edd);
350         _config_font_overlay_edd = NULL;
351      }
352 }
353
354 static int
355 _sort_files_cb(const void *f1,
356                const void *f2)
357 {
358    return strcmp(f1, f2);
359 }
360
361 const char *
362 _elm_config_current_profile_get(void)
363 {
364    return _elm_profile;
365 }
366
367 static size_t
368 _elm_data_dir_snprintf(char       *dst,
369                        size_t      size,
370                        const char *fmt,
371                        ...)
372 {
373    size_t data_dir_len, off;
374    va_list ap;
375
376    data_dir_len = eina_strlcpy(dst, _elm_data_dir, size);
377
378    off = data_dir_len + 1;
379    if (off >= size)
380      goto end;
381
382    va_start(ap, fmt);
383    dst[data_dir_len] = '/';
384
385    off = off + vsnprintf(dst + off, size - off, fmt, ap);
386    va_end(ap);
387
388 end:
389    return off;
390 }
391
392 static size_t
393 _elm_user_dir_snprintf(char       *dst,
394                        size_t      size,
395                        const char *fmt,
396                        ...)
397 {
398    const char *home;
399    size_t user_dir_len, off;
400    va_list ap;
401
402 #ifdef _WIN32
403    home = evil_homedir_get();
404 #else
405    home = getenv("HOME");
406 #endif
407    if (!home)
408      home = "/";
409
410    user_dir_len = eina_str_join_len(dst, size, '/', home, strlen(home),
411                                     ".elementary", sizeof(".elementary") - 1);
412
413    off = user_dir_len + 1;
414    if (off >= size)
415      goto end;
416
417    va_start(ap, fmt);
418    dst[user_dir_len] = '/';
419
420    off = off + vsnprintf(dst + off, size - off, fmt, ap);
421    va_end(ap);
422
423 end:
424    return off;
425 }
426
427 const char *
428 _elm_config_profile_dir_get(const char *prof,
429                             Eina_Bool   is_user)
430 {
431    char buf[PATH_MAX];
432
433    if (!is_user)
434      goto not_user;
435
436    _elm_user_dir_snprintf(buf, sizeof(buf), "config/%s", prof);
437
438    if (ecore_file_is_dir(buf))
439      return strdup(buf);
440
441    return NULL;
442
443 not_user:
444    snprintf(buf, sizeof(buf), "%s/config/%s", _elm_data_dir, prof);
445
446    if (ecore_file_is_dir(buf))
447      return strdup(buf);
448
449    return NULL;
450 }
451
452 Eina_List *
453 _elm_config_font_overlays_list(void)
454 {
455    return _elm_config->font_overlays;
456 }
457
458 void
459 _elm_config_font_overlay_set(const char    *text_class,
460                              const char    *font,
461                              Evas_Font_Size size)
462 {
463    Elm_Font_Overlay *efd;
464    Eina_List *l;
465
466    EINA_LIST_FOREACH(_elm_config->font_overlays, l, efd)
467      {
468         if (strcmp(efd->text_class, text_class))
469           continue;
470
471         if (efd->font) eina_stringshare_del(efd->font);
472         efd->font = eina_stringshare_add(font);
473         efd->size = size;
474         _elm_config->font_overlays =
475           eina_list_promote_list(_elm_config->font_overlays, l);
476         return;
477      }
478
479    /* the text class doesn't exist */
480    efd = calloc(1, sizeof(Elm_Font_Overlay));
481    efd->text_class = eina_stringshare_add(text_class);
482    efd->font = eina_stringshare_add(font);
483    efd->size = size;
484
485    _elm_config->font_overlays = eina_list_prepend(_elm_config->font_overlays,
486                                                   efd);
487 }
488
489 void
490 _elm_config_font_overlay_remove(const char *text_class)
491 {
492    Elm_Font_Overlay *efd;
493    Eina_List *l;
494
495    EINA_LIST_FOREACH(_elm_config->font_overlays, l, efd)
496      {
497         if (strcmp(efd->text_class, text_class))
498           continue;
499
500         _elm_config->font_overlays =
501           eina_list_remove_list(_elm_config->font_overlays, l);
502         if (efd->text_class) eina_stringshare_del(efd->text_class);
503         if (efd->font) eina_stringshare_del(efd->font);
504         free(efd);
505
506         return;
507      }
508 }
509
510 void
511 _elm_config_font_overlay_apply(void)
512 {
513    Elm_Font_Overlay *efd;
514    Eina_List *l;
515    int i;
516
517    for (i = 0; _elm_text_classes[i].desc; i++)
518      edje_text_class_del(_elm_text_classes[i].name);
519
520    EINA_LIST_FOREACH(_elm_config->font_overlays, l, efd)
521      edje_text_class_set(efd->text_class, efd->font, efd->size);
522 }
523
524 Eina_List *
525 _elm_config_text_classes_get(void)
526 {
527    Eina_List *ret = NULL;
528    int i;
529
530    for (i = 0; _elm_text_classes[i].desc; i++)
531      {
532         Elm_Text_Class *tc;
533         tc = malloc(sizeof(*tc));
534
535         *tc = _elm_text_classes[i];
536
537         ret = eina_list_append(ret, tc);
538      }
539
540    return ret;
541 }
542
543 void
544 _elm_config_text_classes_free(Eina_List *l)
545 {
546    Elm_Text_Class *tc;
547
548    EINA_LIST_FREE(l, tc)
549      free(tc);
550 }
551
552 Eina_List *
553 _elm_config_profiles_list(void)
554 {
555    const Eina_File_Direct_Info *info;
556    Eina_List *flist = NULL;
557    Eina_Iterator *file_it;
558    char buf[PATH_MAX];
559    const char *dir;
560    size_t len;
561
562    len = _elm_user_dir_snprintf(buf, sizeof(buf), "config");
563
564    file_it = eina_file_direct_ls(buf);
565    if (!file_it)
566      goto sys;
567
568    buf[len] = '/';
569    len++;
570
571    len = sizeof(buf) - len;
572
573    EINA_ITERATOR_FOREACH(file_it, info)
574      {
575         if (info->name_length >= len)
576           continue;
577
578         if (info->type == EINA_FILE_DIR)
579           {
580              flist =
581                eina_list_sorted_insert(flist, _sort_files_cb,
582                                        eina_stringshare_add(info->path +
583                                                             info->name_start));
584           }
585      }
586
587    eina_iterator_free(file_it);
588
589 sys:
590    len = eina_str_join_len(buf, sizeof(buf), '/', _elm_data_dir,
591                            strlen(_elm_data_dir), "config",
592                            sizeof("config") - 1);
593
594    file_it = eina_file_direct_ls(buf);
595    if (!file_it)
596      goto list_free;
597
598    buf[len] = '/';
599    len++;
600
601    len = sizeof(buf) - len;
602    EINA_ITERATOR_FOREACH(file_it, info)
603      {
604         if (info->name_length >= len)
605           continue;
606
607         switch (info->type)
608           {
609            case EINA_FILE_DIR:
610            {
611               const Eina_List *l;
612               const char *tmp;
613
614               EINA_LIST_FOREACH(flist, l, tmp)
615                 if (!strcmp(info->path + info->name_start, tmp))
616                   break;
617
618               if (!l)
619                 flist =
620                   eina_list_sorted_insert(flist, _sort_files_cb,
621                                           eina_stringshare_add(info->path +
622                                                                info->name_start));
623            }
624            break;
625
626            default:
627              continue;
628           }
629      }
630    eina_iterator_free(file_it);
631    return flist;
632
633 list_free:
634    EINA_LIST_FREE(flist, dir)
635      eina_stringshare_del(dir);
636
637    return NULL;
638 }
639
640 static void
641 _profile_fetch_from_conf(void)
642 {
643    char buf[PATH_MAX], *p, *s;
644    Eet_File *ef = NULL;
645    int len = 0;
646
647    _elm_profile = strdup("default");
648
649    // if env var - use profile without question
650    s = getenv("ELM_PROFILE");
651    if (s)
652      {
653         free(_elm_profile);
654         _elm_profile = strdup(s);
655         return;
656      }
657
658    // user profile
659    _elm_user_dir_snprintf(buf, sizeof(buf), "config/profile.cfg");
660    ef = eet_open(buf, EET_FILE_MODE_READ);
661    if (ef)
662      {
663         p = eet_read(ef, "config", &len);
664         if (p)
665           {
666              free(_elm_profile);
667              _elm_profile = malloc(len + 1);
668              memcpy(_elm_profile, p, len);
669              _elm_profile[len] = 0;
670              free(p);
671           }
672         eet_close(ef);
673         if (!p) ef = NULL;
674      }
675    if (ef) return;
676
677    // system profile
678    _elm_data_dir_snprintf(buf, sizeof(buf), "config/profile.cfg");
679    ef = eet_open(buf, EET_FILE_MODE_READ);
680    if (ef)
681      {
682         p = eet_read(ef, "config", &len);
683         if (p)
684           {
685              free(_elm_profile);
686              _elm_profile = malloc(len + 1);
687              memcpy(_elm_profile, p, len);
688              _elm_profile[len] = 0;
689              free(p);
690           }
691         eet_close(ef);
692      }
693 }
694
695 static void
696 _config_free(void)
697 {
698    Elm_Font_Overlay *fo;
699    const char *fontdir;
700
701    if (!_elm_config) return;
702    EINA_LIST_FREE(_elm_config->font_dirs, fontdir)
703      {
704         eina_stringshare_del(fontdir);
705      }
706    if (_elm_config->engine) eina_stringshare_del(_elm_config->engine);
707    EINA_LIST_FREE(_elm_config->font_overlays, fo)
708      {
709         if (fo->text_class) eina_stringshare_del(fo->text_class);
710         if (fo->font) eina_stringshare_del(fo->font);
711         free(fo);
712      }
713    if (_elm_config->theme) eina_stringshare_del(_elm_config->theme);
714    if (_elm_config->modules) eina_stringshare_del(_elm_config->modules);
715    free(_elm_config);
716    _elm_config = NULL;
717 }
718
719 static void
720 _config_apply(void)
721 {
722    _elm_theme_parse(NULL, _elm_config->theme);
723    ecore_animator_frametime_set(1.0 / _elm_config->fps);
724 }
725
726 static void
727 _config_sub_apply(void)
728 {
729    edje_frametime_set(1.0 / _elm_config->fps);
730    edje_scale_set(_elm_config->scale);
731    edje_password_show_last_set(_elm_config->password_show_last);
732    edje_password_show_last_timeout_set(_elm_config->password_show_last_timeout);
733    if (_elm_config->modules) _elm_module_parse(_elm_config->modules);
734 }
735
736 static Eina_Bool
737 _elm_cache_flush_cb(void *data __UNUSED__)
738 {
739    elm_all_flush();
740    return ECORE_CALLBACK_RENEW;
741 }
742
743 /* kind of abusing this call right now -- shared between all of those
744  * properties -- but they are not meant to be called that periodically
745  * anyway */
746 void
747 _elm_recache(void)
748 {
749    Eina_List *l;
750    Evas_Object *win;
751
752    elm_all_flush();
753
754    EINA_LIST_FOREACH(_elm_win_list, l, win)
755      {
756         Evas *e = evas_object_evas_get(win);
757         evas_image_cache_set(e, _elm_config->image_cache * 1024);
758         evas_font_cache_set(e, _elm_config->font_cache * 1024);
759      }
760    edje_file_cache_set(_elm_config->edje_cache);
761    edje_collection_cache_set(_elm_config->edje_collection_cache);
762
763    if (_elm_cache_flush_poller)
764      {
765         ecore_poller_del(_elm_cache_flush_poller);
766         _elm_cache_flush_poller = NULL;
767      }
768    if (_elm_config->cache_flush_enable)
769      {
770         if (_elm_config->cache_flush_poll_interval > 0)
771           {
772              _elm_cache_flush_poller =
773                 ecore_poller_add(ECORE_POLLER_CORE,
774                                  _elm_config->cache_flush_poll_interval,
775                                  _elm_cache_flush_cb, NULL);
776           }
777      }
778 }
779
780 static Elm_Config *
781 _config_user_load(void)
782 {
783    Elm_Config *cfg = NULL;
784    Eet_File *ef;
785    char buf[PATH_MAX];
786
787    _elm_user_dir_snprintf(buf, sizeof(buf), "config/%s/base.cfg",
788                           _elm_profile);
789
790    ef = eet_open(buf, EET_FILE_MODE_READ);
791    if (ef)
792      {
793         cfg = eet_data_read(ef, _config_edd, "config");
794         eet_close(ef);
795      }
796    return cfg;
797 }
798
799 static Elm_Config *
800 _config_system_load(void)
801 {
802    Elm_Config *cfg = NULL;
803    Eet_File *ef;
804    char buf[PATH_MAX];
805
806    _elm_data_dir_snprintf(buf, sizeof(buf), "config/%s/base.cfg",
807                           _elm_profile);
808
809    ef = eet_open(buf, EET_FILE_MODE_READ);
810    if (ef)
811      {
812         cfg = eet_data_read(ef, _config_edd, "config");
813         eet_close(ef);
814      }
815    return cfg;
816 }
817
818 static void
819 _config_load(void)
820 {
821    _elm_config = _config_user_load();
822    if (_elm_config)
823      {
824         if (_elm_config->config_version < ELM_CONFIG_VERSION)
825           _config_update();
826         return;
827      }
828
829    /* no user config, fallback for system. No need to check version for
830     * this one, if it's not the right one, someone screwed up at the time
831     * of installing it */
832    _elm_config = _config_system_load();
833    if (_elm_config) return;
834    /* FIXME: config load could have failed because of a non-existent
835     * profile. Fallback to default before moving on */
836
837    // config load fail - defaults
838    /* XXX: do these make sense? Only if it's valid to install the lib
839     * without the config, but do we want that? */
840    _elm_config = ELM_NEW(Elm_Config);
841    _elm_config->config_version = ELM_CONFIG_VERSION;
842    _elm_config->engine = eina_stringshare_add("software_x11");
843    _elm_config->vsync = 0;
844    _elm_config->thumbscroll_enable = EINA_TRUE;
845    _elm_config->thumbscroll_threshold = 24;
846    _elm_config->thumbscroll_momentum_threshold = 100.0;
847    _elm_config->thumbscroll_friction = 1.0;
848    _elm_config->thumbscroll_bounce_friction = 0.5;
849    _elm_config->thumbscroll_bounce_enable = EINA_TRUE;
850    _elm_config->page_scroll_friction = 0.5;
851    _elm_config->bring_in_scroll_friction = 0.5;
852    _elm_config->zoom_friction = 0.5;
853    _elm_config->thumbscroll_border_friction = 0.5;
854    _elm_config->thumbscroll_sensitivity_friction = 0.25; // magic number! just trial and error shows this makes it behave "nicer" and not run off at high speed all the time
855    _elm_config->scroll_smooth_amount = 1.0;
856    _elm_config->scroll_smooth_history_weight = 0.3;
857    _elm_config->scroll_smooth_future_time = 0.0;
858    _elm_config->scroll_smooth_time_window = 0.2;
859    _elm_config->scale = 1.0;
860    _elm_config->bgpixmap = 0;
861    _elm_config->compositing = 1;
862    _elm_config->font_hinting = 2;
863    _elm_config->cache_flush_poll_interval = 512;
864    _elm_config->cache_flush_enable = EINA_TRUE;
865    _elm_config->font_dirs = NULL;
866    _elm_config->image_cache = 4096;
867    _elm_config->font_cache = 512;
868    _elm_config->edje_cache = 32;
869    _elm_config->edje_collection_cache = 64;
870    _elm_config->finger_size = 40;
871    _elm_config->fps = 60.0;
872    _elm_config->theme = eina_stringshare_add("default");
873    _elm_config->modules = NULL;
874    _elm_config->tooltip_delay = 1.0;
875    _elm_config->cursor_engine_only = EINA_TRUE;
876    _elm_config->focus_highlight_enable = EINA_FALSE;
877    _elm_config->focus_highlight_animate = EINA_TRUE;
878    _elm_config->toolbar_shrink_mode = 2;
879    _elm_config->fileselector_expand_enable = EINA_FALSE;
880    _elm_config->inwin_dialogs_enable = EINA_FALSE;
881    _elm_config->icon_size = 32;
882    _elm_config->longpress_timeout = 1.0;
883    _elm_config->effect_enable = EINA_TRUE;
884    _elm_config->desktop_entry = EINA_FALSE;
885    _elm_config->is_mirrored = EINA_FALSE; /* Read sys value in env_get() */
886    _elm_config->password_show_last = EINA_FALSE;
887    _elm_config->password_show_last_timeout = 2.0;
888    _elm_config->glayer_zoom_finger_factor = 1.0;
889    _elm_config->glayer_zoom_wheel_factor = 0.05;
890    _elm_config->glayer_zoom_distance_tolerance = 1.0; /* 1 times elm_finger_size_get() */
891    _elm_config->glayer_rotate_angular_tolerance = 0.034906585;     /* Represents 2 DEG */
892    _elm_config->glayer_line_min_length = 1.0;         /* 1 times elm_finger_size_get() */
893    _elm_config->glayer_line_distance_tolerance = 3.0; /* 3 times elm_finger_size_get() */
894    _elm_config->glayer_line_angular_tolerance = 0.34906585;       /* Represents 20 DEG */
895    _elm_config->glayer_flick_time_limit_ms = 60;              /* 60 ms to finish flick */
896    _elm_config->glayer_long_tap_start_timeout = 1.2;   /* 1.2 second to start long-tap */
897    _elm_config->glayer_continues_enable = EINA_TRUE;      /* Continue gestures default */
898 }
899
900 static const char *
901 _elm_config_eet_close_error_get(Eet_File *ef,
902                                 char     *file)
903 {
904    Eet_Error err;
905    const char *erstr = NULL;
906
907    err = eet_close(ef);
908    switch (err)
909      {
910       case EET_ERROR_WRITE_ERROR:
911         erstr = "An error occurred while saving Elementary's "
912                 "settings to disk. The error could not be "
913                 "deterimined. The file where the error occurred was: "
914                 "%s. This file has been deleted to avoid corrupt data.";
915         break;
916
917       case EET_ERROR_WRITE_ERROR_FILE_TOO_BIG:
918         erstr = "Elementary's settings files are too big "
919                 "for the file system they are being saved to. "
920                 "This error is very strange as the files should "
921                 "be extremely small. Please check the settings "
922                 "for your home directory. "
923                 "The file where the error occurred was: %s ."
924                 "This file has been deleted to avoid corrupt data.";
925         break;
926
927       case EET_ERROR_WRITE_ERROR_IO_ERROR:
928         erstr = "An output error occurred when writing the settings "
929                 "files for Elementary. Your disk is having troubles "
930                 "and possibly needs replacement. "
931                 "The file where the error occurred was: %s ."
932                 "This file has been deleted to avoid corrupt data.";
933         break;
934
935       case EET_ERROR_WRITE_ERROR_OUT_OF_SPACE:
936         erstr = "Elementary cannot write its settings file "
937                 "because it ran out of space to write the file. "
938                 "You have either run out of disk space or have "
939                 "gone over your quota limit. "
940                 "The file where the error occurred was: %s ."
941                 "This file has been deleted to avoid corrupt data.";
942         break;
943
944       case EET_ERROR_WRITE_ERROR_FILE_CLOSED:
945         erstr = "Elementary unexpectedly had the settings file "
946                 "it was writing closed on it. This is very unusual. "
947                 "The file where the error occurred was: %s "
948                 "This file has been deleted to avoid corrupt data.";
949         break;
950
951       default:
952         break;
953      }
954    if (erstr)
955      {
956         /* delete any partially-written file */
957          ecore_file_unlink(file);
958          return strdup(erstr);
959      }
960
961    return NULL;
962 }
963
964 static Eina_Bool
965 _elm_config_profile_save(void)
966 {
967    char buf[4096], buf2[4096];
968    int ok = 0, ret;
969    const char *err;
970    Eet_File *ef;
971    size_t len;
972
973    len = _elm_user_dir_snprintf(buf, sizeof(buf), "config/profile.cfg");
974    if (len + 1 >= sizeof(buf))
975      return EINA_FALSE;
976
977    len = _elm_user_dir_snprintf(buf2, sizeof(buf2), "config/profile.cfg.tmp");
978    if (len + 1 >= sizeof(buf2))
979      return EINA_FALSE;
980
981    ef = eet_open(buf2, EET_FILE_MODE_WRITE);
982    if (!ef)
983      return EINA_FALSE;
984
985    ok = eet_write(ef, "config", _elm_profile, strlen(_elm_profile), 0);
986    if (!ok)
987      goto err;
988
989    err = _elm_config_eet_close_error_get(ef, buf2);
990    if (err)
991      {
992         ERR("%s", err);
993         free((void *)err);
994         goto err;
995      }
996
997    ret = ecore_file_mv(buf2, buf);
998    if (!ret)
999      {
1000         ERR("Error saving Elementary's configuration file");
1001         goto err;
1002      }
1003
1004    ecore_file_unlink(buf2);
1005    return EINA_TRUE;
1006
1007 err:
1008    ecore_file_unlink(buf2);
1009    return EINA_FALSE;
1010 }
1011
1012 Eina_Bool
1013 _elm_config_save(void)
1014 {
1015    char buf[4096], buf2[4096];
1016    int ok = 0, ret;
1017    const char *err;
1018    Eet_File *ef;
1019    size_t len;
1020
1021    len = _elm_user_dir_snprintf(buf, sizeof(buf), "config/%s", _elm_profile);
1022    if (len + 1 >= sizeof(buf))
1023      return EINA_FALSE;
1024
1025    ok = ecore_file_mkpath(buf);
1026    if (!ok)
1027      {
1028         ERR("Problem acessing Elementary's user configuration directory: %s",
1029             buf);
1030         return EINA_FALSE;
1031      }
1032
1033    if (!_elm_config_profile_save())
1034      return EINA_FALSE;
1035
1036    buf[len] = '/';
1037    len++;
1038
1039    if (len + sizeof("base.cfg") >= sizeof(buf) - len)
1040      return EINA_FALSE;
1041
1042    memcpy(buf + len, "base.cfg", sizeof("base.cfg"));
1043    len += sizeof("base.cfg") - 1;
1044
1045    if (len + sizeof(".tmp") >= sizeof(buf))
1046      return EINA_FALSE;
1047
1048    memcpy(buf2, buf, len);
1049    memcpy(buf2 + len, ".tmp", sizeof(".tmp"));
1050
1051    ef = eet_open(buf2, EET_FILE_MODE_WRITE);
1052    if (!ef)
1053      return EINA_FALSE;
1054
1055    ok = eet_data_write(ef, _config_edd, "config", _elm_config, 1);
1056    if (!ok)
1057      goto err;
1058
1059    err = _elm_config_eet_close_error_get(ef, buf2);
1060    if (err)
1061      {
1062         ERR("%s", err);
1063         free((void *)err);
1064         goto err;
1065      }
1066
1067    ret = ecore_file_mv(buf2, buf);
1068    if (!ret)
1069      {
1070         ERR("Error saving Elementary's configuration file");
1071         goto err;
1072      }
1073
1074    ecore_file_unlink(buf2);
1075    return EINA_TRUE;
1076
1077 err:
1078    ecore_file_unlink(buf2);
1079    return EINA_FALSE;
1080 }
1081
1082 static void
1083 _config_update(void)
1084 {
1085    Elm_Config *tcfg;
1086
1087    tcfg = _config_system_load();
1088    if (!tcfg)
1089      {
1090         /* weird profile or something? We should probably fill
1091          * with hardcoded defaults, or get from default previx */
1092           return;
1093      }
1094 #define IFCFG(v)   if ((_elm_config->config_version & 0xffff) < (v)) {
1095 #define IFCFGELSE } else {
1096 #define IFCFGEND  }
1097 #define COPYVAL(x) do {_elm_config->x = tcfg->x; } while(0)
1098 #define COPYPTR(x) do {_elm_config->x = tcfg->x; tcfg->x = NULL; } while(0)
1099 #define COPYSTR(x) COPYPTR(x)
1100
1101      /* we also need to update for property changes in the root window
1102       * if needed, but that will be dependent on new properties added
1103       * with each version */
1104
1105      IFCFG(0x0003);
1106      COPYVAL(longpress_timeout);
1107      IFCFGEND;
1108
1109 #undef COPYSTR
1110 #undef COPYPTR
1111 #undef COPYVAL
1112 #undef IFCFGEND
1113 #undef IFCFGELSE
1114 #undef IFCFG
1115
1116      /* after updating user config, we must save */
1117 }
1118
1119 static void
1120 _env_get(void)
1121 {
1122    char *s;
1123    double friction;
1124
1125    s = getenv("ELM_ENGINE");
1126    if (s)
1127      {
1128         if ((!strcasecmp(s, "x11")) ||
1129             (!strcasecmp(s, "x")) ||
1130             (!strcasecmp(s, "software-x11")) ||
1131             (!strcasecmp(s, "software_x11")))
1132           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_X11);
1133         else if ((!strcasecmp(s, "opengl")) ||
1134                  (!strcasecmp(s, "gl")) ||
1135                  (!strcasecmp(s, "opengl-x11")) ||
1136                  (!strcasecmp(s, "opengl_x11")))
1137           eina_stringshare_replace(&_elm_config->engine, ELM_OPENGL_X11);
1138         else if ((!strcasecmp(s, "x11-8")) ||
1139                  (!strcasecmp(s, "x8")) ||
1140                  (!strcasecmp(s, "software-8-x11")) ||
1141                  (!strcasecmp(s, "software_8_x11")))
1142           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_8_X11);
1143         else if ((!strcasecmp(s, "x11-16")) ||
1144                  (!strcasecmp(s, "x16")) ||
1145                  (!strcasecmp(s, "software-16-x11")) ||
1146                  (!strcasecmp(s, "software_16_x11")))
1147           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_16_X11);
1148 /*
1149         else if ((!strcasecmp(s, "xrender")) ||
1150                  (!strcasecmp(s, "xr")) ||
1151                  (!strcasecmp(s, "xrender-x11")) ||
1152                  (!strcasecmp(s, "xrender_x11")))
1153           eina_stringshare_replace(&_elm_config->engine, ELM_XRENDER_X11);
1154  */
1155         else if ((!strcasecmp(s, "fb")) ||
1156                  (!strcasecmp(s, "software-fb")) ||
1157                  (!strcasecmp(s, "software_fb")))
1158           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_FB);
1159         else if ((!strcasecmp(s, "directfb")) ||
1160                  (!strcasecmp(s, "dfb")))
1161           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_DIRECTFB);
1162         else if ((!strcasecmp(s, "sdl")) ||
1163                  (!strcasecmp(s, "software-sdl")) ||
1164                  (!strcasecmp(s, "software_sdl")))
1165           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_SDL);
1166         else if ((!strcasecmp(s, "sdl-16")) ||
1167                  (!strcasecmp(s, "software-16-sdl")) ||
1168                  (!strcasecmp(s, "software_16_sdl")))
1169           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_16_SDL);
1170         else if ((!strcasecmp(s, "opengl-sdl")) ||
1171                  (!strcasecmp(s, "opengl_sdl")) ||
1172                  (!strcasecmp(s, "gl-sdl")) ||
1173                  (!strcasecmp(s, "gl_sdl")))
1174           eina_stringshare_replace(&_elm_config->engine, ELM_OPENGL_SDL);
1175         else if ((!strcasecmp(s, "gdi")) ||
1176                  (!strcasecmp(s, "software-gdi")) ||
1177                  (!strcasecmp(s, "software_gdi")))
1178           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_WIN32);
1179         else if ((!strcasecmp(s, "wince-gdi")) ||
1180                  (!strcasecmp(s, "software-16-wince-gdi")) ||
1181                  (!strcasecmp(s, "software_16_wince_gdi")))
1182           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_16_WINCE);
1183         else if (!strcasecmp(s, "buffer"))
1184           eina_stringshare_replace(&_elm_config->engine, ELM_BUFFER);
1185         else if ((!strncmp(s, "shot:", 5)))
1186           eina_stringshare_replace(&_elm_config->engine, s);
1187         else if ((!strcasecmp(s, "ews")))
1188           eina_stringshare_replace(&_elm_config->engine, ELM_EWS);
1189      }
1190
1191    s = getenv("ELM_VSYNC");
1192    if (s) _elm_config->vsync = !!atoi(s);
1193
1194    s = getenv("ELM_THUMBSCROLL_ENABLE");
1195    if (s) _elm_config->thumbscroll_enable = !!atoi(s);
1196    s = getenv("ELM_THUMBSCROLL_THRESHOLD");
1197    if (s) _elm_config->thumbscroll_threshold = atoi(s);
1198    // FIXME: floatformat locale issues here 1.0 vs 1,0 - should just be 1.0
1199    s = getenv("ELM_THUMBSCROLL_MOMENTUM_THRESHOLD");
1200    if (s) _elm_config->thumbscroll_momentum_threshold = atof(s);
1201    s = getenv("ELM_THUMBSCROLL_FRICTION");
1202    if (s) _elm_config->thumbscroll_friction = atof(s);
1203    s = getenv("ELM_THUMBSCROLL_BOUNCE_ENABLE");
1204    if (s) _elm_config->thumbscroll_bounce_enable = !!atoi(s);
1205    s = getenv("ELM_THUMBSCROLL_BOUNCE_FRICTION");
1206    if (s) _elm_config->thumbscroll_bounce_friction = atof(s);
1207    s = getenv("ELM_PAGE_SCROLL_FRICTION");
1208    if (s) _elm_config->page_scroll_friction = atof(s);
1209    s = getenv("ELM_BRING_IN_SCROLL_FRICTION");
1210    if (s) _elm_config->bring_in_scroll_friction = atof(s);
1211    s = getenv("ELM_ZOOM_FRICTION");
1212    if (s) _elm_config->zoom_friction = atof(s);
1213    s = getenv("ELM_THUMBSCROLL_BORDER_FRICTION");
1214    if (s)
1215      {
1216         friction = atof(s);
1217         if (friction < 0.0)
1218           friction = 0.0;
1219
1220         if (friction > 1.0)
1221           friction = 1.0;
1222
1223         _elm_config->thumbscroll_border_friction = friction;
1224      }
1225    s = getenv("ELM_THUMBSCROLL_SENSITIVITY_FRICTION");
1226    if (s)
1227      {
1228         friction = atof(s);
1229         if (friction < 0.1)
1230           friction = 0.1;
1231
1232         if (friction > 1.0)
1233           friction = 1.0;
1234
1235         _elm_config->thumbscroll_sensitivity_friction = friction;
1236      }
1237    s = getenv("ELM_SCROLL_SMOOTH_AMOUNT");
1238    if (s) _elm_config->scroll_smooth_amount = atof(s);
1239    s = getenv("ELM_SCROLL_SMOOTH_HISTORY_WEIGHT");
1240    if (s) _elm_config->scroll_smooth_history_weight = atof(s);
1241    s = getenv("ELM_SCROLL_SMOOTH_FUTURE_TIME");
1242    if (s) _elm_config->scroll_smooth_future_time = atof(s);
1243    s = getenv("ELM_SCROLL_SMOOTH_TIME_WINDOW");
1244    if (s) _elm_config->scroll_smooth_time_window = atof(s);
1245    s = getenv("ELM_THEME");
1246    if (s) eina_stringshare_replace(&_elm_config->theme, s);
1247
1248    s = getenv("ELM_FONT_HINTING");
1249    if (s)
1250      {
1251         if      (!strcasecmp(s, "none")) _elm_config->font_hinting = 0;
1252         else if (!strcasecmp(s, "auto"))
1253           _elm_config->font_hinting = 1;
1254         else if (!strcasecmp(s, "bytecode"))
1255           _elm_config->font_hinting = 2;
1256      }
1257
1258    s = getenv("ELM_FONT_PATH");
1259    if (s)
1260      {
1261         const char *p, *pp;
1262         char *buf2;
1263
1264         EINA_LIST_FREE(_elm_config->font_dirs, p)
1265           {
1266              eina_stringshare_del(p);
1267           }
1268
1269         buf2 = alloca(strlen(s) + 1);
1270         p = s;
1271         pp = p;
1272         for (;; )
1273           {
1274              if ((*p == ':') || (*p == 0))
1275                {
1276                   int len;
1277
1278                   len = p - pp;
1279                   strncpy(buf2, pp, len);
1280                   buf2[len] = 0;
1281                   _elm_config->font_dirs =
1282                     eina_list_append(_elm_config->font_dirs,
1283                                      eina_stringshare_add(buf2));
1284                   if (*p == 0) break;
1285                   p++;
1286                   pp = p;
1287                }
1288              else
1289                {
1290                   if (*p == 0) break;
1291                   p++;
1292                }
1293           }
1294      }
1295
1296    s = getenv("ELM_IMAGE_CACHE");
1297    if (s) _elm_config->image_cache = atoi(s);
1298
1299    s = getenv("ELM_FONT_CACHE");
1300    if (s) _elm_config->font_cache = atoi(s);
1301
1302    s = getenv("ELM_SCALE");
1303    if (s) _elm_config->scale = atof(s);
1304
1305    _elm_config->finger_size =
1306      (double)_elm_config->finger_size * _elm_config->scale;
1307    s = getenv("ELM_FINGER_SIZE");
1308    if (s) _elm_config->finger_size = atoi(s);
1309
1310    s = getenv("ELM_PASSWORD_SHOW_LAST");
1311    if (s) _elm_config->password_show_last = !!atoi(s);
1312
1313    s = getenv("ELM_PASSWORD_SHOW_LAST_TIMEOUT");
1314    if (s)
1315      {
1316         double pw_show_last_timeout = atof(s);
1317         if (pw_show_last_timeout >= 0.0)
1318           _elm_config->password_show_last_timeout = pw_show_last_timeout;
1319      }
1320
1321    s = getenv("ELM_FPS");
1322    if (s) _elm_config->fps = atof(s);
1323    if (_elm_config->fps < 1.0) _elm_config->fps = 1.0;
1324
1325    s = getenv("ELM_MODULES");
1326    if (s) eina_stringshare_replace(&_elm_config->modules, s);
1327
1328    /* Get RTL orientation from system */
1329    setlocale(LC_ALL, "");
1330    bindtextdomain(PACKAGE, LOCALE_DIR);
1331    _elm_config->is_mirrored = !strcmp(E_("default:LTR"), "default:RTL");
1332
1333    s = getenv("ELM_TOOLTIP_DELAY");
1334    if (s)
1335      {
1336         double delay = atof(s);
1337         if (delay >= 0.0)
1338           _elm_config->tooltip_delay = delay;
1339      }
1340
1341    s = getenv("ELM_CURSOR_ENGINE_ONLY");
1342    if (s) _elm_config->cursor_engine_only = !!atoi(s);
1343
1344    s = getenv("ELM_FOCUS_HIGHLIGHT_ENABLE");
1345    if (s) _elm_config->focus_highlight_enable = !!atoi(s);
1346
1347    s = getenv("ELM_FOCUS_HIGHLIGHT_ANIMATE");
1348    if (s) _elm_config->focus_highlight_animate = !!atoi(s);
1349
1350    s = getenv("ELM_TOOLBAR_SHRINK_MODE");
1351    if (s) _elm_config->toolbar_shrink_mode = atoi(s);
1352
1353    s = getenv("ELM_FILESELECTOR_EXPAND_ENABLE");
1354    if (s) _elm_config->fileselector_expand_enable = !!atoi(s);
1355
1356    s = getenv("ELM_INWIN_DIALOGS_ENABLE");
1357    if (s) _elm_config->inwin_dialogs_enable = !!atoi(s);
1358
1359    s = getenv("ELM_ICON_SIZE");
1360    if (s) _elm_config->icon_size = atoi(s);
1361
1362    s = getenv("ELM_LONGPRESS_TIMEOUT");
1363    if (s) _elm_config->longpress_timeout = atof(s);
1364    if (_elm_config->longpress_timeout < 0.0)
1365      _elm_config->longpress_timeout = 0.0;
1366
1367    s = getenv("ELM_EFFECT_ENABLE");
1368    if (s) _elm_config->effect_enable = !!atoi(s);
1369
1370    s = getenv("ELM_DESKTOP_ENTRY");
1371    if (s) _elm_config->desktop_entry = !!atoi(s);
1372    s = getenv("ELM_ACCESS_MODE");
1373    if (s) _elm_config->access_mode = ELM_ACCESS_MODE_ON;
1374 }
1375
1376 EAPI Eina_Bool
1377 elm_mirrored_get(void)
1378 {
1379    return _elm_config->is_mirrored;
1380 }
1381
1382 EAPI void
1383 elm_mirrored_set(Eina_Bool mirrored)
1384 {
1385    _elm_config->is_mirrored = mirrored;
1386    _elm_rescale();
1387 }
1388
1389 static void
1390 _translation_init()
1391 {
1392 #ifdef ENABLE_NLS
1393    const char *cur_dom = textdomain(NULL);
1394    const char *trans_comment = gettext("");
1395    const char *msg_locale = setlocale(LC_MESSAGES, NULL);
1396
1397    /* Same concept as what glib does:
1398     * We shouldn't translate if there are no translations for the
1399     * application in the current locale + domain. (Unless locale is
1400     * en_/C where translating only parts of the interface make some
1401     * sense).
1402     */
1403    _elm_config->translate = !(strcmp (cur_dom, "messages") &&
1404          !*trans_comment && strncmp (msg_locale, "en_", 3) &&
1405          strcmp (msg_locale, "C"));
1406 #endif
1407 }
1408
1409 void
1410 _elm_config_init(void)
1411 {
1412    if (!ELM_EVENT_CONFIG_ALL_CHANGED)
1413       ELM_EVENT_CONFIG_ALL_CHANGED = ecore_event_type_new();
1414    _desc_init();
1415    _profile_fetch_from_conf();
1416    _config_load();
1417    _translation_init();
1418    _env_get();
1419    _config_apply();
1420    _elm_config_font_overlay_apply();
1421    _elm_recache();
1422 }
1423
1424 void
1425 _elm_config_sub_shutdown(void)
1426 {
1427 #define ENGINE_COMPARE(name) (!strcmp(_elm_config->engine, name))
1428    if (ENGINE_COMPARE(ELM_SOFTWARE_X11) ||
1429        ENGINE_COMPARE(ELM_SOFTWARE_16_X11) ||
1430        ENGINE_COMPARE(ELM_XRENDER_X11) ||
1431        ENGINE_COMPARE(ELM_OPENGL_X11))
1432 #undef ENGINE_COMPARE
1433      {
1434 #ifdef HAVE_ELEMENTARY_X
1435         ecore_x_disconnect();
1436 #endif
1437      }
1438 }
1439
1440 void
1441 _elm_config_sub_init(void)
1442 {
1443 #define ENGINE_COMPARE(name) (!strcmp(_elm_config->engine, name))
1444    if (ENGINE_COMPARE(ELM_SOFTWARE_X11) ||
1445        ENGINE_COMPARE(ELM_SOFTWARE_16_X11) ||
1446        ENGINE_COMPARE(ELM_XRENDER_X11) ||
1447        ENGINE_COMPARE(ELM_OPENGL_X11))
1448 #undef ENGINE_COMPARE
1449      {
1450 #ifdef HAVE_ELEMENTARY_X
1451         if (!ecore_x_init(NULL))
1452           {
1453              ERR("Cannot connect to X11 display. check $DISPLAY variable");
1454              exit(1);
1455           }
1456         _root_1st = ecore_x_window_root_first_get();
1457
1458         if (!ecore_x_screen_is_composited(0))
1459           _elm_config->compositing = 0;
1460
1461         ecore_x_atoms_get(_atom_names, ATOM_COUNT, _atom);
1462         ecore_x_event_mask_set(_root_1st,
1463                                ECORE_X_EVENT_MASK_WINDOW_PROPERTY);
1464         _prop_change_handler = ecore_event_handler_add
1465             (ECORE_X_EVENT_WINDOW_PROPERTY, _prop_change, NULL);
1466         if (!getenv("ELM_PROFILE"))
1467           {
1468              char *s;
1469
1470              s = ecore_x_window_prop_string_get(_root_1st,
1471                                                 _atom[ATOM_E_PROFILE]);
1472              if (s)
1473                {
1474                   int changed = 0;
1475
1476                   if (_elm_profile)
1477                     {
1478                        if (strcmp(_elm_profile, s)) changed = 1;
1479                        free(_elm_profile);
1480                     }
1481                   _elm_profile = s;
1482                   if (changed) _prop_config_get();
1483                }
1484           }
1485 #endif
1486      }
1487    _config_sub_apply();
1488 }
1489
1490 void
1491 _elm_config_reload(void)
1492 {
1493    _config_free();
1494    _config_load();
1495    _config_apply();
1496    _elm_config_font_overlay_apply();
1497    _elm_rescale();
1498    _elm_recache();
1499 }
1500
1501 void
1502 _elm_config_engine_set(const char *engine)
1503 {
1504    if (_elm_config->engine && strcmp(_elm_config->engine, engine))
1505      eina_stringshare_del(_elm_config->engine);
1506
1507    _elm_config->engine = eina_stringshare_add(engine);
1508 }
1509
1510 void
1511 _elm_config_all_update(void)
1512 {
1513 #ifdef HAVE_ELEMENTARY_X
1514    if (_prop_all_update_timer) ecore_timer_del(_prop_all_update_timer);
1515    _prop_all_update_timer = ecore_timer_add(0.1, _prop_all_update_cb, NULL);
1516    _prop_config_set();
1517    ecore_x_window_prop_string_set(_root_1st, _atom[ATOM_E_PROFILE],
1518                                   _elm_profile);
1519 #endif
1520 }
1521
1522 void
1523 _elm_config_profile_set(const char *profile)
1524 {
1525    Eina_Bool changed = EINA_FALSE;
1526
1527    if (_elm_profile)
1528      {
1529         if (strcmp(_elm_profile, profile))
1530           changed = 1;
1531         free(_elm_profile);
1532      }
1533
1534    _elm_profile = strdup(profile);
1535
1536    if (changed)
1537      {
1538         _config_free();
1539         _config_load();
1540         _config_apply();
1541         _elm_config_font_overlay_apply();
1542         _elm_rescale();
1543         _elm_recache();
1544      }
1545 }
1546
1547 void
1548 _elm_config_shutdown(void)
1549 {
1550 #ifdef HAVE_ELEMENTARY_X
1551    if (_prop_all_update_timer)
1552      {
1553         ecore_timer_del(_prop_all_update_timer);
1554         _prop_all_update_cb(NULL);
1555      }
1556    _prop_all_update_timer = NULL;
1557    if (_prop_change_delay_timer) ecore_timer_del(_prop_change_delay_timer);
1558    _prop_change_delay_timer = NULL;
1559 #endif
1560
1561 #define ENGINE_COMPARE(name) (!strcmp(_elm_config->engine, name))
1562    if (ENGINE_COMPARE(ELM_SOFTWARE_X11) ||
1563        ENGINE_COMPARE(ELM_SOFTWARE_16_X11) ||
1564        ENGINE_COMPARE(ELM_XRENDER_X11) ||
1565        ENGINE_COMPARE(ELM_OPENGL_X11))
1566 #undef ENGINE_COMPARE
1567      {
1568 #ifdef HAVE_ELEMENTARY_X
1569         ecore_event_handler_del(_prop_change_handler);
1570         _prop_change_handler = NULL;
1571 #endif
1572      }
1573    _config_free();
1574    if (_elm_profile)
1575      {
1576         free(_elm_profile);
1577         _elm_profile = NULL;
1578      }
1579    _desc_shutdown();
1580 }
1581