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