ladybird/LibGUI/GObject.cpp
Andreas Kling 95c3442d59 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.
2019-02-01 03:50:06 +01:00

80 lines
1.6 KiB
C++

#include "GObject.h"
#include "GEvent.h"
#include "GEventLoop.h"
#include <AK/Assertions.h>
GObject::GObject(GObject* parent)
: m_parent(parent)
{
if (m_parent)
m_parent->add_child(*this);
}
GObject::~GObject()
{
if (m_parent)
m_parent->remove_child(*this);
auto children_to_delete = move(m_children);
for (auto* child : children_to_delete)
delete child;
}
void GObject::event(GEvent& event)
{
switch (event.type()) {
case GEvent::Timer:
return timer_event(static_cast<GTimerEvent&>(event));
case GEvent::DeferredDestroy:
delete this;
break;
case GEvent::Invalid:
ASSERT_NOT_REACHED();
break;
default:
break;
}
}
void GObject::add_child(GObject& object)
{
m_children.append(&object);
}
void GObject::remove_child(GObject& object)
{
for (unsigned i = 0; i < m_children.size(); ++i) {
if (m_children[i] == &object) {
m_children.remove(i);
return;
}
}
}
void GObject::timer_event(GTimerEvent&)
{
}
void GObject::start_timer(int ms)
{
if (m_timer_id) {
dbgprintf("GObject{%p} already has a timer!\n", this);
ASSERT_NOT_REACHED();
}
m_timer_id = GEventLoop::main().register_timer(*this, ms, true);
}
void GObject::stop_timer()
{
if (!m_timer_id)
return;
bool success = GEventLoop::main().unregister_timer(m_timer_id);
ASSERT(success);
m_timer_id = 0;
}
void GObject::delete_later()
{
GEventLoop::main().post_event(this, make<GEvent>(GEvent::DeferredDestroy));
}