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