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