1b7453777507fe42ac816b0de4c0899d7dc16be8
[platform/core/api/gesture.git] / src / Gesture.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdlib.h>
19 #include "GestureSensor.h"
20
21 #define FEATURE_GESTURE "tizen.org/feature/sensor.gesture_recognition"
22
23 #define GESTURE_FIRST   GESTURE_DOUBLE_TAP
24 #define GESTURE_LAST    GESTURE_WRIST_UP
25 #define IS_VALID_GESTURE(X)     (GESTURE_FIRST <= (X) && (X) <= GESTURE_LAST)
26
27 using namespace gesture;
28
29 struct _gesture_handle_s {
30         GestureSensor *sensor;
31 };
32
33 EXPORT_API int gesture_is_supported(gesture_type_e gesture, bool* supported)
34 {
35         if (supported)
36                 *supported = false;
37
38         ASSERT_SUPPORTED(FEATURE_GESTURE);
39         //LCOV_EXCL_START
40         ASSERT_NOT_NULL(supported);
41         IF_FAIL_RETURN(IS_VALID_GESTURE(gesture), ERR_INVALID_PARAMETER);
42
43         *supported = GestureSensor::isSupported(gesture);
44         return ERR_NONE;
45         //LCOV_EXCL_STOP
46 }
47
48 EXPORT_API int gesture_create(gesture_h *handle)
49 {
50         ASSERT_SUPPORTED(FEATURE_GESTURE);
51         //LCOV_EXCL_START
52         ASSERT_NOT_NULL(handle);
53
54         _gesture_handle_s *hdl = static_cast<gesture_h>(malloc(sizeof(_gesture_handle_s)));
55         IF_FAIL_RETURN_TAG(hdl, ERR_OPERATION_FAILED, _E, "Memory allocation failed");
56
57         hdl->sensor = new(std::nothrow) GestureSensor();
58         if (hdl->sensor == NULL) {
59                 _E("Memory allocation failed");
60                 free(hdl);
61                 return ERR_OPERATION_FAILED;
62         }
63
64         *handle = hdl;
65         return ERR_NONE;
66         //LCOV_EXCL_STOP
67 }
68
69 EXPORT_API int gesture_release(gesture_h handle)
70 {
71         ASSERT_SUPPORTED(FEATURE_GESTURE);
72         //LCOV_EXCL_START
73         ASSERT_NOT_NULL(handle);
74
75         delete handle->sensor;
76         free(handle);
77
78         return ERR_NONE;
79         //LCOV_EXCL_STOP
80 }
81
82 EXPORT_API int gesture_start_recognition(gesture_h handle, gesture_type_e gesture, gesture_option_e option, gesture_recognition_cb callback, void *user_data)
83 {
84         ASSERT_SUPPORTED(FEATURE_GESTURE);
85         //LCOV_EXCL_START
86         ASSERT_NOT_NULL(handle);
87         ASSERT_NOT_NULL(callback);
88         IF_FAIL_RETURN(IS_VALID_GESTURE(gesture), ERR_INVALID_PARAMETER);
89
90         if (option == GESTURE_OPTION_DEFAULT) {
91                 handle->sensor->setPowerSave(true);
92         } else if (option == GESTURE_OPTION_ALWAYS_ON) {
93                 handle->sensor->setPowerSave(false);
94         } else {
95                 return ERR_INVALID_PARAMETER;
96         }
97
98         if (!handle->sensor->setGesture(gesture)) {
99                 return GESTURE_ERROR_NOT_SUPPORTED;
100         }
101
102         handle->sensor->setCallback(callback);
103         handle->sensor->setUserData(user_data);
104
105         if (!handle->sensor->start()) {
106                 return ERR_OPERATION_FAILED;
107         }
108
109         return ERR_NONE;
110         //LCOV_EXCL_STOP
111 }
112
113 EXPORT_API int gesture_stop_recognition(gesture_h handle)
114 {
115         ASSERT_SUPPORTED(FEATURE_GESTURE);
116         //LCOV_EXCL_START
117         ASSERT_NOT_NULL(handle);
118
119         IF_FAIL_RETURN(handle->sensor->stop(), GESTURE_ERROR_NOT_STARTED);
120
121         return ERR_NONE;
122         //LCOV_EXCL_STOP
123 }
124
125 EXPORT_API int gesture_get_event(const gesture_data_h data, gesture_event_e *event)
126 {
127         ASSERT_SUPPORTED(FEATURE_GESTURE);
128         //LCOV_EXCL_START
129         ASSERT_NOT_NULL(data);
130         ASSERT_NOT_NULL(event);
131
132         if (data->gesture == GESTURE_TILT)
133                 return GESTURE_ERROR_NOT_SUPPORTED;
134
135         *event = static_cast<gesture_event_e>(data->event);
136
137         return ERR_NONE;
138         //LCOV_EXCL_STOP
139 }
140
141 EXPORT_API int gesture_get_tilt(const gesture_data_h data, int *x, int *y)
142 {
143         ASSERT_SUPPORTED(FEATURE_GESTURE);
144         //LCOV_EXCL_START
145         ASSERT_NOT_NULL(data);
146         ASSERT_NOT_NULL(x);
147         ASSERT_NOT_NULL(y);
148
149         if (data->gesture != GESTURE_TILT)
150                 return ERR_NOT_SUPPORTED;
151
152         *x = data->tilt_x;
153         *y = data->tilt_y;
154
155         return ERR_NONE;
156         //LCOV_EXCL_STOP
157 }