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