e03c950044a9760bf4b85e23c49b3d53d69a0367
[platform/core/system/libsvi.git] / src / feedback.c
1 /*
2  * libfeedback
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
19 #include <stdio.h>
20 #include <string.h>
21 #include "feedback.h"
22 #include "feedback-internal.h"
23 #include "feedback-log.h"
24
25 #define MAX_PATH_LENGTH      256
26 #define NOT_ASSIGNED         NULL
27
28 #ifndef API
29 #define API __attribute__ ((visibility("default")))
30 #endif
31
32 static feedback_h feedback_handle = NOT_ASSIGNED;
33
34 API int feedback_initialize()
35 {
36         int err = -1;
37
38         if (feedback_handle != NOT_ASSIGNED) {
39                 FEEDBACK_LOG("Already initialized");
40                 return FEEDBACK_ERROR_NONE;
41         }
42
43         err = feedback_init(&feedback_handle);
44         if (FEEDBACK_FAILED(err)) {
45                 FEEDBACK_ERROR("feedback_init is failed");
46                 return FEEDBACK_ERROR_OPERATION_FAILED;
47         }
48
49         return FEEDBACK_ERROR_NONE;
50 }
51
52 API int feedback_deinitialize()
53 {
54         int err = -1;
55
56         if (feedback_handle == NOT_ASSIGNED) {
57                 FEEDBACK_ERROR("Not initialized");
58                 return FEEDBACK_ERROR_NOT_INITIALIZED;
59         }
60
61         err = feedback_fini(feedback_handle);
62         if (FEEDBACK_FAILED(err)) {
63                 FEEDBACK_ERROR("feedback_fini is failed");
64                 return FEEDBACK_ERROR_OPERATION_FAILED;
65         }
66
67         feedback_handle = NOT_ASSIGNED;
68         return FEEDBACK_ERROR_NONE;
69 }
70
71 API int feedback_play(feedback_pattern_e pattern)
72 {
73         int err = -1;
74
75         if (feedback_handle == NOT_ASSIGNED) {
76                 FEEDBACK_ERROR("Not initialized");
77                 return FEEDBACK_ERROR_NOT_INITIALIZED;
78         }
79
80         if (pattern < FEEDBACK_PATTERN_NONE || pattern >= FEEDBACK_PATTERN_END) {
81                 FEEDBACK_ERROR("Invalid parameter : pattern(%d)", pattern);
82                 return FEEDBACK_ERROR_INVALID_PARAMETER;
83         }
84
85         if (pattern == FEEDBACK_PATTERN_NONE) {
86                 FEEDBACK_LOG("pattern is NONE");
87                 return FEEDBACK_ERROR_NONE;
88         }
89
90         err = feedback_play_sound(feedback_handle, pattern);
91         if (FEEDBACK_FAILED(err)) {
92                 FEEDBACK_ERROR("feedback_play_sound is failed");
93                 return FEEDBACK_ERROR_OPERATION_FAILED;
94         }
95
96         err = feedback_play_vibration(feedback_handle, pattern);
97         if (FEEDBACK_FAILED(err)) {
98                 FEEDBACK_ERROR("feedback_play_vibration is failed");
99                 return FEEDBACK_ERROR_OPERATION_FAILED;
100         }
101
102         return FEEDBACK_ERROR_NONE;
103 }
104
105 API int feedback_play_type(feedback_type_e type, feedback_pattern_e pattern)
106 {
107         int err = -1;
108
109         if (feedback_handle == NOT_ASSIGNED) {
110                 FEEDBACK_ERROR("Not initialized");
111                 return FEEDBACK_ERROR_NOT_INITIALIZED;
112         }
113
114         if (type <= FEEDBACK_TYPE_NONE || type >= FEEDBACK_TYPE_END) {
115                 FEEDBACK_ERROR("Invalid parameter : type(%d)", type);
116                 return FEEDBACK_ERROR_INVALID_PARAMETER;
117         }
118
119         if (pattern < FEEDBACK_PATTERN_NONE || pattern >= FEEDBACK_PATTERN_END) {
120                 FEEDBACK_ERROR("Invalid parameter : pattern(%d)", pattern);
121                 return FEEDBACK_ERROR_INVALID_PARAMETER;
122         }
123
124         if (pattern == FEEDBACK_PATTERN_NONE) {
125                 FEEDBACK_LOG("pattern is NONE");
126                 return FEEDBACK_ERROR_NONE;
127         }
128
129         switch(type) {
130                 case FEEDBACK_TYPE_SOUND:
131                         err = feedback_play_sound(feedback_handle, pattern);
132                         break;
133                 case FEEDBACK_TYPE_VIBRATION:
134                         err = feedback_play_vibration(feedback_handle, pattern);
135                         break;
136                 default:
137                         FEEDBACK_ERROR("Invalid parameter : type(%d)", type);
138                 return FEEDBACK_ERROR_INVALID_PARAMETER;
139         }
140
141         if (FEEDBACK_FAILED(err)) {
142                 FEEDBACK_ERROR("feedback_play(type:%d) is failed", type);
143                 return FEEDBACK_ERROR_OPERATION_FAILED;
144         }
145
146         return FEEDBACK_ERROR_NONE;
147 }
148
149 API int feedback_get_resource_path(feedback_type_e type, feedback_pattern_e pattern, char** path)
150 {
151         int err = -1;
152         char buf[MAX_PATH_LENGTH] = {0,};
153
154         if (path == NULL) {
155                 FEEDBACK_ERROR("Invalid parameter : path(NULL)");
156                 return FEEDBACK_ERROR_INVALID_PARAMETER;
157         }
158
159         if (type <= FEEDBACK_TYPE_NONE || type >= FEEDBACK_TYPE_END) {
160                 FEEDBACK_ERROR("Invalid parameter : type(%d)", type);
161                 return FEEDBACK_ERROR_INVALID_PARAMETER;
162         }
163
164         if (pattern <= FEEDBACK_PATTERN_NONE || pattern >= FEEDBACK_PATTERN_END) {
165                 FEEDBACK_ERROR("Invalid parameter : pattern(%d)", pattern);
166                 return FEEDBACK_ERROR_INVALID_PARAMETER;
167         }
168
169         err = feedback_get_path(type, pattern, buf, MAX_PATH_LENGTH);
170         if (FEEDBACK_FAILED(err)) {
171                 FEEDBACK_ERROR("feedback_get_path is failed");
172                 return FEEDBACK_ERROR_OPERATION_FAILED;
173         }
174
175         *path = strdup(buf);
176
177         return FEEDBACK_ERROR_NONE;
178 }
179
180 API int feedback_set_resource_path(feedback_type_e type, feedback_pattern_e pattern, char* path)
181 {
182         int err = -1;
183
184         if (path == NULL) {
185                 FEEDBACK_ERROR("Invalid parameter : path(NULL)");
186                 return FEEDBACK_ERROR_INVALID_PARAMETER;
187         }
188
189         if (type <= FEEDBACK_TYPE_NONE || type >= FEEDBACK_TYPE_END) {
190                 FEEDBACK_ERROR("Invalid parameter : type(%d)", type);
191                 return FEEDBACK_ERROR_INVALID_PARAMETER;
192         }
193
194         if (pattern <= FEEDBACK_PATTERN_NONE || pattern >= FEEDBACK_PATTERN_END) {
195                 FEEDBACK_ERROR("Invalid parameter : pattern(%d)", pattern);
196                 return FEEDBACK_ERROR_INVALID_PARAMETER;
197         }
198
199         err = feedback_set_path(type, pattern, path);
200         if (FEEDBACK_FAILED(err)) {
201                 FEEDBACK_ERROR("feedback_set_path is failed");
202                 return FEEDBACK_ERROR_OPERATION_FAILED;
203         }
204
205         return FEEDBACK_ERROR_NONE;
206 }