[dali_1.9.1] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / haptics / common / feedback-player-impl.cpp
1 /*
2  * Copyright (c) 2019 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 // CLASS HEADER
19 #include <dali/internal/haptics/common/feedback-player-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/devel-api/adaptor-framework/file-loader.h>
24 #include <dali/devel-api/common/singleton-service.h>
25 #include <dali/integration-api/debug.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace Adaptor
34 {
35
36 namespace // unnamed namespace
37 {
38
39 // Type Registration
40 Dali::BaseHandle Create()
41 {
42   return FeedbackPlayer::Get();
43 }
44
45 Dali::TypeRegistration FEEDBACK_PLAYER_TYPE( typeid(Dali::FeedbackPlayer), typeid(Dali::BaseHandle), Create );
46
47 } // unnamed namespace
48
49 Dali::FeedbackPlayer FeedbackPlayer::New()
50 {
51   Dali::FeedbackPlayer player = Dali::FeedbackPlayer( new FeedbackPlayer() );
52   return player;
53 }
54
55 Dali::FeedbackPlayer FeedbackPlayer::Get()
56 {
57   Dali::FeedbackPlayer player;
58
59   Dali::SingletonService service( SingletonService::Get() );
60   if ( service )
61   {
62     // Check whether the singleton is already created
63     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::FeedbackPlayer ) );
64     if ( handle )
65     {
66       // If so, downcast the handle
67       player = Dali::FeedbackPlayer( dynamic_cast< FeedbackPlayer* >( handle.GetObjectPtr() ) );
68     }
69     else
70     {
71       player = Dali::FeedbackPlayer( New() );
72       service.Register( typeid( player ), player );
73     }
74   }
75
76   return player;
77 }
78
79 void FeedbackPlayer::PlayMonotone( unsigned int duration )
80 {
81   mPlugin.PlayHapticMonotone( duration );
82 }
83
84 void FeedbackPlayer::PlayFile( const std::string& filePath )
85 {
86   mPlugin.PlayHaptic( filePath );
87 }
88
89 void FeedbackPlayer::Stop()
90 {
91   mPlugin.StopHaptic();
92 }
93
94 int FeedbackPlayer::PlaySound( const std::string& filename )
95 {
96   return mPlugin.PlaySound(filename);
97 }
98
99 void FeedbackPlayer::StopSound( int handle )
100 {
101   mPlugin.StopSound(handle);
102 }
103
104 void FeedbackPlayer::PlayFeedbackPattern( int type, int pattern )
105 {
106   mPlugin.PlayFeedbackPattern(type, pattern);
107 }
108
109 bool FeedbackPlayer::LoadFile(const std::string& filename, std::string& data)
110 {
111   bool loaded = false;
112
113   std::streampos bufferSize = 0;
114   Dali::Vector<char> fileBuffer;
115   if( Dali::FileLoader::ReadFile( filename, bufferSize, fileBuffer, FileLoader::FileType::TEXT ) )
116   {
117     data.assign( &fileBuffer[0], bufferSize );
118     loaded = true;
119   }
120
121   return loaded;
122 }
123
124 FeedbackPlayer::FeedbackPlayer()
125 : mPlugin( FeedbackPluginProxy::DEFAULT_OBJECT_NAME )
126 {
127 }
128
129 FeedbackPlayer::~FeedbackPlayer()
130 {
131 }
132
133 } // namespace Adaptor
134
135 } // namespace Internal
136
137 } // namespace Dali