2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/Timer.h>
|
2019-03-30 21:40:27 +01:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
Timer::Timer(Object* parent)
|
|
|
|
: Object(parent)
|
2019-03-30 21:40:27 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
Timer::Timer(int interval, Function<void()>&& timeout_handler, Object* parent)
|
|
|
|
: Object(parent)
|
2019-04-14 05:44:15 +02:00
|
|
|
, on_timeout(move(timeout_handler))
|
|
|
|
{
|
|
|
|
start(interval);
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
Timer::~Timer()
|
2019-03-30 21:40:27 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
void Timer::start()
|
2019-03-30 21:40:27 +01:00
|
|
|
{
|
|
|
|
start(m_interval);
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
void Timer::start(int interval)
|
2019-03-30 21:40:27 +01:00
|
|
|
{
|
|
|
|
if (m_active)
|
|
|
|
return;
|
2019-04-14 05:44:15 +02:00
|
|
|
m_interval = interval;
|
2019-03-30 21:40:27 +01:00
|
|
|
start_timer(interval);
|
|
|
|
m_active = true;
|
|
|
|
}
|
|
|
|
|
2020-06-11 22:35:37 +02:00
|
|
|
void Timer::restart()
|
|
|
|
{
|
|
|
|
restart(m_interval);
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
void Timer::restart(int interval)
|
2019-04-18 04:38:04 +02:00
|
|
|
{
|
|
|
|
if (m_active)
|
|
|
|
stop();
|
|
|
|
start(interval);
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
void Timer::stop()
|
2019-03-30 21:40:27 +01:00
|
|
|
{
|
|
|
|
if (!m_active)
|
|
|
|
return;
|
|
|
|
stop_timer();
|
|
|
|
m_active = false;
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
void Timer::timer_event(TimerEvent&)
|
2019-03-30 21:40:27 +01:00
|
|
|
{
|
|
|
|
if (m_single_shot)
|
|
|
|
stop();
|
2019-04-18 04:38:04 +02:00
|
|
|
else {
|
|
|
|
if (m_interval_dirty) {
|
|
|
|
stop();
|
|
|
|
start(m_interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 21:40:27 +01:00
|
|
|
if (on_timeout)
|
|
|
|
on_timeout();
|
|
|
|
}
|
2020-02-02 12:34:39 +01:00
|
|
|
|
|
|
|
}
|