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