Add code to launch apps(indicator, quickpanel)
[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 #define SERVICE_OPERATION_MAIN_KEY "__APP_SVC_OP_TYPE__"
130 #define SERVICE_OPERATION_MAIN_VALUE "http://tizen.org/appcontrol/operation/main"
131 void home_mgr_open_home(const char *appid)
132 {
133         char *home_appid = NULL;
134
135         if (!appid) {
136                 home_appid = status_active_get()->setappl_selected_package_name;
137         } else {
138                 home_appid = (char *) appid;
139         }
140         ret_if(!home_appid);
141
142         process_mgr_must_launch(home_appid, SERVICE_OPERATION_MAIN_KEY, SERVICE_OPERATION_MAIN_VALUE, _change_home_cb, _after_launch_home);
143 }
144
145
146
147 static int _show_home_cb(status_active_key_e key, void *data)
148 {
149         int seq = status_active_get()->starter_sequence;
150         int is_fallback = 0;
151
152         _D("[MENU_DAEMON] _show_home_cb is invoked(%d)", seq);
153
154         switch (seq) {
155         case 0:
156                 if (s_home_mgr.home_pid > 0) {
157                         _D("Home[%d] has to be terminated.", s_home_mgr.home_pid);
158                         if (aul_terminate_pid(s_home_mgr.home_pid) != AUL_R_OK) {
159                                 _E("Failed to terminate %d", s_home_mgr.home_pid);
160                         }
161                         s_home_mgr.home_pid = -1; /* to freeze the dead_cb */
162                 }
163                 break;
164         case 1:
165                 if (vconf_get_int(VCONFKEY_STARTER_IS_FALLBACK, &is_fallback) < 0) {
166                         _E("Failed to get vconfkey : %s", VCONFKEY_STARTER_IS_FALLBACK);
167                 }
168
169                 if (is_fallback) {
170                         if (vconf_set_int(VCONFKEY_STARTER_IS_FALLBACK, 0)) {
171                                 _E("Failed to set vconfkey : %s", VCONFKEY_STARTER_IS_FALLBACK);
172                         }
173
174                         if (!strcmp(status_active_get()->setappl_selected_package_name, MENU_SCREEN_PKG_NAME)) {
175                                 char *fallback_pkg;
176
177                                 fallback_pkg = vconf_get_str(VCONFKEY_STARTER_FALLBACK_PKG);
178                                 _D("fallback pkg : %s", fallback_pkg);
179                                 if (fallback_pkg) {
180                                         int status;
181
182                                         status = vconf_set_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, fallback_pkg);
183                                         free(fallback_pkg);
184                                         if (status == 0) {
185                                                 break;
186                                         }
187                                         _E("Failed to set vconfkey : %s (%d)", VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, status);
188                                 } else {
189                                         _E("Failed to get vconfkey : %s", VCONFKEY_STARTER_FALLBACK_PKG);
190                                 }
191                         }
192                 }
193
194                 home_mgr_open_home(NULL);
195                 break;
196         default:
197                 _E("False sequence [%d]", seq);
198                 break;
199         }
200
201         return 1;
202 }
203
204
205
206 static int _change_selected_package_name(status_active_key_e key, void *data)
207 {
208         char *appid = NULL;
209         int seq = status_active_get()->starter_sequence;
210
211         if (seq < 1) {
212                 _E("Sequence is not ready yet, do nothing");
213                 return 1;
214         }
215
216         _D("_change_selected_package_name is invoked");
217
218         appid = status_active_get()->setappl_selected_package_name;
219         if (!appid) {
220                 return 1;
221         }
222         _SECURE_D("pkg_name : %s", appid);
223
224         if (s_home_mgr.home_pid > 0) {
225                 char old_appid[BUF_SIZE_512] = { 0 , };
226
227                 if (aul_app_get_pkgname_bypid(s_home_mgr.home_pid, old_appid, sizeof(old_appid)) == AUL_R_OK) {
228                         if (!strcmp(appid, old_appid)) {
229                                 _D("Package is changed but same package is selected");
230                                 return 1;
231                         }
232                 }
233
234                 if (AUL_R_OK != aul_terminate_pid(s_home_mgr.home_pid)) {
235                         _D("Failed to terminate pid %d", s_home_mgr.home_pid);
236                 }
237                 s_home_mgr.home_pid = -1;
238                 s_home_mgr.dead_count = 0;
239                 if (s_home_mgr.dead_timer) {
240                         ecore_timer_del(s_home_mgr.dead_timer);
241                         s_home_mgr.dead_timer = NULL;
242                 }
243         }
244
245         home_mgr_open_home(appid);
246
247         return 1;
248 }
249
250
251
252 static void _after_launch_volume(int pid)
253 {
254         if (dbus_util_send_oomadj(pid, OOM_ADJ_VALUE_DEFAULT) < 0){
255                 _E("failed to send oom dbus signal");
256         }
257         s_home_mgr.volume_pid = pid;
258 }
259
260
261
262 static void _after_launch_indicator(int pid)
263 {
264         s_home_mgr.indicator_pid = pid;
265 }
266
267
268
269 static void _after_launch_quickpanel(int pid)
270 {
271         s_home_mgr.quickpanel_pid = pid;
272 }
273
274
275
276 static void _launch_after_home(int pid)
277 {
278         if (pid > 0) {
279                 if(dbus_util_send_oomadj(pid, OOM_ADJ_VALUE_HOMESCREEN) < 0){
280                         _E("failed to send oom dbus signal");
281                 }
282         }
283         s_home_mgr.home_pid = pid;
284 }
285
286
287
288 static void _launch_home(const char *appid)
289 {
290         const char *home_appid = NULL;
291
292         if (!appid) {
293                 home_appid = status_active_get()->setappl_selected_package_name;
294         } else {
295                 home_appid = (char *) appid;
296         }
297         ret_if(!home_appid);
298
299         process_mgr_must_launch(home_appid, HOME_TERMINATED, ISTRUE, _change_home_cb, _launch_after_home);
300 }
301
302
303
304 static void _popup_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
305 {
306         _D("popup is deleted");
307
308         s_home_mgr.popup = NULL;
309 }
310
311
312
313 static Eina_Bool _dead_timer_cb(void *data)
314 {
315         Evas_Object *popup = NULL;
316         char title[BUF_SIZE_128] = { 0, };
317         char text[BUF_SIZE_1024] = { 0, };
318         char *appid = NULL;
319
320         appid = status_active_get()->setappl_selected_package_name;
321         if (!appid) {
322                 _E("appid is NULL");
323                 return ECORE_CALLBACK_CANCEL;
324         }
325
326         _D("dead count : %s(%d)", appid, s_home_mgr.dead_count);
327
328         if (s_home_mgr.dead_count >= DEAD_TIMER_COUNT_MAX) {
329                 _D("Change homescreen package to default");
330
331                 /* set fallback status */
332                 if (vconf_set_int(VCONFKEY_STARTER_IS_FALLBACK, 1) < 0) {
333                         _E("Failed to set vconfkey : %s", VCONFKEY_STARTER_IS_FALLBACK);
334                 }
335
336                 if (vconf_set_str(VCONFKEY_STARTER_FALLBACK_PKG, appid) < 0) {
337                         _E("Failed to set vconfkey : %s", VCONFKEY_STARTER_FALLBACK_PKG);
338                 }
339
340                 strncpy(title, _("IDS_COM_POP_WARNING"), sizeof(title));
341                 title[sizeof(title) - 1] = '\0';
342
343                 snprintf(text, sizeof(text), _("IDS_IDLE_POP_UNABLE_TO_LAUNCH_PS"), appid);
344                 _D("title : %s / text : %s", title, text);
345
346                 if (!s_home_mgr.popup) {
347                         popup = popup_create(title, text);
348                         if (!popup) {
349                                 _E("Failed to create popup");
350                         } else {
351                                 s_home_mgr.popup = popup;
352                                 evas_object_event_callback_add(popup, EVAS_CALLBACK_DEL, _popup_del_cb, NULL);
353                         }
354                 }
355
356                 if (vconf_set_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, MENU_SCREEN_PKG_NAME) != 0) {
357                         _E("cannot set the vconf key as %s", MENU_SCREEN_PKG_NAME);
358                         return ECORE_CALLBACK_RENEW;
359                 }
360         }
361
362         s_home_mgr.dead_timer = NULL;
363         s_home_mgr.dead_count = 0;
364
365         return ECORE_CALLBACK_CANCEL;
366 }
367
368
369
370 void home_mgr_relaunch_homescreen(void)
371 {
372         char *appid = NULL;
373
374         if (s_home_mgr.power_off) {
375                 _E("power off");
376                 return;
377         }
378
379         appid = status_active_get()->setappl_selected_package_name;
380         if (!appid) {
381                 _E("appid is NULL");
382                 return;
383         }
384
385         s_home_mgr.dead_count++;
386         _D("home dead count : %d", s_home_mgr.dead_count);
387
388         if (!s_home_mgr.dead_timer) {
389                 _D("Add dead timer");
390                 s_home_mgr.dead_timer = ecore_timer_add(DEAD_TIMER_SEC, _dead_timer_cb, NULL);
391                 if (!s_home_mgr.dead_timer) {
392                         _E("Failed to add a dead timer");
393                 }
394         }
395
396         _launch_home(appid);
397 }
398
399
400
401 void home_mgr_relaunch_volume(void)
402 {
403         process_mgr_must_syspopup_launch(SYSPOPUPID_VOLUME, NULL, NULL, NULL, _after_launch_volume);
404 }
405
406
407
408 void home_mgr_relaunch_indicator(void)
409 {
410         process_mgr_must_launch(APPID_INDICATOR, NULL, NULL, NULL, _after_launch_indicator);
411 }
412
413
414
415 void home_mgr_relaunch_quickpanel(void)
416 {
417         process_mgr_must_launch(APPID_QUICKPANEL, NULL, NULL, NULL, _after_launch_quickpanel);
418 }
419
420
421
422 static int _power_off_cb(status_active_key_e key, void *data)
423 {
424         int val = status_active_get()->sysman_power_off_status;
425
426         if (val == VCONFKEY_SYSMAN_POWER_OFF_DIRECT || val == VCONFKEY_SYSMAN_POWER_OFF_RESTART) {
427                 s_home_mgr.power_off = 1;
428         } else {
429                 s_home_mgr.power_off = 0;
430         }
431
432         _D("power off status : %d", s_home_mgr.power_off);
433
434         return 1;
435 }
436
437
438
439 static Eina_Bool _launch_apps_idler_cb(void *data)
440 {
441         process_mgr_must_syspopup_launch(SYSPOPUPID_VOLUME, NULL, NULL, NULL, _after_launch_volume);
442         process_mgr_must_launch(APPID_INDICATOR, NULL, NULL, NULL, _after_launch_indicator);
443         process_mgr_must_launch(APPID_QUICKPANEL, NULL, NULL, NULL, _after_launch_quickpanel);
444
445         return ECORE_CALLBACK_CANCEL;
446 }
447
448
449
450 void home_mgr_init(void *data)
451 {
452         _D( "[MENU_DAEMON]home_mgr_init is invoked");
453
454         status_active_register_cb(STATUS_ACTIVE_KEY_STARTER_SEQUENCE, _show_home_cb, NULL);
455         status_active_register_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb, NULL);
456         status_active_register_cb(STATUS_ACTIVE_KEY_SETAPPL_SELECTED_PACKAGE_NAME, _change_selected_package_name, NULL);
457         _change_selected_package_name(STATUS_ACTIVE_KEY_SETAPPL_SELECTED_PACKAGE_NAME, NULL);
458
459         ecore_idler_add(_launch_apps_idler_cb, NULL);
460 }
461
462
463
464 void home_mgr_fini(void)
465 {
466         if (s_home_mgr.volume_pid > 0) {
467                 process_mgr_terminate_app(s_home_mgr.volume_pid, 1);
468                 s_home_mgr.volume_pid = -1;
469         }
470
471         status_active_unregister_cb(STATUS_ACTIVE_KEY_STARTER_SEQUENCE, _show_home_cb);
472         status_active_unregister_cb(STATUS_ACTIVE_KEY_SYSMAN_POWER_OFF_STATUS, _power_off_cb);
473         status_active_unregister_cb(STATUS_ACTIVE_KEY_SETAPPL_SELECTED_PACKAGE_NAME, _change_selected_package_name);
474 }
475
476
477
478 // End of a file