From a53cf813748927ed03a862be847769a58453950b Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 7 Apr 2020 22:15:22 +0200 Subject: [PATCH] LibCore: Add Core::Timer::create_single_shot() This is just a convenience function for creating single-shot timers. --- Libraries/LibCore/Timer.h | 10 ++++++++- Libraries/LibWeb/DOM/Document.cpp | 7 ++---- Servers/DHCPClient/DHCPv4Client.cpp | 16 ++++++------- Servers/WindowServer/Compositor.cpp | 35 ++++++++++++----------------- 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/Libraries/LibCore/Timer.h b/Libraries/LibCore/Timer.h index 94319c7e50..9f5aab0056 100644 --- a/Libraries/LibCore/Timer.h +++ b/Libraries/LibCore/Timer.h @@ -32,8 +32,16 @@ namespace Core { class Timer final : public Object { - C_OBJECT(Timer) + C_OBJECT(Timer); + public: + static NonnullRefPtr create_single_shot(int interval, Function&& timeout_handler, Object* parent = nullptr) + { + auto timer = adopt(*new Timer(interval, move(timeout_handler), parent)); + timer->set_single_shot(true); + return timer; + } + virtual ~Timer() override; void start(); diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp index d7fdc1dd5f..43c881ce8f 100644 --- a/Libraries/LibWeb/DOM/Document.cpp +++ b/Libraries/LibWeb/DOM/Document.cpp @@ -62,12 +62,9 @@ Document::Document() , m_style_resolver(make(*this)) , m_window(Window::create_with_document(*this)) { - m_style_update_timer = Core::Timer::construct(); - m_style_update_timer->set_single_shot(true); - m_style_update_timer->set_interval(0); - m_style_update_timer->on_timeout = [this] { + m_style_update_timer = Core::Timer::create_single_shot(0, [this] { update_style(); - }; + }); } Document::~Document() diff --git a/Servers/DHCPClient/DHCPv4Client.cpp b/Servers/DHCPClient/DHCPv4Client.cpp index 6161d02bf3..918a20b93f 100644 --- a/Servers/DHCPClient/DHCPv4Client.cpp +++ b/Servers/DHCPClient/DHCPv4Client.cpp @@ -165,14 +165,14 @@ void DHCPv4Client::handle_ack(const DHCPv4Packet& packet, const ParsedDHCPv4Opti auto new_ip = packet.yiaddr(); auto lease_time = convert_between_host_and_network(options.get(DHCPOption::IPAddressLeaseTime).value_or(transaction->offered_lease_time)); // set a timer for the duration of the lease, we shall renew if needed - Core::Timer::construct( - lease_time * 1000, [this, transaction, interface = InterfaceDescriptor { interface }, new_ip] { + Core::Timer::create_single_shot( + lease_time * 1000, + [this, transaction, interface = InterfaceDescriptor { interface }, new_ip] { transaction->accepted_offer = false; transaction->has_ip = false; dhcp_discover(interface, new_ip); }, - this) - ->set_single_shot(true); + this); set_params(transaction->interface, new_ip, options.get(DHCPOption::SubnetMask).value(), options.get_many(DHCPOption::Router, 1).first()); } @@ -189,12 +189,12 @@ void DHCPv4Client::handle_nak(const DHCPv4Packet& packet, const ParsedDHCPv4Opti transaction->accepted_offer = false; transaction->has_ip = false; auto& iface = transaction->interface; - Core::Timer::construct( - 10000, [this, iface = InterfaceDescriptor { iface }] { + Core::Timer::create_single_shot( + 10000, + [this, iface = InterfaceDescriptor { iface }] { dhcp_discover(iface); }, - this) - ->set_single_shot(true); + this); } void DHCPv4Client::process_incoming(const DHCPv4Packet& packet) diff --git a/Servers/WindowServer/Compositor.cpp b/Servers/WindowServer/Compositor.cpp index 0a0ab29f1d..76526bb18f 100644 --- a/Servers/WindowServer/Compositor.cpp +++ b/Servers/WindowServer/Compositor.cpp @@ -62,30 +62,23 @@ WallpaperMode mode_to_enum(const String& name) Compositor::Compositor() { - m_compose_timer = add(); - m_immediate_compose_timer = add(); + m_compose_timer = Core::Timer::create_single_shot( + 1000 / 60, + [this] { + notify_display_links(); + compose(); + }, + this); + + m_immediate_compose_timer = Core::Timer::create_single_shot( + 0, + [this] { + compose(); + }, + this); m_screen_can_set_buffer = Screen::the().can_set_buffer(); - init_bitmaps(); - - m_compose_timer->on_timeout = [&]() { - notify_display_links(); -#if defined(COMPOSITOR_DEBUG) - dbgprintf("Compositor: delayed frame callback: %d rects\n", m_dirty_rects.size()); -#endif - compose(); - }; - m_compose_timer->set_single_shot(true); - m_compose_timer->set_interval(1000 / 60); - m_immediate_compose_timer->on_timeout = [this]() { -#if defined(COMPOSITOR_DEBUG) - dbgprintf("Compositor: immediate frame callback: %d rects\n", m_dirty_rects.size()); -#endif - compose(); - }; - m_immediate_compose_timer->set_single_shot(true); - m_immediate_compose_timer->set_interval(0); } void Compositor::init_bitmaps()