2 * Copyright 2012 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org/license/
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.
22 #include <Elementary.h>
26 #include <syspopup_caller.h>
27 #include <sys/types.h>
33 #include "pkg_event.h"
41 #define QUERY_UPDATE_NAME "UPDATE app_info SET name='%s' where package='%s';"
42 #define SAT_DESKTOP_FILE "/opt/share/applications/org.tizen.sat-ui.desktop"
43 #define RELAUNCH_INTERVAL 100*1000
44 #define RETRY_MAXCOUNT 30
58 bool menu_daemon_is_homescreen(pid_t pid)
60 if (s_info.home_pid == pid) return true;
66 static inline char *_get_selected_pkgname(void)
70 pkgname = vconf_get_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME);
72 _E("Cannot get pkgname from vconf.");
74 pkgname = strdup(HOME_SCREEN_PKG_NAME);
76 _E("strdup error for pkgname, %s", strerror(errno));
86 static inline void _open_homescreen(const char *pkgname)
89 char *homescreen = (char *) pkgname;
91 system("echo -e '[${_G}menu-daemon launches home-screen${C_}]' > /dev/kmsg");
92 ret = aul_open_app(homescreen);
93 _D("can%s launch %s now. (%d)", ret < 0 ? "not" : "", homescreen, ret);
94 if (ret < 0 && strcmp(homescreen, HOME_SCREEN_PKG_NAME)) {
95 _E("cannot launch package %s", homescreen);
98 ret = aul_open_app(HOME_SCREEN_PKG_NAME);
100 _E("Failed to open a default home, %s(err:%d)", HOME_SCREEN_PKG_NAME, ret);
105 s_info.home_pid = ret;
107 if (-1 == sysconf_set_mempolicy_bypid(ret, OOM_IGNORE)) {
108 _E("Cannot set the memory policy for Home-screen(%d)", ret);
110 _E("Set the memory policy for Home-screen(%d)", ret);
117 static void _show_cb(keynode_t* node, void *data)
122 _D("[MENU_DAEMON] _show_cb is invoked");
124 pkgname = _get_selected_pkgname();
129 seq = vconf_keynode_get_int(node);
131 if (vconf_get_int(VCONFKEY_STARTER_SEQUENCE, &seq) < 0) {
132 _E("Failed to get sequence info");
140 if (s_info.home_pid > 0) {
142 _D("pid[%d] is terminated.", s_info.home_pid);
144 pid = s_info.home_pid;
145 s_info.home_pid = -1;
147 if (aul_terminate_pid(pid) != AUL_R_OK)
148 _E("Failed to terminate %d", s_info.home_pid);
152 _open_homescreen(pkgname);
155 _E("False sequence [%d]", seq);
166 static void _pkg_changed(keynode_t* node, void *data)
171 if (vconf_get_int(VCONFKEY_STARTER_SEQUENCE, &seq) < 0) {
172 _E("Do nothing, there is no sequence info yet");
177 _E("Sequence is not ready yet, do nothing");
181 _D("_pkg_changed is invoked");
183 pkgname = _get_selected_pkgname();
187 _D("pkg_name : %s", pkgname);
189 if (s_info.home_pid > 0) {
190 char old_pkgname[256];
192 if (aul_app_get_pkgname_bypid(s_info.home_pid, old_pkgname, sizeof(old_pkgname)) == AUL_R_OK) {
193 if (!strcmp(pkgname, old_pkgname)) {
194 _D("Package is changed but same package is selected");
200 if (aul_terminate_pid(s_info.home_pid) != AUL_R_OK)
201 _D("Failed to terminate pid %d", s_info.home_pid);
203 _open_homescreen(pkgname);
210 static void _launch_volume(void)
214 _D("_launch_volume");
216 for (i=0; i<RETRY_MAXCOUNT; i++)
218 pid = syspopup_launch("volume", NULL);
220 _D("syspopup_launch(volume), pid = %d", pid);
223 _D("syspopup_launch(volume)is failed [%d]times", i);
224 usleep(RELAUNCH_INTERVAL);
226 s_info.volume_pid = pid;
232 int menu_daemon_check_dead_signal(int pid)
236 if (s_info.power_off) {
237 _D("Power off. ignore dead cb\n");
241 _D("Process %d is termianted", pid);
246 pkgname = _get_selected_pkgname();
250 if (pid == s_info.home_pid) {
251 _D("pkg_name : %s", pkgname);
252 _open_homescreen(pkgname);
253 } else if (pid == s_info.volume_pid) {
256 _D("Unknown process, ignore it (pid %d, home pid %d)",
257 pid, s_info.home_pid);
267 void menu_daemon_init(void *data)
269 _D( "[MENU_DAEMON]menu_daemon_init is invoked");
271 aul_launch_init(NULL,NULL);
274 if (xmonitor_init() < 0) _E("cannot init xmonitor");
279 if (unlink(SAT_DESKTOP_FILE) != 0)
280 _E("cannot remove sat-ui desktop.");
282 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, _pkg_changed, NULL) < 0)
283 _E("Failed to add callback for package change event");
285 if (vconf_notify_key_changed(VCONFKEY_STARTER_SEQUENCE, _show_cb, NULL) < 0)
286 _E("Failed to add callback for show event");
288 _pkg_changed(NULL, NULL);
289 vconf_set_int(VCONFKEY_IDLE_SCREEN_LAUNCHED, VCONFKEY_IDLE_SCREEN_LAUNCHED_TRUE);
294 void menu_daemon_fini(void)
296 vconf_ignore_key_changed(VCONFKEY_STARTER_SEQUENCE, _show_cb);
297 vconf_ignore_key_changed(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, _pkg_changed);
301 destroy_key_window();