2012-06-10 16:50:54 +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) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2004 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "platform/XWindowsEventQueueBuffer.h"
|
|
|
|
|
|
|
|
|
|
#include "base/Event.h"
|
|
|
|
|
#include "base/IEventQueue.h"
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "mt/Lock.h"
|
|
|
|
|
#include "mt/Thread.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
#if HAVE_UNISTD_H
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <unistd.h>
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
2024-07-29 17:04:17 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
#include <poll.h>
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// EventQueueTimer
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
class EventQueueTimer
|
|
|
|
|
{
|
|
|
|
|
};
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-12 11:28:41 +00:00
|
|
|
// XWindowsEventQueueBuffer
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
XWindowsEventQueueBuffer::XWindowsEventQueueBuffer(Display *display, Window window, IEventQueue *events)
|
2024-07-16 13:36:44 +00:00
|
|
|
: m_events(events),
|
|
|
|
|
m_display(display),
|
2025-05-22 22:22:20 +00:00
|
|
|
m_window(window)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-04-27 19:39:23 +00:00
|
|
|
assert(m_display != nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
assert(m_window != None);
|
|
|
|
|
|
2024-09-17 19:00:25 +00:00
|
|
|
m_userEvent = XInternAtom(m_display, "DESKFLOW_USER_EVENT", False);
|
2024-07-02 19:07:06 +00:00
|
|
|
// set up for pipe hack
|
2024-09-03 19:18:13 +00:00
|
|
|
int result = pipe2(m_pipefd, O_NONBLOCK);
|
2024-07-02 19:07:06 +00:00
|
|
|
assert(result == 0);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
XWindowsEventQueueBuffer::~XWindowsEventQueueBuffer()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// release pipe hack resources
|
|
|
|
|
close(m_pipefd[0]);
|
|
|
|
|
close(m_pipefd[1]);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
int XWindowsEventQueueBuffer::getPendingCountLocked()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
Lock lock(&m_mutex);
|
|
|
|
|
return XPending(m_display);
|
2020-05-20 19:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void XWindowsEventQueueBuffer::waitForEvent(double dtimeout)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
Thread::testCancel();
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// clear out the pipe in preparation for waiting.
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
char buf[16];
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// with linux automake, warnings are treated as errors by default
|
2025-05-02 12:03:44 +00:00
|
|
|
if (ssize_t read_response = read(m_pipefd[0], buf, 15); read_response < 0) {
|
2024-07-02 19:07:06 +00:00
|
|
|
// todo: handle read response
|
|
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
{
|
|
|
|
|
Lock lock(&m_mutex);
|
|
|
|
|
// we're now waiting for events
|
|
|
|
|
m_waiting = true;
|
|
|
|
|
|
|
|
|
|
// push out pending events
|
|
|
|
|
flush();
|
|
|
|
|
}
|
|
|
|
|
// calling flush may have queued up a new event.
|
|
|
|
|
if (!XWindowsEventQueueBuffer::isEmpty()) {
|
|
|
|
|
Thread::testCancel();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// use poll() to wait for a message from the X server or for timeout.
|
|
|
|
|
// this is a good deal more efficient than polling and sleeping.
|
|
|
|
|
struct pollfd pfds[2];
|
|
|
|
|
pfds[0].fd = ConnectionNumber(m_display);
|
|
|
|
|
pfds[0].events = POLLIN;
|
|
|
|
|
pfds[1].fd = m_pipefd[0];
|
|
|
|
|
pfds[1].events = POLLIN;
|
|
|
|
|
int timeout = (dtimeout < 0.0) ? -1 : static_cast<int>(1000.0 * dtimeout);
|
|
|
|
|
int remaining = timeout;
|
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
|
|
// It's possible that the X server has queued events locally
|
|
|
|
|
// in xlib's event buffer and not pushed on to the fd. Hence we
|
|
|
|
|
// can't simply monitor the fd as we may never be woken up.
|
|
|
|
|
// ie addEvent calls flush, XFlush may not send via the fd hence
|
|
|
|
|
// there is an event waiting to be sent but we must exit the poll
|
|
|
|
|
// before it can.
|
|
|
|
|
// Instead we poll for a brief period of time (so if events
|
|
|
|
|
// queued locally in the xlib buffer can be processed)
|
|
|
|
|
// and continue doing this until timeout is reached.
|
|
|
|
|
// The human eye can notice 60hz (ansi) which is 16ms, however
|
|
|
|
|
// we want to give the cpu a chance s owe up this to 25ms
|
2012-06-10 16:50:54 +00:00
|
|
|
#define TIMEOUT_DELAY 25
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
while (((dtimeout < 0.0) || (remaining > 0)) && getPendingCountLocked() == 0 && retval == 0) {
|
2024-07-29 17:04:17 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
retval = poll(pfds, 2, TIMEOUT_DELAY); // 16ms = 60hz, but we make it > to
|
|
|
|
|
// play nicely with the cpu
|
|
|
|
|
if (pfds[1].revents & POLLIN) {
|
|
|
|
|
ssize_t read_response = read(m_pipefd[0], buf, 15);
|
|
|
|
|
|
|
|
|
|
// with linux automake, warnings are treated as errors by default
|
|
|
|
|
if (read_response < 0) {
|
|
|
|
|
// todo: handle read response
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
remaining -= TIMEOUT_DELAY;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
{
|
|
|
|
|
// we're no longer waiting for events
|
|
|
|
|
Lock lock(&m_mutex);
|
|
|
|
|
m_waiting = false;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
Thread::testCancel();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-01-11 02:13:19 +00:00
|
|
|
IEventQueueBuffer::Type XWindowsEventQueueBuffer::getEvent(Event &event, uint32_t &dataID)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
Lock lock(&m_mutex);
|
|
|
|
|
|
|
|
|
|
// push out pending events
|
|
|
|
|
flush();
|
|
|
|
|
|
|
|
|
|
// get next event
|
|
|
|
|
XNextEvent(m_display, &m_event);
|
|
|
|
|
|
|
|
|
|
// process event
|
2024-10-17 18:04:35 +00:00
|
|
|
if (m_event.xany.type == ClientMessage && m_event.xclient.message_type == m_userEvent) {
|
2025-01-11 02:13:19 +00:00
|
|
|
dataID = static_cast<uint32_t>(m_event.xclient.data.l[0]);
|
2024-07-02 19:07:06 +00:00
|
|
|
return kUser;
|
|
|
|
|
} else {
|
2025-05-02 01:50:53 +00:00
|
|
|
event = Event(EventTypes::System, m_events->getSystemTarget(), &m_event);
|
2024-07-02 19:07:06 +00:00
|
|
|
return kSystem;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2025-01-11 02:13:19 +00:00
|
|
|
bool XWindowsEventQueueBuffer::addEvent(uint32_t dataID)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// prepare a message
|
|
|
|
|
XEvent xevent;
|
|
|
|
|
xevent.xclient.type = ClientMessage;
|
|
|
|
|
xevent.xclient.window = m_window;
|
|
|
|
|
xevent.xclient.message_type = m_userEvent;
|
|
|
|
|
xevent.xclient.format = 32;
|
|
|
|
|
xevent.xclient.data.l[0] = static_cast<long>(dataID);
|
|
|
|
|
|
|
|
|
|
// save the message
|
|
|
|
|
Lock lock(&m_mutex);
|
|
|
|
|
m_postedEvents.push_back(xevent);
|
|
|
|
|
|
|
|
|
|
// if we're currently waiting for an event then send saved events to
|
|
|
|
|
// the X server now. if we're not waiting then some other thread
|
|
|
|
|
// might be using the display connection so we can't safely use it
|
|
|
|
|
// too.
|
|
|
|
|
if (m_waiting) {
|
2016-12-28 11:50:32 +00:00
|
|
|
flush();
|
2024-07-02 19:07:06 +00:00
|
|
|
// Send a character through the round-trip pipe to wake a thread
|
|
|
|
|
// that is waiting for a ConnectionNumber() socket to be readable.
|
|
|
|
|
// The flush call can read incoming data from the socket and put
|
|
|
|
|
// it in Xlib's input buffer. That sneaks it past the other thread.
|
|
|
|
|
ssize_t write_response = write(m_pipefd[1], "!", 1);
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// with linux automake, warnings are treated as errors by default
|
|
|
|
|
if (write_response < 0) {
|
|
|
|
|
// todo: handle read response
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
return true;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
bool XWindowsEventQueueBuffer::isEmpty() const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
Lock lock(&m_mutex);
|
|
|
|
|
return (XPending(m_display) == 0);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
EventQueueTimer *XWindowsEventQueueBuffer::newTimer(double, bool) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
return new EventQueueTimer;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void XWindowsEventQueueBuffer::deleteTimer(EventQueueTimer *timer) const
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
delete timer;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void XWindowsEventQueueBuffer::flush()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// note -- m_mutex must be locked on entry
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// flush the posted event list to the X server
|
2025-05-22 01:55:49 +00:00
|
|
|
for (auto &event : m_postedEvents) {
|
|
|
|
|
XSendEvent(m_display, m_window, False, 0, &event);
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
XFlush(m_display);
|
|
|
|
|
m_postedEvents.clear();
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|