2020-01-18 03:38:21 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-26 11:09:45 -05:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 03:38:21 -05:00
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 03:38:21 -05:00
|
|
|
*/
|
|
|
|
|
2019-03-30 16:40:27 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2023-08-06 12:09:39 -04:00
|
|
|
#include <LibCore/EventReceiver.h>
|
2019-03-30 16:40:27 -04:00
|
|
|
|
2020-02-02 06:34:39 -05:00
|
|
|
namespace Core {
|
|
|
|
|
2023-08-06 12:09:39 -04:00
|
|
|
class Timer final : public EventReceiver {
|
2020-04-07 16:15:22 -04:00
|
|
|
C_OBJECT(Timer);
|
|
|
|
|
2019-03-30 16:40:27 -04:00
|
|
|
public:
|
2023-08-06 12:09:39 -04:00
|
|
|
static ErrorOr<NonnullRefPtr<Timer>> create_repeating(int interval_ms, Function<void()>&& timeout_handler, EventReceiver* parent = nullptr)
|
2021-03-27 16:33:31 -04:00
|
|
|
{
|
2023-01-11 14:36:46 -05:00
|
|
|
return adopt_nonnull_ref_or_enomem(new Timer(interval_ms, move(timeout_handler), parent));
|
2021-03-27 16:33:31 -04:00
|
|
|
}
|
2023-08-06 12:09:39 -04:00
|
|
|
static ErrorOr<NonnullRefPtr<Timer>> create_single_shot(int interval_ms, Function<void()>&& timeout_handler, EventReceiver* parent = nullptr)
|
2020-04-07 16:15:22 -04:00
|
|
|
{
|
2023-01-11 11:31:10 -05:00
|
|
|
auto timer = TRY(adopt_nonnull_ref_or_enomem(new Timer(interval_ms, move(timeout_handler), parent)));
|
2020-04-07 16:15:22 -04:00
|
|
|
timer->set_single_shot(true);
|
|
|
|
return timer;
|
|
|
|
}
|
|
|
|
|
2022-02-26 11:09:45 -05:00
|
|
|
virtual ~Timer() override = default;
|
2019-03-30 16:40:27 -04:00
|
|
|
|
|
|
|
void start();
|
2021-05-10 05:29:41 -04:00
|
|
|
void start(int interval_ms);
|
2020-06-11 16:35:37 -04:00
|
|
|
void restart();
|
2021-05-10 05:29:41 -04:00
|
|
|
void restart(int interval_ms);
|
2019-03-30 16:40:27 -04:00
|
|
|
void stop();
|
|
|
|
|
2021-12-25 07:25:24 -05:00
|
|
|
void set_active(bool);
|
|
|
|
|
2019-03-30 16:40:27 -04:00
|
|
|
bool is_active() const { return m_active; }
|
2021-05-10 05:29:41 -04:00
|
|
|
int interval() const { return m_interval_ms; }
|
|
|
|
void set_interval(int interval_ms)
|
2019-04-17 22:38:04 -04:00
|
|
|
{
|
2021-05-10 05:29:41 -04:00
|
|
|
if (m_interval_ms == interval_ms)
|
2019-04-17 22:38:04 -04:00
|
|
|
return;
|
2021-05-10 05:29:41 -04:00
|
|
|
m_interval_ms = interval_ms;
|
2019-04-17 22:38:04 -04:00
|
|
|
m_interval_dirty = true;
|
|
|
|
}
|
2019-03-30 16:40:27 -04:00
|
|
|
|
|
|
|
bool is_single_shot() const { return m_single_shot; }
|
|
|
|
void set_single_shot(bool single_shot) { m_single_shot = single_shot; }
|
|
|
|
|
|
|
|
Function<void()> on_timeout;
|
|
|
|
|
|
|
|
private:
|
2023-08-06 12:09:39 -04:00
|
|
|
explicit Timer(EventReceiver* parent = nullptr);
|
|
|
|
Timer(int interval_ms, Function<void()>&& timeout_handler, EventReceiver* parent = nullptr);
|
2019-09-20 09:19:46 -04:00
|
|
|
|
2020-02-02 06:34:39 -05:00
|
|
|
virtual void timer_event(TimerEvent&) override;
|
2019-03-30 16:40:27 -04:00
|
|
|
|
|
|
|
bool m_active { false };
|
|
|
|
bool m_single_shot { false };
|
2019-04-17 22:38:04 -04:00
|
|
|
bool m_interval_dirty { false };
|
2021-05-10 05:29:41 -04:00
|
|
|
int m_interval_ms { 0 };
|
2019-03-30 16:40:27 -04:00
|
|
|
};
|
2020-02-02 06:34:39 -05:00
|
|
|
|
|
|
|
}
|