e_main: remove deprecated API - ecore_evas_app_comp_sync_set
[platform/upstream/enlightenment.git] / src / bin / e_main.c
1 #include "e.h"
2 #ifdef __linux__
3 # include <sys/prctl.h>
4 #endif
5 #ifdef HAVE_SYSTEMD
6 # include <systemd/sd-daemon.h>
7 #endif
8
9 #define MAX_LEVEL 80
10
11 #define TS_DO
12 #ifdef TS_DO
13 # define TS(x)                                                    \
14   {                                                               \
15      t1 = ecore_time_unix_get();                                  \
16      printf("ESTART: %1.5f [%1.5f] - %s\n", t1 - t0, t1 - t2, x); \
17      t2 = t1;                                                     \
18   }
19
20 # define TSB(x)                                                  \
21   do {                                                           \
22      TRACE_DS_BEGIN(ESTART: %s, x);                              \
23      TS(x);                                                      \
24   } while (0)
25 # define TSE(x)                                                  \
26   do {                                                           \
27      TRACE_DS_END();                                             \
28      TS(x);                                                      \
29   } while (0)
30 # define TSM(x)                                                  \
31   do {                                                           \
32      TRACE_DS_MARK(ESTART: %s, x);                               \
33      TS(x);                                                      \
34   } while (0)
35 static double t0, t1, t2;
36 #else
37 # define TS(x)
38 # define TSB(x)
39 # define TSE(x)
40 # define TSM(x)
41 #endif
42 /*
43  * i need to make more use of these when i'm baffled as to when something is
44  * up. other hooks:
45  *
46  *      void *(*__malloc_hook)(size_t size, const void *caller);
47  *
48  *      void *(*__realloc_hook)(void *ptr, size_t size, const void *caller);
49  *
50  *      void *(*__memalign_hook)(size_t alignment, size_t size,
51  *                               const void *caller);
52  *
53  *      void (*__free_hook)(void *ptr, const void *caller);
54  *
55  *      void (*__malloc_initialize_hook)(void);
56  *
57  *      void (*__after_morecore_hook)(void);
58  *
59
60    static void my_init_hook(void);
61    static void my_free_hook(void *p, const void *caller);
62
63    static void (*old_free_hook)(void *ptr, const void *caller) = NULL;
64    void (*__free_hook)(void *ptr, const void *caller);
65
66    void (*__malloc_initialize_hook) (void) = my_init_hook;
67    static void
68    my_init_hook(void)
69    {
70    old_free_hook = __free_hook;
71    __free_hook = my_free_hook;
72    }
73
74    //void *magicfree = NULL;
75
76    static void
77    my_free_hook(void *p, const void *caller)
78    {
79    __free_hook = old_free_hook;
80    //   if ((p) && (p == magicfree))
81    //     {
82    //   printf("CAUGHT!!!!! %p ...\n", p);
83    //   abort();
84    //     }
85    free(p);
86    __free_hook = my_free_hook;
87    }
88  */
89
90 /* local function prototypes */
91 static void      _e_main_shutdown(int errcode);
92 static void      _e_main_shutdown_push(int (*func)(void));
93 static void      _e_main_parse_arguments(int argc, char **argv);
94 static Eina_Bool _e_main_cb_signal_exit(void *data EINA_UNUSED, int ev_type EINA_UNUSED, void *ev EINA_UNUSED);
95 static Eina_Bool _e_main_cb_signal_hup(void *data EINA_UNUSED, int ev_type EINA_UNUSED, void *ev EINA_UNUSED);
96 static int       _e_main_dirs_init(void);
97 static int       _e_main_dirs_shutdown(void);
98 static int       _e_main_path_init(void);
99 static int       _e_main_path_shutdown(void);
100 static int       _e_main_screens_init(void);
101 static int       _e_main_screens_shutdown(void);
102 static void      _e_main_desk_save(void);
103 static void      _e_main_desk_restore(void);
104 static Eina_Bool _e_main_cb_idle_before(void *data EINA_UNUSED);
105 static Eina_Bool _e_main_cb_idle_after(void *data EINA_UNUSED);
106 static void      _e_main_create_wm_ready(void);
107 static void      _e_main_hooks_clean(void);
108 static void      _e_main_hook_call(E_Main_Hook_Point hookpoint, void *data EINA_UNUSED);
109
110 /* local variables */
111 static int _e_main_lvl = 0;
112 static int(*_e_main_shutdown_func[MAX_LEVEL]) (void);
113
114 static Ecore_Idle_Enterer *_idle_before = NULL;
115 static Ecore_Idle_Enterer *_idle_after = NULL;
116
117 static Eina_List *hooks = NULL;
118
119 static int _e_main_hooks_delete = 0;
120 static int _e_main_hooks_walking = 0;
121
122 static Eina_Inlist *_e_main_hooks[] =
123 {
124    [E_MAIN_HOOK_MODULE_LOAD_DONE] = NULL,
125    [E_MAIN_HOOK_E_INFO_READY] = NULL
126 };
127
128 /* external variables */
129 E_API Eina_Bool starting = EINA_TRUE;
130 E_API Eina_Bool stopping = EINA_FALSE;
131
132 static Eina_Bool
133 _xdg_check_str(const char *env, const char *str)
134 {
135    const char *p;
136    size_t len;
137
138    len = strlen(str);
139    for (p = strstr(env, str); p; p++, p = strstr(p, str))
140      {
141         if ((!p[len]) || (p[len] == ':')) return EINA_TRUE;
142      }
143    return EINA_FALSE;
144 }
145
146 static void
147 _xdg_data_dirs_augment(void)
148 {
149    char *s;
150    const char *p = e_prefix_get();
151    char newpath[PATH_MAX], buf[PATH_MAX + PATH_MAX + 200];
152
153    if (!p) return;
154
155    s = e_util_env_get("XDG_DATA_DIRS");
156    if (s)
157      {
158         Eina_Bool pfxdata, pfx;
159
160         pfxdata = !_xdg_check_str(s, e_prefix_data_get());
161         snprintf(newpath, sizeof(newpath), "%s/share", p);
162         pfx = !_xdg_check_str(s, newpath);
163         if (pfxdata || pfx)
164           {
165              snprintf(buf, sizeof(buf), "%s%s%s%s%s",
166                pfxdata ? e_prefix_data_get() : "",
167                pfxdata ? ":" : "",
168                pfx ? newpath : "",
169                pfx ? ":" : "",
170                s);
171              e_util_env_set("XDG_DATA_DIRS", buf);
172           }
173         E_FREE(s);
174      }
175    else
176      {
177         snprintf(buf, sizeof(buf), "%s:%s/share:/usr/local/share:/usr/share", e_prefix_data_get(), p);
178         e_util_env_set("XDG_DATA_DIRS", buf);
179      }
180
181    s = e_util_env_get("XDG_CONFIG_DIRS");
182    snprintf(newpath, sizeof(newpath), "%s/etc/xdg", p);
183    if (s)
184      {
185         if (!_xdg_check_str(s, newpath))
186           {
187              snprintf(buf, sizeof(buf), "%s:%s", newpath, s);
188              e_util_env_set("XDG_CONFIG_DIRS", buf);
189           }
190         E_FREE(s);
191      }
192    else
193      {
194         snprintf(buf, sizeof(buf), "%s:/etc/xdg", newpath);
195         e_util_env_set("XDG_CONFIG_DIRS", buf);
196      }
197
198    s = e_util_env_get("XDG_RUNTIME_DIR");
199    if (s)
200      E_FREE(s);
201    else
202      {
203         const char *dir;
204
205         snprintf(buf, sizeof(buf), "/tmp/xdg-XXXXXX");
206         dir = mkdtemp(buf);
207         if (!dir) dir = "/tmp";
208         else
209           {
210              e_util_env_set("XDG_RUNTIME_DIR", dir);
211              snprintf(buf, sizeof(buf), "%s/.e-deleteme", dir);
212              ecore_file_mkdir(buf);
213           }
214      }
215
216    /* set menu prefix so we get our e menu */
217    s = e_util_env_get("XDG_MENU_PREFIX");
218    if (s)
219      E_FREE(s);
220    else
221      e_util_env_set("XDG_MENU_PREFIX", "e-");
222 }
223
224 static Eina_Bool
225 _e_main_subsystem_defer(void *data EINA_UNUSED)
226 {
227    TRACE_DS_BEGIN(MAIN:SUBSYSTEMS DEFER);
228
229    /* try to init delayed subsystems */
230
231    TRACE_DS_BEGIN(MAIN:DEFERRED INTERNAL SUBSYSTEMS INIT);
232
233    TSB("[DEFERRED] DPMS Init");
234    if (!e_dpms_init())
235      {
236         e_error_message_show(_("Enlightenment cannot set up dpms.\n"));
237         goto failed;
238      }
239    TSE("[DEFERRED] DPMS Init Done");
240    _e_main_shutdown_push(e_dpms_shutdown);
241
242    TSB("[DEFERRED] Screens Init: win");
243    if (!e_win_init())
244      {
245         e_error_message_show(_("Enlightenment cannot setup elementary trap!\n"));
246         goto failed;
247      }
248    TSE("[DEFERRED] Screens Init: win Done");
249
250    TSB("[DEFERRED] E_Dnd Init");
251    if (!e_dnd_init())
252      {
253         e_error_message_show(_("Enlightenment cannot set up its dnd system.\n"));
254         goto failed;
255      }
256    TSE("[DEFERRED] E_Dnd Init Done");
257    _e_main_shutdown_push(e_dnd_shutdown);
258
259    TSB("[DEFERRED] E_Scale Init");
260    if (!e_scale_init())
261      {
262         e_error_message_show(_("Enlightenment cannot set up its scale system.\n"));
263         goto failed;
264      }
265    TSE("[DEFERRED] E_Scale Init Done");
266    _e_main_shutdown_push(e_scale_shutdown);
267
268    TSB("[DEFERRED] E_Test_Helper Init");
269    e_test_helper_init();
270    _e_main_shutdown_push(e_test_helper_shutdown);
271    TSE("[DEFERRED] E_Test_Helper Done");
272
273    TSB("[DEFERRED] E_INFO_SERVER Init");
274    e_info_server_init();
275    _e_main_shutdown_push(e_info_server_shutdown);
276    TSE("[DEFERRED] E_INFO_SERVER Done");
277
278    TRACE_DS_END();
279    TRACE_DS_BEGIN(MAIN:DEFERRED COMP JOB);
280
281    /* try to do deferred job of any subsystems*/
282    TSB("[DEFERRED] Compositor's deferred job");
283    e_comp_deferred_job();
284    TSE("[DEFERRED] Compositor's deferred job Done");
285
286    if (e_config->use_e_policy)
287      {
288         TSB("[DEFERRED] E_Policy's deferred job");
289         e_policy_deferred_job();
290         TSE("[DEFERRED] E_Policy's deferred job Done");
291      }
292
293    TSB("[DEFERRED] E_Module's deferred job");
294    e_module_deferred_job();
295    TSE("[DEFERRED] E_Module's deferred job Done");
296
297    TRACE_DS_END();
298    TRACE_DS_END();
299    return ECORE_CALLBACK_DONE;
300
301 failed:
302    TSE("INIT FAILED");
303    TRACE_DS_END();
304    TRACE_DS_END();
305    _e_main_shutdown(-1);
306    return ECORE_CALLBACK_DONE;
307 }
308
309 static Eina_Bool
310 _e_main_deferred_job_schedule(void *d EINA_UNUSED, int type EINA_UNUSED, void *ev EINA_UNUSED)
311 {
312    PRCTL("[Winsys] all modules loaded");
313    ecore_idler_add(_e_main_subsystem_defer, NULL);
314    return ECORE_CALLBACK_DONE;
315 }
316
317 /* externally accessible functions */
318 int
319 main(int argc, char **argv)
320 {
321    char *s = NULL;
322    struct sigaction action;
323
324 #ifdef __linux__
325 # ifdef PR_SET_PTRACER
326 #  ifdef PR_SET_PTRACER_ANY
327    prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY);
328 #  endif
329 # endif
330 #endif
331
332    /* for debugging by redirecting stdout & stderr of e to a log file to tail */
333    setvbuf(stdout, NULL, _IONBF, 0);
334    setvbuf(stderr, NULL, _IONBF, 0);
335
336 #ifdef TS_DO
337    t0 = t1 = t2 = ecore_time_unix_get();
338    printf("ESTART(main) %1.5f\n", t0);
339 #endif
340    TRACE_DS_BEGIN(MAIN:BEGIN STARTUP);
341    TSB("Begin Startup");
342    PRCTL("[Winsys] start of main");
343
344    /* trap deadly bug signals and allow some form of sane recovery */
345    /* or ability to gdb attach and debug at this point - better than your */
346    /* wm/desktop vanishing and not knowing what happened */
347
348    /* don't install SIGBUS handler */
349    /* Wayland shm sets up a sigbus handler for catching invalid shm region */
350    /* access. If we setup our sigbus handler here, then the wl-shm sigbus */
351    /* handler will not function properly */
352    s = e_util_env_get("NOTIFY_SOCKET");
353    if (s)
354      E_FREE(s);
355    else
356      {
357         TSB("Signal Trap");
358         action.sa_sigaction = e_sigseg_act;
359         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
360         sigemptyset(&action.sa_mask);
361         sigaction(SIGSEGV, &action, NULL);
362
363         action.sa_sigaction = e_sigill_act;
364         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
365         sigemptyset(&action.sa_mask);
366         sigaction(SIGILL, &action, NULL);
367
368         action.sa_sigaction = e_sigfpe_act;
369         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
370         sigemptyset(&action.sa_mask);
371         sigaction(SIGFPE, &action, NULL);
372
373         action.sa_sigaction = e_sigabrt_act;
374         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
375         sigemptyset(&action.sa_mask);
376         sigaction(SIGABRT, &action, NULL);
377         TSE("Signal Trap Done");
378      }
379
380    TSB("Eina Init");
381    if (!eina_init())
382      {
383         e_error_message_show(_("Enlightenment cannot initialize Eina!\n"));
384         goto failed;
385      }
386    TSE("Eina Init Done");
387    _e_main_shutdown_push(eina_shutdown);
388
389 #ifdef OBJECT_HASH_CHECK
390    TSB("E_Object Hash Init");
391    e_object_hash_init();
392    TSE("E_Object Hash Init Done");
393 #endif
394
395    TSB("E_Log Init");
396    if (!e_log_init())
397      {
398         e_error_message_show(_("Enlightenment could not create a logging domain!\n"));
399         goto failed;
400      }
401    TSE("E_Log Init Done");
402    _e_main_shutdown_push(e_log_shutdown);
403
404    TSB("Determine Prefix");
405    if (!e_prefix_determine(argv[0]))
406      {
407         fprintf(stderr,
408                 "ERROR: Enlightenment cannot determine it's installed\n"
409                 "       prefix from the system or argv[0].\n"
410                 "       This is because it is not on Linux AND has been\n"
411                 "       executed strangely. This is unusual.\n");
412      }
413    TSE("Determine Prefix Done");
414
415    TSB("Parse Arguments");
416    _e_main_parse_arguments(argc, argv);
417    TSE("Parse Arguments Done");
418
419    /*** Initialize Core EFL Libraries We Need ***/
420
421    TSB("Eet Init");
422    if (!eet_init())
423      {
424         e_error_message_show(_("Enlightenment cannot initialize Eet!\n"));
425         goto failed;
426      }
427    TSE("Eet Init Done");
428    _e_main_shutdown_push(eet_shutdown);
429
430    /* Allow ecore to not load system modules.
431     * Without it ecore_init will block until dbus authentication
432     * and registration are complete.
433     */
434    ecore_app_no_system_modules();
435
436    TSB("Ecore Init");
437    if (!ecore_init())
438      {
439         e_error_message_show(_("Enlightenment cannot initialize Ecore!\n"));
440         goto failed;
441      }
442    TSE("Ecore Init Done");
443    _e_main_shutdown_push(ecore_shutdown);
444
445    TSB("EIO Init");
446    if (!eio_init())
447      {
448         e_error_message_show(_("Enlightenment cannot initialize EIO!\n"));
449         goto failed;
450      }
451    TSE("EIO Init Done");
452    _e_main_shutdown_push(eio_shutdown);
453
454    TSB("Ecore Event Handlers");
455    if (!ecore_event_handler_add(ECORE_EVENT_SIGNAL_EXIT,
456                                 _e_main_cb_signal_exit, NULL))
457      {
458         e_error_message_show(_("Enlightenment cannot set up an exit signal handler.\n"
459                                "Perhaps you are out of memory?"));
460         goto failed;
461      }
462    if (!ecore_event_handler_add(ECORE_EVENT_SIGNAL_HUP,
463                                 _e_main_cb_signal_hup, NULL))
464      {
465         e_error_message_show(_("Enlightenment cannot set up a HUP signal handler.\n"
466                                "Perhaps you are out of memory?"));
467         goto failed;
468      }
469    TSE("Ecore Event Handlers Done");
470
471    TSB("Ecore_File Init");
472    if (!ecore_file_init())
473      {
474         e_error_message_show(_("Enlightenment cannot initialize Ecore_File!\n"));
475         goto failed;
476      }
477    TSE("Ecore_File Init Done");
478    _e_main_shutdown_push(ecore_file_shutdown);
479
480    TSB("E_Util_File_Monitor Init");
481    e_util_file_monitor_init();
482    TSE("E_Util_File_Monitor Init Done");
483    _e_main_shutdown_push(e_util_file_monitor_shutdown);
484
485    _idle_before = ecore_idle_enterer_before_add(_e_main_cb_idle_before, NULL);
486
487    TSB("XDG_DATA_DIRS Init");
488    _xdg_data_dirs_augment();
489    TSE("XDG_DATA_DIRS Init Done");
490
491    TSB("Ecore_Evas Init");
492    if (!ecore_evas_init())
493      {
494         e_error_message_show(_("Enlightenment cannot initialize Ecore_Evas!\n"));
495         goto failed;
496      }
497    TSE("Ecore_Evas Init Done");
498
499    TSB("Edje Init");
500    if (!edje_init())
501      {
502         e_error_message_show(_("Enlightenment cannot initialize Edje!\n"));
503         goto failed;
504      }
505    TSE("Edje Init Done");
506    _e_main_shutdown_push(edje_shutdown);
507
508    /*** Initialize E Subsystems We Need ***/
509
510    TSB("E User Init");
511    if (!e_user_init())
512      {
513         e_error_message_show(_("Enlightenment cannot set up user home path\n"));
514         goto failed;
515      }
516    TSE("E User Init Done");
517    _e_main_shutdown_push(e_user_shutdown);
518
519    TSB("E Directories Init");
520    /* setup directories we will be using for configurations storage etc. */
521    if (!_e_main_dirs_init())
522      {
523         e_error_message_show(_("Enlightenment cannot create directories in your home directory.\n"
524                                "Perhaps you have no home directory or the disk is full?"));
525         goto failed;
526      }
527    TSE("E Directories Init Done");
528    _e_main_shutdown_push(_e_main_dirs_shutdown);
529
530    TSB("E_Config Init");
531    if (!e_config_init())
532      {
533         e_error_message_show(_("Enlightenment cannot set up its config system.\n"));
534         goto failed;
535      }
536    TSE("E_Config Init Done");
537    _e_main_shutdown_push(e_config_shutdown);
538
539    TSB("E_Env Init");
540    if (!e_env_init())
541      {
542         e_error_message_show(_("Enlightenment cannot set up its environment.\n"));
543         goto failed;
544      }
545    TSE("E_Env Init Done");
546    _e_main_shutdown_push(e_env_shutdown);
547
548    ecore_exe_run_priority_set(e_config->priority);
549
550    TSB("E Paths Init");
551    if (!_e_main_path_init())
552      {
553         e_error_message_show(_("Enlightenment cannot set up paths for finding files.\n"
554                                "Perhaps you are out of memory?"));
555         goto failed;
556      }
557    TSE("E Paths Init Done");
558    _e_main_shutdown_push(_e_main_path_shutdown);
559
560    ecore_animator_frametime_set(1.0 / e_config->framerate);
561
562    TSB("E_Theme Init");
563    if (!e_theme_init())
564      {
565         e_error_message_show(_("Enlightenment cannot set up its theme system.\n"));
566         goto failed;
567      }
568    TSE("E_Theme Init Done");
569    _e_main_shutdown_push(e_theme_shutdown);
570
571    TSB("E_Actions Init");
572    if (!e_actions_init())
573      {
574         e_error_message_show(_("Enlightenment cannot set up its actions system.\n"));
575         goto failed;
576      }
577    TSE("E_Actions Init Done");
578    _e_main_shutdown_push(e_actions_shutdown);
579
580    /* these just add event handlers and can't fail
581     * timestamping them is dumb.
582     */
583    e_screensaver_preinit();
584    e_zone_init();
585    e_desk_init();
586    e_slot_init();
587    e_magnifier_init();
588
589    TRACE_DS_BEGIN(MAIN:WAIT /dev/dri/card0);
590    if (e_config->sleep_for_dri)
591      {
592         while(access("/dev/dri/card0", F_OK) != 0)
593           {
594              struct timespec req, rem;
595              req.tv_sec = 0;
596              req.tv_nsec = 50000000L;
597              nanosleep(&req, &rem);
598           }
599      }
600    TRACE_DS_END();
601
602    TSB("E_Msg Init");
603    if (!e_msg_init())
604      {
605         e_error_message_show(_("Enlightenment cannot set up its msg system."));
606         goto failed;
607      }
608    TSE("E_Msg Init Done");
609    _e_main_shutdown_push(e_msg_shutdown);
610
611    e_module_event_init();
612
613    TSB("E_Pointer Init");
614    if (!e_pointer_init())
615      {
616         e_error_message_show(_("Enlightenment cannot set up its pointer system.\n"));
617         goto failed;
618      }
619    TSE("E_Pointer Init Done");
620    _e_main_shutdown_push(e_pointer_shutdown);
621
622    TRACE_DS_BEGIN(MAIN:SCREEN INIT);
623    TSB("Screens Init");
624    if (!_e_main_screens_init())
625      {
626         e_error_message_show(_("Enlightenment set up window management for all the screens on your system\n"
627                                "failed. Perhaps another window manager is running?\n"));
628         goto failed;
629      }
630    TSE("Screens Init Done");
631    _e_main_shutdown_push(_e_main_screens_shutdown);
632    TRACE_DS_END();
633
634    TSB("E_Devicemgr Init");
635    if (!e_devicemgr_init())
636      {
637         e_error_message_show(_("Enlightenment cannot set up its device_manager system.\n"));
638         goto failed;
639      }
640    TSE("E_Devicemgr Init Done");
641    _e_main_shutdown_push(e_devicemgr_shutdown);
642
643    TSB("E_Keyrouter Init");
644    if (!e_keyrouter_init())
645      {
646         e_error_message_show(_("Enlightenment cannot set up its keyrouting system.\n"));
647         goto failed;
648      }
649    TSE("E_Keyrouter Init Done");
650    _e_main_shutdown_push(e_keyrouter_shutdown);
651
652    if (e_config->eom_enable)
653      {
654         TSB("Eom Init");
655         if (!e_eom_init())
656           {
657              e_error_message_show(_("Enlightenment cannot set up eom.\n"));
658              goto failed;
659           }
660         TSE("Eom Init Done");
661         _e_main_shutdown_push(e_eom_shutdown);
662      }
663
664    TSB("E_Screensaver Init");
665    if (!e_screensaver_init())
666      {
667         e_error_message_show(_("Enlightenment cannot configure the X screensaver.\n"));
668         goto failed;
669      }
670    TSE("E_Screensaver Init Done");
671    _e_main_shutdown_push(e_screensaver_shutdown);
672
673    TSB("E_Comp Freeze");
674    e_comp_all_freeze();
675    TSE("E_Comp Freeze Done");
676
677    TSB("E_Grabinput Init");
678    if (!e_grabinput_init())
679      {
680         e_error_message_show(_("Enlightenment cannot set up its grab input handling system.\n"));
681         goto failed;
682      }
683    TSE("E_Grabinput Init Done");
684    _e_main_shutdown_push(e_grabinput_shutdown);
685
686    TS("E_Gesture Init");
687    e_gesture_init();
688    _e_main_shutdown_push(e_gesture_shutdown);
689
690    ecore_event_handler_add(E_EVENT_MODULE_INIT_END, _e_main_deferred_job_schedule, NULL);
691
692    TSB("E_Module Init");
693    if (!e_module_init())
694      {
695         e_error_message_show(_("Enlightenment cannot set up its module system.\n"));
696         goto failed;
697      }
698    TSE("E_Module Init Done");
699    _e_main_shutdown_push(e_module_shutdown);
700
701    TSB("E_Mouse Init");
702    if (!e_mouse_update())
703      {
704         e_error_message_show(_("Enlightenment cannot configure the mouse settings.\n"));
705         goto failed;
706      }
707    TSE("E_Mouse Init Done");
708
709    TSB("E_Icon Init");
710    if (!e_icon_init())
711      {
712         e_error_message_show(_("Enlightenment cannot initialize the Icon Cache system.\n"));
713         goto failed;
714      }
715    TSE("E_Icon Init Done");
716    _e_main_shutdown_push(e_icon_shutdown);
717
718    if (e_config->use_e_policy)
719      {
720         TSB("E_Policy Init");
721         if (!e_policy_init())
722           {
723              e_error_message_show(_("Enlightenment cannot setup policy system!\n"));
724              goto failed;
725           }
726         TSE("E_Policy Init Done");
727         _e_main_shutdown_push(e_policy_shutdown);
728      }
729
730    TSB("E_Process Init");
731    if (!e_process_init())
732      {
733         e_error_message_show(_("Enlightenment cannot setup process managing system!\n"));
734         goto failed;
735      }
736    TSE("E_Process Init Done");
737    _e_main_shutdown_push(e_process_shutdown);
738
739    TSB("E_Security Init");
740    if (!e_security_init())
741      {
742         e_error_message_show(_("Enlightenment cannot setup security system!\n"));
743         goto failed;
744      }
745    TSE("E_Security Init Done");
746    _e_main_shutdown_push(e_security_shutdown);
747
748    TSB("Load Modules");
749    e_module_all_load();
750    TSE("Load Modules Done");
751
752    TSB("E_Comp Thaw");
753    e_comp_all_thaw();
754    TSE("E_Comp Thaw Done");
755
756    if (e_config->use_thread_max_cpu)
757      {
758         int cpus;
759
760         cpus = eina_cpu_count();
761         if (cpus >= 1)
762           {
763              TSM("Set ecore thread max");
764              ecore_thread_max_set(cpus);
765           }
766      }
767
768    _idle_after = ecore_idle_enterer_add(_e_main_cb_idle_after, NULL);
769
770    starting = EINA_FALSE;
771
772    TSM("MAIN LOOP AT LAST");
773
774    if (e_config->create_wm_ready)
775      _e_main_create_wm_ready();
776
777    TRACE_DS_END();
778
779 #ifdef HAVE_SYSTEMD
780    TSM("[WM] Send start-up completion");
781    sd_notify(0, "READY=1");
782 #else
783    TSM("[WM] Skip sending start-up completion. (no systemd)");
784 #endif
785    ecore_main_loop_begin();
786
787    ELOGF("COMP", "STOPPING enlightenment...", NULL);
788    stopping = EINA_TRUE;
789
790    _e_main_desk_save();
791    e_comp_internal_save();
792
793    _e_main_shutdown(0);
794
795    e_prefix_shutdown();
796
797    return 0;
798
799 failed:
800    TSE("INIT FAILED");
801    TRACE_DS_END();
802    _e_main_shutdown(-1);
803 }
804
805 E_API double
806 e_main_ts(const char *str)
807 {
808    double ret;
809    t1 = ecore_time_unix_get();
810    printf("ESTART: %1.5f [%1.5f] - %s\n", t1 - t0, t1 - t2, str);
811    ret = t1 - t2;
812    t2 = t1;
813    return ret;
814 }
815
816 E_API double
817 e_main_ts_begin(const char *str)
818 {
819    TRACE_DS_BEGIN(ESTART: %s, str);
820    return e_main_ts(str);
821 }
822
823 E_API double
824 e_main_ts_end(const char *str)
825 {
826    TRACE_DS_END();
827    return e_main_ts(str);
828 }
829
830 /* local functions */
831 static void
832 _e_main_shutdown(int errcode)
833 {
834    int i = 0;
835    char buf[PATH_MAX];
836    char *dir;
837
838    printf("E: Begin Shutdown Procedure!\n");
839
840    E_FREE_LIST(hooks, e_main_hook_del);
841
842    if (_idle_before) ecore_idle_enterer_del(_idle_before);
843    _idle_before = NULL;
844    if (_idle_after) ecore_idle_enterer_del(_idle_after);
845    _idle_after = NULL;
846
847    dir = e_util_env_get("XDG_RUNTIME_DIR");
848    if (dir)
849      {
850         char buf_env[PATH_MAX - 12];
851         snprintf(buf_env, sizeof(buf_env), "%s", dir);
852         snprintf(buf, sizeof(buf), "%s/.e-deleteme", buf_env);
853         if (ecore_file_exists(buf)) ecore_file_recursive_rm(buf_env);
854         E_FREE(dir);
855      }
856    for (i = (_e_main_lvl - 1); i >= 0; i--)
857      (*_e_main_shutdown_func[i])();
858 #ifdef OBJECT_HASH_CHECK
859    e_object_hash_shutdown();
860 #endif
861    if (errcode < 0) exit(errcode);
862 }
863
864 static void
865 _e_main_shutdown_push(int (*func)(void))
866 {
867    _e_main_lvl++;
868    if (_e_main_lvl > MAX_LEVEL)
869      {
870         _e_main_lvl--;
871         e_error_message_show("WARNING: too many init levels. MAX = %i\n",
872                              MAX_LEVEL);
873         return;
874      }
875    _e_main_shutdown_func[_e_main_lvl - 1] = func;
876 }
877
878 static void
879 _e_main_parse_arguments(int argc, char **argv)
880 {
881    int i = 0;
882
883    /* handle some command-line parameters */
884    for (i = 1; i < argc; i++)
885      {
886         if ((!strcmp(argv[i], "-profile")) && (i < (argc - 1)))
887           {
888              i++;
889              if (!getenv("E_CONF_PROFILE"))
890                e_util_env_set("E_CONF_PROFILE", argv[i]);
891           }
892         else if ((!strcmp(argv[i], "-version")) ||
893                  (!strcmp(argv[i], "--version")))
894           {
895              printf(_("Version: %s\n"), PACKAGE_VERSION);
896              _e_main_shutdown(-1);
897           }
898         else if ((!strcmp(argv[i], "-h")) ||
899                  (!strcmp(argv[i], "-help")) ||
900                  (!strcmp(argv[i], "--help")))
901           {
902              printf
903                (_(
904                  "Options:\n"
905                  "\t-profile CONF_PROFILE\n"
906                  "\t\tUse the configuration profile CONF_PROFILE instead of the user selected default or just \"default\".\n"
907                  "\t-version\n"
908                  )
909                );
910              _e_main_shutdown(-1);
911           }
912      }
913 }
914
915 static Eina_Bool
916 _e_main_cb_signal_exit(void *data EINA_UNUSED, int ev_type EINA_UNUSED, void *ev EINA_UNUSED)
917 {
918    /* called on ctrl-c, kill (pid) (also SIGINT, SIGTERM and SIGQIT) */
919    ecore_main_loop_quit();
920    return ECORE_CALLBACK_RENEW;
921 }
922
923 static Eina_Bool
924 _e_main_cb_signal_hup(void *data EINA_UNUSED, int ev_type EINA_UNUSED, void *ev EINA_UNUSED)
925 {
926    ecore_main_loop_quit();
927    return ECORE_CALLBACK_RENEW;
928 }
929
930 static int
931 _e_main_dirs_init(void)
932 {
933    if(getenv("E_CONF_RO"))
934      {
935         return 1;
936      }
937
938    const char *base;
939    const char *dirs[] =
940    {
941       "backgrounds",
942       "config",
943       "themes",
944       NULL
945    };
946
947    base = e_user_dir_get();
948    if (ecore_file_mksubdirs(base, dirs) != sizeof(dirs) / sizeof(dirs[0]) - 1)
949      {
950         e_error_message_show("Could not create one of the required "
951                              "subdirectories of '%s'\n", base);
952         return 0;
953      }
954
955    return 1;
956 }
957
958 static int
959 _e_main_dirs_shutdown(void)
960 {
961    return 1;
962 }
963
964 static int
965 _e_main_path_init(void)
966 {
967    char buf[PATH_MAX];
968
969    /* setup data paths */
970    path_data = e_path_new();
971    if (!path_data)
972      {
973         e_error_message_show("Cannot allocate path for path_data\n");
974         return 0;
975      }
976    e_prefix_data_concat_static(buf, "data");
977    e_path_default_path_append(path_data, buf);
978
979    /* setup image paths */
980    path_images = e_path_new();
981    if (!path_images)
982      {
983         e_error_message_show("Cannot allocate path for path_images\n");
984         return 0;
985      }
986    e_user_dir_concat_static(buf, "/images");
987    e_path_default_path_append(path_images, buf);
988    e_prefix_data_concat_static(buf, "data/images");
989    e_path_default_path_append(path_images, buf);
990
991    /* setup font paths */
992    path_fonts = e_path_new();
993    if (!path_fonts)
994      {
995         e_error_message_show("Cannot allocate path for path_fonts\n");
996         return 0;
997      }
998    e_user_dir_concat_static(buf, "/fonts");
999    e_path_default_path_append(path_fonts, buf);
1000    e_prefix_data_concat_static(buf, "data/fonts");
1001    e_path_default_path_append(path_fonts, buf);
1002
1003    /* setup icon paths */
1004    path_icons = e_path_new();
1005    if (!path_icons)
1006      {
1007         e_error_message_show("Cannot allocate path for path_icons\n");
1008         return 0;
1009      }
1010    e_user_dir_concat_static(buf, "/icons");
1011    e_path_default_path_append(path_icons, buf);
1012    e_prefix_data_concat_static(buf, "data/icons");
1013    e_path_default_path_append(path_icons, buf);
1014
1015    /* setup module paths */
1016    path_modules = e_path_new();
1017    if (!path_modules)
1018      {
1019         e_error_message_show("Cannot allocate path for path_modules\n");
1020         return 0;
1021      }
1022    e_user_dir_concat_static(buf, "/modules");
1023    e_path_default_path_append(path_modules, buf);
1024    snprintf(buf, sizeof(buf), "%s/enlightenment/modules", e_prefix_lib_get());
1025    e_path_default_path_append(path_modules, buf);
1026    /* FIXME: eventually this has to go - moduels should have installers that
1027     * add appropriate install paths (if not installed to user homedir) to
1028     * e's module search dirs
1029     */
1030    snprintf(buf, sizeof(buf), "%s/enlightenment/modules_extra", e_prefix_lib_get());
1031    e_path_default_path_append(path_modules, buf);
1032
1033    /* setup background paths */
1034    path_backgrounds = e_path_new();
1035    if (!path_backgrounds)
1036      {
1037         e_error_message_show("Cannot allocate path for path_backgrounds\n");
1038         return 0;
1039      }
1040    e_user_dir_concat_static(buf, "/backgrounds");
1041    e_path_default_path_append(path_backgrounds, buf);
1042    e_prefix_data_concat_static(buf, "data/backgrounds");
1043    e_path_default_path_append(path_backgrounds, buf);
1044
1045    path_messages = e_path_new();
1046    if (!path_messages)
1047      {
1048         e_error_message_show("Cannot allocate path for path_messages\n");
1049         return 0;
1050      }
1051    e_user_dir_concat_static(buf, "/locale");
1052    e_path_default_path_append(path_messages, buf);
1053    e_path_default_path_append(path_messages, e_prefix_locale_get());
1054
1055    return 1;
1056 }
1057
1058 static int
1059 _e_main_path_shutdown(void)
1060 {
1061    if (path_data)
1062      {
1063         e_object_del(E_OBJECT(path_data));
1064         path_data = NULL;
1065      }
1066    if (path_images)
1067      {
1068         e_object_del(E_OBJECT(path_images));
1069         path_images = NULL;
1070      }
1071    if (path_fonts)
1072      {
1073         e_object_del(E_OBJECT(path_fonts));
1074         path_fonts = NULL;
1075      }
1076    if (path_icons)
1077      {
1078         e_object_del(E_OBJECT(path_icons));
1079         path_icons = NULL;
1080      }
1081    if (path_modules)
1082      {
1083         e_object_del(E_OBJECT(path_modules));
1084         path_modules = NULL;
1085      }
1086    if (path_backgrounds)
1087      {
1088         e_object_del(E_OBJECT(path_backgrounds));
1089         path_backgrounds = NULL;
1090      }
1091    if (path_messages)
1092      {
1093         e_object_del(E_OBJECT(path_messages));
1094         path_messages = NULL;
1095      }
1096    return 1;
1097 }
1098
1099 static int
1100 _e_main_screens_init(void)
1101 {
1102    TSB("\tscreens: client");
1103    if (!e_client_init()) return 0;
1104    TSE("\tscreens: client Done");
1105
1106    TSB("Compositor Init");
1107    PRCTL("[Winsys] start of compositor init");
1108    if (!e_comp_init())
1109      {
1110         e_error_message_show(_("Enlightenment cannot create a compositor.\n"));
1111         _e_main_shutdown(-1);
1112      }
1113    TSE("Compositor Init Done");
1114
1115    PRCTL("[Winsys] end of compositor init");
1116    _e_main_desk_restore();
1117
1118    return 1;
1119 }
1120
1121 static int
1122 _e_main_screens_shutdown(void)
1123 {
1124    e_win_shutdown();
1125    e_comp_shutdown();
1126    e_client_shutdown();
1127
1128    e_magnifier_shutdown();
1129    e_slot_shutdown();
1130    e_desk_shutdown();
1131    e_zone_shutdown();
1132    return 1;
1133 }
1134
1135 static void
1136 _e_main_desk_save(void)
1137 {
1138    const Eina_List *l;
1139    char env[1024], name[1024];
1140    E_Zone *zone;
1141
1142    EINA_LIST_FOREACH(e_comp->zones, l, zone)
1143      {
1144         snprintf(name, sizeof(name), "DESK_%d_%d", 0, zone->num);
1145         snprintf(env, sizeof(env), "%d,%d", zone->desk_x_current, zone->desk_y_current);
1146         e_util_env_set(name, env);
1147      }
1148 }
1149
1150 static void
1151 _e_main_desk_restore(void)
1152 {
1153    E_Client *ec;
1154
1155    E_CLIENT_REVERSE_FOREACH(ec)
1156      if ((!e_client_util_ignored_get(ec)) && e_client_util_desk_visible(ec, e_desk_current_get(ec->zone)))
1157        {
1158           ec->want_focus = ec->take_focus = 1;
1159           break;
1160        }
1161 }
1162
1163 static Eina_Bool
1164 _e_main_cb_idle_before(void *data EINA_UNUSED)
1165 {
1166    e_client_idler_before();
1167    edje_thaw();
1168    return ECORE_CALLBACK_RENEW;
1169 }
1170
1171 static Eina_Bool
1172 _e_main_cb_idle_after(void *data EINA_UNUSED)
1173 {
1174    static int first_idle = 1;
1175
1176    eet_clearcache();
1177    edje_freeze();
1178
1179    if (first_idle)
1180      {
1181         TSM("SLEEP");
1182         first_idle = 0;
1183      }
1184
1185    return ECORE_CALLBACK_RENEW;
1186 }
1187
1188 static void
1189 _e_main_create_wm_ready(void)
1190 {
1191    FILE *_wmready_checker = NULL;
1192    const char *path_wm_ready = "/run/.wm_ready";
1193
1194    if (!e_util_file_realpath_check(path_wm_ready, EINA_TRUE))
1195      {
1196         WRN("%s is maybe link, so delete it\n", path_wm_ready);
1197      }
1198
1199    _wmready_checker = fopen(path_wm_ready, "wb");
1200    if (_wmready_checker)
1201      {
1202         TSM("[WM] WINDOW MANAGER is READY!!!");
1203         PRCTL("[Winsys] WINDOW MANAGER is READY!!!");
1204         fclose(_wmready_checker);
1205
1206         /*TODO: Next lines should be removed. */
1207         FILE *_tmp_wm_ready_checker;
1208
1209         _tmp_wm_ready_checker = fopen(path_wm_ready, "wb");
1210
1211         if (_tmp_wm_ready_checker)
1212           {
1213              TSM("[WM] temporary wm_ready path is created.");
1214              PRCTL("[Winsys] temporary wm_ready path is created.");
1215              fclose(_tmp_wm_ready_checker);
1216           }
1217         else
1218           {
1219              TSM("[WM] temporary wm_ready path create failed.");
1220              PRCTL("[Winsys] temporary wm_ready path create failed.");
1221           }
1222      }
1223    else
1224      {
1225         TSM("[WM] WINDOW MANAGER is READY. BUT, failed to create .wm_ready file.");
1226         PRCTL("[Winsys] WINDOW MANAGER is READY. BUT, failed to create .wm_ready file.");
1227      }
1228 }
1229
1230 static void
1231 _e_main_hooks_clean(void)
1232 {
1233    Eina_Inlist *l;
1234    E_Main_Hook *mh;
1235    unsigned int x;
1236
1237    for (x = 0; x < E_MAIN_HOOK_LAST; x++)
1238      EINA_INLIST_FOREACH_SAFE(_e_main_hooks[x], l, mh)
1239        {
1240           if (!mh->delete_me) continue;
1241           _e_main_hooks[x] = eina_inlist_remove(_e_main_hooks[x],
1242                                                 EINA_INLIST_GET(mh));
1243           free(mh);
1244        }
1245 }
1246
1247 static void
1248 _e_main_hook_call(E_Main_Hook_Point hookpoint, void *data EINA_UNUSED)
1249 {
1250    E_Main_Hook *mh;
1251
1252    _e_main_hooks_walking++;
1253    EINA_INLIST_FOREACH(_e_main_hooks[hookpoint], mh)
1254      {
1255         if (mh->delete_me) continue;
1256         mh->func(mh->data);
1257      }
1258    _e_main_hooks_walking--;
1259    if ((_e_main_hooks_walking == 0) && (_e_main_hooks_delete > 0))
1260      _e_main_hooks_clean();
1261 }
1262
1263 E_API E_Main_Hook *
1264 e_main_hook_add(E_Main_Hook_Point hookpoint, E_Main_Hook_Cb func, const void *data)
1265 {
1266    E_Main_Hook *mh;
1267
1268    EINA_SAFETY_ON_TRUE_RETURN_VAL(hookpoint >= E_MAIN_HOOK_LAST, NULL);
1269    mh = E_NEW(E_Main_Hook, 1);
1270    EINA_SAFETY_ON_NULL_RETURN_VAL(mh, NULL);
1271    mh->hookpoint = hookpoint;
1272    mh->func = func;
1273    mh->data = (void*)data;
1274    _e_main_hooks[hookpoint] = eina_inlist_append(_e_main_hooks[hookpoint],
1275                                                  EINA_INLIST_GET(mh));
1276    return mh;
1277 }
1278
1279 E_API void
1280 e_main_hook_del(E_Main_Hook *mh)
1281 {
1282    mh->delete_me = 1;
1283    if (_e_main_hooks_walking == 0)
1284      {
1285         _e_main_hooks[mh->hookpoint] = eina_inlist_remove(_e_main_hooks[mh->hookpoint],
1286                                                           EINA_INLIST_GET(mh));
1287         free(mh);
1288      }
1289    else
1290      _e_main_hooks_delete++;
1291 }
1292
1293 E_API void
1294 e_main_hook_call(E_Main_Hook_Point hookpoint)
1295 {
1296    if ((hookpoint < 0) || (hookpoint >= E_MAIN_HOOK_LAST)) return;
1297
1298    _e_main_hook_call(hookpoint, NULL);
1299 }