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