3 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 #include "automation.h"
26 #include <dali/public-api/dali-core.h>
27 #include <dali/integration-api/debug.h>
30 namespace // un-named namespace
33 const unsigned int MAX_SET_PROPERTY_STRING_LENGTH = 256; ///< maximum length of a set property command
35 class JsonPropertyValue
38 JsonPropertyValue( const std::string& str )
40 std::size_t strLength = str.length();
42 mString.reserve( strLength );
43 for( std::size_t i = 0; i < strLength; ++i )
45 const char c = str[i];
46 if( (c != '[') && c != ']')
48 mString.push_back( c );
53 std::string GetString() const
57 float GetFloat() const
59 return atof( mString.c_str() );
63 return atoi( mString.c_str() );
67 return (GetInt() != 0);
70 Dali::Vector2 GetVector2()
74 int count = sscanf( mString.c_str(),"%f,%f",&vec2.x,&vec2.y );
77 DALI_LOG_ERROR("Bad format\n");
82 Dali::Vector3 GetVector3()
86 int count = sscanf( mString.c_str(),"%f,%f,%f",&vec3.x,&vec3.y,&vec3.z );
89 DALI_LOG_ERROR("Bad format\n");
94 Dali::Vector4 GetVector4()
98 int count = sscanf( mString.c_str(),"%f,%f,%f,%f", &vec4.x, &vec4.y, &vec4.z, &vec4.w );
101 DALI_LOG_ERROR("Bad format\n");
111 void SetProperty( Dali::Handle handle, int propertyId, JsonPropertyValue& propertyValue )
113 Dali::Property::Type type = handle.GetPropertyType( propertyId );
116 case Dali::Property::FLOAT:
118 float val = propertyValue.GetFloat();
119 handle.SetProperty( propertyId, Dali::Property::Value( val ) );
122 case Dali::Property::INTEGER:
124 int val = propertyValue.GetInt();
125 handle.SetProperty( propertyId, Dali::Property::Value( val ) );
128 case Dali::Property::BOOLEAN:
130 bool val = propertyValue.GetBoolean();
131 handle.SetProperty( propertyId, Dali::Property::Value( val ) );
134 case Dali::Property::STRING:
136 std::string str = propertyValue.GetString();
137 handle.SetProperty( propertyId, Dali::Property::Value( str ) );
140 case Dali::Property::VECTOR2:
142 Dali::Vector2 val = propertyValue.GetVector2();
143 handle.SetProperty( propertyId, Dali::Property::Value( val ) );
146 case Dali::Property::VECTOR3:
148 Dali::Vector3 val = propertyValue.GetVector3();
149 handle.SetProperty( propertyId, Dali::Property::Value( val ) );
152 case Dali::Property::VECTOR4:
154 Dali::Vector4 val = propertyValue.GetVector4();
155 handle.SetProperty( propertyId, Dali::Property::Value( val ) );
165 int SetProperties( const std::string& setPropertyMessage )
167 std::istringstream iss( setPropertyMessage );
169 getline( iss, token, '|' ); // swallow command name
170 while( getline( iss, token, '|' ) )
172 std::string actorId, propName, propValue;
173 if( token.compare( "---" ) != 0 )
175 std::istringstream propss( token );
176 getline( propss, actorId, ';' );
177 getline( propss, propName, ';' );
178 getline( propss, propValue );
180 Dali::Actor root = Dali::Stage::GetCurrent().GetRootLayer();
181 int id = atoi( actorId.c_str() );
182 Dali::Actor a = root.FindChildById( id );
185 // lookup by name for custom properties
186 int propId = a.GetPropertyIndex( propName );
189 JsonPropertyValue pv( propValue );
190 SetProperty( a, propId, pv );
201 }; // un-named namespace
203 inline std::string Quote( const std::string& in )
205 return (std::string( "\"" ) + in + std::string( "\"" ));
209 std::string ToString( T i )
211 std::stringstream ss;
220 // currently rotations are output in Euler format ( may change)
221 void AppendPropertyNameAndValue( Dali::Handle handle, int propertyIndex, std::ostringstream& outputStream)
223 // get the property name and the value as a string
224 std::string propertyName( handle.GetPropertyName( propertyIndex ) );
225 Dali::Property::Value value = handle.GetProperty( propertyIndex );
227 // Apply quotes around the property name and the value.. "color", "1.3, 3.4, 2.6"
228 outputStream << "\"" << propertyName << "\"" << ",";
229 outputStream << "\"" << value << "\"";
233 bool ExcludeProperty( int propIndex )
235 return (propIndex == Dali::Actor::Property::NAME ||
237 // all of these are repeat properties of values in vectors....
238 // We don't really need these in the UI
239 propIndex == Dali::Actor::Property::ANCHOR_POINT_X || propIndex == Dali::Actor::Property::ANCHOR_POINT_Y || propIndex == Dali::Actor::Property::ANCHOR_POINT_Z || propIndex == Dali::Actor::Property::PARENT_ORIGIN_X
240 || propIndex == Dali::Actor::Property::PARENT_ORIGIN_Y || propIndex == Dali::Actor::Property::PARENT_ORIGIN_Z || propIndex == Dali::Actor::Property::COLOR_RED || propIndex == Dali::Actor::Property::COLOR_GREEN
241 || propIndex == Dali::Actor::Property::COLOR_BLUE || propIndex == Dali::Actor::Property::COLOR_ALPHA|| propIndex == Dali::Actor::Property::POSITION_X || propIndex == Dali::Actor::Property::POSITION_Y
242 || propIndex == Dali::Actor::Property::POSITION_Z|| propIndex == Dali::Actor::Property::SIZE_WIDTH|| propIndex == Dali::Actor::Property::SIZE_HEIGHT || propIndex == Dali::Actor::Property::SCALE_X || propIndex == Dali::Actor::Property::SCALE_Y
243 || propIndex == Dali::Actor::Property::SCALE_Z || propIndex == Dali::Actor::Property::SIZE_DEPTH);
246 std::string DumpJson( Dali::Actor actor, int level )
248 // All the information about this actor
249 std::ostringstream msg;
250 msg << "{ " << Quote( "Name" ) << " : " << Quote( actor.GetName() ) << ", " << Quote( "level" ) << " : " << level << ", " << Quote( "id" ) << " : " << actor.GetId() << ", " << Quote( "IsVisible" )
251 << " : " << actor.IsVisible() << ", " << Quote( "IsSensitive" ) << " : " << actor.IsSensitive();
253 msg << ", " << Quote( "properties" ) << ": [ ";
255 Dali::Property::IndexContainer indices;
256 actor.GetPropertyIndices( indices );
258 Dali::Property::IndexContainer::Iterator iter = indices.Begin();
260 for( ; iter != indices.End() ; iter++ )
263 if( !ExcludeProperty( i ) )
265 if( numCustom++ != 0 )
271 AppendPropertyNameAndValue( actor, i,msg );
277 msg << ", " << Quote( "children" ) << " : [ ";
279 // Recursively dump all the children as well
280 for( unsigned int i = 0 ; i < actor.GetChildCount() ; ++i )
286 msg << DumpJson( actor.GetChildAt( i ), level + 1 );
293 std::string GetActorTree()
295 Dali::Actor actor = Dali::Stage::GetCurrent().GetRootLayer();
296 std::string str = DumpJson( actor, 0 );
311 void SetProperty( const std::string& message )
313 // check the set property length is within range
314 if( message.length() > MAX_SET_PROPERTY_STRING_LENGTH )
316 DALI_LOG_ERROR("SetProperty message length too long, size = %ul\n", message.length());
320 SetProperties( message );
323 void DumpScene( unsigned int clientId, ClientSendDataInterface* sendData )
326 std::string json = GetActorTree();
327 int length = json.length();
328 snprintf( buf, 32, "%d\n", length );
329 std::string header( buf );
331 sendData->SendData( json.c_str(), json.length(), clientId );
334 } // namespace Automation
336 } // namespace Internal
338 } // namespace Adaptor