1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 02:07:34 +00:00

Implement event loop timers.

GObjects can now register a timer with the GEventLoop. This will eventually
cause GTimerEvents to be dispatched to the GObject.

This needed a few supporting changes in the kernel:

- The PIT now ticks 1000 times/sec.
- select() now supports an arbitrary timeout.
- gettimeofday() now returns something in the tv_usec field.

With these changes, the clock window in guitest2 finally ticks on its own.
This commit is contained in:
Andreas Kling 2019-02-01 03:50:06 +01:00
parent 9153666e72
commit 95c3442d59
10 changed files with 140 additions and 7 deletions

View file

@ -1,6 +1,7 @@
#pragma once
#include "GEvent.h"
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/Vector.h>
@ -23,6 +24,9 @@ public:
bool running() const { return m_running; }
int register_timer(GObject&, int milliseconds, bool should_reload);
bool unregister_timer(int timer_id);
private:
void wait_for_event();
void handle_paint_event(const GUI_Event&, GWindow&);
@ -30,6 +34,8 @@ private:
void handle_key_event(const GUI_Event&, GWindow&);
void handle_window_activation_event(const GUI_Event&, GWindow&);
void get_next_timer_expiration(timeval&);
struct QueuedEvent {
GObject* receiver { nullptr };
OwnPtr<GEvent> event;
@ -38,4 +44,19 @@ private:
int m_event_fd { -1 };
bool m_running { false };
int m_next_timer_id { 1 };
struct EventLoopTimer {
int timer_id { 0 };
int interval { 0 };
timeval fire_time;
bool should_reload { false };
GObject* owner { nullptr };
void reload();
bool has_expired() const;
};
HashMap<int, OwnPtr<EventLoopTimer>> m_timers;
};