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