diff --git a/src/core/poll.cc b/src/core/poll.cc index bd9f50a2..fc7fd9eb 100644 --- a/src/core/poll.cc +++ b/src/core/poll.cc @@ -29,8 +29,7 @@ Poll::poll(utils::Timer timeout) { m_maxFd = std::max(m_maxFd, n); } - timeout = std::min(timeout, utils::Timer(torrent::get(torrent::TIME_SELECT))); - timeval t = timeout.tval(); + timeval t = std::min(timeout, utils::Timer(torrent::get(torrent::TIME_SELECT))).tval(); errno = 0; m_maxFd = select(m_maxFd + 1, m_readSet, m_writeSet, m_exceptSet, &t); diff --git a/src/display/Makefile.am b/src/display/Makefile.am index d134aded..f093d8df 100644 --- a/src/display/Makefile.am +++ b/src/display/Makefile.am @@ -9,10 +9,10 @@ libsub_display_a_SOURCES = \ utils.h \ window.cc \ window.h \ - window_download_statusbar.cc \ - window_download_statusbar.h \ window_download_list.cc \ window_download_list.h \ + window_download_statusbar.cc \ + window_download_statusbar.h \ window_http_queue.cc \ window_http_queue.h \ window_input.cc \ diff --git a/src/display/manager.cc b/src/display/manager.cc index c6b42e1b..58cfbf5c 100644 --- a/src/display/manager.cc +++ b/src/display/manager.cc @@ -56,7 +56,8 @@ void Manager::do_update() { Canvas::refresh_std(); - std::for_each(begin(), end(), func::if_then(std::mem_fun(&Window::is_active), std::mem_fun(&Window::redraw))); + std::for_each(begin(), end(), func::if_then(std::mem_fun(&Window::is_active), func::if_then(std::mem_fun(&Window::is_dirty), + std::mem_fun(&Window::redraw)))); std::for_each(begin(), end(), func::if_then(std::mem_fun(&Window::is_active), std::mem_fun(&Window::refresh))); Canvas::do_update(); diff --git a/src/display/window.cc b/src/display/window.cc index 8b37c419..02231bdb 100644 --- a/src/display/window.cc +++ b/src/display/window.cc @@ -2,7 +2,6 @@ #include -#include "canvas.h" #include "window.h" namespace display { @@ -13,11 +12,6 @@ Window::~Window() { delete m_canvas; } -void -Window::refresh() { - m_canvas->refresh(); -} - void Window::resize(int x, int y, int w, int h) { if (x < 0 || y < 0) diff --git a/src/display/window.h b/src/display/window.h index 27c2d433..549f3a72 100644 --- a/src/display/window.h +++ b/src/display/window.h @@ -3,6 +3,7 @@ #include +#include "canvas.h" #include "utils/timer.h" namespace display { @@ -20,17 +21,19 @@ public: bool is_active() { return m_active; } bool is_dynamic() { return m_dynamic; } - bool is_dirty() { return m_lastDraw == 0; } + bool is_dirty() { return m_nextDraw <= utils::Timer::cache(); } + + utils::Timer get_next_draw() { return m_nextDraw; } int get_min_height() { return m_minHeight; } bool get_active() { return m_active; } void set_active(bool a) { m_active = a; } - void refresh(); + void refresh() { m_canvas->refresh(); } void resize(int x, int y, int w, int h); - void mark_dirty() { m_lastDraw = 0; } + void mark_dirty() { m_nextDraw = utils::Timer::min(); } virtual void redraw() = 0; @@ -48,7 +51,7 @@ protected: bool m_dynamic; int m_minHeight; - utils::Timer m_lastDraw; + utils::Timer m_nextDraw; }; } diff --git a/src/display/window_download_list.cc b/src/display/window_download_list.cc index 635ac296..17902f74 100644 --- a/src/display/window_download_list.cc +++ b/src/display/window_download_list.cc @@ -16,10 +16,7 @@ WindowDownloadList::WindowDownloadList(DList* l, DList::iterator* f) : void WindowDownloadList::redraw() { - if (utils::Timer::cache() - m_lastDraw < 1000000) - return; - - m_lastDraw = utils::Timer::cache(); + m_nextDraw = utils::Timer::cache().round_seconds() + 1000000; m_canvas->erase(); diff --git a/src/display/window_download_statusbar.cc b/src/display/window_download_statusbar.cc index f70f5970..425b47d5 100644 --- a/src/display/window_download_statusbar.cc +++ b/src/display/window_download_statusbar.cc @@ -15,10 +15,7 @@ WindowDownloadStatusbar::WindowDownloadStatusbar(core::Download* d) : void WindowDownloadStatusbar::redraw() { - if (utils::Timer::cache() - m_lastDraw < 1000000) - return; - - m_lastDraw = utils::Timer::cache(); + m_nextDraw = utils::Timer::cache().round_seconds() + 1000000; m_canvas->erase(); diff --git a/src/display/window_http_queue.cc b/src/display/window_http_queue.cc index 35219239..074cb922 100644 --- a/src/display/window_http_queue.cc +++ b/src/display/window_http_queue.cc @@ -22,10 +22,7 @@ WindowHttpQueue::WindowHttpQueue(core::HttpQueue* q) : void WindowHttpQueue::redraw() { - if (utils::Timer::cache() - m_lastDraw < 1000000) - return; - - m_lastDraw = utils::Timer::cache(); + m_nextDraw = utils::Timer::cache().round_seconds() + 1000000; cleanup_list(); diff --git a/src/display/window_input.cc b/src/display/window_input.cc index c2d7360b..abc15dcb 100644 --- a/src/display/window_input.cc +++ b/src/display/window_input.cc @@ -15,10 +15,7 @@ WindowInput::WindowInput(input::TextInput* input) : void WindowInput::redraw() { - if (!is_dirty()) - return; - - m_lastDraw = utils::Timer::cache(); + m_nextDraw = utils::Timer::max(); m_canvas->erase(); m_canvas->print(0, 0, "> %s", m_input->c_str()); diff --git a/src/display/window_log.cc b/src/display/window_log.cc index 8d864f68..6c571504 100644 --- a/src/display/window_log.cc +++ b/src/display/window_log.cc @@ -26,10 +26,7 @@ WindowLog::find_older() { void WindowLog::redraw() { - if (!is_dirty()) - return; - - m_lastDraw = utils::Timer::cache(); + m_nextDraw = utils::Timer::max(); m_canvas->erase(); diff --git a/src/display/window_peer_info.cc b/src/display/window_peer_info.cc index 9fb386ce..e5520d01 100644 --- a/src/display/window_peer_info.cc +++ b/src/display/window_peer_info.cc @@ -19,10 +19,7 @@ WindowPeerInfo::WindowPeerInfo(core::Download* d, PList* l, PList::iterator* f) void WindowPeerInfo::redraw() { - if (utils::Timer::cache() - m_lastDraw < 1000000) - return; - - m_lastDraw = utils::Timer::cache(); + m_nextDraw = utils::Timer::cache().round_seconds() + 1000000; m_canvas->erase(); int y = 0; diff --git a/src/display/window_peer_list.cc b/src/display/window_peer_list.cc index 180b9875..aaad5a56 100644 --- a/src/display/window_peer_list.cc +++ b/src/display/window_peer_list.cc @@ -15,10 +15,7 @@ WindowPeerList::WindowPeerList(PList* l, PList::iterator* f) : void WindowPeerList::redraw() { - if (utils::Timer::cache() - m_lastDraw < 1000000) - return; - - m_lastDraw = utils::Timer::cache(); + m_nextDraw = utils::Timer::cache().round_seconds() + 1000000; m_canvas->erase(); int x = 2; diff --git a/src/display/window_statusbar.cc b/src/display/window_statusbar.cc index 332f4374..d1dcdd7f 100644 --- a/src/display/window_statusbar.cc +++ b/src/display/window_statusbar.cc @@ -17,20 +17,14 @@ WindowStatusbar::WindowStatusbar(core::Manager* c) : void WindowStatusbar::redraw() { - m_counter++; - - if (utils::Timer::cache() - m_lastDraw < 10000000) - return; - - m_lastDraw = utils::Timer::cache(); + m_nextDraw = utils::Timer::cache().round_seconds() + 1000000; m_canvas->erase(); - m_canvas->print(0, 0, "Throttle: %i Listen: %s:%i Handshakes: %i Select: %u", + m_canvas->print(0, 0, "Throttle: %i Listen: %s:%i Handshakes: %i", (int)torrent::get(torrent::THROTTLE_ROOT_CONST_RATE) / 1024, m_core->get_dns().empty() ? "" : m_core->get_dns().c_str(), (int)torrent::get(torrent::LISTEN_PORT), - (int)torrent::get(torrent::HANDSHAKES_TOTAL), - m_counter); + (int)torrent::get(torrent::HANDSHAKES_TOTAL)); } } diff --git a/src/display/window_title.cc b/src/display/window_title.cc index 65c0e0ff..02ac7229 100644 --- a/src/display/window_title.cc +++ b/src/display/window_title.cc @@ -12,10 +12,7 @@ WindowTitle::WindowTitle(const std::string& s) : void WindowTitle::redraw() { - if (utils::Timer::cache() - m_lastDraw < 10000000) - return; - - m_lastDraw = utils::Timer::cache(); + m_nextDraw = utils::Timer::cache().round_seconds() + 1000000; m_canvas->erase(); m_canvas->print(std::max(0, (m_canvas->get_width() - (int)m_title.size()) / 2 - 4), 0, diff --git a/src/main.cc b/src/main.cc index decd0564..3abb19b8 100644 --- a/src/main.cc +++ b/src/main.cc @@ -159,6 +159,8 @@ main(int argc, char** argv) { utils::Timer::update(); utils::TaskSchedule::perform(utils::Timer::cache()); + // This needs to be called every second or so. Currently done by + // the throttle task in libtorrent. uiControl.get_display().do_update(); uiControl.get_core().get_poll().poll(utils::TaskSchedule::get_timeout()); diff --git a/src/utils/timer.h b/src/utils/timer.h index c5edbe48..575509e4 100644 --- a/src/utils/timer.h +++ b/src/utils/timer.h @@ -1,6 +1,7 @@ #ifndef LIBTORRENT_TIMER_H #define LIBTORRENT_TIMER_H +#include #include #include @@ -13,18 +14,10 @@ class Timer { Timer(int64_t usec) : m_time(usec) {} Timer(timeval tv) : m_time((int64_t)(uint32_t)tv.tv_sec * 1000000 + (int64_t)(uint32_t)tv.tv_usec % 1000000) {} - int64_t usec() const { return m_time; } + int64_t usec() const { return m_time; } + timeval tval() const { return (timeval) { m_time / 1000000, m_time % 1000000}; } - timeval tval() const { return (timeval) { m_time / 1000000, m_time % 1000000}; } - - Timer operator - (const Timer& t) const { return Timer(m_time - t.m_time); } - Timer operator + (const Timer& t) const { return Timer(m_time + t.m_time); } - - Timer operator -= (int64_t t) { m_time -= t; return *this; } - Timer operator -= (const Timer& t) { m_time -= t.m_time; return *this; } - - Timer operator += (int64_t t) { m_time += t; return *this; } - Timer operator += (const Timer& t) { m_time += t.m_time; return *this; } + Timer round_seconds() const { return (m_time / 1000000) * 1000000; } static Timer current() { timeval t; @@ -38,15 +31,27 @@ class Timer { // TODO: Find out if it's worth it. The kernel is supposed to cache the // time. Though system calls would be more expensive than we can afford. - static Timer cache() { return Timer(m_cache); } + static Timer cache() { return Timer(m_cache); } - static void update() { m_cache = Timer::current().usec(); } + static Timer min() { return 0; } + static Timer max() { return (int64_t)std::numeric_limits::max() * 1000000; } - bool operator < (const Timer& t) const { return m_time < t.m_time; } - bool operator > (const Timer& t) const { return m_time > t.m_time; } - bool operator <= (const Timer& t) const { return m_time <= t.m_time; } - bool operator >= (const Timer& t) const { return m_time >= t.m_time; } - bool operator == (const Timer& t) const { return m_time == t.m_time; } + static void update() { m_cache = Timer::current().usec(); } + + Timer operator - (const Timer& t) const { return Timer(m_time - t.m_time); } + Timer operator + (const Timer& t) const { return Timer(m_time + t.m_time); } + + Timer operator -= (int64_t t) { m_time -= t; return *this; } + Timer operator -= (const Timer& t) { m_time -= t.m_time; return *this; } + + Timer operator += (int64_t t) { m_time += t; return *this; } + Timer operator += (const Timer& t) { m_time += t.m_time; return *this; } + + bool operator < (const Timer& t) const { return m_time < t.m_time; } + bool operator > (const Timer& t) const { return m_time > t.m_time; } + bool operator <= (const Timer& t) const { return m_time <= t.m_time; } + bool operator >= (const Timer& t) const { return m_time >= t.m_time; } + bool operator == (const Timer& t) const { return m_time == t.m_time; } private: int64_t m_time;