summaryrefslogtreecommitdiff
path: root/popup-wifidirect/src/wfd-app-main.c
blob: 860cd6877d88818647be30e199050c625e0fc490 (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
/*
*  WiFi-Direct UG
*
* 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.
*
*/

/**
 * This file implements wifi direct application main functions.
 *
 * @file    wfd-app-main.c
 * @author  Sungsik Jang (sungsik.jang@samsung.com)
 * @version 0.1
 */

#include <libintl.h>
#include <Elementary.h>
#include <Ecore_X.h>
#include <notification.h>
#include <ui-gadget-module.h>
#include <app_control.h>
#include <feedback.h>
#include <wifi-direct.h>
#include <efl_util.h>
#include <efl_assist.h>
#include <linux/unistd.h>
#include <utilX.h>
#include <vconf.h>

#include "wfd-app.h"
#include "wfd-app-util.h"

wfd_appdata_t *g_wfd_ad;


wfd_appdata_t *wfd_get_appdata()
{
	return g_wfd_ad;
}

static void _win_del(void *data, Evas_Object *obj, void *event)
{
	elm_exit();
}

static Evas_Object *_create_win(Evas_Object *parent, const char *name)
{
	Evas_Object *eo;
	int w, h;

	/* eo = elm_win_add(parent, name, ELM_WIN_BASIC); */
	eo = elm_win_add(NULL, name, ELM_WIN_NOTIFICATION);
	if (eo) {
		elm_win_title_set(eo, name);
		elm_win_borderless_set(eo, EINA_TRUE);
		elm_win_alpha_set(eo, EINA_TRUE);
		evas_object_smart_callback_add(eo, "delete,request", _win_del, NULL);
		ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
		efl_util_set_notification_window_level(eo, EFL_UTIL_NOTIFICATION_LEVEL_1);
		evas_object_resize(eo, w, h);
		evas_object_raise(eo);
	}

	return eo;
}

static bool _app_create(void *data)
{
	__WFD_APP_FUNC_ENTER__;
	wfd_appdata_t *ad = wfd_get_appdata();
	int ret = 0;

	if (data == NULL) {
		WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
		return FALSE;
	}

	bindtextdomain(LOCALE_FILE_NAME, LOCALEDIR);

	ad->popup_data = (wfd_popup_t *) malloc(sizeof(wfd_popup_t));
	if (!ad->popup_data) {
		WFD_APP_LOG(WFD_APP_LOG_ERROR, "malloc failed\n");
		return FALSE;
	}

	memset(ad->popup_data, 0x0, sizeof(wfd_popup_t));
	ad->win = _create_win(NULL, PACKAGE);

	if (elm_win_wm_rotation_supported_get(ad->win)) {
		int rots[4] = { 0, 90, 180, 270 };
		elm_win_wm_rotation_available_rotations_set(ad->win, rots, 1);
	}

	ad->conformant = elm_conformant_add(ad->win);
	assertm_if(NULL == ad->conformant, "conformant is NULL!!");
	elm_win_conformant_set(ad->win, EINA_TRUE);
	elm_win_resize_object_add(ad->win, ad->conformant);
	evas_object_size_hint_weight_set(ad->conformant, EVAS_HINT_EXPAND, 0.0);
	evas_object_size_hint_align_set(ad->conformant, EVAS_HINT_FILL, 0.0);
	evas_object_show(ad->conformant);


	ad->back_grnd = elm_bg_add(ad->conformant);
	if (NULL == ad->back_grnd) {
		WFD_APP_LOG(WFD_APP_LOG_LOW, "Create background failed\n");
		return FALSE;
	}
	elm_object_signal_emit(ad->conformant, "elm,state,indicator,nooverlap", "elm");
	elm_object_style_set(ad->back_grnd, "indicator/headerbg");
	elm_object_part_content_set(ad->conformant, "elm.swallow.indicator_bg", ad->back_grnd);
	evas_object_size_hint_weight_set(ad->back_grnd,
		EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
	evas_object_show(ad->back_grnd);

	ad->layout = elm_layout_add(ad->conformant);
	elm_object_content_set(ad->conformant, ad->layout);

	if (!ecore_x_display_get()) {
		return FALSE;
	}

	ret = init_wfd_client(ad);
	if (!ret) {
		WFD_APP_LOG(WFD_APP_LOG_ERROR, "init_wfd_popup_client error\n");
		wfd_prepare_popup(WFD_POP_FAIL_INIT, NULL);
		__WFD_APP_FUNC_EXIT__;
		return FALSE;
	}

	ret = wfd_app_util_register_vconf_callbacks(ad);
	if (ret < 0) {
		WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf notification");
		return FALSE;
	}

	/* Enable Changeable UI feature */
	ea_theme_changeable_ui_enabled_set(EINA_TRUE);

	/* Register Hard Key Press CB */
	wfd_app_util_register_hard_key_down_cb(ad);

	/* Initializes feedback API */
	ret = feedback_initialize();
	if (ret != FEEDBACK_ERROR_NONE) {
		WFD_APP_LOG(WFD_APP_LOG_ERROR, "feedback_initialize error : %d\n", ret);
		return FALSE;
	}
	__WFD_APP_FUNC_EXIT__;
	return TRUE;
}

static void _app_terminate(void *data)
{
	__WFD_APP_FUNC_ENTER__;
	wfd_appdata_t *ad = (wfd_appdata_t *) data;
	int ret = 0;

	if (data == NULL) {
		WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
		return;
	}

	wfd_app_util_del_notification(ad);

	ret = wfd_app_util_deregister_vconf_callbacks(ad);
	if (ret < 0) {
		WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf notification");
	}

	/* Deregister Hardkey CB */
	wfd_app_util_deregister_hard_key_down_cb(ad);

	/* Deinitializes feedback API */
	ret = feedback_deinitialize();
	if (ret != FEEDBACK_ERROR_NONE) {
		WFD_APP_LOG(WFD_APP_LOG_ERROR, "feedback_deinitialize error : %d\n", ret);
	}
	if (ad->transmit_timer) {
		ecore_timer_del(ad->transmit_timer);
		ad->transmit_timer = NULL;
	}

	wfd_destroy_popup();

	ret = deinit_wfd_client(ad);
	if (ret < 0) {
		WFD_APP_LOG(WFD_APP_LOG_ERROR, "deinit_wfd_client error\n");
	}

	if (ad->back_grnd) {
		evas_object_del(ad->back_grnd);
		ad->back_grnd = NULL;
	}

	if (ad->win) {
		evas_object_del(ad->win);
		ad->win = NULL;
	}

	if (ad->popup_data) {
		free(ad->popup_data);
		ad->popup_data = NULL;
	}

	__WFD_APP_FUNC_EXIT__;
	return;
}

static void _app_pause(void *data)
{
	__WFD_APP_FUNC_ENTER__;
	__WFD_APP_FUNC_EXIT__;
	return;
}

static void _app_resume(void *data)
{
	__WFD_APP_FUNC_ENTER__;
	__WFD_APP_FUNC_EXIT__;
	return;
}

static void _app_reset(app_control_h control, void *data)
{
	__WFD_APP_FUNC_ENTER__;

	int ret;
	wfd_appdata_t *ad = (wfd_appdata_t *) data;
	if (ad == NULL) {
		WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
		return;
	}
	if (control == NULL) {
		WFD_APP_LOG(WFD_APP_LOG_ERROR, "Service is NULL");
		return;
	}

	// From Notification
	char *noti_type = NULL;
	app_control_get_extra_data(control, NOTIFICATION_BUNDLE_PARAM, &noti_type);

	if (noti_type == NULL) {
		WFD_APP_LOG(WFD_APP_LOG_ERROR, "Notification type is wrong.");
		return;
	}

	WFD_APP_LOG(WFD_APP_LOG_LOW, "Notification type is [%s]", noti_type);
	if (strncmp(noti_type, NOTIFICATION_BUNDLE_VALUE, strlen(NOTIFICATION_BUNDLE_PARAM)) == 0) {
		WFD_APP_LOG(WFD_APP_LOG_LOW, "Launch wifidirect-ug");
		wifi_direct_get_state(&ad->wfd_status);
		WFD_APP_LOG(WFD_APP_LOG_LOW, "State: %d", ad->wfd_status);
		if (ad->wfd_status == WIFI_DIRECT_STATE_CONNECTED) {
			WFD_APP_LOG(WFD_APP_LOG_LOW, "Connected");
			if (ad->transmit_timer) {
				ecore_timer_del(ad->transmit_timer);
				ad->transmit_timer = NULL;
			}
			WFD_APP_LOG(WFD_APP_LOG_LOW, "start the transmit timer again\n");
			ad->last_wfd_transmit_time = time(NULL);
			ad->transmit_timer = ecore_timer_add(5.0,
				(Ecore_Task_Cb)wfd_automatic_deactivated_for_connection_cb, ad);
		}
		app_control_h ug_control;
		WFD_APP_LOG(WFD_APP_LOG_LOW, "Launching Settings EFL from notification\n");
		app_control_create(&ug_control);
		app_control_set_operation(ug_control, APP_CONTROL_OPERATION_DEFAULT);
		app_control_set_window(ug_control, elm_win_xwindow_get(ug_get_window()));
		app_control_set_app_id(ug_control, "setting-wifidirect-efl");

		ret = app_control_send_launch_request(ug_control, NULL, NULL);
		if(ret == APP_CONTROL_ERROR_NONE) {
			WFD_APP_LOG(WFD_APP_LOG_LOW, "Launch Wi-Fi Direct successful");
		} else {
			WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to launch Wi-Fi Direct");
		}
		app_control_destroy(ug_control);

	}
	WFD_IF_FREE_MEM(noti_type);
	__WFD_APP_FUNC_EXIT__;
	return;
}

int main(int argc, char *argv[])
{
	wfd_appdata_t ad;
	ui_app_lifecycle_callback_s event_callback;
	memset(&event_callback, 0x0, sizeof(ui_app_lifecycle_callback_s));

	event_callback.create = _app_create;
	event_callback.terminate = _app_terminate;
	event_callback.pause = _app_pause;
	event_callback.resume = _app_resume;
	event_callback.app_control = _app_reset;

	memset(&ad, 0x0, sizeof(wfd_appdata_t));
	g_wfd_ad = &ad;

	return ui_app_main(argc, argv, &event_callback, &ad);
}