2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <devman_haptic.h>
29 #include "haptic_private.h"
32 #define LOG_TAG "TIZEN_SYSTEM_HAPTIC"
34 #define NOT_ASSIGNED -1
35 #define VIBE_SILENCE -128
37 #define _MSG_HAPTIC_ERROR_INVALID_PARAMETER "Invalid parameter"
38 #define _MSG_HAPTIC_ERROR_NO_SUCH_FILE "No such file"
39 #define _MSG_HAPTIC_ERROR_NOT_SUPPORTED_FORMAT "Not supported format"
40 #define _MSG_HAPTIC_ERROR_NOT_INITIALIZED "Not initialize"
41 #define _MSG_HAPTIC_ERROR_OPERATION_FAILED "Operation failed"
43 #define RETURN_ERR_MSG(err_code, msg) \
45 LOGE("[%s] "_MSG_##err_code"(0x%08x) : %s", __FUNCTION__, err_code, msg); \
49 #define RETURN_ERR(err_code) \
51 LOGE("[%s] "_MSG_##err_code"(0x%08x)", __FUNCTION__, err_code); \
61 struct _vibe_pattern {
62 haptic_vibration_iter_s *iters;
71 static const int UNDEFINED = 0;
72 static const int START = 1;
74 GArray *pattern_table = NULL;
76 static int initialize = 0;
77 static int max_device = 0;
79 static int* haptic_ids = NULL;
81 int haptic_get_count(int* vibrator_number)
84 if(vibrator_number == NULL)
85 RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
87 count = device_haptic_get_device_count();
89 RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
91 *vibrator_number = count;
93 return HAPTIC_ERROR_NONE;
96 int haptic_initialize()
102 return HAPTIC_ERROR_NONE;
105 if( haptic_get_count(&max_device) < 0)
106 RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
108 haptic_ids = (int*)malloc(sizeof(int)*max_device+1); // max + zero
110 if(haptic_ids == NULL){
111 RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
114 for(i=0; i<=max_device; i++){
115 id = device_haptic_open(_DEV[i], 0);
117 for (j=i; j>=0; j--){
118 device_haptic_close(haptic_ids[j]);
120 RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
128 pattern_table = g_array_new(FALSE, TRUE, sizeof(int));
132 return HAPTIC_ERROR_NONE;
135 int haptic_deinitialize()
141 RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
143 for(i=0; i<=max_device; i++){
144 // err = device_haptic_close(haptic_ids[i]);
145 device_haptic_close(haptic_ids[i]);
148 if(haptic_ids != NULL)
151 g_array_free(pattern_table, TRUE);
152 pattern_table = NULL;
156 return HAPTIC_ERROR_NONE;
160 int haptic_vibrate_monotone(int device_index, int duration_ms, int level)
164 if(device_index < 0 || device_index > max_device)
165 RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
167 if(level < HAPTIC_LEVEL_AUTO || level > HAPTIC_LEVEL_5)
168 RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
170 device_index = ((device_index < 3) ? device_index : 0); // xxx
173 RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
175 if(level == HAPTIC_LEVEL_0)
176 return HAPTIC_ERROR_NONE;
178 ret = device_haptic_play_monotone_with_detail_feedback_level(haptic_ids[device_index], duration_ms, level);
182 RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
184 RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
187 return HAPTIC_ERROR_NONE;
190 int haptic_stop_device(int device_index)
194 if(device_index < 0 || device_index > max_device)
195 RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
197 device_index = ((device_index < 3) ? device_index : 0); // xxx
200 RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
202 ret = device_haptic_stop_play(haptic_ids[device_index]);
206 RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
208 RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
211 return HAPTIC_ERROR_NONE;
214 static int _haptic_play_monotone(int device_index, long duration, int level)
218 if(device_index < 0 || device_index > max_device)
219 RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
221 device_index = ((device_index < 3) ? device_index : 0); // xxx
224 RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
226 if(level == HAPTIC_LEVEL_AUTO) {
227 level = HAPTIC_FEEDBACK_LEVEL_AUTO;
229 level = level > 100 ? 100 : (level < 0 ? 0 : level);
232 ret = device_haptic_play_monotone_with_detail_feedback_level(haptic_ids[device_index], duration, level);
236 RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
238 RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
241 return HAPTIC_ERROR_NONE;
244 static gboolean _haptic_play_iter(gpointer data)
247 struct _vibe_pattern* pattern = NULL;
249 pattern = (struct _vibe_pattern*)data;
254 if(pattern_table == NULL || g_array_index(pattern_table, int, pattern->pattern_index) != START) {
255 free(pattern->iters);
259 if(pattern->iters == NULL)
261 int current = pattern->current;
263 int device = pattern->iters[current].vibrator_index;
264 long time = pattern->iters[current].time;
265 int level = pattern->level;
267 // set default device, if can't find given device.
268 if(device >= max_device || device < 0)
271 if(level != 0 || time != 0){
272 err = _haptic_play_monotone(device, time, level);
274 pattern->error = err;
278 // pattern play finish
279 if(++pattern->current >= pattern->size){
280 if(pattern->iter_count <= 0){
281 free(pattern->iters);
285 pattern->current = 0;
286 pattern->iter_count--;
289 g_timeout_add(time, _haptic_play_iter, data);
294 int haptic_play_pattern(haptic_vibration_iter_s* pattern, int pattern_size, int count, int level, int* id)
299 RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
302 RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
305 RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
307 haptic_vibration_iter_s* tmp_ptn = (haptic_vibration_iter_s*)
308 malloc(sizeof(haptic_vibration_iter_s) * pattern_size);
310 RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
312 memcpy(tmp_ptn, pattern, sizeof(haptic_vibration_iter_s) * pattern_size);
314 struct _vibe_pattern* vibe_p = (struct _vibe_pattern*)malloc(sizeof(struct _vibe_pattern));
317 RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
319 vibe_p->iters = tmp_ptn;
320 vibe_p->size = pattern_size;
321 vibe_p->level = level;
322 vibe_p->iter_count = count;
326 for(i=0; i< pattern_table->len; i++){
327 if(g_array_index(pattern_table, int, i) == UNDEFINED){
333 g_array_append_val(pattern_table, START);
334 key = pattern_table->len -1;
336 g_array_index(pattern_table, int, key) = START;
339 vibe_p->pattern_index = key;
341 _haptic_play_iter((gpointer)vibe_p);
343 int error = vibe_p->error;
349 RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED);
351 RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
356 return HAPTIC_ERROR_NONE;
359 int haptic_stop_pattern(int id)
362 RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
364 if(!initialize || pattern_table == NULL)
365 RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED);
367 if(id >= pattern_table->len)
368 RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER);
370 g_array_index(pattern_table, int, id) = UNDEFINED;
372 return HAPTIC_ERROR_NONE;