From f2999b727260ae0911aeaf2331aed0a9448d39bf Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Wed, 29 Jun 2016 11:05:30 +0200 Subject: [PATCH] fix(date): Do not trigger updates when unchanged --- include/modules/date.hpp | 2 +- src/modules/date.cpp | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/modules/date.hpp b/include/modules/date.hpp index b237088d..5b437011 100644 --- a/include/modules/date.hpp +++ b/include/modules/date.hpp @@ -16,7 +16,7 @@ namespace modules std::string date_detailed; char date_str[256] = {}; - bool detailed = false; + concurrency::Atomic detailed { false }; public: explicit DateModule(std::string name); diff --git a/src/modules/date.cpp b/src/modules/date.cpp index 35b77d92..4d4d99ff 100644 --- a/src/modules/date.cpp +++ b/src/modules/date.cpp @@ -23,11 +23,18 @@ DateModule::DateModule(std::string name_) bool DateModule::update() { + if (!this->formatter->has(TAG_DATE)) + return false; + auto date_format = this->detailed ? this->date_detailed : this->date; auto time = std::time(nullptr); + char new_str[256] = {0,}; + std::strftime(new_str, sizeof(this->date_str), date_format.c_str(), std::localtime(&time)); - if (this->formatter->has(TAG_DATE)) - std::strftime(this->date_str, sizeof(this->date_str), date_format.c_str(), std::localtime(&time)); + if (std::strncmp(new_str, this->date_str, sizeof(new_str)) == 0) + return false; + else + std::memmove(this->date_str, new_str, sizeof(new_str)); return true; } @@ -36,9 +43,7 @@ std::string DateModule::get_output() { if (!this->date_detailed.empty()) this->builder->cmd(Cmd::LEFT_CLICK, EVENT_TOGGLE); - this->builder->node(this->Module::get_output()); - return this->builder->flush(); } @@ -53,8 +58,7 @@ bool DateModule::handle_command(std::string cmd) { if (cmd == EVENT_TOGGLE) { this->detailed = !this->detailed; - this->broadcast(); + this->wakeup(); } - return cmd == EVENT_TOGGLE; }