Fix setting framework
[platform/framework/native/appfw.git] / src / system / FSys_VibratorImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 /**
18  * @file                FSys_VibratorImpl.cpp
19  * @brief               This is the implementation file for _VibratorImpl class.
20  */
21
22 #include <new>
23 #include <unique_ptr.h>
24
25 #include <FBaseSysLog.h>
26 #include <FBaseResult.h>
27 #include <FSysVibrator.h>
28
29 #include <FSys_VibratorImpl.h>
30
31 namespace Tizen { namespace System
32 {
33 static const int MAX_SIZE_OF_VIBRATION_BUFFER = 64 * 1024; // Immersion lib guide value. 64kbytes
34
35 _VibratorImpl::_VibratorImpl(void)
36         : __handle(null)
37         , __effectHandle(null)
38 {
39
40 }
41
42 _VibratorImpl::~_VibratorImpl(void)
43 {
44         if (__handle != null)
45         {
46                 Stop();
47                 int deviceResult = haptic_close(__handle);
48
49                 if (deviceResult != HAPTIC_ERROR_NONE)
50                 {
51                         SysLogException(NID_SYS, E_SYSTEM,
52                                 "[E_SYSTEM] Vibrator finalize operation has failed. [handle:%d, result:%d]", __handle, deviceResult);
53                 }
54         }
55 }
56
57 result
58 _VibratorImpl::Construct(void)
59 {
60         SysTryReturnResult(NID_SYS, __handle == null, E_DEVICE_FAILED, "_VibratorImpl was already constructed.");
61
62         int ret = haptic_open(HAPTIC_DEVICE_ALL, &__handle);
63
64         SysTryReturnResult(NID_SYS, ret == HAPTIC_ERROR_NONE,E_SYSTEM,
65                         "[E_SYSTEM] Vibrator initialize operation has failed. [result:%d]", ret);
66         SysLog(NID_SYS, "vibrator created handle = %d", __handle);
67         return E_SUCCESS;
68 }
69
70
71 result
72 _VibratorImpl::Start(IntensityDurationVibrationPattern* patterns, int length, int repeatCount)
73 {
74         SysTryReturnResult(NID_SYS, __handle != null, E_DEVICE_FAILED, "_VibratorImpl was not constructed.");
75
76         result r = E_SUCCESS;
77         int errorCode = 0;
78         haptic_state_e state;
79         errorCode = haptic_get_effect_state(__handle, __effectHandle, &state);
80         
81         if ((haptic_error_e)errorCode == HAPTIC_ERROR_NONE)
82         {
83                 if (state == HAPTIC_STATE_PLAYING)
84                 {
85                         r = Stop();
86                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It failed to stop previous vibration.");
87                 }
88         }
89
90         int numberOfElement = length / sizeof(IntensityDurationVibrationPattern);
91         std::unique_ptr<haptic_effect_element_s[]> pPatterns(new (std::nothrow) haptic_effect_element_s[numberOfElement * sizeof(haptic_effect_element_s)]);
92         SysTryReturnResult(NID_SYS, pPatterns != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It failed to allocate memory");
93
94         for (int i = 0; i < numberOfElement; i++)
95         {
96                 SysTryReturnResult(NID_SYS, patterns[i].duration >= 0, E_INVALID_ARG, 
97                                                 "[E_INVALID_ARG] The %dth pattern is the invalid duration[%d].", i, patterns[i].duration);              
98                 pPatterns[i].haptic_duration = patterns[i].duration;
99
100                 SysTryReturnResult(NID_SYS, patterns[i].intensity >= -1 && patterns[i].intensity <= 100, E_INVALID_ARG,
101                                                 "[E_INVALID_ARG] The %dth pattern is the invalid intensity[%d].", i, patterns[i].intensity);
102                 pPatterns[i].haptic_level = (patterns[i].intensity == -1) ? HAPTIC_FEEDBACK_AUTO : (haptic_feedback_e)patterns[i].intensity;
103         }
104
105         int sizeOfBuf = MAX_SIZE_OF_VIBRATION_BUFFER;
106         std::unique_ptr<unsigned char[]> pEffectBuffer(new (std::nothrow) unsigned char[sizeOfBuf]);
107         SysTryReturnResult(NID_SYS, pEffectBuffer != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It failed to allocate memory");
108
109         errorCode = haptic_create_effect(pEffectBuffer.get(), sizeOfBuf, pPatterns.get(), numberOfElement);     
110         r = ConvertErrorCode(errorCode);
111         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r,"[%s] It failed to start vibrator.",GetErrorMessage(r));
112
113         SysLog(NID_SYS, "The number of patterns %d, repeate count %d", numberOfElement, repeatCount);
114         errorCode = haptic_vibrate_buffer_with_detail(__handle, pEffectBuffer.get(), (haptic_iteration_e)repeatCount, HAPTIC_FEEDBACK_5, HAPTIC_PRIORITY_MIN, &__effectHandle);
115         r = ConvertErrorCode(errorCode);
116         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r,"[%s] It failed to start vibrator.",GetErrorMessage(r));
117         return r;
118 }
119
120
121 result
122 _VibratorImpl::Start(long onPeriod, long offPeriod, int count, int level)
123 {
124         SysTryReturnResult(NID_SYS, __handle != null, E_DEVICE_FAILED, "_VibratorImpl was not constructed.");
125
126         result r = E_SUCCESS;
127         int errorCode = 0;
128         haptic_state_e state;
129         errorCode = haptic_get_effect_state(__handle, __effectHandle, &state);
130         
131         if ((haptic_error_e)errorCode == HAPTIC_ERROR_NONE)
132         {
133                 if (state == HAPTIC_STATE_PLAYING)
134                 {
135                         r = Stop();
136                         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It failed to stop previous vibration.");
137                 }
138         }
139
140         haptic_effect_element_s pattern[2];
141         
142         pattern[0].haptic_duration = onPeriod;
143         pattern[0].haptic_level = (level == 0) ? HAPTIC_FEEDBACK_AUTO : (haptic_feedback_e)level;
144
145         pattern[1].haptic_duration = offPeriod;
146         pattern[1].haptic_level = HAPTIC_FEEDBACK_0;    
147
148         int sizeOfBuf = MAX_SIZE_OF_VIBRATION_BUFFER;
149         std::unique_ptr<unsigned char[]> pEffectBuffer(new (std::nothrow) unsigned char[sizeOfBuf]);
150         SysTryReturnResult(NID_SYS, pEffectBuffer != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It failed to allocate memory");
151
152         errorCode = haptic_create_effect(pEffectBuffer.get(), sizeOfBuf, pattern, sizeof(pattern) / sizeof(haptic_effect_element_s));
153         r = ConvertErrorCode(errorCode);
154         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r,"[%s] It failed to start vibrator.",GetErrorMessage(r));
155
156         SysLog(NID_SYS, "onPeriod %d, offPeriod %d, count %d, level = %d", onPeriod, offPeriod, count, level);
157         errorCode = haptic_vibrate_buffer_with_detail(__handle, pEffectBuffer.get(), (haptic_iteration_e)count, HAPTIC_FEEDBACK_5, HAPTIC_PRIORITY_MIN, &__effectHandle);
158         r = ConvertErrorCode(errorCode);
159         SysTryReturnResult(NID_SYS, r == E_SUCCESS, r,"[%s] It failed to start vibrator.",GetErrorMessage(r));
160         return r;
161 }
162
163
164 result
165 _VibratorImpl::Stop(void)
166 {
167         SysTryReturnResult(NID_SYS, __handle != null, E_FAILURE, "_VibratorImpl was not constructed.");
168
169         int deviceResult = haptic_stop_all_effects(__handle);
170         SysTryReturnResult(NID_SYS, deviceResult == HAPTIC_ERROR_NONE,E_DEVICE_FAILED,
171                 "[E_SYSTEM] Vibration stop operation has failed. [result:%d]", deviceResult);
172         SysLog(NID_SYS, "vibrator stopped handle = %d", __handle);
173         __effectHandle = null;
174
175         return E_SUCCESS;
176 }
177
178 result
179 _VibratorImpl::ConvertErrorCode(int errorCode)
180 {
181         switch ((haptic_error_e)errorCode)
182         {
183         case HAPTIC_ERROR_NONE: return E_SUCCESS;
184         case HAPTIC_ERROR_INVALID_PARAMETER: return E_INVALID_ARG;
185         default: return E_DEVICE_FAILED;
186         }
187 }
188
189
190 _VibratorImpl*
191 _VibratorImpl::GetInstance(Vibrator& vibrator)
192 {
193         return vibrator.__pVibratorImpl;
194 }
195
196 const _VibratorImpl*
197 _VibratorImpl::GetInstance(const Vibrator& vibrator)
198 {
199         return vibrator.__pVibratorImpl;
200 }
201
202 } } // Tizen::System