From b9edaffe4c16103b459bd466f710624f6fed1211 Mon Sep 17 00:00:00 2001 From: Michael Liebmann Date: Wed, 12 Jun 2013 00:55:59 +0200 Subject: [PATCH] CComponentsFrmClock: Move thread start/stop to constructor/destructor Origin commit data ------------------ Branch: ni/coolstream Commit: https://github.com/neutrino-images/ni-neutrino/commit/6409cc8a19b7e2c9278c225f07c6863741a508d7 Author: Michael Liebmann Date: 2013-06-12 (Wed, 12 Jun 2013) ------------------ No further description and justification available within origin commit message! ------------------ This commit was generated by Migit --- src/gui/components/cc_frm_clock.cpp | 84 +++++++++++++++++++---------- src/gui/components/cc_frm_clock.h | 11 +++- src/gui/test_menu.cpp | 6 +-- 3 files changed, 67 insertions(+), 34 deletions(-) diff --git a/src/gui/components/cc_frm_clock.cpp b/src/gui/components/cc_frm_clock.cpp index eca8ffb96..a95ce09c9 100644 --- a/src/gui/components/cc_frm_clock.cpp +++ b/src/gui/components/cc_frm_clock.cpp @@ -39,7 +39,7 @@ using namespace std; CComponentsFrmClock::CComponentsFrmClock( const int x_pos, const int y_pos, const int w, const int h, - const char* format_str, bool has_shadow, + const char* format_str, bool activ, bool has_shadow, fb_pixel_t color_frame, fb_pixel_t color_body, fb_pixel_t color_shadow) { @@ -55,6 +55,11 @@ CComponentsFrmClock::CComponentsFrmClock( const int x_pos, const int y_pos, cons col_body = color_body; col_shadow = color_shadow; + paintClock = false; + activeClock = activ; + if (activeClock) + startThread(); + cl_format_str = format_str; } @@ -78,9 +83,8 @@ void CComponentsFrmClock::initVarClock() CComponentsFrmClock::~CComponentsFrmClock() { cleanCCForm(); - if(cl_thread) - pthread_cancel(cl_thread); - cl_thread = 0; + if (activeClock) + stopThread(); } void CComponentsFrmClock::initTimeString() @@ -241,58 +245,80 @@ void* CComponentsFrmClock::initClockThread(void *arg) pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS,0); CComponentsFrmClock *clock = static_cast(arg); - - //ensure paint of segements on first paint - clock->paint(); - + time_t count = time(0); //start loop for paint while(1) { sleep(clock->cl_interval); - - //paint segements, but wihtout saved backgrounds - clock->paint(CC_SAVE_SCREEN_NO); + + if (clock->paintClock) { + //paint segements, but wihtout saved backgrounds + clock->paint(CC_SAVE_SCREEN_NO); + count = time(0); + } + if (time(0) >= count+30) { + clock->cl_thread = 0; + break; + } } return 0; } //start up ticking clock with own thread, return true on succses -bool CComponentsFrmClock::Start() +bool CComponentsFrmClock::startThread() { void *ptr = static_cast(this); if(!cl_thread) { - int res1 = pthread_create (&cl_thread, NULL, initClockThread, ptr) ; - int res2 = pthread_detach(cl_thread); - - if (res1 != 0){ + int res = pthread_create (&cl_thread, NULL, initClockThread, ptr) ; + if (res != 0){ printf("[CComponentsFrmClock] [%s] pthread_create %s\n", __FUNCTION__, strerror(errno)); return false; } - if (res2 != 0){ - printf("[CComponentsFrmClock] [%s] pthread_detach %s\n", __FUNCTION__, strerror(errno)); - return false; - } } return true; } //stop ticking clock and kill thread, return true on succses -bool CComponentsFrmClock::Stop() +bool CComponentsFrmClock::stopThread() { - int res = 0; - - if(cl_thread) - res = pthread_cancel(cl_thread); + if(cl_thread) { + int res = pthread_cancel(cl_thread); + if (res != 0){ + printf("[CComponentsFrmClock] [%s] pthread_cancel %s\n", __FUNCTION__, strerror(errno)); + return false; + } - if (res != 0){ - printf("[CComponentsFrmClock] [%s] pthread_cancel %s\n", __FUNCTION__, strerror(errno)); - return false; + res = pthread_join(cl_thread, NULL); + if (res != 0){ + printf("[CComponentsFrmClock] [%s] pthread_join %s\n", __FUNCTION__, strerror(errno)); + return false; + } } - cl_thread = 0; return true; } +bool CComponentsFrmClock::Start() +{ + if (!activeClock) + return false; + if (!cl_thread) + startThread(); + if (cl_thread) { + //ensure paint of segements on first paint + paint(); + paintClock = true; + } + return cl_thread == 0 ? false : true; +} + +bool CComponentsFrmClock::Stop() +{ + if (!activeClock) + return false; + paintClock = false; + return cl_thread == 0 ? false : true; +} void CComponentsFrmClock::paint(bool do_save_bg) { diff --git a/src/gui/components/cc_frm_clock.h b/src/gui/components/cc_frm_clock.h index ad08a1181..fdcaf8371 100644 --- a/src/gui/components/cc_frm_clock.h +++ b/src/gui/components/cc_frm_clock.h @@ -57,6 +57,9 @@ class CComponentsFrmClock : public CComponentsForm ///raw time chars char cl_timestr[20]; + bool paintClock; + bool activeClock; + ///font int cl_font_type; ///fontrenderer object @@ -80,7 +83,7 @@ class CComponentsFrmClock : public CComponentsForm public: CComponentsFrmClock( const int x_pos, const int y_pos, const int w, const int h, - const char* format_str = "%H:%M", bool has_shadow = CC_SHADOW_OFF, + const char* format_str = "%H:%M", bool activ=true, bool has_shadow = CC_SHADOW_OFF, fb_pixel_t color_frame = COL_LIGHT_GRAY, fb_pixel_t color_body = COL_MENUCONTENT_PLUS_0, fb_pixel_t color_shadow = COL_MENUCONTENTDARK_PLUS_0); ~CComponentsFrmClock(); @@ -97,9 +100,13 @@ class CComponentsFrmClock : public CComponentsForm void setClockFormat(const char* format_str){cl_format_str = format_str;}; ///start ticking clock thread, returns true on success, if false causes log output - bool Start(); + bool startThread(); ///stop ticking clock thread, returns true on success, if false causes log output + bool stopThread(); + + bool Start(); bool Stop(); + ///returns true, if clock is running in thread bool isClockRun() const {return cl_thread == 0 ? false:true;}; ///set refresh interval in seconds, default value=1 (=1 sec) diff --git a/src/gui/test_menu.cpp b/src/gui/test_menu.cpp index aebf045d5..70640e2fa 100644 --- a/src/gui/test_menu.cpp +++ b/src/gui/test_menu.cpp @@ -560,11 +560,11 @@ int CTestMenu::exec(CMenuTarget* parent, const std::string &actionKey) // clock_r->doPaintBg(false); } - if (!clock_r->isClockRun()){ + if (!clock_r->isPainted()){ if (clock_r->Start()) return menu_return::RETURN_EXIT_ALL;; } - else if (clock_r->isClockRun()){ + else { if (clock_r->Stop()){ clock_r->hide(); delete clock_r; @@ -575,7 +575,7 @@ int CTestMenu::exec(CMenuTarget* parent, const std::string &actionKey) } else if (actionKey == "clock"){ if (clock == NULL){ - clock = new CComponentsFrmClock(100, 50, 0, 50, "%H:%M"); + clock = new CComponentsFrmClock(100, 50, 0, 50, "%H:%M", false); clock->setClockFontType(SNeutrinoSettings::FONT_TYPE_INFOBAR_CHANNAME); }