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