summaryrefslogtreecommitdiff
path: root/src/starter.c
blob: 9a6a94624d17197fc7ddaf090b8f227c74e03e3d (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
 /*
  * Copyright 2012  Samsung Electronics Co., Ltd
  *
  * Licensed under the Flora License, Version 1.1 (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 <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 <system_info.h>

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

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

#define DEFAULT_THEME "tizen"
#define PWLOCK_PATH "/usr/apps/org.tizen.pwlock/bin/pwlock"
#define PWLOCK_PKG_NAME "org.tizen.pwlock"
#define QP_EMUL_STR      "Emulator"

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 _check_emul()
{
	int is_emul = 0;
	char *info = NULL;

	if (system_info_get_value_string(SYSTEM_INFO_KEY_MODEL, &info) == 0) {
		if (info == NULL) return 0;
		if (!strncmp(QP_EMUL_STR, info, strlen(info))) {
			is_emul = 1;
		}
	}

	if (info != NULL) free(info);

	return is_emul;
}

static int _launch_pwlock(void)
{
	int r;

	_DBG("%s", __func__);

	if(_check_emul()) {
		_DBG("Emulator => skip pwlock");
		vconf_set_int(VCONFKEY_STARTER_SEQUENCE, 1);
		return 0;
	}

	r = aul_launch_app(PWLOCK_PKG_NAME, 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_NAME, 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 _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 Eina_Bool _init_idle(void *data)
{
	_DBG("%s %d\n", __func__, __LINE__);
	if (_launch_pwlock() < 0) {
		_ERR("launch pwlock error");
	}
	menu_daemon_init(NULL);

	return ECORE_CALLBACK_CANCEL;
}

static void _lock_state_cb(keynode_t * node, void *data)
{
	_DBG("%s %d\n", __func__, __LINE__);
	WRITE_FILE_LOG("%s", "Lock state is changed!");

	if (_launch_pwlock() < 0) {
		_ERR("launch pwlock error");
	}
	menu_daemon_init(NULL);
	if (vconf_ignore_key_changed(VCONFKEY_IDLE_LOCK_STATE,
	     _lock_state_cb) != 0) {
		LOCKD_DBG("Fail to unregister");
	}
}

static void _init(struct appdata *ad)
{
	int r;
	struct sigaction act;

	memset(&act,0x00,sizeof(struct sigaction));
	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));

	gettimeofday(&ad->tv_start, NULL);

	lock_menu_screen();
	_set_elm_theme();

	_DBG("%s %d\n", __func__, __LINE__);

	r = start_lock_daemon(TRUE);
	if (r == 1) {
		if (vconf_notify_key_changed(VCONFKEY_IDLE_LOCK_STATE,
		     _lock_state_cb, NULL) != 0) {
			_ERR("[Error] vconf notify : lock state");
			ecore_timer_add(1.5, _init_idle, NULL);
		}
	} else {
		if (_launch_pwlock() < 0) {
			_ERR("launch pwlock error");
		}
		menu_daemon_init(NULL);
	}
}

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

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

	unlock_menu_screen();
	menu_daemon_fini();

	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;

	WRITE_FILE_LOG("%s", "Main function is started in starter");

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

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