diff --git a/src/app/ui/editor/play_state.cpp b/src/app/ui/editor/play_state.cpp index 242d32d91..5c6e75431 100644 --- a/src/app/ui/editor/play_state.cpp +++ b/src/app/ui/editor/play_state.cpp @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as @@ -70,7 +70,7 @@ void PlayState::onEnterState(Editor* editor) m_toScroll = false; m_nextFrameTime = getNextFrameTime(); - m_curFrameTick = ui::clock(); + m_curFrameTick = base::current_tick(); m_pingPongForward = true; // Maybe we came from ScrollingState and the timer is already @@ -146,7 +146,7 @@ void PlayState::onPlaybackTick() if (m_nextFrameTime < 0) return; - m_nextFrameTime -= (ui::clock() - m_curFrameTick); + m_nextFrameTime -= (base::current_tick() - m_curFrameTick); doc::Sprite* sprite = m_editor->sprite(); doc::FrameTag* tag = get_animation_tag(sprite, m_refFrame); @@ -188,7 +188,7 @@ void PlayState::onPlaybackTick() m_editor->invalidate(); } - m_curFrameTick = ui::clock(); + m_curFrameTick = base::current_tick(); } // Before executing any command, we stop the animation diff --git a/src/app/ui/editor/play_state.h b/src/app/ui/editor/play_state.h index 4ada6d9c3..cb33332ca 100644 --- a/src/app/ui/editor/play_state.h +++ b/src/app/ui/editor/play_state.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as @@ -11,6 +11,7 @@ #include "app/ui/editor/state_with_wheel_behavior.h" #include "base/connection.h" +#include "base/time.h" #include "doc/frame.h" #include "ui/timer.h" @@ -46,7 +47,7 @@ namespace app { // Number of milliseconds to go to the next frame if m_playTimer // is activated. double m_nextFrameTime; - int m_curFrameTick; + base::tick_t m_curFrameTick; bool m_pingPongForward; doc::frame_t m_refFrame; diff --git a/src/app/ui/file_list.cpp b/src/app/ui/file_list.cpp index aa06a36c3..9ddcdfb55 100644 --- a/src/app/ui/file_list.cpp +++ b/src/app/ui/file_list.cpp @@ -15,6 +15,7 @@ #include "app/thumbnail_generator.h" #include "app/ui/skin/skin_theme.h" #include "base/string.h" +#include "base/time.h" #include "she/font.h" #include "she/surface.h" #include "ui/ui.h" @@ -245,7 +246,7 @@ bool FileList::onProcessMessage(Message* msg) std::tolower(unicodeChar) <= 'z') || (unicodeChar >= '0' && unicodeChar <= '9')) { - if (ui::clock() - m_isearchClock > ISEARCH_KEYPRESS_INTERVAL_MSECS) + if ((base::current_tick() - m_isearchClock) > ISEARCH_KEYPRESS_INTERVAL_MSECS) m_isearch.clear(); m_isearch.push_back(unicodeChar); @@ -261,7 +262,7 @@ bool FileList::onProcessMessage(Message* msg) break; } } - m_isearchClock = ui::clock(); + m_isearchClock = base::current_tick(); // Go to selectIndex... } else diff --git a/src/app/ui/file_list.h b/src/app/ui/file_list.h index 8e4a8cc68..2db4d8d99 100644 --- a/src/app/ui/file_list.h +++ b/src/app/ui/file_list.h @@ -11,6 +11,7 @@ #include "app/file_system.h" #include "base/signal.h" +#include "base/time.h" #include "ui/timer.h" #include "ui/widget.h" @@ -72,7 +73,7 @@ namespace app { // Incremental-search std::string m_isearch; - int m_isearchClock; + base::tick_t m_isearchClock; // Timer to start generating the thumbnail after an item is // selected. diff --git a/src/app/ui/status_bar.cpp b/src/app/ui/status_bar.cpp index 679592580..62951eed9 100644 --- a/src/app/ui/status_bar.cpp +++ b/src/app/ui/status_bar.cpp @@ -263,7 +263,7 @@ void StatusBar::updateFromEditor(Editor* editor) bool StatusBar::setStatusText(int msecs, const char *format, ...) { - if ((ui::clock() > m_timeout) || (msecs > 0)) { + if ((base::current_tick() > m_timeout) || (msecs > 0)) { char buf[256]; // TODO warning buffer overflow va_list ap; @@ -271,7 +271,7 @@ bool StatusBar::setStatusText(int msecs, const char *format, ...) vsprintf(buf, format, ap); va_end(ap); - m_timeout = ui::clock() + msecs; + m_timeout = base::current_tick() + msecs; m_state = SHOW_TEXT; setText(buf); @@ -315,7 +315,7 @@ void StatusBar::showTip(int msecs, const char *format, ...) m_tipwindow->startTimer(); // Set the text in status-bar (with inmediate timeout) - m_timeout = ui::clock(); + m_timeout = base::current_tick(); setText(buf); invalidate(); } diff --git a/src/app/ui/status_bar.h b/src/app/ui/status_bar.h index e54a5f66d..1a53a3167 100644 --- a/src/app/ui/status_bar.h +++ b/src/app/ui/status_bar.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License version 2 as @@ -11,6 +11,7 @@ #include "app/color.h" #include "base/observers.h" +#include "base/time.h" #include "doc/context_observer.h" #include "doc/document_observer.h" #include "doc/documents_observer.h" @@ -86,7 +87,7 @@ namespace app { enum State { SHOW_TEXT, SHOW_COLOR, SHOW_TOOL }; - int m_timeout; + base::tick_t m_timeout; State m_state; // Showing a tool diff --git a/src/base/time.cpp b/src/base/time.cpp index 8c788bd52..ce8030edf 100644 --- a/src/base/time.cpp +++ b/src/base/time.cpp @@ -1,5 +1,5 @@ // Aseprite Base Library -// Copyright (c) 2001-2013, 2015 David Capello +// Copyright (c) 2001-2016 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -14,6 +14,11 @@ #include #else #include + #include +#endif + +#if __APPLE__ + #include #endif namespace base { @@ -37,4 +42,22 @@ Time current_time() #endif } +tick_t current_tick() +{ +#if _WIN32 + // TODO use GetTickCount64 (available from Vista) + return GetTickCount(); +#elif defined(__APPLE__) + static mach_timebase_info_data_t timebase = { 0, 0 }; + if (timebase.denom == 0) + (void)mach_timebase_info(&timebase); + return tick_t(double(mach_absolute_time()) * double(timebase.numer) / double(timebase.denom) / 1.0e6); +#else + // TODO use clock_gettime(CLOCK_MONOTONIC, &now); if it's possible + struct timeval now; + gettimeofday(&now, nullptr); + return now.tv_sec*1000 + now.tv_usec/1000; +#endif +} + } // namespace base diff --git a/src/base/time.h b/src/base/time.h index 85235bb92..0d52bf9a8 100644 --- a/src/base/time.h +++ b/src/base/time.h @@ -1,5 +1,5 @@ // Aseprite Base Library -// Copyright (c) 2001-2013, 2015 David Capello +// Copyright (c) 2001-2016 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -8,10 +8,15 @@ #define BASE_TIME_H_INCLUDED #pragma once +#include "base/ints.h" + #include namespace base { + // ~1000 per second + typedef uint64_t tick_t; + class Time { public: int year, month, day; @@ -47,6 +52,7 @@ namespace base { }; Time current_time(); + tick_t current_tick(); } diff --git a/src/she/alleg4/alleg_display.cpp b/src/she/alleg4/alleg_display.cpp index 61bbb5305..521dfcda9 100644 --- a/src/she/alleg4/alleg_display.cpp +++ b/src/she/alleg4/alleg_display.cpp @@ -54,7 +54,6 @@ #include #include -#include "she/alleg4/clock.h" #include "she/alleg4/display_events.h" #ifdef USE_KEY_POLLER diff --git a/src/she/alleg4/clock.cpp b/src/she/alleg4/clock.cpp deleted file mode 100644 index 572f0ab73..000000000 --- a/src/she/alleg4/clock.cpp +++ /dev/null @@ -1,43 +0,0 @@ -// SHE library -// Copyright (C) 2012-2014 David Capello -// -// This file is released under the terms of the MIT license. -// Read LICENSE.txt for more information. - -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif - -#include - -static volatile int clock_var = 0; // Global timer. - -static void clock_inc() -{ - ++clock_var; -} - -END_OF_STATIC_FUNCTION(clock_inc); - -namespace she { - -void clock_init() -{ - // Install timer related stuff - LOCK_VARIABLE(clock_var); - LOCK_FUNCTION(clock_inc); - - install_int_ex(clock_inc, BPS_TO_TIMER(1000)); -} - -void clock_exit() -{ - remove_int(clock_inc); -} - -int clock_value() -{ - return clock_var; -} - -} // namespace she diff --git a/src/she/alleg4/clock.h b/src/she/alleg4/clock.h deleted file mode 100644 index 34888cf10..000000000 --- a/src/she/alleg4/clock.h +++ /dev/null @@ -1,18 +0,0 @@ -// SHE library -// Copyright (C) 2012-2014 David Capello -// -// This file is released under the terms of the MIT license. -// Read LICENSE.txt for more information. - -#ifndef SHE_ALLEG4_CLOCK_H_INCLUDED -#define SHE_ALLEG4_CLOCK_H_INCLUDED -#pragma once - -namespace she { - - void clock_init(); - void clock_exit(); - -} // namespace she - -#endif diff --git a/src/she/alleg4/mouse_poller.cpp b/src/she/alleg4/mouse_poller.cpp index 6e55ab9f9..c586f84b8 100644 --- a/src/she/alleg4/mouse_poller.cpp +++ b/src/she/alleg4/mouse_poller.cpp @@ -8,8 +8,8 @@ #include "config.h" #endif +#include "base/time.h" #include "she/alleg4/alleg_display.h" -#include "she/clock.h" #include "she/display.h" #include "she/event.h" #include "she/event_queue.h" @@ -49,7 +49,7 @@ bool mouse_left = false; DlbClk double_click_level; Event::MouseButton double_click_button = Event::NoneButton; -int double_click_ticks; +base::tick_t double_click_ticks; #if !defined(__APPLE__) && !defined(_WIN32) // Mouse enter/leave @@ -121,11 +121,11 @@ void generate_mouse_event_for_button(Event::MouseButton button, int old_b, int n ev.setButton(button); // Double Click - int current_ticks = clock_value(); + base::tick_t current_ticks = base::current_tick(); if (ev.type() == Event::MouseDown) { if (double_click_level != DOUBLE_CLICK_NONE) { // Time out, back to NONE - if (current_ticks - double_click_ticks > DOUBLE_CLICK_TIMEOUT_MSECS) { + if ((current_ticks - double_click_ticks) > DOUBLE_CLICK_TIMEOUT_MSECS) { double_click_level = DOUBLE_CLICK_NONE; } else if (double_click_button == button) { @@ -152,7 +152,7 @@ void generate_mouse_event_for_button(Event::MouseButton button, int old_b, int n else if (ev.type() == Event::MouseUp) { if (double_click_level != DOUBLE_CLICK_NONE) { // Time out, back to NONE - if (current_ticks - double_click_ticks > DOUBLE_CLICK_TIMEOUT_MSECS) { + if ((current_ticks - double_click_ticks) > DOUBLE_CLICK_TIMEOUT_MSECS) { double_click_level = DOUBLE_CLICK_NONE; } else if (double_click_level == DOUBLE_CLICK_DOWN) { diff --git a/src/she/alleg4/she.cpp b/src/she/alleg4/she.cpp index 1f9738f11..48c4ed176 100644 --- a/src/she/alleg4/she.cpp +++ b/src/she/alleg4/she.cpp @@ -1,5 +1,5 @@ // SHE library -// Copyright (C) 2012-2015 David Capello +// Copyright (C) 2012-2016 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -55,7 +55,6 @@ #include #include -#include "she/alleg4/clock.h" #include "she/alleg4/display_events.h" #ifdef USE_KEY_POLLER #include "she/alleg4/key_poller.h" @@ -123,7 +122,6 @@ public: register_bitmap_file_type("png", load_png, save_png); // Init event sources - clock_init(); display_events_init(); #ifdef USE_KEY_POLLER key_poller_init(); @@ -136,7 +134,6 @@ public: } ~Alleg4System() { - clock_exit(); remove_timer(); allegro_exit(); diff --git a/src/she/clock.h b/src/she/clock.h deleted file mode 100644 index 82aae2ac4..000000000 --- a/src/she/clock.h +++ /dev/null @@ -1,17 +0,0 @@ -// SHE library -// Copyright (C) 2012-2014 David Capello -// -// This file is released under the terms of the MIT license. -// Read LICENSE.txt for more information. - -#ifndef SHE_CLOCK_H_INCLUDED -#define SHE_CLOCK_H_INCLUDED -#pragma once - -namespace she { - - int clock_value(); - -} // namespace she - -#endif diff --git a/src/she/skia/she.cpp b/src/she/skia/she.cpp index 9482dab8e..94c03b07e 100644 --- a/src/she/skia/she.cpp +++ b/src/she/skia/she.cpp @@ -1,5 +1,5 @@ // SHE library -// Copyright (C) 2012-2015 David Capello +// Copyright (C) 2012-2016 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -20,7 +20,6 @@ #if __APPLE__ #include "she/osx/app.h" #include - #include #endif namespace she { @@ -47,20 +46,6 @@ void clear_keyboard_buffer() // Do nothing } -int clock_value() -{ -#if _WIN32 - return (int)GetTickCount(); -#elif defined(__APPLE__) - static mach_timebase_info_data_t timebase = { 0, 0 }; - if (timebase.denom == 0) - (void)mach_timebase_info(&timebase); - return int(float(mach_absolute_time()) * float(timebase.numer) / float(timebase.denom) / 1.0e6f); -#else - return 0; // TODO -#endif -} - } // namespace she extern int app_main(int argc, char* argv[]); diff --git a/src/ui/manager.cpp b/src/ui/manager.cpp index 4b583f12d..4ec3db145 100644 --- a/src/ui/manager.cpp +++ b/src/ui/manager.cpp @@ -15,6 +15,7 @@ #include "ui/manager.h" #include "base/scoped_value.h" +#include "base/time.h" #include "she/display.h" #include "she/event.h" #include "she/event_queue.h" @@ -1189,13 +1190,13 @@ void Manager::onSizeHint(SizeHintEvent& ev) void Manager::pumpQueue() { #ifdef LIMIT_DISPATCH_TIME - int t = ui::clock(); + base::tick_t t = base::current_tick(); #endif Messages::iterator it = msg_queue.begin(); while (it != msg_queue.end()) { #ifdef LIMIT_DISPATCH_TIME - if (ui::clock()-t > 250) + if (base::current_tick()-t > 250) break; #endif diff --git a/src/ui/system.cpp b/src/ui/system.cpp index f3b1298ea..820c4e518 100644 --- a/src/ui/system.cpp +++ b/src/ui/system.cpp @@ -1,5 +1,5 @@ // Aseprite UI Library -// Copyright (C) 2001-2013, 2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -11,7 +11,6 @@ #include "ui/system.h" #include "gfx/point.h" -#include "she/clock.h" #include "she/display.h" #include "she/surface.h" #include "she/system.h" @@ -150,11 +149,6 @@ UISystem::~UISystem() update_mouse_overlay(NULL); } -int clock() -{ - return she::clock_value(); -} - void _internal_set_mouse_display(she::Display* display) { CursorType cursor = get_mouse_cursor(); diff --git a/src/ui/system.h b/src/ui/system.h index 30193694b..72972c939 100644 --- a/src/ui/system.h +++ b/src/ui/system.h @@ -1,5 +1,5 @@ // Aseprite UI Library -// Copyright (C) 2001-2013, 2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -28,10 +28,6 @@ namespace ui { int display_w(); int display_h(); - // Timer related - - int clock(); - // Mouse related // Updates the position of the mouse cursor overlay depending on the diff --git a/src/ui/timer.cpp b/src/ui/timer.cpp index b2e2871f5..5838e86c3 100644 --- a/src/ui/timer.cpp +++ b/src/ui/timer.cpp @@ -1,5 +1,5 @@ // Aseprite UI Library -// Copyright (C) 2001-2013 David Capello +// Copyright (C) 2001-2016 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -10,9 +10,9 @@ #include "ui/timer.h" +#include "base/time.h" #include "ui/manager.h" #include "ui/message.h" -#include "ui/system.h" #include "ui/widget.h" #include @@ -27,9 +27,10 @@ static Timers timers; // Registered timers Timer::Timer(int interval, Widget* owner) : m_owner(owner ? owner: Manager::getDefault()) , m_interval(interval) - , m_lastTime(-1) + , m_running(false) + , m_lastTick(0) { - ASSERT(m_owner != NULL); + ASSERT(m_owner != nullptr); timers.push_back(this); } @@ -44,19 +45,15 @@ Timer::~Timer() Manager::getDefault()->removeMessagesForTimer(this); } -bool Timer::isRunning() const -{ - return (m_lastTime >= 0); -} - void Timer::start() { - m_lastTime = ui::clock(); + m_lastTick = base::current_tick(); + m_running = true; } void Timer::stop() { - m_lastTime = -1; + m_running = false; } void Timer::tick() @@ -79,26 +76,16 @@ void Timer::pollTimers() { // Generate messages for timers if (!timers.empty()) { - int t = ui::clock(); - int count; + base::tick_t t = base::current_tick(); for (Timers::iterator it=timers.begin(), end=timers.end(); it != end; ++it) { Timer* timer = *it; - if (timer && timer->m_lastTime >= 0) { - count = 0; - while (t - timer->m_lastTime > timer->m_interval) { - timer->m_lastTime += timer->m_interval; - ++count; - - /* we spend too much time here */ - if (ui::clock() - t > timer->m_interval) { - timer->m_lastTime = ui::clock(); - break; - } - } - + if (timer && timer->isRunning()) { + int64_t count = ((t - timer->m_lastTick) / timer->m_interval); if (count > 0) { - ASSERT(timer->m_owner != NULL); + timer->m_lastTick += count * timer->m_interval; + + ASSERT(timer->m_owner != nullptr); Message* msg = new TimerMessage(count, timer); msg->addRecipient(timer->m_owner); diff --git a/src/ui/timer.h b/src/ui/timer.h index bff5e7f34..049396e17 100644 --- a/src/ui/timer.h +++ b/src/ui/timer.h @@ -1,5 +1,5 @@ // Aseprite UI Library -// Copyright (C) 2001-2013, 2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -10,21 +10,23 @@ #include "base/disable_copying.h" #include "base/signal.h" +#include "base/time.h" namespace ui { class Widget; - class Timer - { + class Timer { public: Timer(int interval, Widget* owner = NULL); virtual ~Timer(); - int getInterval() const; + int interval() const { return m_interval; } void setInterval(int interval); - bool isRunning() const; + bool isRunning() const { + return m_running; + } void start(); void stop(); @@ -42,7 +44,8 @@ namespace ui { public: Widget* m_owner; int m_interval; - int m_lastTime; + bool m_running; + base::tick_t m_lastTick; DISABLE_COPYING(Timer); };