diff options
Diffstat (limited to 'src/haptic.c')
-rw-r--r--[-rwxr-xr-x] | src/haptic.c | 218 |
1 files changed, 62 insertions, 156 deletions
diff --git a/src/haptic.c b/src/haptic.c index 3c38779..c05fd2a 100755..100644 --- a/src/haptic.c +++ b/src/haptic.c @@ -58,28 +58,19 @@ static int _DEV[] = { DEV_IDX_1, }; -static int _LEVEL[] = -{ - HAPTIC_FEEDBACK_LEVEL_AUTO, - VIBE_SILENCE, - HAPTIC_FEEDBACK_LEVEL_1, - HAPTIC_FEEDBACK_LEVEL_2, - HAPTIC_FEEDBACK_LEVEL_3, - HAPTIC_FEEDBACK_LEVEL_4, - HAPTIC_FEEDBACK_LEVEL_5, -}; - struct _vibe_pattern { haptic_vibration_iter_s *iters; int current; int size; - int level_change; + int level; int iter_count; - int stop; int pattern_index; int error; }; +static const int UNDEFINED = 0; +static const int START = 1; + GArray *pattern_table = NULL; static int initialize = 0; @@ -87,23 +78,6 @@ static int max_device = 0; static int* haptic_ids = NULL; -static void _free_pattern_from_table(int index); - -static bool invalid_ivt(const char* file_name) -{ - if( 0 != access(file_name, R_OK) ) - { - return false; - } - - char* ext = strrchr(file_name, '.'); - if (ext == NULL || 0 != strcasecmp(ext, ".ivt")){ - return false; - } - - return true; -} - int haptic_get_count(int* vibrator_number) { int count; @@ -124,6 +98,10 @@ int haptic_initialize() int i, j; int id; + if(initialize) + return HAPTIC_ERROR_NONE; + + if( haptic_get_count(&max_device) < 0) RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED); @@ -147,7 +125,7 @@ int haptic_initialize() initialize = 1; - pattern_table = g_array_new(FALSE, TRUE, sizeof(struct _vibe_pattern*)); + pattern_table = g_array_new(FALSE, TRUE, sizeof(int)); _haptic_init(); @@ -165,14 +143,11 @@ int haptic_deinitialize() err = device_haptic_close(haptic_ids[i]); } initialize = 0; - if(haptic_ids != NULL) free(haptic_ids); - for(i=0; i< pattern_table->len; i++){ - _free_pattern_from_table(i); - } g_array_free(pattern_table, TRUE); + pattern_table = NULL; _haptic_deinit(); @@ -180,92 +155,33 @@ int haptic_deinitialize() } -int haptic_get_file_duration(int device_index, const char *file_name , int* duration) +int haptic_vibrate_monotone(int device_index, int duration_ms, int level) { int ret; - if(!initialize) - RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED); - - if(device_index < 0 || device_index > max_device) - RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER); - - device_index = ((device_index < 3) ? device_index : 0); // xxx - - if(!invalid_ivt(file_name)) - RETURN_ERR(HAPTIC_ERROR_NOT_SUPPORTED_FORMAT); - - ret = device_haptic_get_file_duration(haptic_ids[device_index], file_name, duration); - - if(ret < 0){ - if(ret == -2) - RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED); - else - RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER); - } - - return HAPTIC_ERROR_NONE; -} - -int haptic_vibrate_file(int device_index, const char *file_name , int count , haptic_level_e level) -{ - int ret; - - if(device_index < 0 || device_index > max_device) - RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER); - - device_index = ((device_index < 3) ? device_index : 0); // xxx - - if(!initialize) - RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED); - - if(level == VIBE_SILENCE) - return HAPTIC_ERROR_NONE; - - if(level > HAPTIC_LEVEL_5 || level < 0) + if(device_index < 0 || device_index > max_device) RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER); - if(count < 0 || count >= HAPTIC_INFINITE_ITERATION) - RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER); - - if(count == 0) - count = HAPTIC_INFINITE_ITERATION; - - if(!invalid_ivt(file_name)) - RETURN_ERR(HAPTIC_ERROR_NOT_SUPPORTED_FORMAT); - - ret = device_haptic_play_file(haptic_ids[device_index], file_name, count, _LEVEL[level]); - - if(ret < 0){ - if(ret == -2) - RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED); - else - RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER); - } - - return HAPTIC_ERROR_NONE; -} - -int haptic_vibrate_monotone(int device_index , int duration) -{ - int ret; + if(level < HAPTIC_LEVEL_AUTO || level > HAPTIC_LEVEL_5) + RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER); - if(device_index < 0 || device_index > max_device) - RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER); + device_index = ((device_index < 3) ? device_index : 0); // xxx - device_index = ((device_index < 3) ? device_index : 0); // xxx + if(!initialize) + RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED); - if(!initialize) - RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED); + if(level == HAPTIC_LEVEL_0) + return HAPTIC_ERROR_NONE; - ret = device_haptic_play_monotone(haptic_ids[device_index], duration); + level = (level == HAPTIC_LEVEL_AUTO) ? 0 : level; + ret = device_haptic_play_monotone_with_detail_feedback_level(haptic_ids[device_index], duration_ms, level); if(ret < 0){ - if(ret == -2) - RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED); - else - RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER); - } + if(ret == -2) + RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED); + else + RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER); + } return HAPTIC_ERROR_NONE; } @@ -294,17 +210,7 @@ int haptic_stop_device(int device_index) return HAPTIC_ERROR_NONE; } -static void _free_pattern_from_table(int index) -{ - struct _vibe_pattern* p = g_array_index(pattern_table, struct _vibe_pattern *, index); - if(p == NULL) - return; - free(p->iters); - free(p); - g_array_index(pattern_table, struct _vibe_pattern *, index) = NULL; -} - -static int _haptic_play_monotone(int device_index, long duration, haptic_level_e level) +static int _haptic_play_monotone(int device_index, long duration, int level) { int ret; @@ -316,8 +222,13 @@ static int _haptic_play_monotone(int device_index, long duration, haptic_level_e if(!initialize) RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED); + if(level == HAPTIC_LEVEL_AUTO) { + level = HAPTIC_FEEDBACK_LEVEL_AUTO; + }else { + level = level > 100 ? 100 : (level < 0 ? 0 : level); + } - ret = device_haptic_play_monotone_with_feedback_level(haptic_ids[device_index], duration, _LEVEL[level]); + ret = device_haptic_play_monotone_with_detail_feedback_level(haptic_ids[device_index], duration, level); if(ret < 0){ if(ret == -2) @@ -332,57 +243,54 @@ static int _haptic_play_monotone(int device_index, long duration, haptic_level_e static gboolean _haptic_play_iter(gpointer data) { int err; - struct _vibe_pattern* pattern = (struct _vibe_pattern*)data; + struct _vibe_pattern* pattern = NULL; + + pattern = (struct _vibe_pattern*)data; + + if(pattern == NULL) + return false; - if(pattern->stop){ - _free_pattern_from_table(pattern->pattern_index); + if(pattern_table == NULL || g_array_index(pattern_table, int, pattern->pattern_index) != START) { + free(pattern->iters); + free(pattern); return false; } + if(pattern->iters == NULL) + return false; int current = pattern->current; int device = pattern->iters[current].vibrator_index; - int level = pattern->iters[current].level; long time = pattern->iters[current].time; - int level_change = pattern->level_change; - int iter_count = pattern->iter_count; + int level = pattern->level; // set default device, if can't find given device. if(device >= max_device || device < 0) device = 0; - if(level + level_change < HAPTIC_LEVEL_0) - level = HAPTIC_LEVEL_0; - if(level + level_change > HAPTIC_LEVEL_5) - level = HAPTIC_LEVEL_5; - - if(level != HAPTIC_LEVEL_0 || time != 0){ + if(level != 0 || time != 0){ err = _haptic_play_monotone(device, time, level); if(err<0){ pattern->error = err; return false; } } - - pattern->current++; - // pattern play finish - if(pattern->current >= pattern->size){ - if(iter_count <= 0){ - _free_pattern_from_table(pattern->pattern_index); + if(++pattern->current >= pattern->size){ + if(pattern->iter_count <= 0){ + free(pattern->iters); + free(pattern); return false; }else{ pattern->current = 0; pattern->iter_count--; } } - - g_timeout_add(time, _haptic_play_iter, data); return false; } -int haptic_play_pattern(haptic_vibration_iter_s* pattern, int pattern_size, int count, int level_change, int* id) +int haptic_play_pattern(haptic_vibration_iter_s* pattern, int pattern_size, int count, int level, int* id) { int i, key = -1; @@ -409,32 +317,34 @@ int haptic_play_pattern(haptic_vibration_iter_s* pattern, int pattern_size, int } vibe_p->iters = tmp_ptn; vibe_p->size = pattern_size; - vibe_p->level_change = level_change; + vibe_p->level = level; vibe_p->iter_count = count; vibe_p->current = 0; - vibe_p->stop = 0; vibe_p->error= 0; for(i=0; i< pattern_table->len; i++){ - if(g_array_index(pattern_table, struct _vibe_pattern *, i) == NULL){ + if(g_array_index(pattern_table, int, i) == UNDEFINED){ key = i; break; } } if(key == -1){ - g_array_append_val(pattern_table, vibe_p); + g_array_append_val(pattern_table, START); key = pattern_table->len -1; }else{ - g_array_index(pattern_table, struct _vibe_pattern *, key) = vibe_p; + g_array_index(pattern_table, int, key) = START; } vibe_p->pattern_index = key; _haptic_play_iter((gpointer)vibe_p); - if(vibe_p->error < 0){ - _free_pattern_from_table(key); - if(vibe_p->error == -2) + int error = vibe_p->error; + if(error < 0){ + free(vibe_p->iters); + free(vibe_p); + + if(error == -2) RETURN_ERR(HAPTIC_ERROR_OPERATION_FAILED); else RETURN_ERR(HAPTIC_ERROR_NOT_INITIALIZED); @@ -456,11 +366,7 @@ int haptic_stop_pattern(int id) if(id >= pattern_table->len) RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER); - struct _vibe_pattern* pattern = g_array_index(pattern_table, struct _vibe_pattern *, id); - if(pattern != NULL) - pattern->stop = 1; - else - RETURN_ERR(HAPTIC_ERROR_INVALID_PARAMETER); + g_array_index(pattern_table, int, id) = UNDEFINED; return HAPTIC_ERROR_NONE; } |