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