Wayland++  0.2.6
C++ Bindings for Wayland
wayland-client.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014-2019, 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 
56  void set_log_handler(log_handler handler);
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();
69  };
70 
71  class display_t;
72 
73  namespace detail
74  {
75  struct proxy_data_t;
76  // base class for event listener storage.
77  struct events_base_t
78  {
79  virtual ~events_base_t() { }
80  };
81  }
82 
98  class proxy_t
99  {
100  public:
105  enum class wrapper_type
106  {
111  standard,
116  display,
126  foreign,
135  };
136 
137  private:
138  wl_proxy *proxy = nullptr;
139  detail::proxy_data_t *data = nullptr;
141  friend class detail::argument_t;
142  friend struct detail::proxy_data_t;
143 
144  // universal dispatcher
145  static int c_dispatcher(const void *implementation, void *target,
146  uint32_t opcode, const wl_message *message,
147  wl_argument *args);
148 
149  // marshal request
150  proxy_t marshal_single(uint32_t opcode, const wl_interface *interface,
151  std::vector<detail::argument_t> args, std::uint32_t version = 0);
152 
153  protected:
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  friend class registry_t;
161  // marshal a request, that doesn't lead a new proxy
162  // Valid types for args are:
163  // - uint32_t
164  // - int32_t
165  // - proxy_t
166  // - std::string
167  // - array_t
168  template <typename...T>
169  void marshal(uint32_t opcode, T...args)
170  {
171  std::vector<detail::argument_t> v = { detail::argument_t(args)... };
172  marshal_single(opcode, nullptr, v);
173  }
174 
175  // marshal a request that leads to a new proxy with inherited version
176  template <typename...T>
177  proxy_t marshal_constructor(uint32_t opcode, const wl_interface *interface,
178  T...args)
179  {
180  std::vector<detail::argument_t> v = { detail::argument_t(args)... };
181  return marshal_single(opcode, interface, v);
182  }
183 
184  // marshal a request that leads to a new proxy with specific version
185  template <typename...T>
186  proxy_t marshal_constructor_versioned(uint32_t opcode, const wl_interface *interface,
187  uint32_t version, T...args)
188  {
189  std::vector<detail::argument_t> v = { detail::argument_t(args)... };
190  return marshal_single(opcode, interface, v, version);
191  }
192 
193  // Set the opcode for destruction of the proxy
194  void set_destroy_opcode(uint32_t destroy_opcode);
195 
196  /*
197  Sets the dispatcher and its user data. User data must be an
198  instance of a class derived from events_base_t, allocated with
199  new. Will automatically be deleted upon destruction.
200  */
201  void set_events(std::shared_ptr<detail::events_base_t> events,
202  int(*dispatcher)(uint32_t, std::vector<detail::any>, std::shared_ptr<detail::events_base_t>));
203 
204  // Retrieve the previously set user data
205  std::shared_ptr<detail::events_base_t> get_events();
206 
207  // Constructs NULL proxies.
208  proxy_t();
209 
210  struct construct_proxy_wrapper_tag {};
211  // Construct from proxy as wrapper
212  proxy_t(const proxy_t &wrapped_proxy, construct_proxy_wrapper_tag /*unused*/);
213 
214  public:
220  proxy_t(wl_proxy *p, wrapper_type t = wrapper_type::standard, event_queue_t const& queue = event_queue_t());
221 
227  proxy_t(const proxy_t &p);
228 
234  proxy_t &operator=(const proxy_t &p);
235 
242  proxy_t(proxy_t &&p);
243 
249  proxy_t &operator=(proxy_t &&p);
250 
260  ~proxy_t();
261 
265  uint32_t get_id() const;
266 
270  std::string get_class() const;
271 
284  uint32_t get_version() const;
285 
289  {
290  return type;
291  }
292 
301  void set_queue(event_queue_t queue);
302 
307  wl_proxy *c_ptr() const;
308 
313  bool proxy_has_object() const;
314 
319  operator bool() const;
320 
323  bool operator==(const proxy_t &right) const;
324 
327  bool operator!=(const proxy_t &right) const;
328 
334  void proxy_release();
335  };
336 
358  {
359  public:
360  read_intent(read_intent &&other) = default;
361  ~read_intent();
362 
366  bool is_finalized() const;
367 
372  void cancel();
373 
387  void read();
388 
389  private:
390  read_intent(wl_display *display, wl_event_queue *event_queue = nullptr);
391  friend class display_t;
392  read_intent(read_intent const &other) = delete;
393  read_intent& operator=(read_intent const &other) = delete;
394 
395  wl_display *display;
396  wl_event_queue *event_queue = nullptr;
397  bool finalized = false;
398  };
399 
400  class callback_t;
401  class registry_t;
402 
463  class display_t : public proxy_t
464  {
465  private:
466  display_t(const display_t &d) { }
467  // Construct as proxy wrapper
468  display_t(proxy_t const &wrapped_proxy, construct_proxy_wrapper_tag /*unused*/);
469 
470  public:
478  display_t(int fd);
479 
480  display_t(display_t &&d);
481  display_t &operator=(display_t &&d);
482 
491  display_t(std::string name = "");
492 
515  explicit display_t(wl_display* display);
516 
524  ~display_t();
525 
531 
538  int get_fd();
539 
547  int roundtrip();
548 
561  int roundtrip_queue(event_queue_t queue);
562 
596 
607 
622  int dispatch_queue(event_queue_t queue);
623 
635 
656  int dispatch();
657 
694  int dispatch_pending();
695 
706  int get_error() const;
707 
722  std::tuple<int, bool> flush();
723 
736  callback_t sync();
737 
744 
745  operator wl_display*() const;
746 
750  };
751 }
752 
753 #include <wayland-client-protocol.hpp>
754 
755 #endif
wayland::display_t
Represents a connection to the compositor and acts as a proxy to the display singleton object.
Definition: wayland-client.hpp:463
wayland::display_t::obtain_read_intent
read_intent obtain_read_intent()
Announce calling thread's intention to read events from the Wayland display file descriptor.
wayland::proxy_t::get_wrapper_type
wrapper_type get_wrapper_type() const
Get the type of a proxy object.
Definition: wayland-client.hpp:288
wayland::display_t::flush
std::tuple< int, bool > flush()
Send all buffered requests on the display to the server.
wayland::display_t::dispatch
int dispatch()
Process incoming events.
wayland::proxy_t::wrapper_type::standard
wayland::display_t::get_registry
registry_t get_registry()
get global registry object
wayland::proxy_t::get_id
uint32_t get_id() const
Get the id of a proxy object.
wayland::detail::refcounted_wrapper
Refcounted wrapper for C objects.
Definition: wayland-util.hpp:147
wayland::display_t::get_error
int get_error() const
Retrieve the last error that occurred on a display.
wayland::proxy_t::proxy_release
void proxy_release()
Release the wrapped object (if any), making this an empty wrapper.
wayland::proxy_t::~proxy_t
~proxy_t()
Destructor.
wayland::proxy_t::proxy_has_object
bool proxy_has_object() const
Check whether this wrapper actually wraps an object.
wayland::read_intent
Represents an intention to read from the display file descriptor.
Definition: wayland-client.hpp:357
wayland::log_handler
std::function< void(std::string)> log_handler
Type for functions that handle log messages.
Definition: wayland-client.hpp:46
wayland::display_t::sync
callback_t sync()
asynchronous roundtrip
wayland::proxy_t::wrapper_type::foreign
wayland::display_t::roundtrip_queue
int roundtrip_queue(event_queue_t queue)
Block until all pending request are processed by the server.
wayland::display_t::obtain_queue_read_intent
read_intent obtain_queue_read_intent(event_queue_t queue)
Announce calling thread's intention to read events from the Wayland display file descriptor.
wayland::proxy_t::operator==
bool operator==(const proxy_t &right) const
Check whether two wrappers refer to the same object.
wayland::display_t::dispatch_queue
int dispatch_queue(event_queue_t queue)
Dispatch events in an event queue.
wayland::proxy_t::wrapper_type
wrapper_type
Definition: wayland-client.hpp:105
wayland::proxy_t::get_version
uint32_t get_version() const
Get the protocol object version of a proxy object.
wayland::read_intent::cancel
void cancel()
Cancel read intent.
wayland::display_t::~display_t
~display_t()
Close a connection to a Wayland display.
wayland::proxy_t
Represents a protocol object on the client side.
Definition: wayland-client.hpp:98
wayland::display_t::dispatch_pending
int dispatch_pending()
Dispatch main queue events without reading from the display fd.
wayland::display_t::proxy_create_wrapper
display_t proxy_create_wrapper()
create proxy wrapper for this display
wayland::proxy_t::get_class
std::string get_class() const
Get the interface name (class) of a proxy object.
wayland::event_queue_t
A queue for proxy_t object events.
Definition: wayland-client.hpp:63
wayland::proxy_t::c_ptr
wl_proxy * c_ptr() const
Get a pointer to the underlying C struct.
wayland::registry_t
global registry object
Definition: wayland-client-protocol.hpp:130
wayland::read_intent::read
void read()
Read events from display file descriptor.
wayland::set_log_handler
void set_log_handler(log_handler handler)
Set C library log handler.
wayland::display_t::create_queue
event_queue_t create_queue()
Create a new event queue for this display.
wayland::proxy_t::operator=
proxy_t & operator=(const proxy_t &p)
Assignment operator.
wayland::proxy_t::operator!=
bool operator!=(const proxy_t &right) const
Check whether two wrappers refer to different objects.
wayland::proxy_t::wrapper_type::proxy_wrapper
wayland::display_t::roundtrip
int roundtrip()
Block until all pending request are processed by the server.
wayland::display_t::dispatch_queue_pending
int dispatch_queue_pending(event_queue_t queue)
Dispatch pending events in an event queue.
wayland::proxy_t::set_queue
void set_queue(event_queue_t queue)
Assign a proxy to an event queue.
wayland::callback_t
callback object
Definition: wayland-client-protocol.hpp:210
wayland::proxy_t::wrapper_type::display
wayland::display_t::get_fd
int get_fd()
Get a display context's file descriptor.
wayland::read_intent::is_finalized
bool is_finalized() const
Check whether this intent was already finalized with cancel or read.