8c908180f6fbcb9c1e9393f31c11e30803008070
[platform/framework/web/wrt.git] / src / view / common / view_logic_vibration_support.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
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  * @file    view_logic_vibration_support.cpp
18  * @author  Jihoon Chung (jihoon.chung@samsung.com)
19  */
20
21 #include "view_logic_vibration_support.h"
22 #include <dpl/log/log.h>
23 #include <haptic.h>
24
25 namespace ViewModule {
26
27 VibrationSupport::VibrationSupport(): m_initialized(false),
28                                              m_handle(NULL),
29                                              m_effect_handle(NULL)
30 {
31 }
32
33 VibrationSupport::~VibrationSupport()
34 {
35     Assert(!m_initialized);
36 }
37
38 void VibrationSupport::initialize(void)
39 {
40     LogDebug("initialize");
41     initializeVibration();
42 }
43
44 void VibrationSupport::deinitialize(void)
45 {
46     LogDebug("deinitialize");
47
48     if( m_initialized )
49     {
50         int ret = haptic_close(m_handle);
51
52         if( HAPTIC_ERROR_NONE == ret )
53         {
54             m_initialized = false;
55             LogDebug("deinitialize success");
56         }
57         else
58         {
59             m_initialized = true;
60             LogDebug("deinitialize failed - error code : " << ret);
61         }
62     }
63 }
64
65 void VibrationSupport::startVibration(const long vibrationTime)
66 {
67     LogDebug("startVibration called");
68
69     if ( m_initialized == false )
70     {
71         if ( initializeVibration() == false )
72         {
73             return;
74         }
75     }
76
77     int time = static_cast<int>(vibrationTime);
78     int ret = haptic_vibrate_monotone(m_handle, time, &m_effect_handle);
79
80     if( HAPTIC_ERROR_NONE == ret )
81     {
82         LogDebug("haptic_vibrate_monotone success");
83     }
84     else
85     {
86         LogDebug("haptic_vibrate_monotone failed - error code : " << ret);
87     }
88
89     return;
90 }
91
92 void VibrationSupport::stopVibration(void)
93 {
94     LogDebug("stopVibration called");
95     if ( m_initialized == false )
96     {
97         return;
98     }
99
100     int ret = haptic_stop_all_effects(m_handle);
101
102     if( HAPTIC_ERROR_NONE == ret )
103     {
104         LogDebug("haptic_stop_all_effects success");
105     }
106     else
107     {
108         LogDebug("haptic_stop_all_effects failed - error code : " << ret);
109     }
110
111     return;
112 }
113
114 bool VibrationSupport::initializeVibration(void)
115 {
116     LogDebug("initializeVibration called");
117
118
119     if ( m_initialized == false )
120     {
121         haptic_device_h handle = NULL;
122         int ret = haptic_open(HAPTIC_DEVICE_0, &handle);
123
124         if ( ret == HAPTIC_ERROR_NONE )
125         {
126             LogDebug("initializeVibration success");
127             m_initialized = true;
128             m_handle = handle;
129         }
130         else
131         {
132             LogDebug("initializeVibration failed - error code : " << ret);
133             m_initialized = false;
134             m_handle = NULL;
135         }
136     }
137
138     return m_initialized;
139 }
140
141 } // namespace ViewModule