2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/Event.h>
|
|
|
|
#include <LibCore/EventLoop.h>
|
|
|
|
#include <LibCore/Notifier.h>
|
2019-04-10 15:35:43 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
namespace Core {
|
|
|
|
|
2023-08-06 16:09:39 +00:00
|
|
|
Notifier::Notifier(int fd, Type type, EventReceiver* parent)
|
|
|
|
: EventReceiver(parent)
|
2019-08-18 09:54:39 +00:00
|
|
|
, m_fd(fd)
|
2023-04-23 18:59:32 +00:00
|
|
|
, m_type(type)
|
2019-04-10 15:35:43 +00:00
|
|
|
{
|
2019-07-16 13:02:22 +00:00
|
|
|
set_enabled(true);
|
2019-04-10 15:35:43 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Notifier::~Notifier()
|
2019-04-10 15:35:43 +00:00
|
|
|
{
|
2019-07-16 13:02:22 +00:00
|
|
|
set_enabled(false);
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void Notifier::set_enabled(bool enabled)
|
2019-07-16 13:02:22 +00:00
|
|
|
{
|
2020-09-16 15:42:59 +00:00
|
|
|
if (m_fd < 0)
|
|
|
|
return;
|
2024-02-02 01:21:15 +00:00
|
|
|
if (enabled == m_is_enabled)
|
|
|
|
return;
|
|
|
|
m_is_enabled = enabled;
|
2019-07-16 13:02:22 +00:00
|
|
|
if (enabled)
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::EventLoop::register_notifier({}, *this);
|
2019-07-16 13:02:22 +00:00
|
|
|
else
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::EventLoop::unregister_notifier({}, *this);
|
2019-04-10 15:35:43 +00:00
|
|
|
}
|
2019-07-16 18:31:14 +00:00
|
|
|
|
2020-09-16 15:42:59 +00:00
|
|
|
void Notifier::close()
|
|
|
|
{
|
|
|
|
if (m_fd < 0)
|
|
|
|
return;
|
|
|
|
set_enabled(false);
|
|
|
|
m_fd = -1;
|
|
|
|
}
|
|
|
|
|
2024-02-02 01:21:15 +00:00
|
|
|
void Notifier::set_type(Type type)
|
|
|
|
{
|
|
|
|
if (m_is_enabled) {
|
|
|
|
// FIXME: Directly communicate intent to the EventLoop.
|
|
|
|
set_enabled(false);
|
|
|
|
m_type = type;
|
|
|
|
set_enabled(true);
|
|
|
|
} else {
|
|
|
|
m_type = type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
void Notifier::event(Core::Event& event)
|
2019-07-16 18:31:14 +00:00
|
|
|
{
|
2023-04-23 18:59:32 +00:00
|
|
|
if (event.type() == Core::Event::NotifierActivation) {
|
|
|
|
if (on_activation)
|
|
|
|
on_activation();
|
|
|
|
return;
|
2019-07-16 18:31:14 +00:00
|
|
|
}
|
2023-08-06 16:09:39 +00:00
|
|
|
EventReceiver::event(event);
|
2019-07-16 18:31:14 +00:00
|
|
|
}
|
2020-02-02 11:34:39 +00:00
|
|
|
|
|
|
|
}
|