From 46f30152c6fe84b7fadc5647b242e7843ae35127 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sun, 5 Mar 2017 22:06:11 +0100 Subject: [PATCH] [DEBUG] X11 correction of the display start error (multi-threading error) --- gale/context/Android/Context.cpp | 1 + gale/context/Context.cpp | 28 +++++++++----- gale/context/Context.hpp | 5 +++ gale/context/IOs/Context.cpp | 1 + gale/context/MacOs/Context.mm | 1 + gale/context/SDL/Context.cpp | 1 + gale/context/Windows/Context.cpp | 1 + gale/context/X11/Context.cpp | 59 +++++++++++++++++++++++++++-- gale/context/simulation/Context.cpp | 1 + gale/context/wayland/Context.cpp | 1 + 10 files changed, 86 insertions(+), 13 deletions(-) diff --git a/gale/context/Android/Context.cpp b/gale/context/Android/Context.cpp index bdf89de..7b9f5b2 100644 --- a/gale/context/Android/Context.cpp +++ b/gale/context/Android/Context.cpp @@ -202,6 +202,7 @@ class AndroidContext : public gale::Context { GALE_CRITICAL(" mission one function ==> system can not work withut it..."); } } + start2ntThreadProcessing(); } ~AndroidContext() { diff --git a/gale/context/Context.cpp b/gale/context/Context.cpp index 3cf534f..edd24be 100644 --- a/gale/context/Context.cpp +++ b/gale/context/Context.cpp @@ -258,8 +258,6 @@ gale::Context::Context(gale::Application* _application, int32_t _argc, const cha lockContext(); // create thread to manage real periodic event m_periodicThread = ememory::makeShared(this); - m_periodicThread->start(); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); // By default we set 2 themes (1 color and 1 shape ...) : etk::theme::setNameDefault("GUI", "shape/square/"); @@ -350,6 +348,14 @@ gale::Context::Context(gale::Application* _application, int32_t _argc, const cha m_simulationFile.filePuts("\n"); } } + #if defined(__GALE_ANDROID_ORIENTATION_LANDSCAPE__) + forceOrientation(gale::orientation::screenLandscape); + #elif defined(__GALE_ANDROID_ORIENTATION_PORTRAIT__) + forceOrientation(gale::orientation::screenPortrait); + #else + forceOrientation(gale::orientation::screenAuto); + #endif + m_msgSystem.post([](gale::Context& _context){ ememory::SharedPtr appl = _context.getApplication(); if (appl == nullptr) { @@ -362,18 +368,20 @@ gale::Context::Context(gale::Application* _application, int32_t _argc, const cha // force a recalculation requestUpdateSize(); - #if defined(__GALE_ANDROID_ORIENTATION_LANDSCAPE__) - forceOrientation(gale::orientation::screenLandscape); - #elif defined(__GALE_ANDROID_ORIENTATION_PORTRAIT__) - forceOrientation(gale::orientation::screenPortrait); - #else - forceOrientation(gale::orientation::screenAuto); - #endif // release the curent interface : unLockContext(); GALE_INFO(" == > Gale system init (END)"); } +void gale::Context::start2ntThreadProcessing() { + // set the curent interface: + lockContext(); + m_periodicThread->start(); + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + // release the curent interface: + unLockContext(); +} + void gale::Context::postAction(std::function _action) { std::unique_lock lock(m_mutex); m_msgSystem.post(_action); @@ -384,7 +392,7 @@ gale::Context::~Context() { m_periodicThread->stop(); getResourcesManager().applicationExiting(); // TODO : Clean the message list ... - // set the curent interface : + // set the curent interface: lockContext(); // clean all widget and sub widget with their resources: //m_objectManager.cleanInternalRemoved(); diff --git a/gale/context/Context.hpp b/gale/context/Context.hpp index d16442e..00f3f6c 100644 --- a/gale/context/Context.hpp +++ b/gale/context/Context.hpp @@ -53,6 +53,11 @@ namespace gale { public: Context(gale::Application* _application, int32_t _argc=0, const char* _argv[]=nullptr); virtual ~Context(); + /** + * @brief StartProcessing (2nd thread). + * @note to call when all the Context is started + */ + void start2ntThreadProcessing(); protected: /** * @brief set the curent interface. diff --git a/gale/context/IOs/Context.cpp b/gale/context/IOs/Context.cpp index c69d2c0..706e14a 100644 --- a/gale/context/IOs/Context.cpp +++ b/gale/context/IOs/Context.cpp @@ -35,6 +35,7 @@ public: MacOSInterface(gale::Application* _application, int32_t _argc, const char* _argv[]) : gale::Context(_application, _argc, _argv) { // nothing to do ... + start2ntThreadProcessing(); } int32_t Run() { diff --git a/gale/context/MacOs/Context.mm b/gale/context/MacOs/Context.mm index 208bbcd..1ab9572 100644 --- a/gale/context/MacOs/Context.mm +++ b/gale/context/MacOs/Context.mm @@ -38,6 +38,7 @@ class MacOSInterface : public gale::Context { MacOSInterface(gale::Application* _application, int _argc, const char* _argv[]) : gale::Context(_application, _argc, _argv) { mm_main(_argc, _argv); + start2ntThreadProcessing(); } int32_t Run() { diff --git a/gale/context/SDL/Context.cpp b/gale/context/SDL/Context.cpp index f1044e1..ca1b8d1 100644 --- a/gale/context/SDL/Context.cpp +++ b/gale/context/SDL/Context.cpp @@ -96,6 +96,7 @@ class GLUTInterface : public gale::Context { GALE_INFO("monitor property : dpi=" << dpi << " px/inch"); } m_run = true; + start2ntThreadProcessing(); } ~GLUTInterface() { diff --git a/gale/context/Windows/Context.cpp b/gale/context/Windows/Context.cpp index 669fbb3..87483d4 100644 --- a/gale/context/Windows/Context.cpp +++ b/gale/context/Windows/Context.cpp @@ -71,6 +71,7 @@ class WindowsContext : public gale::Context { m_inputIsPressed[iii] = false; } configure(); + start2ntThreadProcessing(); } ~WindowsContext() { diff --git a/gale/context/X11/Context.cpp b/gale/context/X11/Context.cpp index bc1775a..83c6df4 100644 --- a/gale/context/X11/Context.cpp +++ b/gale/context/X11/Context.cpp @@ -52,11 +52,36 @@ static bool hasDisplay = false; #define X11_VERBOSE GALE_VERBOSE #define X11_INFO GALE_INFO #define X11_CRITICAL GALE_CRITICAL + static int32_t callLevel = 0; + std::string getOffset(int32_t _size) { + std::string out; + for (int32_t iii=0; iii<_size; ++iii) { + out += "...."; + } + return out; + } + class GaleTmpFuncCall { + private: + std::string m_value; + int32_t m_level; + public: + GaleTmpFuncCall(const std::string& _value) : + m_value(_value), + m_level(callLevel++) { + GALE_INFO(getOffset(m_level) << " ==> " << m_value); + } + ~GaleTmpFuncCall() { + GALE_INFO(getOffset(m_level) << "<== " << m_value); + callLevel--; + } + }; + #define X11_FUNC() GaleTmpFuncCall tmpValueCallqsdfqsdfqsdfqsdfqsdfqsdfqsdfqsdfqsdf(__PRETTY_FUNCTION__) #else #define X11_DEBUG GALE_VERBOSE #define X11_VERBOSE GALE_VERBOSE #define X11_INFO GALE_VERBOSE #define X11_CRITICAL GALE_VERBOSE + #define X11_FUNC() do {} while(false) #endif #if !defined(__TARGET_OS__Web) @@ -170,6 +195,7 @@ class X11Interface : public gale::Context { XAtomDeleteWindows(0), m_currentCursor(gale::context::cursor::arrow), m_lastKeyPressed(0) { + X11_FUNC(); X11_INFO("X11:INIT"); for (int32_t iii=0; iiivisual, AllocNone); + if ( m_display != nullptr + && m_visual != nullptr) { + attr.colormap = XCreateColormap(m_display, Xroot, m_visual->visual, AllocNone); + } #endif attr.border_pixel = 0; @@ -1204,6 +1246,7 @@ class X11Interface : public gale::Context { } /****************************************************************************************/ void setIcon(const std::string& _inputFile) { + X11_FUNC(); #if defined(GALE_BUILD_EGAMI) \ && !defined(__TARGET_OS__Web) egami::Image dataImage = egami::load(_inputFile); @@ -1272,7 +1315,11 @@ class X11Interface : public gale::Context { default: return; } - + if ( m_display == nullptr + || m_visual == nullptr) { + GALE_ERROR("X11 Can not create Image Icon ==> nullptr on m_display or m_visual"); + return; + } XImage* myImage = XCreateImage(m_display, m_visual->visual, depth, @@ -1354,6 +1401,7 @@ class X11Interface : public gale::Context { } /****************************************************************************************/ static void setVSync(bool _sync) { + X11_FUNC(); // Function pointer for the wgl extention function we need to enable/disable typedef int32_t (APIENTRY *PFNWGLSWAPINTERVALPROC)( int ); PFNWGLSWAPINTERVALPROC wglSwapIntervalEXT = 0; @@ -1376,6 +1424,7 @@ class X11Interface : public gale::Context { } /****************************************************************************************/ bool createOGlContext() { + X11_FUNC(); X11_INFO("X11:CreateOGlContext"); #if defined(__TARGET_OS__Web) /* create a GLX context */ @@ -1404,6 +1453,7 @@ class X11Interface : public gale::Context { } /****************************************************************************************/ void setTitle(const std::string& _title) { + X11_FUNC(); X11_INFO("X11: set Title (START)"); XTextProperty tp; tp.value = (unsigned char *)_title.c_str(); @@ -1417,6 +1467,7 @@ class X11Interface : public gale::Context { X11_INFO("X11: set Title (END)"); } void openURL(const std::string& _url) { + X11_FUNC(); std::string req = "xdg-open "; req += _url; system(req.c_str()); @@ -1424,6 +1475,7 @@ class X11Interface : public gale::Context { } /****************************************************************************************/ void clipBoardGet(enum gale::context::clipBoard::clipboardListe _clipboardID) { + X11_FUNC(); switch (_clipboardID) { case gale::context::clipBoard::clipboardSelection: if (m_clipBoardOwnerPrimary == false) { @@ -1462,6 +1514,7 @@ class X11Interface : public gale::Context { } /****************************************************************************************/ void clipBoardSet(enum gale::context::clipBoard::clipboardListe _clipboardID) { + X11_FUNC(); switch (_clipboardID) { case gale::context::clipBoard::clipboardSelection: // Request the selection : diff --git a/gale/context/simulation/Context.cpp b/gale/context/simulation/Context.cpp index 4dbc2e8..fb5b7f4 100644 --- a/gale/context/simulation/Context.cpp +++ b/gale/context/simulation/Context.cpp @@ -65,6 +65,7 @@ class SimulationInterface : public gale::Context { gale::Context(_application, _argc, _argv) { GALE_INFO("SIMU:INIT"); m_run = true; + start2ntThreadProcessing(); } ~SimulationInterface() { diff --git a/gale/context/wayland/Context.cpp b/gale/context/wayland/Context.cpp index 2a9de09..c2d17a8 100644 --- a/gale/context/wayland/Context.cpp +++ b/gale/context/wayland/Context.cpp @@ -329,6 +329,7 @@ class WAYLANDInterface : public gale::Context { m_uniqueWindowsName = "GALE_" + etk::to_string(etk::tool::irand(0, 1999999999)); m_run = true; GALE_WARNING("WAYLAND: INIT [STOP]"); + start2ntThreadProcessing(); } ~WAYLANDInterface() {