summaryrefslogtreecommitdiff
path: root/boot-mgr/starter.c
blob: 4e07eeddbd4c67cf20d78dbe1beb52fdeee27cbf (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
/*
 *  starter
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Seungtaek Chung <seungtaek.chung@samsung.com>, Mi-Ju Lee <miju52.lee@samsung.com>, Xi Zhichan <zhichan.xi@samsung.com>
 *
 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 <Elementary.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <poll.h>

#include <aul.h>
#include <vconf.h>
#include <heynoti.h>

#include "starter.h"
#include "x11.h"
#include "lock-daemon.h"
#include "lockd-debug.h"

#ifndef PACKAGE_NAME
#define PACKAGE_NAME "org.tizen.starter"
#endif

#define PWLOCK_PKG "org.tizen.pwlock"
#define VCONFKEY_START "memory/startapps/sequence"
#define PWLOCK_FIRST_BOOT "db/setting/pwlock_boot"
#define DEFAULT_THEME "tizen"
#define PWLOCK_PATH "/opt/apps/org.tizen.pwlock/bin/pwlock"
#define HIB_CAPTURING "/opt/etc/.hib_capturing"

static void lock_menu_screen(void)
{
	vconf_set_int(VCONFKEY_START, 0);
}

static void unlock_menu_screen(void)
{
	int r;
	int show_menu;

	show_menu = 0;
	r = vconf_get_int(VCONFKEY_START, &show_menu);
	if (r || !show_menu) {
		vconf_set_int(VCONFKEY_START, 1);
	}
}

static void _set_elm_theme(void)
{
	char *vstr;
	char *theme;
	vstr = vconf_get_str(VCONFKEY_SETAPPL_WIDGET_THEME_STR);
	if (vstr == NULL)
		theme = DEFAULT_THEME;
	else
		theme = vstr;

	_DBG("theme vconf[%s]\n set[%s]\n", vstr, theme);
	elm_theme_all_set(theme);

	if (vstr)
		free(vstr);
}

static void _set_elm_entry(void)
{
	int v;
	int r;

	r = vconf_get_bool(VCONFKEY_SETAPPL_AUTOCAPITAL_ALLOW_BOOL, &v);
	if (!r) {
		prop_int_set("ENLIGHTENMENT_AUTOCAPITAL_ALLOW", v);
		_DBG("vconf autocatipal[%d]", v);
	}

	r = vconf_get_bool(VCONFKEY_SETAPPL_AUTOPERIOD_ALLOW_BOOL, &v);
	if (!r) {
		prop_int_set("ENLIGHTENMENT_AUTOPERIOD_ALLOW", v);
		_DBG("vconf autoperiod[%d]", v);
	}
}

static int _launch_pwlock(void)
{
	int r;

	_DBG("%s", __func__);

	r = aul_launch_app(PWLOCK_PKG, NULL);
	if (r < 0) {
		_ERR("PWLock launch error: error(%d)", r);
		if (r == AUL_R_ETIMEOUT) {
			_DBG("Launch pwlock is failed for AUL_R_ETIMEOUT, again launch pwlock");
			r = aul_launch_app(PWLOCK_PKG, NULL);
			if (r < 0) {
				_ERR("2'nd PWLock launch error: error(%d)", r);
				return -1;
			} else {
				_DBG("Launch pwlock");
				return 0;
			}
		} else {
			return -1;
		}
	} else {
		_DBG("Launch pwlock");
		return 0;
	}
}

static void hib_leave(void *data)
{
	struct appdata *ad = data;
	if (ad == NULL) {
		fprintf(stderr, "Invalid argument: appdata is NULL\n");
		return;
	}

	_DBG("%s", __func__);
	_set_elm_theme();
	start_lock_daemon();
	if (_launch_pwlock() < 0) {
		_ERR("launch pwlock error");
	}
}

static int add_noti(struct appdata *ad)
{
	int fd;
	int r;
	_DBG("%s %d\n", __func__, __LINE__);

	if (ad == NULL) {
		fprintf(stderr, "Invalid argument: appdata is NULL\n");
		return -1;
	}

	fd = heynoti_init();
	if (fd == -1) {
		_ERR("Noti init error");
		return -1;
	}
	ad->noti = fd;

	r = heynoti_subscribe(fd, "HIBERNATION_PRELEAVE", hib_leave, ad);
	if (r == -1) {
		_ERR("Noti subs error");
		return -1;
	}

	r = heynoti_attach_handler(fd);
	if (r == -1) {
		_ERR("Noti attach error");
		return -1;
	}

	_DBG("Waiting for hib leave");
	_DBG("%s %d\n", __func__, __LINE__);

	return 0;
}

static int _init(struct appdata *ad)
{
	int fd;
	int r;

	memset(ad, 0, sizeof(struct appdata));

	ad->noti = -1;
	gettimeofday(&ad->tv_start, NULL);

	lock_menu_screen();
	_set_elm_theme();
	_set_elm_entry();

	_DBG("%s %d\n", __func__, __LINE__);
	fd = open(HIB_CAPTURING, O_RDONLY);
	_DBG("fd = %d\n", fd);
	if (fd == -1) {
		_DBG("fd = %d\n", fd);
		start_lock_daemon();
		if (_launch_pwlock() < 0) {
			_ERR("launch pwlock error");
		}
		r = 0;
	} else {
		close(fd);
		r = add_noti(ad);
	}

	if (vconf_set_int("memory/hibernation/starter_ready", 1))
		_ERR("vconf_set_int FAIL");
	else
		_ERR("vconf_set_int OK\n");

	return r;
}

static void _fini(struct appdata *ad)
{
	struct timeval tv, res;

	if (ad == NULL) {
		fprintf(stderr, "Invalid argument: appdata is NULL\n");
		return;
	}
	if (ad->noti != -1)
		heynoti_close(ad->noti);

	unlock_menu_screen();

	gettimeofday(&tv, NULL);
	timersub(&tv, &ad->tv_start, &res);
	_DBG("Total time: %d.%06d sec\n", (int)res.tv_sec, (int)res.tv_usec);
}

int main(int argc, char *argv[])
{
	struct appdata ad;

	set_window_scale();

	elm_init(argc, argv);

	_init(&ad);

	elm_run();

	_fini(&ad);

	elm_shutdown();

	return 0;
}