[SRUK] Initial copy from Tizen 2.2 version
[platform/core/uifw/dali-adaptor.git] / plugins / dali-feedback.cpp
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 #define LOG_TAG "DALI_FEEDBACK"
18
19 // CLASS HEADER
20 #include "dali-feedback.h"
21
22 // EXTERNAL INCLUDES
23 #include <iostream>
24 #include <fstream>
25 #include <feedback.h>
26 #include <mm_sound.h>
27 #include <mm_sound_private.h>
28
29 #include <dlog.h>
30
31 #define DEBUG_PRINTF(fmt, arg...)  LOGD(" "fmt, ##arg)
32
33 using std::string;
34 using namespace Dali;
35
36 // The plugin factories
37 extern "C" DALI_EXPORT_API Dali::FeedbackPlugin* CreateFeedbackPlugin(void)
38 {
39   return new Dali::Plugin::DaliFeedback;
40 }
41
42 namespace Dali
43 {
44
45 namespace Plugin
46 {
47
48 DaliFeedback::DaliFeedback()
49 : mHapticInitialized( false )
50 {
51   feedback_initialize();
52
53   if( 0 == haptic_open( HAPTIC_DEVICE_0, &mDeviceHandle ) )
54   {
55     mHapticInitialized = true;
56   }
57   else
58   {
59     DEBUG_PRINTF( "Haptic feedback controller failed to initialize\n" );
60   }
61 }
62
63 DaliFeedback::~DaliFeedback()
64 {
65   if( mHapticInitialized )
66   {
67     int errorCode = haptic_close( mDeviceHandle );
68     if( errorCode < 0 )
69     {
70       DEBUG_PRINTF( "device_haptic_close() failed with error code: %d\n", errorCode );
71     }
72   }
73
74   feedback_deinitialize();
75 }
76
77 void DaliFeedback::PlayHaptic( const std::string& filePath )
78 {
79   if( mHapticInitialized )
80   {
81     if ( filePath.size() == 0 )
82     {
83       DEBUG_PRINTF( "File Path can't be NULL!\n" );
84       return;
85     }
86
87     int errorCode = haptic_vibrate_file_with_detail( mDeviceHandle, filePath.c_str(), HAPTIC_ITERATION_ONCE, HAPTIC_FEEDBACK_AUTO, HAPTIC_PRIORITY_MIN, NULL );
88     if( errorCode != 0 )
89     {
90       DEBUG_PRINTF( "PlayHaptic() failed with error code: %d\n", errorCode );
91     }
92   }
93   else
94   {
95     DEBUG_PRINTF( "HapticPlayer is not Initialized\n" );
96   }
97 }
98
99 void DaliFeedback::PlayHapticMonotone( unsigned int duration )
100 {
101   if( mHapticInitialized )
102   {
103     int errorCode = haptic_vibrate_monotone_with_detail( mDeviceHandle, duration, HAPTIC_FEEDBACK_AUTO, HAPTIC_PRIORITY_MIN, NULL );
104     if( errorCode != 0 )
105     {
106       DEBUG_PRINTF( "PlayHapticMonotone() failed with error code: %d\n", errorCode );
107     }
108   }
109   else
110   {
111     DEBUG_PRINTF( "HapticPlayer is not Initialized\n" );
112   }
113 }
114
115 void DaliFeedback::StopHaptic()
116 {
117   if( mHapticInitialized )
118   {
119     int errorCode = haptic_stop_all_effects( mDeviceHandle );
120     if( errorCode != 0 )
121     {
122       DEBUG_PRINTF( "StopHaptic() failed with error code: %d\n", errorCode );
123     }
124   }
125   else
126   {
127     DEBUG_PRINTF( "HapticPlayer is not Initialized\n" );
128   }
129 }
130
131 int DaliFeedback::PlaySound( const std::string& fileName )
132 {
133   int handle = -1;
134   int errorCode = mm_sound_play_keysound( fileName.c_str(), VOLUME_TYPE_SYSTEM & VOLUME_GAIN_TOUCH );
135   if( errorCode < 0 )
136   {
137     DEBUG_PRINTF( "PlaySound() %s failed with error code = %d\n", fileName.c_str(), errorCode );
138   }
139
140   return handle;
141 }
142
143 void DaliFeedback::StopSound( int handle )
144 {
145   int errorCode = mm_sound_stop_sound( handle );
146   if( errorCode < 0 )
147   {
148     DEBUG_PRINTF( "StopSound() handle = %d failed with error code = %d\n", handle, errorCode);
149   }
150   else
151   {
152     DEBUG_PRINTF( "stop handle %d success\n", handle );
153   }
154 }
155
156 void DaliFeedback::PlayFeedbackPattern( int type, int pattern )
157 {
158   int errorCode = feedback_play_type( static_cast<feedback_type_e>(type), static_cast<feedback_pattern_e>(pattern) );
159   if( errorCode != 0 )
160   {
161     DEBUG_PRINTF( "DaliFeedback::PlayFeedbackPattern() with type = %d, pattern = %d returned with error = %d\n", (int)type, (int)pattern, errorCode );
162   }
163 }
164
165 } // namespace Plugin
166
167 } // namespace Dali
168