merge with master
[platform/framework/native/uix.git] / src / FUixSensor_MotionImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include <FBaseErrors.h>
19 #include <FUixSensorMotion.h>
20 #include <FBaseSysLog.h>
21 #include <FUixSensor_MotionImpl.h>
22
23 using namespace Tizen::Base;
24
25 namespace Tizen { namespace Uix { namespace Sensor
26 {
27
28 void
29 _MotionImpl::__MotionEventReceiver(unsigned int eventType, sensor_event_data_t* pEventData, void* pData)
30 {
31     int eventData = *((int*) pEventData->event_data);
32     IMotionEventListener* pMotionEventListener = (IMotionEventListener*) pData;
33
34     if (pMotionEventListener != null)
35     {
36         switch (eventType)
37         {
38         case MOTION_ENGINE_EVENT_SNAP:
39         {
40             SysLog(NID_UIX, "Detected SNAP event(%d)\n", eventData);
41             switch (eventData)
42             {
43             case MOTION_ENGIEN_POSITIVE_SNAP_X:
44                 pMotionEventListener->OnSnapDetected(MOTION_SNAP_X_POSITIVE);
45                 break;
46
47             case MOTION_ENGIEN_NEGATIVE_SNAP_X:
48                 pMotionEventListener->OnSnapDetected(MOTION_SNAP_X_NEGATIVE);
49                 break;
50
51             case MOTION_ENGIEN_POSITIVE_SNAP_Y:
52                 pMotionEventListener->OnSnapDetected(MOTION_SNAP_Y_POSITIVE);
53                 break;
54
55             case MOTION_ENGIEN_NEGATIVE_SNAP_Y:
56                 pMotionEventListener->OnSnapDetected(MOTION_SNAP_Y_NEGATIVE);
57                 break;
58
59             case MOTION_ENGIEN_POSITIVE_SNAP_Z:
60                 pMotionEventListener->OnSnapDetected(MOTION_SNAP_Z_POSITIVE);
61                 break;
62
63             case MOTION_ENGIEN_NEGATIVE_SNAP_Z:
64                 pMotionEventListener->OnSnapDetected(MOTION_SNAP_Z_NEGATIVE);
65                 break;
66             }
67         }
68         break;
69
70         case MOTION_ENGINE_EVENT_SHAKE:
71         {
72             SysLog(NID_UIX, "Detected SHAKE event(%d)\n", eventData);
73             switch (eventData)
74             {
75             case MOTION_ENGIEN_SHAKE_DETECTION:
76                 pMotionEventListener->OnShakeDetected(MOTION_STARTED);
77                 break;
78
79             case MOTION_ENGIEN_SHAKE_CONTINUING:
80                 pMotionEventListener->OnShakeDetected(MOTION_INPROGRESS);
81                 break;
82
83             case MOTION_ENGIEN_SHAKE_FINISH:
84                 pMotionEventListener->OnShakeDetected(MOTION_ENDED);
85                 break;
86             }
87         }
88         break;
89
90         case MOTION_ENGINE_EVENT_DOUBLETAP:
91         {
92             SysLog(NID_UIX, "Detected DOUBLETAP event(%d)\n", eventData);
93
94             if (eventData == MOTION_ENGIEN_DOUBLTAP_DETECTION)
95                 pMotionEventListener->OnDoubleTapDetected();
96
97         }
98         break;
99
100         case MOTION_ENGINE_EVENT_DIRECT_CALL:
101                 {
102                         SysLog(NID_UIX, "Detected MOVE_TO_EAR event(%d)\n", eventData);
103
104                         if (eventData == MOTION_ENGINE_DIRECT_CALL_DETECTION)
105                                 pMotionEventListener->OnMoveToEarDetected();
106
107                 }
108                 break;
109         }
110     }
111 }
112
113 _MotionImpl::_MotionImpl(void)
114     : __pIMotionEventListener(null)
115     , __handle(-1)
116     , __motionType(MOTION_TYPE_NONE)
117 {
118 }
119
120 _MotionImpl::~_MotionImpl(void)
121 {
122     int ret = 0;
123
124     ret = sf_stop(__handle);
125     if (ret != 0)
126         SysLog(NID_UIX, "Failed to call sf_stop() method.");
127
128     ret = sf_disconnect(__handle);
129     if (ret != 0)
130         SysLog(NID_UIX, "Failed to call sf_disconnect() method.");
131 }
132
133 result
134 _MotionImpl::Construct(IMotionEventListener& listener)
135 {
136     result r = E_SUCCESS;
137     int ret = 0;
138     ret = sf_is_sensor_event_available(MOTION_SENSOR, 0);
139     SysTryReturn(NID_UIX, ret == 0, E_UNSUPPORTED_OPERATION, E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] The motion detection feature is not supported");
140     __handle = sf_connect(MOTION_SENSOR);
141     SysTryReturn(NID_UIX, __handle >= 0, E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Sensor handle was not created properly");
142     ret = sf_start(__handle, 0);
143     SysTryReturn(NID_UIX, ret == 0, E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Sensor couldn't start");
144
145     __pIMotionEventListener = &listener;
146     return r;
147 }
148
149 void
150 _MotionImpl::SetEnabled(unsigned long type)
151 {
152     int ret = 0;
153
154     SysLog(NID_UIX, "type(%d) / __motionType(%d)", (int)type, (int)__motionType);
155
156     if (type & MOTION_TYPE_SNAP)
157     {
158         if (__motionType & MOTION_TYPE_SNAP)
159         {
160             SysLog(NID_UIX, "MOTION_TYPE_SNAP is already registered.");
161         }
162         else
163         {
164             ret = sf_register_event(__handle, MOTION_ENGINE_EVENT_SNAP, null, __MotionEventReceiver, (void*) __pIMotionEventListener);
165             SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to register motion snap event.");
166         }
167     }
168     else
169     {
170         if (__motionType & MOTION_TYPE_SNAP)
171         {
172             ret = sf_unregister_event(__handle, MOTION_ENGINE_EVENT_SNAP);
173             SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to unregister motion snap event.");
174         }
175     }
176
177     if (type & MOTION_TYPE_SHAKE)
178     {
179         if (__motionType & MOTION_TYPE_SHAKE)
180         {
181             SysLog(NID_UIX, "MOTION_TYPE_SHAKE is already registered.");
182         }
183         else
184         {
185             ret = sf_register_event(__handle, MOTION_ENGINE_EVENT_SHAKE, null, __MotionEventReceiver, (void*) __pIMotionEventListener);
186             SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to register motion shake event.");
187         }
188     }
189     else
190     {
191         if (__motionType & MOTION_TYPE_SHAKE)
192         {
193             ret = sf_unregister_event(__handle, MOTION_ENGINE_EVENT_SHAKE);
194             SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to unregister motion shake event.");
195         }
196     }
197
198     if (type & MOTION_TYPE_DOUBLETAP)
199     {
200         if (__motionType & MOTION_TYPE_DOUBLETAP)
201         {
202             SysLog(NID_UIX, "MOTION_TYPE_DOUBLETAP is already registered.");
203         }
204         else
205         {
206             ret = sf_register_event(__handle, MOTION_ENGINE_EVENT_DOUBLETAP, null, __MotionEventReceiver, (void*) __pIMotionEventListener);
207             SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to register motion double tap event.");
208         }
209     }
210     else
211     {
212         if (__motionType & MOTION_TYPE_DOUBLETAP)
213         {
214             ret = sf_unregister_event(__handle, MOTION_ENGINE_EVENT_DOUBLETAP);
215             SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to unregister motion double tap event.");
216         }
217     }
218
219     if (type & MOTION_TYPE_MOVE_TO_EAR)
220        {
221            if (__motionType & MOTION_TYPE_MOVE_TO_EAR)
222            {
223                SysLog(NID_UIX, "MOTION_TYPE_MOVE_TO_EAR is already registered.");
224            }
225            else
226            {
227                ret = sf_register_event(__handle, MOTION_ENGINE_EVENT_DIRECT_CALL, null, __MotionEventReceiver, (void*) __pIMotionEventListener);
228                SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to register motion direct call event.");
229            }
230        }
231        else
232        {
233            if (__motionType & MOTION_TYPE_MOVE_TO_EAR)
234            {
235                ret = sf_unregister_event(__handle, MOTION_ENGINE_EVENT_DIRECT_CALL);
236                SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to unregister motion direct call event.");
237            }
238        }
239     __motionType = type;
240
241     SetLastResult(E_SUCCESS);
242 }
243
244 bool
245 _MotionImpl::IsSupported(MotionType type) const
246 {
247     bool isSupported = false;
248
249     switch (type)
250     {
251     case MOTION_TYPE_SNAP:
252         // fall through
253     case MOTION_TYPE_SHAKE:
254         // fall through
255     case MOTION_TYPE_DOUBLETAP:
256         // fall through
257     case MOTION_TYPE_MOVE_TO_EAR:
258
259         isSupported = true;
260         break;
261
262     default:
263         isSupported = false;
264         break;
265     }
266
267     SetLastResult(E_SUCCESS);
268
269     return isSupported;
270 }
271
272 bool
273 _MotionImpl::IsMotionTypeValid(unsigned long type)
274 {
275         if (type > MOTION_TYPE_NONE && type <= (MOTION_TYPE_SNAP | MOTION_TYPE_SHAKE | MOTION_TYPE_DOUBLETAP | MOTION_TYPE_MOVE_TO_EAR))
276         {
277                 return true;
278         }
279         else if (type == MOTION_TYPE_NONE || type == MOTION_TYPE_ALL)
280         {
281                 return true;
282         }
283         else
284         {
285                 return false;
286         }
287 }
288
289 _MotionImpl*
290 _MotionImpl::GetInstance(Motion& motion)
291 {
292     return motion.__pMotionImpl;
293 }
294
295 const _MotionImpl*
296 _MotionImpl::GetInstance(const Motion& motion)
297 {
298     return motion.__pMotionImpl;
299 }
300
301 } } } // Tizen::Uix::Sensor