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