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