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