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