KWayland

event_queue.h
1 /*
2  SPDX-FileCopyrightText: 2014 Martin Gräßlin <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 #ifndef WAYLAND_EVENT_QUEUE_H
7 #define WAYLAND_EVENT_QUEUE_H
8 
9 #include <QObject>
10 
11 #include <KWayland/Client/kwaylandclient_export.h>
12 
13 struct wl_display;
14 struct wl_proxy;
15 struct wl_event_queue;
16 
17 namespace KWayland
18 {
19 namespace Client
20 {
21 
22 class ConnectionThread;
23 
24 /**
25  * @short Wrapper class for wl_event_queue interface.
26  *
27  * The EventQueue is needed if a different thread is used for the connection.
28  * If the interface wrappers are held in a different thread than the connection thread
29  * an EventQueue is needed for the thread which holds the interface wrappers. A common
30  * example is a dedicated connection thread while the interface wrappers are created
31  * in the main thread.
32  *
33  * All interface wrappers are set up to support the EventQueue in the most convenient
34  * way. The EventQueue needs only to be passed to the Registry. The EventQueue will then
35  * be passed to all created wrappers through the tree.
36  *
37  * @code
38  * ConnectionThread connection;
39  * EventQueue queue;
40  * Registry registry;
41  *
42  * connect(&connection, &ConnectionThread::connected, this, [&] {
43  * queue.setup(&connection);
44  * registry.setEventQueue(&queue);
45  * registry.setup(&connection);
46  * registry.create();
47  * });
48  *
49  * connection.initConnection();
50  * @endcode
51  *
52  * The EventQueue can be used as a drop-in replacement for any wl_event_queue
53  * pointer as it provides matching cast operators.
54  **/
55 class KWAYLANDCLIENT_EXPORT EventQueue : public QObject
56 {
57  Q_OBJECT
58 public:
59  explicit EventQueue(QObject *parent = nullptr);
60  virtual ~EventQueue();
61 
62  /**
63  * Creates the event queue for the @p display.
64  *
65  * Note: this will not automatically setup the dispatcher.
66  * When using this method one needs to ensure that dispatch
67  * gets invoked whenever new events need to be dispatched.
68  * @see dispatch
69  **/
70  void setup(wl_display *display);
71  /**
72  * Creates the event queue for the @p connection.
73  *
74  * This method also connects the eventsRead signal of the ConnectionThread
75  * to the dispatch method. Events will be automatically dispatched without
76  * the need to call dispatch manually.
77  * @see dispatch
78  **/
79  void setup(ConnectionThread *connection);
80 
81  /**
82  * @returns @c true if EventQueue is setup.
83  **/
84  bool isValid();
85  /**
86  * Releases the wl_event_queue interface.
87  * After the interface has been released the EventQueue instance is no
88  * longer valid and can be setup with another wl_event_queue interface.
89  **/
90  void release();
91  /**
92  * Destroys the data held by this EventQueue.
93  * This method is supposed to be used when the connection to the Wayland
94  * server goes away. If the connection is not valid anymore, it's not
95  * possible to call release anymore as that calls into the Wayland
96  * connection and the call would fail. This method cleans up the data, so
97  * that the instance can be deleted or set up to a new wl_event_queue interface
98  * once there is a new connection available.
99  *
100  * @see release
101  **/
102  void destroy();
103 
104  /**
105  * Adds the @p proxy to the EventQueue.
106  **/
107  void addProxy(wl_proxy *proxy);
108  /**
109  * Adds the @p proxy of type wl_interface (e.g. wl_compositor) to the EventQueue.
110  **/
111  template <typename wl_interface>
112  void addProxy(wl_interface *proxy);
113  /**
114  * Adds the @p proxy wrapper class of type T referencing the wl_interface to the EventQueue.
115  **/
116  template <typename wl_interface, typename T>
117  void addProxy(T *proxy);
118 
119  operator wl_event_queue*();
120  operator wl_event_queue*() const;
121 
122 public Q_SLOTS:
123  /**
124  * Dispatches all pending events on the EventQueue.
125  **/
126  void dispatch();
127 
128 private:
129  class Private;
131 };
132 
133 template <typename wl_interface>
134 inline
135 void EventQueue::addProxy(wl_interface *proxy)
136 {
137  addProxy(reinterpret_cast<wl_proxy*>(proxy));
138 }
139 
140 template <typename wl_interface, typename T>
141 inline
142 void EventQueue::addProxy(T *proxy)
143 {
144  addProxy(reinterpret_cast<wl_proxy*>((wl_interface*)*(proxy)));
145 }
146 
147 }
148 }
149 
150 #endif
Wrapper class for wl_event_queue interface.
Definition: event_queue.h:55
Creates and manages the connection to a Wayland server.
void addProxy(wl_proxy *proxy)
Adds the proxy to the EventQueue.
Definition: event_queue.cpp:76
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Apr 14 2021 22:50:12 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.