CComponentsFrmClock: Move thread start/stop to constructor/destructor

Origin commit data
------------------
Commit: 6409cc8a19
Author: Michael Liebmann <tuxcode.bbg@gmail.com>
Date: 2013-06-12 (Wed, 12 Jun 2013)
This commit is contained in:
Michael Liebmann
2013-06-12 00:55:59 +02:00
parent f287ee5d5e
commit 984a1691f9
3 changed files with 67 additions and 34 deletions

View File

@@ -39,7 +39,7 @@
using namespace std; using namespace std;
CComponentsFrmClock::CComponentsFrmClock( const int x_pos, const int y_pos, const int w, const int h, 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) 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_body = color_body;
col_shadow = color_shadow; col_shadow = color_shadow;
paintClock = false;
activeClock = activ;
if (activeClock)
startThread();
cl_format_str = format_str; cl_format_str = format_str;
} }
@@ -78,9 +83,8 @@ void CComponentsFrmClock::initVarClock()
CComponentsFrmClock::~CComponentsFrmClock() CComponentsFrmClock::~CComponentsFrmClock()
{ {
cleanCCForm(); cleanCCForm();
if(cl_thread) if (activeClock)
pthread_cancel(cl_thread); stopThread();
cl_thread = 0;
} }
void CComponentsFrmClock::initTimeString() void CComponentsFrmClock::initTimeString()
@@ -241,58 +245,80 @@ void* CComponentsFrmClock::initClockThread(void *arg)
pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS,0); pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS,0);
CComponentsFrmClock *clock = static_cast<CComponentsFrmClock*>(arg); CComponentsFrmClock *clock = static_cast<CComponentsFrmClock*>(arg);
time_t count = time(0);
//ensure paint of segements on first paint
clock->paint();
//start loop for paint //start loop for paint
while(1) { while(1) {
sleep(clock->cl_interval); sleep(clock->cl_interval);
if (clock->paintClock) {
//paint segements, but wihtout saved backgrounds //paint segements, but wihtout saved backgrounds
clock->paint(CC_SAVE_SCREEN_NO); clock->paint(CC_SAVE_SCREEN_NO);
count = time(0);
}
if (time(0) >= count+30) {
clock->cl_thread = 0;
break;
}
} }
return 0; return 0;
} }
//start up ticking clock with own thread, return true on succses //start up ticking clock with own thread, return true on succses
bool CComponentsFrmClock::Start() bool CComponentsFrmClock::startThread()
{ {
void *ptr = static_cast<void*>(this); void *ptr = static_cast<void*>(this);
if(!cl_thread) { if(!cl_thread) {
int res1 = pthread_create (&cl_thread, NULL, initClockThread, ptr) ; int res = pthread_create (&cl_thread, NULL, initClockThread, ptr) ;
int res2 = pthread_detach(cl_thread); if (res != 0){
if (res1 != 0){
printf("[CComponentsFrmClock] [%s] pthread_create %s\n", __FUNCTION__, strerror(errno)); printf("[CComponentsFrmClock] [%s] pthread_create %s\n", __FUNCTION__, strerror(errno));
return false; return false;
} }
if (res2 != 0){
printf("[CComponentsFrmClock] [%s] pthread_detach %s\n", __FUNCTION__, strerror(errno));
return false;
}
} }
return true; return true;
} }
//stop ticking clock and kill thread, return true on succses //stop ticking clock and kill thread, return true on succses
bool CComponentsFrmClock::Stop() bool CComponentsFrmClock::stopThread()
{ {
int res = 0; if(cl_thread) {
int res = pthread_cancel(cl_thread);
if(cl_thread)
res = pthread_cancel(cl_thread);
if (res != 0){ if (res != 0){
printf("[CComponentsFrmClock] [%s] pthread_cancel %s\n", __FUNCTION__, strerror(errno)); printf("[CComponentsFrmClock] [%s] pthread_cancel %s\n", __FUNCTION__, strerror(errno));
return false; 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; cl_thread = 0;
return true; 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) void CComponentsFrmClock::paint(bool do_save_bg)
{ {

View File

@@ -57,6 +57,9 @@ class CComponentsFrmClock : public CComponentsForm
///raw time chars ///raw time chars
char cl_timestr[20]; char cl_timestr[20];
bool paintClock;
bool activeClock;
///font ///font
int cl_font_type; int cl_font_type;
///fontrenderer object ///fontrenderer object
@@ -80,7 +83,7 @@ class CComponentsFrmClock : public CComponentsForm
public: public:
CComponentsFrmClock( const int x_pos, const int y_pos, const int w, const int h, 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); 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(); ~CComponentsFrmClock();
@@ -97,9 +100,13 @@ class CComponentsFrmClock : public CComponentsForm
void setClockFormat(const char* format_str){cl_format_str = format_str;}; void setClockFormat(const char* format_str){cl_format_str = format_str;};
///start ticking clock thread, returns true on success, if false causes log output ///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 ///stop ticking clock thread, returns true on success, if false causes log output
bool stopThread();
bool Start();
bool Stop(); bool Stop();
///returns true, if clock is running in thread ///returns true, if clock is running in thread
bool isClockRun() const {return cl_thread == 0 ? false:true;}; bool isClockRun() const {return cl_thread == 0 ? false:true;};
///set refresh interval in seconds, default value=1 (=1 sec) ///set refresh interval in seconds, default value=1 (=1 sec)

View File

@@ -560,11 +560,11 @@ int CTestMenu::exec(CMenuTarget* parent, const std::string &actionKey)
// clock_r->doPaintBg(false); // clock_r->doPaintBg(false);
} }
if (!clock_r->isClockRun()){ if (!clock_r->isPainted()){
if (clock_r->Start()) if (clock_r->Start())
return menu_return::RETURN_EXIT_ALL;; return menu_return::RETURN_EXIT_ALL;;
} }
else if (clock_r->isClockRun()){ else {
if (clock_r->Stop()){ if (clock_r->Stop()){
clock_r->hide(); clock_r->hide();
delete clock_r; delete clock_r;
@@ -575,7 +575,7 @@ int CTestMenu::exec(CMenuTarget* parent, const std::string &actionKey)
} }
else if (actionKey == "clock"){ else if (actionKey == "clock"){
if (clock == NULL){ 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); clock->setClockFontType(SNeutrinoSettings::FONT_TYPE_INFOBAR_CHANNAME);
} }