Wayland++ 1.0.1
C++ Bindings for Wayland
Loading...
Searching...
No Matches
wayland-client.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014-2022, Nils Christopher Brause, Philipp Kerling
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef WAYLAND_CLIENT_HPP
27#define WAYLAND_CLIENT_HPP
28
30
31#include <atomic>
32#include <cstdint>
33#include <functional>
34#include <memory>
35#include <string>
36#include <vector>
37#include <cstdint>
38#include <wayland-version.hpp>
39#include <wayland-client-core.h>
40#include <wayland-util.hpp>
41
42namespace wayland
43{
48 using log_handler = std::function<void(std::string)> ;
49
59
65 class event_queue_t : public detail::refcounted_wrapper<wl_event_queue>
66 {
67 event_queue_t(wl_event_queue *q);
68 friend class display_t;
69 public:
70 event_queue_t() = default;
71 event_queue_t(const event_queue_t&) = default;
72 event_queue_t(event_queue_t&&) noexcept = default;
73 event_queue_t& operator=(const event_queue_t&) = default;
74 event_queue_t& operator=(event_queue_t&&) noexcept = default;
75 ~event_queue_t() noexcept = default;
76 };
77
78 class display_t;
79
80 namespace detail
81 {
82 struct proxy_data_t;
83 // base class for event listener storage.
84 struct events_base_t
85 {
86 events_base_t() = default;
87 events_base_t(const events_base_t&) = default;
88 events_base_t(events_base_t&&) noexcept = default;
89 events_base_t& operator=(const events_base_t&) = default;
90 events_base_t& operator=(events_base_t&&) noexcept = default;
91 virtual ~events_base_t() noexcept = default;
92 };
93 }
94
110 class proxy_t
111 {
112 public:
117 enum class wrapper_type
118 {
123 standard,
128 display,
138 foreign,
146 proxy_wrapper
147 };
148
149 private:
150 wl_proxy *proxy = nullptr;
151 detail::proxy_data_t *data = nullptr;
152 wrapper_type type = wrapper_type::standard;
153 friend class detail::argument_t;
154 friend struct detail::proxy_data_t;
155
156 // Interface desctiption filled in by the each interface class
157 const wl_interface *interface = nullptr;
158
159 // copy constructor filled in by the each interface class
160 std::function<proxy_t(proxy_t)> copy_constructor;
161
162 // universal dispatcher
163 static int c_dispatcher(const void *implementation, void *target,
164 uint32_t opcode, const wl_message *message,
165 wl_argument *args);
166
167 // marshal request
168 proxy_t marshal_single(uint32_t opcode, const wl_interface *interface,
169 const std::vector<detail::argument_t>& args, std::uint32_t version = 0);
170
171 protected:
172 void set_interface(const wl_interface *iface);
173 void set_copy_constructor(const std::function<proxy_t(proxy_t)>& func);
174
175 friend class registry_t;
176 // marshal a request, that doesn't lead a new proxy
177 // Valid types for args are:
178 // - uint32_t
179 // - int32_t
180 // - proxy_t
181 // - std::string
182 // - array_t
183 template <typename...T>
184 void marshal(uint32_t opcode, const T& ...args)
185 {
186 std::vector<detail::argument_t> v = { detail::argument_t(args)... };
187 marshal_single(opcode, nullptr, v);
188 }
189
190 // marshal a request that leads to a new proxy with inherited version
191 template <typename...T>
192 proxy_t marshal_constructor(uint32_t opcode, const wl_interface *interface,
193 const T& ...args)
194 {
195 std::vector<detail::argument_t> v = { detail::argument_t(args)... };
196 return marshal_single(opcode, interface, v);
197 }
198
199 // marshal a request that leads to a new proxy with specific version
200 template <typename...T>
201 proxy_t marshal_constructor_versioned(uint32_t opcode, const wl_interface *interface,
202 uint32_t version, const T& ...args)
203 {
204 std::vector<detail::argument_t> v = { detail::argument_t(args)... };
205 return marshal_single(opcode, interface, v, version);
206 }
207
208 // Set the opcode for destruction of the proxy
209 void set_destroy_opcode(uint32_t destroy_opcode);
210
211 /*
212 Sets the dispatcher and its user data. User data must be an
213 instance of a class derived from events_base_t, allocated with
214 new. Will automatically be deleted upon destruction.
215 */
216 void set_events(std::shared_ptr<detail::events_base_t> events,
217 int(*dispatcher)(uint32_t, const std::vector<detail::any>&, const std::shared_ptr<detail::events_base_t>&));
218
219 // Retrieve the previously set user data
220 std::shared_ptr<detail::events_base_t> get_events();
221
222 // Constructs NULL proxies.
223 proxy_t() = default;
224
225 struct construct_proxy_wrapper_tag {};
226 // Construct from proxy as wrapper
227 proxy_t(const proxy_t &wrapped_proxy, construct_proxy_wrapper_tag /*unused*/);
228
229 public:
236
242 proxy_t(const proxy_t &p);
243
249 proxy_t &operator=(const proxy_t &p);
250
257 proxy_t(proxy_t &&p) noexcept;
258
264 proxy_t &operator=(proxy_t &&p) noexcept;
265
276
280 uint32_t get_id() const;
281
285 std::string get_class() const;
286
299 uint32_t get_version() const;
300
304 {
305 return type;
306 }
307
317
322 wl_proxy *c_ptr() const;
323
328 bool proxy_has_object() const;
329
334 operator bool() const;
335
338 bool operator==(const proxy_t &right) const;
339
342 bool operator!=(const proxy_t &right) const;
343
350 };
351
372 class read_intent
373 {
374 public:
375 read_intent(read_intent &&other) noexcept = default;
376 read_intent(read_intent const &other) = delete;
377 read_intent& operator=(read_intent const &other) = delete;
378 read_intent& operator=(read_intent &&other) noexcept = delete;
379 ~read_intent();
380
384 bool is_finalized() const;
385
390 void cancel();
391
405 void read();
406
407 private:
408 read_intent(wl_display *display, wl_event_queue *event_queue = nullptr);
409 friend class display_t;
410
411 wl_display *display;
412 wl_event_queue *event_queue = nullptr;
413 bool finalized = false;
414 };
415
416 class callback_t;
417 class registry_t;
418
479 class display_t : public proxy_t
480 {
481 private:
482 // Construct as proxy wrapper
483 display_t(proxy_t const &wrapped_proxy, construct_proxy_wrapper_tag /*unused*/);
484
485 public:
493 display_t(int fd);
494
495 display_t(display_t &&d) noexcept;
496 display_t(const display_t &d) = delete;
497 display_t &operator=(const display_t &d) = delete;
498 display_t &operator=(display_t &&d) noexcept;
499
508 display_t(const std::string& name = {});
509
532 explicit display_t(wl_display* display);
533
541 ~display_t() noexcept = default;
542
548
555 int get_fd() const;
556
564 int roundtrip() const;
565
578 int roundtrip_queue(const event_queue_t& queue) const;
579
613
624
639 int dispatch_queue(const event_queue_t& queue) const;
640
651 int dispatch_queue_pending(const event_queue_t& queue) const;
652
673 int dispatch() const;
674
711 int dispatch_pending() const;
712
723 int get_error() const;
724
739 std::tuple<int, bool> flush() const;
740
754
760 registry_t get_registry();
761
762 operator wl_display*() const;
763
767 };
768}
769
770#include <wayland-client-protocol.hpp>
771
772#endif
Refcounted wrapper for C objects.
Represents a connection to the compositor and acts as a proxy to the display singleton object.
~display_t() noexcept=default
Close a connection to a Wayland display.
display_t proxy_create_wrapper()
create proxy wrapper for this display
display_t(const std::string &name={})
Connect to a Wayland display.
int dispatch_pending() const
Dispatch main queue events without reading from the display fd.
int dispatch_queue(const event_queue_t &queue) const
Dispatch events in an event queue.
int get_error() const
Retrieve the last error that occurred on a display.
event_queue_t create_queue() const
Create a new event queue for this display.
int dispatch() const
Process incoming events.
int dispatch_queue_pending(const event_queue_t &queue) const
Dispatch pending events in an event queue.
int roundtrip_queue(const event_queue_t &queue) const
Block until all pending request are processed by the server.
registry_t get_registry()
get global registry object
int get_fd() const
Get a display context's file descriptor.
callback_t sync()
asynchronous roundtrip
int roundtrip() const
Block until all pending request are processed by the server.
display_t(int fd)
Connect to Wayland display on an already open fd.
display_t(wl_display *display)
Use an existing connection to a Wayland display to construct a waylandpp display_t.
read_intent obtain_queue_read_intent(const event_queue_t &queue) const
Announce calling thread's intention to read events from the Wayland display file descriptor.
std::tuple< int, bool > flush() const
Send all buffered requests on the display to the server.
read_intent obtain_read_intent() const
Announce calling thread's intention to read events from the Wayland display file descriptor.
A queue for proxy_t object events.
Represents a protocol object on the client side.
void proxy_release()
Release the wrapped object (if any), making this an empty wrapper.
bool proxy_has_object() const
Check whether this wrapper actually wraps an object.
void set_queue(event_queue_t queue)
Assign a proxy to an event queue.
uint32_t get_id() const
Get the id of a proxy object.
bool operator==(const proxy_t &right) const
Check whether two wrappers refer to the same object.
~proxy_t()
Destructor.
proxy_t(const proxy_t &p)
Copy Constructior.
uint32_t get_version() const
Get the protocol object version of a proxy object.
wrapper_type get_wrapper_type() const
Get the type of a proxy object.
wl_proxy * c_ptr() const
Get a pointer to the underlying C struct.
std::string get_class() const
Get the interface name (class) of a proxy object.
proxy_t(proxy_t &&p) noexcept
Move Constructior.
proxy_t & operator=(const proxy_t &p)
Assignment operator.
bool operator!=(const proxy_t &right) const
Check whether two wrappers refer to different objects.
proxy_t(wl_proxy *p, wrapper_type t=wrapper_type::standard, event_queue_t const &queue=event_queue_t())
Cronstruct a proxy_t from a wl_proxy pointer.
proxy_t & operator=(proxy_t &&p) noexcept
Move Asignment operator.
Represents an intention to read from the display file descriptor.
void cancel()
Cancel read intent.
bool is_finalized() const
Check whether this intent was already finalized with cancel or read.
void read()
Read events from display file descriptor.
std::function< void(std::string)> log_handler
Type for functions that handle log messages.
void set_log_handler(log_handler handler)
Set C library log handler.