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