Move clock/tick from "she" to "base" library and switch to uint64_t type

This commit is contained in:
David Capello 2016-03-29 16:19:23 -03:00
parent 3cd7d273d9
commit 0605166cc6
20 changed files with 84 additions and 167 deletions

View File

@ -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

View File

@ -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;

View File

@ -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

View File

@ -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.

View File

@ -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();
}

View File

@ -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

View File

@ -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 <windows.h>
#else
#include <ctime>
#include <sys/time.h>
#endif
#if __APPLE__
#include <mach/mach_time.h>
#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

View File

@ -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 <string>
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();
}

View File

@ -54,7 +54,6 @@
#include <sstream>
#include <vector>
#include "she/alleg4/clock.h"
#include "she/alleg4/display_events.h"
#ifdef USE_KEY_POLLER

View File

@ -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 <allegro.h>
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

View File

@ -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

View File

@ -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) {

View File

@ -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 <list>
#include <vector>
#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();

View File

@ -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

View File

@ -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 <CoreServices/CoreServices.h>
#include <mach/mach_time.h>
#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[]);

View File

@ -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

View File

@ -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();

View File

@ -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

View File

@ -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 <algorithm>
@ -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);

View File

@ -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);
};