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