2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2022-02-26 16:09:45 +00:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/Timer.h>
|
2019-03-30 20:40:27 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
2024-04-16 18:34:01 +00:00
|
|
|
NonnullRefPtr<Timer> Timer::create()
|
|
|
|
{
|
|
|
|
return adopt_ref(*new Timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<Timer> Timer::create_repeating(int interval_ms, Function<void()>&& timeout_handler, EventReceiver* parent)
|
|
|
|
{
|
|
|
|
return adopt_ref(*new Timer(interval_ms, move(timeout_handler), parent));
|
|
|
|
}
|
|
|
|
|
|
|
|
NonnullRefPtr<Timer> Timer::create_single_shot(int interval_ms, Function<void()>&& timeout_handler, EventReceiver* parent)
|
|
|
|
{
|
|
|
|
auto timer = adopt_ref(*new Timer(interval_ms, move(timeout_handler), parent));
|
|
|
|
timer->set_single_shot(true);
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer::~Timer() = default;
|
|
|
|
|
2023-08-06 16:09:39 +00:00
|
|
|
Timer::Timer(EventReceiver* parent)
|
|
|
|
: EventReceiver(parent)
|
2019-03-30 20:40:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-08-06 16:09:39 +00:00
|
|
|
Timer::Timer(int interval_ms, Function<void()>&& timeout_handler, EventReceiver* parent)
|
|
|
|
: EventReceiver(parent)
|
2019-04-14 03:44:15 +00:00
|
|
|
, on_timeout(move(timeout_handler))
|
2023-01-11 19:36:46 +00:00
|
|
|
, m_interval_ms(interval_ms)
|
2019-04-14 03:44:15 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void Timer::start()
|
2019-03-30 20:40:27 +00:00
|
|
|
{
|
2021-05-10 09:29:41 +00:00
|
|
|
start(m_interval_ms);
|
2019-03-30 20:40:27 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 09:29:41 +00:00
|
|
|
void Timer::start(int interval_ms)
|
2019-03-30 20:40:27 +00:00
|
|
|
{
|
|
|
|
if (m_active)
|
|
|
|
return;
|
2021-05-10 09:29:41 +00:00
|
|
|
m_interval_ms = interval_ms;
|
|
|
|
start_timer(interval_ms);
|
2019-03-30 20:40:27 +00:00
|
|
|
m_active = true;
|
|
|
|
}
|
|
|
|
|
2020-06-11 20:35:37 +00:00
|
|
|
void Timer::restart()
|
|
|
|
{
|
2021-05-10 09:29:41 +00:00
|
|
|
restart(m_interval_ms);
|
2020-06-11 20:35:37 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 09:29:41 +00:00
|
|
|
void Timer::restart(int interval_ms)
|
2019-04-18 02:38:04 +00:00
|
|
|
{
|
|
|
|
if (m_active)
|
|
|
|
stop();
|
2021-05-10 09:29:41 +00:00
|
|
|
start(interval_ms);
|
2019-04-18 02:38:04 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void Timer::stop()
|
2019-03-30 20:40:27 +00:00
|
|
|
{
|
|
|
|
if (!m_active)
|
|
|
|
return;
|
|
|
|
stop_timer();
|
|
|
|
m_active = false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 12:25:24 +00:00
|
|
|
void Timer::set_active(bool active)
|
|
|
|
{
|
|
|
|
if (active)
|
|
|
|
start();
|
|
|
|
else
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void Timer::timer_event(TimerEvent&)
|
2019-03-30 20:40:27 +00:00
|
|
|
{
|
|
|
|
if (m_single_shot)
|
|
|
|
stop();
|
2019-04-18 02:38:04 +00:00
|
|
|
else {
|
|
|
|
if (m_interval_dirty) {
|
|
|
|
stop();
|
2021-05-10 09:29:41 +00:00
|
|
|
start(m_interval_ms);
|
2019-04-18 02:38:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 20:40:27 +00:00
|
|
|
if (on_timeout)
|
|
|
|
on_timeout();
|
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
|
|
|
|
}
|