From 1f5385153bcf7e9539c639a1de78f4f7e8f1fc2c Mon Sep 17 00:00:00 2001 From: Nobuhiko Tanibata Date: Wed, 27 Nov 2013 21:28:52 +0900 Subject: ivi-layermanagement-examples: Add LayerManagerControl for examples Signed-off-by: Nobuhiko Tanibata --- .../LayerManagerControl/CMakeLists.txt | 54 ++ .../LayerManagerControl/include/Expression.h | 72 ++ .../include/ExpressionInterpreter.h | 50 ++ .../LayerManagerControl/include/LMControl.h | 337 ++++++++ .../LayerManagerControl/include/SceneStore.h | 616 ++++++++++++++ .../LayerManagerControl/include/StringMapTree.h | 65 ++ .../LayerManagerControl/src/Expression.cpp | 429 ++++++++++ .../src/ExpressionInterpreter.cpp | 202 +++++ .../LayerManagerControl/src/analyze.cpp | 675 +++++++++++++++ .../LayerManagerControl/src/commands.cpp | 838 +++++++++++++++++++ .../LayerManagerControl/src/common.cpp | 733 +++++++++++++++++ .../LayerManagerControl/src/control.cpp | 417 ++++++++++ .../LayerManagerControl/src/demo.cpp | 903 +++++++++++++++++++++ .../LayerManagerControl/src/main.cpp | 51 ++ .../LayerManagerControl/src/print.cpp | 445 ++++++++++ .../LayerManagerControl/src/scatter.cpp | 344 ++++++++ .../LayerManagerControl/src/sceneio.cpp | 775 ++++++++++++++++++ .../LayerManagerControl/src/util.cpp | 117 +++ 18 files changed, 7123 insertions(+) create mode 100644 ivi-layermanagement-examples/LayerManagerControl/CMakeLists.txt create mode 100644 ivi-layermanagement-examples/LayerManagerControl/include/Expression.h create mode 100644 ivi-layermanagement-examples/LayerManagerControl/include/ExpressionInterpreter.h create mode 100644 ivi-layermanagement-examples/LayerManagerControl/include/LMControl.h create mode 100644 ivi-layermanagement-examples/LayerManagerControl/include/SceneStore.h create mode 100644 ivi-layermanagement-examples/LayerManagerControl/include/StringMapTree.h create mode 100644 ivi-layermanagement-examples/LayerManagerControl/src/Expression.cpp create mode 100644 ivi-layermanagement-examples/LayerManagerControl/src/ExpressionInterpreter.cpp create mode 100644 ivi-layermanagement-examples/LayerManagerControl/src/analyze.cpp create mode 100644 ivi-layermanagement-examples/LayerManagerControl/src/commands.cpp create mode 100644 ivi-layermanagement-examples/LayerManagerControl/src/common.cpp create mode 100644 ivi-layermanagement-examples/LayerManagerControl/src/control.cpp create mode 100644 ivi-layermanagement-examples/LayerManagerControl/src/demo.cpp create mode 100644 ivi-layermanagement-examples/LayerManagerControl/src/main.cpp create mode 100644 ivi-layermanagement-examples/LayerManagerControl/src/print.cpp create mode 100644 ivi-layermanagement-examples/LayerManagerControl/src/scatter.cpp create mode 100644 ivi-layermanagement-examples/LayerManagerControl/src/sceneio.cpp create mode 100644 ivi-layermanagement-examples/LayerManagerControl/src/util.cpp (limited to 'ivi-layermanagement-examples') diff --git a/ivi-layermanagement-examples/LayerManagerControl/CMakeLists.txt b/ivi-layermanagement-examples/LayerManagerControl/CMakeLists.txt new file mode 100644 index 0000000..62136b4 --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/CMakeLists.txt @@ -0,0 +1,54 @@ +############################################################################ +# +# Copyright 2012 BMW Car IT GmbH +# +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +############################################################################ + +project (LayerManagerControl) +project_type(CORE) + +include_directories( + include + "${CMAKE_SOURCE_DIR}/ivi-layermanagement-api/ilmCommon/include" + "${CMAKE_SOURCE_DIR}/ivi-layermanagement-api/ilmClient/include" + "${CMAKE_SOURCE_DIR}/ivi-layermanagement-api/ilmControl/include" + "${CMAKE_SOURCE_DIR}/ivi-layermanagement-example/LayerManagerUtils/include" +) + +add_executable(${PROJECT_NAME} + src/main.cpp + src/commands.cpp + src/analyze.cpp + src/common.cpp + src/control.cpp + src/demo.cpp + src/Expression.cpp + src/ExpressionInterpreter.cpp + src/print.cpp + src/scatter.cpp + src/sceneio.cpp + src/util.cpp +) + +add_dependencies(${PROJECT_NAME} + ilmClient + ilmControl + LayerManagerUtils +) + +target_link_libraries(${PROJECT_NAME} ilmClient ilmControl) + +install (TARGETS ${PROJECT_NAME} DESTINATION bin) diff --git a/ivi-layermanagement-examples/LayerManagerControl/include/Expression.h b/ivi-layermanagement-examples/LayerManagerControl/include/Expression.h new file mode 100644 index 0000000..d31f87c --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/include/Expression.h @@ -0,0 +1,72 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ +#ifndef __EXPRESSION_H__ +#define __EXPRESSION_H__ + +#include +#include +using namespace std; + +class Expression; + +typedef void(*callback)(Expression*); +typedef list ExpressionList; + +class Expression +{ +public: + Expression(string name, Expression* parent); + string getName(); + + Expression* getPreviousExpression(); + void addNextExpression(Expression* word); + Expression* getNextExpression(string text); + ExpressionList getNextExpressions(); + ExpressionList getClosure(bool bypass); + ExpressionList getNextExpressionClosure(string text); + ExpressionList getClosureExecutables(bool canBypass); + + void setFunc(callback funcPtr); + void execute(); + bool isExecutable(); + + bool isVar(); + void setVarValue(string value); + + string getString(string name); + unsigned int getUint(string name); + void getUintArray(string name, unsigned int** array, unsigned int* scount); + int getInt(string name); + double getDouble(string name); + bool getBool(string name); + bool contains(string name); + + void printTree(int level = 0); + void printList(string list = ""); + +private: + string mName; + ExpressionList mNextWords; + Expression* mPreviousWord; + callback mFuncPtr; + string mVarValue; + string mMatchText; +}; + +#endif // __EXPRESSION_H__ diff --git a/ivi-layermanagement-examples/LayerManagerControl/include/ExpressionInterpreter.h b/ivi-layermanagement-examples/LayerManagerControl/include/ExpressionInterpreter.h new file mode 100644 index 0000000..831489f --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/include/ExpressionInterpreter.h @@ -0,0 +1,50 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ +#ifndef __EXPRESSIONINTERPRETER_H__ +#define __EXPRESSIONINTERPRETER_H__ + +#include "Expression.h" +#include +using namespace std; + +enum CommandResult +{ + CommandSuccess, + CommandIncomplete, + CommandInvalid, + CommandExecutionFailed +}; + +class ExpressionInterpreter +{ +public: + ExpressionInterpreter(); + CommandResult interpretCommand(string userInput); + string getLastError(); + static void printExpressionTree(); + static void printExpressionList(); + + static bool addExpression(callback funcPtr, string command); + +private: + static Expression* mpRoot; + string mErrorText; +}; + +#endif // __EXPRESSIONINTERPRETER_H__ diff --git a/ivi-layermanagement-examples/LayerManagerControl/include/LMControl.h b/ivi-layermanagement-examples/LayerManagerControl/include/LMControl.h new file mode 100644 index 0000000..3ede657 --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/include/LMControl.h @@ -0,0 +1,337 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ + +#ifndef __LMCONTROL_H__ +#define __LMCONTROL_H__ + +#include +using std::map; + +#include +using std::vector; + +#include +using std::set; + +#include +using std::string; + +/* + * Datastructure that contains all information about a scene + */ +struct t_scene_data +{ + map > screenLayers; + map > layerSurfaces; + + map surfaceProperties; + map layerProperties; + + map layerScreen; + map surfaceLayer; + + vector surfaces; + vector layers; + vector screens; + + t_ilm_layer extraLayer; + t_ilm_uint screenWidth; + t_ilm_uint screenHeight; +}; + +/* + * Vector of four integers + */ +struct tuple4 +{ +public: + int x; + int y; + int z; + int w; + + tuple4(int _x, int _y, int _z, int _w) : + x(_x), y(_y), z(_z), w(_w) + { + } + + tuple4() : + x(0), y(0), z(0), w(0) + { + } + + tuple4(const tuple4& other) + { + x = other.x; + y = other.y; + z = other.z; + w = other.w; + } + + void scale(float s) + { + x = static_cast(x * s); + y = static_cast(y * s); + z = static_cast(z * s); + w = static_cast(w * s); + } + + const tuple4& operator=(const tuple4& other) + { + x = other.x; + y = other.y; + z = other.z; + w = other.w; + return *this; + } +}; + + + +//============================================================================= +//common.cpp +//============================================================================= +/* + * Captures all information about the rendered scene into an object of type t_scene_data + */ +void captureSceneData(t_scene_data* pScene); + +/* + * Calculates the final coordinates of a surface on the screen in the scene + */ +tuple4 getSurfaceScreenCoordinates(t_scene_data* pScene, t_ilm_surface surface); + +/* + * Gets render order of surfaces in a scene. A surface is execluded from the render order + * if it does not belong to a layer or if it belongs to a layer that does not belong to a screen + * + * The surface at index 0 is the deepest surface in the scene, and surface at size()-1 is + * the topmost surface + */ +vector getSceneRenderOrder(t_scene_data* pScene); + +/* + * Sets a scene to be restored if an interrupt signal (Ctrl+C) is received + */ +void setSceneToRestore(t_scene_data* pScene); + +/* + * Clones the given scene into another t_scene_data object that would look exactly the same + * but all rendered surfaces are rendered on one (newly created) layer. + * Order, sizes and positions of rendered surfaces are preserved relative to screen + * + */ +t_scene_data cloneToUniLayerScene(t_scene_data* pScene); + +/* + * Hide the currently rendered scene, and render the given scene instead + * If clean is set to true all layers and surfaces that do not exist in the given + * scene are destroyed + */ +void setScene(t_scene_data* pScene, bool clean = false); + +/* + * Makes a smooth transformation effect from the intial to the final scene + */ +void transformScene(t_scene_data* pInitialScene, t_scene_data* pFinalScene, t_ilm_long durationMillis, t_ilm_int frameCount); + +/* + * Creates a new empty scene + */ +void emptyScene(t_scene_data* pScene); + +/* + * Returns true if the surface is rendered to the screen + * (If the surface belongs to a layer that belongs to a screen) + */ +t_ilm_bool surfaceRenderedOnScreen(t_scene_data& scene, t_ilm_surface surface); + + +//============================================================================= +//util.cpp +//============================================================================= + +/* + * Returns true if the rectangle A is inside (or typical with) rectangle B + * (each tuple represents a rectangle in 2-D coordinates as ) + */ +t_ilm_bool inside(tuple4 A, tuple4 B); + +/* + * Returns true if a is in the interval between b1 and b2 (inclusive) + */ +t_ilm_bool between(int b1, int a, int b2); + + +/* + * Returns true if the rectangle A intersects (or touches) rectangle B + * (each tuple represents a rectangle in 2-D coordinates as ) + */ +t_ilm_bool intersect(tuple4 A, tuple4 B); + +/* + * Trim white space characters from the beginning of the string + */ +string rtrim(string s); + +/* + * Replace all occurances of a in s by b + */ +string replaceAll(string s, string a, string b); +string replaceAll(string s, char a, char b); + +/* + * For every pair (a,b) in the map: replace all occurances of a in the string by b + */ +string replaceAll(string s, map replacements); + +/* + * Split the string using the giving delimiter + */ +set split(string s, char d); + + +//============================================================================= +//print.cpp +//============================================================================= + +/* + * Functions for printing arrays, vector and maps + */ +void printArray(const char* text, unsigned int* array, int count); + +template +void printArray(const char* text, T* array, int count); + +template +void printVector(const char* text, vector v); + +template +void printMap(const char* text, std::map m); + +/* + * Prints information about the specified screen + */ +void printScreenProperties(unsigned int screenid, const char* prefix = ""); + +/* + * Prints information about the specified layer + */ +void printLayerProperties(unsigned int layerid, const char* prefix = ""); + +/* + * Prints information about the specified surface + */ +void printSurfaceProperties(unsigned int surfaceid, const char* prefix = ""); + +/* + * Prints information about rendered scene + * (All screens, all rendered layers, all rendered surfaces) + */ +void printScene(); + + +//============================================================================= +//control.cpp +//============================================================================= +void getCommunicatorPerformance(); +void setSurfaceKeyboardFocus(t_ilm_surface surface); +void getKeyboardFocus(); +void setSurfaceAcceptsInput(t_ilm_surface surfaceId, string kbdPointerTouch, t_ilm_bool acceptance); +void testNotificationLayer(t_ilm_layer layerid); +void watchLayer(unsigned int* layerids, unsigned int layeridCount); +void watchSurface(unsigned int* surfaceids, unsigned int surfaceidCount); +void setOptimization(t_ilm_uint id, t_ilm_uint mode); +void getOptimization(t_ilm_uint id); + + +//============================================================================= +//analyze.cpp +//============================================================================= + +/* + * Runs and prints the results for a set of analysis procedures to check if there are any potential problems that + * might lead to the surface being not rendered or not visible + */ +t_ilm_bool analyzeSurface(t_ilm_surface targetSurfaceId); + + +//============================================================================= +//scatter.cpp +//============================================================================= + +/* + * Divides the screen into a grid of equal sized portions, and displays every rendered + * surface in a separate tile with its relative size and position preserved + * + * Only rendred surfaces are considered. Surfaces are displayed according to their render order + * topleft-to-rightbottom + */ +void scatter(); + + +/* + * Divides the screen into a grid of equal sized portions, and displays every existing surface + * in a separate tile with its relative size and position preserved + * + * All existing surfaces are considered. surfaces that have 0 visibility are set to visibility 1. + * Surfaces that have 0 opacity are set to opacity 1. Surfaces that are not on any layer and surfaces + * that are on layers that are not on any screen are displayed as well + */ +void scatterAll(); + +/* + * Returns a scattered version of the scene + */ +t_scene_data getScatteredScene(t_scene_data* pInitialScene); + + +//============================================================================= +//demo.cpp +//============================================================================= + +/* + * Run a set of effects on the rendered scene depending on the parameter. + * The function accepts values in range 0-4 + */ +void demo(t_ilm_uint mode); + + +//============================================================================= +//sceneio.cpp +//============================================================================= + +/* + * Saves a representation of the current rendered scene to a file + */ +void exportSceneToFile(string filename); + +/* + * Saves an xtext representation of the grammar of the scene + */ +void exportXtext(string fileName, string grammar, string url); + +/* + * Imports a scene from a saved text file + */ +void importSceneFromFile(string filename); + + + +#endif diff --git a/ivi-layermanagement-examples/LayerManagerControl/include/SceneStore.h b/ivi-layermanagement-examples/LayerManagerControl/include/SceneStore.h new file mode 100644 index 0000000..d2978fe --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/include/SceneStore.h @@ -0,0 +1,616 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ + +#ifndef __SCENESTORE_H__ +#define __SCENESTORE_H__ + + +//========================================================================= +// helper macros fileformat +//========================================================================= +#include + +#include +#include +#include +#include + +using std::string; +using std::stringstream; +using std::ostream; + +#include +using std::list; + +#include +using std::vector; + + + + +template string getPrimitiveType(T var) +{ + return ""; +} + +template string getPrimitiveType(T* var) +{ + (void) var;//suppress warning: unsued variable + T var2 = 0; + return getPrimitiveType(var2) + "*"; +} + +template<> string getPrimitiveType(bool var) +{ + (void) var;//suppress warning: unsued variable + return "bool"; +} + +template<> string getPrimitiveType(char var) +{ + (void) var;//suppress warning: unsued variable + return "char"; +} + +template<> string getPrimitiveType(signed char var) +{ + (void) var;//suppress warning: unsued variable + return "signed char"; +} + +template<> string getPrimitiveType(unsigned char var) +{ + (void) var;//suppress warning: unsued variable + return "unsigned char"; +} + +template<> string getPrimitiveType(wchar_t var) +{ + (void) var;//suppress warning: unsued variable + return "wchar_t"; +} + +template<> string getPrimitiveType(short int var) +{ + (void) var;//suppress warning: unsued variable + return "short int"; +} + +template<> string getPrimitiveType(unsigned short int var) +{ + (void) var;//suppress warning: unsued variable + return "unsigned short int"; +} + +template<> string getPrimitiveType(long int var) +{ + (void) var;//suppress warning: unsued variable + return "long int"; +} + +template<> string getPrimitiveType(unsigned long int var) +{ + (void) var;//suppress warning: unsued variable + return "unsigned long int"; +} + +template<> string getPrimitiveType(int var) +{ + (void) var;//suppress warning: unsued variable + return "int"; +} + +template<> string getPrimitiveType(unsigned int var) +{ + (void) var;//suppress warning: unsued variable + return "unsigned int"; +} + +template<> string getPrimitiveType(float var) +{ + (void) var;//suppress warning: unsued variable + return "float"; +} + +template<> string getPrimitiveType(double var) +{ + (void) var;//suppress warning: unsued variable + return "double"; +} + +template<> string getPrimitiveType(long double var) +{ + (void) var;//suppress warning: unsued variable + return "long double"; +} + +template<> string getPrimitiveType(string var) +{ + (void) var;//suppress warning: unsued variable + return "string"; +} + + +#if defined(__GXX_EXPERIMENTAL_CXX0X) || __cplusplus >= 201103L +template<> string getPrimitiveType(char16_t var) +{ + (void) var;//suppress warning: unsued variable + return "char16_t"; +} + +template<> string getPrimitiveType(char32_t var) +{ + (void) var;//suppress warning: unsued variable + return "char32_t"; +} + +template<> string getPrimitiveType(long long int var) +{ + (void) var;//suppress warning: unsued variable + return "long long int"; +} + +template<> string getPrimitiveType(unsigned long long int var) +{ + (void) var;//suppress warning: unsued variable + return "unsigned long long int"; +} +#endif + + +struct WrapperHelper +{ +public: + const string mType; + WrapperHelper(string t) : + mType(t) + { + } + + virtual ~WrapperHelper() + { + } + + virtual void fromString(string s) + { + (void) s;//suppress warning: unsued variable + } + + virtual string asString() + { + return ""; + } + + virtual void toStringMapTree(StringMapTree* parent) + { + (void) parent;//suppress warning: unsued variable + } + + virtual void toGrammarMapTree(StringMapTree* tree) + { + (void) tree;//suppress warning: unsued variable + } + + virtual WrapperHelper* tryClone(string type, StringMapTree* tree) + { + (void) type;//suppress warning: unsued variable + (void) tree;//suppress warning: unsued variable + return NULL; + } + + virtual void addToComplexWrapper(WrapperHelper* wrapper) + { + (void) wrapper;//suppress warning: unsued variable + } + + virtual string getWrapperPrimitiveType() + { + return ""; + } +}; + +template +struct ComplexWrapper : public WrapperHelper +{ +public: + list components; + + ComplexWrapper(string t) : + WrapperHelper(t) + { + } + + virtual void toStringMapTree(StringMapTree* parent) + { + for (typename list::iterator it = components.begin(); it != components.end(); ++it) + { + StringMapTree* node = new StringMapTree; + (*it)->toStringMapTree(node); + parent->mChildren.push_back(node); + } + } +}; + +template +struct BasicWrapper : public WrapperHelper +{ +public: + T value; + + BasicWrapper(string t) : + WrapperHelper(t) + { + } + + virtual void fromString(string s) + { + stringstream ss; + ss.str(s); + ss >> std::skipws >> value; + } + + virtual string asString() + { + stringstream ss; + ss << value; + return ss.str(); + } + + virtual string getWrapperPrimitiveType() + { + return getPrimitiveType(value); + } +}; + +template<> +class BasicWrapper: public WrapperHelper +{ +public: + string value; + + BasicWrapper(string t) : + WrapperHelper(t) + { + } + + virtual void fromString(string s) + { + value = s; + } + + virtual string asString() + { + return value; + } + + virtual string getWrapperPrimitiveType() + { + return getPrimitiveType(value); + } +}; + +template<> +class BasicWrapper: public WrapperHelper +{ +public: + const char* value; + + BasicWrapper(char* t) : + WrapperHelper(t) + { + } + + virtual void fromString(string s) + { + value = s.c_str(); + } + + virtual string asString() + { + return value; + } + + virtual string getWrapperPrimitiveType() + { + return getPrimitiveType(value); + } +}; + +template +struct DummyWrapper : public WrapperHelper +{ +public: + T value; + + DummyWrapper(string t) : + WrapperHelper(t) + { + } + + virtual WrapperHelper* tryClone(string type, StringMapTree* tree) + { + if (type == mType) + { + DummyWrapper* newWrapper = new DummyWrapper(type); + newWrapper->value.fromStringMapTree(tree); + return newWrapper; + } + + return NULL; + } + + virtual void toGrammarMapTree(StringMapTree* tree) + { + value.toGrammarMapTree(tree); + } + + virtual void addToComplexWrapper(WrapperHelper* wrapper) + { + ComplexWrapper* complexWrapper = static_cast* >(wrapper); + complexWrapper->components.push_back(&value); + } +}; + +map _globalTypeIndexdToType; + +#define OBJECT(class_name) \ +class class_name;\ +ostream& operator<<(ostream& out, class_name& obj )\ +{\ + (void) obj;\ + return out;\ +}\ +istream& operator>>(istream& in, class_name& obj)\ +{\ + (void) obj;\ + return in;\ +}\ +class class_name { \ +public:\ + map properties;\ + map typesMap;\ + map components;\ + map dummyComponentClones;\ +public:\ + const static int classNameIndex = __COUNTER__;\ + static int getClassNameIndex()\ + {\ + return classNameIndex;\ + }\ + \ + string mClassName;\ + template bool get(string key, T* p)\ + {\ + if(properties.find(key) != properties.end())\ + {\ + BasicWrapper* obj = (static_cast*> (properties[key]));\ + if(obj) *p = obj->value;\ + return obj != NULL;\ + }\ + return false;\ + }\ + template bool get(int* count, T*** p)\ + {\ + string type = _globalTypeIndexdToType[T::getClassNameIndex()];\ + if(components.find(type) != components.end())\ + {\ + ComplexWrapper* obj = static_cast* >(components[type]);\ + if(obj){\ + vector* temp = new vector(obj->components.begin(), obj->components.end());\ + *p = temp->data();\ + *count = obj->components.size();\ + }\ + return obj != NULL;\ + }\ + return false;\ + }\ + template bool get(list* p)\ + {\ + string type = _globalTypeIndexdToType[T::getClassNameIndex()];\ + if(components.find(type) != components.end())\ + {\ + ComplexWrapper* obj = static_cast* >(components[type]);\ + if(obj){\ + *p = obj->components;\ + }\ + return obj != NULL;\ + }\ + return false;\ + }\ + template bool set(string key, const T& p)\ + {\ + BasicWrapper* obj = (static_cast*> (properties[key]));\ + if(obj) obj->value = p;\ + return obj != NULL;\ + }\ + template bool set(string key, T* p)\ + {\ + BasicWrapper* obj = (static_cast*> (properties[key]));\ + if(obj) obj->value = p;\ + return obj != NULL;\ + }\ + string getType(string key)\ + {\ + return typesMap[key];\ + }\ + template bool add(T* obj)\ + {\ + if(components.find(obj->mClassName) != components.end())\ + {\ + (static_cast* > (components[obj->mClassName])->components).push_back(obj);\ + return true;\ + }\ + return false;\ + }\ + template bool unwrapAndAdd(DummyWrapper* obj)\ + {\ + if(components.find(obj->mClassName) != components.end())\ + {\ + (static_cast* > (components[obj->mClassName])->components).push_back(obj);\ + return true;\ + }\ + return false;\ + }\ + template bool remove(T* obj)\ + {\ + if(components.find(obj->mClassName) != components.end())\ + {\ + (static_cast* > (components[obj->mClassName])->components).remove(obj);\ + return true;\ + }\ + return false;\ + }\ + ~class_name()\ + {\ + for(map::iterator it = properties.begin(); it != properties.end();++it)\ + delete (*it).second;\ + for(map::iterator it = components.begin(); it != components.end();++it)\ + delete (*it).second;\ + }\ + class_name():mClassName(#class_name)\ + {\ + _globalTypeIndexdToType[getClassNameIndex()] = #class_name; + +#define PROPERTY(type, name) \ + properties[#name] = new BasicWrapper(#type);\ + typesMap[#name] = #type; + + +#define CONTAINS(type) \ + components[#type] = new ComplexWrapper(#type);\ + dummyComponentClones[#type]= (new DummyWrapper(#type)); + + +#define OBJECTEND \ + }\ + void toGrammarMapTree(StringMapTree* node)\ + {\ + node->mNodeLabel = mClassName;\ + for(map::iterator it = properties.begin(); it != properties.end();++it)\ + {\ + node->mNodeValues[(*it).first] = make_pair((*it).second->mType, it->second->getWrapperPrimitiveType());\ + }\ + for(map::iterator it = dummyComponentClones.begin(); it != dummyComponentClones.end();++it)\ + {\ + StringMapTree* child = new StringMapTree;\ + node->mChildren.push_back(child);\ + it->second->toGrammarMapTree(child);\ + }\ + }\ + void toStringMapTree(StringMapTree* node)\ + {\ + node->mNodeLabel = mClassName;\ + for(map::iterator it = properties.begin(); it != properties.end();++it)\ + {\ + node->mNodeValues[(*it).first] = make_pair((*it).second->mType, (*it).second->asString());\ + }\ + for(map::iterator it = components.begin(); it != components.end();++it)\ + {\ + it->second->toStringMapTree(node);\ + }\ + }\ + void fromStringMapTree(StringMapTree* node)\ + {\ + mClassName = node->mNodeLabel;\ + for(map >::iterator it = node->mNodeValues.begin(); it != node->mNodeValues.end();++it)\ + {\ + properties[it->first]->fromString(it->second.second);\ + }\ + for(list::iterator it = node->mChildren.begin(); it != node->mChildren.end(); ++it )\ + {\ + string type = (*it)->mNodeLabel;\ + WrapperHelper* wrapper = dummyComponentClones[type]->tryClone(type, (*it));\ + WrapperHelper* complexWrapper = components[type];\ + wrapper->addToComplexWrapper(complexWrapper);\ + }\ + }\ +}; + + + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//**************************************************************************************************************************// +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + + +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//**************************************************************************************************************************// +////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + +OBJECT(IlmSurface) + PROPERTY(t_ilm_surface, id) + PROPERTY(t_ilm_float, opacity) + PROPERTY(t_ilm_uint, sourceX) + PROPERTY(t_ilm_uint, sourceY) + PROPERTY(t_ilm_uint, sourceWidth) + PROPERTY(t_ilm_uint, sourceHeight) + PROPERTY(t_ilm_uint, origSourceWidth) + PROPERTY(t_ilm_uint, origSourceHeight) + PROPERTY(t_ilm_uint, destX) + PROPERTY(t_ilm_uint, destY) + PROPERTY(t_ilm_uint, destWidth) + PROPERTY(t_ilm_uint, destHeight) + PROPERTY(int, orientation) + PROPERTY(t_ilm_bool, visibility) + PROPERTY(t_ilm_uint, frameCounter) + PROPERTY(t_ilm_uint, drawCounter) + PROPERTY(t_ilm_uint, updateCounter) + PROPERTY(t_ilm_uint, pixelformat) + PROPERTY(t_ilm_uint, nativeSurface) + PROPERTY(ilmInputDevice, inputDevicesAcceptance) + /***/ +OBJECTEND + +OBJECT(IlmLayer) + PROPERTY(t_ilm_layer, id) + PROPERTY(t_ilm_float, opacity) + PROPERTY(t_ilm_uint, sourceX) + PROPERTY(t_ilm_uint, sourceY) + PROPERTY(t_ilm_uint, sourceWidth) + PROPERTY(t_ilm_uint, sourceHeight) + PROPERTY(t_ilm_uint, origSourceWidth) + PROPERTY(t_ilm_uint, origSourceHeight) + PROPERTY(t_ilm_uint, destX) + PROPERTY(t_ilm_uint, destY) + PROPERTY(t_ilm_uint, destWidth) + PROPERTY(t_ilm_uint, destHeight) + PROPERTY(int, orientation) + PROPERTY(t_ilm_bool, visibility) + PROPERTY(t_ilm_uint, type) + /***/ + CONTAINS(IlmSurface) +OBJECTEND + +OBJECT(IlmDisplay) + PROPERTY(t_ilm_display, id) + PROPERTY(t_ilm_uint, width) + PROPERTY(t_ilm_uint, height) + /***/ + CONTAINS(IlmLayer) +OBJECTEND + +OBJECT(IlmScene) + /***/ + CONTAINS(IlmSurface) + CONTAINS(IlmLayer) + CONTAINS(IlmDisplay) +OBJECTEND + +#endif /* __SCENESTORE_H__ */ diff --git a/ivi-layermanagement-examples/LayerManagerControl/include/StringMapTree.h b/ivi-layermanagement-examples/LayerManagerControl/include/StringMapTree.h new file mode 100644 index 0000000..51f3b1e --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/include/StringMapTree.h @@ -0,0 +1,65 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ + + +#ifndef __STRINGMAPTREE_H__ +#define __STRINGMAPTREE_H__ + +#include +#include +#include + +using std::map; +using std::list; +using std::string; + +class StringMapTree +{ +public: + string mNodeLabel; + map > mNodeValues;//key: property name, value= pair(type, property value) + list mChildren;// pair(type, child component node) + + string toString(string prefix = "") + { + string result; + result += prefix + mNodeLabel + ":{\n"; + for(map >::iterator it = mNodeValues.begin(); it != mNodeValues.end(); ++it) + { + result += prefix + "\t[" + it->first + ":" + it->second.first + "]=[" + it->second.second + "]\n"; + } + for(list::iterator it = mChildren.begin(); it != mChildren.end(); ++it) + { + result += (*it)->toString(prefix + "\t") +"\n"; + } + result += prefix + "}"; + + return result; + } + + virtual ~StringMapTree() + { + for(list::iterator it = mChildren.begin(); it != mChildren.end(); ++it) + { + delete *it; + } + } +}; + +#endif /* __STRINGMAPTREE_H__ */ diff --git a/ivi-layermanagement-examples/LayerManagerControl/src/Expression.cpp b/ivi-layermanagement-examples/LayerManagerControl/src/Expression.cpp new file mode 100644 index 0000000..a08dc8f --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/src/Expression.cpp @@ -0,0 +1,429 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ +#include "Expression.h" +#include +#include +#include +#include // memcpy +#include + +Expression::Expression(string name, Expression* parent) +: mName(name) +, mPreviousWord(parent) +, mFuncPtr(NULL) +, mMatchText("") +{ +} + +void Expression::setVarValue(string value) +{ + mVarValue = value; +} + +bool Expression::isVar() +{ + return mName[0] == '<' || (mName[0] == '[' && mName[1] == '<'); +} + +string Expression::getString(string name) +{ + //remove brackets if needed + string noBrackets = mName; + noBrackets = noBrackets[0] == '[' ? noBrackets.substr(1, noBrackets.size() - 1) : noBrackets; + noBrackets = noBrackets[noBrackets.size() - 1] == ']' ? noBrackets.substr(0, noBrackets.size() - 1) : noBrackets; + + noBrackets = noBrackets[0] == '<' ? noBrackets.substr(1, noBrackets.size() - 1) : noBrackets; + noBrackets = noBrackets[noBrackets.size() - 1] == '>' ? noBrackets.substr(0, noBrackets.size() - 1) : noBrackets; + + //remove default value (if needed) + string exprName = noBrackets.substr(0, noBrackets.find("=")); + + if (exprName == name) + { + if (mMatchText != "") + { + //if there was a match return the value + return mVarValue; + } + else if (noBrackets.find("=") != string::npos) + { + //return default value + return noBrackets.substr(noBrackets.find("=") + 1); + } + } + else if (mPreviousWord) + { + return mPreviousWord->getString(name); + } + + return ""; +} + +unsigned int Expression::getUint(string name) +{ + string stringVal = getString(name); + + unsigned int value = 0; + sscanf(stringVal.c_str(), "%u", &value); + + if (!value) + { + sscanf(stringVal.c_str(), "0x%x", &value); + } + return value; +} + +void Expression::getUintArray(string name, unsigned int** array, unsigned int* count) +{ + stringstream ss; + ss << getString(name); + + unsigned int buffer[256]; // more than enough for all cases + *count = 0; + + string stringVal; + while (getline(ss, stringVal, ',')) + { + sscanf(stringVal.c_str(), "%u", &buffer[*count]); + + if (!buffer[*count]) + { + sscanf(stringVal.c_str(), "0x%x", &buffer[*count]); + } + ++(*count); + } + + *array = new unsigned int[*count]; + memcpy(*array, buffer, sizeof(unsigned int) * (*count)); +} + +int Expression::getInt(string name) +{ + string stringVal = getString(name); + + int value = 0; + sscanf(stringVal.c_str(), "%d", &value); + + if (!value) + { + sscanf(stringVal.c_str(), "0x%x", (unsigned int*) &value); + } + return value; +} + +double Expression::getDouble(string name) +{ + string stringVal = getString(name); + + double value = 0; + sscanf(stringVal.c_str(), "%lf", &value); + return value; +} + +bool Expression::getBool(string name) +{ + string stringVal = getString(name); + int value = 0; + return sscanf(stringVal.c_str(), "%d", &value) && value; +} + +bool Expression::contains(string name) +{ + return mMatchText == name || (mPreviousWord && mPreviousWord->contains(name)); +} + +string Expression::getName() +{ + return mName; +} + +bool ExpressionCompare(Expression* a, Expression* b) +{ + return a->getName() < b->getName(); +} + +void Expression::addNextExpression(Expression* word) +{ + mNextWords.push_back(word); + mNextWords.sort(ExpressionCompare); +} + +ExpressionList Expression::getClosure(bool bypass) +{ + ExpressionList closure; + + if (bypass) + { + //if expression is end of the optional expression + bool bypassChildren = mName[mName.length() - 1] != ']'; + //get closure of children + ExpressionList::const_iterator iter = mNextWords.begin(); + ExpressionList::const_iterator end = mNextWords.end(); + for (; iter != end; ++iter) + { + ExpressionList childClosure = (*iter)->getClosure(bypassChildren); + closure.splice(closure.end(), childClosure); + } + } + else + { + closure.push_back(this); + //if start of optional expression + if (mName[0] == '[') + { + //get closure of elements after the end of the expression + ExpressionList restClosure = getClosure(true); + closure.splice(closure.end(), restClosure); + } + } + + return closure; +} + +ExpressionList Expression::getNextExpressionClosure(string text) +{ + ExpressionList nextClosure; + + ExpressionList::const_iterator iter = mNextWords.begin(); + ExpressionList::const_iterator end = mNextWords.end(); + for (; iter != end; ++iter) + { + Expression* childExpr = *iter; + ExpressionList childClosure = childExpr->getClosure(false); + + ExpressionList::const_iterator iter = childClosure.begin(); + ExpressionList::const_iterator end = childClosure.end(); + for (; iter != end; ++iter) + { + Expression* expr = *iter; + + if (expr->isVar()) + { + nextClosure.push_back(expr); + + expr->setVarValue(text); + + string exprName = expr->mName; + + //remove brakcets + exprName = exprName[0] == '[' ? exprName.substr(1, exprName.size() - 1) : exprName; + exprName = exprName[exprName.size() - 1] == ']' ? exprName.substr(0, exprName.size() - 1) : exprName; + + exprName = exprName[0] == '<' ? exprName.substr(1, exprName.size()-1) : exprName; + exprName = exprName[exprName.size() - 1] == '>' ? exprName.substr(0, exprName.size() - 1) : exprName; + + //remove default value (if needed) + exprName = exprName.substr(0, exprName.find("=")); + + expr->mMatchText = exprName; + } + else + { + //remove brackets if needed + string exprName = expr->mName; + exprName = exprName[0] == '[' ? exprName.substr(1, exprName.size() - 1) : exprName; + exprName = exprName[exprName.size() - 1] == ']' ? exprName.substr(0, exprName.size() - 1) : exprName; + + //check all alternatives (in case there are alternatives) + while (exprName.length() > 0) + { + //get next part + string temp = exprName.substr(0, exprName.find("|", 1)); + exprName = exprName.substr(temp.length()); + + //it there is a '|' at beginning remove it + temp = temp[0] == '|' ? temp.substr(1) : temp; + if (temp == text) + { + //add to result ! + nextClosure.push_back(expr); + expr->mMatchText = text; + break; //from inner loop ! + } + } + } + } + } + + return nextClosure; +} + +ExpressionList Expression::getNextExpressions() +{ + return this->mNextWords; +} + +Expression* Expression::getNextExpression(string text) +{ + Expression* varMatch = NULL; + Expression* nameMatch = NULL; + + ExpressionList::const_iterator iter = mNextWords.begin(); + ExpressionList::const_iterator end = mNextWords.end(); + for (; iter != end; ++iter) + { + Expression* expr = *iter; + string exprName = expr->getName(); + + if (exprName == text) + { + nameMatch = expr; + } + + if (expr->isVar()) + { + varMatch = expr; + varMatch->setVarValue(text); + } + } + + return nameMatch ? nameMatch : (varMatch ? varMatch : NULL); +} + +ExpressionList Expression::getClosureExecutables(bool canBypass) +{ + ExpressionList candidateExecutables; + + if (canBypass) + { + string expName = this->mName; + if (mName[mName.length() - 1] == ']') + { + //as if this child was the "last consumed" expression by the user string input ! + ExpressionList childExecutables = getClosureExecutables(false); + candidateExecutables.splice(candidateExecutables.end(), childExecutables); + } + else + { + ExpressionList::const_iterator iter = mNextWords.begin(); + ExpressionList::const_iterator end = mNextWords.end(); + for (; iter != end; ++iter) + { + string childName = (*iter)->mName; + + ExpressionList childClosure = (*iter)->getClosureExecutables(true); + candidateExecutables.splice(candidateExecutables.end(), childClosure); + } + } + } + else + { + //add myself to candidate executables + candidateExecutables.push_back(this); + + //get candidate executables from children + ExpressionList::const_iterator iter = mNextWords.begin(); + ExpressionList::const_iterator end = mNextWords.end(); + for (; iter != end; ++iter) + { + //if child is start of optional expression: get executable closure from the ends of this child + string childName = (*iter)->mName; + if (childName[0] == '[') + { + ExpressionList childClosure = (*iter)->getClosureExecutables(true); + candidateExecutables.splice(candidateExecutables.end(), childClosure); + } + } + } + + //return only the executable expressions + ExpressionList executables; + + ExpressionList::const_iterator iter = candidateExecutables.begin(); + ExpressionList::const_iterator end = candidateExecutables.end(); + for (; iter != end; ++iter) + { + Expression* expr = *iter; + + //check if it already exists in the list + bool duplicate = std::find(executables.begin(), executables.end(), expr) != executables.end(); + + if ((! duplicate) && expr->isExecutable()) + { + executables.push_back(expr); + } + } + + return executables; +} + +void Expression::printTree(int level) +{ + for (int i = 0; i < level; ++i) + { + cout << ((i + 1 != level) ? "| " : "|--"); + } + + stringstream name; + name << mName; + + if (isExecutable()) + { + name << "*"; + } + + cout << name.str() << endl; + + ExpressionList::const_iterator iter = mNextWords.begin(); + ExpressionList::const_iterator end = mNextWords.end(); + for (; iter != end; ++iter) + { + (*iter)->printTree(level + 1); + } +} + +void Expression::printList(string list) +{ + if (mName != "[root]") + { + list += mName; + list += " "; + if (isExecutable()) + { + cout << list << "\n"; + } + } + + ExpressionList::const_iterator iter = mNextWords.begin(); + ExpressionList::const_iterator end = mNextWords.end(); + for (; iter != end; ++iter) + { + (*iter)->printList(list); + } +} + +bool Expression::isExecutable() +{ + return mFuncPtr; +} + +void Expression::execute() +{ + (*mFuncPtr)(this); +} + +void Expression::setFunc(callback funcPtr) +{ + mFuncPtr = funcPtr; +} + +Expression* Expression::getPreviousExpression() +{ + return mPreviousWord; +} diff --git a/ivi-layermanagement-examples/LayerManagerControl/src/ExpressionInterpreter.cpp b/ivi-layermanagement-examples/LayerManagerControl/src/ExpressionInterpreter.cpp new file mode 100644 index 0000000..85efb69 --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/src/ExpressionInterpreter.cpp @@ -0,0 +1,202 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ +#include "ExpressionInterpreter.h" +#include "Expression.h" +#include "ilm_client.h" +#include +#include +#include // transform +#include // tolower + +#include + +Expression* ExpressionInterpreter::mpRoot = NULL; + +ExpressionInterpreter::ExpressionInterpreter() +: mErrorText("No error.") +{ +} + +bool ExpressionInterpreter::addExpression(callback funcPtr, string command) +{ + bool result = false; + + string text; + stringstream ss; + ss << command; + + if (!mpRoot) + { + mpRoot = new Expression("[root]", NULL); + } + + Expression* currentWord = mpRoot; + + while (!ss.eof()) + { + ss >> text; + transform(text.begin(), text.end(), text.begin(), ::tolower); + + Expression* nextWord = currentWord->getNextExpression(text); + + if (!nextWord) + { + nextWord = new Expression(text, currentWord); + currentWord->addNextExpression(nextWord); + } + + currentWord = nextWord; + } + + currentWord->setFunc(funcPtr); + + return result; +} + +CommandResult ExpressionInterpreter::interpretCommand(string userInput) +{ + CommandResult result = CommandSuccess; + string text; + stringstream ss; + ss << userInput; + + ExpressionList currentState; + currentState.push_back(mpRoot); + ExpressionList nextState; + + while (result == CommandSuccess && !ss.eof()) + { + ss >> text; + transform(text.begin(), text.end(), text.begin(), ::tolower); + + ExpressionList::const_iterator iter = currentState.begin(); + ExpressionList::const_iterator end = currentState.end(); + for (; iter != end; ++iter) + { + Expression* expr = *iter; + ExpressionList exprNextList = expr->getNextExpressionClosure(text); + nextState.splice(nextState.end(), exprNextList); + } + + if (nextState.size() > 0) + { + currentState = nextState; + nextState.clear(); + } + else + { + mErrorText = "'" + text + "' not recognized."; + result = CommandInvalid; + } + } + + //remove impossible expressions in the final state before checking for ambiguity + nextState.clear(); + ExpressionList::const_iterator iter = currentState.begin(); + ExpressionList::const_iterator end = currentState.end(); + for (; iter != end; ++iter) + { + Expression* expr = *iter; + if (expr->isExecutable()) + { + nextState.push_back(expr); + } + else + { + ExpressionList children = expr->getNextExpressions(); + + bool flag = false; + + ExpressionList::const_iterator iter = children.begin(); + ExpressionList::const_iterator end = children.end(); + for (; iter != end; ++iter) + { + if ((*iter)->getName()[0] == '[') + { + flag = true; + } + } + + if (flag || children.size() == 0) + { + nextState.push_back(expr); + } + } + } + + currentState = nextState; + + if (currentState.size() != 1) + { + mErrorText = "'" + text + "' ambiguous or incomplete."; + result = CommandInvalid; + } + + //run command if executable and non-ambiguous + if (result == CommandSuccess) + { + Expression* expr = *(currentState.begin()); + + ExpressionList executables = expr->getClosureExecutables(false); + if (executables.size() == 1) + { + ilmErrorTypes initResult = ilm_init(); + if (ILM_SUCCESS != initResult) + { + mErrorText = ILM_ERROR_STRING(initResult); + result = CommandExecutionFailed; + } + else + { + Expression* exec = executables.front(); + exec->execute(); + ilm_destroy(); + } + } + else if (executables.size() == 0) + { + mErrorText = "command is incomplete."; + result = CommandIncomplete; + } + else + { + mErrorText = "command is ambiguous."; + result = CommandIncomplete; + } + } + + return result; +} + +void ExpressionInterpreter::printExpressionTree() +{ + mpRoot->printTree(); +} + +void ExpressionInterpreter::printExpressionList() +{ + mpRoot->printList(); +} + +string ExpressionInterpreter::getLastError() +{ + string tmp = mErrorText; + mErrorText = "no error."; + return tmp; +} diff --git a/ivi-layermanagement-examples/LayerManagerControl/src/analyze.cpp b/ivi-layermanagement-examples/LayerManagerControl/src/analyze.cpp new file mode 100644 index 0000000..4b695bc --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/src/analyze.cpp @@ -0,0 +1,675 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ + +#include "ilm_client.h" +#include "LMControl.h" + +#include +using std::find; + +#include + +#include +using std::iterator; + +#include +using std::cout; +using std::cin; +using std::endl; + +#include +using std::dec; +using std::hex; +using std::left; +using std::right; +using std::setw; + +#include +using std::max; +using std::min; + +#include +using std::string; + +#include +using std::vector; + +namespace +{ +void analyzePrintHelper(string tag, string flag, string description) +{ + cout << left << setw(25) << tag << " | " << setw(7) << flag << " | " << description << endl; +} + +void analyzeVisibilityAndOpacity(t_ilm_surface targetSurfaceId, t_scene_data& scene) +{ + t_ilm_layer targetSurfaceLayer = scene.surfaceLayer[targetSurfaceId]; + ilmSurfaceProperties& targetSurfaceProperties = scene.surfaceProperties[targetSurfaceId]; + ilmLayerProperties& targetLayerProperties = scene.layerProperties[targetSurfaceLayer]; + string tag; + string flag; + char description[300] = ""; + + //check visibility + tag = "Surface Visibility"; + if (targetSurfaceProperties.visibility == ILM_FALSE) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i visibility set to false", targetSurfaceId); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + tag = "Layer Visibility"; + if (targetLayerProperties.visibility == ILM_FALSE) + { + flag = "PROBLEM"; + sprintf(description, "Layer %i visibility set to false.", targetSurfaceLayer); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + //check opacity + tag = "Surface Opacity"; + if (targetSurfaceProperties.opacity <= 0.2) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i opacity set to %f, it is (almost) invisible", targetSurfaceId, targetSurfaceProperties.opacity); + } + else if (targetSurfaceProperties.opacity < 1.0) + { + flag = "WARNING"; + sprintf(description, "Surface %i opacity set to %f, it might not be easy to see", targetSurfaceId, targetSurfaceProperties.opacity); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + tag = "Layer Opacity"; + if (targetLayerProperties.opacity <= 0.2) + { + flag = "PROBLEM"; + sprintf(description, "Layer %i opacity set to %f, it is (almost) invisible", targetSurfaceLayer, targetLayerProperties.opacity); + } + else if (targetLayerProperties.opacity < 1.0) + { + flag = "WARNING"; + sprintf(description, "Layer %i opacity set to %f, it might not be easy to see", targetSurfaceLayer, targetLayerProperties.opacity); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); +} + +void analyzeSurfaceDimensions(t_ilm_surface targetSurfaceId, t_scene_data& scene) +{ + ilmSurfaceProperties& targetSurfaceProperties = scene.surfaceProperties[targetSurfaceId]; + string tag; + string flag; + char description[300] = ""; + + t_ilm_uint minDimension = 32; + + tag = "Surface dest width"; + if (targetSurfaceProperties.destWidth <= minDimension) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i has [destWidth=%i]", targetSurfaceId, targetSurfaceProperties.destWidth); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + tag = "Surface source width"; + if (targetSurfaceProperties.sourceWidth <= minDimension) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i has [sourceWidth=%i]", targetSurfaceId, targetSurfaceProperties.sourceWidth); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + tag = "Surface original width"; + if (targetSurfaceProperties.origSourceWidth <= minDimension) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i has [origSourceWidth=%i]", targetSurfaceId, targetSurfaceProperties.origSourceWidth); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + tag = "Surface dest height"; + if (targetSurfaceProperties.destHeight <= minDimension) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i has [destHeight=%i]", targetSurfaceId, targetSurfaceProperties.destHeight); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + tag = "Surface source height"; + if (targetSurfaceProperties.sourceHeight <= minDimension) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i has [sourceHeight=%i]", targetSurfaceId, targetSurfaceProperties.sourceHeight); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + tag = "Surface original height"; + if (targetSurfaceProperties.origSourceHeight <= minDimension) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i has [origSourceHeight=%i]", targetSurfaceId, targetSurfaceProperties.origSourceHeight); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); +} + +void analyzeLayerDimensions(t_ilm_surface targetSurfaceId, t_scene_data& scene) +{ + t_ilm_layer targetSurfaceLayer = scene.surfaceLayer[targetSurfaceId]; + ilmLayerProperties& targetLayerProperties = scene.layerProperties[targetSurfaceLayer]; + t_ilm_uint minDimension = 32; + + string tag; + string flag; + char description[300] = ""; + tag = "Layer dest width"; + if (targetLayerProperties.destWidth <= minDimension) + { + flag = "PROBLEM"; + sprintf(description, "Layer %i has [destWidth=%i]", targetSurfaceLayer, targetLayerProperties.destWidth); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + tag = "Layer source width"; + if (targetLayerProperties.sourceWidth <= minDimension) + { + flag = "PROBLEM"; + sprintf(description, "Layer %i has [sourceWidth=%i]", targetSurfaceLayer, targetLayerProperties.sourceWidth); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + tag = "Layer original width"; + if (targetLayerProperties.origSourceWidth <= minDimension) + { + flag = "PROBLEM"; + sprintf(description, "Layer %i has [origSourceWidth=%i]", targetSurfaceLayer, targetLayerProperties.origSourceWidth); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + tag = "Layer dest height"; + if (targetLayerProperties.destHeight <= minDimension) + { + flag = "PROBLEM"; + sprintf(description, "Layer %i has [destHeight=%i]", targetSurfaceLayer, targetLayerProperties.destHeight); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + tag = "Layer source height"; + if (targetLayerProperties.sourceHeight <= minDimension) + { + flag = "PROBLEM"; + sprintf(description, "Layer %i has [sourceHeight=%i]", targetSurfaceLayer, targetLayerProperties.sourceHeight); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + tag = "Layer original source"; + if (targetLayerProperties.origSourceHeight <= minDimension) + { + flag = "PROBLEM"; + sprintf(description, "Layer %i has [origSourceHeight=%i]", targetSurfaceLayer, targetLayerProperties.origSourceHeight); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); +} + +void analyzeDimensions(t_ilm_surface targetSurfaceId, t_scene_data& scene) +{ + analyzeSurfaceDimensions(targetSurfaceId, scene); + analyzeLayerDimensions(targetSurfaceId, scene); +} + +void analyzeSurfaceCheckInsideLayer(t_ilm_surface targetSurfaceId, t_scene_data& scene) +{ + t_ilm_layer targetSurfaceLayer = scene.surfaceLayer[targetSurfaceId]; + tuple4 targetSurfaceCoordinates = getSurfaceScreenCoordinates(&scene, targetSurfaceId); + ilmLayerProperties& targetLayerProperties = scene.layerProperties[targetSurfaceLayer]; + string tag; + string flag; + char description[300] = ""; + + tuple4 layerCoordinates(targetLayerProperties.destX, + targetLayerProperties.destY, + targetLayerProperties.destX + targetLayerProperties.destWidth, + targetLayerProperties.destY + targetLayerProperties.destHeight); + + tag = "Surface inside Layer"; + if (!inside(targetSurfaceCoordinates, layerCoordinates)) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i is not viewed completely insde the destination region of layer %i", + targetSurfaceId, targetSurfaceLayer); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); +} + +void analyzeOcclusion(t_ilm_surface targetSurfaceId, + map& surfaceLayers, + map& surfaceProperties, + map& layerProperties, + vector& allSurfaces, tuple4 targetSurfaceCoordinates) +{ + string tag; + string flag; + char description[300] = ""; + + vector occludingSurfaces; + + vector::iterator it = find(allSurfaces.begin(), allSurfaces.end(), targetSurfaceId); + + t_ilm_bool occluded = ILM_FALSE; + tag = "Occlusion"; + ++it; + for (; it != allSurfaces.end(); ++it) + { + t_ilm_surface surfaceId = *it; + t_ilm_layer surfaceLayer = surfaceLayers[surfaceId]; + + //if surface or layer invisible: neglect + if (layerProperties[surfaceLayer].visibility == ILM_FALSE || surfaceProperties[surfaceId].visibility == ILM_FALSE) + continue; + + //if multiplication of their opacity is zero: neglect + if (layerProperties[surfaceLayer].opacity * surfaceProperties[surfaceId].opacity == 0) + continue; + + //coordinates of the surface on screen + t_ilm_int horizontalScale = layerProperties[surfaceLayer].destWidth / layerProperties[surfaceLayer].sourceWidth; + t_ilm_int surfaceX1 = layerProperties[surfaceLayer].destX + horizontalScale + * (surfaceProperties[surfaceId].destX - layerProperties[surfaceLayer].sourceX); + t_ilm_int surfaceX2 = surfaceX1 + horizontalScale * surfaceProperties[surfaceId].destWidth; + + t_ilm_int verticalScale = layerProperties[surfaceLayer].destHeight / layerProperties[surfaceLayer].sourceHeight; + t_ilm_int surfaceY1 = layerProperties[surfaceLayer].destY + verticalScale + * (surfaceProperties[surfaceId].destY - layerProperties[surfaceLayer].sourceY); + t_ilm_int surfaceY2 = surfaceY1 + verticalScale * surfaceProperties[surfaceId].destHeight; + + tuple4 surfaceCoordinates(surfaceX1, surfaceY1, surfaceX2, surfaceY2); + + //if the surface is completely occluded + if (inside(targetSurfaceCoordinates, surfaceCoordinates)) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i is completely occluded by surface %i", targetSurfaceId, surfaceId); + analyzePrintHelper(tag, flag, description); + + occluded = ILM_TRUE; + } + //if the surface is partially occluded + else if (intersect(targetSurfaceCoordinates, surfaceCoordinates)) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i is partially occluded by surface %i", targetSurfaceId, surfaceId); + analyzePrintHelper(tag, flag, description); + occluded = ILM_TRUE; + } + } + + if (!occluded) + { + flag = "OK"; + sprintf(description, "%s", ""); + analyzePrintHelper(tag, flag, description); + } +} + +void analyzeOcclusion(t_ilm_surface targetSurfaceId, t_scene_data& scene) +{ + string tag; + string flag; + char description[300] = ""; + + vector renderedSurfaces = getSceneRenderOrder(&scene); + vector occludingSurfaces; + + vector::iterator it = find(renderedSurfaces.begin(), renderedSurfaces.end(), targetSurfaceId); + + tuple4 targetSurfaceCoordinates = getSurfaceScreenCoordinates(&scene, targetSurfaceId); + + t_ilm_bool occluded = ILM_FALSE; + tag = "Occlusion"; + ++it; + for (; it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surfaceId = *it; + t_ilm_layer surfaceLayer = scene.surfaceLayer[surfaceId]; + + //if surface or layer invisible: neglect + if (scene.layerProperties[surfaceLayer].visibility == ILM_FALSE || scene.surfaceProperties[surfaceId].visibility == ILM_FALSE) + continue; + + //if multiplication of their opacity is zero: neglect + if (scene.layerProperties[surfaceLayer].opacity * scene.surfaceProperties[surfaceId].opacity == 0) + continue; + + //coordinates of the surface on screen + tuple4 surfaceCoordinates = getSurfaceScreenCoordinates(&scene, surfaceId); + + //if the surface is completely occluded + if (inside(targetSurfaceCoordinates, surfaceCoordinates)) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i is completely occluded by surface %i", targetSurfaceId, surfaceId); + analyzePrintHelper(tag, flag, description); + + occluded = ILM_TRUE; + } + //if the surface is partially occluded + else if (intersect(targetSurfaceCoordinates, surfaceCoordinates)) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i is partially occluded by surface %i", targetSurfaceId, surfaceId); + analyzePrintHelper(tag, flag, description); + occluded = ILM_TRUE; + } + } + + if (!occluded) + { + flag = "OK"; + sprintf(description, "%s", ""); + analyzePrintHelper(tag, flag, description); + } +} + +t_ilm_bool analyzeCheckSurfaceExists(t_ilm_surface targetSurfaceId, t_scene_data& scene) +{ + t_ilm_bool exists = ILM_FALSE; + + string tag; + string flag; + char description[300] = ""; + + tag = "Surface existance"; + //check if surface exists + if (find(scene.surfaces.begin(), scene.surfaces.end(), targetSurfaceId) + == scene.surfaces.end()) + { + flag = "PROBLEM"; + sprintf(description, "There is no surface with ID %i", targetSurfaceId); + } + else + { + exists = ILM_TRUE; + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + return exists; +} + +t_ilm_bool analyzeCheckRendered(t_ilm_surface targetSurfaceId, t_scene_data& scene) +{ + t_ilm_bool onLayer = ILM_FALSE; + t_ilm_bool layerOnScreen = ILM_FALSE; + //is surface on layer? + map::iterator surfaceLayerIt = scene.surfaceLayer.find(targetSurfaceId); + + if (surfaceLayerIt != scene.surfaceLayer.end()) + { + onLayer = ILM_TRUE; + t_ilm_layer layer = (*surfaceLayerIt).second; + + //is layer on screen? + layerOnScreen = scene.layerScreen.find(layer) != scene.layerScreen.end(); + } + + //output + string tag; + string flag; + char description[300] = ""; + + tag = "Surface on layer"; + if (!onLayer) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i is not on any layer", targetSurfaceId); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + if (onLayer) + { + tag = "Layer on screen"; + if (!layerOnScreen) + { + flag = "PROBLEM"; + sprintf(description, "Layer %i is not on any screen", scene.surfaceLayer[targetSurfaceId]); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + } + + return onLayer && layerOnScreen; +} + +t_ilm_bool analyzeSharedNative(t_ilm_surface targetSurfaceId, t_scene_data& scene) +{ + string tag; + string flag; + char description[300] = ""; + + tag = "Shared native"; + + t_ilm_bool shared = ILM_FALSE; + + //native of the target surface + t_ilm_uint targetNative = scene.surfaceProperties[targetSurfaceId].nativeSurface; + + //iterate all surface properties + for (map::iterator it = scene.surfaceProperties.begin(); + it != scene.surfaceProperties.end(); ++it) + { + t_ilm_surface surface = (*it).first; + ilmSurfaceProperties& properties = (*it).second; + //if there is a surface that has the same surface as the target surface + if (surface != targetSurfaceId && properties.nativeSurface == targetNative) + { + shared = ILM_TRUE; + + flag = "WARNING"; + sprintf(description, "Surface %i shares native that has ID %i with surface %i", + targetSurfaceId, targetNative, surface); + analyzePrintHelper(tag, flag, description); + } + } + + if (!shared) + { + flag = "OK"; + sprintf(description, "%s", ""); + analyzePrintHelper(tag, flag, description); + } + + return !shared; +} + +t_ilm_bool analyzeUpdateCounter(t_ilm_surface targetSurfaceId, t_scene_data& scene) +{ + ilmSurfaceProperties& targetSurfaceProperties = scene.surfaceProperties[targetSurfaceId]; + + t_ilm_bool problem = targetSurfaceProperties.updateCounter == 0; + string tag; + string flag; + char description[300] = ""; + + tag = "Update Counter"; + //check if surface counter was updated since its creation + if (problem) + { + flag = "PROBLEM"; + sprintf(description, "Surface %i update counter is %i, no content was added to the surface since its creation", + targetSurfaceId, targetSurfaceProperties.updateCounter); + } + else + { + flag = "OK"; + sprintf(description, "%s", ""); + } + + analyzePrintHelper(tag, flag, description); + + return !problem; +} +} //end of anonymous namespace + + +t_ilm_bool analyzeSurface(t_ilm_surface targetSurfaceId) +{ + t_scene_data scene; + captureSceneData(&scene); + + if (!analyzeCheckSurfaceExists(targetSurfaceId, scene)) + return ILM_TRUE; + + if (!analyzeCheckRendered(targetSurfaceId, scene)) + return ILM_TRUE; + + //check no visibility or low opacity + analyzeVisibilityAndOpacity(targetSurfaceId, scene); + + //check small dimensions + analyzeDimensions(targetSurfaceId, scene); + + //check if surface is completely inside the destination region of the layer + analyzeSurfaceCheckInsideLayer(targetSurfaceId, scene); + + //get occluding visible surfaces + analyzeOcclusion(targetSurfaceId, scene); + + //check if the surface has been updated (if it has any content) + analyzeUpdateCounter(targetSurfaceId, scene); + + //check if the surface shares the native with another surface + analyzeSharedNative(targetSurfaceId, scene); + + return ILM_TRUE; +} diff --git a/ivi-layermanagement-examples/LayerManagerControl/src/commands.cpp b/ivi-layermanagement-examples/LayerManagerControl/src/commands.cpp new file mode 100644 index 0000000..f926400 --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/src/commands.cpp @@ -0,0 +1,838 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ +#include "ilm_client.h" +#include "ilm_control.h" +#include "LMControl.h" +#include "Expression.h" +#include "ExpressionInterpreter.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include // signal +#include // alarm + +using namespace std; + + +#define COMMAND(text) COMMAND2(__COUNTER__,text) + +#define COMMAND2(x,y) COMMAND3(x,y) + +#define COMMAND3(funcNumber, text) \ + void func_ ## funcNumber(Expression* input); \ + static const bool reg_ ## funcNumber = \ + ExpressionInterpreter::addExpression(func_ ## funcNumber, text); \ + void func_ ## funcNumber(Expression* input) + + + +//============================================================================= +COMMAND("help") +//============================================================================= +{ + (void)input; + cout << "help: supported commands:\n\n"; + ExpressionInterpreter::printExpressionList(); + cout << "\n"; +} + +//============================================================================= +COMMAND("tree") +//============================================================================= +{ + (void)input; + cout << "help: supported commands:\n\n"; + ExpressionInterpreter::printExpressionTree(); + cout << "\n"; +} + +//============================================================================= +COMMAND("get scene|screens|layers|surfaces") +//============================================================================= +{ + if (input->contains("scene")) + { + printScene(); + } + else if (input->contains("screens")) + { + (void)input; + unsigned int count = 0; + unsigned int* array = NULL; + + ilmErrorTypes callResult = ilm_getScreenIDs(&count, &array); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get screen IDs\n"; + return; + } + + printArray("Screen", array, count); + } + else if (input->contains("layers")) + { + (void)input; + int count = 0; + unsigned int* array = NULL; + + ilmErrorTypes callResult = ilm_getLayerIDs(&count, &array); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get layer IDs\n"; + return; + } + + printArray("Layer", array, count); + } + else if (input->contains("surfaces")) + { + (void)input; + int count = 0; + unsigned int* array = NULL; + + ilmErrorTypes callResult = ilm_getSurfaceIDs(&count, &array); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get surface IDs\n"; + return; + } + + printArray("Surface", array, count); + } +} + +//============================================================================= +COMMAND("get screen|layer|surface ") +//============================================================================= +{ + if (input->contains("screen")) + { + printScreenProperties(input->getUint("id")); + } + else if (input->contains("layer")) + { + printLayerProperties(input->getUint("id")); + } + else if (input->contains("surface")) + { + printSurfaceProperties(input->getUint("id")); + } +} + +//============================================================================= +COMMAND("dump screen|layer|surface to ") +//============================================================================= +{ + if (input->contains("screen")) + { + ilmErrorTypes callResult = ilm_takeScreenshot(input->getUint("id"), + input->getString("file").c_str()); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to take screenshot of screen with ID " << input->getUint("id") << "\n"; + return; + } + } + else if (input->contains("layer")) + { + ilmErrorTypes callResult = ilm_takeLayerScreenshot(input->getString("file").c_str(), + input->getUint("id")); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to take screenshot of layer with ID " << input->getUint("id") << "\n"; + return; + } + } + else if (input->contains("surface")) + { + ilmErrorTypes callResult = ilm_takeSurfaceScreenshot(input->getString("file").c_str(), + input->getUint("id")); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to take screenshot of surface with ID " << input->getUint("id") << "\n"; + return; + } + } +} + +//============================================================================= +COMMAND("set layer|surface source region ") +//============================================================================= +{ + t_ilm_uint id = input->getUint("id"); + t_ilm_uint x = input->getUint("x"); + t_ilm_uint y = input->getUint("y"); + t_ilm_uint w = input->getUint("w"); + t_ilm_uint h = input->getUint("h"); + + if (input->contains("layer")) + { + ilmErrorTypes callResult = ilm_layerSetSourceRectangle(id, x, y, w, h); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set source rectangle (" << x << "," << y << ", " << w << ", " << h << ") for layer with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } + else if (input->contains("surface")) + { + ilmErrorTypes callResult = ilm_surfaceSetSourceRectangle(id, x, y, w, h); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set source rectangle (" << x << ", " << y << ", " << w << ", " << h << ") for surface with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } +} + +//============================================================================= +COMMAND("set layer|surface destination region ") +//============================================================================= +{ + t_ilm_uint id = input->getUint("id"); + t_ilm_uint x = input->getUint("x"); + t_ilm_uint y = input->getUint("y"); + t_ilm_uint w = input->getUint("w"); + t_ilm_uint h = input->getUint("h"); + + if (input->contains("layer")) + { + ilmErrorTypes callResult = ilm_layerSetDestinationRectangle(id, x, y, w, h); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << x << ", " << y << ", " << w << ", " << h << ") for layer with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } + else if (input->contains("surface")) + { + ilmErrorTypes callResult = ilm_surfaceSetDestinationRectangle(id, x, y, w, h); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << x << ", " << y << ", " << w << ", " << h << ") for surface with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } +} + +//============================================================================= +COMMAND("set layer|surface opacity ") +//============================================================================= +{ + t_ilm_uint id = input->getUint("id"); + double opacity = input->getDouble("opacity"); + + if (input->contains("layer")) + { + ilmErrorTypes callResult = ilm_layerSetOpacity(id, opacity); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set opacity " << opacity << " for layer with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } + else if (input->contains("surface")) + { + ilmErrorTypes callResult = ilm_surfaceSetOpacity(id, opacity); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set opacity " << opacity << " for surface with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } +} + +//============================================================================= +COMMAND("set layer|surface visibility ") +//============================================================================= +{ + t_ilm_uint id = input->getUint("id"); + t_ilm_bool visibility = input->getBool("visibility"); + + if (input->contains("layer")) + { + ilmErrorTypes callResult = ilm_layerSetVisibility(id, visibility); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set visibility " << visibility << " for layer with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } + else if (input->contains("surface")) + { + ilmErrorTypes callResult = ilm_surfaceSetVisibility(id, visibility); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set visibility " << visibility << " for surface with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } +} + +//============================================================================= +COMMAND("set layer|surface orientation ") +//============================================================================= +{ + t_ilm_uint id = input->getUint("id"); + ilmOrientation orientation = (ilmOrientation)input->getInt("orientation"); + + if (input->contains("layer")) + { + ilmErrorTypes callResult = ilm_layerSetOrientation(id, orientation); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set orientation " << orientation << " for layer with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } + else if (input->contains("surface")) + { + ilmErrorTypes callResult = ilm_surfaceSetOrientation(id, orientation); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set orientation " << orientation << " for surface with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } +} + +//============================================================================= +COMMAND("set screen|layer render order []") +//============================================================================= +{ + if (input->contains("screen")) + { + if (input->contains("idarray")) + { + unsigned int count = 0; + unsigned int* array = NULL; + unsigned int screenid = input->getUint("id"); + input->getUintArray("idarray", &array, &count); + + ilmErrorTypes callResult = ilm_displaySetRenderOrder(screenid, array, count); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set render order for screen with ID " << screenid << "\n"; + return; + } + + ilm_commitChanges(); + } + else + { + unsigned int screenid = input->getUint("id"); + + ilmErrorTypes callResult = ilm_displaySetRenderOrder(screenid, NULL, 0); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set render order for screen with ID " << screenid << "\n"; + return; + } + + ilm_commitChanges(); + } + } + else if (input->contains("layer")) + { + if (input->contains("idarray")) + { + unsigned int count = 0; + unsigned int* array = NULL; + unsigned int layerid = input->getUint("id"); + input->getUintArray("idarray", &array, &count); + + ilmErrorTypes callResult = ilm_layerSetRenderOrder(layerid, array, count); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set render order for layer with ID " << layerid << "\n"; + return; + } + + ilm_commitChanges(); + } + else + { + unsigned int layerid = input->getUint("id"); + + ilmErrorTypes callResult = ilm_layerSetRenderOrder(layerid, NULL, 0); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set render order for layer with ID " << layerid << "\n"; + return; + } + + ilm_commitChanges(); + } + } +} + +//============================================================================= +COMMAND("set layer|surface width ") +//============================================================================= +{ + if (input->contains("layer")) + { + unsigned int dimension[2]; + unsigned int layerid = input->getUint("id"); + + ilmErrorTypes callResult = ilm_layerGetDimension(layerid, dimension); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get dimensions of layer with ID " << layerid << "\n"; + return; + } + + dimension[0] = input->getUint("width"); + + callResult = ilm_layerSetDimension(layerid, dimension); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set dimensions of layer with ID " << layerid << "\n"; + return; + } + + ilm_commitChanges(); + } + else if (input->contains("surface")) + { + unsigned int dimension[2]; + unsigned int surfaceid = input->getUint("id"); + + ilmErrorTypes callResult = ilm_surfaceGetDimension(surfaceid, dimension); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get dimensions of surface with ID " << surfaceid << "\n"; + return; + } + + dimension[0] = input->getUint("width"); + + callResult = ilm_surfaceSetDimension(surfaceid, dimension); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set dimensions of surface with ID " << surfaceid << "\n"; + return; + } + + ilm_commitChanges(); + } +} + +//============================================================================= +COMMAND("set layer|surface height ") +//============================================================================= +{ + if (input->contains("layer")) + { + unsigned int dimension[2]; + unsigned int layerid = input->getUint("id"); + + ilmErrorTypes callResult = ilm_layerGetDimension(layerid, dimension); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get dimensions of layer with ID " << layerid << "\n"; + return; + } + + dimension[1] = input->getUint("height"); + + callResult = ilm_layerSetDimension(layerid, dimension); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set dimensions of layer with ID " << layerid << "\n"; + return; + } + + ilm_commitChanges(); + } + else if (input->contains("surface")) + { + unsigned int dimension[2]; + unsigned int surfaceid = input->getUint("id"); + + ilmErrorTypes callResult = ilm_surfaceGetDimension(surfaceid, dimension); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get dimensions of surface with ID " << surfaceid << "\n"; + return; + } + + dimension[1] = input->getUint("height"); + + callResult = ilm_surfaceSetDimension(surfaceid, dimension); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set dimensions of surface with ID " << surfaceid << "\n"; + return; + } + + ilm_commitChanges(); + } +} + +//============================================================================= +COMMAND("set layer|surface position ") +//============================================================================= +{ + unsigned int id = input->getUint("id"); + unsigned int dimension[2]; + dimension[0] = input->getUint("x"); + dimension[1] = input->getUint("y"); + + if (input->contains("layer")) + { + ilmErrorTypes callResult = ilm_layerSetPosition(id, dimension); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set position of layer with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } + else if (input->contains("surface")) + { + ilmErrorTypes callResult = ilm_surfaceSetPosition(id, dimension); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set position of surface with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } +} + +//============================================================================= +COMMAND("create layer ") +//============================================================================= +{ + unsigned int layerid = input->getUint("layerid"); + unsigned int width = input->getUint("width"); + unsigned int height = input->getUint("height"); + + ilmErrorTypes callResult = ilm_layerCreateWithDimension(&layerid, width, height); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to create layer with ID " << layerid << "\n"; + return; + } +} + +//============================================================================= +COMMAND("create surface ") +//============================================================================= +{ + unsigned int surfaceid = input->getUint("surfaceid"); + unsigned int nativeHandle = input->getUint("nativehandle"); + unsigned int width = input->getUint("width"); + unsigned int height = input->getUint("height"); + e_ilmPixelFormat pixelformat = (e_ilmPixelFormat)input->getUint("pixelformat"); + + ilmErrorTypes callResult = ilm_surfaceCreate(nativeHandle, width, height, pixelformat, &surfaceid); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to create surface with ID " << surfaceid << "\n"; + return; + } +} + +//============================================================================= +COMMAND("destroy layer|surface ") +//============================================================================= +{ + if (input->contains("layer")) + { + unsigned int layerid = input->getUint("id"); + + ilmErrorTypes callResult = ilm_layerRemove(layerid); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to remove layer with ID " << layerid << "\n"; + return; + } + } + else if (input->contains("surface")) + { + unsigned int surfaceid = input->getUint("id"); + + ilmErrorTypes callResult = ilm_surfaceRemove(surfaceid); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to remove surface with ID " << surfaceid << "\n"; + return; + } + } + + ilm_commitChanges(); +} + +//============================================================================= +COMMAND("get communicator performance") +//============================================================================= +{ + (void) input; //suppress warning: unused parameter + getCommunicatorPerformance(); +} + +//============================================================================= +COMMAND("set surface keyboard focus") +//============================================================================= +{ + t_ilm_surface surface = input->getUint("surfaceid"); + + setSurfaceKeyboardFocus(surface); +} + +//============================================================================= +COMMAND("get keyboard focus") +//============================================================================= +{ + (void) input; //suppress warning: unused parameter + getKeyboardFocus(); +} + +//============================================================================= +COMMAND("set surface accept input events from devices ") +//============================================================================= +{ + t_ilm_surface surfaceId = input->getUint("surfaceid"); + t_ilm_bool acceptance = input->getBool("acceptance"); + string kbdPointerTouch = input->getString("kbd:pointer:touch"); + + setSurfaceAcceptsInput(surfaceId, kbdPointerTouch, acceptance); +} + +//============================================================================= +COMMAND("set layer|surface chromakey ") +//============================================================================= +{ + t_ilm_layer id = input->getUint("id"); + t_ilm_int color[3] = + { + input->getInt("red"), + input->getInt("green"), + input->getInt("blue") + }; + + if (input->contains("layer")) + { + ilmErrorTypes callResult = ilm_layerSetChromaKey(id, color); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set chroma key (" << color[0] << ", " << color[1] << ", " << color[2] << ") for layer with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } + else if (input->contains("surface")) + { + ilmErrorTypes callResult = ilm_surfaceSetChromaKey(id, color); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set chroma key (" << color[0] << ", " << color[1] << ", " << color[2] << ") for surface with ID " << id << "\n"; + return; + } + + ilm_commitChanges(); + } +} + +//============================================================================= +COMMAND("set surface chromakey disabled") +//============================================================================= +{ + t_ilm_surface surface = input->getUint("surfaceid"); + + ilmErrorTypes callResult = ilm_surfaceSetChromaKey(surface, NULL); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to disable chroma key for surface with ID " << surface << "\n"; + return; + } + + ilm_commitChanges(); +} + +//============================================================================= +COMMAND("test notification layer ") +//============================================================================= +{ + unsigned int layerid = input->getUint("layerid"); + + testNotificationLayer(layerid); +} + +//============================================================================= +COMMAND("watch layer|surface ") +//============================================================================= +{ + if (input->contains("layer")) + { + unsigned int* layerids = NULL; + unsigned int layeridCount; + input->getUintArray("idarray", &layerids, &layeridCount); + + watchLayer(layerids, layeridCount); + } + else if (input->contains("surface")) + { + unsigned int* surfaceids = NULL; + unsigned int surfaceidCount; + input->getUintArray("idarray", &surfaceids, &surfaceidCount); + + watchSurface(surfaceids, surfaceidCount); + } +} + +//============================================================================= +COMMAND("set optimization mode ") +//============================================================================= +{ + t_ilm_uint id = input->getUint("id"); + t_ilm_uint mode = input->getUint("mode"); + setOptimization(id, mode); +} + +//============================================================================= +COMMAND("get optimization ") +//============================================================================= +{ + t_ilm_uint id = input->getUint("id"); + getOptimization(id); +} + +//============================================================================= +COMMAND("analyze surface ") +//============================================================================= +{ + t_ilm_surface targetSurfaceId = (t_ilm_uint) input->getUint("surfaceid"); + analyzeSurface(targetSurfaceId); +} + +//============================================================================= +COMMAND("scatter [all]") +//============================================================================= +{ + if (input->contains("all")) + { + scatterAll(); + } + else + { + scatter(); + } +} + +//============================================================================= +COMMAND("demo []") +//============================================================================= +{ + t_ilm_uint mode = (t_ilm_uint) input->getUint("animation_mode"); + demo(mode); +} + +//============================================================================= +COMMAND("export scene to ") +//============================================================================= +{ + string filename = (string) input->getString("filename"); + exportSceneToFile(filename); +} + +//============================================================================= +COMMAND("export xtext to ") +//============================================================================= +{ + string filename = (string) input->getString("filename"); + string grammar = (string) input->getString("grammar"); + string url = (string) input->getString("url"); + exportXtext(filename, grammar, url); +} + +//============================================================================= +COMMAND("import scene from ") +//============================================================================= +{ + string filename = (string) input->getString("filename"); + importSceneFromFile(filename); +} diff --git a/ivi-layermanagement-examples/LayerManagerControl/src/common.cpp b/ivi-layermanagement-examples/LayerManagerControl/src/common.cpp new file mode 100644 index 0000000..8bfe376 --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/src/common.cpp @@ -0,0 +1,733 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ + +#include "ilm_client.h" +#include "ilm_control.h" +#include "LMControl.h" + +#include +using std::find; + +#include +using std::max; +using std::min; + +#include +using std::iterator; + +#include +using std::cout; +using std::cin; +using std::endl; + +#include +using std::vector; + +#include +#include +#include +#include + + +tuple4 getSurfaceScreenCoordinates(ilmSurfaceProperties targetSurfaceProperties, ilmLayerProperties targetLayerProperties) +{ + t_ilm_float horizontalScale = targetLayerProperties.sourceWidth ? + 1.0 * targetLayerProperties.destWidth / targetLayerProperties.sourceWidth : 0; + + t_ilm_float targetX1 = targetLayerProperties.destX + horizontalScale + * (targetSurfaceProperties.destX - targetLayerProperties.sourceX); + t_ilm_float targetX2 = targetX1 + horizontalScale * targetSurfaceProperties.destWidth; + + t_ilm_float verticalScale = targetLayerProperties.sourceHeight ? + 1.0 * targetLayerProperties.destHeight / targetLayerProperties.sourceHeight : 0; + + t_ilm_float targetY1 = targetLayerProperties.destY + verticalScale + * (targetSurfaceProperties.destY - targetLayerProperties.sourceY); + t_ilm_float targetY2 = targetY1 + verticalScale * targetSurfaceProperties.destHeight; + + tuple4 targetSurfaceCoordinates(static_cast(targetX1), + static_cast(targetY1), + max(0, static_cast(targetX2) - 1), + max(0, static_cast(targetY2) - 1)); + + return targetSurfaceCoordinates; +} + +tuple4 getSurfaceScreenCoordinates(t_scene_data* pScene, t_ilm_surface surface) +{ + tuple4 surfaceCoordinates; + + //if surface belongs to a layer make it appear exacrly as it would appear on its current layer + if (pScene->surfaceLayer.find(surface) != pScene->surfaceLayer.end()) + { + //set dimensions of the surface to map to the extra layer according to its current placement in the + t_ilm_layer layer = pScene->surfaceLayer[surface]; + ilmLayerProperties layerProperties = pScene->layerProperties[layer]; + ilmSurfaceProperties surfaceProperties = pScene->surfaceProperties[surface]; + + surfaceCoordinates = getSurfaceScreenCoordinates(surfaceProperties, layerProperties); + } + //if surface does not belong to a layer just assume it belongs to a layer that fills the screen + else + { + ilmSurfaceProperties surfaceProperties = pScene->surfaceProperties[surface]; + + surfaceCoordinates.x = surfaceProperties.destX; + surfaceCoordinates.y = surfaceProperties.destY; + surfaceCoordinates.z = surfaceProperties.destX + surfaceProperties.destWidth; + surfaceCoordinates.w = surfaceProperties.destX + surfaceProperties.destHeight; + } + + return surfaceCoordinates; +} + +t_ilm_bool surfaceRenderedOnScreen(t_scene_data& scene, t_ilm_surface surface) +{ + //if scene belongs to a layer and that layer belongs to a screen + if (scene.surfaceLayer.find(surface) != scene.surfaceLayer.end()) + { + t_ilm_layer layer = scene.surfaceLayer[surface]; + if (scene.layerScreen.find(layer) != scene.layerScreen.end()) + { + return ILM_TRUE; + } + } + + return ILM_FALSE; +} + +vector getSceneRenderOrder(t_scene_data* pScene) +{ + t_scene_data& scene = *pScene; + vector renderOrder; + + //iterate over screens + for (vector::iterator it = scene.screens.begin(); it != scene.screens.end(); ++it) + { + t_ilm_display screen = *it; + vector layers = scene.screenLayers[screen]; + + //iterate over layers + for (vector::iterator layerIterator = layers.begin(); + layerIterator != layers.end(); ++layerIterator) + { + t_ilm_layer layer = (*layerIterator); + vector surfaces = scene.layerSurfaces[layer]; + + //iterate over surfaces + for (vector::iterator it = surfaces.begin(); it != surfaces.end(); ++it) + { + t_ilm_surface surface = *it; + + renderOrder.push_back(surface); + } + } + } + + return renderOrder; +} + +void captureSceneData(t_scene_data* pScene) +{ + t_scene_data& scene = *pScene; + + //get screen information + t_ilm_uint screenWidth = 0; + t_ilm_uint screenHeight = 0; + + ilmErrorTypes callResult = ilm_getScreenResolution(0, &screenWidth, &screenHeight); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get screen resolution for screen with ID " << 0 << "\n"; + return; + } + + scene.screenWidth = screenWidth; + scene.screenHeight = screenHeight; + + //extra layer for debugging + scene.extraLayer = 0xFFFFFFFF; + + //get screens + unsigned int screenCount = 0; + t_ilm_display* screenArray = NULL; + + callResult = ilm_getScreenIDs(&screenCount, &screenArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get available screen IDs\n"; + return; + } + + scene.screens = vector(screenArray, screenArray + screenCount); + + //layers on each screen + for (unsigned int i = 0; i < screenCount; ++i) + { + t_ilm_display screenId = screenArray[i]; + + t_ilm_int layerCount = 0; + t_ilm_layer* layerArray = NULL; + + callResult = ilm_getLayerIDsOnScreen(screenId, &layerCount, &layerArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get layers on screen with ID " << screenId << "\n"; + return; + } + + scene.screenLayers[screenId] = vector(layerArray, layerArray + layerCount); + + //preserve rendering order for layers on each screen + for (int j = 0; j < layerCount; ++j) + { + t_ilm_layer layerId = layerArray[j]; + + scene.layerScreen[layerId] = screenId; + } + } + + //get all layers (rendered and not rendered) + t_ilm_int layerCount = 0; + t_ilm_layer* layerArray = NULL; + ilm_getLayerIDs(&layerCount, &layerArray); + scene.layers = vector(layerArray, layerArray + layerCount); + + for (int j = 0; j < layerCount; ++j) + { + t_ilm_layer layerId = layerArray[j]; + + //layer properties + ilmLayerProperties lp; + + callResult = ilm_getPropertiesOfLayer(layerId, &lp); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get properties of layer with ID " << layerId << "\n"; + return; + } + + scene.layerProperties[layerId] = lp; + } + + //surfaces on each layer + for (int j = 0; j < layerCount; ++j) + { + t_ilm_layer layerId = layerArray[j]; + + //surfaces on layer (in rendering order) + int surfaceCount = 0; + t_ilm_surface* surfaceArray = NULL; + + callResult = ilm_getSurfaceIDsOnLayer(layerId, &surfaceCount, &surfaceArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get surfaces on layer with ID " << layerId << "\n"; + return; + } + + //rendering order on layer + scene.layerSurfaces[layerId] = vector(surfaceArray, surfaceArray + surfaceCount); + + //make each surface aware of its layer + for (int k = 0; k < surfaceCount; ++k) + { + t_ilm_surface surfaceId = surfaceArray[k]; + scene.surfaceLayer[surfaceId] = layerId; + } + } + + //get all surfaces (on layers and without layers) + t_ilm_int surfaceCount = 0; + t_ilm_surface* surfaceArray = NULL; + + callResult = ilm_getSurfaceIDs(&surfaceCount, &surfaceArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get available surfaces\n"; + return; + } + + scene.surfaces = vector(surfaceArray, surfaceArray + surfaceCount); + + for (int k = 0; k < surfaceCount; ++k) + { + t_ilm_surface surfaceId = surfaceArray[k]; + + //surface properties + ilmSurfaceProperties sp; + + callResult = ilm_getPropertiesOfSurface(surfaceId, &sp); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get properties of surface with ID " << surfaceId << "\n"; + return; + } + + scene.surfaceProperties[surfaceId] = sp; + } +} + +void setScene(t_scene_data* pScene, bool clean) +{ + t_scene_data initialScene; + captureSceneData(&initialScene); + + //dismantel current scene + for (map::iterator it = initialScene.surfaceLayer.begin(); + it != initialScene.surfaceLayer.end(); ++it) + { + t_ilm_surface surface = it->first; + t_ilm_layer layer = it->second; + + ilmErrorTypes callResult = ilm_layerRemoveSurface(layer, surface); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to remove surface " << surface << " from layer " << layer << "\n"; + return; + } + } + + ilm_commitChanges(); + + //cleaning scene if needed + if (clean) + { + //remove unneeded surfaces and layers! + for (vector::iterator it = initialScene.surfaces.begin(); + it != initialScene.surfaces.end(); ++it) + { + t_ilm_surface surface = *it; + //if surface does not exist (in final scene) + if (find(pScene->surfaces.begin(), pScene->surfaces.end(), surface) == pScene->surfaces.end()) + { + //remove surface ! + ilmErrorTypes callResult = ilm_surfaceRemove(surface); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to remove surface " << surface << "\n"; + return; + } + } + } + + ilm_commitChanges(); + + for (vector::iterator it = initialScene.layers.begin(); + it != initialScene.layers.end(); ++it) + { + t_ilm_layer layer = *it; + //if layer does not exist (in final scene) + if (find(pScene->layers.begin(), pScene->layers.end(), layer) == pScene->layers.end()) + { + //remove surface ! + ilmErrorTypes callResult = ilm_layerRemove(layer); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to remove layer " << layer << "\n"; + return; + } + } + } + + ilm_commitChanges(); + } + + //make sure all layers and surfaces of the new scene are created + for (vector::iterator it = pScene->layers.begin(); + it != pScene->layers.end(); ++it) + { + t_ilm_layer layer = *it; + //if layer does not exist (in initial scene) + if (find(initialScene.layers.begin(), initialScene.layers.end(), layer) == initialScene.layers.end()) + { + ilmLayerProperties& props = pScene->layerProperties[layer]; + + ilmErrorTypes callResult = ilm_layerCreateWithDimension(&layer, props.origSourceWidth, props.origSourceHeight); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to create layer with ID " << layer << " and dimensions (" << props.origSourceWidth << " ," << props.origSourceHeight << ")\n"; + return; + } + } + } + + ilm_commitChanges(); + + for (vector::iterator it = pScene->surfaces.begin(); + it != pScene->surfaces.end(); ++it) + { + t_ilm_surface surface = *it; + //if surface does not exist (in initial scene) + if (find(initialScene.surfaces.begin(), initialScene.surfaces.end(), surface) + == initialScene.surfaces.end()) + { + ilmSurfaceProperties& props = pScene->surfaceProperties[surface]; + + ilm_surfaceCreate(props.nativeSurface, + props.origSourceWidth, + props.origSourceHeight, + (e_ilmPixelFormat) props.pixelformat, + &surface); + } + + //if surface exists but mapped to different native: remove then recreate it + if (initialScene.surfaceProperties[surface].nativeSurface + != pScene->surfaceProperties[surface].nativeSurface) + { + ilm_surfaceRemove(surface); + ilmSurfaceProperties& props = pScene->surfaceProperties[surface]; + ilm_surfaceCreate(props.nativeSurface, + props.origSourceWidth, + props.origSourceHeight, + (e_ilmPixelFormat) props.pixelformat, + &surface); + } + } + + ilm_commitChanges(); + + //set render order of layers on each screen + for (vector::iterator it = pScene->screens.begin(); + it != pScene->screens.end(); ++it) + { + t_ilm_display screen = *it; + vector& layers = pScene->screenLayers[screen]; + + //if vector data is NULL (no data in vector) + if (layers.data() == NULL) + { + layers.reserve(1); + } + + ilm_displaySetRenderOrder(screen, layers.data(), layers.size()); + } + + ilm_commitChanges(); + + //set render order of surfaces on each layer + for (map >::iterator it = pScene->layerSurfaces.begin(); + it != pScene->layerSurfaces.end(); ++it) + { + t_ilm_layer layer = it->first; + vector& surfaces = it->second; + if (surfaces.data() == NULL) + { + surfaces.reserve(1); + } + + ilm_layerSetRenderOrder(layer, surfaces.data(), surfaces.size()); + } + + ilm_commitChanges(); + + //set properties of layers + for (map::iterator it = pScene->layerProperties.begin(); + it != pScene->layerProperties.end(); ++it) + { + t_ilm_layer layer = it->first; + ilmLayerProperties& props = it->second; + + //set layer properties + ilm_layerSetDestinationRectangle(layer, props.destX, props.destY, props.destWidth, props.destHeight); + ilm_commitChanges(); + + ilm_layerSetOpacity(layer, props.opacity); + ilm_commitChanges(); + + ilm_layerSetOrientation(layer, props.orientation); + ilm_commitChanges(); + + ilm_layerSetSourceRectangle(layer, props.sourceX, props.sourceY, props.sourceWidth, props.sourceHeight); + ilm_commitChanges(); + + ilm_layerSetVisibility(layer, props.visibility); + ilm_commitChanges(); + } + + //set properties of surfaces + for (map::iterator it = pScene->surfaceProperties.begin(); + it != pScene->surfaceProperties.end(); ++it) + { + t_ilm_surface surface = it->first; + ilmSurfaceProperties& props = it->second; + + ilm_surfaceSetNativeContent(props.nativeSurface, + props.origSourceWidth, + props.origSourceHeight, + (e_ilmPixelFormat) props.pixelformat, + surface); + ilm_commitChanges(); + + ilm_surfaceSetOpacity(surface, props.opacity); + ilm_commitChanges(); + + ilm_surfaceSetOrientation(surface, props.orientation); + ilm_commitChanges(); + + ilm_surfaceSetSourceRectangle(surface, props.sourceX, props.sourceY, props.sourceWidth, props.sourceHeight); + ilm_commitChanges(); + + ilm_surfaceSetDestinationRectangle(surface, props.destX, props.destY, props.destWidth, props.destHeight); + ilm_commitChanges(); + + ilm_surfaceSetVisibility(surface, props.visibility); + ilm_commitChanges(); + } + + ilm_commitChanges(); +} + +void emptyScene(t_scene_data* pScene) +{ + pScene->extraLayer = -1; + pScene->layerProperties.clear(); + pScene->layerScreen.clear(); + pScene->layerSurfaces.clear(); + pScene->layers.clear(); + pScene->screenLayers.clear(); + pScene->screens.clear(); + pScene->surfaceLayer.clear(); + pScene->surfaceProperties.clear(); + pScene->surfaces.clear(); + + t_ilm_uint count; + t_ilm_display* screenArray; + ilm_getScreenIDs(&count, &screenArray); + for (t_ilm_uint i = 0; i < count; ++i) + { + pScene->screens.push_back(screenArray[i]); + + ilmErrorTypes callResult = ilm_getScreenResolution(screenArray[0], & pScene->screenWidth, & pScene->screenHeight); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get screen resolution for screen with ID " << screenArray[0] << "\n"; + return; + } + } +} + +t_scene_data cloneToUniLayerScene(t_scene_data* pScene) +{ + //prepare values needed for dummy (animation) scene + t_ilm_layer extraLayer = -1; + //if the scene is already uni layer just reuse same layer + if (pScene->layers.size() == 1) + { + extraLayer = pScene->layers[0]; + } + + t_ilm_uint screenWidth; + t_ilm_uint screenHeight; + ilmErrorTypes callResult = ilm_getScreenResolution(0, &screenWidth, &screenHeight); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to create layer\n"; + } + + callResult = ilm_layerCreateWithDimension(&extraLayer, screenWidth, screenHeight); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to create layer\n"; + } + + ilmLayerProperties extraLayerProperties; + extraLayerProperties.destHeight = pScene->screenHeight; + extraLayerProperties.destWidth = pScene->screenWidth; + extraLayerProperties.destX = 0; + extraLayerProperties.destY = 0; + extraLayerProperties.opacity = 1; + extraLayerProperties.orientation = (e_ilmOrientation) 0; + extraLayerProperties.origSourceHeight = pScene->screenHeight; + extraLayerProperties.origSourceWidth = pScene->screenWidth; + extraLayerProperties.sourceHeight = pScene->screenHeight; + extraLayerProperties.sourceWidth = pScene->screenWidth; + extraLayerProperties.sourceX = 0; + extraLayerProperties.sourceY = 0; + extraLayerProperties.type = 2; + extraLayerProperties.visibility = 1; + + t_ilm_display screen = pScene->screens[0]; + + vector finalRenderOrder = getSceneRenderOrder(pScene); + + //build dummy scene to be used for animation + t_scene_data dummyScene; + //to avoid destroying and recreating surfaces and layers + dummyScene.surfaces = pScene->surfaces; + + //the real deal ! + dummyScene.screens.push_back(screen); + dummyScene.screenLayers[screen] = vector(&extraLayer, &extraLayer + 1); + dummyScene.layerScreen[extraLayer] = screen; + dummyScene.layerSurfaces[extraLayer] = finalRenderOrder; + + for (vector::iterator it = finalRenderOrder.begin(); + it != finalRenderOrder.end(); ++it) + { + dummyScene.surfaceProperties[*it] = pScene->surfaceProperties[*it]; + + tuple4 coords = getSurfaceScreenCoordinates(pScene, *it); + + dummyScene.surfaceProperties[*it].destX = coords.x; + dummyScene.surfaceProperties[*it].destY = coords.y; + dummyScene.surfaceProperties[*it].destWidth = coords.z - coords.x; + dummyScene.surfaceProperties[*it].destHeight = coords.w - coords.y; + } + + dummyScene.layerProperties[extraLayer] = extraLayerProperties; + + dummyScene.layerScreen[extraLayer] = screen; + + for (vector::iterator it = finalRenderOrder.begin(); + it != finalRenderOrder.end(); ++it) + { + dummyScene.surfaceLayer[*it] = extraLayer; + } + + dummyScene.surfaces = finalRenderOrder; + dummyScene.layers = vector(&extraLayer, &extraLayer + 1); + + dummyScene.extraLayer = -1; + dummyScene.screenWidth = pScene->screenWidth; + dummyScene.screenHeight = pScene->screenHeight; + + return dummyScene; +} + +tuple4 interpolateCoordinatesHelper(tuple4& start, tuple4& end, float t) +{ + t = static_cast(1 - pow((t - 1), 4)); + return tuple4(static_cast(start.x * (1 - t) + end.x * t), + static_cast(start.y * (1 - t) + end.y * t), + static_cast(start.z * (1 - t) + end.z * t), + static_cast(start.w * (1 - t) + end.w * t)); +} + +void transformScene(t_scene_data* pInitialScene, t_scene_data* pFinalScene, t_ilm_long durationMillis, t_ilm_int frameCount) +{ + t_scene_data dummyScene = cloneToUniLayerScene(pFinalScene); + //set dummy scene + setScene(&dummyScene); + + //animate dummy scene ! + + if (durationMillis > 0 && frameCount > 0) + { + //sleep time + long sleepMillis = durationMillis / frameCount; + struct timespec sleepTime; + sleepTime.tv_nsec = (sleepMillis % 1000) * 1000000; + sleepTime.tv_sec = sleepMillis / 1000; + + struct timespec remTime; + + //start and end coordinates of surfaces + map start; + map end; + for (vector::iterator it = dummyScene.surfaces.begin(); + it != dummyScene.surfaces.end(); ++it) + { + t_ilm_surface surface = *it; + start[surface] = getSurfaceScreenCoordinates(pInitialScene, surface); + + end[surface] = getSurfaceScreenCoordinates(pFinalScene, surface); + } + + for (int i = 0; i < frameCount; ++i) + { + float t = 1.0 * i / frameCount; + + //interpolate properties of each surface + for (vector::iterator it = dummyScene.surfaces.begin(); + it != dummyScene.surfaces.end(); ++it) + { + t_ilm_surface surface = *it; + tuple4 coords = interpolateCoordinatesHelper(start[surface], end[surface], t); + + ilmErrorTypes callResult = ilm_surfaceSetDestinationRectangle(surface, coords.x, coords.y, + coords.z - coords.x, coords.w - coords.y); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << coords.x << "," << coords.y << ", " + << coords.z - coords.x << ", " << coords.w - coords.y + <<") for surface with ID " << surface << "\n"; + return; + } + + float opacity = t * pFinalScene->surfaceProperties[surface].opacity + + (1 - t) * pInitialScene->surfaceProperties[surface].opacity; + ilm_surfaceSetOpacity(surface, opacity); + } + + ilm_commitChanges(); + + //sleep + nanosleep(&sleepTime, &remTime); + } + } + + //set final scene + setScene(pFinalScene); +} + +static t_scene_data* global_pOriginalScene = NULL; + +void interruptSignalRestoreScene(int s) +{ + (void) s; + + cout << "LayerManagerControl :Interrupt signal...\n"; + if (global_pOriginalScene != NULL) + { + setScene(global_pOriginalScene, true); + sleep(3); + exit(0); + } +} + +void setSceneToRestore(t_scene_data* pScene) +{ + if (global_pOriginalScene == NULL) + { + global_pOriginalScene = pScene; + struct sigaction signalAction; + + signalAction.sa_handler = &interruptSignalRestoreScene; + sigemptyset(&signalAction.sa_mask); + signalAction.sa_flags = 0; + + sigaction(SIGINT, &signalAction, NULL); + } +} diff --git a/ivi-layermanagement-examples/LayerManagerControl/src/control.cpp b/ivi-layermanagement-examples/LayerManagerControl/src/control.cpp new file mode 100644 index 0000000..7404b2a --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/src/control.cpp @@ -0,0 +1,417 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ + +#include "ilm_client.h" +#include "ilm_control.h" +#include "LMControl.h" + +#include + +#include +using std::cout; +using std::cin; +using std::cerr; +using std::endl; + + +#include +using std::dec; +using std::hex; + + +#include +#include +#include +#include + + +bool gBenchmark_running; + +void benchmarkSigHandler(int sig) +{ + (void) sig; + gBenchmark_running = false; +} + +void getCommunicatorPerformance() +{ + int runs = 0; + int runtimeInSec = 5; + unsigned int hwLayerCnt = 0; + cout << "running performance test for " << runtimeInSec << " seconds... "; + flush(cout); + + signal(SIGALRM, benchmarkSigHandler); + + gBenchmark_running = true; + + alarm(runtimeInSec); + + while (gBenchmark_running) + { + t_ilm_uint screenid = 0; + + ilmErrorTypes callResult = ilm_getNumberOfHardwareLayers(screenid, &hwLayerCnt); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get number of hardware layers for screen with ID " << screenid << "\n"; + return; + } + + ++runs; + } + + signal(SIGALRM, SIG_DFL); + + cout << (runs / runtimeInSec) << " transactions/second\n"; +} + +void setSurfaceKeyboardFocus(t_ilm_surface surface) +{ + ilmErrorTypes callResult = ilm_SetKeyboardFocusOn(surface); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set keyboard focus at surface with ID " << surface << "\n"; + return; + } +} + +void getKeyboardFocus() +{ + t_ilm_surface surfaceId; + + ilmErrorTypes callResult = ilm_GetKeyboardFocusSurfaceId(&surfaceId); + if (ILM_SUCCESS == callResult) + { + cout << "keyboardFocusSurfaceId == " << surfaceId << endl; + } + else + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get keyboard focus surface ID\n"; + return; + } +} + +void setSurfaceAcceptsInput(t_ilm_surface surfaceId, string kbdPointerTouch, t_ilm_bool acceptance) +{ + char* str; + char* tok; + + ilmInputDevice devices = (ilmInputDevice)0; + + str = new char [kbdPointerTouch.size()+1]; + strcpy(str, kbdPointerTouch.c_str()); + tok = strtok(str, ":"); + while (tok != NULL) + { + if (!strcmp(tok, "kbd")) + { + devices |= ILM_INPUT_DEVICE_KEYBOARD; + } + else if (!strcmp(tok, "pointer")) + { + devices |= ILM_INPUT_DEVICE_POINTER; + } + else if (!strcmp(tok, "touch")) + { + devices |= ILM_INPUT_DEVICE_TOUCH; + } + else + { + cerr << "Unknown devices specified." << endl; + } + + tok = strtok(NULL, ":"); + } + + ilmErrorTypes callResult = ilm_UpdateInputEventAcceptanceOn(surfaceId, devices, acceptance); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to update input event acceptance on surface with ID " << surfaceId << "\n"; + delete[] str; + return; + } + + ilm_commitChanges(); + + delete[] str; +} + +void layerNotificationCallback(t_ilm_layer layer, struct ilmLayerProperties* properties, t_ilm_notification_mask mask) +{ + cout << "\nNotification: layer " << layer << " updated properties:\n"; + + if (ILM_NOTIFICATION_VISIBILITY & mask) + { + cout << "\tvisibility = " << properties->visibility << "\n"; + } + + if (ILM_NOTIFICATION_OPACITY & mask) + { + cout << "\topacity = " << properties->opacity << "\n"; + } + + if (ILM_NOTIFICATION_ORIENTATION & mask) + { + cout << "\torientation = " << properties->orientation << "\n"; + } + + if (ILM_NOTIFICATION_SOURCE_RECT & mask) + { + cout << "\tsource rect = x:" << properties->sourceX + << ", y:" << properties->sourceY + << ", width:" << properties->sourceWidth + << ", height:" << properties->sourceHeight + << "\n"; + } + + if (ILM_NOTIFICATION_DEST_RECT & mask) + { + cout << "\tdest rect = x:" << properties->destX + << ", y:" << properties->destY + << ", width:" << properties->destWidth + << ", height:" << properties->destHeight + << "\n"; + } +} + +void testNotificationLayer(t_ilm_layer layerid) +{ + cout << "Setup notification for layer " << layerid << " \n"; + + ilmErrorTypes callResult = ilm_layerAddNotification(layerid, layerNotificationCallback); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to add notification callback to layer with ID " << layerid << "\n"; + return; + } + + for (int i = 0; i < 2; ++i) + { + usleep(100 * 1000); + cout << "Set layer 1000 visbility to FALSE\n"; + ilm_layerSetVisibility(layerid, ILM_FALSE); + ilm_commitChanges(); + + usleep(100 * 1000); + cout << "Set layer 1000 visbility to TRUE\n"; + ilm_layerSetVisibility(layerid, ILM_TRUE); + + cout << "Set layer 1000 opacity to 0.3\n"; + ilm_layerSetOpacity(layerid, 0.3); + ilm_commitChanges(); + + usleep(100 * 1000); + cout << "Set layer 1000 opacity to 1.0\n"; + ilm_layerSetOpacity(layerid, 1.0); + ilm_commitChanges(); + } + + ilm_commitChanges(); // make sure, app lives long enough to receive last notification +} + +void watchLayer(unsigned int* layerids, unsigned int layeridCount) +{ + for (unsigned int i = 0; i < layeridCount; ++i) + { + unsigned int layerid = layerids[i]; + cout << "Setup notification for layer " << layerid << "\n"; + + ilmErrorTypes callResult = ilm_layerAddNotification(layerid, layerNotificationCallback); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to add notification callback to layer with ID " << layerid << "\n"; + return; + } + } + + cout << "Waiting for notifications...\n"; + int block; + cin >> block; + + for (unsigned int i = 0; i < layeridCount; ++i) + { + unsigned int layerid = layerids[i]; + cout << "Removing notification for layer " << layerid << "\n"; + + ilmErrorTypes callResult = ilm_layerRemoveNotification(layerid); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to remove notification callback of layer with ID " << layerid << "\n"; + return; + } + } + + if (layerids) + { + delete[] layerids; + } +} + +void surfaceNotificationCallback(t_ilm_layer surface, struct ilmSurfaceProperties* properties, t_ilm_notification_mask mask) +{ + cout << "\nNotification: surface " << surface << " updated properties:\n"; + + if (ILM_NOTIFICATION_VISIBILITY & mask) + { + cout << "\tvisibility = " << properties->visibility << "\n"; + } + + if (ILM_NOTIFICATION_OPACITY & mask) + { + cout << "\topacity = " << properties->opacity << "\n"; + } + + if (ILM_NOTIFICATION_ORIENTATION & mask) + { + cout << "\torientation = " << properties->orientation << "\n"; + } + + if (ILM_NOTIFICATION_SOURCE_RECT & mask) + { + cout << "\tsource rect = x:" << properties->sourceX + << ", y:" << properties->sourceY + << ", width:" << properties->sourceWidth + << ", height:" << properties->sourceHeight + << "\n"; + } + + if (ILM_NOTIFICATION_DEST_RECT & mask) + { + cout << "\tdest rect = x:" << properties->destX + << ", y:" << properties->destY + << ", width:" << properties->destWidth + << ", height:" << properties->destHeight + << "\n"; + } +} + +void watchSurface(unsigned int* surfaceids, unsigned int surfaceidCount) +{ + for (unsigned int i = 0; i < surfaceidCount; ++i) + { + unsigned int surfaceid = surfaceids[i]; + cout << "Setup notification for surface " << surfaceid << "\n"; + + ilmErrorTypes callResult = ilm_surfaceAddNotification(surfaceid, surfaceNotificationCallback); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to add notification callback to surface with ID " << surfaceid << "\n"; + return; + } + } + + cout << "Waiting for notifications...\n"; + int block; + cin >> block; + + for (unsigned int i = 0; i < surfaceidCount; ++i) + { + unsigned int surfaceid = surfaceids[i]; + cout << "Removing notification for surface " << surfaceid << "\n"; + + ilmErrorTypes callResult = ilm_surfaceRemoveNotification(surfaceid); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to remove notification callback of surface with ID " << surfaceid << "\n"; + return; + } + } + + if (surfaceids) + { + delete[] surfaceids; + } +} + +void setOptimization(t_ilm_uint id, t_ilm_uint mode) +{ + ilmErrorTypes callResult = ilm_SetOptimizationMode((ilmOptimization)id, + (ilmOptimizationMode)mode); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set optimization with ID " << id << " mode " << mode << "\n"; + return; + } + + ilm_commitChanges(); +} + +void getOptimization(t_ilm_uint id) +{ + ilmOptimization optimizationId = (ilmOptimization)id; + ilmOptimizationMode optimizationMode; + + ilmErrorTypes callResult = ilm_GetOptimizationMode(optimizationId, &optimizationMode); + if (callResult == ILM_SUCCESS) + { + switch (optimizationId) + { + case ILM_OPT_MULTITEXTURE : + cout << "Optimization " << (int)optimizationId << " (Multitexture Optimization)" << endl; + break; + + case ILM_OPT_SKIP_CLEAR : + cout << "Optimization " << (int)optimizationId << " (Skip Clear)" << endl; + break; + default: + cout << "Optimization " << "unknown" << endl; + break; + } + + switch (optimizationMode) + { + case ILM_OPT_MODE_FORCE_OFF : + cout << "Mode " << (int)optimizationMode << " (forced off)" << endl; + break; + + case ILM_OPT_MODE_FORCE_ON : + cout << "Mode " << (int)optimizationMode << " (forced on)" << endl; + break; + case ILM_OPT_MODE_HEURISTIC : + cout << "Mode " << (int)optimizationMode << " (Heuristic / Render choose the optimization)" << endl; + break; + case ILM_OPT_MODE_TOGGLE : + cout << "Mode " << (int)optimizationMode << " (Toggle on/and off rapidly for debugging)" << endl; + break; + + default: + cout << "Mode " << "unknown" << endl; + break; + } + } + else + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get mode for optimization with ID " << optimizationId << "\n"; + return; + } + + ilm_commitChanges(); +} diff --git a/ivi-layermanagement-examples/LayerManagerControl/src/demo.cpp b/ivi-layermanagement-examples/LayerManagerControl/src/demo.cpp new file mode 100644 index 0000000..20c3a5c --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/src/demo.cpp @@ -0,0 +1,903 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ + +#include "ilm_client.h" +#include "ilm_control.h" +#include "LMControl.h" + +#include +using std::min; +using std::max; + +#include + + +#include +using std::cout; +using std::cin; +using std::cerr; +using std::endl; + +#include +using std::dec; +using std::hex; + +#include + +#include +using std::vector; + + + +namespace +{ +typedef void(*t_pDemoAnimatorFunc)(t_scene_data* pInitialScene, t_scene_data* pDemoScene, bool* pStopDemo); + +void demoAnimatorDownwards(t_scene_data* pInitialScene, t_scene_data* pDemoScene, bool* pStopDemo) +{ + (void) pInitialScene; //suppress warning: unused varaible + + //get rendered surfaces + vector renderedSurfaces = getSceneRenderOrder(pDemoScene); + + //make random scale factors to scale down surfaces + float maxScale = 0.7; + float minScale = 0.2; + float rangeScale = maxScale - minScale; + + srand((unsigned) time(0)); + map surfaceScale; + + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + surfaceScale[surface] = minScale + rangeScale * (1.0 * rand() / RAND_MAX); + } + + //make random speeds (in pixel) + t_ilm_int minSpeed = 2; + + srand((unsigned) time(0)); + map surfaceSpeed; + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + surfaceSpeed[surface] = minSpeed + static_cast(rangeScale* (1.0 * rand() / RAND_MAX)); + surfaceSpeed[surface] = minSpeed + + (static_cast(surfaceSpeed[surface] * (pow(2 + surfaceScale[surface], 2) - 4))); + } + + //set random direction + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + //set 50% of surfaces to move in -ve y direction + surfaceSpeed[surface] *= rand() % 2 ? -1 : 1; + } + + //get surface positions and set initial positions + map surfaceCoordinates; + + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + ilmSurfaceProperties& surfaceProperties = + pDemoScene->surfaceProperties[surface]; + + //get current destination region AND scale surface dimensions + tuple4 coordinates(surfaceProperties.destX, + surfaceProperties.destY, + surfaceProperties.destX + static_cast(surfaceScale[surface] * surfaceProperties.destWidth), + surfaceProperties.destY + static_cast(surfaceScale[surface] * surfaceProperties.destHeight)); + + //make random X position to make surfaces spread in the screen + int surfaceWidth = coordinates.z - coordinates.x; + + //make a random X position so that the surface stays totally displayable inside the screen + int maxX = pDemoScene->screenWidth - surfaceWidth; + int xRandom = static_cast(maxX * 1.0 * rand() / RAND_MAX); + + coordinates.x = xRandom; + coordinates.z = xRandom + surfaceWidth; + + surfaceCoordinates[surface] = coordinates; + } + + //sleeping time + long sleepTimeMillis = 25; + + //start animation ! + while (! *pStopDemo) + { + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + ilmSurfaceProperties& properties = pDemoScene->surfaceProperties[surface]; + tuple4& coordinates = surfaceCoordinates[surface]; + + //if out: get back to screen + if (coordinates.y >= static_cast(pDemoScene->screenHeight)) + { + //reset to top + coordinates.y = coordinates.y - coordinates.w; + coordinates.w = 0; + } + + //move + coordinates.y += abs(surfaceSpeed[surface]); + coordinates.w += abs(surfaceSpeed[surface]); + + //if the upper part is not visible remove it from the source and destination regions + if (coordinates.y <= 0) + { + //set source region to only the visible part of the surface + int ySource = static_cast(-coordinates.y / surfaceScale[surface]); + + ilmErrorTypes callResult = ilm_surfaceSetSourceRectangle(surface, 0, ySource, properties.sourceWidth, + properties.sourceHeight - ySource); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set source rectangle (" << 0 << "," << ySource << ", " << properties.sourceWidth + << ", " << properties.sourceHeight - ySource + << ") for surface with ID " << surface << "\n"; + } + + //set the destination region AT THE TOP of the layer used for displaying the surface + callResult = ilm_surfaceSetDestinationRectangle(surface, + coordinates.x, + 0, + max(0, coordinates.z - coordinates.x), + max(0, coordinates.w)); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << coordinates.x << "," << 0 << ", " << max(0, coordinates.z - coordinates.x) << ", " + << max(0, coordinates.w) << ") for surface with ID " << surface << "\n"; + } + } + else + { + //set source region to whole surface + ilmErrorTypes callResult = ilm_surfaceSetSourceRectangle(surface, 0, 0, properties.sourceWidth, properties.sourceHeight); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set source rectangle (" << 0 << "," << 0 << ", " << properties.sourceWidth << ", " << properties.sourceHeight + << ") for surface with ID " << surface << "\n"; + } + + //set destination region to the region on the layer used for displaying the surface + callResult = ilm_surfaceSetDestinationRectangle(surface, + coordinates.x, + coordinates.y, + max(0, coordinates.z - coordinates.x), + max(0, coordinates.w - coordinates.y)); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << coordinates.x << "," << coordinates.y << ", " << max(0, coordinates.z - coordinates.x) << ", " + << max(0, coordinates.w - coordinates.y) << ") for surface with ID " << surface << "\n"; + } + } + + surfaceCoordinates[surface] = coordinates; + } + + ilm_commitChanges(); + usleep(sleepTimeMillis * 1000); + } +} + +void demoAnimatorRandomDirections(t_scene_data* pInitialScene, t_scene_data* pDemoScene, bool* pStopDemo) +{ + (void) pInitialScene; //suppress warning: unused varaible + + //get rendered surfaces + vector renderedSurfaces = getSceneRenderOrder(pDemoScene); + + //make random scale factors to scale down surfaces + float maxScale = 0.7; + float minScale = 0.2; + float rangeScale = maxScale - minScale; + + srand((unsigned) time(0)); + map surfaceScale; + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + surfaceScale[surface] = minScale + rangeScale * (1.0 * rand() / RAND_MAX); + } + + //make random speeds (in pixel) + t_ilm_int minSpeed = 2; + + srand((unsigned) time(0)); + map surfaceSpeed; + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + surfaceSpeed[surface] = minSpeed + static_cast(rangeScale * (1.0 * rand() / RAND_MAX)); + surfaceSpeed[surface] = minSpeed + + (static_cast(surfaceSpeed[surface] * (pow(2 + surfaceScale[surface], 2) - 4))); + } + + //set random direction + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + //set 50% of surfaces to move in -ve y direction + surfaceSpeed[surface] *= rand() % 2 ? -1 : 1; + } + + //get surface positions and set initial positions + map surfaceCoordinates; + + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + ilmSurfaceProperties& surfaceProperties = pDemoScene->surfaceProperties[surface]; + + //get current destination region AND scale surface dimensions + tuple4 coordinates(surfaceProperties.destX, + surfaceProperties.destY, + surfaceProperties.destX + static_cast(surfaceScale[surface] * surfaceProperties.destWidth), + surfaceProperties.destY + static_cast(surfaceScale[surface] * surfaceProperties.destHeight)); + + //make random X position to make surfaces spread in the screen + int surfaceWidth = coordinates.z - coordinates.x; + + //make a random X position so that the surface stays totally displayable inside the screen + int maxX = pDemoScene->screenWidth - surfaceWidth; + int xRandom = static_cast(maxX * 1.0 * rand() / RAND_MAX); + + coordinates.x = xRandom; + coordinates.z = xRandom + surfaceWidth; + + surfaceCoordinates[surface] = coordinates; + } + + //sleeping time + long sleepTimeMillis = 25; + + //start animation ! + while (! *pStopDemo) + { + //set transparency to be inversly proportional to scale + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + + float opacity = min(1.0, 1.1 - surfaceScale[surface]); + + ilmErrorTypes callResult = ilm_surfaceSetOpacity(surface, opacity); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set opacity " << opacity << " for surface with ID " << surface << "\n"; + } + } + + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + ilmSurfaceProperties& properties = pDemoScene->surfaceProperties[surface]; + tuple4& coordinates = surfaceCoordinates[surface]; + int speed = surfaceSpeed[surface]; + + //if out: get back to screen + if (coordinates.y >= static_cast(pDemoScene->screenHeight) && speed > 0) + { + //reset to top + coordinates.y = coordinates.y - coordinates.w; + coordinates.w = 0; + } + else if (coordinates.w <= 0 && speed < 0) + { + coordinates.w = pDemoScene->screenHeight + coordinates.w - coordinates.y; + coordinates.y = pDemoScene->screenHeight; + } + + //move + coordinates.y += surfaceSpeed[surface]; + coordinates.w += surfaceSpeed[surface]; + + //if the upper part is not visible remove it from the source and destination regions + if (coordinates.y <= 0) + { + //set source region to only the visible part of the surface + int ySource = static_cast(-coordinates.y / surfaceScale[surface]); + + ilmErrorTypes callResult = ilm_surfaceSetSourceRectangle(surface, 0, ySource, properties.sourceWidth, + properties.sourceHeight - ySource); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set source rectangle (" << 0 << "," << ySource << ", " << properties.sourceWidth + << ", " << properties.sourceHeight - ySource + << ") for surface with ID " << surface << "\n"; + } + + //set the destination region AT THE TOP of the layer used for displaying the surface + callResult = ilm_surfaceSetDestinationRectangle(surface, + coordinates.x, + 0, + max(0, coordinates.z - coordinates.x), + max(0, coordinates.w)); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << coordinates.x << "," << 0 << ", " << max(0, coordinates.z - coordinates.x) << ", " << max(0, coordinates.w) + << ") for surface with ID " << surface << "\n"; + } + } + else + { + //set source region to whole surface + ilmErrorTypes callResult = ilm_surfaceSetSourceRectangle(surface, 0, 0, properties.sourceWidth, properties.sourceHeight); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set source rectangle (" << 0 << "," << 0 << ", " << properties.sourceWidth << ", " << properties.sourceHeight + << ") for surface with ID " << surface << "\n"; + } + + //set destination region to the region on the layer used for displaying the surface + callResult = ilm_surfaceSetDestinationRectangle(surface, + coordinates.x, + coordinates.y, + max(0, coordinates.z - coordinates.x), + max(0, coordinates.w - coordinates.y)); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << coordinates.x << "," << coordinates.y << ", " << max(0, coordinates.z - coordinates.x) << ", " + << max(0, coordinates.w - coordinates.y) << ") for surface with ID " << surface << "\n"; + } + } + + surfaceCoordinates[surface] = coordinates; + } + + ilm_commitChanges(); + usleep(sleepTimeMillis * 1000); + } +} + +void demoAnimatorWaterfall(t_scene_data* pInitialScene, t_scene_data* pDemoScene, bool* pStopDemo) +{ + (void) pInitialScene; //suppress warning: unused varaible + + //get rendered surfaces + vector renderedSurfaces = getSceneRenderOrder(pDemoScene); + + //make random scale factors to scale down surfaces + float maxScale = 0.7; + float minScale = 0.2; + float rangeScale = maxScale - minScale; + + srand((unsigned) time(0)); + map surfaceScale; + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + surfaceScale[surface] = minScale + rangeScale * (1.0 * rand() / RAND_MAX); + } + + //make random speeds (in pixel) + t_ilm_int minSpeed = 2; + + srand((unsigned) time(0)); + map surfaceSpeed; + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + surfaceSpeed[surface] = minSpeed + static_cast(rangeScale * (1.0 * rand() / RAND_MAX)); + surfaceSpeed[surface] = minSpeed + + (static_cast(surfaceSpeed[surface] * (pow(2 + surfaceScale[surface], 2) - 4))); + } + + //set random direction + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + //set 50% of surfaces to move in -ve y direction + surfaceSpeed[surface] *= rand() % 2 ? -1 : 1; + } + + //get surface positions and set initial positions + map surfaceCoordinates; + + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + ilmSurfaceProperties& surfaceProperties = pDemoScene->surfaceProperties[surface]; + + //get current destination region AND scale surface dimensions + tuple4 coordinates(surfaceProperties.destX, + surfaceProperties.destY, + surfaceProperties.destX + static_cast(surfaceScale[surface] * surfaceProperties.destWidth), + surfaceProperties.destY + static_cast(surfaceScale[surface] * surfaceProperties.destHeight)); + + //make random X position to make surfaces spread in the screen + int surfaceWidth = coordinates.z - coordinates.x; + + //make a random X position so that the surface stays totally displayable inside the screen + int maxX = pDemoScene->screenWidth - surfaceWidth; + int xRandom = static_cast(maxX * 1.0 * rand() / RAND_MAX); + + coordinates.x = xRandom; + coordinates.z = xRandom + surfaceWidth; + + surfaceCoordinates[surface] = coordinates; + } + + //sleeping time + long sleepTimeMillis = 25; + + //start animation ! + while (! *pStopDemo) + { + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + ilmSurfaceProperties& properties = pDemoScene->surfaceProperties[surface]; + tuple4& coordinates = surfaceCoordinates[surface]; + + //if out: get back to screen + if (coordinates.y >= static_cast(pDemoScene->screenHeight)) + { + //reset to top + coordinates.y = coordinates.y - coordinates.w; + coordinates.w = 0; + } + + t_ilm_float fraction = max(0.0, 1.0 * (coordinates.w) / (pDemoScene->screenHeight + coordinates.w - coordinates.y)); + t_ilm_float t = pow(3, 0.0251 + fraction); + int displacement = static_cast(t * abs(surfaceSpeed[surface])); + + t_ilm_float opacity = min(1.0, max(0.0, 1 - fraction)); //between 0 and 1 + + ilmErrorTypes callResult = ilm_surfaceSetOpacity(surface, opacity); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set opacity " << opacity << " for surface with ID " << surface << "\n"; + } + + //move + coordinates.y += displacement; + coordinates.w += displacement; + + //if the upper part is not visible remove it from the source and destination regions + if (coordinates.y <= 0) + { + //set source region to only the visible part of the surface + int ySource = static_cast(-coordinates.y / surfaceScale[surface]); + ilmErrorTypes callResult = ilm_surfaceSetSourceRectangle(surface, + 0, + ySource, + properties.sourceWidth, + properties.sourceHeight - ySource); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set source rectangle (" << 0 << "," << ySource << ", " << properties.sourceWidth << ", " << properties.sourceHeight - ySource + << ") for surface with ID " << surface << "\n"; + } + + //set the destination region AT THE TOP of the layer used for displaying the surface + callResult = ilm_surfaceSetDestinationRectangle(surface, + coordinates.x, + 0, + max(0, coordinates.z - coordinates.x), + max(0, coordinates.w)); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << coordinates.x << "," << 0 << ", " << max(0, coordinates.z - coordinates.x) << ", " << max(0, coordinates.w) + << ") for surface with ID " << surface << "\n"; + } + } + else + { + //set source region to whole surface + ilmErrorTypes callResult = ilm_surfaceSetSourceRectangle(surface, 0, 0, properties.sourceWidth, properties.sourceHeight); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set source rectangle (" << 0 << "," << 0 << ", " << properties.sourceWidth << ", " << properties.sourceHeight + << ") for surface with ID " << surface << "\n"; + } + + //set destination region to the region on the layer used for displaying the surface + callResult = ilm_surfaceSetDestinationRectangle(surface, + coordinates.x, + coordinates.y, + max(0, coordinates.z - coordinates.x), + max(0, coordinates.w - coordinates.y)); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << coordinates.x << "," << coordinates.y << ", " << max(0, coordinates.z - coordinates.x) << ", " + << max(0, coordinates.w - coordinates.y) << ") for surface with ID " << surface << "\n"; + } + } + + surfaceCoordinates[surface] = coordinates; + } + + ilm_commitChanges(); + usleep(sleepTimeMillis * 1000); + } +} + +void demoAnimatorZooming(t_scene_data* pInitialScene, t_scene_data* pDemoScene, bool* pStopDemo) +{ + (void) pInitialScene; //suppress warning: unused varaible + + //get rendered surfaces + vector renderedSurfaces = getSceneRenderOrder(pDemoScene); + + //set surface initial states + int screenHorizontalMidpoint = pDemoScene->screenWidth / 2; + int screenVerticalMidpoint = pDemoScene->screenHeight / 2; + + for (vector::iterator it = renderedSurfaces.begin(); + it != renderedSurfaces.end(); ++it) + { + t_ilm_surface surface = *it; + ilmSurfaceProperties& surfaceProperties = pDemoScene->surfaceProperties[surface]; + + surfaceProperties.destX = screenHorizontalMidpoint; + surfaceProperties.destY = screenVerticalMidpoint; + surfaceProperties.destHeight = 0; + surfaceProperties.destWidth = 0; + + ilmErrorTypes callResult = ilm_surfaceSetDestinationRectangle(surface, + surfaceProperties.destX, + surfaceProperties.destY, + surfaceProperties.destWidth, + surfaceProperties.destHeight); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << surfaceProperties.destX << "," << surfaceProperties.destY << ", " << surfaceProperties.destWidth << ", " + << surfaceProperties.destHeight << ") for surface with ID " << surface << "\n"; + } + } + + //sleeping time + long sleepTimeMillis = 25; + + //start animation ! + + t_ilm_float t = 1; + + int currentSurfaceIndex = 0; + + while (!*pStopDemo) + { + t_ilm_surface surface = renderedSurfaces[currentSurfaceIndex]; + + ilmSurfaceProperties& surfaceProperties = pDemoScene->surfaceProperties[surface]; + //if currentsurface dimensions touch the screen: change surface and reset scale factor + if (surfaceProperties.destX == 0 || surfaceProperties.destY == 0) + { + t = 1; + surfaceProperties.destX = screenHorizontalMidpoint; + surfaceProperties.destY = screenVerticalMidpoint; + surfaceProperties.destHeight = 0; + surfaceProperties.destWidth = 0; + + currentSurfaceIndex = (currentSurfaceIndex + 1) % renderedSurfaces.size(); + t_ilm_layer layer = pDemoScene->screenLayers.begin()->second[0]; + + ilmErrorTypes callResult = ilm_surfaceSetDestinationRectangle(surface, + surfaceProperties.destX, + surfaceProperties.destY, + surfaceProperties.destWidth, + surfaceProperties.destHeight); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << surfaceProperties.destX << "," << surfaceProperties.destY << ", " << surfaceProperties.destWidth << ", " + << surfaceProperties.destHeight << ") for surface with ID " << surface << "\n"; + } + + callResult = ilm_layerSetRenderOrder(layer, renderedSurfaces.data() + currentSurfaceIndex, 1); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set render order for layer with ID " << layer << "\n"; + } + } + else + { + t += t * 0.1; + int change = (int) t; + surfaceProperties.destX = max(0, (int) surfaceProperties.destX - change); + surfaceProperties.destY = max(0, (int) surfaceProperties.destY - change); + surfaceProperties.destWidth += 2 * change; + surfaceProperties.destHeight += 2 * change; + + ilmErrorTypes callResult = ilm_surfaceSetDestinationRectangle(surface, + surfaceProperties.destX, + surfaceProperties.destY, + surfaceProperties.destWidth, + surfaceProperties.destHeight); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << surfaceProperties.destX << "," << surfaceProperties.destY << ", " << surfaceProperties.destWidth << ", " + << surfaceProperties.destHeight << ") for surface with ID " << surface << "\n"; + } + + float opacity = 1 / pow(t, 0.4); + + callResult = ilm_surfaceSetOpacity(surface, opacity); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set opacity " << opacity << " for surface with ID " << surface << "\n"; + } + } + + ilm_commitChanges(); + usleep(sleepTimeMillis * 1000); + } +} + +void demoAnimatorCascadedZooming(t_scene_data* pInitialScene, t_scene_data* pDemoScene, bool* pStopDemo) +{ + (void) pInitialScene; //suppress warning: unused varaible + //get rendered surfaces + vector renderedSurfaces = getSceneRenderOrder(pDemoScene); + + map scaleFactors; + //set surface initial states + int screenHorizontalMidpoint = pDemoScene->screenWidth / 2; + int screenVerticalMidpoint = pDemoScene->screenHeight / 2; + + for (std::size_t i = 0; i < renderedSurfaces.size(); ++i) + { + t_ilm_surface surface = renderedSurfaces[i]; + ilmSurfaceProperties& surfaceProperties = pDemoScene->surfaceProperties[surface]; + + surfaceProperties.destX = screenHorizontalMidpoint; + surfaceProperties.destY = screenVerticalMidpoint; + surfaceProperties.destHeight = 0; + surfaceProperties.destWidth = 0; + + ilmErrorTypes callResult = ilm_surfaceSetDestinationRectangle(surface, + surfaceProperties.destX, + surfaceProperties.destY, + surfaceProperties.destWidth, + surfaceProperties.destHeight); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << surfaceProperties.destX << "," << surfaceProperties.destY << ", " << surfaceProperties.destWidth << ", " + << surfaceProperties.destHeight << ") for surface with ID " << surface << "\n"; + } + + scaleFactors[surface] = 1; + } + + //sleeping time + long sleepTimeMillis = 25; + + //start animation ! + + while (!*pStopDemo) + { + for (std::size_t i = 0; i < renderedSurfaces.size(); ++i) + { + t_ilm_surface surface = renderedSurfaces[i]; + ilmSurfaceProperties& surfaceProperties = pDemoScene->surfaceProperties[surface]; + //if currentsurface dimensions touch the screen: change surface and reset scale factor + if (surfaceProperties.destX == 0 || surfaceProperties.destY == 0) + { + scaleFactors[surface] = 1; + + surfaceProperties.destX = screenHorizontalMidpoint; + surfaceProperties.destY = screenVerticalMidpoint; + surfaceProperties.destHeight = 0; + surfaceProperties.destWidth = 0; + + t_ilm_layer layer = pDemoScene->screenLayers.begin()->second[0]; + + ilmErrorTypes callResult = ilm_surfaceSetDestinationRectangle(surface, + surfaceProperties.destX, + surfaceProperties.destY, + surfaceProperties.destWidth, + surfaceProperties.destHeight); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << surfaceProperties.destX << "," << surfaceProperties.destY << ", " << surfaceProperties.destWidth << ", " + << surfaceProperties.destHeight << ") for surface with ID " << surface << "\n"; + } + + ilm_commitChanges(); + //update render order + t_ilm_surface firstSurface = renderedSurfaces[0]; + for (std::size_t j = 1; j < renderedSurfaces.size(); ++j) + { + renderedSurfaces[j - 1] = renderedSurfaces[j]; + } + + renderedSurfaces[renderedSurfaces.size() - 1] = firstSurface; + + callResult = ilm_layerSetRenderOrder(layer, renderedSurfaces.data(), renderedSurfaces.size()); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set render order for layer with ID " << layer << "\n"; + } + + ilm_commitChanges(); + } + else + { + //just some fancy function math that gives a special effect + scaleFactors[surface] = pow(1.2125, i * 0.85 + scaleFactors[surface] * 0.85); + + int change = (int) scaleFactors[surface]; + surfaceProperties.destX = max(0, (int) surfaceProperties.destX - change); + surfaceProperties.destY = max(0, (int) surfaceProperties.destY - change); + surfaceProperties.destWidth += 2 * change; + surfaceProperties.destHeight += 2 * change; + + ilmErrorTypes callResult = ilm_surfaceSetDestinationRectangle(surface, + surfaceProperties.destX, + surfaceProperties.destY, + surfaceProperties.destWidth, + surfaceProperties.destHeight); + + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle (" << surfaceProperties.destX << "," << surfaceProperties.destY << ", " << surfaceProperties.destWidth << ", " + << surfaceProperties.destHeight << ") for surface with ID " << surface << "\n"; + } + + float opacity = 1 / pow(scaleFactors[surface], 0.4f); + + callResult = ilm_surfaceSetOpacity(surface, opacity); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set opacity " << opacity << " for surface with ID " << surface << "\n"; + } + } + } + + ilm_commitChanges(); + usleep(sleepTimeMillis * 1000); + } +} + +static vector animators; + +void* demoThreadCallback(void* param) +{ + //call function on parameters + map* paramMap = (map*) param; + + int* pAnimatorIndex = (int*) paramMap->at("pAnimatorIndex"); + t_pDemoAnimatorFunc animator = animators[*pAnimatorIndex]; + t_scene_data* pInitialScene = (t_scene_data*) paramMap->at("pInitialScene"); + t_scene_data* pDemoScene = (t_scene_data*) paramMap->at("pDemoScene"); + bool* pStopDemo= (bool*) paramMap->at("pStopDemo"); + + //call demo animator function ! + (*animator)(pInitialScene, pDemoScene, pStopDemo); + + return NULL; +} +} //end of anonymous namespace + +void demo(t_ilm_uint mode) +{ + animators.clear(); + animators.push_back(&demoAnimatorZooming); + animators.push_back(&demoAnimatorCascadedZooming); + animators.push_back(&demoAnimatorDownwards); + animators.push_back(&demoAnimatorRandomDirections); + animators.push_back(&demoAnimatorWaterfall); + + //if valid mode entered + if (mode < animators.size()) + { + //capture initial scene + t_scene_data initialScene; + captureSceneData(&initialScene); + + setSceneToRestore(&initialScene); + + //set dummy scene + t_scene_data demoScene = cloneToUniLayerScene(&initialScene); + setScene(&demoScene); + + //start animation + volatile bool stopDemo = false; + + map paramMap; + paramMap["pAnimatorIndex"] = (void*) &mode; + paramMap["pInitialScene"] = (void*) &initialScene; + paramMap["pDemoScene"] = (void*) &demoScene; + paramMap["pStopDemo"] = (void*) &stopDemo; + + pthread_t demoThread; + pthread_create(&demoThread, NULL, demoThreadCallback, (void*) ¶mMap); + + //set initial scene (reset) + cout << "Press ENTER to stop demo..."; + cin.get(); + + cout << "Resetting scene..."; + cout.flush(); + + stopDemo = true; //this variable has to be checked in the animator function !! + pthread_join(demoThread, NULL); + + t_scene_data finalScene; + captureSceneData(&finalScene); + transformScene(&finalScene, &initialScene, 1000, 50); + setScene(&initialScene, true); + cout << "done" << endl; + } + else + { + cerr << "fatal error :" << mode << " is not a valid mode, please choose a mode between " << 0 + << " and " << animators.size() - 1 << endl; + exit(0); + } +} diff --git a/ivi-layermanagement-examples/LayerManagerControl/src/main.cpp b/ivi-layermanagement-examples/LayerManagerControl/src/main.cpp new file mode 100644 index 0000000..8ee0546 --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/src/main.cpp @@ -0,0 +1,51 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ +#include "ExpressionInterpreter.h" +#include +using namespace std; + +int main(int argc, char* argv[]) +{ + ExpressionInterpreter interpreter; + + // create full string of arguments + string userCommand; + + if (argc == 1) + { + userCommand = "help"; + } + else + { + for (int i = 1; i < argc; ++i) + { + userCommand += argv[i]; + userCommand += ' '; + } + userCommand = userCommand.substr(0, userCommand.size() - 1); + } + + // start interpreter + if (CommandSuccess != interpreter.interpretCommand(userCommand)) + { + cerr << "Interpreter error: " << interpreter.getLastError() << endl; + } + + return 0; +} diff --git a/ivi-layermanagement-examples/LayerManagerControl/src/print.cpp b/ivi-layermanagement-examples/LayerManagerControl/src/print.cpp new file mode 100644 index 0000000..a4d079b --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/src/print.cpp @@ -0,0 +1,445 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ + +#include "ilm_client.h" +#include "ilm_control.h" +#include "LMControl.h" + +#include +using std::cout; +using std::cin; +using std::endl; + +#include +using std::dec; +using std::hex; + + +#include +using std::map; + +#include +using std::vector; + + +void printArray(const char* text, unsigned int* array, int count) +{ + cout << count << " " << text << "(s):\n"; + for (int i = 0; i < count; ++i) + { + cout << "- " << text << " " << dec << array[i] << hex << " (0x" + << array[i] << ")" << dec << "\n"; + } +} + +template +void printArray(const char* text, T* array, int count) +{ + cout << count << " " << text << "(s):\n"; + for (int i = 0; i < count; ++i) + { + cout << "- " << text << " " << "[" << array[i] << "]" << "\n"; + } +} + +template +void printVector(const char* text, vector v) +{ + cout << v.size() << " " << text << "(s) Vector:\n"; + for (int i = 0; i < v.size(); ++i) + { + cout << "- " << text << " " << v[i] << endl; + } +} + +template +void printMap(const char* text, std::map m) +{ + cout << m.size() << " " << text << endl; + + for (typename map::iterator it = m.begin(); it != m.end(); ++it) + { + cout << "- " << (*it).first << " -> " << (*it).second << endl; + } +} + +void printScreenProperties(unsigned int screenid, const char* prefix) +{ + cout << prefix << "screen " << screenid << " (0x" << hex << screenid << dec + << ")\n"; + cout << prefix << "---------------------------------------\n"; + + ilmScreenProperties screenProperties; + + ilmErrorTypes callResult = ilm_getPropertiesOfScreen(screenid, &screenProperties); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get properties of screen with ID " << screenid << " found\n"; + return; + } + + cout << prefix << "- resolution: x=" << screenProperties.screenWidth << ", y=" + << screenProperties.screenHeight << "\n"; + + cout << prefix << "- hardware layer count: " << screenProperties.harwareLayerCount << "\n"; + + cout << prefix << "- layer render order: "; + + for (t_ilm_uint layerIndex = 0; layerIndex < screenProperties.layerCount; ++layerIndex) + { + t_ilm_layer layerid = screenProperties.layerIds[layerIndex]; + cout << layerid << "(0x" << hex << layerid << dec << "), "; + } + cout << "\n"; +} + +void printLayerProperties(unsigned int layerid, const char* prefix) +{ + cout << prefix << "layer " << layerid << " (0x" << hex << layerid << dec + << ")\n"; + cout << prefix << "---------------------------------------\n"; + + ilmLayerProperties p; + + ilmErrorTypes callResult = ilm_getPropertiesOfLayer(layerid, &p); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get properties of layer with ID " << layerid << " found\n"; + return; + } + + cout << prefix << "- created by pid: " << p.creatorPid << "\n"; + + cout << prefix << "- original size: x=" << p.origSourceWidth + << ", y=" << p.origSourceHeight << "\n"; + cout << prefix << "- destination region: x=" << p.destX << ", y=" + << p.destY << ", w=" << p.destWidth << ", h=" << p.destHeight + << "\n"; + cout << prefix << "- source region: x=" << p.sourceX << ", y=" + << p.sourceY << ", w=" << p.sourceWidth << ", h=" << p.sourceHeight + << "\n"; + + cout << prefix << "- orientation: " << p.orientation << " ("; + switch (p.orientation) + { + case 0/*Zero*/: + cout << "up is top)\n"; + break; + case 1/*Ninety*/: + cout << "up is right)\n"; + break; + case 2/*OneEighty*/: + cout << "up is bottom)\n"; + break; + case 3/*TwoSeventy*/: + cout << "up is left)\n"; + break; + default: + cout << "unknown)\n"; + break; + } + + cout << prefix << "- opacity: " << p.opacity << "\n"; + cout << prefix << "- visibility: " << p.visibility << "\n"; + + cout << prefix << "- type: " << p.type << " ("; + switch (p.type) + { + case 1/*Hardware*/: + cout << "hardware)\n"; + break; + case 2/*Software_2D*/: + cout << "software 2d)\n"; + break; + case 3/*Software_2_5D*/: + cout << "software 2.5d)\n"; + break; + default: + cout << "unknown)\n"; + break; + } + + cout << prefix << "- chromakey: " + << (p.chromaKeyEnabled ? "enabled " : "disabled ") + << "(r=" << p.chromaKeyRed << ", g=" << p.chromaKeyGreen << ", b=" << p.chromaKeyBlue << ")\n"; + + cout << prefix << "- surface render order: "; + + int surfaceCount = 0; + unsigned int* surfaceArray = NULL; + + callResult = ilm_getSurfaceIDsOnLayer(layerid, &surfaceCount, &surfaceArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get surfaces on layer with ID " << layerid << " \n"; + return; + } + + for (int surfaceIndex = 0; surfaceIndex < surfaceCount; ++surfaceIndex) + { + cout << surfaceArray[surfaceIndex] << "(0x" << hex + << surfaceArray[surfaceIndex] << dec << "), "; + } + cout << "\n"; + + cout << prefix << "- on screen: "; + + unsigned int screenCount = 0; + unsigned int* screenArray = NULL; + + callResult = ilm_getScreenIDs(&screenCount, &screenArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get available screens\n"; + return; + } + + for (unsigned int screenIndex = 0; screenIndex < screenCount; + ++screenIndex) + { + unsigned int screenid = screenArray[screenIndex]; + int layerCount = 0; + unsigned int* layerArray = NULL; + + callResult = ilm_getLayerIDsOnScreen(screenid, &layerCount, &layerArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get available layers on screen with ID" << screenid << "\n"; + return; + } + + for (int layerIndex = 0; layerIndex < layerCount; ++layerIndex) + { + unsigned int id = layerArray[layerIndex]; + if (id == layerid) + { + cout << screenid << "(0x" << hex << screenid << dec << ") "; + } + } + } + cout << "\n"; +} + +void printSurfaceProperties(unsigned int surfaceid, const char* prefix) +{ + cout << prefix << "surface " << surfaceid << " (0x" << hex << surfaceid + << dec << ")\n"; + cout << prefix << "---------------------------------------\n"; + + ilmSurfaceProperties p; + + ilmErrorTypes callResult = ilm_getPropertiesOfSurface(surfaceid, &p); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "No surface with ID " << surfaceid << " found\n"; + return; + } + + cout << prefix << "- created by pid: " << p.creatorPid << "\n"; + + cout << prefix << "- original size: x=" << p.origSourceWidth << ", y=" + << p.origSourceHeight << "\n"; + cout << prefix << "- destination region: x=" << p.destX << ", y=" << p.destY + << ", w=" << p.destWidth << ", h=" << p.destHeight << "\n"; + cout << prefix << "- source region: x=" << p.sourceX << ", y=" + << p.sourceY << ", w=" << p.sourceWidth << ", h=" << p.sourceHeight + << "\n"; + + cout << prefix << "- orientation: " << p.orientation << " ("; + switch (p.orientation) + { + case 0/*Zero*/: + cout << "up is top)\n"; + break; + case 1/*Ninety*/: + cout << "up is right)\n"; + break; + case 2/*OneEighty*/: + cout << "up is bottom)\n"; + break; + case 3/*TwoSeventy*/: + cout << "up is left)\n"; + break; + default: + cout << "unknown)\n"; + break; + } + + cout << prefix << "- opacity: " << p.opacity << "\n"; + cout << prefix << "- visibility: " << p.visibility << "\n"; + + cout << prefix << "- pixel format: " << p.pixelformat << " ("; + switch (p.pixelformat) + { + case 0/*PIXELFORMAT_R8*/: + cout << "R-8)\n"; + break; + case 1/*PIXELFORMAT_RGB888*/: + cout << "RGB-888)\n"; + break; + case 2/*PIXELFORMAT_RGBA8888*/: + cout << "RGBA-8888)\n"; + break; + case 3/*PIXELFORMAT_RGB565*/: + cout << "RGB-565)\n"; + break; + case 4/*PIXELFORMAT_RGBA5551*/: + cout << "RGBA-5551)\n"; + break; + case 5/*PIXELFORMAT_RGBA6661*/: + cout << "RGBA-6661)\n"; + break; + case 6/*PIXELFORMAT_RGBA4444*/: + cout << "RGBA-4444)\n"; + break; + default: + cout << "unknown)\n"; + break; + } + + cout << prefix << "- native surface: " << p.nativeSurface << "\n"; + + cout << prefix << "- accepts input from: " + << (p.inputDevicesAcceptance & ILM_INPUT_DEVICE_KEYBOARD ? "keyboard " : "") + << (p.inputDevicesAcceptance & ILM_INPUT_DEVICE_POINTER ? "mouse " : "") + << (p.inputDevicesAcceptance & ILM_INPUT_DEVICE_TOUCH ? "touch " : "") + << "\n"; + + t_ilm_surface keyboardFocusSurfaceId; + callResult = ilm_GetKeyboardFocusSurfaceId(&keyboardFocusSurfaceId); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get keyboard focus surface ID\n"; + return; + } + + cout << prefix << "- has keyboard focus: " + << (keyboardFocusSurfaceId == surfaceid ? "true" : "false") + << "\n"; + + cout << prefix << "- chromakey: " + << (p.chromaKeyEnabled ? "enabled " : "disabled ") + << "(r=" << p.chromaKeyRed << ", g=" << p.chromaKeyGreen << ", b=" << p.chromaKeyBlue << ")\n"; + + cout << prefix << "- counters: frame=" << p.frameCounter + << ", draw=" << p.drawCounter << ", update=" << p.updateCounter + << "\n"; + + cout << prefix << "- on layer: "; + int layerCount = 0; + unsigned int* layerArray = NULL; + + callResult = ilm_getLayerIDs(&layerCount, &layerArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get available layer IDs\n"; + return; + } + + for (int layerIndex = 0; layerIndex < layerCount; ++layerIndex) + { + unsigned int layerid = layerArray[layerIndex]; + int surfaceCount = 0; + unsigned int* surfaceArray = NULL; + + callResult = ilm_getSurfaceIDsOnLayer(layerid, &surfaceCount, &surfaceArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get surface IDs on layer" << layerid << "\n"; + return; + } + + for (int surfaceIndex = 0; surfaceIndex < surfaceCount; + ++surfaceIndex) + { + unsigned int id = surfaceArray[surfaceIndex]; + if (id == surfaceid) + { + cout << layerid << "(0x" << hex << layerid << dec << ") "; + } + } + } + cout << "\n"; +} + +void printScene() +{ + unsigned int screenCount = 0; + unsigned int* screenArray = NULL; + + ilmErrorTypes callResult = ilm_getScreenIDs(&screenCount, &screenArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get available screen IDs\n"; + return; + } + + for (unsigned int screenIndex = 0; screenIndex < screenCount; ++screenIndex) + { + unsigned int screenid = screenArray[screenIndex]; + printScreenProperties(screenid); + cout << "\n"; + + int layerCount = 0; + unsigned int* layerArray = NULL; + + callResult = ilm_getLayerIDsOnScreen(screenid, &layerCount, &layerArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get available layers on screen with ID" << screenid << "\n"; + return; + } + + for (int layerIndex = 0; layerIndex < layerCount; ++layerIndex) + { + unsigned int layerid = layerArray[layerIndex]; + printLayerProperties(layerid, " "); + cout << "\n"; + + int surfaceCount = 0; + unsigned int* surfaceArray = NULL; + + callResult = ilm_getSurfaceIDsOnLayer(layerid, &surfaceCount, &surfaceArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get available surfaces on layer with ID" << layerid << "\n"; + return; + } + + for (int surfaceIndex = 0; surfaceIndex < surfaceCount; ++surfaceIndex) + { + unsigned int surfaceid = surfaceArray[surfaceIndex]; + printSurfaceProperties(surfaceid, " "); + cout << "\n"; + } + } + } +} diff --git a/ivi-layermanagement-examples/LayerManagerControl/src/scatter.cpp b/ivi-layermanagement-examples/LayerManagerControl/src/scatter.cpp new file mode 100644 index 0000000..be12458 --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/src/scatter.cpp @@ -0,0 +1,344 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ + +#include "ilm_client.h" +#include "ilm_control.h" +#include "LMControl.h" + +#include +using std::max; +using std::min; + +#include + +#include +using std::cout; +using std::cin; +using std::endl; + +#include +using std::dec; +using std::hex; + +#include +using std::stringstream; + +#include +#include + + +t_scene_data getScatteredScene(t_scene_data* pInitialScene) +{ + t_scene_data& initialScene = *pInitialScene; + + //make dummy scene + t_scene_data scatteredScene = cloneToUniLayerScene(&initialScene); + + //get render order of surfaces in initial scene + vector renderOrder = getSceneRenderOrder(&initialScene); + //get surfaces' coordinates of surfaces in initial scene + map coordinates; + + for (vector::iterator it = renderOrder.begin(); + it != renderOrder.end(); ++it) + { + t_ilm_surface surface = *it; + coordinates[surface] = getSurfaceScreenCoordinates(&initialScene, surface); + } + + //scale surfaces' coordinates + //calculate needed proportions + int n = renderOrder.size(); + int rows = ceil(sqrt(n)); + int cols = ceil(sqrt(n)); + float scale = min(1.0 / cols, 1.0 / rows); + int maxWidth = static_cast(initialScene.screenWidth * scale); + int maxHeight = static_cast(initialScene.screenHeight * scale); + + //scale surface coordinates + int counter = 0; + for (vector::iterator it = renderOrder.begin(); + it != renderOrder.end(); ++it) + { + t_ilm_surface surface = *it; + coordinates[surface].scale(scale); + + coordinates[surface].x += maxWidth * (counter % cols); + coordinates[surface].y += maxHeight * (counter / cols); + coordinates[surface].z += maxWidth * (counter % cols); + coordinates[surface].w += maxHeight * (counter / cols); + + scatteredScene.surfaceProperties[surface].destX = coordinates[surface].x; + scatteredScene.surfaceProperties[surface].destY = coordinates[surface].y; + scatteredScene.surfaceProperties[surface].destWidth = coordinates[surface].z - coordinates[surface].x; + scatteredScene.surfaceProperties[surface].destHeight = coordinates[surface].w - coordinates[surface].y; + + ++counter; + } + + return scatteredScene; +} + +void* scatterThreadCallback(void* param) +{ + map * pParamMap = static_cast* >(param); + t_ilm_surface* surface = static_cast((*pParamMap)["pHighlighedSurface"]); + bool* stop = static_cast((*pParamMap)["pStop"]); + while (! *stop) + { + if (*surface != 0xFFFFFFFF) + { + t_ilm_surface currentSurface = *surface; + + ilmErrorTypes callResult = ilm_surfaceSetVisibility(currentSurface, false); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set visibility " << false << " for surface with ID " << currentSurface << "\n"; + } + + ilm_commitChanges(); + usleep(100000); + + callResult = ilm_surfaceSetVisibility(currentSurface, true); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set visibility " << true << " for surface with ID " << currentSurface << "\n"; + } + + ilm_commitChanges(); + } + + usleep(400000); + } + return NULL; +} + +void scatterHandleUserInput(t_scene_data* pOriginalScene, t_scene_data* pScatteredScene) +{ + int n = pScatteredScene->surfaces.size(); + int cols = ceil(sqrt(n)); + + //make keys map + map keySurfaceMap; + + for (int i = 0; i < n; ++i) + { + int row = i / cols; + int col = i % cols; + + char key[3]; + key[0] = 'A' + row; + key[1] = '0' + col; + key[2] = '\0'; + + keySurfaceMap[key] = pScatteredScene->surfaces[i]; + } + + //start thread for making surfaces blink + map threadParamMap; + t_ilm_surface hightlightThreadSurface = 0xFFFFFFFF; + threadParamMap["pHighlighedSurface"] = static_cast (& hightlightThreadSurface); + bool hightlightThreadStop = false; + threadParamMap["pStop"] = static_cast (& hightlightThreadStop); + + pthread_t highlightThread; + pthread_create(& highlightThread, NULL, scatterThreadCallback, &threadParamMap); + + char userInput[3]; + while (true) + { + //print Grid + for (int i = 0; i < n; ++i) + { + int row = i / cols; + int col = i % cols; + + char key[3]; + key[0] = 'A' + row; + key[1] = '0' + col; + key[2] = '\0'; + if (col == 0) + { + cout << endl; + cout << string(5 * cols + 1, '-') << endl; + cout << "| "; + } + cout << key << " | "; + } + + cout << endl << string(5 * cols + 1, '-') << endl; + //take input from user + cout << "Choose a surface [Or press ENTER to exit...]:" << endl; + + userInput[0] = cin.get(); + + //if the user presses Enter: exit + if (userInput[0] == '\n') + { + hightlightThreadStop = true; + pthread_join(highlightThread, NULL); + break; + } + else if (userInput[0] >= 'a' && userInput[0] <='z') + { + //switch to uppercase + userInput[0] += 'A' - 'a'; + } + + userInput[1] = cin.get(); + //if the user presses Enter by mistake: reset input + if (userInput[1] == '\n') + { + continue; + } + + userInput[2] = cin.get(); + //if the user DOES NOT press Enter by mistake: reset input + if (userInput[2] != '\n') + { + continue; + } + + userInput[2] = '\0'; + + if (keySurfaceMap.find(userInput) != keySurfaceMap.end()) + { + //this change is reflected in the highligh thread + hightlightThreadSurface = keySurfaceMap[userInput]; + + //print surface information + ilmSurfaceProperties& surfaceProperties = pOriginalScene->surfaceProperties[hightlightThreadSurface]; + + map orientations; + + orientations[ILM_ZERO] = "0 degrees"; + orientations[ILM_NINETY] = "90 degrees"; + orientations[ILM_ONEHUNDREDEIGHTY]= "180 degrees"; + orientations[ILM_TWOHUNDREDSEVENTY] = "270 degrees"; + + map pixelFormats; + pixelFormats[ILM_PIXELFORMAT_R_8] = "R_8"; + pixelFormats[ILM_PIXELFORMAT_RGB_888] = "RGB_888"; + pixelFormats[ILM_PIXELFORMAT_RGBA_8888] = "RGBA_8888"; + pixelFormats[ILM_PIXELFORMAT_RGB_565] = "RGB_565"; + pixelFormats[ILM_PIXELFORMAT_RGBA_5551] = "RGBA_5551"; + pixelFormats[ILM_PIXELFORMAT_RGBA_6661] = "RGBA_6661"; + pixelFormats[ILM_PIXELFORMAT_RGBA_4444] = "RGBA_4444"; + pixelFormats[ILM_PIXEL_FORMAT_UNKNOWN] = "FORMAT_UNKNOWN"; + + string inputDeviceAcceptance = (surfaceProperties.inputDevicesAcceptance & ILM_INPUT_DEVICE_KEYBOARD ? "Keyboard " : ""); + inputDeviceAcceptance +=(surfaceProperties.inputDevicesAcceptance & ILM_INPUT_DEVICE_POINTER ? "Pointer " : ""); + inputDeviceAcceptance += (surfaceProperties.inputDevicesAcceptance & ILM_INPUT_DEVICE_TOUCH ? "Touch " : ""); + + stringstream tempStream; + char fillChar = '-'; + int width = 20; + tempStream << "\n--------------------------------\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Surface" << ":" << hightlightThreadSurface << "\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Destination Region" << ":" << "[x:" << surfaceProperties.destX << ", y:" << surfaceProperties.destY << ", w:" << surfaceProperties.destWidth << ", h:" << surfaceProperties.destHeight << "]\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Source Region" << ":" << "[x:" << surfaceProperties.sourceX << ", y:" << surfaceProperties.sourceY << ", w:" << surfaceProperties.sourceWidth << ", h:" << surfaceProperties.sourceHeight << "]\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Original Dimensions" << ":" << "[w:" << surfaceProperties.origSourceWidth << ", h:" << surfaceProperties.origSourceHeight << "]\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Opacity" << ":" << surfaceProperties.opacity << "\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Visibility" << ":" << surfaceProperties.visibility << "\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Input Devices" << ":" << surfaceProperties.inputDevicesAcceptance << " [" << inputDeviceAcceptance << "]\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Native Surface" << ":" << surfaceProperties.nativeSurface << "\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Orientation" << ":" << surfaceProperties.orientation << " [" << orientations[surfaceProperties.orientation] << "]\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Pixel Format" << ":" << surfaceProperties.pixelformat << " [" << pixelFormats[surfaceProperties.pixelformat] << "]\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Update Counter" << ":" << surfaceProperties.updateCounter << "\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Draw Counter" << ":" << surfaceProperties.drawCounter << "\n"; + tempStream << std::setw(width) << std::left << std::setfill(fillChar) << "Frame Counter" << ":" << surfaceProperties.frameCounter << "\n"; + + cout << tempStream.str(); + } + else + { + cout << "Unrecognized input!" << endl; + } + } +} + +void scatter() +{ + //capture initial scene + t_scene_data initialScene; + captureSceneData(&initialScene); + + setSceneToRestore(&initialScene); + + //make scattered scene + t_scene_data scatteredScene = getScatteredScene(&initialScene); + + //transform from initial to scattered scene + transformScene(&initialScene, &scatteredScene, 1000, 50); + + //wait for user input + scatterHandleUserInput(&initialScene, &scatteredScene); + + //transform from scattered to initial scene + transformScene(&scatteredScene, &initialScene, 1000, 50); + + //set initial scene (reset!) + setScene(&initialScene, true); +} + +void scatterAll() +{ + //capture initial scene + t_scene_data initialScene; + captureSceneData(&initialScene); + + setSceneToRestore(&initialScene); + + //make a scene in which all surfaces belong to a layer (any layer) + + t_scene_data modifiedScene = cloneToUniLayerScene(&initialScene); + for (vector::iterator it = initialScene.surfaces.begin(); + it != initialScene.surfaces.end(); ++it) + { + t_ilm_surface surface = *it; + + //if surface does not belong to a layer or layer does not belong to a surface + if (! surfaceRenderedOnScreen(initialScene, surface)) + { + modifiedScene.surfaces.push_back(surface); + modifiedScene.surfaceProperties[surface] = initialScene.surfaceProperties[surface]; + modifiedScene.surfaceLayer[surface] = modifiedScene.layers[0]; + modifiedScene.layerSurfaces.begin()->second.push_back(surface); + + tuple4 coords = getSurfaceScreenCoordinates(&initialScene, surface); + + modifiedScene.surfaceProperties[surface].destX = coords.x; + modifiedScene.surfaceProperties[surface].destY = coords.y; + modifiedScene.surfaceProperties[surface].destWidth = coords.z - coords.x; + modifiedScene.surfaceProperties[surface].destHeight = coords.w - coords.y; + } + + modifiedScene.surfaceProperties[surface].opacity = 1; + modifiedScene.surfaceProperties[surface].visibility = true; + } + + //just scatter !! + setScene(&modifiedScene); + scatter(); + + //set initial scene (reset) + setScene(&initialScene, true); +} diff --git a/ivi-layermanagement-examples/LayerManagerControl/src/sceneio.cpp b/ivi-layermanagement-examples/LayerManagerControl/src/sceneio.cpp new file mode 100644 index 0000000..7c15998 --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/src/sceneio.cpp @@ -0,0 +1,775 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ +#include "ilm_client.h" +#include "ilm_control.h" +#include "LMControl.h" +#include "Expression.h" +#include "ExpressionInterpreter.h" +#include "SceneStore.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + + +namespace { +void captureSceneDataHelper(t_ilm_surface surfaceId, t_scene_data* pSceneData, IlmSurface* pSurface) +{ + ilmSurfaceProperties& props = pSceneData->surfaceProperties[surfaceId]; + + pSurface->set("id", surfaceId); + pSurface->set("destHeight", props.destHeight); + pSurface->set("destWidth", props.destWidth); + pSurface->set("destX", props.destX); + pSurface->set("destY", props.destY); + pSurface->set("drawCounter", props.drawCounter); + pSurface->set("frameCounter", props.frameCounter); + pSurface->set("inputDevicesAcceptance", props.inputDevicesAcceptance); + pSurface->set("nativeSurface", props.nativeSurface); + pSurface->set("opacity", props.opacity); + pSurface->set("orientation", props.orientation); + pSurface->set("origSourceHeight", props.origSourceHeight); + pSurface->set("origSourceWidth", props.origSourceWidth); + pSurface->set("pixelformat", props.pixelformat); + pSurface->set("sourceHeight", props.sourceHeight); + pSurface->set("sourceWidth", props.sourceWidth); + pSurface->set("sourceX", props.sourceX); + pSurface->set("sourceY", props.sourceY); + pSurface->set("updateCounter", props.updateCounter); + pSurface->set("visibility", props.visibility); +} + +void captureSceneDataHelper(t_ilm_layer layerId, t_scene_data* pSceneData, IlmLayer* pLayer) +{ + ilmLayerProperties& props = pSceneData->layerProperties[layerId]; + pLayer->set("id", layerId); + pLayer->set("destHeight", props.destHeight); + pLayer->set("destWidth", props.destWidth); + pLayer->set("destX", props.destX); + pLayer->set("destY", props.destY); + pLayer->set("opacity", props.opacity); + pLayer->set("orientation", props.orientation); + pLayer->set("origSourceHeight", props.origSourceHeight); + pLayer->set("origSourceWidth", props.origSourceWidth); + pLayer->set("sourceHeight", props.sourceHeight); + pLayer->set("sourceWidth", props.sourceWidth); + pLayer->set("sourceX", props.sourceX); + pLayer->set("sourceY", props.sourceY); + pLayer->set("type", props.type); + pLayer->set("visibility", props.visibility); + + if (pSceneData->layerSurfaces.find(layerId) != pSceneData->layerSurfaces.end()) + { + for (vector::iterator it = pSceneData->layerSurfaces[layerId].begin(); + it != pSceneData->layerSurfaces[layerId].end(); ++it) + { + IlmSurface* pIlmsurface = new IlmSurface; + captureSceneDataHelper(*it, pSceneData, pIlmsurface); + pLayer->add(pIlmsurface); + } + } +} + +void captureSceneData(IlmScene* scene) +{ + t_scene_data sceneStruct; + captureSceneData(&sceneStruct); + + for (vector::iterator it = sceneStruct.screens.begin(); + it != sceneStruct.screens.end(); ++it) + { + t_ilm_display displayId = *it; + IlmDisplay* pIlmdisplay = new IlmDisplay; + pIlmdisplay->set("id", displayId); + pIlmdisplay->set("width", sceneStruct.screenWidth); + pIlmdisplay->set("height", sceneStruct.screenHeight); + + if (sceneStruct.screenLayers.find(displayId) != sceneStruct.screenLayers.end()) + { + for (vector::iterator it = sceneStruct.screenLayers[displayId].begin(); + it != sceneStruct.screenLayers[displayId].end(); ++it) + { + IlmLayer* pIlmlayer = new IlmLayer; + captureSceneDataHelper(*it, &sceneStruct, pIlmlayer); + + pIlmdisplay->add(pIlmlayer); + } + } + + scene->add(pIlmdisplay); + } + + for (vector::iterator it = sceneStruct.layers.begin(); + it != sceneStruct.layers.end(); ++it) + { + if (sceneStruct.layerScreen.find(*it) == sceneStruct.layerScreen.end()) + { + IlmLayer* pIlmlayer = new IlmLayer; + captureSceneDataHelper(*it, &sceneStruct, pIlmlayer); + + scene->add(pIlmlayer); + } + } + + for (vector::iterator it = sceneStruct.surfaces.begin(); + it != sceneStruct.surfaces.end(); ++it) + { + if (sceneStruct.surfaceLayer.find(*it) == sceneStruct.surfaceLayer.end()) + { + IlmSurface* pIlmsurface = new IlmSurface; + captureSceneDataHelper(*it, &sceneStruct, pIlmsurface); + + scene->add(pIlmsurface); + } + } +} + +string encodeEscapesequences(string s) +{ + map code; + + code["\\"] = "\\[\\]"; + code["\n"] = "\\[n]"; + code["\t"] = "\\[t]"; + code["\v"] = "\\[v]"; + code["\b"] = "\\[b]"; + code["\f"] = "\\[f]"; + code["\r"] = "\\[r]"; + + return replaceAll(s, code); +} + +void exportSceneToTXTHelper(ostream& stream, StringMapTree* tree, string prefix = "") +{ + stream << prefix << encodeEscapesequences(tree->mNodeLabel) + ":{\n"; + for (map >::iterator it = tree->mNodeValues.begin(); + it != tree->mNodeValues.end(); ++it) + { + stream << prefix + "\t[" + encodeEscapesequences(it->first) + ":" + + encodeEscapesequences(it->second.first) + "]=[" + + encodeEscapesequences(it->second.second) + "]\n"; + } + + for (list::iterator it = tree->mChildren.begin(); it != tree->mChildren.end(); ++it) + { + exportSceneToTXTHelper(stream, *it, prefix + "\t"); + stream << "\n"; + } + + stream << prefix + "}"; +} + +string decodeEscapesequences(string s) +{ + map code; + code["\\[\\]"] = "\\"; + code["\\[n]"] = "\n"; + code["\\[t]"] = "\t"; + code["\\[v]"] = "\v"; + code["\\[b]"] = "\b"; + code["\\[f]"] = "\f"; + code["\\[r]"] = "\r"; + return replaceAll(s, code); +} + +void importSceneFromTXTHelper(istream& stream, StringMapTree* node) +{ + string in; + //Type + getline(stream, in); + int typeSize = in.find(":") - in.find_first_not_of('\t'); + int typeStart = in.find_first_not_of('\t'); + node->mNodeLabel = in.substr(typeStart, typeSize); + while (true) + { + long streamPosition = stream.tellg(); + getline(stream, in); + in = rtrim(in); + + //end of object + if (in.substr(0, 1) == "}") + return; + + //start of object property + if (in.substr(0, 1) == "[") + { + int startIndex = in.find('[') + 1; + int endIndex = in.find(":"); + string propertyName = in.substr(startIndex, endIndex - startIndex); + propertyName = decodeEscapesequences(propertyName); + + startIndex = endIndex + 1; + endIndex = in.find("]"); + string propertyType = in.substr(startIndex, endIndex - startIndex); + propertyType = decodeEscapesequences(propertyType); + + startIndex = in.find('[', endIndex) + 1; + endIndex = in.find_last_of(']', startIndex) - 1; + string propertyValue = in.substr(startIndex, endIndex - startIndex); + propertyValue = decodeEscapesequences(propertyValue); + + node->mNodeValues[propertyName] = make_pair(propertyType, propertyValue); + } + else + { + stream.seekg(streamPosition); + StringMapTree* child = new StringMapTree; + node->mChildren.push_back(child); + importSceneFromTXTHelper(stream, child); + } + } +} + +string makeValidXMLCharacters(string s) +{ + map code; + code["<"] = "<"; + code[">"] = ">"; + code["&"] = "&"; + code["\'"] = "'"; + code["\""] = """; + return replaceAll(s, code); +} + +void exportSceneToXMLHelper(ostream& stream, StringMapTree* tree, string prefix = "") +{ + stream << prefix << "<" << tree->mNodeLabel << ">\n"; + + for (map >::iterator it = tree->mNodeValues.begin(); + it != tree->mNodeValues.end(); ++it) + { + stream << prefix + "\tfirst << "\" type=\"" << it->second.first << "\">\n"; + stream << prefix << "\t\t" << makeValidXMLCharacters(it->second.second) + "\n"; + stream << prefix << "\t\n"; + } + + for (list::iterator it = tree->mChildren.begin(); + it != tree->mChildren.end(); ++it) + { + exportSceneToXMLHelper(stream, *it, prefix + "\t"); + stream << "\n"; + } + + stream << prefix << "mNodeLabel << ">\n"; +} + +ilmPixelFormat toPixelFormat(t_ilm_int format) +{ + switch (format) + { + case 0: + return ILM_PIXELFORMAT_R_8; + case 1: + return ILM_PIXELFORMAT_RGB_888; + case 2: + return ILM_PIXELFORMAT_RGBA_8888; + case 3: + return ILM_PIXELFORMAT_RGB_565; + case 4: + return ILM_PIXELFORMAT_RGBA_5551; + case 5: + return ILM_PIXELFORMAT_RGBA_6661; + case 6: + return ILM_PIXELFORMAT_RGBA_4444; + } + + return ILM_PIXEL_FORMAT_UNKNOWN; +} + +ilmSurfaceProperties getSurfaceProperties(IlmSurface* pIlmsurface) +{ + ilmSurfaceProperties props; + + pIlmsurface->get("destHeight", &(props.destHeight)); + pIlmsurface->get("destWidth", &(props.destWidth)); + pIlmsurface->get("destX", &(props.destX)); + pIlmsurface->get("destY", &(props.destY)); + pIlmsurface->get("drawCounter", &(props.drawCounter)); + pIlmsurface->get("frameCounter", &(props.frameCounter)); + pIlmsurface->get("inputDevicesAcceptance", &(props.inputDevicesAcceptance)); + pIlmsurface->get("nativeSurface", &(props.nativeSurface)); + pIlmsurface->get("opacity", &(props.opacity)); + pIlmsurface->get("orientation", &(props.orientation)); + pIlmsurface->get("origSourceHeight", &(props.origSourceHeight)); + pIlmsurface->get("origSourceWidth", &(props.origSourceWidth)); + pIlmsurface->get("pixelformat", &(props.pixelformat)); + pIlmsurface->get("sourceHeight", &(props.sourceHeight)); + pIlmsurface->get("sourceWidth", &(props.sourceWidth)); + pIlmsurface->get("sourceX", &(props.sourceX)); + pIlmsurface->get("sourceY", &(props.sourceY)); + pIlmsurface->get("updateCounter", &(props.updateCounter)); + pIlmsurface->get("visibility", &(props.visibility)); + + return props; +} + +ilmLayerProperties getLayerProperties(IlmLayer* pIlmlayer) +{ + ilmLayerProperties props; + pIlmlayer->get("destHeight", &(props.destHeight)); + pIlmlayer->get("destWidth", &(props.destWidth)); + pIlmlayer->get("destX", &(props.destX)); + pIlmlayer->get("destY", &(props.destY)); + pIlmlayer->get("opacity", &(props.opacity)); + pIlmlayer->get("orientation", &(props.orientation)); + pIlmlayer->get("origSourceHeight", &(props.origSourceHeight)); + pIlmlayer->get("origSourceHeight", &(props.origSourceHeight)); + pIlmlayer->get("origSourceWidth", &(props.origSourceWidth)); + pIlmlayer->get("sourceHeight", &(props.sourceHeight)); + pIlmlayer->get("sourceWidth", &(props.sourceWidth)); + pIlmlayer->get("sourceX", &(props.sourceX)); + pIlmlayer->get("sourceY", &(props.sourceY)); + pIlmlayer->get("type", &(props.type)); + pIlmlayer->get("visibility", &(props.visibility)); + + return props; +} + +void createSceneContentsHelper(IlmSurface* pIlmsurface) +{ + t_ilm_surface surfaceId; + pIlmsurface->get("id", &surfaceId); + ilmSurfaceProperties props = getSurfaceProperties(pIlmsurface); + + //if surface does not exist: create it + t_ilm_int surfaceCount; + t_ilm_surface* surfaceArray; + + ilmErrorTypes callResult = ilm_getSurfaceIDs(&surfaceCount, &surfaceArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get available surface IDs\n"; + } + + if (find(surfaceArray, surfaceArray + surfaceCount, surfaceId) + == surfaceArray + surfaceCount) + { + ilmPixelFormat pixelFormat; + pixelFormat = toPixelFormat(props.pixelformat); + + ilmErrorTypes callResult = ilm_surfaceCreate(props.nativeSurface, props.origSourceWidth, + props.origSourceHeight, pixelFormat, &surfaceId); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to create surface\n"; + } + } +} + +void createSceneContentsHelper(IlmLayer* pIlmlayer) +{ + t_ilm_layer layerId; + pIlmlayer->get("id", &layerId); + + ilmLayerProperties props = getLayerProperties(pIlmlayer); + + //create layer if does not exist + t_ilm_int layerCount; + t_ilm_layer* layerArray; + + ilmErrorTypes callResult = ilm_getLayerIDs(&layerCount, &layerArray); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to get available layer IDs\n"; + } + + if (find(layerArray, layerArray + layerCount, layerId) == layerArray + layerCount) + { + ilmErrorTypes callResult = ilm_layerCreateWithDimension(&layerId, props.origSourceWidth, props.origSourceHeight); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to create layer width dimensions (" << props.origSourceWidth << " ," << props.origSourceHeight << ")\n"; + } + + ilm_commitChanges(); + } + + list surfaceList; + pIlmlayer->get(&surfaceList); + vector renderOrder; + for (list::iterator it = surfaceList.begin(); + it != surfaceList.end(); ++it) + { + t_ilm_surface surfaceId; + (*it)->get("id", &surfaceId); + renderOrder.push_back(surfaceId); + + createSceneContentsHelper(*it); + } + + callResult = ilm_layerSetRenderOrder(layerId, renderOrder.data(), renderOrder.size()); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set render order for layer with ID " << layerId << ")\n"; + } + + ilm_commitChanges(); +} + +void createSceneContentsHelper(IlmDisplay* pIlmdisplay) +{ + t_ilm_display displayId = 0xFFFFFFFF; + pIlmdisplay->get("id", &displayId); + + list layerList; + pIlmdisplay->get(&layerList); + vector renderOrder; + for (list::iterator it = layerList.begin(); it != layerList.end(); ++it) + { + t_ilm_layer layerId; + (*it)->get("id", &layerId); + renderOrder.push_back(layerId); + + createSceneContentsHelper(*it); + ilm_commitChanges(); + } + + ilmErrorTypes callResult = ilm_displaySetRenderOrder(displayId, renderOrder.data(), renderOrder.size()); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set render order for display with ID " << displayId << ")\n"; + } + + ilm_commitChanges(); +} + +void createSceneContents(IlmScene* pIlmscene) +{ + list surfaceList; + pIlmscene->get(&surfaceList); + for (list::iterator it = surfaceList.begin(); it != surfaceList.end(); ++it) + { + createSceneContentsHelper(*it); + } + + list layerList; + pIlmscene->get(&layerList); + for (list::iterator it = layerList.begin(); + it != layerList.end(); ++it) + { + createSceneContentsHelper(*it); + } + + list displayList; + pIlmscene->get(&displayList); + for (list::iterator it = displayList.begin(); it != displayList.end(); ++it) + { + createSceneContentsHelper(*it); + } +} + +void restoreSceneHelper(IlmSurface* pIlmsurface) +{ + t_ilm_surface surfaceId = 0xFFFFFFFF; + pIlmsurface->get("id", &surfaceId); + ilmSurfaceProperties props = getSurfaceProperties(pIlmsurface); + + ilmPixelFormat pixelFormat; + pixelFormat = toPixelFormat(props.pixelformat); + + ilmErrorTypes callResult = ilm_surfaceSetNativeContent(props.nativeSurface, props.origSourceWidth, + props.origSourceHeight, pixelFormat, surfaceId); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set native content for surface with ID " << surfaceId << ")\n"; + } + + ilm_commitChanges(); + + ilm_surfaceSetOpacity(surfaceId, props.opacity); + ilm_commitChanges(); + ilm_surfaceSetOrientation(surfaceId, props.orientation); + ilm_commitChanges(); + ilm_surfaceSetSourceRectangle(surfaceId, props.sourceX, props.sourceY, props.sourceWidth, props.sourceHeight); + ilm_commitChanges(); + ilm_surfaceSetDestinationRectangle(surfaceId, props.destX, props.destY, props.destWidth, props.destHeight); + ilm_commitChanges(); + ilm_surfaceSetVisibility(surfaceId, props.visibility); + ilm_commitChanges(); +} + +void restoreSceneHelper(IlmLayer* pIlmlayer) +{ + t_ilm_layer layerId = 0xFFFFFFFF; + pIlmlayer->get("id", &layerId); + + ilmLayerProperties props = getLayerProperties(pIlmlayer); + + //set layer properties + ilmErrorTypes callResult = ilm_layerSetDestinationRectangle(layerId, props.destX, props.destY, props.destWidth, props.destHeight); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to set destination rectangle for layer with ID " << layerId << ")\n"; + } + + ilm_commitChanges(); + ilm_layerSetOpacity(layerId, props.opacity); + ilm_commitChanges(); + ilm_layerSetOrientation(layerId, props.orientation); + ilm_commitChanges(); + ilm_layerSetSourceRectangle(layerId, props.sourceX, props.sourceY, props.sourceWidth, props.sourceHeight); + ilm_commitChanges(); + ilm_layerSetVisibility(layerId, props.visibility); + ilm_commitChanges(); + + list surfaceList; + pIlmlayer->get(&surfaceList); + + //restore surfaces + for (list::iterator it = surfaceList.begin(); it != surfaceList.end(); ++it) + { + t_ilm_surface surfaceId; + (*it)->get("id", &surfaceId); + + restoreSceneHelper(*it); + } +} + +void restoreSceneHelper(IlmDisplay* pIlmdisplay) +{ + t_ilm_display displayId; + pIlmdisplay->get("id", &displayId); + + list layerList; + pIlmdisplay->get(&layerList); + for (list::iterator it = layerList.begin(); it != layerList.end(); ++it) + { + t_ilm_layer layerId; + (*it)->get("id", &layerId); + + restoreSceneHelper(*it); + ilm_commitChanges(); + } +} + +void restoreScene(IlmScene* pIlmscene) +{ + t_scene_data currentScene; + captureSceneData(¤tScene); + + //remove all surfaces from all layers + for (map::iterator it = currentScene.surfaceLayer.begin(); + it != currentScene.surfaceLayer.end(); ++it) + { + ilmErrorTypes callResult = ilm_layerRemoveSurface(it->second, it->first); + if (ILM_SUCCESS != callResult) + { + cout << "LayerManagerService returned: " << ILM_ERROR_STRING(callResult) << "\n"; + cout << "Failed to remove surface " << it->first << " from layer " << it->second << ")\n"; + } + } + + ilm_commitChanges(); + + //create scene contents + createSceneContents(pIlmscene); + + //restore scene + list surfaceList; + pIlmscene->get(&surfaceList); + for (list::iterator it = surfaceList.begin(); it != surfaceList.end(); ++it) + { + restoreSceneHelper(*it); + } + + list layerList; + pIlmscene->get(&layerList); + for (list::iterator it = layerList.begin(); it != layerList.end(); ++it) + { + restoreSceneHelper(*it); + } + + list displayList; + pIlmscene->get(&displayList); + for (list::iterator it = displayList.begin(); it != displayList.end(); ++it) + { + restoreSceneHelper(*it); + } +} +} //end of anonymous namespace + + +void exportSceneToFile(string filename) +{ + IlmScene ilmscene; + IlmScene* pScene = &ilmscene; + captureSceneData(&ilmscene); + stringstream buffer; + StringMapTree sceneTree; + pScene->toStringMapTree(&sceneTree); + + //check extension + if (filename.find_last_of(".") != string::npos) + { + string extension = filename.substr(filename.find_last_of(".")); + cout << extension << endl; + + if (extension == ".xml") + { + buffer << "<\?xml version=\"1.0\"\?>\n"; + exportSceneToXMLHelper(buffer, &sceneTree); + cout << "DONE WRITING XML" << endl; + } + else if (extension == ".txt") + { + exportSceneToTXTHelper(buffer, &sceneTree); + cout << "DONE WRITING TXT" << endl; + } + } + else + { + //defult: + exportSceneToTXTHelper(buffer, &sceneTree); + cout << "DONE WRITING TXT" << endl; + } + + fstream stream(filename.c_str(), ios::out); + + cout << buffer.str() << endl; + stream << buffer.str(); + stream.flush(); + stream.close(); +} + +void importSceneFromFile(string filename) +{ + IlmScene ilmscene; + IlmScene* pScene = &ilmscene; + + fstream stream(filename.c_str(), ios::in); + + StringMapTree sceneTree; + + //check extension + if (filename.find_last_of(".") != string::npos) + { + string extension = filename.substr(filename.find_last_of(".")); + cout << extension << endl; + + if (extension == ".xml") + { +// importSceneFromXMLHelper(stream, &sceneTree); + cout << "READING XML IS NOT SUPPORTED YET" << endl; + } + else if (extension == ".txt") + { + importSceneFromTXTHelper(stream, &sceneTree); + cout << "DONE READING TXT" << endl; + } + } + else + { + //defult behavior: assume txt + importSceneFromTXTHelper(stream, &sceneTree); + cout << "DONE READING TXT" << endl; + } + + stream.close(); + + cout << "Scene Tree :[" << sceneTree.toString() << "]" << endl; + pScene->fromStringMapTree(&sceneTree); + cout << "Scene successfully created from tree" << endl; + + restoreScene(pScene); + cout << "Scene restored successfully" << endl; +} + +void exportXtext(string fileName, string grammar, string url) +{ + string name = grammar.substr(grammar.find_last_of('.') + 1); + //make sure first character is lower case + std::transform(name.begin(), ++(name.begin()), name.begin(), ::tolower); + + IlmScene scene; + StringMapTree grammarTree; + scene.toGrammarMapTree(&grammarTree); + + //writing to file + stringstream buffer; + buffer << "grammar " << grammar << " with org.eclipse.xtext.common.Terminals" << endl; + buffer << "generate " << name << " \"" << url << "\"" << endl; + + list doneTypes; + list waitingNodes; + waitingNodes.push_back(&grammarTree); + while (!waitingNodes.empty()) + { + //pop first element of the waiting types + StringMapTree* typeNode = *(waitingNodes.begin()); + waitingNodes.pop_front(); + string type = typeNode->mNodeLabel; + + //if the type was not printed before + if (find(doneTypes.begin(), doneTypes.end(), type) == doneTypes.end()) + { + doneTypes.push_back(type); + + buffer << type << ":" << endl; + buffer << "\t\'" << type << ":\'" << endl; + + for (map >::iterator it = typeNode->mNodeValues.begin(); + it != typeNode->mNodeValues.end(); ++it) + { + buffer << "\t\t\'" << it->first << ":\' " << it->first << "=" << + (it->second.second.size() > 0 ? it->second.second : it->second.first) << endl; + } + + for (list::iterator it = typeNode->mChildren.begin(); + it != typeNode->mChildren.end(); ++it) + { + waitingNodes.push_back(*it); + string childName = (*it)->mNodeLabel; + //make lower case + std::transform(childName.begin(), childName.end(), childName.begin(), ::tolower); + childName += "s"; + + buffer << "\t\t" << childName << "+=" << (*it)->mNodeLabel << "*" << endl; + } + + buffer << ";" << endl; + } + } + + cout << "Xtext:[" << buffer.str() << "]" << endl; + + fstream fileout(fileName.c_str(), ios::out); + fileout << buffer.str(); + fileout.flush(); + fileout.close(); +} diff --git a/ivi-layermanagement-examples/LayerManagerControl/src/util.cpp b/ivi-layermanagement-examples/LayerManagerControl/src/util.cpp new file mode 100644 index 0000000..a48acbb --- /dev/null +++ b/ivi-layermanagement-examples/LayerManagerControl/src/util.cpp @@ -0,0 +1,117 @@ +/*************************************************************************** + * + * Copyright 2012 BMW Car IT GmbH + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************/ + +#include "ilm_client.h" +#include "LMControl.h" + +#include +using std::min; +using std::max; +using std::pow; + +t_ilm_bool inside(tuple4 A, tuple4 B) +{ + if (A.x >= B.x && A.y >= B.y && A.w <= B.w && A.z <= B.z) + return ILM_TRUE; + return ILM_FALSE; +} + +t_ilm_bool between(int b1, int a, int b2) +{ + return a >= b1 && a <= b2; +} + +t_ilm_bool intersect(tuple4 A, tuple4 B) +{ + if (between(B.x, A.x, B.z) || between(B.x, A.z, B.z)) + { + if (between(A.y, B.y, A.w) || between(A.y, B.w, A.w)) + return ILM_TRUE; + } + + if (between(B.y, A.y, B.w) || between(B.y, A.w, B.w)) + { + if (between(A.x, B.x, A.z) || between(A.x, B.z, A.z)) + return ILM_TRUE; + } + return inside(A, B) || inside(B, A); +} + +string rtrim(string s) +{ + s = s.substr(s.find_first_not_of(" \r\n\t\b")); + return s; +} + +string replaceAll(string s, string a, string b) +{ + std::size_t index = -b.size(); + + while ((index = s.find(a, index + b.size())) != string::npos) + { + s.replace(index, a.size(), b); + } + + return s; +} + +string replaceAll(string s, map replacements) +{ + for (std::size_t i = 0; i < s.size(); ++i) + { + for (map::iterator it = replacements.begin(); it != replacements.end(); ++it) + { + if (s.size() - i >= it->first.size() && s.substr(i, it->first.size()) == it->first) + { + s.replace(i, it->first.size(), it->second); + i += it->second.size(); + } + } + } + + return s; +} + +string replaceAll(string s, char a, char b) +{ + std::size_t index = -1; + while ((index = s.find(a, index + 1)) != string::npos) + { + s.replace(index, 1, string(1, b)); + } + + return s; +} + +set split(string s, char d) +{ + set result; + std::size_t start = 0; + + while (start < s.size() && start != string::npos) + { + start = s.find_first_not_of(d, start); + std::size_t end = s.find(d, start); + result.insert(s.substr(start, end - start)); + + start = end; + } + + return result; +} -- cgit v1.2.1