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