2003574ab0e6f9a96796f0c6d6211e96985901bd
[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_poll_interval > 0)
1099      {
1100         _elm_cache_flush_poller =
1101           ecore_poller_add(ECORE_POLLER_CORE,
1102                            _elm_config->cache_flush_poll_interval,
1103                            _elm_cache_flush_cb, NULL);
1104      }
1105 }
1106
1107 static Elm_Config *
1108 _config_user_load(void)
1109 {
1110    Elm_Config *cfg = NULL;
1111    Eet_File *ef;
1112    char buf[PATH_MAX];
1113
1114    _elm_user_dir_snprintf(buf, sizeof(buf), "config/%s/base.cfg",
1115                           _elm_profile);
1116
1117    ef = eet_open(buf, EET_FILE_MODE_READ);
1118    if (ef)
1119      {
1120         cfg = eet_data_read(ef, _config_edd, "config");
1121         eet_close(ef);
1122      }
1123    return cfg;
1124 }
1125
1126 static Elm_Config *
1127 _config_system_load(void)
1128 {
1129    Elm_Config *cfg = NULL;
1130    Eet_File *ef;
1131    char buf[PATH_MAX];
1132
1133    _elm_data_dir_snprintf(buf, sizeof(buf), "config/%s/base.cfg",
1134                           _elm_profile);
1135
1136    ef = eet_open(buf, EET_FILE_MODE_READ);
1137    if (ef)
1138      {
1139         cfg = eet_data_read(ef, _config_edd, "config");
1140         eet_close(ef);
1141      }
1142    return cfg;
1143 }
1144
1145 static void
1146 _config_load(void)
1147 {
1148    _elm_config = _config_user_load();
1149    if (_elm_config)
1150      {
1151         if (_elm_config->config_version < ELM_CONFIG_VERSION)
1152           _config_update();
1153         return;
1154      }
1155
1156    /* no user config, fallback for system. No need to check version for
1157     * this one, if it's not the right one, someone screwed up at the time
1158     * of installing it */
1159    _elm_config = _config_system_load();
1160    if (_elm_config) return;
1161    /* FIXME: config load could have failed because of a non-existent
1162     * profile. Fallback to default before moving on */
1163
1164    // config load fail - defaults
1165    /* XXX: do these make sense? Only if it's valid to install the lib
1166     * without the config, but do we want that? */
1167    _elm_config = ELM_NEW(Elm_Config);
1168    _elm_config->config_version = ELM_CONFIG_VERSION;
1169    _elm_config->engine = eina_stringshare_add("software_x11");
1170    _elm_config->thumbscroll_enable = EINA_TRUE;
1171    _elm_config->thumbscroll_threshold = 24;
1172    _elm_config->thumbscroll_momentum_threshold = 100.0;
1173    _elm_config->thumbscroll_friction = 1.0;
1174    _elm_config->thumbscroll_bounce_friction = 0.5;
1175    _elm_config->thumbscroll_bounce_enable = EINA_TRUE;
1176    _elm_config->page_scroll_friction = 0.5;
1177    _elm_config->bring_in_scroll_friction = 0.5;
1178    _elm_config->zoom_friction = 0.5;
1179    _elm_config->thumbscroll_border_friction = 0.5;
1180    _elm_config->scale = 1.0;
1181    _elm_config->bgpixmap = 0;
1182    _elm_config->compositing = 1;
1183    _elm_config->font_hinting = 2;
1184    _elm_config->cache_flush_poll_interval = 512;
1185    _elm_config->cache_flush_enable = EINA_TRUE;
1186    _elm_config->font_dirs = NULL;
1187    _elm_config->image_cache = 4096;
1188    _elm_config->font_cache = 512;
1189    _elm_config->edje_cache = 32;
1190    _elm_config->edje_collection_cache = 64;
1191    _elm_config->finger_size = 40;
1192    _elm_config->fps = 60.0;
1193    _elm_config->theme = eina_stringshare_add("default");
1194    _elm_config->modules = NULL;
1195    _elm_config->tooltip_delay = 1.0;
1196    _elm_config->cursor_engine_only = EINA_TRUE;
1197    _elm_config->focus_highlight_enable = EINA_FALSE;
1198    _elm_config->focus_highlight_animate = EINA_TRUE;
1199    _elm_config->toolbar_shrink_mode = 2;
1200    _elm_config->fileselector_expand_enable = EINA_FALSE;
1201    _elm_config->inwin_dialogs_enable = EINA_FALSE;
1202    _elm_config->icon_size = 32;
1203    _elm_config->longpress_timeout = 1.0;
1204    _elm_config->password_show_last_character = EINA_FALSE;
1205 }
1206
1207 static const char *
1208 _elm_config_eet_close_error_get(Eet_File *ef,
1209                                 char     *file)
1210 {
1211    Eet_Error err;
1212    const char *erstr = NULL;
1213
1214    err = eet_close(ef);
1215    switch (err)
1216      {
1217       case EET_ERROR_WRITE_ERROR:
1218         erstr = "An error occurred while saving Elementary's "
1219                 "settings to disk. The error could not be "
1220                 "deterimined. The file where the error occurred was: "
1221                 "%s. This file has been deleted to avoid corrupt data.";
1222         break;
1223
1224       case EET_ERROR_WRITE_ERROR_FILE_TOO_BIG:
1225         erstr = "Elementary's settings files are too big "
1226                 "for the file system they are being saved to. "
1227                 "This error is very strange as the files should "
1228                 "be extremely small. Please check the settings "
1229                 "for your home directory. "
1230                 "The file where the error occurred was: %s ."
1231                 "This file has been deleted to avoid corrupt data.";
1232         break;
1233
1234       case EET_ERROR_WRITE_ERROR_IO_ERROR:
1235         erstr = "An output error occurred when writing the settings "
1236                 "files for Elementary. Your disk is having troubles "
1237                 "and possibly needs replacement. "
1238                 "The file where the error occurred was: %s ."
1239                 "This file has been deleted to avoid corrupt data.";
1240         break;
1241
1242       case EET_ERROR_WRITE_ERROR_OUT_OF_SPACE:
1243         erstr = "Elementary cannot write its settings file "
1244                 "because it ran out of space to write the file. "
1245                 "You have either run out of disk space or have "
1246                 "gone over your quota limit. "
1247                 "The file where the error occurred was: %s ."
1248                 "This file has been deleted to avoid corrupt data.";
1249         break;
1250
1251       case EET_ERROR_WRITE_ERROR_FILE_CLOSED:
1252         erstr = "Elementary unexpectedly had the settings file "
1253                 "it was writing closed on it. This is very unusual. "
1254                 "The file where the error occurred was: %s "
1255                 "This file has been deleted to avoid corrupt data.";
1256         break;
1257
1258       default:
1259         break;
1260      }
1261    if (erstr)
1262      {
1263         /* delete any partially-written file */
1264          ecore_file_unlink(file);
1265          return strdup(erstr);
1266      }
1267
1268    return NULL;
1269 }
1270
1271 static Eina_Bool
1272 _elm_config_profile_save(void)
1273 {
1274    char buf[4096], buf2[4096];
1275    int ok = 0, ret;
1276    const char *err;
1277    Eet_File *ef;
1278    size_t len;
1279
1280    len = _elm_user_dir_snprintf(buf, sizeof(buf), "config/profile.cfg");
1281    if (len + 1 >= sizeof(buf))
1282      return EINA_FALSE;
1283
1284    len = _elm_user_dir_snprintf(buf2, sizeof(buf2), "config/profile.cfg.tmp");
1285    if (len + 1 >= sizeof(buf2))
1286      return EINA_FALSE;
1287
1288    ef = eet_open(buf2, EET_FILE_MODE_WRITE);
1289    if (!ef)
1290      return EINA_FALSE;
1291
1292    ok = eet_write(ef, "config", _elm_profile, strlen(_elm_profile), 0);
1293    if (!ok)
1294      goto err;
1295
1296    err = _elm_config_eet_close_error_get(ef, buf2);
1297    if (err)
1298      {
1299         ERR("%s", err);
1300         free((void *)err);
1301         goto err;
1302      }
1303
1304    ret = ecore_file_mv(buf2, buf);
1305    if (!ret)
1306      {
1307         ERR("Error saving Elementary's configuration file");
1308         goto err;
1309      }
1310
1311    ecore_file_unlink(buf2);
1312    return EINA_TRUE;
1313
1314 err:
1315    ecore_file_unlink(buf2);
1316    return EINA_FALSE;
1317 }
1318
1319 Eina_Bool
1320 _elm_config_save(void)
1321 {
1322    char buf[4096], buf2[4096];
1323    int ok = 0, ret;
1324    const char *err;
1325    Eet_File *ef;
1326    size_t len;
1327
1328    len = _elm_user_dir_snprintf(buf, sizeof(buf), "config/%s", _elm_profile);
1329    if (len + 1 >= sizeof(buf))
1330      return EINA_FALSE;
1331
1332    ok = ecore_file_mkpath(buf);
1333    if (!ok)
1334      {
1335         ERR("Problem acessing Elementary's user configuration directory: %s",
1336             buf);
1337         return EINA_FALSE;
1338      }
1339
1340    if (!_elm_config_profile_save())
1341      return EINA_FALSE;
1342
1343    buf[len] = '/';
1344    len++;
1345
1346    if (len + sizeof("base.cfg") >= sizeof(buf) - len)
1347      return EINA_FALSE;
1348
1349    memcpy(buf + len, "base.cfg", sizeof("base.cfg"));
1350    len += sizeof("base.cfg") - 1;
1351
1352    if (len + sizeof(".tmp") >= sizeof(buf))
1353      return EINA_FALSE;
1354
1355    memcpy(buf2, buf, len);
1356    memcpy(buf2 + len, ".tmp", sizeof(".tmp"));
1357
1358    ef = eet_open(buf2, EET_FILE_MODE_WRITE);
1359    if (!ef)
1360      return EINA_FALSE;
1361
1362    ok = eet_data_write(ef, _config_edd, "config", _elm_config, 1);
1363    if (!ok)
1364      goto err;
1365
1366    err = _elm_config_eet_close_error_get(ef, buf2);
1367    if (err)
1368      {
1369         ERR("%s", err);
1370         free((void *)err);
1371         goto err;
1372      }
1373
1374    ret = ecore_file_mv(buf2, buf);
1375    if (!ret)
1376      {
1377         ERR("Error saving Elementary's configuration file");
1378         goto err;
1379      }
1380
1381    ecore_file_unlink(buf2);
1382    return EINA_TRUE;
1383
1384 err:
1385    ecore_file_unlink(buf2);
1386    return EINA_FALSE;
1387 }
1388
1389 static void
1390 _config_update(void)
1391 {
1392    Elm_Config *tcfg;
1393
1394    tcfg = _config_system_load();
1395    if (!tcfg)
1396      {
1397         /* weird profile or something? We should probably fill
1398          * with hardcoded defaults, or get from default previx */
1399           return;
1400      }
1401 #define IFCFG(v)   if ((_elm_config->config_version & 0xffff) < (v)) {
1402 #define IFCFGELSE } else {
1403 #define IFCFGEND  }
1404 #define COPYVAL(x) do {_elm_config->x = tcfg->x; } while(0)
1405 #define COPYPTR(x) do {_elm_config->x = tcfg->x; tcfg->x = NULL; } while(0)
1406 #define COPYSTR(x) COPYPTR(x)
1407
1408      /* we also need to update for property changes in the root window
1409       * if needed, but that will be dependent on new properties added
1410       * with each version */
1411
1412      IFCFG(0x0003);
1413      COPYVAL(longpress_timeout);
1414      IFCFGEND;
1415
1416 #undef COPYSTR
1417 #undef COPYPTR
1418 #undef COPYVAL
1419 #undef IFCFGEND
1420 #undef IFCFGELSE
1421 #undef IFCFG
1422
1423      /* after updating user config, we must save */
1424 }
1425
1426 static void
1427 _env_get(void)
1428 {
1429    char *s;
1430    double friction;
1431
1432    s = getenv("ELM_ENGINE");
1433    if (s)
1434      {
1435         if ((!strcasecmp(s, "x11")) ||
1436             (!strcasecmp(s, "x")) ||
1437             (!strcasecmp(s, "software-x11")) ||
1438             (!strcasecmp(s, "software_x11")))
1439           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_X11);
1440         else if ((!strcasecmp(s, "opengl")) ||
1441                  (!strcasecmp(s, "gl")) ||
1442                  (!strcasecmp(s, "opengl-x11")) ||
1443                  (!strcasecmp(s, "opengl_x11")))
1444           eina_stringshare_replace(&_elm_config->engine, ELM_OPENGL_X11);
1445         else if ((!strcasecmp(s, "x11-8")) ||
1446                  (!strcasecmp(s, "x18")) ||
1447                  (!strcasecmp(s, "software-8-x11")) ||
1448                  (!strcasecmp(s, "software_8_x11")))
1449           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_8_X11);
1450         else if ((!strcasecmp(s, "x11-16")) ||
1451                  (!strcasecmp(s, "x16")) ||
1452                  (!strcasecmp(s, "software-16-x11")) ||
1453                  (!strcasecmp(s, "software_16_x11")))
1454           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_16_X11);
1455         else if ((!strcasecmp(s, "xrender")) ||
1456                  (!strcasecmp(s, "xr")) ||
1457                  (!strcasecmp(s, "xrender-x11")) ||
1458                  (!strcasecmp(s, "xrender_x11")))
1459           eina_stringshare_replace(&_elm_config->engine, ELM_XRENDER_X11);
1460         else if ((!strcasecmp(s, "fb")) ||
1461                  (!strcasecmp(s, "software-fb")) ||
1462                  (!strcasecmp(s, "software_fb")))
1463           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_FB);
1464         else if ((!strcasecmp(s, "directfb")) ||
1465                  (!strcasecmp(s, "dfb")))
1466           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_DIRECTFB);
1467         else if ((!strcasecmp(s, "sdl")) ||
1468                  (!strcasecmp(s, "software-sdl")) ||
1469                  (!strcasecmp(s, "software_sdl")))
1470           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_SDL);
1471         else if ((!strcasecmp(s, "sdl-16")) ||
1472                  (!strcasecmp(s, "software-16-sdl")) ||
1473                  (!strcasecmp(s, "software_16_sdl")))
1474           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_16_SDL);
1475         else if ((!strcasecmp(s, "opengl-sdl")) ||
1476                  (!strcasecmp(s, "opengl_sdl")) ||
1477                  (!strcasecmp(s, "gl-sdl")) ||
1478                  (!strcasecmp(s, "gl_sdl")))
1479           eina_stringshare_replace(&_elm_config->engine, ELM_OPENGL_SDL);
1480         else if ((!strcasecmp(s, "gdi")) ||
1481                  (!strcasecmp(s, "software-gdi")) ||
1482                  (!strcasecmp(s, "software_gdi")))
1483           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_WIN32);
1484         else if ((!strcasecmp(s, "wince-gdi")) ||
1485                  (!strcasecmp(s, "software-16-wince-gdi")) ||
1486                  (!strcasecmp(s, "software_16_wince_gdi")))
1487           eina_stringshare_replace(&_elm_config->engine, ELM_SOFTWARE_16_WINCE);
1488      }
1489
1490    s = getenv("ELM_THUMBSCROLL_ENABLE");
1491    if (s) _elm_config->thumbscroll_enable = !!atoi(s);
1492    s = getenv("ELM_THUMBSCROLL_THRESHOLD");
1493    if (s) _elm_config->thumbscroll_threshold = atoi(s);
1494    // FIXME: floatformat locale issues here 1.0 vs 1,0 - should just be 1.0
1495    s = getenv("ELM_THUMBSCROLL_MOMENTUM_THRESHOLD");
1496    if (s) _elm_config->thumbscroll_momentum_threshold = atof(s);
1497    s = getenv("ELM_THUMBSCROLL_FRICTION");
1498    if (s) _elm_config->thumbscroll_friction = atof(s);
1499    s = getenv("ELM_THUMBSCROLL_BOUNCE_ENABLE");
1500    if (s) _elm_config->thumbscroll_bounce_enable = !!atoi(s);
1501    s = getenv("ELM_THUMBSCROLL_BOUNCE_FRICTION");
1502    if (s) _elm_config->thumbscroll_bounce_friction = atof(s);
1503    s = getenv("ELM_PAGE_SCROLL_FRICTION");
1504    if (s) _elm_config->page_scroll_friction = atof(s);
1505    s = getenv("ELM_BRING_IN_SCROLL_FRICTION");
1506    if (s) _elm_config->bring_in_scroll_friction = atof(s);
1507    s = getenv("ELM_ZOOM_FRICTION");
1508    if (s) _elm_config->zoom_friction = atof(s);
1509    s = getenv("ELM_THUMBSCROLL_BORDER_FRICTION");
1510    if (s)
1511      {
1512         friction = atof(s);
1513         if (friction < 0.0)
1514           friction = 0.0;
1515
1516         if (friction > 1.0)
1517           friction = 1.0;
1518
1519         _elm_config->thumbscroll_border_friction = friction;
1520      }
1521    s = getenv("ELM_THEME");
1522    if (s) eina_stringshare_replace(&_elm_config->theme, s);
1523
1524    s = getenv("ELM_FONT_HINTING");
1525    if (s)
1526      {
1527         if      (!strcasecmp(s, "none")) _elm_config->font_hinting = 0;
1528         else if (!strcasecmp(s, "auto"))
1529           _elm_config->font_hinting = 1;
1530         else if (!strcasecmp(s, "bytecode"))
1531           _elm_config->font_hinting = 2;
1532      }
1533
1534    s = getenv("ELM_FONT_PATH");
1535    if (s)
1536      {
1537         const char *p, *pp;
1538         char *buf2;
1539
1540         EINA_LIST_FREE(_elm_config->font_dirs, p)
1541           {
1542              eina_stringshare_del(p);
1543           }
1544
1545         buf2 = alloca(strlen(s) + 1);
1546         p = s;
1547         pp = p;
1548         for (;; )
1549           {
1550              if ((*p == ':') || (*p == 0))
1551                {
1552                   int len;
1553
1554                   len = p - pp;
1555                   strncpy(buf2, pp, len);
1556                   buf2[len] = 0;
1557                   _elm_config->font_dirs =
1558                     eina_list_append(_elm_config->font_dirs,
1559                                      eina_stringshare_add(buf2));
1560                   if (*p == 0) break;
1561                   p++;
1562                   pp = p;
1563                }
1564              else
1565                {
1566                   if (*p == 0) break;
1567                   p++;
1568                }
1569           }
1570      }
1571
1572    s = getenv("ELM_IMAGE_CACHE");
1573    if (s) _elm_config->image_cache = atoi(s);
1574
1575    s = getenv("ELM_FONT_CACHE");
1576    if (s) _elm_config->font_cache = atoi(s);
1577
1578    s = getenv("ELM_SCALE");
1579    if (s) _elm_config->scale = atof(s);
1580
1581    _elm_config->finger_size =
1582      (double)_elm_config->finger_size * _elm_config->scale;
1583    s = getenv("ELM_FINGER_SIZE");
1584    if (s) _elm_config->finger_size = atoi(s);
1585
1586    s = getenv("ELM_PASSWORD_SHOW_LAST_CHARACTER");
1587    if (s) _elm_config->password_show_last_character = !!atoi(s);
1588
1589    s = getenv("ELM_FPS");
1590    if (s) _elm_config->fps = atof(s);
1591    if (_elm_config->fps < 1.0) _elm_config->fps = 1.0;
1592
1593    s = getenv("ELM_MODULES");
1594    if (s) eina_stringshare_replace(&_elm_config->modules, s);
1595
1596    s = getenv("ELM_TOOLTIP_DELAY");
1597    if (s)
1598      {
1599         double delay = atof(s);
1600         if (delay >= 0.0)
1601           _elm_config->tooltip_delay = delay;
1602      }
1603
1604    s = getenv("ELM_CURSOR_ENGINE_ONLY");
1605    if (s) _elm_config->cursor_engine_only = !!atoi(s);
1606
1607    s = getenv("ELM_FOCUS_HIGHLIGHT_ENABLE");
1608    if (s) _elm_config->focus_highlight_enable = !!atoi(s);
1609
1610    s = getenv("ELM_FOCUS_HIGHLIGHT_ANIMATE");
1611    if (s) _elm_config->focus_highlight_animate = !!atoi(s);
1612
1613    s = getenv("ELM_TOOLBAR_SHRINK_MODE");
1614    if (s) _elm_config->toolbar_shrink_mode = atoi(s);
1615
1616    s = getenv("ELM_FILESELECTOR_EXPAND_ENABLE");
1617    if (s) _elm_config->fileselector_expand_enable = !!atoi(s);
1618
1619    s = getenv("ELM_INWIN_DIALOGS_ENABLE");
1620    if (s) _elm_config->inwin_dialogs_enable = !!atoi(s);
1621
1622    s = getenv("ELM_ICON_SIZE");
1623    if (s) _elm_config->icon_size = atoi(s);
1624
1625    s = getenv("ELM_INPUT_PANEL");
1626    if (s) _elm_config->input_panel_enable = atoi(s);
1627
1628    s = getenv("ELM_LONGPRESS_TIMEOUT");
1629    if (s) _elm_config->longpress_timeout = atof(s);
1630    if (_elm_config->longpress_timeout < 0.0)
1631      _elm_config->longpress_timeout = 0.0;
1632 }
1633
1634 void
1635 _elm_config_init(void)
1636 {
1637    _desc_init();
1638    _profile_fetch_from_conf();
1639    _config_load();
1640 //   _env_get();
1641 //   _config_apply();
1642 //   _elm_config_font_overlay_apply();
1643 //   _elm_recache();
1644 }
1645
1646 void
1647 _elm_config_sub_init(void)
1648 {
1649    _env_get();
1650    _config_apply();
1651    _elm_config_font_overlay_apply();
1652    _elm_recache();
1653
1654 #define ENGINE_COMPARE(name) (!strcmp(_elm_config->engine, name))
1655    if (ENGINE_COMPARE(ELM_SOFTWARE_X11) ||
1656        ENGINE_COMPARE(ELM_SOFTWARE_16_X11) ||
1657        ENGINE_COMPARE(ELM_XRENDER_X11) ||
1658        ENGINE_COMPARE(ELM_OPENGL_X11))
1659 #undef ENGINE_COMPARE
1660      {
1661 #ifdef HAVE_ELEMENTARY_X
1662         unsigned int val = 1000;
1663
1664         if (!ecore_x_init(NULL))
1665           {
1666              ERR("Cannot connect to X11 display. check $DISPLAY variable");
1667              exit(1);
1668           }
1669         _root_1st = ecore_x_window_root_first_get();
1670
1671         if (!ecore_x_screen_is_composited(0))
1672           _elm_config->compositing = 0;
1673
1674         ecore_x_atoms_get(_atom_names, ATOM_COUNT, _atom);
1675         ecore_x_event_mask_set(_root_1st,
1676                                ECORE_X_EVENT_MASK_WINDOW_PROPERTY);
1677         _prop_change_handler = ecore_event_handler_add
1678             (ECORE_X_EVENT_WINDOW_PROPERTY, _prop_change, NULL);
1679         if (!getenv("ELM_SCALE"))
1680           {
1681              if (ecore_x_window_prop_card32_get(_root_1st,
1682                                                 _atom[ATOM_E_SCALE],
1683                                                 &val, 1) > 0)
1684                {
1685                   if (val > 0)
1686                     {
1687                        _elm_config->scale = (double)val / 1000.0;
1688      // FIXME: hack until e export finger size too
1689                        if (!getenv("ELM_FINGER_SIZE"))
1690                          {
1691                             _elm_config->finger_size = 40.0 * _elm_config->scale;
1692                          }
1693                        edje_scale_set(_elm_config->scale);
1694                     }
1695                }
1696           }
1697         if (!getenv("ELM_FINGER_SIZE"))
1698           {
1699              if (ecore_x_window_prop_card32_get(_root_1st,
1700                                                 _atom[ATOM_E_FINGER_SIZE],
1701                                                 &val, 1) > 0)
1702                {
1703                   if (val > 0)
1704                     {
1705                        _elm_config->finger_size = val;
1706                     }
1707                }
1708           }
1709         if (!getenv("ELM_THEME"))
1710           {
1711              char *s;
1712
1713              s = ecore_x_window_prop_string_get(_root_1st,
1714                                                 _atom[ATOM_E_THEME]);
1715              if (s)
1716                {
1717                   eina_stringshare_replace(&_elm_config->theme, s);
1718                   _elm_theme_parse(NULL, s);
1719                   free(s);
1720                }
1721           }
1722         if (!getenv("ELM_PROFILE"))
1723           {
1724              char *s;
1725
1726              s = ecore_x_window_prop_string_get(_root_1st,
1727                                                 _atom[ATOM_E_PROFILE]);
1728              if (s)
1729                {
1730                   int changed = 0;
1731
1732                   if (_elm_profile)
1733                     {
1734                        if (strcmp(_elm_profile, s)) changed = 1;
1735                        free(_elm_profile);
1736                     }
1737                   _elm_profile = s;
1738                   if (changed) _prop_config_get();
1739                }
1740           }
1741         if (!getenv("ELM_INPUT_PANEL"))
1742           {
1743              if (ecore_x_window_prop_card32_get(_root_1st,
1744                                                 _atom[ATOM_E_INPUT_PANEL],
1745                                                 &val, 1) > 0)
1746                {
1747                   if (val > 0)
1748                     {
1749                        _elm_config->input_panel_enable = val;
1750                        edje_input_panel_enabled_set(_elm_config->input_panel_enable);
1751                     }
1752                }
1753           }
1754         if (ecore_x_window_prop_card32_get(_root_1st,
1755                                            _atom[ATOM_E_AUTOCAPITAL_ALLOW],
1756                                            &val, 1) > 0)
1757           {
1758              if (val > 0)
1759                {
1760                   _elm_config->autocapital_allow = val;
1761                   edje_autocapitalization_allow_set(_elm_config->autocapital_allow);
1762                }
1763           }
1764         if (ecore_x_window_prop_card32_get(_root_1st,
1765                                            _atom[ATOM_E_AUTOPERIOD_ALLOW],
1766                                            &val, 1) > 0)
1767           {
1768              if (val > 0)
1769                {
1770                   _elm_config->autoperiod_allow = val;
1771                   edje_autoperiod_allow_set(_elm_config->autoperiod_allow);
1772                }
1773           }
1774 #endif
1775      }
1776    _config_sub_apply();
1777 }
1778
1779 void
1780 _elm_config_reload(void)
1781 {
1782    _config_free();
1783    _config_load();
1784    _config_apply();
1785    _elm_config_font_overlay_apply();
1786    _elm_rescale();
1787    _elm_recache();
1788 }
1789
1790 void
1791 _elm_config_engine_set(const char *engine)
1792 {
1793    if (_elm_config->engine && strcmp(_elm_config->engine, engine))
1794      eina_stringshare_del(_elm_config->engine);
1795
1796    _elm_config->engine = eina_stringshare_add(engine);
1797 }
1798
1799 void
1800 _elm_config_profile_set(const char *profile)
1801 {
1802    Eina_Bool changed = EINA_FALSE;
1803
1804    if (_elm_profile)
1805      {
1806         if (strcmp(_elm_profile, profile))
1807           changed = 1;
1808         free(_elm_profile);
1809      }
1810
1811    _elm_profile = strdup(profile);
1812
1813    if (changed)
1814      {
1815         _config_free();
1816         _config_load();
1817         _config_apply();
1818         _elm_config_font_overlay_apply();
1819         _elm_rescale();
1820         _elm_recache();
1821      }
1822 }
1823
1824 void
1825 _elm_config_shutdown(void)
1826 {
1827 #define ENGINE_COMPARE(name) (!strcmp(_elm_config->engine, name))
1828    if (ENGINE_COMPARE(ELM_SOFTWARE_X11) ||
1829        ENGINE_COMPARE(ELM_SOFTWARE_16_X11) ||
1830        ENGINE_COMPARE(ELM_XRENDER_X11) ||
1831        ENGINE_COMPARE(ELM_OPENGL_X11))
1832 #undef ENGINE_COMPARE
1833      {
1834 #ifdef HAVE_ELEMENTARY_X
1835         ecore_event_handler_del(_prop_change_handler);
1836         _prop_change_handler = NULL;
1837 #endif
1838      }
1839    _config_free();
1840    if (_elm_profile)
1841      {
1842         free(_elm_profile);
1843         _elm_profile = NULL;
1844      }
1845    _desc_shutdown();
1846 }
1847