Elementary: define the gettext macro in a place that's visible to all widgets.
[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 #undef T
603 #undef D
604 #undef T_INT
605 #undef T_DOUBLE
606 #undef T_STRING
607 #undef T_UCHAR
608 }
609
610 static void
611 _desc_shutdown(void)
612 {
613    if (_config_edd)
614      {
615         eet_data_descriptor_free(_config_edd);
616         _config_edd = NULL;
617      }
618
619    if (_config_font_overlay_edd)
620      {
621         eet_data_descriptor_free(_config_font_overlay_edd);
622         _config_font_overlay_edd = NULL;
623      }
624 }
625
626 static int
627 _sort_files_cb(const void *f1,
628                const void *f2)
629 {
630    return strcmp(f1, f2);
631 }
632
633 const char *
634 _elm_config_current_profile_get(void)
635 {
636    return _elm_profile;
637 }
638
639 static size_t
640 _elm_data_dir_snprintf(char       *dst,
641                        size_t      size,
642                        const char *fmt,
643                        ...)
644 {
645    size_t data_dir_len, off;
646    va_list ap;
647
648    data_dir_len = eina_strlcpy(dst, _elm_data_dir, size);
649
650    off = data_dir_len + 1;
651    if (off >= size)
652      goto end;
653
654    va_start(ap, fmt);
655    dst[data_dir_len] = '/';
656
657    off = off + vsnprintf(dst + off, size - off, fmt, ap);
658    va_end(ap);
659
660 end:
661    return off;
662 }
663
664 static size_t
665 _elm_user_dir_snprintf(char       *dst,
666                        size_t      size,
667                        const char *fmt,
668                        ...)
669 {
670    const char *home;
671    size_t user_dir_len, off;
672    va_list ap;
673
674    home = getenv("HOME");
675    if (!home)
676      home = "/";
677
678    user_dir_len = eina_str_join_len(dst, size, '/', home, strlen(home),
679                                     ".elementary", sizeof(".elementary") - 1);
680
681    off = user_dir_len + 1;
682    if (off >= size)
683      goto end;
684
685    va_start(ap, fmt);
686    dst[user_dir_len] = '/';
687
688    off = off + vsnprintf(dst + off, size - off, fmt, ap);
689    va_end(ap);
690
691 end:
692    return off;
693 }
694
695 const char *
696 _elm_config_profile_dir_get(const char *prof,
697                             Eina_Bool   is_user)
698 {
699    char buf[PATH_MAX];
700
701    if (!is_user)
702      goto not_user;
703
704    _elm_user_dir_snprintf(buf, sizeof(buf), "config/%s", prof);
705
706    if (ecore_file_is_dir(buf))
707      return strdup(buf);
708
709    return NULL;
710
711 not_user:
712    snprintf(buf, sizeof(buf), "%s/config/%s", _elm_data_dir, prof);
713
714    if (ecore_file_is_dir(buf))
715      return strdup(buf);
716
717    return NULL;
718 }
719
720 Eina_List *
721 _elm_config_font_overlays_list(void)
722 {
723    return _elm_config->font_overlays;
724 }
725
726 void
727 _elm_config_font_overlay_set(const char    *text_class,
728                              const char    *font,
729                              Evas_Font_Size size)
730 {
731    Elm_Font_Overlay *efd;
732    Eina_List *l;
733
734    EINA_LIST_FOREACH(_elm_config->font_overlays, l, efd)
735      {
736         if (strcmp(efd->text_class, text_class))
737           continue;
738
739         if (efd->font) eina_stringshare_del(efd->font);
740         efd->font = eina_stringshare_add(font);
741         efd->size = size;
742         _elm_config->font_overlays =
743           eina_list_promote_list(_elm_config->font_overlays, l);
744         return;
745      }
746
747    /* the text class doesn't exist */
748    efd = calloc(1, sizeof(Elm_Font_Overlay));
749    efd->text_class = eina_stringshare_add(text_class);
750    efd->font = eina_stringshare_add(font);
751    efd->size = size;
752
753    _elm_config->font_overlays = eina_list_prepend(_elm_config->font_overlays,
754                                                   efd);
755 }
756
757 void
758 _elm_config_font_overlay_remove(const char *text_class)
759 {
760    Elm_Font_Overlay *efd;
761    Eina_List *l;
762
763    EINA_LIST_FOREACH(_elm_config->font_overlays, l, efd)
764      {
765         if (strcmp(efd->text_class, text_class))
766           continue;
767
768         _elm_config->font_overlays =
769           eina_list_remove_list(_elm_config->font_overlays, l);
770         if (efd->text_class) eina_stringshare_del(efd->text_class);
771         if (efd->font) eina_stringshare_del(efd->font);
772         free(efd);
773
774         return;
775      }
776 }
777
778 void
779 _elm_config_font_overlay_apply(void)
780 {
781    Elm_Font_Overlay *efd;
782    Eina_List *l;
783    int i;
784
785    for (i = 0; _elm_text_classes[i].desc; i++)
786      edje_text_class_del(_elm_text_classes[i].name);
787
788    EINA_LIST_FOREACH(_elm_config->font_overlays, l, efd)
789      edje_text_class_set(efd->text_class, efd->font, efd->size);
790 }
791
792 Eina_List *
793 _elm_config_text_classes_get(void)
794 {
795    Eina_List *ret = NULL;
796    int i;
797
798    for (i = 0; _elm_text_classes[i].desc; i++)
799      {
800         Elm_Text_Class *tc;
801         tc = malloc(sizeof(*tc));
802
803         *tc = _elm_text_classes[i];
804
805         ret = eina_list_append(ret, tc);
806      }
807
808    return ret;
809 }
810
811 void
812 _elm_config_text_classes_free(Eina_List *l)
813 {
814    Elm_Text_Class *tc;
815
816    EINA_LIST_FREE(l, tc)
817      free(tc);
818 }
819
820 Eina_List *
821 _elm_config_profiles_list(void)
822 {
823    const Eina_File_Direct_Info *info;
824    Eina_List *flist = NULL;
825    Eina_Iterator *file_it;
826    char buf[PATH_MAX];
827    const char *dir;
828    size_t len;
829
830    len = _elm_user_dir_snprintf(buf, sizeof(buf), "config");
831
832    file_it = eina_file_direct_ls(buf);
833    if (!file_it)
834      goto sys;
835
836    buf[len] = '/';
837    len++;
838
839    len = sizeof(buf) - len;
840
841    EINA_ITERATOR_FOREACH(file_it, info)
842      {
843         if (info->name_length >= len)
844           continue;
845
846         if (info->type == EINA_FILE_DIR)
847           {
848              flist =
849                eina_list_sorted_insert(flist, _sort_files_cb,
850                                        eina_stringshare_add(info->path +
851                                                             info->name_start));
852           }
853      }
854
855    eina_iterator_free(file_it);
856
857 sys:
858    len = eina_str_join_len(buf, sizeof(buf), '/', _elm_data_dir,
859                            strlen(_elm_data_dir), "config",
860                            sizeof("config") - 1);
861
862    file_it = eina_file_direct_ls(buf);
863    if (!file_it)
864      goto list_free;
865
866    buf[len] = '/';
867    len++;
868
869    len = sizeof(buf) - len;
870    EINA_ITERATOR_FOREACH(file_it, info)
871      {
872         if (info->name_length >= len)
873           continue;
874
875         switch (info->type)
876           {
877            case EINA_FILE_DIR:
878            {
879               const Eina_List *l;
880               const char *tmp;
881
882               EINA_LIST_FOREACH(flist, l, tmp)
883                 if (!strcmp(info->path + info->name_start, tmp))
884                   break;
885
886               if (!l)
887                 flist =
888                   eina_list_sorted_insert(flist, _sort_files_cb,
889                                           eina_stringshare_add(info->path +
890                                                                info->name_start));
891            }
892            break;
893
894            default:
895              continue;
896           }
897      }
898    return flist;
899
900    eina_iterator_free(file_it);
901
902 list_free:
903    EINA_LIST_FREE(flist, dir)
904      eina_stringshare_del(dir);
905
906    return NULL;
907 }
908
909 static void
910 _profile_fetch_from_conf(void)
911 {
912    char buf[PATH_MAX], *p, *s;
913    Eet_File *ef = NULL;
914    int len = 0;
915
916    _elm_profile = strdup("default");
917
918    // if env var - use profile without question
919    s = getenv("ELM_PROFILE");
920    if (s)
921      {
922         free(_elm_profile);
923         _elm_profile = strdup(s);
924         return;
925      }
926
927    // user profile
928    _elm_user_dir_snprintf(buf, sizeof(buf), "config/profile.cfg");
929    ef = eet_open(buf, EET_FILE_MODE_READ);
930    if (ef)
931      {
932         p = eet_read(ef, "config", &len);
933         if (p)
934           {
935              free(_elm_profile);
936              _elm_profile = malloc(len + 1);
937              memcpy(_elm_profile, p, len);
938              _elm_profile[len] = 0;
939              free(p);
940           }
941         eet_close(ef);
942         if (!p) ef = NULL;
943      }
944    if (ef) return;
945
946    // system profile
947    _elm_data_dir_snprintf(buf, sizeof(buf), "config/profile.cfg");
948    ef = eet_open(buf, EET_FILE_MODE_READ);
949    if (ef)
950      {
951         p = eet_read(ef, "config", &len);
952         if (p)
953           {
954              free(_elm_profile);
955              _elm_profile = malloc(len + 1);
956              memcpy(_elm_profile, p, len);
957              _elm_profile[len] = 0;
958              free(p);
959           }
960         eet_close(ef);
961      }
962 }
963
964 static void
965 _config_free(void)
966 {
967    Elm_Font_Overlay *fo;
968    const char *fontdir;
969
970    if (!_elm_config) return;
971    EINA_LIST_FREE(_elm_config->font_dirs, fontdir)
972      {
973         eina_stringshare_del(fontdir);
974      }
975    if (_elm_config->engine) eina_stringshare_del(_elm_config->engine);
976    EINA_LIST_FREE(_elm_config->font_overlays, fo)
977      {
978         if (fo->text_class) eina_stringshare_del(fo->text_class);
979         if (fo->font) eina_stringshare_del(fo->font);
980         free(fo);
981      }
982    if (_elm_config->theme) eina_stringshare_del(_elm_config->theme);
983    if (_elm_config->modules) eina_stringshare_del(_elm_config->modules);
984    free(_elm_config);
985    _elm_config = NULL;
986 }
987
988 static void
989 _config_apply(void)
990 {
991    _elm_theme_parse(NULL, _elm_config->theme);
992    ecore_animator_frametime_set(1.0 / _elm_config->fps);
993 }
994
995 static void
996 _config_sub_apply(void)
997 {
998    edje_frametime_set(1.0 / _elm_config->fps);
999    edje_scale_set(_elm_config->scale);
1000    if (_elm_config->modules) _elm_module_parse(_elm_config->modules);
1001 }
1002
1003 static Eina_Bool
1004 _elm_cache_flush_cb(void *data __UNUSED__)
1005 {
1006    elm_all_flush();
1007    return ECORE_CALLBACK_RENEW;
1008 }
1009
1010 /* kind of abusing this call right now -- shared between all of those
1011  * properties -- but they are not meant to be called that periodically
1012  * anyway */
1013 void
1014 _elm_recache(void)
1015 {
1016    Eina_List *l;
1017    Evas_Object *win;
1018
1019    elm_all_flush();
1020
1021    EINA_LIST_FOREACH(_elm_win_list, l, win)
1022      {
1023         Evas *e = evas_object_evas_get(win);
1024         evas_image_cache_set(e, _elm_config->image_cache * 1024);
1025         evas_font_cache_set(e, _elm_config->font_cache * 1024);
1026      }
1027    edje_file_cache_set(_elm_config->edje_cache);
1028    edje_collection_cache_set(_elm_config->edje_collection_cache);
1029
1030    if (_elm_cache_flush_poller)
1031      {
1032         ecore_poller_del(_elm_cache_flush_poller);
1033         _elm_cache_flush_poller = NULL;
1034      }
1035    if (_elm_config->cache_flush_enable)
1036      {
1037         if (_elm_config->cache_flush_poll_interval > 0)
1038           {
1039              _elm_cache_flush_poller =
1040                 ecore_poller_add(ECORE_POLLER_CORE,
1041                                  _elm_config->cache_flush_poll_interval,
1042                                  _elm_cache_flush_cb, NULL);
1043           }
1044      }
1045 }
1046
1047 static Elm_Config *
1048 _config_user_load(void)
1049 {
1050    Elm_Config *cfg = NULL;
1051    Eet_File *ef;
1052    char buf[PATH_MAX];
1053
1054    _elm_user_dir_snprintf(buf, sizeof(buf), "config/%s/base.cfg",
1055                           _elm_profile);
1056
1057    ef = eet_open(buf, EET_FILE_MODE_READ);
1058    if (ef)
1059      {
1060         cfg = eet_data_read(ef, _config_edd, "config");
1061         eet_close(ef);
1062      }
1063    return cfg;
1064 }
1065
1066 static Elm_Config *
1067 _config_system_load(void)
1068 {
1069    Elm_Config *cfg = NULL;
1070    Eet_File *ef;
1071    char buf[PATH_MAX];
1072
1073    _elm_data_dir_snprintf(buf, sizeof(buf), "config/%s/base.cfg",
1074                           _elm_profile);
1075
1076    ef = eet_open(buf, EET_FILE_MODE_READ);
1077    if (ef)
1078      {
1079         cfg = eet_data_read(ef, _config_edd, "config");
1080         eet_close(ef);
1081      }
1082    return cfg;
1083 }
1084
1085 static void
1086 _config_load(void)
1087 {
1088    _elm_config = _config_user_load();
1089    if (_elm_config)
1090      {
1091         if (_elm_config->config_version < ELM_CONFIG_VERSION)
1092           _config_update();
1093         return;
1094      }
1095
1096    /* no user config, fallback for system. No need to check version for
1097     * this one, if it's not the right one, someone screwed up at the time
1098     * of installing it */
1099    _elm_config = _config_system_load();
1100    if (_elm_config) return;
1101    /* FIXME: config load could have failed because of a non-existent
1102     * profile. Fallback to default before moving on */
1103
1104    // config load fail - defaults
1105    /* XXX: do these make sense? Only if it's valid to install the lib
1106     * without the config, but do we want that? */
1107    _elm_config = ELM_NEW(Elm_Config);
1108    _elm_config->config_version = ELM_CONFIG_VERSION;
1109    _elm_config->engine = eina_stringshare_add("software_x11");
1110    _elm_config->thumbscroll_enable = EINA_TRUE;
1111    _elm_config->thumbscroll_threshold = 24;
1112    _elm_config->thumbscroll_momentum_threshold = 100.0;
1113    _elm_config->thumbscroll_friction = 1.0;
1114    _elm_config->thumbscroll_bounce_friction = 0.5;
1115    _elm_config->thumbscroll_bounce_enable = EINA_TRUE;
1116    _elm_config->page_scroll_friction = 0.5;
1117    _elm_config->bring_in_scroll_friction = 0.5;
1118    _elm_config->zoom_friction = 0.5;
1119    _elm_config->thumbscroll_border_friction = 0.5;
1120    _elm_config->scale = 1.0;
1121    _elm_config->bgpixmap = 0;
1122    _elm_config->compositing = 1;
1123    _elm_config->font_hinting = 2;
1124    _elm_config->cache_flush_poll_interval = 512;
1125    _elm_config->cache_flush_enable = EINA_TRUE;
1126    _elm_config->font_dirs = NULL;
1127    _elm_config->image_cache = 4096;
1128    _elm_config->font_cache = 512;
1129    _elm_config->edje_cache = 32;
1130    _elm_config->edje_collection_cache = 64;
1131    _elm_config->finger_size = 40;
1132    _elm_config->fps = 60.0;
1133    _elm_config->theme = eina_stringshare_add("default");
1134    _elm_config->modules = NULL;
1135    _elm_config->is_mirrored = EINA_FALSE; /* Read sys value in env_get() */
1136    _elm_config->tooltip_delay = 1.0;
1137    _elm_config->cursor_engine_only = EINA_TRUE;
1138    _elm_config->focus_highlight_enable = EINA_FALSE;
1139    _elm_config->focus_highlight_animate = EINA_TRUE;
1140    _elm_config->toolbar_shrink_mode = 2;
1141    _elm_config->fileselector_expand_enable = EINA_FALSE;
1142    _elm_config->inwin_dialogs_enable = EINA_FALSE;
1143    _elm_config->icon_size = 32;
1144    _elm_config->longpress_timeout = 1.0;
1145    _elm_config->effect_enable = EINA_TRUE;
1146 }
1147
1148 static const char *
1149 _elm_config_eet_close_error_get(Eet_File *ef,
1150                                 char     *file)
1151 {
1152    Eet_Error err;
1153    const char *erstr = NULL;
1154
1155    err = eet_close(ef);
1156    switch (err)
1157      {
1158       case EET_ERROR_WRITE_ERROR:
1159         erstr = "An error occurred while saving Elementary's "
1160                 "settings to disk. The error could not be "
1161                 "deterimined. The file where the error occurred was: "
1162                 "%s. This file has been deleted to avoid corrupt data.";
1163         break;
1164
1165       case EET_ERROR_WRITE_ERROR_FILE_TOO_BIG:
1166         erstr = "Elementary's settings files are too big "
1167                 "for the file system they are being saved to. "
1168                 "This error is very strange as the files should "
1169                 "be extremely small. Please check the settings "
1170                 "for your home directory. "
1171                 "The file where the error occurred was: %s ."
1172                 "This file has been deleted to avoid corrupt data.";
1173         break;
1174
1175       case EET_ERROR_WRITE_ERROR_IO_ERROR:
1176         erstr = "An output error occurred when writing the settings "
1177                 "files for Elementary. Your disk is having troubles "
1178                 "and possibly needs replacement. "
1179                 "The file where the error occurred was: %s ."
1180                 "This file has been deleted to avoid corrupt data.";
1181         break;
1182
1183       case EET_ERROR_WRITE_ERROR_OUT_OF_SPACE:
1184         erstr = "Elementary cannot write its settings file "
1185                 "because it ran out of space to write the file. "
1186                 "You have either run out of disk space or have "
1187                 "gone over your quota limit. "
1188                 "The file where the error occurred was: %s ."
1189                 "This file has been deleted to avoid corrupt data.";
1190         break;
1191
1192       case EET_ERROR_WRITE_ERROR_FILE_CLOSED:
1193         erstr = "Elementary unexpectedly had the settings file "
1194                 "it was writing closed on it. This is very unusual. "
1195                 "The file where the error occurred was: %s "
1196                 "This file has been deleted to avoid corrupt data.";
1197         break;
1198
1199       default:
1200         break;
1201      }
1202    if (erstr)
1203      {
1204         /* delete any partially-written file */
1205          ecore_file_unlink(file);
1206          return strdup(erstr);
1207      }
1208
1209    return NULL;
1210 }
1211
1212 static Eina_Bool
1213 _elm_config_profile_save(void)
1214 {
1215    char buf[4096], buf2[4096];
1216    int ok = 0, ret;
1217    const char *err;
1218    Eet_File *ef;
1219    size_t len;
1220
1221    len = _elm_user_dir_snprintf(buf, sizeof(buf), "config/profile.cfg");
1222    if (len + 1 >= sizeof(buf))
1223      return EINA_FALSE;
1224
1225    len = _elm_user_dir_snprintf(buf2, sizeof(buf2), "config/profile.cfg.tmp");
1226    if (len + 1 >= sizeof(buf2))
1227      return EINA_FALSE;
1228
1229    ef = eet_open(buf2, EET_FILE_MODE_WRITE);
1230    if (!ef)
1231      return EINA_FALSE;
1232
1233    ok = eet_write(ef, "config", _elm_profile, strlen(_elm_profile), 0);
1234    if (!ok)
1235      goto err;
1236
1237    err = _elm_config_eet_close_error_get(ef, buf2);
1238    if (err)
1239      {
1240         ERR("%s", err);
1241         free((void *)err);
1242         goto err;
1243      }
1244
1245    ret = ecore_file_mv(buf2, buf);
1246    if (!ret)
1247      {
1248         ERR("Error saving Elementary's configuration file");
1249         goto err;
1250      }
1251
1252    ecore_file_unlink(buf2);
1253    return EINA_TRUE;
1254
1255 err:
1256    ecore_file_unlink(buf2);
1257    return EINA_FALSE;
1258 }
1259
1260 Eina_Bool
1261 _elm_config_save(void)
1262 {
1263    char buf[4096], buf2[4096];
1264    int ok = 0, ret;
1265    const char *err;
1266    Eet_File *ef;
1267    size_t len;
1268
1269    len = _elm_user_dir_snprintf(buf, sizeof(buf), "config/%s", _elm_profile);
1270    if (len + 1 >= sizeof(buf))
1271      return EINA_FALSE;
1272
1273    ok = ecore_file_mkpath(buf);
1274    if (!ok)
1275      {
1276         ERR("Problem acessing Elementary's user configuration directory: %s",
1277             buf);
1278         return EINA_FALSE;
1279      }
1280
1281    if (!_elm_config_profile_save())
1282      return EINA_FALSE;
1283
1284    buf[len] = '/';
1285    len++;
1286
1287    if (len + sizeof("base.cfg") >= sizeof(buf) - len)
1288      return EINA_FALSE;
1289
1290    memcpy(buf + len, "base.cfg", sizeof("base.cfg"));
1291    len += sizeof("base.cfg") - 1;
1292
1293    if (len + sizeof(".tmp") >= sizeof(buf))
1294      return EINA_FALSE;
1295
1296    memcpy(buf2, buf, len);
1297    memcpy(buf2 + len, ".tmp", sizeof(".tmp"));
1298
1299    ef = eet_open(buf2, EET_FILE_MODE_WRITE);
1300    if (!ef)
1301      return EINA_FALSE;
1302
1303    ok = eet_data_write(ef, _config_edd, "config", _elm_config, 1);
1304    if (!ok)
1305      goto err;
1306
1307    err = _elm_config_eet_close_error_get(ef, buf2);
1308    if (err)
1309      {
1310         ERR("%s", err);
1311         free((void *)err);
1312         goto err;
1313      }
1314
1315    ret = ecore_file_mv(buf2, buf);
1316    if (!ret)
1317      {
1318         ERR("Error saving Elementary's configuration file");
1319         goto err;
1320      }
1321
1322    ecore_file_unlink(buf2);
1323    return EINA_TRUE;
1324
1325 err:
1326    ecore_file_unlink(buf2);
1327    return EINA_FALSE;
1328 }
1329
1330 static void
1331 _config_update(void)
1332 {
1333    Elm_Config *tcfg;
1334
1335    tcfg = _config_system_load();
1336    if (!tcfg)
1337      {
1338         /* weird profile or something? We should probably fill
1339          * with hardcoded defaults, or get from default previx */
1340           return;
1341      }
1342 #define IFCFG(v)   if ((_elm_config->config_version & 0xffff) < (v)) {
1343 #define IFCFGELSE } else {
1344 #define IFCFGEND  }
1345 #define COPYVAL(x) do {_elm_config->x = tcfg->x; } while(0)
1346 #define COPYPTR(x) do {_elm_config->x = tcfg->x; tcfg->x = NULL; } while(0)
1347 #define COPYSTR(x) COPYPTR(x)
1348
1349      /* we also need to update for property changes in the root window
1350       * if needed, but that will be dependent on new properties added
1351       * with each version */
1352
1353      IFCFG(0x0003);
1354      COPYVAL(longpress_timeout);
1355      IFCFGEND;
1356
1357 #undef COPYSTR
1358 #undef COPYPTR
1359 #undef COPYVAL
1360 #undef IFCFGEND
1361 #undef IFCFGELSE
1362 #undef IFCFG
1363
1364      /* after updating user config, we must save */
1365 }
1366
1367 static void
1368 _env_get(void)
1369 {
1370    char *s;
1371    double friction;
1372
1373    s = getenv("ELM_ENGINE");
1374    if (s)
1375      {
1376         if ((!strcasecmp(s, "x11")) ||
1377             (!strcasecmp(s, "x")) ||
1378             (!strcasecmp(s, "software-x11")) ||
1379             (!strcasecmp(s, "software_x11")))
1380           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_X11);
1381         else if ((!strcasecmp(s, "opengl")) ||
1382                  (!strcasecmp(s, "gl")) ||
1383                  (!strcasecmp(s, "opengl-x11")) ||
1384                  (!strcasecmp(s, "opengl_x11")))
1385           eina_stringshare_replace(&_elm_config->engine, ELM_OPENGL_X11);
1386         else if ((!strcasecmp(s, "x11-8")) ||
1387                  (!strcasecmp(s, "x18")) ||
1388                  (!strcasecmp(s, "software-8-x11")) ||
1389                  (!strcasecmp(s, "software_8_x11")))
1390           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_8_X11);
1391         else if ((!strcasecmp(s, "x11-16")) ||
1392                  (!strcasecmp(s, "x16")) ||
1393                  (!strcasecmp(s, "software-16-x11")) ||
1394                  (!strcasecmp(s, "software_16_x11")))
1395           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_16_X11);
1396         else if ((!strcasecmp(s, "xrender")) ||
1397                  (!strcasecmp(s, "xr")) ||
1398                  (!strcasecmp(s, "xrender-x11")) ||
1399                  (!strcasecmp(s, "xrender_x11")))
1400           eina_stringshare_replace(&_elm_config->engine, ELM_XRENDER_X11);
1401         else if ((!strcasecmp(s, "fb")) ||
1402                  (!strcasecmp(s, "software-fb")) ||
1403                  (!strcasecmp(s, "software_fb")))
1404           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_FB);
1405         else if ((!strcasecmp(s, "directfb")) ||
1406                  (!strcasecmp(s, "dfb")))
1407           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_DIRECTFB);
1408         else if ((!strcasecmp(s, "sdl")) ||
1409                  (!strcasecmp(s, "software-sdl")) ||
1410                  (!strcasecmp(s, "software_sdl")))
1411           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_SDL);
1412         else if ((!strcasecmp(s, "sdl-16")) ||
1413                  (!strcasecmp(s, "software-16-sdl")) ||
1414                  (!strcasecmp(s, "software_16_sdl")))
1415           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_16_SDL);
1416         else if ((!strcasecmp(s, "opengl-sdl")) ||
1417                  (!strcasecmp(s, "opengl_sdl")) ||
1418                  (!strcasecmp(s, "gl-sdl")) ||
1419                  (!strcasecmp(s, "gl_sdl")))
1420           eina_stringshare_replace(&_elm_config->engine, ELM_OPENGL_SDL);
1421         else if ((!strcasecmp(s, "gdi")) ||
1422                  (!strcasecmp(s, "software-gdi")) ||
1423                  (!strcasecmp(s, "software_gdi")))
1424           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_WIN32);
1425         else if ((!strcasecmp(s, "wince-gdi")) ||
1426                  (!strcasecmp(s, "software-16-wince-gdi")) ||
1427                  (!strcasecmp(s, "software_16_wince_gdi")))
1428           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_16_WINCE);
1429      }
1430
1431    s = getenv("ELM_THUMBSCROLL_ENABLE");
1432    if (s) _elm_config->thumbscroll_enable = !!atoi(s);
1433    s = getenv("ELM_THUMBSCROLL_THRESHOLD");
1434    if (s) _elm_config->thumbscroll_threshold = atoi(s);
1435    // FIXME: floatformat locale issues here 1.0 vs 1,0 - should just be 1.0
1436    s = getenv("ELM_THUMBSCROLL_MOMENTUM_THRESHOLD");
1437    if (s) _elm_config->thumbscroll_momentum_threshold = atof(s);
1438    s = getenv("ELM_THUMBSCROLL_FRICTION");
1439    if (s) _elm_config->thumbscroll_friction = atof(s);
1440    s = getenv("ELM_THUMBSCROLL_BOUNCE_ENABLE");
1441    if (s) _elm_config->thumbscroll_bounce_enable = !!atoi(s);
1442    s = getenv("ELM_THUMBSCROLL_BOUNCE_FRICTION");
1443    if (s) _elm_config->thumbscroll_bounce_friction = atof(s);
1444    s = getenv("ELM_PAGE_SCROLL_FRICTION");
1445    if (s) _elm_config->page_scroll_friction = atof(s);
1446    s = getenv("ELM_BRING_IN_SCROLL_FRICTION");
1447    if (s) _elm_config->bring_in_scroll_friction = atof(s);
1448    s = getenv("ELM_ZOOM_FRICTION");
1449    if (s) _elm_config->zoom_friction = atof(s);
1450    s = getenv("ELM_THUMBSCROLL_BORDER_FRICTION");
1451    if (s)
1452      {
1453         friction = atof(s);
1454         if (friction < 0.0)
1455           friction = 0.0;
1456
1457         if (friction > 1.0)
1458           friction = 1.0;
1459
1460         _elm_config->thumbscroll_border_friction = friction;
1461      }
1462    s = getenv("ELM_THEME");
1463    if (s) eina_stringshare_replace(&_elm_config->theme, s);
1464
1465    s = getenv("ELM_FONT_HINTING");
1466    if (s)
1467      {
1468         if      (!strcasecmp(s, "none")) _elm_config->font_hinting = 0;
1469         else if (!strcasecmp(s, "auto"))
1470           _elm_config->font_hinting = 1;
1471         else if (!strcasecmp(s, "bytecode"))
1472           _elm_config->font_hinting = 2;
1473      }
1474
1475    s = getenv("ELM_FONT_PATH");
1476    if (s)
1477      {
1478         const char *p, *pp;
1479         char *buf2;
1480
1481         EINA_LIST_FREE(_elm_config->font_dirs, p)
1482           {
1483              eina_stringshare_del(p);
1484           }
1485
1486         buf2 = alloca(strlen(s) + 1);
1487         p = s;
1488         pp = p;
1489         for (;; )
1490           {
1491              if ((*p == ':') || (*p == 0))
1492                {
1493                   int len;
1494
1495                   len = p - pp;
1496                   strncpy(buf2, pp, len);
1497                   buf2[len] = 0;
1498                   _elm_config->font_dirs =
1499                     eina_list_append(_elm_config->font_dirs,
1500                                      eina_stringshare_add(buf2));
1501                   if (*p == 0) break;
1502                   p++;
1503                   pp = p;
1504                }
1505              else
1506                {
1507                   if (*p == 0) break;
1508                   p++;
1509                }
1510           }
1511      }
1512
1513    s = getenv("ELM_IMAGE_CACHE");
1514    if (s) _elm_config->image_cache = atoi(s);
1515
1516    s = getenv("ELM_FONT_CACHE");
1517    if (s) _elm_config->font_cache = atoi(s);
1518
1519    s = getenv("ELM_SCALE");
1520    if (s) _elm_config->scale = atof(s);
1521
1522    _elm_config->finger_size =
1523      (double)_elm_config->finger_size * _elm_config->scale;
1524    s = getenv("ELM_FINGER_SIZE");
1525    if (s) _elm_config->finger_size = atoi(s);
1526
1527    s = getenv("ELM_FPS");
1528    if (s) _elm_config->fps = atof(s);
1529    if (_elm_config->fps < 1.0) _elm_config->fps = 1.0;
1530
1531    s = getenv("ELM_MODULES");
1532    if (s) eina_stringshare_replace(&_elm_config->modules, s);
1533
1534    /* Get RTL orientation from system */
1535    setlocale(LC_ALL, "");
1536    bindtextdomain("elementary", LOCALE_DIR);
1537    textdomain("elementary");
1538    _elm_config->is_mirrored = !strcmp(_("default:LTR"), "default:RTL");
1539
1540    s = getenv("ELM_TOOLTIP_DELAY");
1541    if (s)
1542      {
1543         double delay = atof(s);
1544         if (delay >= 0.0)
1545           _elm_config->tooltip_delay = delay;
1546      }
1547
1548    s = getenv("ELM_CURSOR_ENGINE_ONLY");
1549    if (s) _elm_config->cursor_engine_only = !!atoi(s);
1550
1551    s = getenv("ELM_FOCUS_HIGHLIGHT_ENABLE");
1552    if (s) _elm_config->focus_highlight_enable = !!atoi(s);
1553
1554    s = getenv("ELM_FOCUS_HIGHLIGHT_ANIMATE");
1555    if (s) _elm_config->focus_highlight_animate = !!atoi(s);
1556
1557    s = getenv("ELM_TOOLBAR_SHRINK_MODE");
1558    if (s) _elm_config->toolbar_shrink_mode = atoi(s);
1559
1560    s = getenv("ELM_FILESELECTOR_EXPAND_ENABLE");
1561    if (s) _elm_config->fileselector_expand_enable = !!atoi(s);
1562
1563    s = getenv("ELM_INWIN_DIALOGS_ENABLE");
1564    if (s) _elm_config->inwin_dialogs_enable = !!atoi(s);
1565
1566    s = getenv("ELM_ICON_SIZE");
1567    if (s) _elm_config->icon_size = atoi(s);
1568
1569    s = getenv("ELM_LONGPRESS_TIMEOUT");
1570    if (s) _elm_config->longpress_timeout = atof(s);
1571    if (_elm_config->longpress_timeout < 0.0)
1572      _elm_config->longpress_timeout = 0.0;
1573    
1574    s = getenv("ELM_EFFECT_ENABLE");
1575    if (s) _elm_config->effect_enable = !!atoi(s);
1576 }
1577
1578 /**
1579  * Get the system mirrored mode. This determines the default mirrored mode
1580  * of widgets.
1581  *
1582  * @return EINA_TRUE if mirrored is set, EINA_FALSE otherwise
1583  */
1584 EAPI Eina_Bool
1585 elm_mirrored_get(void)
1586 {
1587    return _elm_config->is_mirrored;
1588 }
1589
1590 /**
1591  * Set the system mirrored mode. This determines the default mirrored mode
1592  * of widgets.
1593  *
1594  * @param mirrored EINA_TRUE to set mirrored mode, EINA_FALSE to unset it.
1595  */
1596 EAPI void
1597 elm_mirrored_set(Eina_Bool mirrored)
1598 {
1599    _elm_config->is_mirrored = mirrored;
1600    _elm_rescale();
1601 }
1602
1603 void
1604 _elm_config_init(void)
1605 {
1606    _desc_init();
1607    _profile_fetch_from_conf();
1608    _config_load();
1609    _env_get();
1610    _config_apply();
1611    _elm_config_font_overlay_apply();
1612    _elm_recache();
1613 }
1614
1615 void
1616 _elm_config_sub_init(void)
1617 {
1618 #define ENGINE_COMPARE(name) (!strcmp(_elm_config->engine, name))
1619    if (ENGINE_COMPARE(ELM_SOFTWARE_X11) ||
1620        ENGINE_COMPARE(ELM_SOFTWARE_16_X11) ||
1621        ENGINE_COMPARE(ELM_XRENDER_X11) ||
1622        ENGINE_COMPARE(ELM_OPENGL_X11))
1623 #undef ENGINE_COMPARE
1624      {
1625 #ifdef HAVE_ELEMENTARY_X
1626         unsigned int val = 1000;
1627
1628         if (!ecore_x_init(NULL))
1629           {
1630              ERR("Cannot connect to X11 display. check $DISPLAY variable");
1631              exit(1);
1632           }
1633         _root_1st = ecore_x_window_root_first_get();
1634
1635         if (!ecore_x_screen_is_composited(0))
1636           _elm_config->compositing = 0;
1637
1638         ecore_x_atoms_get(_atom_names, ATOM_COUNT, _atom);
1639         ecore_x_event_mask_set(_root_1st,
1640                                ECORE_X_EVENT_MASK_WINDOW_PROPERTY);
1641         _prop_change_handler = ecore_event_handler_add
1642             (ECORE_X_EVENT_WINDOW_PROPERTY, _prop_change, NULL);
1643         if (!getenv("ELM_SCALE"))
1644           {
1645              if (ecore_x_window_prop_card32_get(_root_1st,
1646                                                 _atom[ATOM_E_SCALE],
1647                                                 &val, 1) > 0)
1648                {
1649                   if (val > 0)
1650                     {
1651                        _elm_config->scale = (double)val / 1000.0;
1652      // FIXME: hack until e export finger size too
1653                        if (!getenv("ELM_FINGER_SIZE"))
1654                          {
1655                             _elm_config->finger_size = 40.0 * _elm_config->scale;
1656                          }
1657                        edje_scale_set(_elm_config->scale);
1658                     }
1659                }
1660           }
1661         if (!getenv("ELM_FINGER_SIZE"))
1662           {
1663              if (ecore_x_window_prop_card32_get(_root_1st,
1664                                                 _atom[ATOM_E_FINGER_SIZE],
1665                                                 &val, 1) > 0)
1666                {
1667                   if (val > 0)
1668                     {
1669                        _elm_config->finger_size = val;
1670                     }
1671                }
1672           }
1673         if (!getenv("ELM_THEME"))
1674           {
1675              char *s;
1676
1677              s = ecore_x_window_prop_string_get(_root_1st,
1678                                                 _atom[ATOM_E_THEME]);
1679              if (s)
1680                {
1681                   eina_stringshare_replace(&_elm_config->theme, s);
1682                   _elm_theme_parse(NULL, s);
1683                   free(s);
1684                }
1685           }
1686         if (!getenv("ELM_PROFILE"))
1687           {
1688              char *s;
1689
1690              s = ecore_x_window_prop_string_get(_root_1st,
1691                                                 _atom[ATOM_E_PROFILE]);
1692              if (s)
1693                {
1694                   int changed = 0;
1695
1696                   if (_elm_profile)
1697                     {
1698                        if (strcmp(_elm_profile, s)) changed = 1;
1699                        free(_elm_profile);
1700                     }
1701                   _elm_profile = s;
1702                   if (changed) _prop_config_get();
1703                }
1704           }
1705 #endif
1706      }
1707    _config_sub_apply();
1708 }
1709
1710 void
1711 _elm_config_reload(void)
1712 {
1713    _config_free();
1714    _config_load();
1715    _config_apply();
1716    _elm_config_font_overlay_apply();
1717    _elm_rescale();
1718    _elm_recache();
1719 }
1720
1721 void
1722 _elm_config_engine_set(const char *engine)
1723 {
1724    if (_elm_config->engine && strcmp(_elm_config->engine, engine))
1725      eina_stringshare_del(_elm_config->engine);
1726
1727    _elm_config->engine = eina_stringshare_add(engine);
1728 }
1729
1730 void
1731 _elm_config_profile_set(const char *profile)
1732 {
1733    Eina_Bool changed = EINA_FALSE;
1734
1735    if (_elm_profile)
1736      {
1737         if (strcmp(_elm_profile, profile))
1738           changed = 1;
1739         free(_elm_profile);
1740      }
1741
1742    _elm_profile = strdup(profile);
1743
1744    if (changed)
1745      {
1746         _config_free();
1747         _config_load();
1748         _config_apply();
1749         _elm_config_font_overlay_apply();
1750         _elm_rescale();
1751         _elm_recache();
1752      }
1753 }
1754
1755 void
1756 _elm_config_shutdown(void)
1757 {
1758 #define ENGINE_COMPARE(name) (!strcmp(_elm_config->engine, name))
1759    if (ENGINE_COMPARE(ELM_SOFTWARE_X11) ||
1760        ENGINE_COMPARE(ELM_SOFTWARE_16_X11) ||
1761        ENGINE_COMPARE(ELM_XRENDER_X11) ||
1762        ENGINE_COMPARE(ELM_OPENGL_X11))
1763 #undef ENGINE_COMPARE
1764      {
1765 #ifdef HAVE_ELEMENTARY_X
1766         ecore_event_handler_del(_prop_change_handler);
1767         _prop_change_handler = NULL;
1768 #endif
1769      }
1770    _config_free();
1771    if (_elm_profile)
1772      {
1773         free(_elm_profile);
1774         _elm_profile = NULL;
1775      }
1776    _desc_shutdown();
1777 }
1778