File indexing completed on 2024-12-22 05:09:20

0001 /*
0002     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 #include "event_queue.h"
0007 #include "connection_thread.h"
0008 #include "wayland_pointer_p.h"
0009 
0010 #include <wayland-client.h>
0011 
0012 namespace KWayland
0013 {
0014 namespace Client
0015 {
0016 class Q_DECL_HIDDEN EventQueue::Private
0017 {
0018 public:
0019     wl_display *display = nullptr;
0020     WaylandPointer<wl_event_queue, wl_event_queue_destroy> queue;
0021 };
0022 
0023 EventQueue::EventQueue(QObject *parent)
0024     : QObject(parent)
0025     , d(new Private)
0026 {
0027 }
0028 
0029 EventQueue::~EventQueue()
0030 {
0031     release();
0032 }
0033 
0034 void EventQueue::release()
0035 {
0036     d->queue.release();
0037     d->display = nullptr;
0038 }
0039 
0040 void EventQueue::destroy()
0041 {
0042     d->queue.destroy();
0043     d->display = nullptr;
0044 }
0045 
0046 bool EventQueue::isValid()
0047 {
0048     return d->queue.isValid();
0049 }
0050 
0051 void EventQueue::setup(wl_display *display)
0052 {
0053     Q_ASSERT(display);
0054     Q_ASSERT(!d->display);
0055     Q_ASSERT(!d->queue);
0056     d->display = display;
0057     d->queue.setup(wl_display_create_queue(display));
0058 }
0059 
0060 void EventQueue::setup(ConnectionThread *connection)
0061 {
0062     setup(connection->display());
0063     connect(connection, &ConnectionThread::eventsRead, this, &EventQueue::dispatch, Qt::QueuedConnection);
0064 }
0065 
0066 void EventQueue::dispatch()
0067 {
0068     if (!d->display || !d->queue) {
0069         return;
0070     }
0071     wl_display_dispatch_queue_pending(d->display, d->queue);
0072     wl_display_flush(d->display);
0073 }
0074 
0075 void EventQueue::addProxy(wl_proxy *proxy)
0076 {
0077     Q_ASSERT(d->queue);
0078     wl_proxy_set_queue(proxy, d->queue);
0079 }
0080 
0081 EventQueue::operator wl_event_queue *() const
0082 {
0083     return d->queue;
0084 }
0085 
0086 EventQueue::operator wl_event_queue *()
0087 {
0088     return d->queue;
0089 }
0090 
0091 }
0092 }
0093 
0094 #include "moc_event_queue.cpp"