summaryrefslogtreecommitdiff
path: root/boot-mgr/starter.c
blob: 98148a9a9406062d5748f934917eef29455d0631 (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
/*
 * 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://www.tizenopensource.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 <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 <signal.h>


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

#define DEFAULT_THEME "tizen"

#define HIB_CAPTURING "/opt/etc/.hib_capturing"
#define STR_STARTER_READY "/tmp/hibernation/starter_ready"

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

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

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

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

	th = elm_theme_new();
	_DBG("theme vconf[%s]\n set[%s]\n", vstr, theme);
	elm_theme_set(th, theme);

	if (vstr)
		free(vstr);
}
static int _launch_pwlock(void)
{
	int r;

	_DBG("%s", __func__);

	r = aul_launch_app("org.tizen.pwlock", 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("org.tizen.pwlock", 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 void _signal_handler(int signum, siginfo_t *info, void *unused)
{
    _DBG("_signal_handler : Terminated...");
    elm_exit();
}
static void _heynoti_event_power_off(void *data)
{
    _DBG("_heynoti_event_power_off : Terminated...");
    elm_exit();
}

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

	struct sigaction act;
	act.sa_sigaction = _signal_handler;
	act.sa_flags = SA_SIGINFO;

	int ret = sigemptyset(&act.sa_mask);
	if (ret < 0) {
		_ERR("Failed to sigemptyset[%s]", strerror(errno));
	}
	ret = sigaddset(&act.sa_mask, SIGTERM);
	if (ret < 0) {
		_ERR("Failed to sigaddset[%s]", strerror(errno));
	}
	ret = sigaction(SIGTERM, &act, NULL);
	if (ret < 0) {
		_ERR("Failed to sigaction[%s]", strerror(errno));
	}

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

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

	lock_menu_screen();
	_set_elm_theme();

	_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);
	}

	fd1 = open(STR_STARTER_READY, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
	if (fd1 > 0) {
		_DBG("Hibernation ready.\n");
		close(fd1);
	}

	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;

	int heyfd = heynoti_init();
	if (heyfd < 0) {
		_ERR("Failed to heynoti_init[%d]", heyfd);
	}

	int ret = heynoti_subscribe(heyfd, "power_off_start", _heynoti_event_power_off, NULL);
	if (ret < 0) {
		_ERR("Failed to heynoti_subscribe[%d]", ret);
	}
	ret = heynoti_attach_handler(heyfd);
	if (ret < 0) {
		_ERR("Failed to heynoti_attach_handler[%d]", ret);
	}

	elm_init(argc, argv);

	_init(&ad);

	elm_run();

	_fini(&ad);

	elm_shutdown();

	return 0;
}