Tizen 2.0 Release
[framework/system/libhaptic.git] / src / haptic_legacy.c
1 /*
2  * haptic
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 <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <dlfcn.h>
27 #include <vconf.h>
28
29 #include "haptic_legacy.h"
30 #include "haptic_plugin_intf.h"
31 #include "haptic_log.h"
32
33 #define HAPTIC_MODULE_PATH              "/usr/lib/libhaptic-module.so"
34
35 static enum {
36         MODULE_NONE = 0,
37         MODULE_EXIST,
38 };
39
40 static int has_haptic_module = MODULE_NONE;
41
42 /* Haptic Plugin Interface */
43 static void *dlopen_handle;
44 static const haptic_plugin_interface *plugin_intf;
45
46 /* START of Static Function Section */
47 static haptic_feedback_e __get_setting_feedback_level()
48 {
49         int setting_fb_level = -1;
50
51         if (vconf_get_int(VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT, &setting_fb_level) < 0) {
52                 setting_fb_level = SETTING_VIB_FEEDBACK_LEVEL3;
53         }
54
55         switch (setting_fb_level) {
56         case SETTING_VIB_FEEDBACK_LEVEL0:
57                 return HAPTIC_FEEDBACK_0;
58         case SETTING_VIB_FEEDBACK_LEVEL1:
59                 return HAPTIC_FEEDBACK_1;
60         case SETTING_VIB_FEEDBACK_LEVEL2:
61                 return HAPTIC_FEEDBACK_2;
62         case SETTING_VIB_FEEDBACK_LEVEL3:
63                 return HAPTIC_FEEDBACK_3;
64         case SETTING_VIB_FEEDBACK_LEVEL4:
65                 return HAPTIC_FEEDBACK_4;
66         case SETTING_VIB_FEEDBACK_LEVEL5:
67                 return HAPTIC_FEEDBACK_5;
68         default:
69                 break;
70         }
71         return -1;
72 }
73
74 static haptic_error_e __error_to_haptic_type(int error)
75 {
76         switch(error) {
77         case HAPTIC_MODULE_ERROR_NONE          : return HAPTIC_ERROR_NONE;
78         case HAPTIC_MODULE_NOT_INITIALIZED     : return HAPTIC_ERROR_NOT_INITIALIZED;
79         case HAPTIC_MODULE_INVALID_ARGUMENT    : return HAPTIC_ERROR_INVALID_PARAMETER;
80         default:
81                 break;
82         }
83         return HAPTIC_ERROR_OPERATION_FAILED;
84 }
85 /* END of Static Function Section */
86
87 /* START: haptic legacy APIs */
88 int _haptic_get_count(int *device_number)
89 {
90         int ret = -1;
91         int number = -1;
92
93         if (has_haptic_module == MODULE_NONE) {
94                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
95                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
96         }
97
98         ret = plugin_intf->haptic_internal_get_device_count(&number);
99         if (ret != HAPTIC_MODULE_ERROR_NONE) {
100                 HAPTIC_ERROR("haptic_internal_get_device_count is failed : %d", ret);
101                 return __error_to_haptic_type(ret);
102         }
103
104         *device_number = number;
105         return HAPTIC_ERROR_NONE;
106 }
107
108 int _haptic_open(haptic_device_e device_index, haptic_device_h *device_handle)
109 {
110         int ret = -1;
111         int handle = -1;
112
113         if (has_haptic_module == MODULE_NONE) {
114                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
115                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
116         }
117
118         ret = plugin_intf->haptic_internal_open_device(device_index, &handle);
119         if (ret != HAPTIC_MODULE_ERROR_NONE) {
120                 HAPTIC_ERROR("haptic_internal_open_device is failed : %d", ret);
121                 return __error_to_haptic_type(ret);
122         }
123
124         *device_handle = (haptic_device_h)handle;
125         return HAPTIC_ERROR_NONE;
126 }
127
128 int _haptic_close(haptic_device_h device_handle)
129 {
130         int ret = -1;
131
132         if (has_haptic_module == MODULE_NONE) {
133                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
134                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
135         }
136
137         ret = plugin_intf->haptic_internal_close_device((int)device_handle);
138         if (ret != HAPTIC_MODULE_ERROR_NONE) {
139                 HAPTIC_ERROR("haptic_internal_close_device is failed : %d", ret);
140                 return __error_to_haptic_type(ret);
141         }
142
143         return HAPTIC_ERROR_NONE;
144 }
145
146 int _haptic_vibrate_monotone(haptic_device_h device_handle, int duration, int feedback, int priority, haptic_effect_h *effect_handle)
147 {
148         int ret = -1;
149         int handle = -1;
150
151         if (has_haptic_module == MODULE_NONE) {
152                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
153                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
154         }
155
156         if (feedback == HAPTIC_FEEDBACK_AUTO) {
157                 HAPTIC_LOG("Auto feedback level, feedback value will be changed");
158                 feedback = __get_setting_feedback_level();
159         }
160
161         if (feedback == HAPTIC_FEEDBACK_0) {
162                 HAPTIC_LOG("feedback_level is 0, so do not vibe");
163                 return HAPTIC_ERROR_NONE;
164         }
165
166         HAPTIC_LOG("duration : %d, feedback : %d, priority : %d", duration, feedback, priority);
167         ret = plugin_intf->haptic_internal_vibrate_monotone((int)device_handle, duration, feedback, priority, &handle);
168         if (ret != HAPTIC_MODULE_ERROR_NONE) {
169                 HAPTIC_ERROR("haptic_internal_vibrate_monotone is failed : %d", ret);
170                 return __error_to_haptic_type(ret);
171         }
172
173         *effect_handle = (haptic_effect_h)handle;
174         return HAPTIC_ERROR_NONE;
175 }
176
177 int _haptic_vibrate_file(haptic_device_h device_handle, const char *file_name, int iteration, int feedback, int priority, haptic_effect_h *effect_handle)
178 {
179         int ret = -1;
180         int handle = -1;
181
182         if (has_haptic_module == MODULE_NONE) {
183                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
184                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
185         }
186
187         if (feedback == HAPTIC_FEEDBACK_AUTO) {
188                 HAPTIC_LOG("Auto feedback level, feedback value will be changed");
189                 feedback = __get_setting_feedback_level();
190         }
191
192         if (feedback == HAPTIC_FEEDBACK_0) {
193                 HAPTIC_LOG("feedback_level is 0, so do not vibe");
194                 return HAPTIC_ERROR_NONE;
195         }
196
197         HAPTIC_LOG("file_name : %s, iteration : %d, feedback : %d, priority : %d", file_name, iteration, feedback, priority);
198         ret = plugin_intf->haptic_internal_vibrate_file((int)device_handle, file_name, iteration, feedback, priority, &handle);
199         if (ret != HAPTIC_MODULE_ERROR_NONE) {
200                 HAPTIC_ERROR("haptic_internal_vibrate_file is failed : %d", ret);
201                 return __error_to_haptic_type(ret);
202         }
203
204         *effect_handle = (haptic_effect_h)handle;
205         return HAPTIC_ERROR_NONE;
206 }
207
208 int _haptic_vibrate_buffer(haptic_device_h device_handle, const unsigned char *vibe_buffer, int iteration, int feedback, int priority, haptic_effect_h *effect_handle)
209 {
210         int ret = -1;
211         int handle = -1;
212
213         if (has_haptic_module == MODULE_NONE) {
214                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
215                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
216         }
217
218 /*
219         if (device_handle_count == 0) {
220                 HAPTIC_ERROR("Haptic device is not initialized");
221                 return HAPTIC_ERROR_NOT_INITIALIZED;
222         }
223 */
224
225         if (feedback == HAPTIC_FEEDBACK_AUTO) {
226                 HAPTIC_LOG("Auto feedback level, feedback value will be changed");
227                 feedback = __get_setting_feedback_level();
228         }
229
230         if (feedback == HAPTIC_FEEDBACK_0) {
231                 HAPTIC_LOG("feedback_level is 0, so do not vibe");
232                 return HAPTIC_ERROR_NONE;
233         }
234
235         HAPTIC_LOG("iteration : %d, feedback : %d, priority : %d", iteration, feedback, priority);
236         ret = plugin_intf->haptic_internal_vibrate_buffer((int)device_handle, vibe_buffer, iteration, feedback, priority, &handle);
237         if (ret != HAPTIC_MODULE_ERROR_NONE) {
238                 HAPTIC_ERROR("haptic_internal_vibrate_buffer is failed : %d", ret);
239                 return __error_to_haptic_type(ret);
240         }
241
242         *effect_handle = (haptic_effect_h)handle;
243         return HAPTIC_ERROR_NONE;
244 }
245
246 int _haptic_stop_effect(haptic_device_h device_handle, haptic_effect_h effect_handle)
247 {
248         int ret = -1;
249
250         if (has_haptic_module == MODULE_NONE) {
251                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
252                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
253         }
254
255         ret = plugin_intf->haptic_internal_stop_effect((int)device_handle, (int)effect_handle);
256         if (ret != HAPTIC_MODULE_ERROR_NONE) {
257                 HAPTIC_ERROR("haptic_internal_stop_effect is failed : %d", ret);
258                 return __error_to_haptic_type(ret);
259         }
260
261         return HAPTIC_ERROR_NONE;
262 }
263
264 int _haptic_stop_all_effects(haptic_device_h device_handle)
265 {
266         int ret = -1;
267
268         if (has_haptic_module == MODULE_NONE) {
269                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
270                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
271         }
272
273         ret = plugin_intf->haptic_internal_stop_all_effects((int)device_handle);
274         if (ret != HAPTIC_MODULE_ERROR_NONE) {
275                 HAPTIC_ERROR("haptic_internal_stop_all_effects is failed : %d", ret);
276                 return __error_to_haptic_type(ret);
277         }
278
279         return HAPTIC_ERROR_NONE;
280 }
281
282 int _haptic_get_effect_state(haptic_device_h device_handle, haptic_effect_h effect_handle, haptic_state_e *effect_state)
283 {
284         int ret = -1;
285         int state = -1;
286
287         if (has_haptic_module == MODULE_NONE) {
288                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
289                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
290         }
291
292 /*
293         if (device_handle_count == 0) {
294                 HAPTIC_ERROR("Haptic device is not initialized");
295                 return HAPTIC_ERROR_NOT_INITIALIZED;
296         }
297 */
298
299         ret = plugin_intf->haptic_internal_get_effect_state((int)device_handle, (int)effect_handle, &state);
300         if (ret != HAPTIC_MODULE_ERROR_NONE) {
301                 HAPTIC_ERROR("haptic_internal_get_effect_state is failed : %d", ret);
302                 return __error_to_haptic_type(ret);
303         }
304
305         *effect_state = state;
306         return HAPTIC_ERROR_NONE;
307 }
308
309 int _haptic_create_effect(const unsigned char *vibe_buffer, int max_bufsize, haptic_effect_element_s *elem_arr, int max_elemcnt)
310 {
311         int ret = -1;
312
313         if (has_haptic_module == MODULE_NONE) {
314                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
315                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
316         }
317
318         ret = plugin_intf->haptic_internal_create_effect(vibe_buffer, max_bufsize, (haptic_module_effect_element*)elem_arr, max_elemcnt);
319         if (ret != HAPTIC_MODULE_ERROR_NONE) {
320                 HAPTIC_ERROR("haptic_internal_create_effect is failed : %d", ret);
321                 return __error_to_haptic_type(ret);
322         }
323
324         return HAPTIC_ERROR_NONE;
325 }
326
327 int _haptic_save_effect(const unsigned char *vibe_buffer, int max_bufsize, const char *file_path)
328 {
329         int ret = -1;
330
331         if (has_haptic_module == MODULE_NONE) {
332                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
333                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
334         }
335
336 /*
337         if (device_handle_count == 0) {
338                 HAPTIC_ERROR("Haptic device is not initialized");
339                 return HAPTIC_ERROR_NOT_INITIALIZED;
340         }
341 */
342
343         HAPTIC_LOG("file path : %s", file_path);
344         ret = plugin_intf->haptic_internal_save_effect(vibe_buffer, max_bufsize, file_path);
345         if (ret != HAPTIC_MODULE_ERROR_NONE) {
346                 HAPTIC_ERROR("haptic_internal_save_effect is failed : %d", ret);
347                 return __error_to_haptic_type(ret);
348         }
349
350         return HAPTIC_ERROR_NONE;
351 }
352
353 int _haptic_get_file_duration(haptic_device_h device_handle, const char *file_path, int *file_duration)
354 {
355         int ret = -1;
356         int duration = -1;
357
358         if (has_haptic_module == MODULE_NONE) {
359                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
360                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
361         }
362
363 /*
364         if (device_handle_count == 0) {
365                 HAPTIC_ERROR("Haptic device is not initialized");
366                 return HAPTIC_ERROR_NOT_INITIALIZED;
367         }
368 */
369
370         ret = plugin_intf->haptic_internal_get_file_duration((int)device_handle, file_path, &duration);
371         if (ret != HAPTIC_MODULE_ERROR_NONE) {
372                 HAPTIC_ERROR("haptic_internal_stop_get_file_duration is failed : %d", ret);
373                 return __error_to_haptic_type(ret);
374         }
375
376         *file_duration = duration;
377         return HAPTIC_ERROR_NONE;
378 }
379
380 int _haptic_get_buffer_duration(haptic_device_h device_handle, const unsigned char *vibe_buffer, int *buffer_duration)
381 {
382         int ret = -1;
383         int duration = -1;
384
385         if (has_haptic_module == MODULE_NONE) {
386                 HAPTIC_ERROR("%s() is not supported without specific haptic module", __func__);
387                 return HAPTIC_ERROR_NOT_SUPPORTED_DEVICE;
388         }
389
390 /*
391         if (device_handle_count == 0) {
392                 HAPTIC_ERROR("Haptic device is not initialized");
393                 return HAPTIC_ERROR_NOT_INITIALIZED;
394         }
395 */
396
397         ret = plugin_intf->haptic_internal_get_buffer_duration((int)device_handle, vibe_buffer, &duration);
398         if (ret != HAPTIC_MODULE_ERROR_NONE) {
399                 HAPTIC_ERROR("haptic_internal_stop_get_buffer_duration is failed : %d", ret);
400                 return __error_to_haptic_type(ret);
401         }
402
403         *buffer_duration = duration;
404         return HAPTIC_ERROR_NONE;
405 }
406 /* END: haptic legacy APIs */
407
408 static void __attribute__ ((constructor)) module_init()
409 {
410         struct stat buf;
411
412         if (stat(HAPTIC_MODULE_PATH, &buf)) {
413                 HAPTIC_ERROR("file(%s) is not presents", HAPTIC_MODULE_PATH);
414                 goto EXIT;
415         }
416
417         dlopen_handle = dlopen(HAPTIC_MODULE_PATH, RTLD_NOW);
418         if (!dlopen_handle) {
419                 HAPTIC_ERROR("dlopen failed: %s", dlerror());
420                 goto EXIT;
421         }
422
423         const haptic_plugin_interface *(*get_haptic_plugin_interface) () = NULL;
424         get_haptic_plugin_interface = dlsym(dlopen_handle, "get_haptic_plugin_interface");
425         if (!get_haptic_plugin_interface) {
426                 HAPTIC_ERROR("dlsym failed : %s", dlerror());
427                 goto EXIT;
428         }
429
430         plugin_intf = get_haptic_plugin_interface();
431         if (!plugin_intf) {
432                 HAPTIC_ERROR("get_haptic_plugin_interface() failed");
433                 goto EXIT;
434         }
435
436         has_haptic_module = MODULE_EXIST;
437         HAPTIC_LOG("This device can vibe");
438         return;
439
440 EXIT:
441         has_haptic_module = MODULE_NONE;
442         HAPTIC_LOG("This device can not vibe");
443         return;
444 }
445
446 static void __attribute__ ((destructor)) module_fini()
447 {
448         if (has_haptic_module == MODULE_NONE)
449                 return;
450
451         if (dlopen_handle) {
452                 dlclose(dlopen_handle);
453         }
454 }