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