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://www.tizenopensource.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 <sys/types.h>
31 #include "desktop_to_db.h"
34 #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 HIBERNATION_DIR "/tmp/hibernation"
44 #define HIBERNATION_READY "/tmp/hibernation/menuscreen_ready"
48 // Define prototype of the "hidden API of AUL"
49 extern int aul_listen_app_dead_signal(int (*func)(int signal, void *data), void *data);
63 static inline char *_get_selected_pkgname(void)
67 pkgname = vconf_get_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME);
69 _E("Cannot get pkgname from vconf.");
71 if (0 != vconf_set_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, HOME_SCREEN_PKG_NAME)) {
72 _E("Cannot set value(%s) into key(%s)", VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, HOME_SCREEN_PKG_NAME);
75 pkgname = strdup(HOME_SCREEN_PKG_NAME);
77 _E("strdup error for pkgname, %s", strerror(errno));
87 static inline void _open_homescreen(const char *pkgname)
90 char *homescreen = (char *) pkgname;
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 if (0 != vconf_set_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, HOME_SCREEN_PKG_NAME)) {
96 _E("Cannot set value(%s) into key(%s)", VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, HOME_SCREEN_PKG_NAME);
99 ret = aul_open_app(HOME_SCREEN_PKG_NAME);
101 _E("Failed to open a default home, %s", HOME_SCREEN_PKG_NAME);
103 homescreen = HOME_SCREEN_PKG_NAME;
112 static void _show_cb(keynode_t* node, void *data)
117 _D("[MENU_DAEMON] _show_cb is invoked");
119 pkgname = _get_selected_pkgname();
124 seq = vconf_keynode_get_int(node);
126 if (vconf_get_int(VCONFKEY_STARTER_SEQUENCE, &seq) < 0) {
127 _E("Failed to get sequence info");
135 if (s_info.pid > 0) {
137 _D("pid[%d] is terminated.", s_info.pid);
140 s_info.pid = -1; /* to freeze the dead_cb */
142 if (aul_terminate_pid(pid) != AUL_R_OK)
143 _E("Failed to terminate %d", s_info.pid);
147 _open_homescreen(pkgname);
150 _E("False sequence [%d]", seq);
161 static void _pkg_changed(keynode_t* node, void *data)
166 if (vconf_get_int(VCONFKEY_STARTER_SEQUENCE, &seq) < 0) {
167 _E("Do nothing, there is no sequence info yet");
172 _E("Sequence is not ready yet, do nothing");
176 _D("_pkg_changed is invoked");
178 pkgname = _get_selected_pkgname();
182 _D("pkg_name : %s", pkgname);
184 if (s_info.pid > 0) {
185 char old_pkgname[256];
187 if (aul_app_get_pkgname_bypid(s_info.pid, old_pkgname, sizeof(old_pkgname)) == AUL_R_OK) {
188 if (!strcmp(pkgname, old_pkgname)) {
189 _D("Package is changed but same package is selected");
195 if (aul_terminate_pid(s_info.pid) != AUL_R_OK)
196 _D("Failed to terminate pid %d", s_info.pid);
198 /* If there is no running home */
199 _open_homescreen(pkgname);
202 /* NOTE: Dead callback will catch the termination of a current menuscreen
203 * _open_homescreen(pkgname);
211 static int _dead_cb(int pid, void *data)
215 if (s_info.power_off) {
216 _D("Power off. ignore dead cb\n");
220 _D("Process %d is termianted", pid);
225 pkgname = _get_selected_pkgname();
229 _D("pkg_name : %s", pkgname);
232 _open_homescreen(pkgname);
239 static void _hibernation_preleave_cb(void *data)
241 _D( "[MENU_DAEMON]_hibernation_preleave_cb is invoked");
243 aul_launch_init(NULL,NULL);
244 aul_listen_app_dead_signal(_dead_cb, NULL);
249 if (unlink(SAT_DESKTOP_FILE) != 0)
250 _E("cannot remove sat-ui desktop.");
252 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, _pkg_changed, NULL) < 0)
253 _E("Failed to add callback for package change event");
255 if (vconf_notify_key_changed(VCONFKEY_STARTER_SEQUENCE, _show_cb, NULL) < 0)
256 _E("Failed to add callback for show event");
258 _pkg_changed(NULL, NULL);
259 // THIS ROUTINE IS FOR SAT.
260 vconf_set_int(VCONFKEY_IDLE_SCREEN_LAUNCHED, VCONFKEY_IDLE_SCREEN_LAUNCHED_TRUE);
265 static void power_off_cb(void *data)
267 _D("heynoti power off\n");
268 s_info.power_off = 1;
273 int elm_main(int argc, char *argv[])
279 elm_init(argc, argv);
283 fprintf(stderr, "heynoti_init\n");
285 ret = heynoti_subscribe(fd, "power_off_start", power_off_cb, NULL);
287 fprintf(stderr, "heynoti_subscribe\n");
289 ret = system("/usr/bin/ail_initdb");
291 _D("Cannot execute ail_initdb");
293 fp = fopen("/opt/etc/.hib_capturing", "r");
295 fprintf(stderr, "[MENU_DAEMON]hib_capturing file is not found\n");
296 _hibernation_preleave_cb(NULL);
298 fprintf(stderr,"\n\n\n Menu Daemon enter %s hey noti init\n", __func__);
300 fprintf(stderr, "[MENU_DAEMON]\n\n\n Hey Noti Init Failed\n");
302 if (heynoti_subscribe(fd, "HIBERNATION_PRELEAVE", _hibernation_preleave_cb, NULL)) {
303 fprintf(stderr,"Heynoti subscribe is failed\n");
305 fprintf(stderr,"Heynoti subscribe is done\n");
307 if (mkdir(HIBERNATION_DIR, 0777) != -1)
308 _D("Make a directory - [%s]", HIBERNATION_DIR);
310 fd = creat(HIBERNATION_READY, 0644);
312 _D("Create a file for hibernation_ready");
321 if (heynoti_attach_handler(fd))
322 fprintf(stderr,"Heynoti attach handler is failed\n");
324 fprintf(stderr,"Heynoti attach handler is done\n");
327 destroy_key_window();