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.
23 #include <sys/types.h>
29 #include "haptic_legacy.h"
30 #include "haptic_plugin_intf.h"
31 #include "haptic_log.h"
33 #define HAPTIC_MODULE_PATH "/usr/lib/libhaptic-module.so"
40 static int has_haptic_module = MODULE_NONE;
42 /* Haptic Plugin Interface */
43 static void *dlopen_handle;
44 static const haptic_plugin_interface *plugin_intf;
46 /* START of Static Function Section */
47 static haptic_feedback_e __get_setting_feedback_level()
49 int setting_fb_level = -1;
51 if (vconf_get_int(VCONFKEY_SETAPPL_TOUCH_FEEDBACK_VIBRATION_LEVEL_INT, &setting_fb_level) < 0) {
52 setting_fb_level = SETTING_VIB_FEEDBACK_LEVEL3;
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;
74 static haptic_error_e __error_to_haptic_type(int 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;
83 return HAPTIC_ERROR_OPERATION_FAILED;
85 /* END of Static Function Section */
87 /* START: haptic legacy APIs */
88 int _haptic_get_count(int *device_number)
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;
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);
104 *device_number = number;
105 return HAPTIC_ERROR_NONE;
108 int _haptic_open(haptic_device_e device_index, haptic_device_h *device_handle)
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;
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);
124 *device_handle = (haptic_device_h)handle;
125 return HAPTIC_ERROR_NONE;
128 int _haptic_close(haptic_device_h device_handle)
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;
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);
143 return HAPTIC_ERROR_NONE;
146 int _haptic_vibrate_monotone(haptic_device_h device_handle, int duration, int feedback, int priority, haptic_effect_h *effect_handle)
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;
156 if (feedback == HAPTIC_FEEDBACK_AUTO) {
157 HAPTIC_LOG("Auto feedback level, feedback value will be changed");
158 feedback = __get_setting_feedback_level();
161 if (feedback == HAPTIC_FEEDBACK_0) {
162 HAPTIC_LOG("feedback_level is 0, so do not vibe");
163 return HAPTIC_ERROR_NONE;
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);
173 *effect_handle = (haptic_effect_h)handle;
174 return HAPTIC_ERROR_NONE;
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)
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;
187 if (feedback == HAPTIC_FEEDBACK_AUTO) {
188 HAPTIC_LOG("Auto feedback level, feedback value will be changed");
189 feedback = __get_setting_feedback_level();
192 if (feedback == HAPTIC_FEEDBACK_0) {
193 HAPTIC_LOG("feedback_level is 0, so do not vibe");
194 return HAPTIC_ERROR_NONE;
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);
204 *effect_handle = (haptic_effect_h)handle;
205 return HAPTIC_ERROR_NONE;
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)
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;
219 if (device_handle_count == 0) {
220 HAPTIC_ERROR("Haptic device is not initialized");
221 return HAPTIC_ERROR_NOT_INITIALIZED;
225 if (feedback == HAPTIC_FEEDBACK_AUTO) {
226 HAPTIC_LOG("Auto feedback level, feedback value will be changed");
227 feedback = __get_setting_feedback_level();
230 if (feedback == HAPTIC_FEEDBACK_0) {
231 HAPTIC_LOG("feedback_level is 0, so do not vibe");
232 return HAPTIC_ERROR_NONE;
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);
242 *effect_handle = (haptic_effect_h)handle;
243 return HAPTIC_ERROR_NONE;
246 int _haptic_stop_effect(haptic_device_h device_handle, haptic_effect_h effect_handle)
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;
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);
261 return HAPTIC_ERROR_NONE;
264 int _haptic_stop_all_effects(haptic_device_h device_handle)
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;
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);
279 return HAPTIC_ERROR_NONE;
282 int _haptic_get_effect_state(haptic_device_h device_handle, haptic_effect_h effect_handle, haptic_state_e *effect_state)
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;
293 if (device_handle_count == 0) {
294 HAPTIC_ERROR("Haptic device is not initialized");
295 return HAPTIC_ERROR_NOT_INITIALIZED;
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);
305 *effect_state = state;
306 return HAPTIC_ERROR_NONE;
309 int _haptic_create_effect(const unsigned char *vibe_buffer, int max_bufsize, haptic_effect_element_s *elem_arr, int max_elemcnt)
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;
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);
324 return HAPTIC_ERROR_NONE;
327 int _haptic_save_effect(const unsigned char *vibe_buffer, int max_bufsize, const char *file_path)
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;
337 if (device_handle_count == 0) {
338 HAPTIC_ERROR("Haptic device is not initialized");
339 return HAPTIC_ERROR_NOT_INITIALIZED;
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);
350 return HAPTIC_ERROR_NONE;
353 int _haptic_get_file_duration(haptic_device_h device_handle, const char *file_path, int *file_duration)
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;
364 if (device_handle_count == 0) {
365 HAPTIC_ERROR("Haptic device is not initialized");
366 return HAPTIC_ERROR_NOT_INITIALIZED;
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);
376 *file_duration = duration;
377 return HAPTIC_ERROR_NONE;
380 int _haptic_get_buffer_duration(haptic_device_h device_handle, const unsigned char *vibe_buffer, int *buffer_duration)
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;
391 if (device_handle_count == 0) {
392 HAPTIC_ERROR("Haptic device is not initialized");
393 return HAPTIC_ERROR_NOT_INITIALIZED;
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);
403 *buffer_duration = duration;
404 return HAPTIC_ERROR_NONE;
406 /* END: haptic legacy APIs */
408 static void __attribute__ ((constructor)) module_init()
412 if (stat(HAPTIC_MODULE_PATH, &buf)) {
413 HAPTIC_ERROR("file(%s) is not presents", HAPTIC_MODULE_PATH);
417 dlopen_handle = dlopen(HAPTIC_MODULE_PATH, RTLD_NOW);
418 if (!dlopen_handle) {
419 HAPTIC_ERROR("dlopen failed: %s", dlerror());
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());
430 plugin_intf = get_haptic_plugin_interface();
432 HAPTIC_ERROR("get_haptic_plugin_interface() failed");
436 has_haptic_module = MODULE_EXIST;
437 HAPTIC_LOG("This device can vibe");
441 has_haptic_module = MODULE_NONE;
442 HAPTIC_LOG("This device can not vibe");
446 static void __attribute__ ((destructor)) module_fini()
448 if (has_haptic_module == MODULE_NONE)
452 dlclose(dlopen_handle);