summaryrefslogtreecommitdiff
path: root/src/menu_daemon.c
blob: b400b5e46eaaebc07757267d242ed97b4a3f09f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
 /*
  * Copyright 2012  Samsung Electronics Co., Ltd
  *
  * Licensed under the Flora License, Version 1.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *     http://floralicense.org/license/
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */



#include <ail.h>
#include <aul.h>
#include <db-util.h>
#include <Elementary.h>
#include <errno.h>
#include <fcntl.h>
#include <pkgmgr-info.h>
#include <stdio.h>
#include <sysman.h>
#include <syspopup_caller.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <vconf.h>

#include "hw_key.h"
#include "pkg_event.h"
#include "util.h"
#include "xmonitor.h"


int errno;


#define QUERY_UPDATE_NAME "UPDATE app_info SET name='%s' where package='%s';"
#define SAT_DESKTOP_FILE "/opt/share/applications/org.tizen.sat-ui.desktop"
#define RELAUNCH_INTERVAL 100*1000
#define RETRY_MAXCOUNT 30

static struct info {
	pid_t home_pid;
	pid_t volume_pid;
	int power_off;
} s_info = {
	.home_pid = -1,
	.volume_pid = -1,
	.power_off = 0,
};



#define RETRY_COUNT 5
int menu_daemon_open_app(const char *pkgname)
{
	register int i;
	int r = AUL_R_ETIMEOUT;
	for (i = 0; AUL_R_ETIMEOUT == r && i < RETRY_COUNT; i ++) {
		r = aul_open_app(pkgname);
		if (0 <= r) return r;
		else {
			_D("aul_open_app error(%d)", r);
			_F("cannot open an app(%s) by %d", pkgname, r);
		}
		usleep(500000);
	}

	return r;
}



int menu_daemon_launch_app(const char *pkgname, bundle *b)
{
	register int i;
	int r = AUL_R_ETIMEOUT;
	for (i = 0; AUL_R_ETIMEOUT == r && i < RETRY_COUNT; i ++) {
		r = aul_launch_app(pkgname, b);
		if (0 <= r) return r;
		else {
			_D("aul_launch_app error(%d)", r);
			_F("cannot launch an app(%s) by %d", pkgname, r);
		}
		usleep(500000);
	}

	return r;
}



bool menu_daemon_is_homescreen(pid_t pid)
{
	if (s_info.home_pid == pid) return true;
	return false;
}



inline char *menu_daemon_get_selected_pkgname(void)
{
	char *pkgname = NULL;

	pkgname = vconf_get_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME);
	retv_if(NULL == pkgname, NULL);

	return pkgname;
}



static bool _exist_package(char *pkgid)
{
	int ret = 0;
	pkgmgrinfo_pkginfo_h handle = NULL;

	ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
	if (PMINFO_R_OK != ret || NULL == handle) {
		_D("%s doesn't exist in this binary", pkgid);
		return false;
	}

	pkgmgrinfo_pkginfo_destroy_pkginfo(handle);

	return true;
}



inline void menu_daemon_open_homescreen(const char *pkgname)
{
	char *homescreen = NULL;
	char *tmp = NULL;

	system("echo -e '[${_G}menu-daemon launches home-screen${C_}]' > /dev/kmsg");

	if (NULL == pkgname) {
		tmp = menu_daemon_get_selected_pkgname();
		ret_if(NULL == tmp);
		homescreen = tmp;
	} else {
		homescreen = (char *) pkgname;
	}

	syspopup_destroy_all();

	int ret;
	ret = menu_daemon_open_app(homescreen);
	_D("can%s launch %s now. (%d)", ret < 0 ? "not" : "", homescreen, ret);
	if (ret < 0 && strcmp(homescreen, HOME_SCREEN_PKG_NAME) && _exist_package(HOME_SCREEN_PKG_NAME)) {
		_E("cannot launch package %s", homescreen);

		if (0 != vconf_set_str(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, HOME_SCREEN_PKG_NAME)) {
			_E("Cannot set value(%s) into key(%s)", VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, HOME_SCREEN_PKG_NAME);
		}

		while (AUL_R_ETIMEOUT == ret) {
			_E("Failed to open a default home, %s", HOME_SCREEN_PKG_NAME);
			ret = menu_daemon_open_app(HOME_SCREEN_PKG_NAME);
		}
	}

	if (ret < 0) {
		_E("Critical! Starter cannot launch anymore[%d]", ret);
		_F("Critical! Starter cannot launch anymore[%d]", ret);
	}

	s_info.home_pid = ret;
	if (ret > 0) {
		if (-1 == sysconf_set_mempolicy_bypid(ret, OOM_IGNORE)) {
			_E("Cannot set the memory policy for Home-screen(%d)", ret);
		} else {
			_D("Set the memory policy for Home-screen(%d)", ret);
		}
	}

	if (tmp) free(tmp);
}



static void _show_cb(keynode_t* node, void *data)
{
	int seq;

	_D("[MENU_DAEMON] _show_cb is invoked");

	if (node) {
		seq = vconf_keynode_get_int(node);
	} else {
		if (vconf_get_int(VCONFKEY_STARTER_SEQUENCE, &seq) < 0) {
			_E("Failed to get sequence info");
			return;
		}
	}

	switch (seq) {
		case 0:
			if (s_info.home_pid > 0) {
				int pid;
				_D("pid[%d] is terminated.", s_info.home_pid);
				pid = s_info.home_pid;
				s_info.home_pid = -1;

				if (aul_terminate_pid(pid) != AUL_R_OK)
					_E("Failed to terminate %d", s_info.home_pid);
			}
			break;
		case 1:
			menu_daemon_open_homescreen(NULL);
			break;
		default:
			_E("False sequence [%d]", seq);
			break;
	}

	return;
}



static void _pkg_changed(keynode_t* node, void *data)
{
	char *pkgname;
	int seq;

	if (vconf_get_int(VCONFKEY_STARTER_SEQUENCE, &seq) < 0) {
		_E("Do nothing, there is no sequence info yet");
		return;
	}

	if (seq < 1) {
		_E("Sequence is not ready yet, do nothing");
		return;
	}

	_D("_pkg_changed is invoked");

	pkgname = menu_daemon_get_selected_pkgname();
	if (!pkgname)
		return;

	_D("pkg_name : %s", pkgname);

	if (s_info.home_pid > 0) {
		char old_pkgname[256];

		if (aul_app_get_pkgname_bypid(s_info.home_pid, old_pkgname, sizeof(old_pkgname)) == AUL_R_OK) {
			if (!strcmp(pkgname, old_pkgname)) {
				_D("Package is changed but same package is selected");
				free(pkgname);
				return;
			}
		}

		if (AUL_R_OK != aul_terminate_pid(s_info.home_pid))
			_D("Failed to terminate pid %d", s_info.home_pid);
	} else {
		/* If there is no running home */
		menu_daemon_open_homescreen(pkgname);
	}

	free(pkgname);
	return;
}



static void _launch_volume(void)
{
	int pid;
	int i;
	_D("_launch_volume");

	for (i=0; i<RETRY_MAXCOUNT; i++)
	{
		pid = syspopup_launch("volume", NULL);

		_D("syspopup_launch(volume), pid = %d", pid);

		if (pid <0) {
			_D("syspopup_launch(volume)is failed [%d]times", i);
			usleep(RELAUNCH_INTERVAL);
		} else {
			s_info.volume_pid = pid;
			return;
		}
	}
}

int menu_daemon_check_dead_signal(int pid)
{
	if (s_info.power_off) {
		_D("Power off. ignore dead cb\n");
		return 0;
	}

	_D("Process %d is termianted", pid);

	if (pid < 0)
		return 0;

	if (pid == s_info.home_pid) {
		char *pkgname = NULL;
		pkgname = menu_daemon_get_selected_pkgname();
		retv_if(NULL == pkgname, 0);

		_D("pkg_name : %s", pkgname);
		menu_daemon_open_homescreen(pkgname);
		free(pkgname);
	} else if (pid == s_info.volume_pid) {
		_launch_volume();
	} else {
		_D("Unknown process, ignore it (pid %d, home pid %d)",
				pid, s_info.home_pid);
	}


	return 0;
}



void menu_daemon_init(void *data)
{
	_D( "[MENU_DAEMON]menu_daemon_init is invoked");

	aul_launch_init(NULL,NULL);

	create_key_window();
	if (xmonitor_init() < 0) _E("cannot init xmonitor");

	pkg_event_init();
	_launch_volume();

	if (unlink(SAT_DESKTOP_FILE) != 0)
		_E("cannot remove sat-ui desktop.");

	if (vconf_notify_key_changed(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, _pkg_changed, NULL) < 0)
		_E("Failed to add the callback for package change event");

	if (vconf_notify_key_changed(VCONFKEY_STARTER_SEQUENCE, _show_cb, NULL) < 0)
		_E("Failed to add the callback for show event");

	_pkg_changed(NULL, NULL);
	vconf_set_int(VCONFKEY_IDLE_SCREEN_LAUNCHED, VCONFKEY_IDLE_SCREEN_LAUNCHED_TRUE);
}



void menu_daemon_fini(void)
{
	if (vconf_ignore_key_changed(VCONFKEY_SETAPPL_SELECTED_PACKAGE_NAME, _pkg_changed) < 0)
		_E("Failed to ignore the callback for package change event");

	if (vconf_ignore_key_changed(VCONFKEY_STARTER_SEQUENCE, _show_cb) < 0)
		_E("Failed to ignore the callback for show event");

	xmonitor_fini();
	pkg_event_fini();
	destroy_key_window();
}



// End of a file