f3ca73fad5b5f2302e1c34c69950caaf3a91ae84
[apps/native/ug-wifi-direct.git] / ug-wifidirect / src / wfd_motion_control.c
1 /*
2  * Wi-Fi direct
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 #include <vconf.h>
21 #include <sensor_internal.h>
22 #include <vconf-keys.h>
23
24 #include <libintl.h>
25 #include <glib.h>
26
27 #include <Elementary.h>
28 #include <vconf.h>
29 #include <ui-gadget-module.h>
30
31 #include "wfd_motion_control.h"
32 #include "wfd_ug_view.h"
33 #include "wfd_client.h"
34
35
36 static int motion_handle = -1;
37
38 static TARGET_VIEW_FOCUS __motion_target_view_focus_get(void *data)
39 {
40         struct ug_data *ugd = (struct ug_data *)data;
41
42         if ((ugd == NULL) || (ugd->naviframe == NULL))
43                 return MOTION_TARGET_VIEW_FOCUS_OFF;
44
45         if (elm_object_focus_get(ugd->naviframe))
46                 return MOTION_TARGET_VIEW_FOCUS_ON;
47         else
48                 return MOTION_TARGET_VIEW_FOCUS_OFF;
49 }
50
51
52 static void __motion_shake_cb(unsigned int event_type, sensor_event_data_t *event_data, void *data)
53 {
54         __FUNC_ENTER__;
55
56         gboolean motion_activated = FALSE;
57         struct ug_data *ugd = (struct ug_data *) data;
58         if (NULL == ugd) {
59                 DBG(LOG_ERROR, "NULL pointer!");
60                 return;
61         }
62         TARGET_VIEW_FOCUS focus_state = __motion_target_view_focus_get(ugd);
63
64         DBG(LOG_DEBUG, "event type: %d, focus state: %d\n", event_type, focus_state);
65
66         if (focus_state != MOTION_TARGET_VIEW_FOCUS_ON)
67                 return;
68
69         if (vconf_get_bool(VCONFKEY_SETAPPL_MOTION_ACTIVATION,
70                         (void *)&motion_activated)) {
71                 DBG(LOG_ERROR, "Get motion activation status fail");
72                 return;
73         }
74
75         if (FALSE == motion_activated) {
76                 DBG(LOG_INFO, "Motion value is false");
77                 return;
78         }
79
80         if (vconf_get_bool(VCONFKEY_SETAPPL_USE_SHAKE,
81                         (void *)&motion_activated)) {
82                 DBG(LOG_ERROR, "Get use shake status fail");
83                 return;
84         }
85
86         if (FALSE == motion_activated) {
87                 DBG(LOG_INFO, "Shake status is false");
88                 return;
89         }
90
91         int ret = -1;
92         const char *btn_text = NULL;
93
94         if (NULL == ugd) {
95                 DBG(LOG_ERROR, "Incorrect parameter(NULL)\n");
96                 return;
97         }
98
99         wfd_refresh_wifi_direct_state(ugd);
100
101         if (NULL == ugd->scan_toolbar) {
102                 DBG(LOG_ERROR, "NULL == ugd->scan_toolbar\n");
103                 return;
104         }
105
106         btn_text = elm_object_part_text_get(ugd->scan_toolbar, "default");
107         if (NULL == btn_text) {
108                 DBG(LOG_ERROR, "Incorrect button text(NULL)\n");
109                 return;
110         }
111
112         GList *iterator = NULL;
113
114         if (0 == strcmp(btn_text, D_("IDS_WIFI_SK4_SCAN"))) {
115                 if (WIFI_DIRECT_STATE_CONNECTED == ugd->wfd_status) {
116                         ret = wfd_client_disconnect(NULL);
117                         if (ret != WIFI_DIRECT_ERROR_NONE) {
118                                 DBG(LOG_ERROR, "Failed wfd_client_disconnect() [%d]\n", ret);
119                                 __FUNC_EXIT__;
120                                 return;
121                         }
122
123                         if (ugd->multi_connect_mode != WFD_MULTI_CONNECT_MODE_NONE) {
124                                 wfd_free_multi_selected_peers(ugd);
125                         } else {
126                                 /* update the connecting icon */
127                                 for (iterator = ugd->raw_discovered_peer_list; iterator; iterator = iterator->next) {
128                                         ((device_type_s *)iterator->data)->conn_status = PEER_CONN_STATUS_DISCONNECTED;
129                                         wfd_ug_view_refresh_glitem(((device_type_s *)iterator->data)->gl_item);
130                                 }
131                         }
132                 } else if (WIFI_DIRECT_STATE_DEACTIVATED == ugd->wfd_status) {
133                         wfd_client_switch_on(ugd);
134                 } else if ((WIFI_DIRECT_STATE_DISCOVERING == ugd->wfd_status) || (WIFI_DIRECT_STATE_ACTIVATED == ugd->wfd_status)) {
135                         ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_SOCIAL_CHANNEL_START;
136                         ret = wifi_direct_start_discovery_specific_channel(false, 1, WIFI_DIRECT_DISCOVERY_SOCIAL_CHANNEL);
137                         if (ret != WIFI_DIRECT_ERROR_NONE) {
138                                 ugd->wfd_discovery_status = WIFI_DIRECT_DISCOVERY_NONE;
139                                 DBG(LOG_ERROR, "Failed to start discovery. [%d]\n", ret);
140                                 wifi_direct_cancel_discovery();
141                         }
142                 }
143         }
144
145         __FUNC_EXIT__;
146 }
147
148 void motion_create(struct ug_data *ugd)
149 {
150         int ret = -1;
151
152         motion_handle = sf_connect(MOTION_SENSOR);
153         if (motion_handle < 0) {
154                 DBG(LOG_ERROR, "Failed Operation sf_connect.\n");
155                 return;
156         }
157         ret = sf_register_event(motion_handle, MOTION_ENGINE_EVENT_SHAKE, NULL,
158                         __motion_shake_cb, ugd);
159         if (ret < 0) {
160                 DBG(LOG_ERROR, "Failed Operation sf_register_event. [%d]\n", ret);
161                 goto fail;
162         }
163         ret = sf_start(motion_handle, 0);
164         if (ret < 0) {
165                 DBG(LOG_ERROR, "Failed Operation sf_start. [%d]\n", ret);
166                 goto fail;
167         }
168
169         DBG(LOG_INFO, "Successfully, Init Sensor Handle\n");
170         return;
171
172 fail:
173         ret = sf_disconnect(motion_handle);
174         if (ret < 0)
175                 DBG(LOG_ERROR, "Failed Operation sf_disconnect. [%d]\n", ret);
176 }
177
178 void motion_destroy(void)
179 {
180         int ret = -1;
181
182         if (motion_handle < 0) {
183                 DBG(LOG_ERROR, "Motion Handle Not valid !!!");
184                 return;
185         }
186
187         ret = sf_stop(motion_handle);
188         if (ret < 0)
189                 DBG(LOG_ERROR, "Failed Operation sf_stop. [%d]\n", ret);
190
191         ret = sf_unregister_event(motion_handle, MOTION_ENGINE_EVENT_SHAKE);
192         if (ret < 0)
193                 DBG(LOG_ERROR, "Failed Operation sf_unregister_event. [%d]\n", ret);
194
195         ret = sf_disconnect(motion_handle);
196         if (ret < 0)
197                 DBG(LOG_ERROR, "Failed Operation sf_disconnect. [%d]\n", ret);
198 }