tizen 2.3 release
[apps/home/b2-clocksetting.git] / src / setting_control_haptic.c
1 /*
2  * Copyright (c) 2010 Samsung Electronics, Inc.
3  * All rights reserved.
4  *
5  * This software is a confidential and proprietary information
6  * of Samsung Electronics, Inc. ("Confidential Information").  You
7  * shall not disclose such Confidential Information and shall use
8  * it only in accordance with the terms of the license agreement
9  * you entered into with Samsung Electronics.
10  */
11 #include "setting_control_haptic.h"
12 #include "util.h"
13
14 static struct _haptic_data h_data;
15 static Ecore_Timer * vibration_timer = NULL;
16
17 static haptic_device_h hnd_hpt;
18
19 int _haptic_open()
20 {
21         int haptic_return = 0;
22         haptic_return = haptic_open(HAPTIC_DEVICE_0, &hnd_hpt);
23         if (haptic_return < 0) {
24                 DBG("Setting - Failed haptic_open");
25                 return 0;
26         }
27
28         h_data.is_haptic_opened = 1;
29
30         return 1;
31 }
32
33 int _is_haptic_open()
34 {
35         return h_data.is_haptic_opened;
36 }
37
38 Eina_Bool __vibration_timeout(void *data)
39 {
40         if( _is_haptic_open() ) {
41                 _haptic_close();
42         }
43
44         vibration_timer = NULL;
45
46         return ECORE_CALLBACK_CANCEL;
47 }
48
49 void _start_vibration(int level, int feedback_rate, char * res_path)
50 {
51         if( _is_haptic_open() ) {
52                 _haptic_close();
53         }
54
55         if( _haptic_open() )
56         {
57                 int duration = 0;
58                 int err = -1;
59
60                 err = haptic_get_file_duration(hnd_hpt, res_path, &duration);
61                 if (err != 0)
62                 {
63                         DBG("Setting - haptic_get_file_duration() failed");
64                         duration = 1000;
65                 }
66
67                 DBG("Setting - duration : %d", duration);
68
69                 err = haptic_vibrate_file_with_detail(hnd_hpt, res_path,
70                                 HAPTIC_ITERATION_ONCE, feedback_rate, HAPTIC_PRIORITY_HIGH, NULL);
71                 if(err != 0)
72                         DBG("Setting - haptic_vibrate_file_with_detail() failed");
73
74                 double real_duration = (double)(duration / 1000.0);
75
76                 DBG("Setting - duration2 : %f", real_duration);
77
78                 if( vibration_timer )
79                 {
80                         ecore_timer_del(vibration_timer);
81                         vibration_timer = NULL;
82                 }
83                 vibration_timer = ecore_timer_add(real_duration, (Ecore_Task_Cb)__vibration_timeout, NULL);
84         }
85 }
86
87 int _haptic_close()
88 {
89         int ret = haptic_close(hnd_hpt);
90         if (ret != 0) {
91                 DBG("Setting - Failed haptic_deinitialize");
92                 return 0;
93         }
94
95         if( vibration_timer ) {
96                 ecore_timer_del(vibration_timer);
97                 vibration_timer = NULL;
98         }
99
100         h_data.is_haptic_opened = 0;
101
102         return 1;
103 }