tizen 2.3.1 release
[apps/home/b2-clocksetting.git] / src / setting_control_haptic.c
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  *  Licensed under the Flora License, Version 1.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
7  *
8  *      http://floralicense.org/license/
9  *
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.
15  *
16  */
17 #include "setting_control_haptic.h"
18 #include "util.h"
19
20 static struct _haptic_data h_data;
21 static Ecore_Timer *vibration_timer = NULL;
22
23 static haptic_device_h hnd_hpt;
24
25 int _haptic_open()
26 {
27         int haptic_return = 0;
28         haptic_return = haptic_open(HAPTIC_DEVICE_0, &hnd_hpt);
29         if (haptic_return < 0) {
30                 DBG("Setting - Failed haptic_open");
31                 return 0;
32         }
33
34         h_data.is_haptic_opened = 1;
35
36         return 1;
37 }
38
39 int _is_haptic_open()
40 {
41         return h_data.is_haptic_opened;
42 }
43
44 Eina_Bool __vibration_timeout(void *data)
45 {
46         if (_is_haptic_open()) {
47                 _haptic_close();
48         }
49
50         vibration_timer = NULL;
51
52         return ECORE_CALLBACK_CANCEL;
53 }
54
55 void _start_vibration(int level, int feedback_rate, char *res_path)
56 {
57         if (_is_haptic_open()) {
58                 _haptic_close();
59         }
60
61         if (_haptic_open()) {
62                 int duration = 0;
63                 int err = -1;
64
65                 err = haptic_get_file_duration(hnd_hpt, res_path, &duration);
66                 if (err != 0) {
67                         DBG("Setting - haptic_get_file_duration() failed");
68                         duration = 1000;
69                 }
70
71                 DBG("Setting - duration : %d", duration);
72
73                 err = haptic_vibrate_file_with_detail(hnd_hpt, res_path,
74                                                                                           HAPTIC_ITERATION_ONCE, feedback_rate, HAPTIC_PRIORITY_HIGH, NULL);
75                 if (err != 0)
76                         DBG("Setting - haptic_vibrate_file_with_detail() failed");
77
78                 double real_duration = (double)(duration / 1000.0);
79
80                 DBG("Setting - duration2 : %f", real_duration);
81
82                 if (vibration_timer) {
83                         ecore_timer_del(vibration_timer);
84                         vibration_timer = NULL;
85                 }
86                 vibration_timer = ecore_timer_add(real_duration, (Ecore_Task_Cb)__vibration_timeout, NULL);
87         }
88 }
89
90 int _haptic_close()
91 {
92         int ret = haptic_close(hnd_hpt);
93         if (ret != 0) {
94                 DBG("Setting - Failed haptic_deinitialize");
95                 return 0;
96         }
97
98         if (vibration_timer) {
99                 ecore_timer_del(vibration_timer);
100                 vibration_timer = NULL;
101         }
102
103         h_data.is_haptic_opened = 0;
104
105         return 1;
106 }