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