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