Merge "Apply build option naming rule" into tizen
[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 #define APPID_INDICATOR "org.tizen.indicator"
41 #define APPID_QUICKPANEL "org.tizen.quickpanel"
42
43 #define DEAD_TIMER_SEC 10.0
44 #define DEAD_TIMER_COUNT_MAX 2
45
46
47
48 int errno;
49 static struct {
50         pid_t home_pid;
51         pid_t volume_pid;
52         pid_t indicator_pid;
53         pid_t quickpanel_pid;
54         int power_off;
55
56         Ecore_Timer *dead_timer;
57         int dead_count;
58
59         Evas_Object *popup;
60 } s_home_mgr = {
61         .home_pid = (pid_t)-1,
62         .volume_pid = (pid_t)-1,
63         .indicator_pid = (pid_t)-1,
64         .quickpanel_pid = (pid_t)-1,
65         .power_off = 0,
66
67         .dead_timer = NULL,
68         .dead_count = 0,
69
70         .popup = NULL,
71 };
72
73
74 int home_mgr_get_home_pid(void)
75 {
76         return s_home_mgr.home_pid;
77 }
78
79
80
81 int home_mgr_get_volume_pid(void)
82 {
83         return s_home_mgr.volume_pid;
84 }
85
86
87
88 int home_mgr_get_indicator_pid(void)
89 {
90         return s_home_mgr.indicator_pid;
91 }
92
93
94
95 int home_mgr_get_quickpanel_pid(void)
96 {
97         return s_home_mgr.quickpanel_pid;
98 }
99
100
101
102 static void _after_launch_home(int pid)
103 {
104         if (dbus_util_send_oomadj(pid, OOM_ADJ_VALUE_HOMESCREEN) < 0){
105                 _E("failed to send oom dbus signal");
106         }
107         s_home_mgr.home_pid = pid;
108 }
109
110
111
112 static int _change_home_cb(const char *appid, const char *key, const char *value, void *cfn, void *afn)
113 {
114         if (!strcmp(appid, MENU_SCREEN_PKG_NAME)) {
115                 _E("We cannot do anything anymore.");
116         } else if (!strcmp(appid, status_active_get()->setappl_selected_package_name)) {
117                 if (vconf_set_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, MENU_SCREEN_PKG_NAME) != 0) {
118                         _E("cannot set the vconf key as %s", MENU_SCREEN_PKG_NAME);
119                 }
120                 /* change_home func will be called by changing the home */
121                 return 0;
122         }
123         _E("cannot change home");
124         return -1;
125 }
126
127
128
129 void home_mgr_open_home(const char *appid, const char *key, const char *val)
130 {
131         char *home_appid = NULL;
132
133 #ifndef TIZEN_ARCH_ARM64
134         if (!appid) {
135                 home_appid = status_active_get()->setappl_selected_package_name;
136         } else {
137                 home_appid = (char *) appid;
138         }
139         ret_if(!home_appid);
140 #else
141         /*
142          * If the architecture is 64bit,
143          * starter will launch menu-screen only.
144          */
145         _D("[64bit] menu-screen will be launched.");
146         home_appid = MENU_SCREEN_PKG_NAME;
147 #endif
148
149         process_mgr_must_launch(home_appid, key, val, _change_home_cb, _after_launch_home);
150 }
151
152
153
154 static int _show_home_cb(status_active_key_e key, void *data)
155 {
156         int seq = status_active_get()->starter_sequence;
157         int is_fallback = 0;
158
159         _D("[MENU_DAEMON] _show_home_cb is invoked(%d)", seq);
160
161         switch (seq) {
162         case 0:
163                 if (s_home_mgr.home_pid > 0) {
164                         _D("Home[%d] has to be terminated.", s_home_mgr.home_pid);
165                         if (aul_terminate_pid(s_home_mgr.home_pid) != AUL_R_OK) {
166                                 _E("Failed to terminate %d", s_home_mgr.home_pid);
167                         }
168                         s_home_mgr.home_pid = -1; /* to freeze the dead_cb */
169                 }
170                 break;
171         case 1:
172                 if (vconf_get_int(VCONFKEY_STARTER_IS_FALLBACK, &is_fallback) < 0) {
173                         _E("Failed to get vconfkey : %s", VCONFKEY_STARTER_IS_FALLBACK);
174                 }
175
176                 if (is_fallback) {
177                         if (vconf_set_int(VCONFKEY_STARTER_IS_FALLBACK, 0)) {
178                                 _E("Failed to set vconfkey : %s", VCONFKEY_STARTER_IS_FALLBACK);
179                         }
180
181                         if (!strcmp(status_active_get()->setappl_selected_package_name, MENU_SCREEN_PKG_NAME)) {
182                                 char *fallback_pkg;
183
184                                 fallback_pkg = vconf_get_str(VCONFKEY_STARTER_FALLBACK_PKG);
185                                 _D("fallback pkg : %s", fallback_pkg);
186                                 if (fallback_pkg) {
187                                         int status;
188
189                                         status = vconf_set_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, fallback_pkg);
190                                         free(fallback_pkg);
191                                         if (status == 0) {
192                                                 break;
193                                         }
194                                         _E("Failed to set vconfkey : %s (%d)", VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, status);
195                                 } else {
196                                         _E("Failed to get vconfkey : %s", VCONFKEY_STARTER_FALLBACK_PKG);
197                                 }
198                         }
199                 }
200
201                 home_mgr_open_home(NULL, NULL, NULL);
202                 break;
203         default:
204                 _E("False sequence [%d]", seq);
205                 break;
206         }
207
208         return 1;
209 }
210
211
212
213 static int _change_selected_package_name(status_active_key_e key, void *data)
214 {
215         char *appid = NULL;
216         int seq = status_active_get()->starter_sequence;
217
218         if (seq < 1) {
219                 _E("Sequence is not ready yet, do nothing");
220                 return 1;
221         }
222
223         _D("_change_selected_package_name is invoked");
224
225         appid = status_active_get()->setappl_selected_package_name;
226         if (!appid) {
227                 return 1;
228         }
229         _SECURE_D("pkg_name : %s", appid);
230
231         if (s_home_mgr.home_pid > 0) {
232                 char old_appid[BUF_SIZE_512] = { 0 , };
233
234                 if (aul_app_get_pkgname_bypid(s_home_mgr.home_pid, old_appid, sizeof(old_appid)) == AUL_R_OK) {
235                         if (!strcmp(appid, old_appid)) {
236                                 _D("Package is changed but same package is selected");
237                                 return 1;
238                         }
239                 }
240
241                 if (AUL_R_OK != aul_terminate_pid(s_home_mgr.home_pid)) {
242                         _D("Failed to terminate pid %d", s_home_mgr.home_pid);
243                 }
244                 s_home_mgr.home_pid = -1;
245                 s_home_mgr.dead_count = 0;
246                 if (s_home_mgr.dead_timer) {
247                         ecore_timer_del(s_home_mgr.dead_timer);
248                         s_home_mgr.dead_timer = NULL;
249                 }
250         }
251
252         home_mgr_open_home(appid, NULL, NULL);
253
254         return 1;
255 }
256
257
258
259 static void _after_launch_volume(int pid)
260 {
261         if (dbus_util_send_oomadj(pid, OOM_ADJ_VALUE_DEFAULT) < 0){
262                 _E("failed to send oom dbus signal");
263         }
264         s_home_mgr.volume_pid = pid;
265 }
266
267
268
269 static void _after_launch_indicator(int pid)
270 {
271         s_home_mgr.indicator_pid = pid;
272 }
273
274
275
276 static void _after_launch_quickpanel(int pid)
277 {
278         s_home_mgr.quickpanel_pid = pid;
279 }
280
281
282
283 static void _launch_after_home(int pid)
284 {
285         if (pid > 0) {
286                 if(dbus_util_send_oomadj(pid, OOM_ADJ_VALUE_HOMESCREEN) < 0){
287                         _E("failed to send oom dbus signal");
288                 }
289         }
290         s_home_mgr.home_pid = pid;
291 }
292
293
294
295 static void _launch_home(const char *appid)
296 {
297         const char *home_appid = NULL;
298
299         if (!appid) {
300                 home_appid = status_active_get()->setappl_selected_package_name;
301         } else {
302                 home_appid = (char *) appid;
303         }
304         ret_if(!home_appid);
305
306         process_mgr_must_launch(home_appid, HOME_TERMINATED, ISTRUE, _change_home_cb, _launch_after_home);
307 }
308
309
310
311 static void _popup_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
312 {
313         _D("popup is deleted");
314
315         s_home_mgr.popup = NULL;
316 }
317
318
319
320 static Eina_Bool _dead_timer_cb(void *data)
321 {
322         Evas_Object *popup = NULL;
323         char title[BUF_SIZE_128] = { 0, };
324         char text[BUF_SIZE_1024] = { 0, };
325         char *appid = NULL;
326
327         appid = status_active_get()->setappl_selected_package_name;
328         if (!appid) {
329                 _E("appid is NULL");
330                 return ECORE_CALLBACK_CANCEL;
331         }
332
333         _D("dead count : %s(%d)", appid, s_home_mgr.dead_count);
334
335         if (s_home_mgr.dead_count >= DEAD_TIMER_COUNT_MAX) {
336                 _D("Change homescreen package to default");
337
338                 /* set fallback status */
339                 if (vconf_set_int(VCONFKEY_STARTER_IS_FALLBACK, 1) < 0) {
340                         _E("Failed to set vconfkey : %s", VCONFKEY_STARTER_IS_FALLBACK);
341                 }
342
343                 if (vconf_set_str(VCONFKEY_STARTER_FALLBACK_PKG, appid) < 0) {
344                         _E("Failed to set vconfkey : %s", VCONFKEY_STARTER_FALLBACK_PKG);
345                 }
346
347                 strncpy(title, _("IDS_COM_POP_WARNING"), sizeof(title));
348                 title[sizeof(title) - 1] = '\0';
349
350                 snprintf(text, sizeof(text), _("IDS_IDLE_POP_UNABLE_TO_LAUNCH_PS"), appid);
351                 _D("title : %s / text : %s", title, text);
352
353                 if (!s_home_mgr.popup) {
354                         popup = popup_create(title, text);
355                         if (!popup) {
356                                 _E("Failed to create popup");
357                         } else {
358                                 s_home_mgr.popup = popup;
359                                 evas_object_event_callback_add(popup, EVAS_CALLBACK_DEL, _popup_del_cb, NULL);
360                         }
361                 }
362
363                 if (vconf_set_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, MENU_SCREEN_PKG_NAME) != 0) {
364                         _E("cannot set the vconf key as %s", MENU_SCREEN_PKG_NAME);
365                         return ECORE_CALLBACK_RENEW;
366                 }
367         }
368
369         s_home_mgr.dead_timer = NULL;
370         s_home_mgr.dead_count = 0;
371
372         return ECORE_CALLBACK_CANCEL;
373 }
374
375
376
377 void home_mgr_relaunch_homescreen(void)
378 {
379         char *appid = NULL;
380
381         if (s_home_mgr.power_off) {
382                 _E("power off");
383                 return;
384         }
385
386         appid = status_active_get()->setappl_selected_package_name;
387         if (!appid) {
388                 _E("appid is NULL");
389                 return;
390         }
391
392         s_home_mgr.dead_count++;
393         _D("home dead count : %d", s_home_mgr.dead_count);
394
395         if (!s_home_mgr.dead_timer) {
396                 _D("Add dead timer");
397                 s_home_mgr.dead_timer = ecore_timer_add(DEAD_TIMER_SEC, _dead_timer_cb, NULL);
398                 if (!s_home_mgr.dead_timer) {
399                         _E("Failed to add a dead timer");
400                 }
401         }
402
403         _launch_home(appid);
404 }
405
406
407
408 void home_mgr_relaunch_volume(void)
409 {
410         process_mgr_must_syspopup_launch(SYSPOPUPID_VOLUME, NULL, NULL, NULL, _after_launch_volume);
411 }
412
413
414
415 void home_mgr_relaunch_indicator(void)
416 {
417         process_mgr_must_launch(APPID_INDICATOR, NULL, NULL, NULL, _after_launch_indicator);
418 }
419
420
421
422 void home_mgr_relaunch_quickpanel(void)
423 {
424         process_mgr_must_launch(APPID_QUICKPANEL, NULL, NULL, NULL, _after_launch_quickpanel);
425 }
426
427
428
429 static int _power_off_cb(status_active_key_e key, void *data)
430 {
431         int val = status_active_get()->sysman_power_off_status;
432
433         if (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || val == VCONFKEY_SYSMAN_POWER_OFF_RESTART) {
434                 s_home_mgr.power_off = 1;
435         } else {
436                 s_home_mgr.power_off = 0;
437         }
438
439         _D("power off status : %d", s_home_mgr.power_off);
440
441         return 1;
442 }
443
444
445
446 static Eina_Bool _launch_apps_idler_cb(void *data)
447 {
448 #ifndef TIZEN_ARCH_ARM64
449         process_mgr_must_syspopup_launch(SYSPOPUPID_VOLUME, NULL, NULL, NULL, _after_launch_volume);
450         process_mgr_must_launch(APPID_INDICATOR, NULL, NULL, NULL, _after_launch_indicator);
451         process_mgr_must_launch(APPID_QUICKPANEL, NULL, NULL, NULL, _after_launch_quickpanel);
452 #endif
453
454         return ECORE_CALLBACK_CANCEL;
455 }
456
457
458
459 void home_mgr_init(void *data)
460 {
461         _D( "[MENU_DAEMON]home_mgr_init is invoked");
462
463         status_active_register_cb(STATUS_ACTIVE_KEY_STARTER_SEQUENCE, _show_home_cb, NULL);
464         status_active_register_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb, NULL);
465         status_active_register_cb(STATUS_ACTIVE_KEY_SETAPPL_SELECTED_PACKAGE_NAME, _change_selected_package_name, NULL);
466
467         ecore_idler_add(_launch_apps_idler_cb, NULL);
468 }
469
470
471
472 void home_mgr_fini(void)
473 {
474         if (s_home_mgr.volume_pid > 0) {
475                 process_mgr_terminate_app(s_home_mgr.volume_pid, 1);
476                 s_home_mgr.volume_pid = -1;
477         }
478
479         status_active_unregister_cb(STATUS_ACTIVE_KEY_STARTER_SEQUENCE, _show_home_cb);
480         status_active_unregister_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb);
481         status_active_unregister_cb(STATUS_ACTIVE_KEY_SETAPPL_SELECTED_PACKAGE_NAME, _change_selected_package_name);
482 }
483
484
485
486 // End of a file