aaa88e815ab8ed9a24e92678f8c1a670ef1b776a
[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     }
101 }
102
103 _MotionImpl::_MotionImpl(void)
104     : __pIMotionEventListener(null)
105     , __handle(-1)
106     , __motionType(MOTION_TYPE_NONE)
107 {
108 }
109
110 _MotionImpl::~_MotionImpl(void)
111 {
112     int ret = 0;
113
114     ret = sf_stop(__handle);
115     if (ret != 0)
116         SysLog(NID_UIX, "Failed to call sf_stop() method.");
117
118     ret = sf_disconnect(__handle);
119     if (ret != 0)
120         SysLog(NID_UIX, "Failed to call sf_disconnect() method.");
121 }
122
123 result
124 _MotionImpl::Construct(IMotionEventListener& listener)
125 {
126     result r = E_SUCCESS;
127     int ret = 0;
128     ret = sf_is_sensor_event_available(MOTION_SENSOR, 0);
129     SysTryReturn(NID_UIX, ret == 0, E_UNSUPPORTED_OPERATION, E_UNSUPPORTED_OPERATION, "[E_UNSUPPORTED_OPERATION] The motion detection feature is not supported");
130     __handle = sf_connect(MOTION_SENSOR);
131     SysTryReturn(NID_UIX, __handle >= 0, E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Sensor handle was not created properly");
132     ret = sf_start(__handle, 0);
133     SysTryReturn(NID_UIX, ret == 0, E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Sensor couldn't start");
134
135     __pIMotionEventListener = &listener;
136     return r;
137 }
138
139 void
140 _MotionImpl::SetEnabled(unsigned long type)
141 {
142     int ret = 0;
143
144     SysLog(NID_UIX, "type(%d) / __motionType(%d)", (int)type, (int)__motionType);
145
146     if (type & MOTION_TYPE_SNAP)
147     {
148         if (__motionType & MOTION_TYPE_SNAP)
149         {
150             SysLog(NID_UIX, "MOTION_TYPE_SNAP is already registered.");
151         }
152         else
153         {
154             ret = sf_register_event(__handle, MOTION_ENGINE_EVENT_SNAP, null, __MotionEventReceiver, (void*) __pIMotionEventListener);
155             SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to register motion snap event.");
156         }
157     }
158     else
159     {
160         if (__motionType & MOTION_TYPE_SNAP)
161         {
162             ret = sf_unregister_event(__handle, MOTION_ENGINE_EVENT_SNAP);
163             SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to unregister motion snap event.");
164         }
165     }
166
167     if (type & MOTION_TYPE_SHAKE)
168     {
169         if (__motionType & MOTION_TYPE_SHAKE)
170         {
171             SysLog(NID_UIX, "MOTION_TYPE_SHAKE is already registered.");
172         }
173         else
174         {
175             ret = sf_register_event(__handle, MOTION_ENGINE_EVENT_SHAKE, null, __MotionEventReceiver, (void*) __pIMotionEventListener);
176             SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to register motion shake event.");
177         }
178     }
179     else
180     {
181         if (__motionType & MOTION_TYPE_SHAKE)
182         {
183             ret = sf_unregister_event(__handle, MOTION_ENGINE_EVENT_SHAKE);
184             SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to unregister motion shake event.");
185         }
186     }
187
188     if (type & MOTION_TYPE_DOUBLETAP)
189     {
190         if (__motionType & MOTION_TYPE_DOUBLETAP)
191         {
192             SysLog(NID_UIX, "MOTION_TYPE_DOUBLETAP is already registered.");
193         }
194         else
195         {
196             ret = sf_register_event(__handle, MOTION_ENGINE_EVENT_DOUBLETAP, null, __MotionEventReceiver, (void*) __pIMotionEventListener);
197             SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to register motion double tap event.");
198         }
199     }
200     else
201     {
202         if (__motionType & MOTION_TYPE_DOUBLETAP)
203         {
204             ret = sf_unregister_event(__handle, MOTION_ENGINE_EVENT_DOUBLETAP);
205             SysTryReturnVoidResult(NID_UIX, ret == 0, E_INVALID_STATE, "[E_INVALID_STATE] Failed to unregister motion double tap event.");
206         }
207     }
208     __motionType = type;
209
210     SetLastResult(E_SUCCESS);
211 }
212
213 bool
214 _MotionImpl::IsSupported(MotionType type) const
215 {
216     bool isSupported = false;
217
218     switch (type)
219     {
220     case MOTION_TYPE_SNAP:
221         // fall through
222     case MOTION_TYPE_SHAKE:
223         // fall through
224     case MOTION_TYPE_DOUBLETAP:
225         isSupported = true;
226         break;
227
228     default:
229         isSupported = false;
230         break;
231     }
232
233     SetLastResult(E_SUCCESS);
234
235     return isSupported;
236 }
237
238 bool
239 _MotionImpl::IsMotionTypeValid(unsigned long type)
240 {
241         if (type > MOTION_TYPE_NONE && type <= (MOTION_TYPE_SNAP | MOTION_TYPE_SHAKE | MOTION_TYPE_DOUBLETAP))
242         {
243                 return true;
244         }
245         else if (type == MOTION_TYPE_NONE || type == MOTION_TYPE_ALL)
246         {
247                 return true;
248         }
249         else
250         {
251                 return false;
252         }
253 }
254
255 _MotionImpl*
256 _MotionImpl::GetInstance(Motion& motion)
257 {
258     return motion.__pMotionImpl;
259 }
260
261 const _MotionImpl*
262 _MotionImpl::GetInstance(const Motion& motion)
263 {
264     return motion.__pMotionImpl;
265 }
266
267 } } } // Tizen::Uix::Sensor