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