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