tizen 2.3.1 release
[apps/home/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 #include <Ecore_X.h>
31 #include <notification.h>
32 #include <ui-gadget-module.h>
33 #include <app_control.h>
34 #include <feedback.h>
35 #include <wifi-direct.h>
36 #include <efl_util.h>
37 #include <efl_assist.h>
38 #include <linux/unistd.h>
39 #include <utilX.h>
40 #include <vconf.h>
41
42 #include "wfd-app.h"
43 #include "wfd-app-util.h"
44
45 wfd_appdata_t *g_wfd_ad;
46
47
48 wfd_appdata_t *wfd_get_appdata()
49 {
50         return g_wfd_ad;
51 }
52
53 static void _win_del(void *data, Evas_Object *obj, void *event)
54 {
55         elm_exit();
56 }
57
58 static Evas_Object *_create_win(Evas_Object *parent, const char *name)
59 {
60         Evas_Object *eo;
61         int w, h;
62
63         /* eo = elm_win_add(parent, name, ELM_WIN_BASIC); */
64         eo = elm_win_add(NULL, name, ELM_WIN_NOTIFICATION);
65         if (eo) {
66                 elm_win_title_set(eo, name);
67                 elm_win_borderless_set(eo, EINA_TRUE);
68                 elm_win_alpha_set(eo, EINA_TRUE);
69                 evas_object_smart_callback_add(eo, "delete,request", _win_del, NULL);
70                 ecore_x_window_size_get(ecore_x_window_root_first_get(), &w, &h);
71                 efl_util_set_notification_window_level(eo, EFL_UTIL_NOTIFICATION_LEVEL_1);
72                 evas_object_resize(eo, w, h);
73                 evas_object_raise(eo);
74         }
75
76         return eo;
77 }
78
79 static bool _app_create(void *data)
80 {
81         __WFD_APP_FUNC_ENTER__;
82         wfd_appdata_t *ad = wfd_get_appdata();
83         int ret = 0;
84
85         if (data == NULL) {
86                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
87                 return FALSE;
88         }
89
90         bindtextdomain(LOCALE_FILE_NAME, LOCALEDIR);
91
92         ad->popup_data = (wfd_popup_t *) malloc(sizeof(wfd_popup_t));
93         if (!ad->popup_data) {
94                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "malloc failed\n");
95                 return FALSE;
96         }
97
98         memset(ad->popup_data, 0x0, sizeof(wfd_popup_t));
99         ad->win = _create_win(NULL, PACKAGE);
100
101         if (elm_win_wm_rotation_supported_get(ad->win)) {
102                 int rots[4] = { 0, 90, 180, 270 };
103                 elm_win_wm_rotation_available_rotations_set(ad->win, rots, 1);
104         }
105
106         ad->conformant = elm_conformant_add(ad->win);
107         assertm_if(NULL == ad->conformant, "conformant is NULL!!");
108         elm_win_conformant_set(ad->win, EINA_TRUE);
109         elm_win_resize_object_add(ad->win, ad->conformant);
110         evas_object_size_hint_weight_set(ad->conformant, EVAS_HINT_EXPAND, 0.0);
111         evas_object_size_hint_align_set(ad->conformant, EVAS_HINT_FILL, 0.0);
112         evas_object_show(ad->conformant);
113
114
115         ad->back_grnd = elm_bg_add(ad->conformant);
116         if (NULL == ad->back_grnd) {
117                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Create background failed\n");
118                 return FALSE;
119         }
120         elm_object_signal_emit(ad->conformant, "elm,state,indicator,nooverlap", "elm");
121         elm_object_style_set(ad->back_grnd, "indicator/headerbg");
122         elm_object_part_content_set(ad->conformant, "elm.swallow.indicator_bg", ad->back_grnd);
123         evas_object_size_hint_weight_set(ad->back_grnd,
124                 EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
125         evas_object_show(ad->back_grnd);
126
127         ad->layout = elm_layout_add(ad->conformant);
128         elm_object_content_set(ad->conformant, ad->layout);
129
130         if (!ecore_x_display_get()) {
131                 return FALSE;
132         }
133
134         ret = init_wfd_client(ad);
135         if (!ret) {
136                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "init_wfd_popup_client error\n");
137                 wfd_prepare_popup(WFD_POP_FAIL_INIT, NULL);
138                 __WFD_APP_FUNC_EXIT__;
139                 return FALSE;
140         }
141
142         ret = wfd_app_util_register_vconf_callbacks(ad);
143         if (ret < 0) {
144                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf notification");
145                 return FALSE;
146         }
147
148         /* Enable Changeable UI feature */
149         ea_theme_changeable_ui_enabled_set(EINA_TRUE);
150
151         /* Register Hard Key Press CB */
152         wfd_app_util_register_hard_key_down_cb(ad);
153
154         /* Initializes feedback API */
155         ret = feedback_initialize();
156         if (ret != FEEDBACK_ERROR_NONE) {
157                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "feedback_initialize error : %d\n", ret);
158                 return FALSE;
159         }
160         __WFD_APP_FUNC_EXIT__;
161         return TRUE;
162 }
163
164 static void _app_terminate(void *data)
165 {
166         __WFD_APP_FUNC_ENTER__;
167         wfd_appdata_t *ad = (wfd_appdata_t *) data;
168         int ret = 0;
169
170         if (data == NULL) {
171                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
172                 return;
173         }
174
175         wfd_app_util_del_notification(ad);
176
177         ret = wfd_app_util_deregister_vconf_callbacks(ad);
178         if (ret < 0) {
179                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Failed to register vconf notification");
180         }
181
182         /* Deregister Hardkey CB */
183         wfd_app_util_deregister_hard_key_down_cb(ad);
184
185         /* Deinitializes feedback API */
186         ret = feedback_deinitialize();
187         if (ret != FEEDBACK_ERROR_NONE) {
188                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "feedback_deinitialize error : %d\n", ret);
189         }
190         if (ad->transmit_timer) {
191                 ecore_timer_del(ad->transmit_timer);
192                 ad->transmit_timer = NULL;
193         }
194
195         wfd_destroy_popup();
196
197         ret = deinit_wfd_client(ad);
198         if (ret < 0) {
199                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "deinit_wfd_client error\n");
200         }
201
202         if (ad->back_grnd) {
203                 evas_object_del(ad->back_grnd);
204                 ad->back_grnd = NULL;
205         }
206
207         if (ad->win) {
208                 evas_object_del(ad->win);
209                 ad->win = NULL;
210         }
211
212         if (ad->popup_data) {
213                 free(ad->popup_data);
214                 ad->popup_data = NULL;
215         }
216
217         __WFD_APP_FUNC_EXIT__;
218         return;
219 }
220
221 static void _app_pause(void *data)
222 {
223         __WFD_APP_FUNC_ENTER__;
224         __WFD_APP_FUNC_EXIT__;
225         return;
226 }
227
228 static void _app_resume(void *data)
229 {
230         __WFD_APP_FUNC_ENTER__;
231         __WFD_APP_FUNC_EXIT__;
232         return;
233 }
234
235 static void _app_reset(app_control_h control, void *data)
236 {
237         __WFD_APP_FUNC_ENTER__;
238
239         int ret;
240         wfd_appdata_t *ad = (wfd_appdata_t *) data;
241         if (ad == NULL) {
242                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Incorrect parameter\n");
243                 return;
244         }
245         if (control == NULL) {
246                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Service is NULL");
247                 return;
248         }
249
250         // From Notification
251         char *noti_type = NULL;
252         app_control_get_extra_data(control, NOTIFICATION_BUNDLE_PARAM, &noti_type);
253
254         if (noti_type == NULL) {
255                 WFD_APP_LOG(WFD_APP_LOG_ERROR, "Notification type is wrong.");
256                 return;
257         }
258
259         WFD_APP_LOG(WFD_APP_LOG_LOW, "Notification type is [%s]", noti_type);
260         if (strncmp(noti_type, NOTIFICATION_BUNDLE_VALUE, strlen(NOTIFICATION_BUNDLE_PARAM)) == 0) {
261                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Launch wifidirect-ug");
262                 wifi_direct_get_state(&ad->wfd_status);
263                 WFD_APP_LOG(WFD_APP_LOG_LOW, "State: %d", ad->wfd_status);
264                 if (ad->wfd_status == WIFI_DIRECT_STATE_CONNECTED) {
265                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Connected");
266                         if (ad->transmit_timer) {
267                                 ecore_timer_del(ad->transmit_timer);
268                                 ad->transmit_timer = NULL;
269                         }
270                         WFD_APP_LOG(WFD_APP_LOG_LOW, "start the transmit timer again\n");
271                         ad->last_wfd_transmit_time = time(NULL);
272                         ad->transmit_timer = ecore_timer_add(5.0,
273                                 (Ecore_Task_Cb)wfd_automatic_deactivated_for_connection_cb, ad);
274                 }
275                 app_control_h ug_control;
276                 WFD_APP_LOG(WFD_APP_LOG_LOW, "Launching Settings EFL from notification\n");
277                 app_control_create(&ug_control);
278                 app_control_set_operation(ug_control, APP_CONTROL_OPERATION_DEFAULT);
279                 app_control_set_window(ug_control, elm_win_xwindow_get(ug_get_window()));
280                 app_control_set_app_id(ug_control, "setting-wifidirect-efl");
281
282                 ret = app_control_send_launch_request(ug_control, NULL, NULL);
283                 if(ret == APP_CONTROL_ERROR_NONE) {
284                         WFD_APP_LOG(WFD_APP_LOG_LOW, "Launch Wi-Fi Direct successful");
285                 } else {
286                         WFD_APP_LOG(WFD_APP_LOG_ERROR, "Fail to launch Wi-Fi Direct");
287                 }
288                 app_control_destroy(ug_control);
289
290         }
291         WFD_IF_FREE_MEM(noti_type);
292         __WFD_APP_FUNC_EXIT__;
293         return;
294 }
295
296 int main(int argc, char *argv[])
297 {
298         wfd_appdata_t ad;
299         ui_app_lifecycle_callback_s event_callback;
300         memset(&event_callback, 0x0, sizeof(ui_app_lifecycle_callback_s));
301
302         event_callback.create = _app_create;
303         event_callback.terminate = _app_terminate;
304         event_callback.pause = _app_pause;
305         event_callback.resume = _app_resume;
306         event_callback.app_control = _app_reset;
307
308         memset(&ad, 0x0, sizeof(wfd_appdata_t));
309         g_wfd_ad = &ad;
310
311         return ui_app_main(argc, argv, &event_callback, &ad);
312 }