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