Loading...
Searching...
No Matches
wayland-client.hpp
Go to the documentation of this file.
/*
* Copyright (c) 2014-2022, Nils Christopher Brause, Philipp Kerling
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef WAYLAND_CLIENT_HPP
#define WAYLAND_CLIENT_HPP
#include <atomic>
#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <vector>
#include <cstdint>
#include <wayland-version.hpp>
#include <wayland-client-core.h>
#include <wayland-util.hpp>
namespace wayland
{
using log_handler = std::function<void(std::string)> ;
void set_log_handler(log_handler handler);
class event_queue_t : public detail::refcounted_wrapper<wl_event_queue>
{
event_queue_t(wl_event_queue *q);
friend class display_t;
public:
event_queue_t() = default;
event_queue_t(const event_queue_t&) = default;
event_queue_t(event_queue_t&&) noexcept = default;
event_queue_t& operator=(const event_queue_t&) = default;
event_queue_t& operator=(event_queue_t&&) noexcept = default;
~event_queue_t() noexcept = default;
};
class display_t;
namespace detail
{
struct proxy_data_t;
// base class for event listener storage.
struct events_base_t
{
events_base_t() = default;
events_base_t(const events_base_t&) = default;
events_base_t(events_base_t&&) noexcept = default;
events_base_t& operator=(const events_base_t&) = default;
events_base_t& operator=(events_base_t&&) noexcept = default;
virtual ~events_base_t() noexcept = default;
};
}
class proxy_t
{
public:
enum class wrapper_type
{
standard,
display,
foreign,
proxy_wrapper
};
private:
wl_proxy *proxy = nullptr;
detail::proxy_data_t *data = nullptr;
wrapper_type type = wrapper_type::standard;
friend class detail::argument_t;
friend struct detail::proxy_data_t;
// Interface desctiption filled in by the each interface class
const wl_interface *interface = nullptr;
// copy constructor filled in by the each interface class
std::function<proxy_t(proxy_t)> copy_constructor;
// universal dispatcher
static int c_dispatcher(const void *implementation, void *target,
uint32_t opcode, const wl_message *message,
wl_argument *args);
// marshal request
proxy_t marshal_single(uint32_t opcode, const wl_interface *interface,
const std::vector<detail::argument_t>& args, std::uint32_t version = 0);
protected:
void set_interface(const wl_interface *iface);
void set_copy_constructor(const std::function<proxy_t(proxy_t)>& func);
friend class registry_t;
// marshal a request, that doesn't lead a new proxy
// Valid types for args are:
// - uint32_t
// - int32_t
// - proxy_t
// - std::string
// - array_t
template <typename...T>
void marshal(uint32_t opcode, const T& ...args)
{
std::vector<detail::argument_t> v = { detail::argument_t(args)... };
marshal_single(opcode, nullptr, v);
}
// marshal a request that leads to a new proxy with inherited version
template <typename...T>
proxy_t marshal_constructor(uint32_t opcode, const wl_interface *interface,
const T& ...args)
{
std::vector<detail::argument_t> v = { detail::argument_t(args)... };
return marshal_single(opcode, interface, v);
}
// marshal a request that leads to a new proxy with specific version
template <typename...T>
proxy_t marshal_constructor_versioned(uint32_t opcode, const wl_interface *interface,
uint32_t version, const T& ...args)
{
std::vector<detail::argument_t> v = { detail::argument_t(args)... };
return marshal_single(opcode, interface, v, version);
}
// Set the opcode for destruction of the proxy
void set_destroy_opcode(uint32_t destroy_opcode);
/*
Sets the dispatcher and its user data. User data must be an
instance of a class derived from events_base_t, allocated with
new. Will automatically be deleted upon destruction.
*/
void set_events(std::shared_ptr<detail::events_base_t> events,
int(*dispatcher)(uint32_t, const std::vector<detail::any>&, const std::shared_ptr<detail::events_base_t>&));
// Retrieve the previously set user data
std::shared_ptr<detail::events_base_t> get_events();
// Constructs NULL proxies.
proxy_t() = default;
struct construct_proxy_wrapper_tag {};
// Construct from proxy as wrapper
proxy_t(const proxy_t &wrapped_proxy, construct_proxy_wrapper_tag /*unused*/);
public:
proxy_t(wl_proxy *p, wrapper_type t = wrapper_type::standard, event_queue_t const& queue = event_queue_t());
proxy_t(const proxy_t &p);
proxy_t &operator=(const proxy_t &p);
proxy_t(proxy_t &&p) noexcept;
proxy_t &operator=(proxy_t &&p) noexcept;
~proxy_t();
uint32_t get_id() const;
std::string get_class() const;
uint32_t get_version() const;
wrapper_type get_wrapper_type() const
{
return type;
}
void set_queue(event_queue_t queue);
wl_proxy *c_ptr() const;
bool proxy_has_object() const;
operator bool() const;
bool operator==(const proxy_t &right) const;
bool operator!=(const proxy_t &right) const;
void proxy_release();
};
class read_intent
{
public:
read_intent(read_intent &&other) noexcept = default;
read_intent(read_intent const &other) = delete;
read_intent& operator=(read_intent const &other) = delete;
read_intent& operator=(read_intent &&other) noexcept = delete;
~read_intent();
bool is_finalized() const;
void cancel();
void read();
private:
read_intent(wl_display *display, wl_event_queue *event_queue = nullptr);
friend class display_t;
wl_display *display;
wl_event_queue *event_queue = nullptr;
bool finalized = false;
};
class callback_t;
class registry_t;
class display_t : public proxy_t
{
private:
// Construct as proxy wrapper
display_t(proxy_t const &wrapped_proxy, construct_proxy_wrapper_tag /*unused*/);
public:
display_t(int fd);
display_t(display_t &&d) noexcept;
display_t(const display_t &d) = delete;
display_t &operator=(const display_t &d) = delete;
display_t &operator=(display_t &&d) noexcept;
display_t(const std::string& name = {});
explicit display_t(wl_display* display);
~display_t() noexcept = default;
event_queue_t create_queue() const;
int get_fd() const;
int roundtrip() const;
int roundtrip_queue(const event_queue_t& queue) const;
read_intent obtain_read_intent() const;
read_intent obtain_queue_read_intent(const event_queue_t& queue) const;
int dispatch_queue(const event_queue_t& queue) const;
int dispatch_queue_pending(const event_queue_t& queue) const;
int dispatch() const;
int dispatch_pending() const;
int get_error() const;
std::tuple<int, bool> flush() const;
callback_t sync();
registry_t get_registry();
operator wl_display*() const;
display_t proxy_create_wrapper();
};
}
#include <wayland-client-protocol.hpp>
#endif
wayland::callback_t
callback object
Definition wayland-client-protocol.hpp:213
wayland::detail::refcounted_wrapper
Refcounted wrapper for C objects.
Definition wayland-util.hpp:154
wayland::display_t
Represents a connection to the compositor and acts as a proxy to the display singleton object.
Definition wayland-client.hpp:480
wayland::display_t::~display_t
~display_t() noexcept=default
Close a connection to a Wayland display.
wayland::display_t::proxy_create_wrapper
display_t proxy_create_wrapper()
create proxy wrapper for this display
wayland::display_t::display_t
display_t(const std::string &name={})
Connect to a Wayland display.
wayland::display_t::dispatch_pending
int dispatch_pending() const
Dispatch main queue events without reading from the display fd.
wayland::display_t::dispatch_queue
int dispatch_queue(const event_queue_t &queue) const
Dispatch events in an event queue.
wayland::display_t::get_error
int get_error() const
Retrieve the last error that occurred on a display.
wayland::display_t::create_queue
event_queue_t create_queue() const
Create a new event queue for this display.
wayland::display_t::dispatch
int dispatch() const
Process incoming events.
wayland::display_t::dispatch_queue_pending
int dispatch_queue_pending(const event_queue_t &queue) const
Dispatch pending events in an event queue.
wayland::display_t::roundtrip_queue
int roundtrip_queue(const event_queue_t &queue) const
Block until all pending request are processed by the server.
wayland::display_t::get_registry
registry_t get_registry()
get global registry object
wayland::display_t::get_fd
int get_fd() const
Get a display context's file descriptor.
wayland::display_t::sync
callback_t sync()
asynchronous roundtrip
wayland::display_t::roundtrip
int roundtrip() const
Block until all pending request are processed by the server.
wayland::display_t::display_t
display_t(int fd)
Connect to Wayland display on an already open fd.
wayland::display_t::display_t
display_t(wl_display *display)
Use an existing connection to a Wayland display to construct a waylandpp display_t.
wayland::display_t::obtain_queue_read_intent
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.
wayland::display_t::flush
std::tuple< int, bool > flush() const
Send all buffered requests on the display to the server.
wayland::display_t::obtain_read_intent
read_intent obtain_read_intent() const
Announce calling thread's intention to read events from the Wayland display file descriptor.
wayland::event_queue_t
A queue for proxy_t object events.
Definition wayland-client.hpp:66
wayland::proxy_t
Represents a protocol object on the client side.
Definition wayland-client.hpp:111
wayland::proxy_t::proxy_release
void proxy_release()
Release the wrapped object (if any), making this an empty wrapper.
wayland::proxy_t::proxy_has_object
bool proxy_has_object() const
Check whether this wrapper actually wraps an object.
wayland::proxy_t::set_queue
void set_queue(event_queue_t queue)
Assign a proxy to an event queue.
wayland::proxy_t::get_id
uint32_t get_id() const
Get the id of a proxy object.
wayland::proxy_t::operator==
bool operator==(const proxy_t &right) const
Check whether two wrappers refer to the same object.
wayland::proxy_t::~proxy_t
~proxy_t()
Destructor.
wayland::proxy_t::proxy_t
proxy_t(const proxy_t &p)
Copy Constructior.
wayland::proxy_t::get_version
uint32_t get_version() const
Get the protocol object version of a proxy object.
wayland::proxy_t::get_wrapper_type
wrapper_type get_wrapper_type() const
Get the type of a proxy object.
Definition wayland-client.hpp:303
wayland::proxy_t::c_ptr
wl_proxy * c_ptr() const
Get a pointer to the underlying C struct.
wayland::proxy_t::get_class
std::string get_class() const
Get the interface name (class) of a proxy object.
wayland::proxy_t::proxy_t
proxy_t(proxy_t &&p) noexcept
Move Constructior.
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::proxy_t
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.
wayland::proxy_t::wrapper_type
wrapper_type
Definition wayland-client.hpp:118
wayland::proxy_t::wrapper_type::standard
@ standard
Definition wayland-client.hpp:123
wayland::proxy_t::operator=
proxy_t & operator=(proxy_t &&p) noexcept
Move Asignment operator.
wayland::read_intent
Represents an intention to read from the display file descriptor.
Definition wayland-client.hpp:373
wayland::read_intent::cancel
void cancel()
Cancel read intent.
wayland::read_intent::is_finalized
bool is_finalized() const
Check whether this intent was already finalized with cancel or read.
wayland::read_intent::read
void read()
Read events from display file descriptor.
wayland::registry_t
global registry object
Definition wayland-client-protocol.hpp:133
wayland::log_handler
std::function< void(std::string)> log_handler
Type for functions that handle log messages.
Definition wayland-client.hpp:48
wayland::set_log_handler
void set_log_handler(log_handler handler)
Set C library log handler.