2024-08-30 14:53:25 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2024 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2022 Red Hat, Inc.
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2024-08-30 14:53:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "platform/EiEventQueueBuffer.h"
|
|
|
|
|
|
|
|
|
|
#include "base/Event.h"
|
|
|
|
|
#include "base/EventTypes.h"
|
|
|
|
|
#include "base/IEventQueue.h"
|
|
|
|
|
#include "base/Log.h"
|
|
|
|
|
#include "mt/Thread.h"
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <poll.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
class EventQueueTimer
|
|
|
|
|
{
|
|
|
|
|
};
|
2024-08-30 14:53:25 +00:00
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
namespace deskflow {
|
2024-08-30 14:53:25 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
EiEventQueueBuffer::EiEventQueueBuffer(EiScreen *screen, ei *ei, IEventQueue *events) : ei_(ei_ref(ei)), events_(events)
|
|
|
|
|
{
|
2024-08-30 14:53:25 +00:00
|
|
|
// We need a pipe to signal ourselves when addEvent() is called
|
|
|
|
|
int pipefd[2];
|
2024-09-03 19:18:13 +00:00
|
|
|
int result = pipe2(pipefd, O_NONBLOCK);
|
2024-08-30 14:53:25 +00:00
|
|
|
assert(result == 0);
|
|
|
|
|
|
|
|
|
|
pipe_r_ = pipefd[0];
|
|
|
|
|
pipe_w_ = pipefd[1];
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
EiEventQueueBuffer::~EiEventQueueBuffer()
|
|
|
|
|
{
|
2024-08-30 14:53:25 +00:00
|
|
|
ei_unref(ei_);
|
|
|
|
|
close(pipe_r_);
|
|
|
|
|
close(pipe_w_);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void EiEventQueueBuffer::waitForEvent(double timeout_in_ms)
|
|
|
|
|
{
|
2024-08-30 14:53:25 +00:00
|
|
|
Thread::testCancel();
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
enum
|
|
|
|
|
{
|
2024-08-30 14:53:25 +00:00
|
|
|
EIFD,
|
|
|
|
|
PIPEFD,
|
|
|
|
|
POLLFD_COUNT, // Last element
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct pollfd pfds[POLLFD_COUNT];
|
|
|
|
|
pfds[EIFD].fd = ei_get_fd(ei_);
|
|
|
|
|
pfds[EIFD].events = POLLIN;
|
|
|
|
|
pfds[PIPEFD].fd = pipe_r_;
|
|
|
|
|
pfds[PIPEFD].events = POLLIN;
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
int timeout = (timeout_in_ms < 0.0) ? -1 : static_cast<int>(1000.0 * timeout_in_ms);
|
2024-08-30 14:53:25 +00:00
|
|
|
|
|
|
|
|
int retval = poll(pfds, POLLFD_COUNT, timeout);
|
|
|
|
|
if (retval > 0) {
|
|
|
|
|
if (pfds[EIFD].revents & POLLIN) {
|
2025-03-10 13:35:42 +00:00
|
|
|
std::lock_guard lock(mutex_);
|
2024-08-30 14:53:25 +00:00
|
|
|
|
|
|
|
|
// libei doesn't allow ei_event_ref() because events are
|
2025-04-27 19:39:23 +00:00
|
|
|
// supposed to be short-lived only. So instead, we create an nullptr-data
|
2024-08-30 14:53:25 +00:00
|
|
|
// kSystemEvent whenever there's data on the fd, shove that event
|
|
|
|
|
// into our event queue and once we process the event (see
|
|
|
|
|
// getEvent()), the EiScreen will call ei_dispatch() and process
|
|
|
|
|
// all actual pending ei events. In theory this means that a
|
|
|
|
|
// flood of ei events could starve the events added with
|
|
|
|
|
// addEvents() but let's hope it doesn't come to that.
|
|
|
|
|
queue_.push({true, 0U});
|
|
|
|
|
}
|
|
|
|
|
// the pipefd data doesn't matter, it only exists to wake up the thread
|
|
|
|
|
// and potentially testCancel
|
|
|
|
|
if (pfds[PIPEFD].revents & POLLIN) {
|
|
|
|
|
char buf[64];
|
2025-05-01 00:27:50 +00:00
|
|
|
ssize_t total = 0;
|
|
|
|
|
ssize_t result;
|
|
|
|
|
while ((result = read(pipe_r_, buf, sizeof(buf)) > 0)) {
|
|
|
|
|
total += result;
|
|
|
|
|
}
|
|
|
|
|
LOG_DEBUG2("event queue read result: %d (total drained: %zd)", result, total);
|
2024-08-30 14:53:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Thread::testCancel();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
IEventQueueBuffer::Type EiEventQueueBuffer::getEvent(Event &event, uint32_t &dataID)
|
|
|
|
|
{
|
2024-08-30 14:53:25 +00:00
|
|
|
// the addEvent/getEvent pair is a bit awkward for libei.
|
|
|
|
|
//
|
|
|
|
|
// it assumes that there's a nice queue of events sitting there that we can
|
|
|
|
|
// just append to and get everything back out in the same order. We *could*
|
|
|
|
|
// emulate that by taking the libei events immediately out of the event
|
|
|
|
|
// queue after dispatch (see above) and putting it into the event queue,
|
|
|
|
|
// intermixed with whatever addEvents() did.
|
|
|
|
|
//
|
|
|
|
|
// But this makes locking more awkward and libei isn't really designed to
|
|
|
|
|
// keep calling ei_dispatch() while we hold a bunch of event refs. So instead
|
|
|
|
|
// we just have a "something happened" event on the ei fd and the rest is
|
|
|
|
|
// handled by the EiScreen.
|
|
|
|
|
//
|
2025-03-10 13:35:42 +00:00
|
|
|
std::lock_guard lock(mutex_);
|
2024-08-30 14:53:25 +00:00
|
|
|
auto pair = queue_.front();
|
|
|
|
|
queue_.pop();
|
|
|
|
|
|
|
|
|
|
// if this an injected special event, just return the data and exit
|
|
|
|
|
if (pair.first == false) {
|
|
|
|
|
dataID = pair.second;
|
|
|
|
|
return kUser;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-02 01:50:53 +00:00
|
|
|
event = Event(EventTypes::System, events_->getSystemTarget());
|
2024-08-30 14:53:25 +00:00
|
|
|
|
|
|
|
|
return kSystem;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool EiEventQueueBuffer::addEvent(uint32_t dataID)
|
|
|
|
|
{
|
2025-03-10 13:35:42 +00:00
|
|
|
std::lock_guard lock(mutex_);
|
2024-08-30 14:53:25 +00:00
|
|
|
queue_.push({false, dataID});
|
|
|
|
|
|
|
|
|
|
// tickle the pipe so our read thread wakes up
|
|
|
|
|
auto result = write(pipe_w_, "!", 1);
|
2024-09-09 12:33:57 +00:00
|
|
|
LOG_DEBUG2("event queue write result: %d", result);
|
2024-08-30 14:53:25 +00:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool EiEventQueueBuffer::isEmpty() const
|
|
|
|
|
{
|
2025-03-10 13:35:42 +00:00
|
|
|
std::lock_guard lock(mutex_);
|
2024-08-30 14:53:25 +00:00
|
|
|
|
|
|
|
|
return queue_.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
EventQueueTimer *EiEventQueueBuffer::newTimer(double duration, bool oneShot) const
|
|
|
|
|
{
|
2024-08-30 14:53:25 +00:00
|
|
|
return new EventQueueTimer;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void EiEventQueueBuffer::deleteTimer(EventQueueTimer *timer) const
|
|
|
|
|
{
|
2024-08-30 14:53:25 +00:00
|
|
|
delete timer;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
} // namespace deskflow
|