2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <FBaseErrors.h>
19 #include <FUixSensorMotion.h>
20 #include <FBaseSysLog.h>
21 #include <FUixSensor_MotionImpl.h>
23 using namespace Tizen::Base;
25 namespace Tizen { namespace Uix { namespace Sensor
29 _MotionImpl::__MotionEventReceiver(unsigned int eventType, sensor_event_data_t* pEventData, void* pData)
31 int eventData = *((int*) pEventData->event_data);
32 IMotionEventListener* pMotionEventListener = (IMotionEventListener*) pData;
34 if (pMotionEventListener != null)
38 case MOTION_ENGINE_EVENT_SNAP:
40 SysLog(NID_UIX, "Detected SNAP event(%d)\n", eventData);
43 case MOTION_ENGIEN_POSITIVE_SNAP_X:
44 pMotionEventListener->OnSnapDetected(MOTION_SNAP_X_POSITIVE);
47 case MOTION_ENGIEN_NEGATIVE_SNAP_X:
48 pMotionEventListener->OnSnapDetected(MOTION_SNAP_X_NEGATIVE);
51 case MOTION_ENGIEN_POSITIVE_SNAP_Y:
52 pMotionEventListener->OnSnapDetected(MOTION_SNAP_Y_POSITIVE);
55 case MOTION_ENGIEN_NEGATIVE_SNAP_Y:
56 pMotionEventListener->OnSnapDetected(MOTION_SNAP_Y_NEGATIVE);
59 case MOTION_ENGIEN_POSITIVE_SNAP_Z:
60 pMotionEventListener->OnSnapDetected(MOTION_SNAP_Z_POSITIVE);
63 case MOTION_ENGIEN_NEGATIVE_SNAP_Z:
64 pMotionEventListener->OnSnapDetected(MOTION_SNAP_Z_NEGATIVE);
70 case MOTION_ENGINE_EVENT_SHAKE:
72 SysLog(NID_UIX, "Detected SHAKE event(%d)\n", eventData);
75 case MOTION_ENGIEN_SHAKE_DETECTION:
76 pMotionEventListener->OnShakeDetected(MOTION_STARTED);
79 case MOTION_ENGIEN_SHAKE_CONTINUING:
80 pMotionEventListener->OnShakeDetected(MOTION_INPROGRESS);
83 case MOTION_ENGIEN_SHAKE_FINISH:
84 pMotionEventListener->OnShakeDetected(MOTION_ENDED);
90 case MOTION_ENGINE_EVENT_DOUBLETAP:
92 SysLog(NID_UIX, "Detected DOUBLETAP event(%d)\n", eventData);
94 if (eventData == MOTION_ENGIEN_DOUBLTAP_DETECTION)
95 pMotionEventListener->OnDoubleTapDetected();
100 case MOTION_ENGINE_EVENT_DIRECT_CALL:
102 SysLog(NID_UIX, "Detected MOVE_TO_EAR event(%d)\n", eventData);
104 if (eventData == MOTION_ENGINE_DIRECT_CALL_DETECTION)
105 pMotionEventListener->OnMoveToEarDetected();
113 _MotionImpl::_MotionImpl(void)
114 : __pIMotionEventListener(null)
116 , __motionType(MOTION_TYPE_NONE)
120 _MotionImpl::~_MotionImpl(void)
124 ret = sf_stop(__handle);
126 SysLog(NID_UIX, "Failed to call sf_stop() method.");
128 ret = sf_disconnect(__handle);
130 SysLog(NID_UIX, "Failed to call sf_disconnect() method.");
134 _MotionImpl::Construct(IMotionEventListener& listener)
136 result r = E_SUCCESS;
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");
145 __pIMotionEventListener = &listener;
150 _MotionImpl::SetEnabled(unsigned long type)
154 SysLog(NID_UIX, "type(%d) / __motionType(%d)", (int)type, (int)__motionType);
156 if (type & MOTION_TYPE_SNAP)
158 if (__motionType & MOTION_TYPE_SNAP)
160 SysLog(NID_UIX, "MOTION_TYPE_SNAP is already registered.");
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.");
170 if (__motionType & MOTION_TYPE_SNAP)
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.");
177 if (type & MOTION_TYPE_SHAKE)
179 if (__motionType & MOTION_TYPE_SHAKE)
181 SysLog(NID_UIX, "MOTION_TYPE_SHAKE is already registered.");
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.");
191 if (__motionType & MOTION_TYPE_SHAKE)
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.");
198 if (type & MOTION_TYPE_DOUBLETAP)
200 if (__motionType & MOTION_TYPE_DOUBLETAP)
202 SysLog(NID_UIX, "MOTION_TYPE_DOUBLETAP is already registered.");
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.");
212 if (__motionType & MOTION_TYPE_DOUBLETAP)
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.");
219 if (type & MOTION_TYPE_MOVE_TO_EAR)
221 if (__motionType & MOTION_TYPE_MOVE_TO_EAR)
223 SysLog(NID_UIX, "MOTION_TYPE_MOVE_TO_EAR is already registered.");
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.");
233 if (__motionType & MOTION_TYPE_MOVE_TO_EAR)
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.");
241 SetLastResult(E_SUCCESS);
245 _MotionImpl::IsSupported(MotionType type) const
247 bool isSupported = false;
251 case MOTION_TYPE_SNAP:
253 case MOTION_TYPE_SHAKE:
255 case MOTION_TYPE_DOUBLETAP:
257 case MOTION_TYPE_MOVE_TO_EAR:
267 SetLastResult(E_SUCCESS);
273 _MotionImpl::IsMotionTypeValid(unsigned long type)
275 if (type > MOTION_TYPE_NONE && type <= (MOTION_TYPE_SNAP | MOTION_TYPE_SHAKE | MOTION_TYPE_DOUBLETAP | MOTION_TYPE_MOVE_TO_EAR))
279 else if (type == MOTION_TYPE_NONE || type == MOTION_TYPE_ALL)
290 _MotionImpl::GetInstance(Motion& motion)
292 return motion.__pMotionImpl;
296 _MotionImpl::GetInstance(const Motion& motion)
298 return motion.__pMotionImpl;
301 } } } // Tizen::Uix::Sensor