Remove warnings
[apps/home/starter.git] / src / starter.c
1  /*
2   * Copyright 2012  Samsung Electronics Co., Ltd
3   *
4   * Licensed under the Flora License, Version 1.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   *
8   *     http://floralicense.org/license/
9   *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16
17
18
19 #include <Elementary.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <poll.h>
28
29 #include <aul.h>
30 #include <vconf.h>
31 #include <heynoti.h>
32 #include <signal.h>
33 #include <system_info.h>
34
35 #include "starter.h"
36 #include "starter-util.h"
37 #include "x11.h"
38 #include "lock-daemon.h"
39 #include "lockd-debug.h"
40 #include "menu_daemon.h"
41
42 #ifndef PACKAGE_NAME
43 #define PACKAGE_NAME "org.tizen.starter"
44 #endif
45
46 #define DEFAULT_THEME "tizen"
47 #define PWLOCK_PATH "/usr/apps/org.tizen.pwlock/bin/pwlock"
48 #define PWLOCK_PKG_NAME "org.tizen.pwlock"
49 #define QP_EMUL_STR      "Emulator"
50
51 static void lock_menu_screen(void)
52 {
53         vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 0);
54 }
55
56 static void unlock_menu_screen(void)
57 {
58         int r;
59         int show_menu;
60
61         show_menu = 0;
62         r = vconf_get_int(VCONFKEY_STARTER_SEQUENCE, &show_menu);
63         if (r || !show_menu) {
64                 vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 1);
65         }
66 }
67
68 static void _set_elm_theme(void)
69 {
70         char *vstr;
71         char *theme;
72         Elm_Theme *th = NULL;
73         vstr = vconf_get_str(VCONFKEY_SETAPPL_WIDGET_THEME_STR);
74         if (vstr == NULL)
75                 theme = DEFAULT_THEME;
76         else
77                 theme = vstr;
78
79         th = elm_theme_new();
80         _DBG("theme vconf[%s]\n set[%s]\n", vstr, theme);
81         elm_theme_set(th, theme);
82
83         if (vstr)
84                 free(vstr);
85 }
86
87 static int _check_emul()
88 {
89         int is_emul = 0;
90         char *info = NULL;
91
92         if (system_info_get_value_string(SYSTEM_INFO_KEY_MODEL, &info) == 0) {
93                 if (info == NULL) return 0;
94                 if (!strncmp(QP_EMUL_STR, info, strlen(info))) {
95                         is_emul = 1;
96                 }
97         }
98
99         if (info != NULL) free(info);
100
101         return is_emul;
102 }
103
104 static int _launch_pwlock(void)
105 {
106         int r;
107
108         _DBG("%s", __func__);
109
110         if(_check_emul()) {
111                 _DBG("Emulator => skip pwlock");
112                 vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 1);
113                 return 0;
114         }
115
116         r = aul_launch_app(PWLOCK_PKG_NAME, NULL);
117         if (r < 0) {
118                 _ERR("PWLock launch error: error(%d)", r);
119                 if (r == AUL_R_ETIMEOUT) {
120                         _DBG("Launch pwlock is failed for AUL_R_ETIMEOUT, again launch pwlock");
121                         r = aul_launch_app(PWLOCK_PKG_NAME, NULL);
122                         if (r < 0) {
123                                 _ERR("2'nd PWLock launch error: error(%d)", r);
124                                 return -1;
125                         } else {
126                                 _DBG("Launch pwlock");
127                                 return 0;
128                         }
129                 } else {
130                         return -1;
131                 }
132         } else {
133                 _DBG("Launch pwlock");
134                 return 0;
135         }
136 }
137
138 static void _signal_handler(int signum, siginfo_t *info, void *unused)
139 {
140     _DBG("_signal_handler : Terminated...");
141     elm_exit();
142 }
143
144 static void _heynoti_event_power_off(void *data)
145 {
146     _DBG("_heynoti_event_power_off : Terminated...");
147     elm_exit();
148 }
149
150 static Eina_Bool _init_idle(void *data)
151 {
152         _DBG("%s %d\n", __func__, __LINE__);
153         if (_launch_pwlock() < 0) {
154                 _ERR("launch pwlock error");
155         }
156         menu_daemon_init(NULL);
157
158         return ECORE_CALLBACK_CANCEL;
159 }
160
161 static void _lock_state_cb(keynode_t * node, void *data)
162 {
163         _DBG("%s %d\n", __func__, __LINE__);
164         WRITE_FILE_LOG("%s", "Lock state is changed!");
165
166         if (_launch_pwlock() < 0) {
167                 _ERR("launch pwlock error");
168         }
169         menu_daemon_init(NULL);
170         if (vconf_ignore_key_changed(VCONFKEY_IDLE_LOCK_STATE,
171              _lock_state_cb) != 0) {
172                 LOCKD_DBG("Fail to unregister");
173         }
174 }
175
176 static void _init(struct appdata *ad)
177 {
178         int r;
179         struct sigaction act;
180
181         memset(&act,0x00,sizeof(struct sigaction));
182         act.sa_sigaction = _signal_handler;
183         act.sa_flags = SA_SIGINFO;
184
185         int ret = sigemptyset(&act.sa_mask);
186         if (ret < 0) {
187                 _ERR("Failed to sigemptyset[%s]", strerror(errno));
188         }
189         ret = sigaddset(&act.sa_mask, SIGTERM);
190         if (ret < 0) {
191                 _ERR("Failed to sigaddset[%s]", strerror(errno));
192         }
193         ret = sigaction(SIGTERM, &act, NULL);
194         if (ret < 0) {
195                 _ERR("Failed to sigaction[%s]", strerror(errno));
196         }
197
198         memset(ad, 0, sizeof(struct appdata));
199
200         gettimeofday(&ad->tv_start, NULL);
201
202         lock_menu_screen();
203         _set_elm_theme();
204
205         _DBG("%s %d\n", __func__, __LINE__);
206
207         r = start_lock_daemon(TRUE);
208         if (r == 1) {
209                 if (vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE,
210                      _lock_state_cb, NULL) != 0) {
211                         _ERR("[Error] vconf notify : lock state");
212                         ecore_timer_add(1.5, _init_idle, NULL);
213                 }
214         } else {
215                 if (_launch_pwlock() < 0) {
216                         _ERR("launch pwlock error");
217                 }
218                 menu_daemon_init(NULL);
219         }
220 }
221
222 static void _fini(struct appdata *ad)
223 {
224         struct timeval tv, res;
225
226         if (ad == NULL) {
227                 fprintf(stderr, "Invalid argument: appdata is NULL\n");
228                 return;
229         }
230
231         unlock_menu_screen();
232         menu_daemon_fini();
233
234         gettimeofday(&tv, NULL);
235         timersub(&tv, &ad->tv_start, &res);
236         _DBG("Total time: %d.%06d sec\n", (int)res.tv_sec, (int)res.tv_usec);
237 }
238
239 int main(int argc, char *argv[])
240 {
241         struct appdata ad;
242
243         WRITE_FILE_LOG("%s", "Main function is started in starter");
244
245     int heyfd = heynoti_init();
246         if (heyfd < 0) {
247                 _ERR("Failed to heynoti_init[%d]", heyfd);
248                 return -1;
249         }
250
251         int ret = heynoti_subscribe(heyfd, "power_off_start", _heynoti_event_power_off, NULL);
252         if (ret < 0) {
253                 _ERR("Failed to heynoti_subscribe[%d]", ret);
254         }
255         ret = heynoti_attach_handler(heyfd);
256         if (ret < 0) {
257                 _ERR("Failed to heynoti_attach_handler[%d]", ret);
258         }
259
260         elm_init(argc, argv);
261
262         _init(&ad);
263
264         elm_run();
265
266         _fini(&ad);
267
268         elm_shutdown();
269
270         return 0;
271 }