Wayland++  0.2.9
C++ Bindings for Wayland
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 
31 #include <atomic>
32 #include <functional>
33 #include <memory>
34 #include <string>
35 #include <vector>
36 #include <wayland-version.hpp>
37 #include <wayland-client-core.h>
38 #include <wayland-util.hpp>
39 
40 namespace wayland
41 {
46  using log_handler = std::function<void(std::string)> ;
47 
57 
63  class event_queue_t : public detail::refcounted_wrapper<wl_event_queue>
64  {
65  event_queue_t(wl_event_queue *q);
66  friend class display_t;
67  public:
68  event_queue_t() = default;
69  event_queue_t(const event_queue_t&) = default;
70  event_queue_t(event_queue_t&&) noexcept = default;
71  event_queue_t& operator=(const event_queue_t&) = default;
72  event_queue_t& operator=(event_queue_t&&) noexcept = default;
73  ~event_queue_t() noexcept = default;
74  };
75 
76  class display_t;
77 
78  namespace detail
79  {
80  struct proxy_data_t;
81  // base class for event listener storage.
82  struct events_base_t
83  {
84  events_base_t() = default;
85  events_base_t(const events_base_t&) = default;
86  events_base_t(events_base_t&&) noexcept = default;
87  events_base_t& operator=(const events_base_t&) = default;
88  events_base_t& operator=(events_base_t&&) noexcept = default;
89  virtual ~events_base_t() noexcept = default;
90  };
91  }
92 
108  class proxy_t
109  {
110  public:
115  enum class wrapper_type
116  {
121  standard,
126  display,
136  foreign,
144  proxy_wrapper
145  };
146 
147  private:
148  wl_proxy *proxy = nullptr;
149  detail::proxy_data_t *data = nullptr;
150  wrapper_type type = wrapper_type::standard;
151  friend class detail::argument_t;
152  friend struct detail::proxy_data_t;
153 
154  // Interface desctiption filled in by the each interface class
155  const wl_interface *interface = nullptr;
156 
157  // copy constructor filled in by the each interface class
158  std::function<proxy_t(proxy_t)> copy_constructor;
159 
160  // universal dispatcher
161  static int c_dispatcher(const void *implementation, void *target,
162  uint32_t opcode, const wl_message *message,
163  wl_argument *args);
164 
165  // marshal request
166  proxy_t marshal_single(uint32_t opcode, const wl_interface *interface,
167  const std::vector<detail::argument_t>& args, std::uint32_t version = 0);
168 
169  protected:
170  void set_interface(const wl_interface *iface);
171  void set_copy_constructor(const std::function<proxy_t(proxy_t)>& func);
172 
173  friend class registry_t;
174  // marshal a request, that doesn't lead a new proxy
175  // Valid types for args are:
176  // - uint32_t
177  // - int32_t
178  // - proxy_t
179  // - std::string
180  // - array_t
181  template <typename...T>
182  void marshal(uint32_t opcode, const T& ...args)
183  {
184  std::vector<detail::argument_t> v = { detail::argument_t(args)... };
185  marshal_single(opcode, nullptr, v);
186  }
187 
188  // marshal a request that leads to a new proxy with inherited version
189  template <typename...T>
190  proxy_t marshal_constructor(uint32_t opcode, const wl_interface *interface,
191  const T& ...args)
192  {
193  std::vector<detail::argument_t> v = { detail::argument_t(args)... };
194  return marshal_single(opcode, interface, v);
195  }
196 
197  // marshal a request that leads to a new proxy with specific version
198  template <typename...T>
199  proxy_t marshal_constructor_versioned(uint32_t opcode, const wl_interface *interface,
200  uint32_t version, const T& ...args)
201  {
202  std::vector<detail::argument_t> v = { detail::argument_t(args)... };
203  return marshal_single(opcode, interface, v, version);
204  }
205 
206  // Set the opcode for destruction of the proxy
207  void set_destroy_opcode(uint32_t destroy_opcode);
208 
209  /*
210  Sets the dispatcher and its user data. User data must be an
211  instance of a class derived from events_base_t, allocated with
212  new. Will automatically be deleted upon destruction.
213  */
214  void set_events(std::shared_ptr<detail::events_base_t> events,
215  int(*dispatcher)(uint32_t, const std::vector<detail::any>&, const std::shared_ptr<detail::events_base_t>&));
216 
217  // Retrieve the previously set user data
218  std::shared_ptr<detail::events_base_t> get_events();
219 
220  // Constructs NULL proxies.
221  proxy_t() = default;
222 
223  struct construct_proxy_wrapper_tag {};
224  // Construct from proxy as wrapper
225  proxy_t(const proxy_t &wrapped_proxy, construct_proxy_wrapper_tag /*unused*/);
226 
227  public:
233  proxy_t(wl_proxy *p, wrapper_type t = wrapper_type::standard, event_queue_t const& queue = event_queue_t());
234 
240  proxy_t(const proxy_t &p);
241 
248 
255  proxy_t(proxy_t &&p) noexcept;
256 
262  proxy_t &operator=(proxy_t &&p) noexcept;
263 
274 
278  uint32_t get_id() const;
279 
283  std::string get_class() const;
284 
297  uint32_t get_version() const;
298 
302  {
303  return type;
304  }
305 
315 
320  wl_proxy *c_ptr() const;
321 
326  bool proxy_has_object() const;
327 
332  operator bool() const;
333 
336  bool operator==(const proxy_t &right) const;
337 
340  bool operator!=(const proxy_t &right) const;
341 
348  };
349 
371  {
372  public:
373  read_intent(read_intent &&other) noexcept = default;
374  read_intent(read_intent const &other) = delete;
375  read_intent& operator=(read_intent const &other) = delete;
376  read_intent& operator=(read_intent &&other) noexcept = delete;
377  ~read_intent();
378 
382  bool is_finalized() const;
383 
388  void cancel();
389 
403  void read();
404 
405  private:
406  read_intent(wl_display *display, wl_event_queue *event_queue = nullptr);
407  friend class display_t;
408 
409  wl_display *display;
410  wl_event_queue *event_queue = nullptr;
411  bool finalized = false;
412  };
413 
414  class callback_t;
415  class registry_t;
416 
477  class display_t : public proxy_t
478  {
479  private:
480  // Construct as proxy wrapper
481  display_t(proxy_t const &wrapped_proxy, construct_proxy_wrapper_tag /*unused*/);
482 
483  public:
491  display_t(int fd);
492 
493  display_t(display_t &&d) noexcept;
494  display_t(const display_t &d) = delete;
495  display_t &operator=(const display_t &d) = delete;
496  display_t &operator=(display_t &&d) noexcept;
497 
506  display_t(const std::string& name = {});
507 
530  explicit display_t(wl_display* display);
531 
539  ~display_t() noexcept = default;
540 
545  event_queue_t create_queue() const;
546 
553  int get_fd() const;
554 
562  int roundtrip() const;
563 
576  int roundtrip_queue(const event_queue_t& queue) const;
577 
610  read_intent obtain_read_intent() const;
611 
621  read_intent obtain_queue_read_intent(const event_queue_t& queue) const;
622 
637  int dispatch_queue(const event_queue_t& queue) const;
638 
649  int dispatch_queue_pending(const event_queue_t& queue) const;
650 
671  int dispatch() const;
672 
709  int dispatch_pending() const;
710 
721  int get_error() const;
722 
737  std::tuple<int, bool> flush() const;
738 
751  callback_t sync();
752 
758  registry_t get_registry();
759 
760  operator wl_display*() const;
761 
764  display_t proxy_create_wrapper();
765  };
766 }
767 
768 #include <wayland-client-protocol.hpp>
769 
770 #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(const std::string &name={})
Connect to a Wayland display.
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.
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.
proxy_t & operator=(proxy_t &&p) noexcept
Move Asignment operator.
std::string get_class() const
Get the interface name (class) of a proxy object.
proxy_t(proxy_t &&p) noexcept
Move Constructior.
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=(const proxy_t &p)
Assignment 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.
global registry object
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.