1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:17:44 +00:00

LibCore+Ladybird: Add EventLoopManager interface for persistent state

Things such as timers and notifiers aren't specific to one instance of
Core::EventLoop, so let's not tie them down to EventLoopImplementation.

Instead, move those APIs + signals & a few other things to a new
EventLoopManager interface. EventLoopManager also knows how to create a
new EventLoopImplementation object.
This commit is contained in:
Andreas Kling 2023-04-25 17:38:48 +02:00
parent c21eb30a2b
commit 7b963e1e98
11 changed files with 177 additions and 118 deletions

View file

@ -35,11 +35,6 @@ struct ThreadData {
EventLoopImplementationQt::EventLoopImplementationQt()
{
m_process_core_events_timer.setSingleShot(true);
m_process_core_events_timer.setInterval(0);
QObject::connect(&m_process_core_events_timer, &QTimer::timeout, [] {
Core::ThreadEventQueue::current().process();
});
}
EventLoopImplementationQt::~EventLoopImplementationQt() = default;
@ -79,7 +74,7 @@ void EventLoopImplementationQt::wake()
m_event_loop.wakeUp();
}
void EventLoopImplementationQt::deferred_invoke(Function<void()> function)
void EventLoopManagerQt::deferred_invoke(Function<void()> function)
{
VERIFY(function);
QTimer::singleShot(0, [function = move(function)] {
@ -97,7 +92,7 @@ static void qt_timer_fired(int timer_id, Core::TimerShouldFireWhenNotVisible sho
object.dispatch_event(event);
}
int EventLoopImplementationQt::register_timer(Core::Object& object, int milliseconds, bool should_reload, Core::TimerShouldFireWhenNotVisible should_fire_when_not_visible)
int EventLoopManagerQt::register_timer(Core::Object& object, int milliseconds, bool should_reload, Core::TimerShouldFireWhenNotVisible should_fire_when_not_visible)
{
auto& thread_data = ThreadData::the();
auto timer = make<QTimer>();
@ -116,7 +111,7 @@ int EventLoopImplementationQt::register_timer(Core::Object& object, int millisec
return timer_id;
}
bool EventLoopImplementationQt::unregister_timer(int timer_id)
bool EventLoopManagerQt::unregister_timer(int timer_id)
{
auto& thread_data = ThreadData::the();
thread_data.timer_id_allocator.deallocate(timer_id);
@ -129,7 +124,7 @@ static void qt_notifier_activated(Core::Notifier& notifier)
notifier.dispatch_event(event);
}
void EventLoopImplementationQt::register_notifier(Core::Notifier& notifier)
void EventLoopManagerQt::register_notifier(Core::Notifier& notifier)
{
QSocketNotifier::Type type;
switch (notifier.type()) {
@ -150,14 +145,34 @@ void EventLoopImplementationQt::register_notifier(Core::Notifier& notifier)
ThreadData::the().notifiers.set(&notifier, move(socket_notifier));
}
void EventLoopImplementationQt::unregister_notifier(Core::Notifier& notifier)
void EventLoopManagerQt::unregister_notifier(Core::Notifier& notifier)
{
ThreadData::the().notifiers.remove(&notifier);
}
void EventLoopImplementationQt::did_post_event()
void EventLoopManagerQt::did_post_event()
{
m_process_core_events_timer.start();
}
EventLoopManagerQt::EventLoopManagerQt()
{
m_process_core_events_timer.setSingleShot(true);
m_process_core_events_timer.setInterval(0);
QObject::connect(&m_process_core_events_timer, &QTimer::timeout, [] {
Core::ThreadEventQueue::current().process();
});
}
EventLoopManagerQt::~EventLoopManagerQt() = default;
void EventLoopManagerQt::wake()
{
}
NonnullOwnPtr<Core::EventLoopImplementation> EventLoopManagerQt::make_implementation()
{
return adopt_own(*new EventLoopImplementationQt);
}
}

View file

@ -16,16 +16,11 @@
namespace Ladybird {
class EventLoopImplementationQt final : public Core::EventLoopImplementation {
class EventLoopManagerQt final : public Core::EventLoopManager {
public:
static NonnullOwnPtr<EventLoopImplementationQt> create() { return adopt_own(*new EventLoopImplementationQt); }
virtual ~EventLoopImplementationQt() override;
virtual int exec() override;
virtual size_t pump(PumpMode) override;
virtual void quit(int) override;
virtual void wake() override;
EventLoopManagerQt();
virtual ~EventLoopManagerQt() override;
virtual NonnullOwnPtr<Core::EventLoopImplementation> make_implementation() override;
virtual void deferred_invoke(Function<void()>) override;
@ -37,21 +32,41 @@ public:
virtual void did_post_event() override;
virtual void wake() override;
// FIXME: These APIs only exist for obscure use-cases inside SerenityOS. Try to get rid of them.
virtual int register_signal(int, Function<void(int)>) override { return 0; }
virtual void unregister_signal(int) override { }
private:
QTimer m_process_core_events_timer;
};
class EventLoopImplementationQt final : public Core::EventLoopImplementation {
public:
static NonnullOwnPtr<EventLoopImplementationQt> create() { return adopt_own(*new EventLoopImplementationQt); }
virtual ~EventLoopImplementationQt() override;
virtual int exec() override;
virtual size_t pump(PumpMode) override;
virtual void quit(int) override;
virtual void wake() override;
// FIXME: These APIs only exist for obscure use-cases inside SerenityOS. Try to get rid of them.
virtual void unquit() override { }
virtual bool was_exit_requested() const override { return false; }
virtual void notify_forked_and_in_child() override { }
virtual int register_signal(int, Function<void(int)>) override { return 0; }
virtual void unregister_signal(int) override { }
void set_main_loop() { m_main_loop = true; }
private:
friend class EventLoopManagerQt;
EventLoopImplementationQt();
bool is_main_loop() const { return m_main_loop; }
QEventLoop m_event_loop;
QTimer m_process_core_events_timer;
bool m_main_loop { false };
};

View file

@ -40,7 +40,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
{
QGuiApplication app(arguments.argc, arguments.argv);
Core::EventLoop::make_implementation = Ladybird::EventLoopImplementationQt::create;
Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
Core::EventLoop event_loop;
platform_init();

View file

@ -54,7 +54,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
{
QApplication app(arguments.argc, arguments.argv);
Core::EventLoop::make_implementation = Ladybird::EventLoopImplementationQt::create;
Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
Core::EventLoop event_loop;
static_cast<Ladybird::EventLoopImplementationQt&>(event_loop.impl()).set_main_loop();