4 * Copyright (c) 2012 - 2017 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the License);
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
27 #include <sys/types.h>
30 #include <linux/input.h>
33 #include "core/list.h"
35 #include "standard-vibcore.h"
37 #define MAX_MAGNITUDE 0xFFFF
38 #define PERIODIC_MAX_MAGNITUDE 0x7FFF /* 0.5 * MAX_MAGNITUDE */
39 #define RUMBLE_MAX_MAGNITUDE 0xFFFF
41 #define DEV_INPUT "/dev/input"
45 #define FF_INFO_MAGIC 0xDEADFEED
47 struct ff_info_header {
50 int ff_info_data_count;
54 int type;/* play, stop etc */
55 int magnitude; /* strength */
56 int length; /* in ms for stop, play*/
59 struct ff_info_buffer {
60 struct ff_info_header header;
61 struct ff_info_data data[MAX_DATA];
67 struct ff_effect effect;
68 struct ff_info_buffer *ffinfobuffer;
73 static dd_list *ff_list;
74 static dd_list *handle_list;
75 static char ff_path[PATH_MAX];
76 static int unique_number;
78 struct ff_info *read_from_list(int handle)
83 DD_LIST_FOREACH(ff_list, elem, temp) {
84 if (temp->handle == handle)
90 static bool check_valid_handle(struct ff_info *info)
95 DD_LIST_FOREACH(ff_list, elem, temp) {
105 static bool check_fd(int *fd)
112 ffd = open(ff_path, O_RDWR);
120 static int ff_stop(int fd, struct ff_effect *effect);
121 static gboolean timer_cb(void *data)
123 struct ff_info *info = (struct ff_info *)data;
126 return G_SOURCE_REMOVE;
128 if (!check_valid_handle(info))
129 return G_SOURCE_REMOVE;
131 _I("stop vibration by timer : id(%d)", info->effect.id);
133 /* stop previous vibration */
134 ff_stop(ff_fd, &info->effect);
139 return G_SOURCE_REMOVE;
142 static int ff_find_device(void)
146 char ev_path[PATH_MAX];
147 unsigned long features[1+FF_MAX/sizeof(unsigned long)];
150 dir = opendir(DEV_INPUT);
159 if (dent->d_type == DT_DIR ||
160 !strstr(dent->d_name, "event"))
163 snprintf(ev_path, sizeof(ev_path), "%s/%s", DEV_INPUT, dent->d_name);
165 fd = open(ev_path, O_RDWR);
169 /* get force feedback device */
170 memset(features, 0, sizeof(features));
171 ret = ioctl(fd, EVIOCGBIT(EV_FF, sizeof(features)), features);
177 if (test_bit(FF_CONSTANT, features))
178 _D("%s type : constant", ev_path);
179 if (test_bit(FF_PERIODIC, features))
180 _D("%s type : periodic", ev_path);
181 if (test_bit(FF_SPRING, features))
182 _D("%s type : spring", ev_path);
183 if (test_bit(FF_FRICTION, features))
184 _D("%s type : friction", ev_path);
185 if (test_bit(FF_RUMBLE, features))
186 _D("%s type : rumble", ev_path);
188 if (test_bit(FF_RUMBLE, features)) {
189 memcpy(ff_path, ev_path, strlen(ev_path));
202 static int ff_init_effect(struct ff_effect *effect)
207 /*Only rumble supported as of now*/
208 effect->type = FF_RUMBLE;
209 effect->replay.length = 0;
210 effect->replay.delay = 10;
212 effect->u.rumble.strong_magnitude = 0x8000;
213 effect->u.rumble.weak_magnitude = 0xc000;
218 static int ff_set_effect(struct ff_effect *effect, int length, int level)
225 magnitude = (double)level/HAPTIC_MODULE_FEEDBACK_MAX;
226 magnitude *= RUMBLE_MAX_MAGNITUDE;
228 _I("info : magnitude(%d) length(%d)", (int)magnitude, length);
230 /* set member variables in effect struct */
231 effect->u.rumble.strong_magnitude = (int)magnitude;
232 effect->replay.length = length; /* length millisecond */
237 static int ff_play(int fd, struct ff_effect *effect)
239 struct input_event play;
242 if (fd < 0 || !effect) {
244 _E("fail to check fd");
246 _E("fail to check effect");
250 /* upload an effect */
251 if (ioctl(fd, EVIOCSFF, effect) == -1) {
258 play.code = effect->id;
259 play.value = 1; /* 1 : PLAY, 0 : STOP */
261 ret = write(fd, (const void *)&play, sizeof(play));
270 static int ff_stop(int fd, struct ff_effect *effect)
272 struct input_event stop;
280 stop.code = effect->id;
281 stop.value = 0; /* 1 : PLAY, 0 : STOP */
282 ret = write(fd, (const void *)&stop, sizeof(stop));
286 /* removing an effect from the device */
287 if (ioctl(fd, EVIOCRMFF, effect->id) == -1)
290 /* reset effect id */
296 /* START: Haptic Module APIs */
297 static int get_device_count(int *count)
299 /* suppose there is just one haptic device */
306 static int open_device(int device_index, int *device_handle)
308 struct ff_info *info;
316 /* if it is the first element */
317 n = DD_LIST_LENGTH(ff_list);
318 if (n == 0 && !ff_fd) {
319 _I("First element: open ff driver");
321 ff_fd = open(ff_path, O_RDWR);
323 _E("Failed to open %s : %d", ff_path, errno);
328 /* allocate memory */
329 info = calloc(sizeof(struct ff_info), 1);
331 _E("Failed to allocate memory : %d", errno);
335 /* initialize ff_effect structure */
336 ff_init_effect(&info->effect);
338 if (unique_number == INT_MAX)
341 while (found != true) {
343 elem = DD_LIST_FIND(handle_list, (gpointer)(long)unique_number);
348 info->handle = unique_number;
350 /* add info to local list */
351 DD_LIST_APPEND(ff_list, info);
352 DD_LIST_APPEND(handle_list, (gpointer)(long)info->handle);
354 *device_handle = info->handle;
358 static int close_device(int device_handle)
360 struct ff_info *info;
363 info = read_from_list(device_handle);
367 if (!check_valid_handle(info))
370 if (!check_fd(&ff_fd))
374 r = ff_stop(ff_fd, &info->effect);
376 _I("already stopped or failed to stop effect : %d", r);
378 /* unregister existing timer */
379 if (r >= 0 && info->timer) {
380 _D("device handle %d is closed and timer deleted", device_handle);
381 g_source_remove(info->timer);
385 standard_vibrate_close();
387 DD_LIST_REMOVE(handle_list, (gpointer)(long)info->handle);
389 safe_free(info->ffinfobuffer);
390 /* remove info from local list */
391 DD_LIST_REMOVE(ff_list, info);
394 /* if it is the last element */
395 n = DD_LIST_LENGTH(ff_list);
396 if (n == 0 && ff_fd) {
397 _I("Last element: close ff driver");
398 /* close ff driver */
406 static int vibrate_monotone(int device_handle, int duration, int feedback, int priority, int *effect_handle)
408 struct ff_info *info;
411 info = read_from_list(device_handle);
413 _E("fail to check list");
417 if (!check_valid_handle(info)) {
418 _E("fail to check handle");
422 if (!check_fd(&ff_fd))
425 /* Zero(0) is the infinitely vibration value */
426 if (duration == HAPTIC_MODULE_DURATION_UNLIMITED)
429 /* unregister existing timer */
431 ff_stop(ff_fd, &info->effect);
432 g_source_remove(info->timer);
436 /* set effect as per arguments */
437 ff_init_effect(&info->effect);
438 ret = ff_set_effect(&info->effect, duration, feedback);
440 _E("failed to set effect(duration:%d, feedback:%d) : %d",
441 duration, feedback, ret);
445 /* play effect as per arguments */
446 ret = ff_play(ff_fd, &info->effect);
448 _E("failed to play haptic effect(fd:%d id:%d) : %d",
449 ff_fd, info->effect.id, ret);
455 info->timer = g_timeout_add(duration, timer_cb, info);
457 _E("Failed to add timer callback");
460 _D("device handle %d effect id : %d %dms", device_handle, info->effect.id, duration);
462 *effect_handle = info->effect.id;
467 static gboolean _buffer_play(void *cbdata)
469 struct ff_info *info = (struct ff_info *)cbdata;
470 struct ff_info_header *header = &info->ffinfobuffer->header;
471 struct ff_info_data *data = info->ffinfobuffer->data;
472 int index = info->currentindex;
473 int play_type = (index < header->ff_info_data_count) ? data[index].type : 0;
474 int length = (index < header->ff_info_data_count) ? data[index].length : 1;
477 ff_set_effect(&info->effect, length, 1);
478 if (play_type != 0) {
479 _D("Going to play for %d ms", length);
480 ret = ff_play(ff_fd, &info->effect);
482 _D("Failed to play the effect %d", ret);
484 _D("Going to stop for %d ms", length);
485 ret = ff_stop(ff_fd, &info->effect);
487 _D("Failed to stop the effect %d", ret);
490 if (info->currentindex < header->ff_info_data_count) {
491 info->currentindex++;
492 info->timer = g_timeout_add(length, _buffer_play, info);
495 if (header->iteration > 0) {
496 info->currentindex = 0;
497 info->timer = g_timeout_add(0, _buffer_play, info);
502 return G_SOURCE_REMOVE;
505 static void print_buffer(const unsigned char *vibe_buffer)
507 struct ff_info_buffer fb;
509 memcpy(&fb.header, vibe_buffer, sizeof(struct ff_info_header));
510 memcpy(&fb.data, (unsigned char *)vibe_buffer+sizeof(struct ff_info_header),
511 sizeof(struct ff_info_data) * fb.header.ff_info_data_count);
512 _D("\nMagic %x\niteration %d\ncount %d\n", fb.header.magic,
513 fb.header.iteration, fb.header.ff_info_data_count);
515 for (i = 0; i < fb.header.ff_info_data_count; i++)
516 _D("type %d\nmagn 0x%x\nlen %d\n", fb.data[i].type,
517 fb.data[i].magnitude, fb.data[i].length);
520 static int vibrate_custom_buffer(int device_handle, const unsigned char *vibe_buffer, int iteration, int feedback, int priority, int *effect_handle)
522 struct ff_info *info;
523 struct ff_info_header *header;
524 struct ff_info_data *data;
526 info = read_from_list(device_handle);
530 if (!check_valid_handle(info))
533 if (!check_fd(&ff_fd))
536 if (!info->ffinfobuffer)
537 info->ffinfobuffer = (struct ff_info_buffer *)calloc(sizeof(struct ff_info_buffer), 1);
538 if (!info->ffinfobuffer)
541 header = &info->ffinfobuffer->header;
542 data = info->ffinfobuffer->data;
544 memcpy(header, vibe_buffer, sizeof(struct ff_info_header));
545 if (header->ff_info_data_count < 0 || header->ff_info_data_count > MAX_DATA)
548 memcpy(data, vibe_buffer+sizeof(struct ff_info_header), sizeof(struct ff_info_data) * header->ff_info_data_count);
550 info->currentindex = 0;
552 g_source_remove(info->timer);
554 if (header->iteration > 0)
560 static int vibrate_buffer(int device_handle, const unsigned char *vibe_buffer, int iteration, int feedback, int priority, int *effect_handle)
568 magic = *(int *)vibe_buffer;
570 if (magic == FF_INFO_MAGIC) {
571 print_buffer(vibe_buffer);
572 return vibrate_custom_buffer(device_handle, vibe_buffer, iteration, feedback, priority, effect_handle);
574 return vibrate_monotone(device_handle, 300, feedback, priority, effect_handle);
577 static int stop_device(int device_handle)
579 struct ff_info *info;
582 info = read_from_list(device_handle);
586 if (!check_valid_handle(info))
589 if (!check_fd(&ff_fd))
592 if (cur_h_data.handle > 0 && cur_h_data.handle != info->handle) {
593 _E("Only same handle can stop current vibration");
597 /* Remove duration_timer for vibrate_effect */
598 standard_vibrate_close();
601 r = ff_stop(ff_fd, &info->effect);
603 _E("failed to stop effect(id:%d) : %d", info->effect.id, r);
605 _D("succeed to stop effect");
607 /* unregister existing timer */
608 if (r >= 0 && info->timer) {
609 g_source_remove(info->timer);
616 static int get_device_state(int device_index, int *effect_state)
618 struct ff_info *info;
625 /* suppose there is just one haptic device */
626 DD_LIST_FOREACH(ff_list, elem, info) {
627 if (info->effect.id >= 0) {
633 *effect_state = status;
637 static int create_effect(unsigned char *vibe_buffer, int max_bufsize, const haptic_module_effect_element *elem_arr, int max_elemcnt)
639 _E("Not support feature");
643 static int get_buffer_duration(int device_handle, const unsigned char *vibe_buffer, int *buffer_duration)
645 _E("Not support feature");
649 static int convert_binary(const unsigned char *vibe_buffer, int max_bufsize, const char *file_path)
651 _E("Not support feature");
654 /* END: Haptic Module APIs */
656 static const struct haptic_plugin_ops default_plugin = {
657 .get_device_count = get_device_count,
658 .open_device = open_device,
659 .close_device = close_device,
660 .vibrate_monotone = vibrate_monotone,
661 .vibrate_buffer = vibrate_buffer,
662 .vibrate_effect = standard_vibrate_effect,
663 .is_supported = standard_is_supported,
664 .stop_device = stop_device,
665 .get_device_state = get_device_state,
666 .create_effect = create_effect,
667 .get_buffer_duration = get_buffer_duration,
668 .convert_binary = convert_binary,
671 static bool is_valid(void)
675 ret = ff_find_device();
677 _E("Do not support standard haptic device");
681 standard_config_parse();
683 _I("Support standard haptic device");
687 static const struct haptic_plugin_ops *load(void)
689 standard_set_vib_function(&vibrate_monotone);
690 return &default_plugin;
693 static const struct haptic_ops std_ops = {
694 .type = HAPTIC_STANDARD,
695 .is_valid = is_valid,
699 HAPTIC_OPS_REGISTER(&std_ops)