Code sync up from tizen 2.4
[apps/native/starter.git] / src / mobile / home_mgr.c
1 /*
2  * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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 #include <aul.h>
18 #include <app.h>
19 #include <db-util.h>
20 #include <Elementary.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <pkgmgr-info.h>
24 #include <stdio.h>
25 #include <dd-deviced.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29 #include <vconf.h>
30
31 #include "util.h"
32 #include "dbus_util.h"
33 #include "status.h"
34 #include "process_mgr.h"
35 #include "popup.h"
36
37 #define HOME_TERMINATED "home_terminated"
38 #define ISTRUE "TRUE"
39 #define SYSPOPUPID_VOLUME "volume"
40
41 #define DEAD_TIMER_SEC 2.0
42 #define DEAD_TIMER_COUNT_MAX 2
43
44 #define VCONFKEY_PRIVATE_STARTER_IS_FALLBACK "db/private/starter/is_fallback"
45 #define VCONFKEY_PRIVATE_STARTER_FALLBACK_PKG "db/private/starter/fallback_pkg"
46
47
48
49 int errno;
50 static struct {
51         pid_t home_pid;
52         pid_t volume_pid;
53         int power_off;
54
55         Ecore_Timer *dead_timer;
56         int dead_count;
57
58         Evas_Object *popup;
59 } s_home_mgr = {
60         .home_pid = (pid_t)-1,
61         .volume_pid = (pid_t)-1,
62         .power_off = 0,
63
64         .dead_timer = NULL,
65         .dead_count = 0,
66
67         .popup = NULL,
68 };
69
70
71 int home_mgr_get_home_pid(void)
72 {
73         return s_home_mgr.home_pid;
74 }
75
76
77
78 int home_mgr_get_volume_pid(void)
79 {
80         return s_home_mgr.volume_pid;
81 }
82
83
84
85 static void _after_launch_home(int pid)
86 {
87         if (dbus_util_send_oomadj(pid, OOM_ADJ_VALUE_HOMESCREEN) < 0){
88                 _E("failed to send oom dbus signal");
89         }
90         s_home_mgr.home_pid = pid;
91 }
92
93
94
95 static int _change_home_cb(const char *appid, const char *key, const char *value, void *cfn, void *afn)
96 {
97         if (!strcmp(appid, MENU_SCREEN_PKG_NAME)) {
98                 _E("We cannot do anything anymore.");
99         } else if (!strcmp(appid, status_active_get()->setappl_selected_package_name)) {
100                 if (vconf_set_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, MENU_SCREEN_PKG_NAME) != 0) {
101                         _E("cannot set the vconf key as %s", MENU_SCREEN_PKG_NAME);
102                 }
103                 /* change_home func will be called by changing the home */
104                 return 0;
105         }
106         _E("cannot change home");
107         return -1;
108 }
109
110
111
112 #define SERVICE_OPERATION_MAIN_KEY "__APP_SVC_OP_TYPE__"
113 #define SERVICE_OPERATION_MAIN_VALUE "http://tizen.org/appcontrol/operation/main"
114 void home_mgr_open_home(const char *appid)
115 {
116         char *home_appid = NULL;
117
118         if (status_passive_get()->idle_screen_safemode) {
119                 home_appid = MENU_SCREEN_PKG_NAME;
120         } else if (!appid) {
121                 home_appid = status_active_get()->setappl_selected_package_name;
122         } else {
123                 home_appid = (char *) appid;
124         }
125         ret_if(!home_appid);
126
127         process_mgr_must_launch(home_appid, SERVICE_OPERATION_MAIN_KEY, SERVICE_OPERATION_MAIN_VALUE, _change_home_cb, _after_launch_home);
128 }
129
130
131
132 static int _show_home_cb(status_active_key_e key, void *data)
133 {
134         int seq = status_active_get()->starter_sequence;
135         int is_fallback = 0;
136
137         _D("[MENU_DAEMON] _show_home_cb is invoked(%d)", seq);
138
139         switch (seq) {
140         case 0:
141                 if (s_home_mgr.home_pid > 0) {
142                         _D("Home[%d] has to be terminated.", s_home_mgr.home_pid);
143                         if (aul_terminate_pid(s_home_mgr.home_pid) != AUL_R_OK) {
144                                 _E("Failed to terminate %d", s_home_mgr.home_pid);
145                         }
146                         s_home_mgr.home_pid = -1; /* to freeze the dead_cb */
147                 }
148                 break;
149         case 1:
150                 if (vconf_get_int(VCONFKEY_PRIVATE_STARTER_IS_FALLBACK, &is_fallback) < 0) {
151                         _E("Failed to get vconfkey : %s", VCONFKEY_PRIVATE_STARTER_IS_FALLBACK);
152                 }
153
154                 if (is_fallback) {
155                         if (vconf_set_int(VCONFKEY_PRIVATE_STARTER_IS_FALLBACK, 0)) {
156                                 _E("Failed to set vconfkey : %s", VCONFKEY_PRIVATE_STARTER_IS_FALLBACK);
157                         }
158
159                         if (!strcmp(status_active_get()->setappl_selected_package_name, MENU_SCREEN_PKG_NAME)) {
160                                 char *fallback_pkg;
161
162                                 fallback_pkg = vconf_get_str(VCONFKEY_PRIVATE_STARTER_FALLBACK_PKG);
163                                 _D("fallback pkg : %s", fallback_pkg);
164                                 if (fallback_pkg) {
165                                         int status;
166
167                                         status = vconf_set_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, fallback_pkg);
168                                         free(fallback_pkg);
169                                         if (status == 0) {
170                                                 break;
171                                         }
172                                         _E("Failed to set vconfkey : %s (%d)", VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, status);
173                                 } else {
174                                         _E("Failed to get vconfkey : %s", VCONFKEY_PRIVATE_STARTER_FALLBACK_PKG);
175                                 }
176                         }
177                 }
178
179                 home_mgr_open_home(NULL);
180                 break;
181         default:
182                 _E("False sequence [%d]", seq);
183                 break;
184         }
185
186         return 1;
187 }
188
189
190
191 static int _change_selected_package_name(status_active_key_e key, void *data)
192 {
193         char *appid = NULL;
194         int seq = status_active_get()->starter_sequence;
195
196         if (seq < 1) {
197                 _E("Sequence is not ready yet, do nothing");
198                 return 1;
199         }
200
201         _D("_change_selected_package_name is invoked");
202
203         appid = status_active_get()->setappl_selected_package_name;
204         if (!appid) {
205                 return 1;
206         }
207         _SECURE_D("pkg_name : %s", appid);
208
209         if (s_home_mgr.home_pid > 0) {
210                 char old_appid[BUF_SIZE_512] = { 0 , };
211
212                 if (aul_app_get_pkgname_bypid(s_home_mgr.home_pid, old_appid, sizeof(old_appid)) == AUL_R_OK) {
213                         if (!strcmp(appid, old_appid)) {
214                                 _D("Package is changed but same package is selected");
215                                 return 1;
216                         }
217                 }
218
219                 if (AUL_R_OK != aul_terminate_pid(s_home_mgr.home_pid)) {
220                         _D("Failed to terminate pid %d", s_home_mgr.home_pid);
221                 }
222                 s_home_mgr.home_pid = -1;
223                 s_home_mgr.dead_count = 0;
224                 if (s_home_mgr.dead_timer) {
225                         ecore_timer_del(s_home_mgr.dead_timer);
226                         s_home_mgr.dead_timer = NULL;
227                 }
228         }
229
230         home_mgr_open_home(appid);
231
232         return 1;
233 }
234
235
236
237 static void _after_launch_volume(int pid)
238 {
239         if (dbus_util_send_oomadj(pid, OOM_ADJ_VALUE_DEFAULT) < 0){
240                 _E("failed to send oom dbus signal");
241         }
242         s_home_mgr.volume_pid = pid;
243 }
244
245
246
247 static void _launch_after_home(int pid)
248 {
249         if (pid > 0) {
250                 if(dbus_util_send_oomadj(pid, OOM_ADJ_VALUE_HOMESCREEN) < 0){
251                         _E("failed to send oom dbus signal");
252                 }
253         }
254         s_home_mgr.home_pid = pid;
255 }
256
257
258
259 static void _launch_home(const char *appid)
260 {
261         const char *home_appid = NULL;
262
263         if (status_passive_get()->idle_screen_safemode) {
264                 home_appid = MENU_SCREEN_PKG_NAME;
265         } else if (!appid) {
266                 home_appid = status_active_get()->setappl_selected_package_name;
267         } else {
268                 home_appid = (char *) appid;
269         }
270         ret_if(!home_appid);
271
272         process_mgr_must_launch(home_appid, HOME_TERMINATED, ISTRUE, _change_home_cb, _launch_after_home);
273 }
274
275
276
277 static void _popup_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
278 {
279         _D("popup is deleted");
280
281         s_home_mgr.popup = NULL;
282 }
283
284
285
286 static Eina_Bool _dead_timer_cb(void *data)
287 {
288         Evas_Object *popup = NULL;
289         char title[BUF_SIZE_128] = { 0, };
290         char text[BUF_SIZE_1024] = { 0, };
291
292         char *appid = (char *)data;
293         retv_if(!appid, ECORE_CALLBACK_CANCEL);
294
295         _D("dead count : %s(%d)", appid, s_home_mgr.dead_count);
296
297         if (s_home_mgr.dead_count >= DEAD_TIMER_COUNT_MAX) {
298                 _D("Change homescreen package to default");
299
300                 if (vconf_set_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, MENU_SCREEN_PKG_NAME) != 0) {
301                         _E("cannot set the vconf key as %s", MENU_SCREEN_PKG_NAME);
302                         return ECORE_CALLBACK_RENEW;
303                 }
304
305                 strncpy(title, _("IDS_COM_POP_WARNING"), sizeof(title));
306                 title[sizeof(title) - 1] = '\0';
307
308                 snprintf(text, sizeof(text), _("IDS_IDLE_POP_UNABLE_TO_LAUNCH_PS"), appid);
309                 _D("title : %s / text : %s", title, text);
310
311                 if (!s_home_mgr.popup) {
312                         popup = popup_create(title, text);
313                         if (!popup) {
314                                 _E("Failed to create popup");
315                         } else {
316                                 s_home_mgr.popup = popup;
317                                 evas_object_event_callback_add(popup, EVAS_CALLBACK_DEL, _popup_del_cb, NULL);
318                         }
319                 }
320
321                 /* set fallback status */
322                 if (vconf_set_int(VCONFKEY_PRIVATE_STARTER_IS_FALLBACK, 1) < 0) {
323                         _E("Failed to set vconfkey : %s", VCONFKEY_PRIVATE_STARTER_IS_FALLBACK);
324                 }
325
326                 if (vconf_set_str(VCONFKEY_PRIVATE_STARTER_FALLBACK_PKG, appid) < 0) {
327                         _E("Failed to set vconfkey : %s", VCONFKEY_PRIVATE_STARTER_FALLBACK_PKG);
328                 }
329         }
330
331         s_home_mgr.dead_timer = NULL;
332         s_home_mgr.dead_count = 0;
333
334         return ECORE_CALLBACK_CANCEL;
335 }
336
337
338
339 void home_mgr_relaunch_homescreen(void)
340 {
341         char *appid = NULL;
342
343         if (s_home_mgr.power_off) {
344                 _E("power off");
345                 return;
346         }
347
348         appid = status_active_get()->setappl_selected_package_name;
349         if (!appid) {
350                 _E("appid is NULL");
351                 return;
352         }
353
354         s_home_mgr.dead_count++;
355         _D("home dead count : %d", s_home_mgr.dead_count);
356
357         if (!s_home_mgr.dead_timer) {
358                 _D("Add dead timer");
359                 s_home_mgr.dead_timer = ecore_timer_add(DEAD_TIMER_SEC, _dead_timer_cb, (void *)appid);
360         }
361
362         _launch_home(appid);
363 }
364
365
366
367 void home_mgr_relaunch_volume(void)
368 {
369         process_mgr_must_syspopup_launch(SYSPOPUPID_VOLUME, NULL, NULL, NULL, _after_launch_volume);
370 }
371
372
373
374 static int _power_off_cb(status_active_key_e key, void *data)
375 {
376         int val = status_active_get()->sysman_power_off_status;
377
378         if (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || val == VCONFKEY_SYSMAN_POWER_OFF_RESTART) {
379                 s_home_mgr.power_off = 1;
380         } else {
381                 s_home_mgr.power_off = 0;
382         }
383
384         _D("power off status : %d", s_home_mgr.power_off);
385
386         return 1;
387 }
388
389
390
391 static Eina_Bool _launch_volume_idler_cb(void *data)
392 {
393         process_mgr_must_syspopup_launch(SYSPOPUPID_VOLUME, NULL, NULL, NULL, _after_launch_volume);
394         return ECORE_CALLBACK_CANCEL;
395 }
396
397
398
399 void home_mgr_init(void *data)
400 {
401         _D( "[MENU_DAEMON]home_mgr_init is invoked");
402
403         status_active_register_cb(STATUS_ACTIVE_KEY_STARTER_SEQUENCE, _show_home_cb, NULL);
404         status_active_register_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb, NULL);
405         status_active_register_cb(STATUS_ACTIVE_KEY_SETAPPL_SELECTED_PACKAGE_NAME, _change_selected_package_name, NULL);
406         _change_selected_package_name(STATUS_ACTIVE_KEY_SETAPPL_SELECTED_PACKAGE_NAME, NULL);
407
408         ecore_idler_add(_launch_volume_idler_cb, NULL);
409 }
410
411
412
413 void home_mgr_fini(void)
414 {
415         if (s_home_mgr.volume_pid > 0) {
416                 process_mgr_terminate_app(s_home_mgr.volume_pid, 1);
417                 s_home_mgr.volume_pid = -1;
418         }
419
420         status_active_unregister_cb(STATUS_ACTIVE_KEY_STARTER_SEQUENCE, _show_home_cb);
421         status_active_unregister_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb);
422         status_active_unregister_cb(STATUS_ACTIVE_KEY_SETAPPL_SELECTED_PACKAGE_NAME, _change_selected_package_name);
423 }
424
425
426
427 // End of a file