[ug-wifi-direct]Sync with Tizen 2.4
[apps/native/ug-wifi-direct.git] / popup-wifidirect / src / wfd-app-main.c
1 /*
2 *  WiFi-Direct UG
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://www.tizenopensource.org/license
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 /**
21  * This file implements wifi direct application main functions.
22  *
23  * @file    wfd-app-main.c
24  * @author  Sungsik Jang (sungsik.jang@samsung.com)
25  * @version 0.1
26  */
27
28 #include <libintl.h>
29 #include <Elementary.h>
30 #if defined(X)
31 #include <Ecore_X.h>
32 #endif
33 #include <notification.h>
34 #include <ui-gadget-module.h>
35 #include <app_control_internal.h>
36 #include <feedback.h>
37 #include <wifi-direct.h>
38 #include <efl_util.h>
39 #include <efl_assist.h>
40 #include <linux/unistd.h>
41 #if defined(X)
42 #include <utilX.h>
43 #endif
44 #include <vconf.h>
45
46 #include "wfd-app.h"
47 #include "wfd-app-util.h"
48
49 wfd_appdata_t *g_wfd_ad;
50
51
52 wfd_appdata_t *wfd_get_appdata()
53 {
54         return g_wfd_ad;
55 }
56
57 static void _win_del(void *data, Evas_Object *obj, void *event)
58 {
59         elm_exit();
60 }
61
62 static Evas_Object *_create_win(Evas_Object *parent, const char *name)
63 {
64         Evas_Object *eo;
65         int w, h;
66
67         /* eo = elm_win_add(parent, name, ELM_WIN_BASIC); */
68         eo = elm_win_add(NULL, name, ELM_WIN_NOTIFICATION);
69         if (eo) {
70                 elm_win_title_set(eo, name);
71                 elm_win_borderless_set(eo, EINA_TRUE);
72                 elm_win_alpha_set(eo, EINA_TRUE);
73                 evas_object_smart_callback_add(eo, "delete,request", _win_del, NULL);
74 #if defined(X)
75                 ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
76 #endif
77                 efl_util_set_notification_window_level(eo, EFL_UTIL_NOTIFICATION_LEVEL_1);
78                 evas_object_resize(eo, w, h);
79                 evas_object_raise(eo);
80         }
81
82         return eo;
83 }
84
85 static bool _app_create(void *data)
86 {
87         __WFD_APP_FUNC_ENTER__;
88         wfd_appdata_t *ad = wfd_get_appdata();
89         int ret = 0;
90
91         if (data == NULL) {
92                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
93                 return FALSE;
94         }
95
96         bindtextdomain(LOCALE_FILE_NAME, LOCALEDIR);
97
98         ad->popup_data = (wfd_popup_t *) malloc(sizeof(wfd_popup_t));
99         if (!ad->popup_data) {
100                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "malloc failed\n");
101                 return FALSE;
102         }
103
104         memset(ad->popup_data, 0x0, sizeof(wfd_popup_t));
105         ad->win = _create_win(NULL, PACKAGE);
106
107         if (elm_win_wm_rotation_supported_get(ad->win)) {
108                 int rots[4] = { 0, 90, 180, 270 };
109                 elm_win_wm_rotation_available_rotations_set(ad->win, rots, 1);
110         }
111
112         ad->conformant = elm_conformant_add(ad->win);
113         assertm_if(NULL == ad->conformant, "conformant is NULL!!");
114         elm_win_conformant_set(ad->win, EINA_TRUE);
115         elm_win_resize_object_add(ad->win, ad->conformant);
116         evas_object_size_hint_weight_set(ad->conformant, EVAS_HINT_EXPAND, 0.0);
117         evas_object_size_hint_align_set(ad->conformant, EVAS_HINT_FILL, 0.0);
118         evas_object_show(ad->conformant);
119
120
121         ad->back_grnd = elm_bg_add(ad->conformant);
122         if (NULL == ad->back_grnd) {
123                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Create background failed\n");
124                 return FALSE;
125         }
126         elm_object_signal_emit(ad->conformant, "elm,state,indicator,nooverlap", "elm");
127         elm_object_style_set(ad->back_grnd, "indicator/headerbg");
128         elm_object_part_content_set(ad->conformant, "elm.swallow.indicator_bg", ad->back_grnd);
129         evas_object_size_hint_weight_set(ad->back_grnd,
130                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
131         evas_object_show(ad->back_grnd);
132
133         ad->layout = elm_layout_add(ad->conformant);
134         elm_object_content_set(ad->conformant, ad->layout);
135
136 #if defined(X)
137         if (!ecore_x_display_get()) {
138                 return FALSE;
139         }
140 #endif
141
142         ret = init_wfd_client(ad);
143         if (!ret) {
144                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "init_wfd_popup_client error\n");
145                 wfd_prepare_popup(WFD_POP_FAIL_INIT, NULL);
146                 __WFD_APP_FUNC_EXIT__;
147                 return FALSE;
148         }
149
150         ret = wfd_app_util_register_vconf_callbacks(ad);
151         if (ret < 0) {
152                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf notification");
153                 return FALSE;
154         }
155
156         /* Register Hard Key Press CB */
157         wfd_app_util_register_hard_key_down_cb(ad);
158
159         /* Initializes feedback API */
160         ret = feedback_initialize();
161         if (ret != FEEDBACK_ERROR_NONE) {
162                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "feedback_initialize error : %d\n", ret);
163                 return FALSE;
164         }
165         __WFD_APP_FUNC_EXIT__;
166         return TRUE;
167 }
168
169 static void _app_terminate(void *data)
170 {
171         __WFD_APP_FUNC_ENTER__;
172         wfd_appdata_t *ad = (wfd_appdata_t *) data;
173         int ret = 0;
174
175         if (data == NULL) {
176                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
177                 return;
178         }
179
180         wfd_app_util_del_notification(ad);
181
182         ret = wfd_app_util_deregister_vconf_callbacks(ad);
183         if (ret < 0) {
184                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf notification");
185         }
186
187         /* Deregister Hardkey CB */
188         wfd_app_util_deregister_hard_key_down_cb(ad);
189
190         /* Deinitializes feedback API */
191         ret = feedback_deinitialize();
192         if (ret != FEEDBACK_ERROR_NONE) {
193                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "feedback_deinitialize error : %d\n", ret);
194         }
195         if (ad->transmit_timer) {
196                 ecore_timer_del(ad->transmit_timer);
197                 ad->transmit_timer = NULL;
198         }
199
200         wfd_destroy_popup();
201
202         ret = deinit_wfd_client(ad);
203         if (ret < 0) {
204                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "deinit_wfd_client error\n");
205         }
206
207         if (ad->back_grnd) {
208                 evas_object_del(ad->back_grnd);
209                 ad->back_grnd = NULL;
210         }
211
212         if (ad->win) {
213                 evas_object_del(ad->win);
214                 ad->win = NULL;
215         }
216
217         if (ad->popup_data) {
218                 free(ad->popup_data);
219                 ad->popup_data = NULL;
220         }
221
222         __WFD_APP_FUNC_EXIT__;
223         return;
224 }
225
226 static void _app_pause(void *data)
227 {
228         __WFD_APP_FUNC_ENTER__;
229         __WFD_APP_FUNC_EXIT__;
230         return;
231 }
232
233 static void _app_resume(void *data)
234 {
235         __WFD_APP_FUNC_ENTER__;
236         __WFD_APP_FUNC_EXIT__;
237         return;
238 }
239
240 static void _app_reset(app_control_h control, void *data)
241 {
242         __WFD_APP_FUNC_ENTER__;
243
244         int ret;
245         wfd_appdata_t *ad = (wfd_appdata_t *) data;
246         if (ad == NULL) {
247                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
248                 return;
249         }
250         if (control == NULL) {
251                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Service is NULL");
252                 return;
253         }
254
255         // From Notification
256         char *noti_type = NULL;
257         app_control_get_extra_data(control, NOTIFICATION_BUNDLE_PARAM, &noti_type);
258
259         if (noti_type == NULL) {
260                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Notification type is wrong.");
261                 return;
262         }
263
264         WFD_APP_LOG(WFD_APP_LOG_LOW, "Notification type is [%s]", noti_type);
265         if (strncmp(noti_type, NOTIFICATION_BUNDLE_VALUE, strlen(NOTIFICATION_BUNDLE_PARAM)) == 0) {
266                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Launch wifidirect-ug");
267                 wifi_direct_get_state(&ad->wfd_status);
268                 WFD_APP_LOG(WFD_APP_LOG_LOW, "State: %d", ad->wfd_status);
269                 if (ad->wfd_status == WIFI_DIRECT_STATE_CONNECTED) {
270                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Connected");
271                         if (ad->transmit_timer) {
272                                 ecore_timer_del(ad->transmit_timer);
273                                 ad->transmit_timer = NULL;
274                         }
275                         WFD_APP_LOG(WFD_APP_LOG_LOW, "start the transmit timer again\n");
276                         ad->last_wfd_transmit_time = time(NULL);
277                         ad->transmit_timer = ecore_timer_add(5.0,
278                                 (Ecore_Task_Cb)wfd_automatic_deactivated_for_connection_cb, ad);
279                 }
280                 app_control_h ug_control;
281                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Launching Settings EFL from notification\n");
282                 app_control_create(&ug_control);
283                 app_control_set_operation(ug_control, APP_CONTROL_OPERATION_DEFAULT);
284                 app_control_set_window(ug_control, elm_win_xwindow_get(ug_get_window()));
285                 app_control_set_app_id(ug_control, "setting-wifidirect-efl");
286
287                 ret = app_control_send_launch_request(ug_control, NULL, NULL);
288                 if(ret == APP_CONTROL_ERROR_NONE) {
289                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Launch Wi-Fi Direct successful");
290                 } else {
291                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to launch Wi-Fi Direct");
292                 }
293                 app_control_destroy(ug_control);
294
295         }
296         WFD_IF_FREE_MEM(noti_type);
297         __WFD_APP_FUNC_EXIT__;
298         return;
299 }
300
301 int main(int argc, char *argv[])
302 {
303         wfd_appdata_t ad;
304         ui_app_lifecycle_callback_s event_callback;
305         memset(&event_callback, 0x0, sizeof(ui_app_lifecycle_callback_s));
306
307         event_callback.create = _app_create;
308         event_callback.terminate = _app_terminate;
309         event_callback.pause = _app_pause;
310         event_callback.resume = _app_resume;
311         event_callback.app_control = _app_reset;
312
313         memset(&ad, 0x0, sizeof(wfd_appdata_t));
314         g_wfd_ad = &ad;
315
316         return ui_app_main(argc, argv, &event_callback, &ad);
317 }