summaryrefslogtreecommitdiff
path: root/src/starter.c
blob: 1be63e4f2ee3852eca4db67e7c060c95fef80c22 (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
/*
 *  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 <errno.h>
#include <string.h>

#include <vconf.h>
#include <signal.h>

#include "starter.h"
#include "starter-util.h"
#include "x11.h"
#include "lockd-debug.h"
#include "hw_key.h"
#include "util.h"

int errno;

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

#define DEFAULT_THEME "tizen"

#define W_LAUNCHER_PKGNAME "org.tizen.w-launcher-app"
#define W_LOCKSCREEN_PKGNAME "org.tizen.w-lockscreen"

#ifdef FEATURE_TIZENW2
#define SETUP_WIZARD_PKGNAME "org.tizen.b2-setup-wizard"
#else
#define SETUP_WIZARD_PKGNAME "org.tizen.b2-setup-wizard"
#endif


static void _signal_handler(int signum, siginfo_t *info, void *unused)
{
    _DBG("_signal_handler : Terminated...");
    elm_exit();
}

int w_launch_app(char *pkgname, bundle *b)
{
	int r = AUL_R_OK;

	_SECURE_D("w_launch_app:[%s]", pkgname);

	r = aul_launch_app(pkgname, b);

	if (r < 0) {
		_ERR("launch failed [%s] ret=[%d]", pkgname, r);
	}

	return r;
}

static int _w_app_dead_cb(int pid, void *data)
{
	_DBG("app dead cb call! (pid : %d)", pid);

	struct appdata *ad = (struct appdata *)data;

	if (pid == ad->launcher_pid) {
		_ERR("w-launcher-app (pid:%d) is destroyed.", pid);
		ad->launcher_pid = w_launch_app(W_LAUNCHER_PKGNAME, NULL);
	}

	return 0;
}

static int _w_check_first_boot(void)
{
	int is_first = 0;
	int ret = 0;

#if 1 // NOT YET define vconfkey from setting  "VCONFKEY_SETUP_WIZARD_FIRST_BOOT"
	ret = vconf_get_bool(VCONFKEY_SETUP_WIZARD_FIRST_BOOT, &is_first);
	if (ret < 0){
		_ERR("can't get vconfkey value of [%s], ret=[%d]", VCONFKEY_SETUP_WIZARD_FIRST_BOOT, ret);
		is_first = 0;
	} else if (is_first == 1) {
		_ERR("[%s] value is [%d], first booting..!!", VCONFKEY_SETUP_WIZARD_FIRST_BOOT, is_first);
	}
#endif

	return is_first;
}

static Eina_Bool _w_starter_idler_cb(void *data)
{
	struct appdata *ad = (struct appdata *)data;

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

	ad->launcher_pid = w_launch_app(W_LAUNCHER_PKGNAME, NULL);

	return ECORE_CALLBACK_CANCEL;
}


#define TEMP_VCONFKEY_LOCK_TYPE "db/setting/lock_type"
static void _w_wms_changed_cb(keynode_t* node, void *data)
{
	int wms_state = -1;
	int lock_type = -1;
	int test_mode = -1;
	struct appdata *ad = (struct appdata *)data;

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

	if (node) {
		wms_state = vconf_keynode_get_bool(node);
	} else {
		if (vconf_get_bool(VCONFKEY_WMS_WMANAGER_CONNECTED, &wms_state) < 0) {
			_ERR("Failed to get %s", VCONFKEY_WMS_WMANAGER_CONNECTED);
			return;
		}
	}
	_DBG("WMS key value:[%d], previous state:[%d]", wms_state, ad->wms_connected);

	vconf_get_int(TEMP_VCONFKEY_LOCK_TYPE, &lock_type);
	vconf_get_int(VCONFKEY_TESTMODE_SCREEN_LOCK, &test_mode);

	if ((lock_type != 1) || (test_mode == VCONFKEY_TESTMODE_SCREEN_LOCK_DISABLE)) {
		ad->wms_connected = wms_state;
		return;
	}

	if (wms_state == FALSE) {
		if (ad->wms_connected == TRUE) {
			_ERR("WMS connect state is changed from [%d] to [%d]", ad->wms_connected, wms_state);
			w_launch_app(W_LOCKSCREEN_PKGNAME, NULL);
		}
	}
	ad->wms_connected = wms_state;

	return;
}

static void _w_power_off_cb(keynode_t* node, void *data)
{
	int val = VCONFKEY_SYSMAN_POWER_OFF_NONE;

	vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &val);

	if (val > VCONFKEY_SYSMAN_POWER_OFF_POPUP) {
		_ERR("power off status : %d", val);
		if (vconf_ignore_key_changed(VCONFKEY_WMS_WMANAGER_CONNECTED, _w_wms_changed_cb) < 0)
			_ERR("Failed to ignore the callback for [%s]", VCONFKEY_WMS_WMANAGER_CONNECTED);
		exit(0);
	}
}

static void _w_lang_changed_cb(keynode_t* node, void *data)
{
	char *locale = NULL;
	_DBG("%s, %d", __func__, __LINE__);

	locale = vconf_get_str(VCONFKEY_LANGSET);

	if (locale != NULL) {
		elm_language_set(locale);
	}
}

//TO DO. later remove this definition after applying DB structure
#define LAUNCHER_XML_PATH "/opt/usr/share/w-launcher"
static void _init(struct appdata *ad)
{
	int r;
	struct sigaction act;
	char *file = NULL;
	int first = -1;
	int lock_type = -1;
	int wms_state = -1;
	int test_mode = -1;

	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);
	aul_launch_init(NULL,NULL);

	aul_listen_app_dead_signal(_w_app_dead_cb, ad);

	//TO DO. later remove this after applying DB structure
	if (chmod(LAUNCHER_XML_PATH, 0777) < 0) {
		_ERR("chmod: %s\n", strerror(errno));
	}

	if (vconf_notify_key_changed(VCONFKEY_LANGSET, _w_lang_changed_cb, NULL) < 0) {
		_ERR("Failed to add the callback for [%s]", VCONFKEY_LANGSET);
	}

	if (vconf_notify_key_changed(VCONFKEY_WMS_WMANAGER_CONNECTED, _w_wms_changed_cb, ad) < 0) {
		_ERR("Failed to add the callback for %s changed", VCONFKEY_WMS_WMANAGER_CONNECTED);
	}
	if (vconf_get_bool(VCONFKEY_WMS_WMANAGER_CONNECTED, &wms_state) < 0) {
		_ERR("Failed to get [%s]", VCONFKEY_WMS_WMANAGER_CONNECTED);
	} else {
		ad->wms_connected = wms_state;
		_DBG("ad->wms_connected : [%d]", ad->wms_connected);
	}

	if (_w_check_first_boot() == TRUE) {
		w_launch_app(SETUP_WIZARD_PKGNAME, NULL);
		ecore_idler_add(_w_starter_idler_cb, ad);
	} else {
		_DBG("Not first booting, launch [%s]..!!", W_LAUNCHER_PKGNAME);

		vconf_get_int(VCONFKEY_TESTMODE_SCREEN_LOCK, &test_mode);
		vconf_get_int(TEMP_VCONFKEY_LOCK_TYPE, &lock_type);

		if ((wms_state == FALSE) && (lock_type == 1) && (test_mode != VCONFKEY_TESTMODE_SCREEN_LOCK_DISABLE))  {
			_ERR("BT disconneted and privacy lock is set");
			w_launch_app(W_LOCKSCREEN_PKGNAME, NULL);
			ecore_idler_add(_w_starter_idler_cb, ad);
		} else {
			ad->launcher_pid = w_launch_app(W_LAUNCHER_PKGNAME, NULL);
		}
	}

	create_key_window();
	init_hourly_alert(ad);

	if (vconf_notify_key_changed(VCONFKEY_SYSMAN_POWER_OFF_STATUS, _w_power_off_cb, NULL) < 0)
		_ERR("Failed to add the callback for [%s]", VCONFKEY_SYSMAN_POWER_OFF_STATUS);

	return;
}

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

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

	destroy_key_window();
	fini_hourly_alert(ad);

	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");
	_DBG("starter is launched..!!");
#if 0
	set_window_scale();	/* not in loop */
#endif

	elm_init(argc, argv);

	_init(&ad);

	elm_run();

	_fini(&ad);

	elm_shutdown();

	return 0;
}