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