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