summaryrefslogtreecommitdiff
path: root/src/bin/e_startup.c
blob: 0286375e25e4e74d0bfa01bff863c013df22a592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "e.h"

/* TODO:
 * - Need some kind of "wait for exit" system, maybe register with
 *   e_config? startup and restart apps could also be in e_config
 */

/* local subsystem functions */
static void      _e_startup(void);
static void      _e_startup_next_cb(void *data);
static Eina_Bool _e_startup_event_cb(void *data, int ev_type, void *ev);
static Eina_Bool _e_startup_time_exceeded(void *data);

/* local subsystem globals */
static E_Order *startup_apps = NULL;
static int start_app_pos = -1;
static Ecore_Event_Handler *desktop_cache_update_handler = NULL;
static Ecore_Timer *timer = NULL;
static Eina_Bool desktop_cache_update = EINA_FALSE;
static Eina_Bool started = EINA_FALSE;
static E_Startup_Mode startup_mode = 0;

/* externally accessible functions */


E_API void
e_startup_mode_set(E_Startup_Mode mode)
{
   startup_mode = mode;
   desktop_cache_update_handler =
     ecore_event_handler_add(EFREET_EVENT_DESKTOP_CACHE_BUILD,
                             _e_startup_event_cb, NULL);
   if (timer) ecore_timer_del(timer);
   timer = ecore_timer_add(20.0, _e_startup_time_exceeded, NULL);
   e_init_undone();
}

E_API void
e_startup(void)
{
   if (!desktop_cache_update)
     started = EINA_TRUE;
   else
     _e_startup();
}

/* local subsystem functions */
static Eina_Bool
_e_startup_delay(void *data)
{
   Efreet_Desktop *desktop = data;
   e_exec(NULL, desktop, NULL, NULL, NULL);
   efreet_desktop_unref(desktop);
   return EINA_FALSE;
}

// custom float parser for N.nnnn, or N,nnnn or N to avoid locale issues
static double
_atof(const char *s)
{
   const char *p;
   double v = 0, dec;

   for (p = s; isdigit(*p); p++)
     {
        v *= 10.0;
        v += (double)(*p - '0');
     }
   if ((*p == '.') || (*p == ','))
     {
        dec = 0.1;
        for (p++; isdigit(*p); p++)
          {
             v += ((double)(*p - '0')) * dec;
             dec /= 10.0;
          }
     }
   return v;
}

static void
_e_startup(void)
{
   Efreet_Desktop *desktop;
   const char *s;
   double delay = 0.0;

   if (!startup_apps)
     {
        e_init_done();
        return;
     }
   desktop = eina_list_nth(startup_apps->desktops, start_app_pos);
   start_app_pos++;
   if (!desktop)
     {
        e_object_del(E_OBJECT(startup_apps));
        startup_apps = NULL;
        start_app_pos = -1;
        e_init_done();
        return;
     }
   if (desktop->x)
     {
        s = eina_hash_find(desktop->x, "X-GNOME-Autostart-Delay");
        if (s)
          {
             const char *prev = setlocale(LC_NUMERIC, "C");
             delay = _atof(s);
             setlocale(LC_NUMERIC, prev);
          }
     }
   if (delay > 0.0)
     {
        efreet_desktop_ref(desktop);
        ecore_timer_add(delay, _e_startup_delay, desktop);
     }
   else e_exec(NULL, desktop, NULL, NULL, NULL);
   ecore_job_add(_e_startup_next_cb, NULL);
}

static void
_e_startup_next_cb(void *data EINA_UNUSED)
{
   _e_startup();
}

static void
_e_startup_error_dialog(const char *msg)
{
   E_Dialog *dia;

   dia = e_dialog_new(NULL, "E", "_startup_error_dialog");
   EINA_SAFETY_ON_NULL_RETURN(dia);

   e_dialog_title_set(dia, "ERROR!");
   e_dialog_icon_set(dia, "enlightenment", 64);
   e_dialog_text_set(dia, msg);
   e_dialog_button_add(dia, _("Close"), NULL, NULL, NULL);
   elm_win_center(dia->win, 1, 1);
   e_win_no_remember_set(dia->win, 1);
   e_dialog_show(dia);
}

static E_Order *
_e_startup_load(const char *file)
{
   E_Order *o = e_order_new(file);
   if (!o) return NULL;
   if (!o->desktops)
     {
        e_object_del(E_OBJECT(o));
        o = NULL;
     }
   return o;
}

static Eina_Bool
_e_startup_event_cb(void *data EINA_UNUSED, int ev_type EINA_UNUSED, void *ev)
{
   char buf[PATH_MAX];
   Efreet_Event_Cache_Update *e;

   if (timer) ecore_timer_del(timer);
   timer = NULL;

   e = ev;
   if ((e) && (e->error))
     {
        fprintf(stderr, "E: efreet couldn't build cache\n");
        _e_startup_error_dialog("E: Efreetd cannot be connected to.<br>"
                                "Please check:<br>"
                                "$XDG_RUTIME_DIR/.ecore/efreetd/0<br>"
                                "or<br>"
                                "~/.ecore/efreetd/0<br>"
                                "Is your system very slow on start so<br>"
                                "efreetd cannot be connected to within<br>"
                                "0.5sec after launch??");
     }
   desktop_cache_update = EINA_TRUE;
   E_FREE_FUNC(desktop_cache_update_handler, ecore_event_handler_del);

   if (startup_mode == E_STARTUP_START)
     {
        e_user_dir_concat_static(buf, "applications/startup/.order");
        startup_apps = _e_startup_load(buf);
        if (!startup_apps)
          {
             e_prefix_data_concat_static(buf, "data/applications/startup/.order");
             startup_apps = e_order_new(buf);
          }
     }
   else if (startup_mode == E_STARTUP_RESTART)
     {
        e_user_dir_concat_static(buf, "applications/restart/.order");
        startup_apps = _e_startup_load(buf);
        if (!startup_apps)
          {
             e_prefix_data_concat_static(buf, "data/applications/restart/.order");
             startup_apps = e_order_new(buf);
          }
     }
   if (startup_apps) start_app_pos = 0;
   if (started) _e_startup();
   return ECORE_CALLBACK_PASS_ON;
}

static Eina_Bool
_e_startup_time_exceeded(void *data EINA_UNUSED)
{
   fprintf(stderr, "E: efreet didn't notify about cache update\n");
   _e_startup_error_dialog("E: Efreet did not update cache.<br>"
                           "Please check your Efreet setup.<br>"
                           "Is efreetd running?<br>"
                           "Can ~/.cache/efreet be written to?");
   timer = NULL;
   return ECORE_CALLBACK_CANCEL;
}