e_log: get rid of unused e_pixmap parameter of ELOG
[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[4096], buf[4096];
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 #ifdef TS_DO
332    t0 = t1 = t2 = ecore_time_unix_get();
333    printf("ESTART(main) %1.5f\n", t0);
334 #endif
335    TRACE_DS_BEGIN(MAIN:BEGIN STARTUP);
336    TSB("Begin Startup");
337    PRCTL("[Winsys] start of main");
338
339    /* trap deadly bug signals and allow some form of sane recovery */
340    /* or ability to gdb attach and debug at this point - better than your */
341    /* wm/desktop vanishing and not knowing what happened */
342
343    /* don't install SIGBUS handler */
344    /* Wayland shm sets up a sigbus handler for catching invalid shm region */
345    /* access. If we setup our sigbus handler here, then the wl-shm sigbus */
346    /* handler will not function properly */
347    s = e_util_env_get("NOTIFY_SOCKET");
348    if (s)
349      E_FREE(s);
350    else
351      {
352         TSB("Signal Trap");
353         action.sa_sigaction = e_sigseg_act;
354         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
355         sigemptyset(&action.sa_mask);
356         sigaction(SIGSEGV, &action, NULL);
357
358         action.sa_sigaction = e_sigill_act;
359         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
360         sigemptyset(&action.sa_mask);
361         sigaction(SIGILL, &action, NULL);
362
363         action.sa_sigaction = e_sigfpe_act;
364         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
365         sigemptyset(&action.sa_mask);
366         sigaction(SIGFPE, &action, NULL);
367
368         action.sa_sigaction = e_sigabrt_act;
369         action.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
370         sigemptyset(&action.sa_mask);
371         sigaction(SIGABRT, &action, NULL);
372         TSE("Signal Trap Done");
373      }
374
375    TSB("Eina Init");
376    if (!eina_init())
377      {
378         e_error_message_show(_("Enlightenment cannot initialize Eina!\n"));
379         goto failed;
380      }
381    TSE("Eina Init Done");
382    _e_main_shutdown_push(eina_shutdown);
383
384 #ifdef OBJECT_HASH_CHECK
385    TSB("E_Object Hash Init");
386    e_object_hash_init();
387    TSE("E_Object Hash Init Done");
388 #endif
389
390    TSB("E_Log Init");
391    if (!e_log_init())
392      {
393         e_error_message_show(_("Enlightenment could not create a logging domain!\n"));
394         goto failed;
395      }
396    TSE("E_Log Init Done");
397    _e_main_shutdown_push(e_log_shutdown);
398
399    TSB("Determine Prefix");
400    if (!e_prefix_determine(argv[0]))
401      {
402         fprintf(stderr,
403                 "ERROR: Enlightenment cannot determine it's installed\n"
404                 "       prefix from the system or argv[0].\n"
405                 "       This is because it is not on Linux AND has been\n"
406                 "       executed strangely. This is unusual.\n");
407      }
408    TSE("Determine Prefix Done");
409
410    /* for debugging by redirecting stdout of e to a log file to tail */
411    setvbuf(stdout, NULL, _IONBF, 0);
412
413    TSB("Parse Arguments");
414    _e_main_parse_arguments(argc, argv);
415    TSE("Parse Arguments Done");
416
417    /*** Initialize Core EFL Libraries We Need ***/
418
419    TSB("Eet Init");
420    if (!eet_init())
421      {
422         e_error_message_show(_("Enlightenment cannot initialize Eet!\n"));
423         goto failed;
424      }
425    TSE("Eet Init Done");
426    _e_main_shutdown_push(eet_shutdown);
427
428    /* Allow ecore to not load system modules.
429     * Without it ecore_init will block until dbus authentication
430     * and registration are complete.
431     */
432    ecore_app_no_system_modules();
433
434    TSB("Ecore Init");
435    if (!ecore_init())
436      {
437         e_error_message_show(_("Enlightenment cannot initialize Ecore!\n"));
438         goto failed;
439      }
440    TSE("Ecore Init Done");
441    _e_main_shutdown_push(ecore_shutdown);
442
443    TSB("EIO Init");
444    if (!eio_init())
445      {
446         e_error_message_show(_("Enlightenment cannot initialize EIO!\n"));
447         goto failed;
448      }
449    TSE("EIO Init Done");
450    _e_main_shutdown_push(eio_shutdown);
451
452    TSB("Ecore Event Handlers");
453    if (!ecore_event_handler_add(ECORE_EVENT_SIGNAL_EXIT,
454                                 _e_main_cb_signal_exit, NULL))
455      {
456         e_error_message_show(_("Enlightenment cannot set up an exit signal handler.\n"
457                                "Perhaps you are out of memory?"));
458         goto failed;
459      }
460    if (!ecore_event_handler_add(ECORE_EVENT_SIGNAL_HUP,
461                                 _e_main_cb_signal_hup, NULL))
462      {
463         e_error_message_show(_("Enlightenment cannot set up a HUP signal handler.\n"
464                                "Perhaps you are out of memory?"));
465         goto failed;
466      }
467    TSE("Ecore Event Handlers Done");
468
469    TSB("Ecore_File Init");
470    if (!ecore_file_init())
471      {
472         e_error_message_show(_("Enlightenment cannot initialize Ecore_File!\n"));
473         goto failed;
474      }
475    TSE("Ecore_File Init Done");
476    _e_main_shutdown_push(ecore_file_shutdown);
477
478    TSB("E_Util_File_Monitor Init");
479    e_util_file_monitor_init();
480    TSE("E_Util_File_Monitor Init Done");
481    _e_main_shutdown_push(e_util_file_monitor_shutdown);
482
483    _idle_before = ecore_idle_enterer_before_add(_e_main_cb_idle_before, NULL);
484
485    TSB("XDG_DATA_DIRS Init");
486    _xdg_data_dirs_augment();
487    TSE("XDG_DATA_DIRS Init Done");
488
489    TSB("Ecore_Evas Init");
490    if (!ecore_evas_init())
491      {
492         e_error_message_show(_("Enlightenment cannot initialize Ecore_Evas!\n"));
493         goto failed;
494      }
495    TSE("Ecore_Evas Init Done");
496
497    /* e doesn't sync to compositor - it should be one */
498    ecore_evas_app_comp_sync_set(0);
499
500    TSB("Edje Init");
501    if (!edje_init())
502      {
503         e_error_message_show(_("Enlightenment cannot initialize Edje!\n"));
504         goto failed;
505      }
506    TSE("Edje Init Done");
507    _e_main_shutdown_push(edje_shutdown);
508
509    /*** Initialize E Subsystems We Need ***/
510
511    TSB("E User Init");
512    if (!e_user_init())
513      {
514         e_error_message_show(_("Enlightenment cannot set up user home path\n"));
515         goto failed;
516      }
517    TSE("E User Init Done");
518    _e_main_shutdown_push(e_user_shutdown);
519
520    TSB("E Directories Init");
521    /* setup directories we will be using for configurations storage etc. */
522    if (!_e_main_dirs_init())
523      {
524         e_error_message_show(_("Enlightenment cannot create directories in your home directory.\n"
525                                "Perhaps you have no home directory or the disk is full?"));
526         goto failed;
527      }
528    TSE("E Directories Init Done");
529    _e_main_shutdown_push(_e_main_dirs_shutdown);
530
531    TSB("E_Config Init");
532    if (!e_config_init())
533      {
534         e_error_message_show(_("Enlightenment cannot set up its config system.\n"));
535         goto failed;
536      }
537    TSE("E_Config Init Done");
538    _e_main_shutdown_push(e_config_shutdown);
539
540    TSB("E_Env Init");
541    if (!e_env_init())
542      {
543         e_error_message_show(_("Enlightenment cannot set up its environment.\n"));
544         goto failed;
545      }
546    TSE("E_Env Init Done");
547    _e_main_shutdown_push(e_env_shutdown);
548
549    ecore_exe_run_priority_set(e_config->priority);
550
551    TSB("E Paths Init");
552    if (!_e_main_path_init())
553      {
554         e_error_message_show(_("Enlightenment cannot set up paths for finding files.\n"
555                                "Perhaps you are out of memory?"));
556         goto failed;
557      }
558    TSE("E Paths Init Done");
559    _e_main_shutdown_push(_e_main_path_shutdown);
560
561    ecore_animator_frametime_set(1.0 / e_config->framerate);
562
563    TSB("E_Theme Init");
564    if (!e_theme_init())
565      {
566         e_error_message_show(_("Enlightenment cannot set up its theme system.\n"));
567         goto failed;
568      }
569    TSE("E_Theme Init Done");
570    _e_main_shutdown_push(e_theme_shutdown);
571
572    TSB("E_Actions Init");
573    if (!e_actions_init())
574      {
575         e_error_message_show(_("Enlightenment cannot set up its actions system.\n"));
576         goto failed;
577      }
578    TSE("E_Actions Init Done");
579    _e_main_shutdown_push(e_actions_shutdown);
580
581    /* these just add event handlers and can't fail
582     * timestamping them is dumb.
583     */
584    e_screensaver_preinit();
585    e_zone_init();
586    e_desk_init();
587    e_slot_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    e_module_event_init();
603
604    TSB("E_Pointer Init");
605    if (!e_pointer_init())
606      {
607         e_error_message_show(_("Enlightenment cannot set up its pointer system.\n"));
608         goto failed;
609      }
610    TSE("E_Pointer Init Done");
611    _e_main_shutdown_push(e_pointer_shutdown);
612
613    TRACE_DS_BEGIN(MAIN:SCREEN INIT);
614    TSB("Screens Init");
615    if (!_e_main_screens_init())
616      {
617         e_error_message_show(_("Enlightenment set up window management for all the screens on your system\n"
618                                "failed. Perhaps another window manager is running?\n"));
619         goto failed;
620      }
621    TSE("Screens Init Done");
622    _e_main_shutdown_push(_e_main_screens_shutdown);
623    TRACE_DS_END();
624
625    TSB("E_Devicemgr Init");
626    if (!e_devicemgr_init())
627      {
628         e_error_message_show(_("Enlightenment cannot set up its device_manager system.\n"));
629         goto failed;
630      }
631    TSE("E_Devicemgr Init Done");
632    _e_main_shutdown_push(e_devicemgr_shutdown);
633
634    TSB("E_Keyrouter Init");
635    if (!e_keyrouter_init())
636      {
637         e_error_message_show(_("Enlightenment cannot set up its keyrouting system.\n"));
638         goto failed;
639      }
640    TSE("E_Keyrouter Init Done");
641    _e_main_shutdown_push(e_keyrouter_shutdown);
642
643    if (e_config->eom_enable)
644      {
645         TSB("Eom Init");
646         if (!e_eom_init())
647           {
648              e_error_message_show(_("Enlightenment cannot set up eom.\n"));
649              goto failed;
650           }
651         TSE("Eom Init Done");
652         _e_main_shutdown_push(e_eom_shutdown);
653      }
654
655    TSB("E_Screensaver Init");
656    if (!e_screensaver_init())
657      {
658         e_error_message_show(_("Enlightenment cannot configure the X screensaver.\n"));
659         goto failed;
660      }
661    TSE("E_Screensaver Init Done");
662    _e_main_shutdown_push(e_screensaver_shutdown);
663
664    TSB("E_Comp Freeze");
665    e_comp_all_freeze();
666    TSE("E_Comp Freeze Done");
667
668    TSB("E_Grabinput Init");
669    if (!e_grabinput_init())
670      {
671         e_error_message_show(_("Enlightenment cannot set up its grab input handling system.\n"));
672         goto failed;
673      }
674    TSE("E_Grabinput Init Done");
675    _e_main_shutdown_push(e_grabinput_shutdown);
676
677    TS("E_Gesture Init");
678    e_gesture_init();
679    _e_main_shutdown_push(e_gesture_shutdown);
680
681    ecore_event_handler_add(E_EVENT_MODULE_INIT_END, _e_main_deferred_job_schedule, NULL);
682
683    TSB("E_Module Init");
684    if (!e_module_init())
685      {
686         e_error_message_show(_("Enlightenment cannot set up its module system.\n"));
687         goto failed;
688      }
689    TSE("E_Module Init Done");
690    _e_main_shutdown_push(e_module_shutdown);
691
692    TSB("E_Mouse Init");
693    if (!e_mouse_update())
694      {
695         e_error_message_show(_("Enlightenment cannot configure the mouse settings.\n"));
696         goto failed;
697      }
698    TSE("E_Mouse Init Done");
699
700    TSB("E_Icon Init");
701    if (!e_icon_init())
702      {
703         e_error_message_show(_("Enlightenment cannot initialize the Icon Cache system.\n"));
704         goto failed;
705      }
706    TSE("E_Icon Init Done");
707    _e_main_shutdown_push(e_icon_shutdown);
708
709    if (e_config->use_e_policy)
710      {
711         TSB("E_Policy Init");
712         if (!e_policy_init())
713           {
714              e_error_message_show(_("Enlightenment cannot setup policy system!\n"));
715              goto failed;
716           }
717         TSE("E_Policy Init Done");
718         _e_main_shutdown_push(e_policy_shutdown);
719      }
720
721    TSB("E_Process Init");
722    if (!e_process_init())
723      {
724         e_error_message_show(_("Enlightenment cannot setup process managing system!\n"));
725         goto failed;
726      }
727    TSE("E_Process Init Done");
728    _e_main_shutdown_push(e_process_shutdown);
729
730    TSB("E_Security Init");
731    if (!e_security_init())
732      {
733         e_error_message_show(_("Enlightenment cannot setup security system!\n"));
734         goto failed;
735      }
736    TSE("E_Security Init Done");
737    _e_main_shutdown_push(e_security_shutdown);
738
739    TSB("Load Modules");
740    e_module_all_load();
741    TSE("Load Modules Done");
742
743    TSB("E_Comp Thaw");
744    e_comp_all_thaw();
745    TSE("E_Comp Thaw Done");
746
747    _idle_after = ecore_idle_enterer_add(_e_main_cb_idle_after, NULL);
748
749    starting = EINA_FALSE;
750
751    TSM("MAIN LOOP AT LAST");
752
753    if (e_config->create_wm_ready)
754      _e_main_create_wm_ready();
755
756    TRACE_DS_END();
757
758 #ifdef HAVE_SYSTEMD
759    TSM("[WM] Send start-up completion");
760    sd_notify(0, "READY=1");
761 #else
762    TSM("[WM] Skip sending start-up completion. (no systemd)");
763 #endif
764    ecore_main_loop_begin();
765
766    ELOGF("COMP", "STOPPING enlightenment...", NULL);
767    stopping = EINA_TRUE;
768
769    _e_main_desk_save();
770    e_comp_internal_save();
771
772    _e_main_shutdown(0);
773
774    e_prefix_shutdown();
775
776    return 0;
777
778 failed:
779    TSE("INIT FAILED");
780    TRACE_DS_END();
781    _e_main_shutdown(-1);
782 }
783
784 E_API double
785 e_main_ts(const char *str)
786 {
787    double ret;
788    t1 = ecore_time_unix_get();
789    printf("ESTART: %1.5f [%1.5f] - %s\n", t1 - t0, t1 - t2, str);
790    ret = t1 - t2;
791    t2 = t1;
792    return ret;
793 }
794
795 E_API double
796 e_main_ts_begin(const char *str)
797 {
798    TRACE_DS_BEGIN(ESTART: %s, str);
799    return e_main_ts(str);
800 }
801
802 E_API double
803 e_main_ts_end(const char *str)
804 {
805    TRACE_DS_END();
806    return e_main_ts(str);
807 }
808
809 /* local functions */
810 static void
811 _e_main_shutdown(int errcode)
812 {
813    int i = 0;
814    char buf[PATH_MAX];
815    char *dir;
816
817    printf("E: Begin Shutdown Procedure!\n");
818
819    E_FREE_LIST(hooks, e_main_hook_del);
820
821    if (_idle_before) ecore_idle_enterer_del(_idle_before);
822    _idle_before = NULL;
823    if (_idle_after) ecore_idle_enterer_del(_idle_after);
824    _idle_after = NULL;
825
826    dir = e_util_env_get("XDG_RUNTIME_DIR");
827    if (dir)
828      {
829         char buf_env[PATH_MAX];
830         snprintf(buf_env, sizeof(buf_env), "%s", dir);
831         snprintf(buf, sizeof(buf), "%s/.e-deleteme", buf_env);
832         if (ecore_file_exists(buf)) ecore_file_recursive_rm(buf_env);
833         E_FREE(dir);
834      }
835    for (i = (_e_main_lvl - 1); i >= 0; i--)
836      (*_e_main_shutdown_func[i])();
837 #ifdef OBJECT_HASH_CHECK
838    e_object_hash_shutdown();
839 #endif
840    if (errcode < 0) exit(errcode);
841 }
842
843 static void
844 _e_main_shutdown_push(int (*func)(void))
845 {
846    _e_main_lvl++;
847    if (_e_main_lvl > MAX_LEVEL)
848      {
849         _e_main_lvl--;
850         e_error_message_show("WARNING: too many init levels. MAX = %i\n",
851                              MAX_LEVEL);
852         return;
853      }
854    _e_main_shutdown_func[_e_main_lvl - 1] = func;
855 }
856
857 static void
858 _e_main_parse_arguments(int argc, char **argv)
859 {
860    int i = 0;
861
862    /* handle some command-line parameters */
863    for (i = 1; i < argc; i++)
864      {
865         if ((!strcmp(argv[i], "-profile")) && (i < (argc - 1)))
866           {
867              i++;
868              if (!getenv("E_CONF_PROFILE"))
869                e_util_env_set("E_CONF_PROFILE", argv[i]);
870           }
871         else if ((!strcmp(argv[i], "-version")) ||
872                  (!strcmp(argv[i], "--version")))
873           {
874              printf(_("Version: %s\n"), PACKAGE_VERSION);
875              _e_main_shutdown(-1);
876           }
877         else if ((!strcmp(argv[i], "-h")) ||
878                  (!strcmp(argv[i], "-help")) ||
879                  (!strcmp(argv[i], "--help")))
880           {
881              printf
882                (_(
883                  "Options:\n"
884                  "\t-profile CONF_PROFILE\n"
885                  "\t\tUse the configuration profile CONF_PROFILE instead of the user selected default or just \"default\".\n"
886                  "\t-version\n"
887                  )
888                );
889              _e_main_shutdown(-1);
890           }
891      }
892 }
893
894 static Eina_Bool
895 _e_main_cb_signal_exit(void *data EINA_UNUSED, int ev_type EINA_UNUSED, void *ev EINA_UNUSED)
896 {
897    /* called on ctrl-c, kill (pid) (also SIGINT, SIGTERM and SIGQIT) */
898    ecore_main_loop_quit();
899    return ECORE_CALLBACK_RENEW;
900 }
901
902 static Eina_Bool
903 _e_main_cb_signal_hup(void *data EINA_UNUSED, int ev_type EINA_UNUSED, void *ev EINA_UNUSED)
904 {
905    ecore_main_loop_quit();
906    return ECORE_CALLBACK_RENEW;
907 }
908
909 static int
910 _e_main_dirs_init(void)
911 {
912    if(getenv("E_CONF_RO"))
913      {
914         return 1;
915      }
916
917    const char *base;
918    const char *dirs[] =
919    {
920       "backgrounds",
921       "config",
922       "themes",
923       NULL
924    };
925
926    base = e_user_dir_get();
927    if (ecore_file_mksubdirs(base, dirs) != sizeof(dirs) / sizeof(dirs[0]) - 1)
928      {
929         e_error_message_show("Could not create one of the required "
930                              "subdirectories of '%s'\n", base);
931         return 0;
932      }
933
934    return 1;
935 }
936
937 static int
938 _e_main_dirs_shutdown(void)
939 {
940    return 1;
941 }
942
943 static int
944 _e_main_path_init(void)
945 {
946    char buf[PATH_MAX];
947
948    /* setup data paths */
949    path_data = e_path_new();
950    if (!path_data)
951      {
952         e_error_message_show("Cannot allocate path for path_data\n");
953         return 0;
954      }
955    e_prefix_data_concat_static(buf, "data");
956    e_path_default_path_append(path_data, buf);
957
958    /* setup image paths */
959    path_images = e_path_new();
960    if (!path_images)
961      {
962         e_error_message_show("Cannot allocate path for path_images\n");
963         return 0;
964      }
965    e_user_dir_concat_static(buf, "/images");
966    e_path_default_path_append(path_images, buf);
967    e_prefix_data_concat_static(buf, "data/images");
968    e_path_default_path_append(path_images, buf);
969
970    /* setup font paths */
971    path_fonts = e_path_new();
972    if (!path_fonts)
973      {
974         e_error_message_show("Cannot allocate path for path_fonts\n");
975         return 0;
976      }
977    e_user_dir_concat_static(buf, "/fonts");
978    e_path_default_path_append(path_fonts, buf);
979    e_prefix_data_concat_static(buf, "data/fonts");
980    e_path_default_path_append(path_fonts, buf);
981
982    /* setup icon paths */
983    path_icons = e_path_new();
984    if (!path_icons)
985      {
986         e_error_message_show("Cannot allocate path for path_icons\n");
987         return 0;
988      }
989    e_user_dir_concat_static(buf, "/icons");
990    e_path_default_path_append(path_icons, buf);
991    e_prefix_data_concat_static(buf, "data/icons");
992    e_path_default_path_append(path_icons, buf);
993
994    /* setup module paths */
995    path_modules = e_path_new();
996    if (!path_modules)
997      {
998         e_error_message_show("Cannot allocate path for path_modules\n");
999         return 0;
1000      }
1001    e_user_dir_concat_static(buf, "/modules");
1002    e_path_default_path_append(path_modules, buf);
1003    snprintf(buf, sizeof(buf), "%s/enlightenment/modules", e_prefix_lib_get());
1004    e_path_default_path_append(path_modules, buf);
1005    /* FIXME: eventually this has to go - moduels should have installers that
1006     * add appropriate install paths (if not installed to user homedir) to
1007     * e's module search dirs
1008     */
1009    snprintf(buf, sizeof(buf), "%s/enlightenment/modules_extra", e_prefix_lib_get());
1010    e_path_default_path_append(path_modules, buf);
1011
1012    /* setup background paths */
1013    path_backgrounds = e_path_new();
1014    if (!path_backgrounds)
1015      {
1016         e_error_message_show("Cannot allocate path for path_backgrounds\n");
1017         return 0;
1018      }
1019    e_user_dir_concat_static(buf, "/backgrounds");
1020    e_path_default_path_append(path_backgrounds, buf);
1021    e_prefix_data_concat_static(buf, "data/backgrounds");
1022    e_path_default_path_append(path_backgrounds, buf);
1023
1024    path_messages = e_path_new();
1025    if (!path_messages)
1026      {
1027         e_error_message_show("Cannot allocate path for path_messages\n");
1028         return 0;
1029      }
1030    e_user_dir_concat_static(buf, "/locale");
1031    e_path_default_path_append(path_messages, buf);
1032    e_path_default_path_append(path_messages, e_prefix_locale_get());
1033
1034    return 1;
1035 }
1036
1037 static int
1038 _e_main_path_shutdown(void)
1039 {
1040    if (path_data)
1041      {
1042         e_object_del(E_OBJECT(path_data));
1043         path_data = NULL;
1044      }
1045    if (path_images)
1046      {
1047         e_object_del(E_OBJECT(path_images));
1048         path_images = NULL;
1049      }
1050    if (path_fonts)
1051      {
1052         e_object_del(E_OBJECT(path_fonts));
1053         path_fonts = NULL;
1054      }
1055    if (path_icons)
1056      {
1057         e_object_del(E_OBJECT(path_icons));
1058         path_icons = NULL;
1059      }
1060    if (path_modules)
1061      {
1062         e_object_del(E_OBJECT(path_modules));
1063         path_modules = NULL;
1064      }
1065    if (path_backgrounds)
1066      {
1067         e_object_del(E_OBJECT(path_backgrounds));
1068         path_backgrounds = NULL;
1069      }
1070    if (path_messages)
1071      {
1072         e_object_del(E_OBJECT(path_messages));
1073         path_messages = NULL;
1074      }
1075    return 1;
1076 }
1077
1078 static int
1079 _e_main_screens_init(void)
1080 {
1081    TSB("\tscreens: client");
1082    if (!e_client_init()) return 0;
1083    TSE("\tscreens: client Done");
1084
1085    TSB("Compositor Init");
1086    PRCTL("[Winsys] start of compositor init");
1087    if (!e_comp_init())
1088      {
1089         e_error_message_show(_("Enlightenment cannot create a compositor.\n"));
1090         _e_main_shutdown(-1);
1091      }
1092    TSE("Compositor Init Done");
1093
1094    PRCTL("[Winsys] end of compositor init");
1095    _e_main_desk_restore();
1096
1097    return 1;
1098 }
1099
1100 static int
1101 _e_main_screens_shutdown(void)
1102 {
1103    e_win_shutdown();
1104    e_comp_shutdown();
1105    e_client_shutdown();
1106
1107    e_slot_shutdown();
1108    e_desk_shutdown();
1109    e_zone_shutdown();
1110    return 1;
1111 }
1112
1113 static void
1114 _e_main_desk_save(void)
1115 {
1116    const Eina_List *l;
1117    char env[1024], name[1024];
1118    E_Zone *zone;
1119
1120    EINA_LIST_FOREACH(e_comp->zones, l, zone)
1121      {
1122         snprintf(name, sizeof(name), "DESK_%d_%d", 0, zone->num);
1123         snprintf(env, sizeof(env), "%d,%d", zone->desk_x_current, zone->desk_y_current);
1124         e_util_env_set(name, env);
1125      }
1126 }
1127
1128 static void
1129 _e_main_desk_restore(void)
1130 {
1131    E_Client *ec;
1132
1133    E_CLIENT_REVERSE_FOREACH(ec)
1134      if ((!e_client_util_ignored_get(ec)) && e_client_util_desk_visible(ec, e_desk_current_get(ec->zone)))
1135        {
1136           ec->want_focus = ec->take_focus = 1;
1137           break;
1138        }
1139 }
1140
1141 static Eina_Bool
1142 _e_main_cb_idle_before(void *data EINA_UNUSED)
1143 {
1144    e_client_idler_before();
1145    edje_thaw();
1146    return ECORE_CALLBACK_RENEW;
1147 }
1148
1149 static Eina_Bool
1150 _e_main_cb_idle_after(void *data EINA_UNUSED)
1151 {
1152    static int first_idle = 1;
1153
1154    eet_clearcache();
1155    edje_freeze();
1156
1157    if (first_idle)
1158      {
1159         TSM("SLEEP");
1160         first_idle = 0;
1161      }
1162
1163    return ECORE_CALLBACK_RENEW;
1164 }
1165
1166 static void
1167 _e_main_create_wm_ready(void)
1168 {
1169    FILE *_wmready_checker = NULL;
1170    const char *path_wm_ready = "/run/.wm_ready";
1171
1172    if (!e_util_file_realpath_check(path_wm_ready, EINA_TRUE))
1173      {
1174         WRN("%s is maybe link, so delete it\n", path_wm_ready);
1175      }
1176
1177    _wmready_checker = fopen(path_wm_ready, "wb");
1178    if (_wmready_checker)
1179      {
1180         TSM("[WM] WINDOW MANAGER is READY!!!");
1181         PRCTL("[Winsys] WINDOW MANAGER is READY!!!");
1182         fclose(_wmready_checker);
1183
1184         /*TODO: Next lines should be removed. */
1185         FILE *_tmp_wm_ready_checker;
1186
1187         _tmp_wm_ready_checker = fopen(path_wm_ready, "wb");
1188
1189         if (_tmp_wm_ready_checker)
1190           {
1191              TSM("[WM] temporary wm_ready path is created.");
1192              PRCTL("[Winsys] temporary wm_ready path is created.");
1193              fclose(_tmp_wm_ready_checker);
1194           }
1195         else
1196           {
1197              TSM("[WM] temporary wm_ready path create failed.");
1198              PRCTL("[Winsys] temporary wm_ready path create failed.");
1199           }
1200      }
1201    else
1202      {
1203         TSM("[WM] WINDOW MANAGER is READY. BUT, failed to create .wm_ready file.");
1204         PRCTL("[Winsys] WINDOW MANAGER is READY. BUT, failed to create .wm_ready file.");
1205      }
1206 }
1207
1208 static void
1209 _e_main_hooks_clean(void)
1210 {
1211    Eina_Inlist *l;
1212    E_Main_Hook *mh;
1213    unsigned int x;
1214
1215    for (x = 0; x < E_MAIN_HOOK_LAST; x++)
1216      EINA_INLIST_FOREACH_SAFE(_e_main_hooks[x], l, mh)
1217        {
1218           if (!mh->delete_me) continue;
1219           _e_main_hooks[x] = eina_inlist_remove(_e_main_hooks[x],
1220                                                 EINA_INLIST_GET(mh));
1221           free(mh);
1222        }
1223 }
1224
1225 static void
1226 _e_main_hook_call(E_Main_Hook_Point hookpoint, void *data EINA_UNUSED)
1227 {
1228    E_Main_Hook *mh;
1229
1230    _e_main_hooks_walking++;
1231    EINA_INLIST_FOREACH(_e_main_hooks[hookpoint], mh)
1232      {
1233         if (mh->delete_me) continue;
1234         mh->func(mh->data);
1235      }
1236    _e_main_hooks_walking--;
1237    if ((_e_main_hooks_walking == 0) && (_e_main_hooks_delete > 0))
1238      _e_main_hooks_clean();
1239 }
1240
1241 E_API E_Main_Hook *
1242 e_main_hook_add(E_Main_Hook_Point hookpoint, E_Main_Hook_Cb func, const void *data)
1243 {
1244    E_Main_Hook *mh;
1245
1246    EINA_SAFETY_ON_TRUE_RETURN_VAL(hookpoint >= E_MAIN_HOOK_LAST, NULL);
1247    mh = E_NEW(E_Main_Hook, 1);
1248    EINA_SAFETY_ON_NULL_RETURN_VAL(mh, NULL);
1249    mh->hookpoint = hookpoint;
1250    mh->func = func;
1251    mh->data = (void*)data;
1252    _e_main_hooks[hookpoint] = eina_inlist_append(_e_main_hooks[hookpoint],
1253                                                  EINA_INLIST_GET(mh));
1254    return mh;
1255 }
1256
1257 E_API void
1258 e_main_hook_del(E_Main_Hook *mh)
1259 {
1260    mh->delete_me = 1;
1261    if (_e_main_hooks_walking == 0)
1262      {
1263         _e_main_hooks[mh->hookpoint] = eina_inlist_remove(_e_main_hooks[mh->hookpoint],
1264                                                           EINA_INLIST_GET(mh));
1265         free(mh);
1266      }
1267    else
1268      _e_main_hooks_delete++;
1269 }
1270
1271 E_API void
1272 e_main_hook_call(E_Main_Hook_Point hookpoint)
1273 {
1274    if ((hookpoint < 0) || (hookpoint >= E_MAIN_HOOK_LAST)) return;
1275
1276    _e_main_hook_call(hookpoint, NULL);
1277 }