Added initial test case framework
[platform/upstream/enlightenment.git] / src / bin / e_main.c
1 #include "e.h"
2 #ifdef __linux__
3 # include <sys/prctl.h>
4 #endif
5
6 #define MAX_LEVEL 80
7
8 #define TS_DO
9 #ifdef TS_DO
10 # define TS(x)                                                    \
11   {                                                               \
12      t1 = ecore_time_unix_get();                                  \
13      printf("ESTART: %1.5f [%1.5f] - %s\n", t1 - t0, t1 - t2, x); \
14      t2 = t1;                                                     \
15   }
16 static double t0, t1, t2;
17 #else
18 # define TS(x)
19 #endif
20
21 /*
22  * i need to make more use of these when i'm baffled as to when something is
23  * up. other hooks:
24  *
25  *      void *(*__malloc_hook)(size_t size, const void *caller);
26  *
27  *      void *(*__realloc_hook)(void *ptr, size_t size, const void *caller);
28  *
29  *      void *(*__memalign_hook)(size_t alignment, size_t size,
30  *                               const void *caller);
31  *
32  *      void (*__free_hook)(void *ptr, const void *caller);
33  *
34  *      void (*__malloc_initialize_hook)(void);
35  *
36  *      void (*__after_morecore_hook)(void);
37  *
38
39    static void my_init_hook(void);
40    static void my_free_hook(void *p, const void *caller);
41
42    static void (*old_free_hook)(void *ptr, const void *caller) = NULL;
43    void (*__free_hook)(void *ptr, const void *caller);
44
45    void (*__malloc_initialize_hook) (void) = my_init_hook;
46    static void
47    my_init_hook(void)
48    {
49    old_free_hook = __free_hook;
50    __free_hook = my_free_hook;
51    }
52
53    //void *magicfree = NULL;
54
55    static void
56    my_free_hook(void *p, const void *caller)
57    {
58    __free_hook = old_free_hook;
59    //   if ((p) && (p == magicfree))
60    //     {
61    //   printf("CAUGHT!!!!! %p ...\n", p);
62    //   abort();
63    //     }
64    free(p);
65    __free_hook = my_free_hook;
66    }
67  */
68
69 /* local function prototypes */
70 static void      _e_main_shutdown(int errcode);
71 static void      _e_main_shutdown_push(int (*func)(void));
72 static void      _e_main_parse_arguments(int argc, char **argv);
73 static Eina_Bool _e_main_cb_signal_exit(void *data __UNUSED__, int ev_type __UNUSED__, void *ev __UNUSED__);
74 static Eina_Bool _e_main_cb_signal_hup(void *data __UNUSED__, int ev_type __UNUSED__, void *ev __UNUSED__);
75 static Eina_Bool _e_main_cb_signal_user(void *data __UNUSED__, int ev_type __UNUSED__, void *ev);
76 static int       _e_main_dirs_init(void);
77 static int       _e_main_dirs_shutdown(void);
78 static int       _e_main_path_init(void);
79 static int       _e_main_path_shutdown(void);
80 static void      _e_main_test_formats(void);
81 static int       _e_main_screens_init(void);
82 static int       _e_main_screens_shutdown(void);
83 static void      _e_main_desk_save(void);
84 static void      _e_main_desk_restore(void);
85 static void      _e_main_efreet_paths_init(void);
86 static void      _e_main_modules_load(Eina_Bool safe_mode);
87 static Eina_Bool _e_main_cb_x_flusher(void *data __UNUSED__);
88 static Eina_Bool _e_main_cb_idle_before(void *data __UNUSED__);
89 static Eina_Bool _e_main_cb_idle_after(void *data __UNUSED__);
90 static Eina_Bool _e_main_cb_startup_fake_end(void *data __UNUSED__);
91
92 /* local variables */
93 static Eina_Bool really_know = EINA_FALSE;
94 static Eina_Bool locked = EINA_FALSE;
95 static Eina_Bool inloop = EINA_FALSE;
96 static jmp_buf x_fatal_buff;
97
98 static int _e_main_lvl = 0;
99 static int(*_e_main_shutdown_func[MAX_LEVEL]) (void);
100
101 static Ecore_Idle_Enterer *_idle_before = NULL;
102 static Ecore_Idle_Enterer *_idle_after = NULL;
103 static Ecore_Idle_Enterer *_idle_flush = NULL;
104
105 static Ecore_Event_Handler *mod_init_end = NULL;
106
107 /* external variables */
108 EAPI Eina_Bool e_precache_end = EINA_FALSE;
109 EAPI Eina_Bool x_fatal = EINA_FALSE;
110 EAPI Eina_Bool good = EINA_FALSE;
111 EAPI Eina_Bool evil = EINA_FALSE;
112 EAPI Eina_Bool starting = EINA_TRUE;
113 EAPI Eina_Bool stopping = EINA_FALSE;
114 EAPI Eina_Bool restart = EINA_FALSE;
115 EAPI Eina_Bool e_nopause = EINA_FALSE;
116 EINTERN const char *e_first_frame = NULL;
117 EINTERN double e_first_frame_start_time = -1;
118
119 static Eina_Bool
120 _xdg_check_str(const char *env, const char *str)
121 {
122    const char *p;
123    size_t len;
124
125    len = strlen(str);
126    for (p = strstr(env, str); p; p++, p = strstr(p, str))
127      {
128         if ((!p[len]) || (p[len] == ':')) return EINA_TRUE;
129      }
130    return EINA_FALSE;
131 }
132
133 static void
134 _xdg_data_dirs_augment(void)
135 {
136    const char *s;
137    const char *p = e_prefix_get();
138    char newpath[4096], buf[4096];
139
140    if (!p) return;
141
142    s = getenv("XDG_DATA_DIRS");
143    if (s)
144      {
145         Eina_Bool pfxdata, pfx;
146
147         pfxdata = !_xdg_check_str(s, e_prefix_data_get());
148         snprintf(newpath, sizeof(newpath), "%s/share", p);
149         pfx = !_xdg_check_str(s, newpath);
150         if (pfxdata || pfx)
151           {
152              snprintf(buf, sizeof(buf), "%s%s%s%s%s",
153                pfxdata ? e_prefix_data_get() : "",
154                pfxdata ? ":" : "",
155                pfx ? newpath : "",
156                pfx ? ":" : "",
157                s);
158              e_util_env_set("XDG_DATA_DIRS", buf);
159           }
160      }
161    else
162      {
163         snprintf(buf, sizeof(buf), "%s:%s/share:/usr/local/share:/usr/share", e_prefix_data_get(), p);
164         e_util_env_set("XDG_DATA_DIRS", buf);
165      }
166
167    s = getenv("XDG_CONFIG_DIRS");
168    snprintf(newpath, sizeof(newpath), "%s/etc/xdg", p);
169    if (s)
170      {
171         if (!_xdg_check_str(s, newpath))
172           {
173              snprintf(buf, sizeof(buf), "%s:%s", newpath, s);
174              e_util_env_set("XDG_CONFIG_DIRS", buf);
175           }
176      }
177    else
178      {
179         snprintf(buf, sizeof(buf), "%s:/etc/xdg", newpath);
180         e_util_env_set("XDG_CONFIG_DIRS", buf);
181      }
182
183    if (!getenv("XDG_RUNTIME_DIR"))
184      {
185         const char *dir;
186
187         snprintf(buf, sizeof(buf), "/tmp/xdg-XXXXXX");
188         dir = mkdtemp(buf);
189         if (!dir) dir = "/tmp";
190         else
191           {
192              e_util_env_set("XDG_RUNTIME_DIR", dir);
193              snprintf(buf, sizeof(buf), "%s/.e-deleteme", dir);
194              ecore_file_mkdir(buf);
195           }
196      }
197
198    /* set menu prefix so we get our e menu */
199    if (!getenv("XDG_MENU_PREFIX"))
200      {
201         e_util_env_set("XDG_MENU_PREFIX", "e-");
202      }
203 }
204
205 static Eina_Bool
206 _e_main_shelf_init_job(void *data EINA_UNUSED)
207 {
208    e_shelf_config_update();
209    return ECORE_CALLBACK_CANCEL;
210 }
211
212 /* externally accessible functions */
213 int
214 main(int argc, char **argv)
215 {
216    Eina_Bool nostartup = EINA_FALSE;
217    Eina_Bool safe_mode = EINA_FALSE;
218    Eina_Bool after_restart = EINA_FALSE;
219    Eina_Bool waslocked = EINA_FALSE;
220    double t = 0.0, tstart = 0.0;
221    char *s = NULL, buff[32];
222    struct sigaction action;
223
224 #ifdef __linux__
225 # ifdef PR_SET_PTRACER
226 #  ifdef PR_SET_PTRACER_ANY
227    prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY);
228 #  endif
229 # endif
230 #endif
231    
232 #ifdef TS_DO
233    t0 = t1 = t2 = ecore_time_unix_get();
234 #endif
235    TS("Begin Startup");
236
237    /* trap deadly bug signals and allow some form of sane recovery */
238    /* or ability to gdb attach and debug at this point - better than your */
239    /* wm/desktop vanishing and not knowing what happened */
240    if (!getenv("NOTIFY_SOCKET"))
241      {
242         TS("Signal Trap");
243         action.sa_sigaction = e_sigseg_act;
244         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
245         sigemptyset(&action.sa_mask);
246         sigaction(SIGSEGV, &action, NULL);
247
248         action.sa_sigaction = e_sigill_act;
249         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
250         sigemptyset(&action.sa_mask);
251         sigaction(SIGILL, &action, NULL);
252
253         action.sa_sigaction = e_sigfpe_act;
254         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
255         sigemptyset(&action.sa_mask);
256         sigaction(SIGFPE, &action, NULL);
257
258 #ifndef HAVE_WAYLAND_ONLY
259         action.sa_sigaction = e_sigbus_act;
260         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
261         sigemptyset(&action.sa_mask);
262         sigaction(SIGBUS, &action, NULL);
263 #endif
264
265         action.sa_sigaction = e_sigabrt_act;
266         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
267         sigemptyset(&action.sa_mask);
268         sigaction(SIGABRT, &action, NULL);
269         TS("Signal Trap Done");
270      }
271
272    t = ecore_time_unix_get();
273    s = getenv("E_START_TIME");
274    if ((s) && (!getenv("E_RESTART_OK")))
275      {
276         tstart = atof(s);
277         if ((t - tstart) < 5.0) safe_mode = EINA_TRUE;
278      }
279    tstart = t;
280    snprintf(buff, sizeof(buff), "%1.1f", tstart);
281    e_util_env_set("E_START_TIME", buff);
282
283    if (getenv("E_START_MTRACK"))
284      e_util_env_set("MTRACK", NULL);
285    TS("Eina Init");
286    if (!eina_init())
287      {
288         e_error_message_show(_("Enlightenment cannot initialize Eina!\n"));
289         _e_main_shutdown(-1);
290      }
291    _e_main_shutdown_push(eina_shutdown);
292    if (!e_log_init())
293      {
294         e_error_message_show(_("Enlightenment could not create a logging domain!\n"));
295         _e_main_shutdown(-1);
296      }
297 #ifdef TS_DO
298 #undef TS
299 # define TS(x)                                                    \
300   {                                                               \
301      t1 = ecore_time_unix_get();                                  \
302      printf("ESTART: %1.5f [%1.5f] - %s\n", t1 - t0, t1 - t2, x); \
303      t2 = t1;                                                     \
304   }
305 #endif
306    TS("Eina Init Done");
307    _e_main_shutdown_push(e_log_shutdown);
308
309    TS("Determine Prefix");
310    if (!e_prefix_determine(argv[0]))
311      {
312         fprintf(stderr,
313                 "ERROR: Enlightenment cannot determine it's installed\n"
314                 "       prefix from the system or argv[0].\n"
315                 "       This is because it is not on Linux AND has been\n"
316                 "       executed strangely. This is unusual.\n");
317      }
318    TS("Determine Prefix Done");
319
320    /* for debugging by redirecting stdout of e to a log file to tail */
321    setvbuf(stdout, NULL, _IONBF, 0);
322
323    TS("Environment Variables");
324    if (getenv("E_RESTART")) after_restart = EINA_TRUE;
325    if (getenv("DESKTOP_STARTUP_ID"))
326      e_util_env_set("DESKTOP_STARTUP_ID", NULL);
327    e_util_env_set("E_RESTART_OK", NULL);
328    e_util_env_set("PANTS", "ON");
329    e_util_env_set("DESKTOP", "Enlightenment-0.17.0");
330    TS("Environment Variables Done");
331
332    TS("Parse Arguments");
333    _e_main_parse_arguments(argc, argv);
334    TS("Parse Arguments Done");
335
336    /*** Initialize Core EFL Libraries We Need ***/
337
338    TS("Eet Init");
339    if (!eet_init())
340      {
341         e_error_message_show(_("Enlightenment cannot initialize Eet!\n"));
342         _e_main_shutdown(-1);
343      }
344    TS("Eet Init Done");
345    _e_main_shutdown_push(eet_shutdown);
346
347    TS("Ecore Init");
348    if (!ecore_init())
349      {
350         e_error_message_show(_("Enlightenment cannot initialize Ecore!\n"));
351         _e_main_shutdown(-1);
352      }
353    TS("Ecore Init Done");
354    _e_main_shutdown_push(ecore_shutdown);
355
356    e_first_frame = getenv("E_FIRST_FRAME");
357    if (e_first_frame && (!e_first_frame[0]))
358      e_first_frame = NULL;
359    else
360      e_first_frame_start_time = ecore_time_get();
361
362    TS("EIO Init");
363    if (!eio_init())
364      {
365         e_error_message_show(_("Enlightenment cannot initialize EIO!\n"));
366         _e_main_shutdown(-1);
367      }
368    TS("EIO Init Done");
369    _e_main_shutdown_push(eio_shutdown);
370
371    ecore_app_args_set(argc, (const char **)argv);
372
373    TS("Ecore Event Handlers");
374    if (!ecore_event_handler_add(ECORE_EVENT_SIGNAL_EXIT,
375                                 _e_main_cb_signal_exit, NULL))
376      {
377         e_error_message_show(_("Enlightenment cannot set up an exit signal handler.\n"
378                                "Perhaps you are out of memory?"));
379         _e_main_shutdown(-1);
380      }
381    if (!ecore_event_handler_add(ECORE_EVENT_SIGNAL_HUP,
382                                 _e_main_cb_signal_hup, NULL))
383      {
384         e_error_message_show(_("Enlightenment cannot set up a HUP signal handler.\n"
385                                "Perhaps you are out of memory?"));
386         _e_main_shutdown(-1);
387      }
388    if (!ecore_event_handler_add(ECORE_EVENT_SIGNAL_USER,
389                                 _e_main_cb_signal_user, NULL))
390      {
391         e_error_message_show(_("Enlightenment cannot set up a USER signal handler.\n"
392                                "Perhaps you are out of memory?"));
393         _e_main_shutdown(-1);
394      }
395    TS("Ecore Event Handlers Done");
396
397    TS("Ecore_File Init");
398    if (!ecore_file_init())
399      {
400         e_error_message_show(_("Enlightenment cannot initialize Ecore_File!\n"));
401         _e_main_shutdown(-1);
402      }
403    TS("Ecore_File Init Done");
404    _e_main_shutdown_push(ecore_file_shutdown);
405
406    TS("Ecore_Con Init");
407    if (!ecore_con_init())
408      {
409         e_error_message_show(_("Enlightenment cannot initialize Ecore_Con!\n"));
410         _e_main_shutdown(-1);
411      }
412    TS("Ecore_Con Init Done");
413    _e_main_shutdown_push(ecore_con_shutdown);
414
415    TS("Ecore_Ipc Init");
416    if (!ecore_ipc_init())
417      {
418         e_error_message_show(_("Enlightenment cannot initialize Ecore_Ipc!\n"));
419         _e_main_shutdown(-1);
420      }
421    TS("Ecore_Ipc Init Done");
422    _e_main_shutdown_push(ecore_ipc_shutdown);
423
424    _idle_before = ecore_idle_enterer_before_add(_e_main_cb_idle_before, NULL);
425
426    _xdg_data_dirs_augment();
427
428    TS("Ecore_Evas Init");
429    if (!ecore_evas_init())
430      {
431         e_error_message_show(_("Enlightenment cannot initialize Ecore_Evas!\n"));
432         _e_main_shutdown(-1);
433      }
434    TS("Ecore_Evas Init Done");
435 //   _e_main_shutdown_push(ecore_evas_shutdown);
436
437    TS("Elementary Init");
438    if (!elm_init(argc, argv))
439      {
440         e_error_message_show(_("Enlightenment cannot initialize Elementary!\n"));
441         _e_main_shutdown(-1);
442      }
443    TS("Elementary Init Done");
444    //_e_main_shutdown_push(elm_shutdown);
445
446    TS("Emotion Init");
447    if (!emotion_init())
448      {
449         e_error_message_show(_("Enlightenment cannot initialize Emotion!\n"));
450         _e_main_shutdown(-1);
451      }
452    TS("Emotion Init Done");
453    _e_main_shutdown_push((void *)emotion_shutdown);
454
455    /* e doesn't sync to compositor - it should be one */
456    ecore_evas_app_comp_sync_set(0);
457
458    TS("Ecore_Evas Engine Check");
459 #ifdef HAVE_WAYLAND_ONLY
460    if (!ecore_evas_engine_type_supported_get(ECORE_EVAS_ENGINE_WAYLAND_SHM))
461      {
462         e_error_message_show(_("Enlightenment found ecore_evas doesn't support the Wayland SHM\n"
463                                "rendering in Evas. Please check your installation of Evas and\n"
464                                 "Ecore and check they support the Wayland SHM rendering engine."));
465         _e_main_shutdown(-1);
466      }
467 #else
468    if (!ecore_evas_engine_type_supported_get(ECORE_EVAS_ENGINE_SOFTWARE_XCB))
469      {
470         if (!ecore_evas_engine_type_supported_get(ECORE_EVAS_ENGINE_SOFTWARE_XLIB))
471           {
472              e_error_message_show(_("Enlightenment found ecore_evas doesn't support the Software X11\n"
473                                     "rendering in Evas. Please check your installation of Evas and\n"
474                                     "Ecore and check they support the Software X11 rendering engine."));
475              _e_main_shutdown(-1);
476           }
477      }
478 #endif
479    if (!ecore_evas_engine_type_supported_get(ECORE_EVAS_ENGINE_SOFTWARE_BUFFER))
480      {
481         e_error_message_show(_("Enlightenment found ecore_evas doesn't support the Software Buffer\n"
482                                "rendering in Evas. Please check your installation of Evas and\n"
483                                "Ecore and check they support the Software Buffer rendering engine."));
484         _e_main_shutdown(-1);
485      }
486    TS("Ecore_Evas Engine Check Done");
487
488    TS("Edje Init");
489    if (!edje_init())
490      {
491         e_error_message_show(_("Enlightenment cannot initialize Edje!\n"));
492         _e_main_shutdown(-1);
493      }
494    TS("Edje Init Done");
495    _e_main_shutdown_push(edje_shutdown);
496    edje_freeze();
497
498    /*** Initialize E Subsystems We Need ***/
499
500    TS("E Intl Init");
501    if (!e_intl_init())
502      {
503         e_error_message_show(_("Enlightenment cannot initialize E_Intl!\n"));
504         _e_main_shutdown(-1);
505      }
506    TS("E Intl Init Done");
507    _e_main_shutdown_push(e_intl_shutdown);
508
509 #ifndef HAVE_WAYLAND_ONLY
510    /* init white box of death alert */
511    TS("E_Alert Init");
512    if (!e_alert_init())
513      {
514         e_error_message_show(_("Enlightenment cannot initialize its emergency alert system.\n"
515                                "Have you set your DISPLAY variable?"));
516         _e_main_shutdown(-1);
517      }
518    TS("E_Alert Init Done");
519    _e_main_shutdown_push(e_alert_shutdown);
520 #endif
521
522    TS("E_Configure Init");
523    e_configure_init();
524    TS("E_Configure Init Done");
525
526    TS("E Directories Init");
527    /* setup directories we will be using for configurations storage etc. */
528    if (!_e_main_dirs_init())
529      {
530         e_error_message_show(_("Enlightenment cannot create directories in your home directory.\n"
531                                "Perhaps you have no home directory or the disk is full?"));
532         _e_main_shutdown(-1);
533      }
534    TS("E Directories Init Done");
535    _e_main_shutdown_push(_e_main_dirs_shutdown);
536
537    TS("E_Filereg Init");
538    if (!e_filereg_init())
539      {
540         e_error_message_show(_("Enlightenment cannot set up its file registry system.\n"));
541         _e_main_shutdown(-1);
542      }
543    TS("E_Filereg Init Done");
544    _e_main_shutdown_push(e_filereg_shutdown);
545
546    TS("E_Config Init");
547    if (!e_config_init())
548      {
549         e_error_message_show(_("Enlightenment cannot set up its config system.\n"));
550         _e_main_shutdown(-1);
551      }
552    TS("E_Config Init Done");
553    _e_main_shutdown_push(e_config_shutdown);
554
555    TS("E_Env Init");
556    if (!e_env_init())
557      {
558         e_error_message_show(_("Enlightenment cannot set up its environment.\n"));
559         _e_main_shutdown(-1);
560      }
561    TS("E_Env Init Done");
562    _e_main_shutdown_push(e_env_shutdown);
563
564    efreet_desktop_environment_set(e_config->desktop_environment);
565    e_util_env_set("E_ICON_THEME", e_config->icon_theme);
566    ecore_exe_run_priority_set(e_config->priority);
567    locked |= e_config->desklock_start_locked;
568
569    s = getenv("E_DESKLOCK_LOCKED");
570    if ((s) && (!strcmp(s, "locked"))) waslocked = EINA_TRUE;
571
572    TS("E Paths Init");
573    if (!_e_main_path_init())
574      {
575         e_error_message_show(_("Enlightenment cannot set up paths for finding files.\n"
576                                "Perhaps you are out of memory?"));
577         _e_main_shutdown(-1);
578      }
579    TS("E Paths Init Done");
580    _e_main_shutdown_push(_e_main_path_shutdown);
581
582    TS("E_Ipc Init");
583    if (!e_ipc_init()) _e_main_shutdown(-1);
584    TS("E_Ipc Init Done");
585    _e_main_shutdown_push(e_ipc_shutdown);
586
587    edje_frametime_set(1.0 / e_config->framerate);
588
589    TS("E_Font Init");
590    if (!e_font_init())
591      {
592         e_error_message_show(_("Enlightenment cannot set up its font system.\n"));
593         _e_main_shutdown(-1);
594      }
595    TS("E_Font Init Done");
596    _e_main_shutdown_push(e_font_shutdown);
597
598    TS("E_Font Apply");
599    e_font_apply();
600    TS("E_Font Apply Done");
601
602    TS("E_Theme Init");
603    if (!e_theme_init())
604      {
605         e_error_message_show(_("Enlightenment cannot set up its theme system.\n"));
606         _e_main_shutdown(-1);
607      }
608    TS("E_Theme Init Done");
609    _e_main_shutdown_push(e_theme_shutdown);
610
611    TS("E_Moveresize Init");
612    e_moveresize_init();
613    TS("E_Moveresize Init Done");
614    _e_main_shutdown_push(e_moveresize_shutdown);
615
616    if (e_config->show_splash)
617      e_init_status_set(_("Setup Message Bus"));
618    TS("E_Msgbus Init");
619    if (e_msgbus_init())
620      _e_main_shutdown_push(e_msgbus_shutdown);
621    TS("E_Msgbus Init Done");
622
623    TS("Efreet Init");
624    if (!efreet_init())
625      {
626         e_error_message_show(_("Enlightenment cannot initialize the FDO desktop system.\n"
627                                "Perhaps you lack permissions on ~/.cache/efreet or are\n"
628                                "out of memory or disk space?"));
629         _e_main_shutdown(-1);
630      }
631    TS("Efreet Init Done");
632    _e_main_shutdown_push(efreet_shutdown);
633
634    if (e_config->show_splash)
635      e_init_status_set(_("Starting International Support"));
636    TS("E_Intl Post Init");
637    if (!e_intl_post_init())
638      {
639         e_error_message_show(_("Enlightenment cannot set up its intl system.\n"));
640         _e_main_shutdown(-1);
641      }
642    TS("E_Intl Post Init Done");
643    _e_main_shutdown_push(e_intl_post_shutdown);
644
645    e_screensaver_preinit();
646
647    if (e_config->show_splash)
648      e_init_status_set(_("Setup Actions"));
649    TS("E_Actions Init");
650    if (!e_actions_init())
651      {
652         e_error_message_show(_("Enlightenment cannot set up its actions system.\n"));
653         _e_main_shutdown(-1);
654      }
655    TS("E_Actions Init Done");
656    _e_main_shutdown_push(e_actions_shutdown);
657
658    /* these just add event handlers and can't fail
659     * timestamping them is dumb.
660     */
661    e_zone_init();
662    e_desk_init();
663    e_exehist_init();
664
665    if (e_config->show_splash)
666      e_init_status_set(_("Setup Powersave Modes"));
667    TS("E_Powersave Init");
668    if (!e_powersave_init())
669      {
670         e_error_message_show(_("Enlightenment cannot set up its powersave modes.\n"));
671         _e_main_shutdown(-1);
672      }
673    TS("E_Powersave Init Done");
674    _e_main_shutdown_push(e_powersave_shutdown);
675
676    if (e_config->show_splash)
677      e_init_status_set(_("Setup Screensaver"));
678    TS("E_Screensaver Init");
679    if (!e_screensaver_init())
680      {
681         e_error_message_show(_("Enlightenment cannot configure the X screensaver.\n"));
682         _e_main_shutdown(-1);
683      }
684    TS("E_Screensaver Init Done");
685    _e_main_shutdown_push(e_screensaver_shutdown);
686
687    if (e_config->show_splash)
688      e_init_status_set(_("Setup Screens"));
689    TS("Screens Init");
690    if (!_e_main_screens_init())
691      {
692         e_error_message_show(_("Enlightenment set up window management for all the screens on your system\n"
693                                "failed. Perhaps another window manager is running?\n"));
694         _e_main_shutdown(-1);
695      }
696    TS("Screens Init Done");
697    _e_main_shutdown_push(_e_main_screens_shutdown);
698    e_screensaver_force_update();
699
700    TS("E_Pointer Init");
701    if (!e_pointer_init())
702      {
703         e_error_message_show(_("Enlightenment cannot set up its pointer system.\n"));
704         _e_main_shutdown(-1);
705      }
706    TS("E_Pointer Init Done");
707    _e_main_shutdown_push(e_pointer_shutdown);
708    e_menu_init();
709
710    TS("E_Scale Init");
711    if (!e_scale_init())
712      {
713         e_error_message_show(_("Enlightenment cannot set up its scale system.\n"));
714         _e_main_shutdown(-1);
715      }
716    TS("E_Scale Init Done");
717    _e_main_shutdown_push(e_scale_shutdown);
718
719    if (e_config->show_splash)
720      {
721         TS("E_Splash Init");
722         if (!e_init_init())
723           {
724              e_error_message_show(_("Enlightenment cannot set up its init screen.\n"));
725              _e_main_shutdown(-1);
726           }
727         TS("E_Splash Init Done");
728         _e_main_shutdown_push(e_init_shutdown);
729      }
730    if (!((!e_config->show_splash) || (after_restart)))
731      e_init_show();
732
733    if (!really_know)
734      {
735         TS("Test File Format Support");
736         _e_main_test_formats();
737         TS("Test File Format Support Done");
738      }
739    else
740      {
741         efreet_icon_extension_add(".svg");
742         efreet_icon_extension_add(".jpg");
743         efreet_icon_extension_add(".png");
744         efreet_icon_extension_add(".edj");
745      }
746
747    if (e_config->show_splash)
748      e_init_status_set(_("Setup ACPI"));
749    TS("E_Acpi Init");
750    e_acpi_init();
751    TS("E_Acpi Init Done");
752    _e_main_shutdown_push(e_acpi_shutdown);
753
754    if (e_config->show_splash)
755      e_init_status_set(_("Setup Backlight"));
756    TS("E_Backlight Init");
757    if (!e_backlight_init())
758      {
759         e_error_message_show(_("Enlightenment cannot configure the backlight.\n"));
760         _e_main_shutdown(-1);
761      }
762    TS("E_Backlight Init Done");
763
764    if (e_config->show_splash)
765      e_init_status_set(_("Setup DPMS"));
766    TS("E_Dpms Init");
767    if (!e_dpms_init())
768      {
769         e_error_message_show(_("Enlightenment cannot configure the DPMS settings.\n"));
770         _e_main_shutdown(-1);
771      }
772    TS("E_Dpms Init Done");
773    _e_main_shutdown_push(e_dpms_shutdown);
774
775    if (e_config->show_splash)
776      e_init_status_set(_("Setup Desklock"));
777    TS("E_Desklock Init");
778    if (!e_desklock_init())
779      {
780         e_error_message_show(_("Enlightenment cannot set up its desk locking system.\n"));
781         _e_main_shutdown(-1);
782      }
783    TS("E_Desklock Init Done");
784    _e_main_shutdown_push(e_desklock_shutdown);
785
786    if (waslocked || (locked && ((!after_restart) || (!getenv("E_DESKLOCK_UNLOCKED")))))
787      e_desklock_show(EINA_TRUE);
788
789    if (e_config->show_splash)
790      e_init_status_set(_("Setup Paths"));
791    TS("Efreet Paths");
792    _e_main_efreet_paths_init();
793    TS("Efreet Paths Done");
794
795    if (e_config->show_splash)
796      e_init_status_set(_("Setup System Controls"));
797    TS("E_Sys Init");
798    if (!e_sys_init())
799      {
800         e_error_message_show(_("Enlightenment cannot initialize the System Command system.\n"));
801         _e_main_shutdown(-1);
802      }
803    TS("E_Sys Init Done");
804    _e_main_shutdown_push(e_sys_shutdown);
805
806    if (e_config->show_splash)
807      e_init_status_set(_("Setup Execution System"));
808    TS("E_Exec Init");
809    if (!e_exec_init())
810      {
811         e_error_message_show(_("Enlightenment cannot set up its exec system.\n"));
812         _e_main_shutdown(-1);
813      }
814    TS("E_Exec Init Done");
815
816    TS("E_Comp Freeze");
817    e_comp_all_freeze();
818    TS("E_Comp Freeze Done");
819
820    if (e_config->show_splash)
821      e_init_status_set(_("Setup Filemanager"));
822    TS("E_Fm2 Init");
823    if (!e_fm2_init())
824      {
825         e_error_message_show(_("Enlightenment cannot initialize the File manager.\n"));
826         _e_main_shutdown(-1);
827      }
828    TS("E_Fm2 Init Done");
829    _e_main_shutdown_push(e_fm2_shutdown);
830
831    if (e_config->show_splash)
832      e_init_status_set(_("Setup Message System"));
833    TS("E_Msg Init");
834    if (!e_msg_init())
835      {
836         e_error_message_show(_("Enlightenment cannot set up its msg system.\n"));
837         _e_main_shutdown(-1);
838      }
839    TS("E_Msg Init Done");
840    _e_main_shutdown_push(e_msg_shutdown);
841
842    if (e_config->show_splash)
843      e_init_status_set(_("Setup Grab Input Handling"));
844    TS("E_Grabinput Init");
845    if (!e_grabinput_init())
846      {
847         e_error_message_show(_("Enlightenment cannot set up its grab input handling system.\n"));
848         _e_main_shutdown(-1);
849      }
850    TS("E_Grabinput Init Done");
851    _e_main_shutdown_push(e_grabinput_shutdown);
852
853    if (e_config->show_splash)
854      e_init_status_set(_("Setup Modules"));
855    TS("E_Module Init");
856    if (!e_module_init())
857      {
858         e_error_message_show(_("Enlightenment cannot set up its module system.\n"));
859         _e_main_shutdown(-1);
860      }
861    TS("E_Module Init Done");
862    _e_main_shutdown_push(e_module_shutdown);
863
864    if (e_config->show_splash)
865      e_init_status_set(_("Setup Remembers"));
866    TS("E_Remember Init");
867    if (!e_remember_init(after_restart ? E_STARTUP_RESTART : E_STARTUP_START))
868      {
869         e_error_message_show(_("Enlightenment cannot setup remember settings.\n"));
870         _e_main_shutdown(-1);
871      }
872    TS("E_Remember Init Done");
873    _e_main_shutdown_push(e_remember_shutdown);
874
875    if (e_config->show_splash)
876      e_init_status_set(_("Setup Color Classes"));
877    TS("E_Color_Class Init");
878    if (!e_color_class_init())
879      {
880         e_error_message_show(_("Enlightenment cannot set up its color class system.\n"));
881         _e_main_shutdown(-1);
882      }
883    TS("E_Color_Class Init Done");
884    _e_main_shutdown_push(e_color_class_shutdown);
885
886    if (e_config->show_splash)
887      e_init_status_set(_("Setup Gadcon"));
888    TS("E_Gadcon Init");
889    if (!e_gadcon_init())
890      {
891         e_error_message_show(_("Enlightenment cannot set up its gadget control system.\n"));
892         _e_main_shutdown(-1);
893      }
894    TS("E_Gadcon Init Done");
895    _e_main_shutdown_push(e_gadcon_shutdown);
896
897    if (e_config->show_splash)
898      e_init_status_set(_("Setup Toolbars"));
899    TS("E_Toolbar Init");
900    if (!e_toolbar_init())
901      {
902         e_error_message_show(_("Enlightenment cannot set up its toolbars.\n"));
903         _e_main_shutdown(-1);
904      }
905    TS("E_Toolbar Init Done");
906    _e_main_shutdown_push(e_toolbar_shutdown);
907
908    if (e_config->show_splash)
909      e_init_status_set(_("Setup Wallpaper"));
910    TS("E_Bg Init");
911    if (!e_bg_init())
912      {
913         e_error_message_show(_("Enlightenment cannot set up its desktop background system.\n"));
914         _e_main_shutdown(-1);
915      }
916    TS("E_Bg Init Done");
917    _e_main_shutdown_push(e_bg_shutdown);
918
919    if (e_config->show_splash)
920      e_init_status_set(_("Setup Mouse"));
921    TS("E_Mouse Init");
922    if (!e_mouse_update())
923      {
924         e_error_message_show(_("Enlightenment cannot configure the mouse settings.\n"));
925         _e_main_shutdown(-1);
926      }
927    TS("E_Mouse Init Done");
928
929    if (e_config->show_splash)
930      e_init_status_set(_("Setup Bindings"));
931    TS("E_Bindings Init");
932    if (!e_bindings_init())
933      {
934         e_error_message_show(_("Enlightenment cannot set up its bindings system.\n"));
935         _e_main_shutdown(-1);
936      }
937    TS("E_Bindings Init Done");
938    _e_main_shutdown_push(e_bindings_shutdown);
939
940    if (e_config->show_splash)
941      e_init_status_set(_("Setup Thumbnailer"));
942    TS("E_Thumb Init");
943    if (!e_thumb_init())
944      {
945         e_error_message_show(_("Enlightenment cannot initialize the Thumbnailing system.\n"));
946         _e_main_shutdown(-1);
947      }
948    TS("E_Thumb Init Done");
949    _e_main_shutdown_push(e_thumb_shutdown);
950
951    TS("E_Icon Init");
952    if (!e_icon_init())
953      {
954         e_error_message_show(_("Enlightenment cannot initialize the Icon Cache system.\n"));
955         _e_main_shutdown(-1);
956      }
957    TS("E_Icon Init Done");
958    _e_main_shutdown_push(e_icon_shutdown);
959
960    TS("E_Update Init");
961    if (!e_update_init())
962      {
963         e_error_message_show(_("Enlightenment cannot initialize the Update system.\n"));
964         _e_main_shutdown(-1);
965      }
966    TS("E_Update Init Done");
967    _e_main_shutdown_push(e_update_shutdown);
968
969    if (e_config->show_splash)
970      e_init_status_set(_("Setup Desktop Environment"));
971    TS("E_Deskenv Init");
972    if (!e_deskenv_init())
973      {
974         e_error_message_show(_("Enlightenment cannot initialize its desktop environment.\n"));
975         _e_main_shutdown(-1);
976      }
977    TS("E_Deskenv Init Done");
978    _e_main_shutdown_push(e_deskenv_shutdown);
979
980    if (e_config->show_splash)
981      e_init_status_set(_("Setup File Ordering"));
982    TS("E_Order Init");
983    if (!e_order_init())
984      {
985         e_error_message_show(_("Enlightenment cannot set up its order file system.\n"));
986         _e_main_shutdown(-1);
987      }
988    TS("E_Order Init Done");
989    _e_main_shutdown_push(e_order_shutdown);
990
991    TS("Add Idler For X Flush");
992    _idle_flush = ecore_idle_enterer_add(_e_main_cb_x_flusher, NULL);
993    TS("Add Idler For X Flush Done");
994
995    TS("E_Manager Keys Grab");
996    e_managers_keys_grab();
997    TS("E_Manager Keys Grab Done");
998
999    if (e_config->show_splash)
1000      e_init_status_set(_("Load Modules"));
1001    TS("Load Modules");
1002    _e_main_modules_load(safe_mode);
1003    TS("Load Modules Done");
1004
1005    TS("Run Startup Apps");
1006    if (!nostartup)
1007      {
1008         if (after_restart)
1009           e_startup(E_STARTUP_RESTART);
1010         else
1011           e_startup(E_STARTUP_START);
1012      }
1013    TS("Run Startup Apps Done");
1014
1015    if (e_config->show_splash && (!after_restart))
1016      ecore_timer_add(2.0, _e_main_cb_startup_fake_end, NULL);
1017
1018    TS("E_Comp Thaw");
1019    e_comp_all_thaw();
1020    TS("E_Comp Thaw Done");
1021
1022    TS("E_Test Init");
1023    e_test();
1024    TS("E_Test Done");
1025
1026    TS("E_Test_Helper Init");
1027    e_test_helper_init();
1028    _e_main_shutdown_push(e_test_helper_shutdown);
1029    TS("E_Test_Helper Done");
1030
1031    if (e_config->show_splash)
1032      e_init_status_set(_("Setup Shelves"));
1033    TS("E_Shelf Init");
1034    if (!e_shelf_init())
1035      {
1036         e_error_message_show(_("Enlightenment cannot set up its module system.\n"));
1037         _e_main_shutdown(-1);
1038      }
1039    TS("E_Shelf Init Done");
1040
1041    ecore_idle_enterer_before_add(_e_main_shelf_init_job, NULL);
1042
1043    _idle_after = ecore_idle_enterer_add(_e_main_cb_idle_after, NULL);
1044
1045    if (e_config->show_splash)
1046      e_init_status_set(_("Almost Done"));
1047
1048    starting = EINA_FALSE;
1049    inloop = EINA_TRUE;
1050
1051    e_util_env_set("E_RESTART", "1");
1052
1053    TS("MAIN LOOP AT LAST");
1054    if (!setjmp(x_fatal_buff))
1055      ecore_main_loop_begin();
1056    else
1057      CRI("FATAL: X Died. Connection gone. Abbreviated Shutdown\n");
1058
1059    inloop = EINA_FALSE;
1060    stopping = EINA_TRUE;
1061
1062    //if (!x_fatal) e_canvas_idle_flush();
1063
1064    e_config_save_flush();
1065    _e_main_desk_save();
1066    e_remember_internal_save();
1067    e_comp_internal_save();
1068
1069    _e_main_shutdown(0);
1070
1071    if (restart)
1072      {
1073         e_util_env_set("E_RESTART_OK", "1");
1074         if (getenv("E_START_MTRACK"))
1075           e_util_env_set("MTRACK", "track");
1076         ecore_app_restart();
1077      }
1078
1079    e_prefix_shutdown();
1080
1081    return 0;
1082 }
1083
1084 EAPI double
1085 e_main_ts(const char *str)
1086 {
1087    double ret;
1088    t1 = ecore_time_unix_get();
1089    printf("ESTART: %1.5f [%1.5f] - %s\n", t1 - t0, t1 - t2, str);
1090    ret = t1 - t2;
1091    t2 = t1;
1092    return ret;
1093 }
1094
1095 /* local functions */
1096 static void
1097 _e_main_shutdown(int errcode)
1098 {
1099    int i = 0;
1100    char buf[PATH_MAX];
1101    const char *dir;
1102
1103    printf("E: Begin Shutdown Procedure!\n");
1104
1105    if (_idle_before) ecore_idle_enterer_del(_idle_before);
1106    _idle_before = NULL;
1107    if (_idle_after) ecore_idle_enterer_del(_idle_after);
1108    _idle_after = NULL;
1109    if (_idle_flush) ecore_idle_enterer_del(_idle_flush);
1110    _idle_flush = NULL;
1111
1112    dir = getenv("XDG_RUNTIME_DIR");
1113    if (dir)
1114      {
1115         snprintf(buf, sizeof(buf), "%s/.e-deleteme", dir);
1116         if (ecore_file_exists(buf)) ecore_file_recursive_rm(dir);
1117      }
1118    for (i = (_e_main_lvl - 1); i >= 0; i--)
1119      (*_e_main_shutdown_func[i])();
1120    if (errcode < 0) exit(errcode);
1121 }
1122
1123 static void
1124 _e_main_shutdown_push(int (*func)(void))
1125 {
1126    _e_main_lvl++;
1127    if (_e_main_lvl > MAX_LEVEL)
1128      {
1129         _e_main_lvl--;
1130         e_error_message_show("WARNING: too many init levels. MAX = %i\n",
1131                              MAX_LEVEL);
1132         return;
1133      }
1134    _e_main_shutdown_func[_e_main_lvl - 1] = func;
1135 }
1136
1137 static void
1138 _e_main_parse_arguments(int argc, char **argv)
1139 {
1140    char *s = NULL;
1141    int i = 0;
1142
1143    /* handle some command-line parameters */
1144    for (i = 1; i < argc; i++)
1145      {
1146         if ((!strcmp(argv[i], "-display")) && (i < (argc - 1)))
1147           {
1148              i++;
1149              e_util_env_set("DISPLAY", argv[i]);
1150           }
1151         else if ((!strcmp(argv[i], "-fake-xinerama-screen")) && (i < (argc - 1)))
1152           {
1153              int x, y, w, h;
1154
1155              i++;
1156              if (sscanf(argv[i], "%ix%i+%i+%i", &w, &h, &x, &y) == 4)
1157                e_xinerama_fake_screen_add(x, y, w, h);
1158           }
1159         else if (!strcmp(argv[i], "-good"))
1160           {
1161              good = EINA_TRUE;
1162              evil = EINA_FALSE;
1163              printf("LA LA LA\n");
1164           }
1165         else if (!strcmp(argv[i], "-evil"))
1166           {
1167              good = EINA_FALSE;
1168              evil = EINA_TRUE;
1169              printf("MUHAHAHAHHAHAHAHAHA\n");
1170           }
1171         else if (!strcmp(argv[i], "-psychotic"))
1172           {
1173              good = EINA_TRUE;
1174              evil = EINA_TRUE;
1175              printf("MUHAHALALALALALALALA\n");
1176           }
1177         else if ((!strcmp(argv[i], "-profile")) && (i < (argc - 1)))
1178           {
1179              i++;
1180              if (!getenv("E_CONF_PROFILE"))
1181                e_util_env_set("E_CONF_PROFILE", argv[i]);
1182           }
1183         else if (!strcmp(argv[i], "-i-really-know-what-i-am-doing-and-accept-full-responsibility-for-it"))
1184           really_know = EINA_TRUE;
1185         else if (!strcmp(argv[i], "-locked"))
1186           locked = EINA_TRUE;
1187         else if (!strcmp(argv[i], "-nopause"))
1188           e_nopause = EINA_TRUE;
1189         else if ((!strcmp(argv[i], "-h")) ||
1190                  (!strcmp(argv[i], "-help")) ||
1191                  (!strcmp(argv[i], "--help")))
1192           {
1193              printf
1194                (_(
1195                  "Options:\n"
1196                  "\t-display DISPLAY\n"
1197                  "\t\tConnect to display named DISPLAY.\n"
1198                  "\t\tEG: -display :1.0\n"
1199                  "\t-fake-xinerama-screen WxH+X+Y\n"
1200                  "\t\tAdd a FAKE xinerama screen (instead of the real ones)\n"
1201                  "\t\tgiven the geometry. Add as many as you like. They all\n"
1202                  "\t\treplace the real xinerama screens, if any. This can\n"
1203                  "\t\tbe used to simulate xinerama.\n"
1204                  "\t\tEG: -fake-xinerama-screen 800x600+0+0 -fake-xinerama-screen 800x600+800+0\n"
1205                  "\t-profile CONF_PROFILE\n"
1206                  "\t\tUse the configuration profile CONF_PROFILE instead of the user selected default or just \"default\".\n"
1207                  "\t-good\n"
1208                  "\t\tBe good.\n"
1209                  "\t-evil\n"
1210                  "\t\tBe evil.\n"
1211                  "\t-psychotic\n"
1212                  "\t\tBe psychotic.\n"
1213                  "\t-locked\n"
1214                  "\t\tStart with desklock on, so password will be asked.\n"
1215                  "\t-i-really-know-what-i-am-doing-and-accept-full-responsibility-for-it\n"
1216                  "\t\tIf you need this help, you don't need this option.\n"
1217                  )
1218                );
1219              _e_main_shutdown(-1);
1220           }
1221      }
1222
1223    /* fix up DISPLAY to be :N.0 if no .screen is in it */
1224    s = getenv("DISPLAY");
1225    if (s)
1226      {
1227         char *p, buff[4096];
1228
1229         if (!(p = strrchr(s, ':')))
1230           {
1231              snprintf(buff, sizeof(buff), "%s:0.0", s);
1232              e_util_env_set("DISPLAY", buff);
1233           }
1234         else
1235           {
1236              if (!strrchr(p, '.'))
1237                {
1238                   snprintf(buff, sizeof(buff), "%s.0", s);
1239                   e_util_env_set("DISPLAY", buff);
1240                }
1241           }
1242      }
1243
1244    /* we want to have been launched by enlightenment_start. there is a very */
1245    /* good reason we want to have been launched this way, thus check */
1246    if (!getenv("E_START"))
1247      {
1248         e_error_message_show(_("You are executing enlightenment directly. This is\n"
1249                                "bad. Please do not execute the \"enlightenment\"\n"
1250                                "binary. Use the \"enlightenment_start\" launcher. It\n"
1251                                "will handle setting up environment variables, paths,\n"
1252                                "and launching any other required services etc.\n"
1253                                "before enlightenment itself begins running.\n"));
1254         _e_main_shutdown(-1);
1255      }
1256 }
1257
1258 EINTERN void
1259 _e_main_cb_x_fatal(void *data __UNUSED__)
1260 {
1261    e_error_message_show("Lost X Connection.\n");
1262    ecore_main_loop_quit();
1263    if (!x_fatal)
1264      {
1265         x_fatal = EINA_TRUE;
1266         if (inloop) longjmp(x_fatal_buff, -99);
1267      }
1268 }
1269
1270 static Eina_Bool
1271 _e_main_cb_signal_exit(void *data __UNUSED__, int ev_type __UNUSED__, void *ev __UNUSED__)
1272 {
1273    /* called on ctrl-c, kill (pid) (also SIGINT, SIGTERM and SIGQIT) */
1274    e_sys_action_do(E_SYS_EXIT, NULL);
1275    return ECORE_CALLBACK_RENEW;
1276 }
1277
1278 static Eina_Bool
1279 _e_main_cb_signal_hup(void *data __UNUSED__, int ev_type __UNUSED__, void *ev __UNUSED__)
1280 {
1281    e_sys_action_do(E_SYS_RESTART, NULL);
1282    return ECORE_CALLBACK_RENEW;
1283 }
1284
1285 static Eina_Bool
1286 _e_main_cb_signal_user(void *data __UNUSED__, int ev_type __UNUSED__, void *ev)
1287 {
1288    Ecore_Event_Signal_User *e = ev;
1289
1290    if (e->number == 1)
1291      {
1292 //        E_Action *a = e_action_find("configuration");
1293 //        if ((a) && (a->func.go)) a->func.go(NULL, NULL);
1294      }
1295    else if (e->number == 2)
1296      {
1297         // comp module has its own handler for this for enabling/disabling fps debug
1298      }
1299    return ECORE_CALLBACK_RENEW;
1300
1301 }
1302
1303 static int
1304 _e_main_dirs_init(void)
1305 {
1306    const char *base;
1307    const char *dirs[] =
1308    {
1309       "images",
1310       "fonts",
1311       "themes",
1312       "icons",
1313       "backgrounds",
1314       "applications",
1315       "applications/menu",
1316       "applications/menu/favorite",
1317       "applications/menu/all",
1318       "applications/bar",
1319       "applications/bar/default",
1320       "applications/startup",
1321       "applications/restart",
1322       "applications/trash",
1323       "applications/desk-lock",
1324       "applications/desk-unlock",
1325       "modules",
1326       "config",
1327       "locale",
1328       "input_methods",
1329       NULL
1330    };
1331
1332    base = e_user_dir_get();
1333    if (ecore_file_mksubdirs(base, dirs) != sizeof(dirs) / sizeof(dirs[0]) - 1)
1334      {
1335         e_error_message_show("Could not create one of the required "
1336                              "subdirectories of '%s'\n", base);
1337         return 0;
1338      }
1339
1340    return 1;
1341 }
1342
1343 static int
1344 _e_main_dirs_shutdown(void)
1345 {
1346    return 1;
1347 }
1348
1349 static int
1350 _e_main_path_init(void)
1351 {
1352    char buf[PATH_MAX];
1353
1354    /* setup data paths */
1355    path_data = e_path_new();
1356    if (!path_data)
1357      {
1358         e_error_message_show("Cannot allocate path for path_data\n");
1359         return 0;
1360      }
1361    e_prefix_data_concat_static(buf, "data");
1362    e_path_default_path_append(path_data, buf);
1363    e_path_user_path_set(path_data, &(e_config->path_append_data));
1364
1365    /* setup image paths */
1366    path_images = e_path_new();
1367    if (!path_images)
1368      {
1369         e_error_message_show("Cannot allocate path for path_images\n");
1370         return 0;
1371      }
1372    e_user_dir_concat_static(buf, "/images");
1373    e_path_default_path_append(path_images, buf);
1374    e_prefix_data_concat_static(buf, "data/images");
1375    e_path_default_path_append(path_images, buf);
1376    e_path_user_path_set(path_images, &(e_config->path_append_images));
1377
1378    /* setup font paths */
1379    path_fonts = e_path_new();
1380    if (!path_fonts)
1381      {
1382         e_error_message_show("Cannot allocate path for path_fonts\n");
1383         return 0;
1384      }
1385    e_user_dir_concat_static(buf, "/fonts");
1386    e_path_default_path_append(path_fonts, buf);
1387    e_prefix_data_concat_static(buf, "data/fonts");
1388    e_path_default_path_append(path_fonts, buf);
1389    e_path_user_path_set(path_fonts, &(e_config->path_append_fonts));
1390
1391    /* setup icon paths */
1392    path_icons = e_path_new();
1393    if (!path_icons)
1394      {
1395         e_error_message_show("Cannot allocate path for path_icons\n");
1396         return 0;
1397      }
1398    e_user_dir_concat_static(buf, "/icons");
1399    e_path_default_path_append(path_icons, buf);
1400    e_prefix_data_concat_static(buf, "data/icons");
1401    e_path_default_path_append(path_icons, buf);
1402    e_path_user_path_set(path_icons, &(e_config->path_append_icons));
1403
1404    /* setup module paths */
1405    path_modules = e_path_new();
1406    if (!path_modules)
1407      {
1408         e_error_message_show("Cannot allocate path for path_modules\n");
1409         return 0;
1410      }
1411    e_user_dir_concat_static(buf, "/modules");
1412    e_path_default_path_append(path_modules, buf);
1413    snprintf(buf, sizeof(buf), "%s/enlightenment/modules", e_prefix_lib_get());
1414    e_path_default_path_append(path_modules, buf);
1415    /* FIXME: eventually this has to go - moduels should have installers that
1416     * add appropriate install paths (if not installed to user homedir) to
1417     * e's module search dirs
1418     */
1419    snprintf(buf, sizeof(buf), "%s/enlightenment/modules_extra", e_prefix_lib_get());
1420    e_path_default_path_append(path_modules, buf);
1421    e_path_user_path_set(path_modules, &(e_config->path_append_modules));
1422
1423    /* setup background paths */
1424    path_backgrounds = e_path_new();
1425    if (!path_backgrounds)
1426      {
1427         e_error_message_show("Cannot allocate path for path_backgrounds\n");
1428         return 0;
1429      }
1430    e_user_dir_concat_static(buf, "/backgrounds");
1431    e_path_default_path_append(path_backgrounds, buf);
1432    e_prefix_data_concat_static(buf, "data/backgrounds");
1433    e_path_default_path_append(path_backgrounds, buf);
1434    e_path_user_path_set(path_backgrounds, &(e_config->path_append_backgrounds));
1435
1436    path_messages = e_path_new();
1437    if (!path_messages)
1438      {
1439         e_error_message_show("Cannot allocate path for path_messages\n");
1440         return 0;
1441      }
1442    e_user_dir_concat_static(buf, "/locale");
1443    e_path_default_path_append(path_messages, buf);
1444    e_path_default_path_append(path_messages, e_prefix_locale_get());
1445    e_path_user_path_set(path_messages, &(e_config->path_append_messages));
1446
1447    return 1;
1448 }
1449
1450 static int
1451 _e_main_path_shutdown(void)
1452 {
1453    if (path_data)
1454      {
1455         e_object_del(E_OBJECT(path_data));
1456         path_data = NULL;
1457      }
1458    if (path_images)
1459      {
1460         e_object_del(E_OBJECT(path_images));
1461         path_images = NULL;
1462      }
1463    if (path_fonts)
1464      {
1465         e_object_del(E_OBJECT(path_fonts));
1466         path_fonts = NULL;
1467      }
1468    if (path_icons)
1469      {
1470         e_object_del(E_OBJECT(path_icons));
1471         path_icons = NULL;
1472      }
1473    if (path_modules)
1474      {
1475         e_object_del(E_OBJECT(path_modules));
1476         path_modules = NULL;
1477      }
1478    if (path_backgrounds)
1479      {
1480         e_object_del(E_OBJECT(path_backgrounds));
1481         path_backgrounds = NULL;
1482      }
1483    if (path_messages)
1484      {
1485         e_object_del(E_OBJECT(path_messages));
1486         path_messages = NULL;
1487      }
1488    return 1;
1489 }
1490
1491 static void
1492 _e_main_test_formats(void)
1493 {
1494    Evas *evas;
1495    Ecore_Evas *ee;
1496    Evas_Object *im, *txt;
1497    Evas_Coord tw, th;
1498    char buff[PATH_MAX];
1499
1500    if (e_config->show_splash)
1501      e_init_status_set(_("Testing Format Support"));
1502
1503    if (!(ee = ecore_evas_buffer_new(1, 1)))
1504      {
1505         e_error_message_show(_("Enlightenment found Evas can't create a buffer canvas. Please check\n"
1506                                "Evas has Software Buffer engine support.\n"));
1507         _e_main_shutdown(-1);
1508      }
1509    evas = ecore_evas_get(ee);
1510    im = evas_object_image_add(evas);
1511
1512    e_prefix_data_concat_static(buff, "data/images/test.svg");
1513    evas_object_image_file_set(im, buff, NULL);
1514    if (evas_object_image_load_error_get(im) != EVAS_LOAD_ERROR_NONE)
1515      {
1516         e_error_message_show(_("Enlightenment found Evas can't load SVG files. "
1517                                "Check Evas has SVG loader support.\n"));
1518      }
1519    else
1520      efreet_icon_extension_add(".svg");
1521
1522    e_prefix_data_concat_static(buff, "data/images/test.jpg");
1523    evas_object_image_file_set(im, buff, NULL);
1524    if (evas_object_image_load_error_get(im) != EVAS_LOAD_ERROR_NONE)
1525      {
1526         e_error_message_show(_("Enlightenment found Evas can't load JPEG files. "
1527                                "Check Evas has JPEG loader support.\n"));
1528         _e_main_shutdown(-1);
1529      }
1530    efreet_icon_extension_add(".jpg");
1531
1532    e_prefix_data_concat_static(buff, "data/images/test.png");
1533    evas_object_image_file_set(im, buff, NULL);
1534    if (evas_object_image_load_error_get(im) != EVAS_LOAD_ERROR_NONE)
1535      {
1536         e_error_message_show(_("Enlightenment found Evas can't load PNG files. "
1537                                "Check Evas has PNG loader support.\n"));
1538         _e_main_shutdown(-1);
1539      }
1540    efreet_icon_extension_add(".png");
1541
1542    e_prefix_data_concat_static(buff, "data/images/test.edj");
1543    evas_object_image_file_set(im, buff, "images/0");
1544    if (evas_object_image_load_error_get(im) != EVAS_LOAD_ERROR_NONE)
1545      {
1546         e_error_message_show(_("Enlightenment found Evas can't load EET files. "
1547                                "Check Evas has EET loader support.\n"));
1548         _e_main_shutdown(-1);
1549      }
1550    efreet_icon_extension_add(".edj");
1551
1552    evas_object_del(im);
1553
1554    txt = evas_object_text_add(evas);
1555    evas_object_text_font_set(txt, "Sans", 10);
1556    evas_object_text_text_set(txt, "Hello");
1557    evas_object_geometry_get(txt, NULL, NULL, &tw, &th);
1558    if ((tw <= 0) && (th <= 0))
1559      {
1560         e_error_message_show(_("Enlightenment found Evas can't load the 'Sans' font. Check Evas has fontconfig\n"
1561                                "support and system fontconfig defines a 'Sans' font.\n"));
1562         _e_main_shutdown(-1);
1563      }
1564    evas_object_del(txt);
1565    ecore_evas_free(ee);
1566 }
1567
1568 static int
1569 _e_main_screens_init(void)
1570 {
1571    TS("\tscreens: manager");
1572    if (!e_manager_init()) return 0;
1573
1574    TS("\tscreens: client");
1575    if (!e_client_init()) return 0;
1576    TS("\tscreens: win");
1577    if (!e_win_init()) return 0;
1578 #ifndef HAVE_WAYLAND_ONLY
1579    TS("E_Xkb Init");
1580    if (!e_xkb_init())
1581      {
1582         e_error_message_show(_("Enlightenment cannot setup XKB Keyboard layouts.\n"));
1583         _e_main_shutdown(-1);
1584      }
1585    TS("E_Xkb Init Done");
1586 #endif
1587
1588    TS("Compositor Init");
1589    if (!e_comp_init())
1590      {
1591         e_error_message_show(_("Enlightenment cannot create a compositor.\n"));
1592         _e_main_shutdown(-1);
1593      }
1594
1595    _e_main_desk_restore();
1596
1597 #ifndef HAVE_WAYLAND_ONLY
1598    if (e_config->show_splash)
1599      e_init_status_set(_("Setup DND"));
1600    TS("E_Dnd Init");
1601    if (!e_dnd_init())
1602      {
1603         e_error_message_show(_("Enlightenment cannot set up its dnd system.\n"));
1604         _e_main_shutdown(-1);
1605      }
1606    TS("E_Dnd Init Done");
1607    _e_main_shutdown_push(e_dnd_shutdown);
1608 #endif
1609
1610    return 1;
1611 }
1612
1613 static int
1614 _e_main_screens_shutdown(void)
1615 {
1616    e_win_shutdown();
1617    e_menu_shutdown();
1618    e_shelf_shutdown();
1619    e_comp_shutdown();
1620    e_client_shutdown();
1621    e_exehist_shutdown();
1622    e_backlight_shutdown();
1623    e_exec_shutdown();
1624
1625    e_desk_shutdown();
1626    e_zone_shutdown();
1627    e_manager_shutdown();
1628    return 1;
1629 }
1630
1631 static void
1632 _e_main_desk_save(void)
1633 {
1634    const Eina_List *l;
1635    char env[1024], name[1024];
1636    E_Zone *zone;
1637
1638    EINA_LIST_FOREACH(e_comp->zones, l, zone)
1639      {
1640         snprintf(name, sizeof(name), "DESK_%d_%d", e_comp->num, zone->num);
1641         snprintf(env, sizeof(env), "%d,%d", zone->desk_x_current, zone->desk_y_current);
1642         e_util_env_set(name, env);
1643      }
1644 }
1645
1646 static void
1647 _e_main_desk_restore(void)
1648 {
1649    const Eina_List *l;
1650    E_Zone *zone;
1651    char *env;
1652    char name[1024];
1653
1654    EINA_LIST_FOREACH(e_comp->zones, l, zone)
1655      {
1656         E_Desk *desk;
1657         int desk_x, desk_y;
1658
1659         snprintf(name, sizeof(name), "DESK_%d_%d", e_comp->num, zone->num);
1660         env = getenv(name);
1661         if (!env) continue;
1662         if (!sscanf(env, "%d,%d", &desk_x, &desk_y)) continue;
1663         desk = e_desk_at_xy_get(zone, desk_x, desk_y);
1664         if (!desk) continue;
1665         e_desk_show(desk);
1666      }
1667 }
1668
1669 static void
1670 _e_main_efreet_paths_init(void)
1671 {
1672    Eina_List **list;
1673
1674    if ((list = efreet_icon_extra_list_get()))
1675      {
1676         char buff[PATH_MAX];
1677
1678         e_user_dir_concat_static(buff, "icons");
1679         *list = eina_list_prepend(*list, (void *)eina_stringshare_add(buff));
1680         e_prefix_data_concat_static(buff, "data/icons");
1681         *list = eina_list_prepend(*list, (void *)eina_stringshare_add(buff));
1682      }
1683 }
1684
1685 static Eina_Bool
1686 _e_main_modules_load_after(void *d EINA_UNUSED, int type EINA_UNUSED, void *ev EINA_UNUSED)
1687 {
1688    e_int_config_modules(NULL, NULL);
1689    E_FREE_FUNC(mod_init_end, ecore_event_handler_del);
1690    return ECORE_CALLBACK_RENEW;
1691 }
1692
1693 static void
1694 _e_main_modules_load(Eina_Bool safe_mode)
1695 {
1696    if (!safe_mode)
1697      e_module_all_load();
1698    else
1699      {
1700         E_Module *m;
1701         char *crashmodule;
1702
1703         crashmodule = getenv("E_MODULE_LOAD");
1704         if (crashmodule) m = e_module_new(crashmodule);
1705
1706         if ((crashmodule) && (m))
1707           {
1708              e_module_disable(m);
1709              e_object_del(E_OBJECT(m));
1710
1711              e_error_message_show
1712                (_("Enlightenment crashed early on start and has<br>"
1713                   "been restarted. There was an error loading the<br>"
1714                   "module named: %s. This module has been disabled<br>"
1715                   "and will not be loaded."), crashmodule);
1716              e_util_dialog_show
1717                (_("Enlightenment crashed early on start and has been restarted"),
1718                _("Enlightenment crashed early on start and has been restarted.<br>"
1719                  "There was an error loading the module named: %s<br><br>"
1720                  "This module has been disabled and will not be loaded."), crashmodule);
1721              e_module_all_load();
1722           }
1723         else
1724           {
1725              e_error_message_show
1726                (_("Enlightenment crashed early on start and has<br>"
1727                   "been restarted. All modules have been disabled<br>"
1728                   "and will not be loaded to help remove any problem<br>"
1729                   "modules from your configuration. The module<br>"
1730                   "configuration dialog should let you select your<br>"
1731                   "modules again.\n"));
1732              e_util_dialog_show
1733                (_("Enlightenment crashed early on start and has been restarted"),
1734                _("Enlightenment crashed early on start and has been restarted.<br>"
1735                  "All modules have been disabled and will not be loaded to help<br>"
1736                  "remove any problem modules from your configuration.<br><br>"
1737                  "The module configuration dialog should let you select your<br>"
1738                  "modules again."));
1739           }
1740         mod_init_end = ecore_event_handler_add(E_EVENT_MODULE_INIT_END, _e_main_modules_load_after, NULL);
1741      }
1742 }
1743
1744 static Eina_Bool
1745 _e_main_cb_idle_before(void *data __UNUSED__)
1746 {
1747    e_menu_idler_before();
1748    e_client_idler_before();
1749    e_pointer_idler_before();
1750    edje_thaw();
1751    return ECORE_CALLBACK_RENEW;
1752 }
1753
1754 static Eina_Bool
1755 _e_main_cb_idle_after(void *data __UNUSED__)
1756 {
1757    static int first_idle = 1;
1758
1759    edje_freeze();
1760
1761 #ifdef E_RELEASE_BUILD
1762    if (first_idle)
1763      {
1764         TS("SLEEP");
1765         first_idle = 0;
1766         e_precache_end = EINA_TRUE;
1767      }
1768 #else
1769    if (first_idle++ < 60)
1770      {
1771         TS("SLEEP");
1772         if (!first_idle)
1773           e_precache_end = EINA_TRUE;
1774      }
1775 #endif
1776
1777    return ECORE_CALLBACK_RENEW;
1778 }
1779
1780 static Eina_Bool
1781 _e_main_cb_x_flusher(void *data __UNUSED__)
1782 {
1783    eet_clearcache();
1784 #ifndef HAVE_WAYLAND_ONLY
1785    if (e_comp->comp_type == E_PIXMAP_TYPE_X)
1786      ecore_x_flush();
1787 #endif
1788    return ECORE_CALLBACK_RENEW;
1789 }
1790
1791 static Eina_Bool
1792 _e_main_cb_startup_fake_end(void *data __UNUSED__)
1793 {
1794    e_init_hide();
1795    return ECORE_CALLBACK_CANCEL;
1796 }