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