26#ifndef WAYLAND_SERVER_HPP
27#define WAYLAND_SERVER_HPP
35#include <wayland-server-core.h>
36#include <wayland-util.hpp>
48 wl_listener listener = { {
nullptr,
nullptr },
nullptr };
71 template <
class resource>
class global_t;
80 std::function<void()> destroy;
81 std::function<void(client_t&)> client_created;
82 detail::listener_t destroy_listener;
83 detail::listener_t client_created_listener;
85 wayland::detail::any user_data;
86 std::atomic<unsigned int> counter{1};
89 wl_display *display =
nullptr;
90 data_t *data =
nullptr;
92 static void destroy_func(wl_listener *listener,
void *data);
93 static void client_created_func(wl_listener *listener,
void *cl);
94 static data_t *wl_display_get_user_data(wl_display *display);
95 static bool c_filter_func(
const wl_client *client,
const wl_global *global,
void *data);
98 display_t(wl_display *c);
102 friend class client_t;
121 display_t(
const display_t& d);
122 display_t(display_t&& d)
noexcept;
123 display_t &operator=(
const display_t& d);
124 display_t &operator=(display_t&& d)
noexcept;
125 bool operator==(
const display_t& d)
const;
126 wl_display *c_ptr()
const;
127 wayland::detail::any &user_data();
134 event_loop_t get_event_loop()
const;
157 int add_socket(
const std::string& name)
const;
164 std::string add_socket_auto()
const;
175 int add_socket_fd(
int sock_fd)
const;
182 void terminate()
const;
197 void flush_clients()
const;
204 uint32_t get_serial()
const;
211 uint32_t next_serial()
const;
212 std::function<void()> &on_destroy();
218 std::function<void(client_t&)> &on_client_created();
226 client_t client_create(
int fd);
235 std::list<client_t> get_client_list()
const;
251 void set_global_filter(
const std::function<
bool(client_t, global_base_t)>& filter);
261 wl_client *client =
nullptr;
262 std::function<void()> destroy;
263 detail::listener_t destroy_listener;
264 wayland::detail::any user_data;
265 std::atomic<unsigned int> counter{1};
266 bool destroyed =
false;
269 wl_client *client =
nullptr;
270 data_t *data =
nullptr;
272 static void destroy_func(wl_listener *listener,
void *data);
273 static wl_iterator_result resource_iterator(wl_resource *resource,
void *data);
274 static data_t *wl_client_get_user_data(wl_client *client);
277 client_t(wl_client *c);
281 friend class display_t;
282 friend class resource_t;
283 template <
class resource>
friend class global_t;
309 client_t(display_t &display,
int fd);
312 client_t(
const client_t &c);
313 client_t(client_t &&c)
noexcept;
314 client_t &operator=(
const client_t& c);
315 client_t &operator=(client_t&& c)
noexcept;
316 bool operator==(
const client_t &c)
const;
317 wl_client *c_ptr()
const;
318 wayland::detail::any &user_data();
344 void get_credentials(pid_t &pid, uid_t &uid, gid_t &gid)
const;
373 std::function<void()> &on_destroy();
383 resource_t get_object(uint32_t
id);
391 void post_no_memory()
const;
400 void post_implementation_error(std::string
const& msg)
const;
410 template <
typename...types>
411 void post_implementation_error(std::string
const& msg, types&&... args)
413 wl_client_post_implementation_error(c_ptr(), msg.c_str(), std::forward<types...>(args...));
420 display_t get_display()
const;
426 std::list<resource_t> get_resource_list()
const;
435 events_base_t() =
default;
436 events_base_t(
const events_base_t& e) =
default;
437 events_base_t(events_base_t&& e) =
default;
438 events_base_t& operator=(
const events_base_t& e) =
default;
439 events_base_t& operator=(events_base_t&& e) =
default;
440 virtual ~events_base_t() =
default;
446 std::shared_ptr<events_base_t> events;
447 std::function<void()> destroy;
448 detail::listener_t destroy_listener;
449 wayland::detail::any user_data;
450 std::atomic<unsigned int> counter{1};
451 bool destroyed =
false;
454 wl_resource *resource =
nullptr;
455 data_t *data =
nullptr;
457 static void destroy_func(wl_listener *listener,
void *data);
458 static int c_dispatcher(
const void *implementation,
void *target,
459 uint32_t opcode,
const wl_message *message,
461 static int dummy_dispatcher(
int opcode,
const std::vector<wayland::detail::any>& args,
const std::shared_ptr<resource_t::events_base_t>& events);
465 static constexpr const wl_interface *
interface = nullptr;
472 void set_events(
const std::shared_ptr<events_base_t>& events,
473 int(*dispatcher)(
int,
const std::vector<wayland::detail::any>&,
const std::shared_ptr<resource_t::events_base_t>&));
476 std::shared_ptr<events_base_t> get_events()
const;
478 void post_event_array(uint32_t opcode,
const std::vector<wayland::detail::argument_t>& v)
const;
479 void queue_event_array(uint32_t opcode,
const std::vector<wayland::detail::argument_t>& v)
const;
481 template <
typename...T>
482 void post_event(uint32_t opcode, T...args)
const
484 std::vector<wayland::detail::argument_t> v = { wayland::detail::argument_t(args)... };
486 post_event_array(opcode, v);
489 template <
typename...T>
490 void queue_event(uint32_t opcode, T...args)
const
492 std::vector<wayland::detail::argument_t> v = { wayland::detail::argument_t(args)... };
494 queue_event_array(opcode, v);
497 template <
typename...T>
498 void send_event(
bool post, uint32_t opcode, T...args)
const
501 post_event(opcode, args...);
503 queue_event(opcode, args...);
506 void post_error(uint32_t code,
const std::string& msg)
const;
508 resource_t(wl_resource *c);
512 friend class client_t;
515 resource_t() =
default;
527 resource_t(
const client_t& client,
const wl_interface *interface,
int version, uint32_t
id);
529 resource_t(
const resource_t &r);
530 resource_t(resource_t &&r)
noexcept;
531 resource_t &operator=(
const resource_t& r);
532 resource_t &operator=(resource_t&& r)
noexcept;
533 bool operator==(
const resource_t& r)
const;
534 operator bool()
const;
535 wl_resource *c_ptr()
const;
536 wayland::detail::any &user_data();
542 bool proxy_has_object()
const;
550 void post_no_memory()
const;
556 uint32_t get_id()
const;
562 client_t get_client()
const;
568 unsigned int get_version()
const;
574 std::string get_class();
575 std::function<void()> &on_destroy();
585 wl_global *global =
nullptr;
590 wayland::detail::any user_data;
591 std::atomic<unsigned int> counter{1};
594 global_base_t(display_t &display,
const wl_interface* interface,
int version, data_t *dat, wl_global_bind_func_t func);
604 wl_global *c_ptr()
const;
605 wayland::detail::any &user_data();
612 template <
typename resource>
623 template <
class resource>
627 struct data_t :
public global_base_t::data_t
629 std::function<void(client_t, resource)> bind;
632 static void bind_func(wl_client *cl,
void *d, uint32_t ver, uint32_t
id)
634 auto *data =
reinterpret_cast<data_t*
>(d);
636 resource res(client, ver,
id);
638 data->bind(client, res);
649 global_t(display_t &display,
unsigned int version = resource::max_version)
650 :
global_base_t(display, resource::interface, version, new data_t, bind_func)
660 std::function<void(client_t, resource)> &
on_bind()
662 return static_cast<data_t*
>(data)->bind;
666 struct fd_event_mask_t :
public wayland::detail::bitfield<2, -1>
668 fd_event_mask_t(
const wayland::detail::bitfield<2, -1> &b)
669 : wayland::detail::bitfield<2, -1>(b) {}
670 fd_event_mask_t(
const uint32_t value)
671 : wayland::detail::bitfield<2, -1>(value) {}
672 static const wayland::detail::bitfield<2, -1> readable;
673 static const wayland::detail::bitfield<2, -1> writable;
674 static const wayland::detail::bitfield<2, -1> hangup;
675 static const wayland::detail::bitfield<2, -1> error;
683 std::function<void()> destroy;
684 detail::listener_t destroy_listener;
685 std::list<std::function<int(
int, uint32_t)>> fd_funcs;
686 std::list<std::function<int()>> timer_funcs;
687 std::list<std::function<int(
int)>> signal_funcs;
688 std::list<std::function<void()>> idle_funcs;
689 wayland::detail::any user_data;
690 bool do_delete =
true;
691 std::atomic<unsigned int> counter{1};
694 wl_event_loop *event_loop =
nullptr;
695 data_t *data =
nullptr;
697 static data_t *wl_event_loop_get_user_data(wl_event_loop *client);
698 static void destroy_func(wl_listener *listener,
void *data);
699 static int event_loop_fd_func(
int fd, uint32_t mask,
void *data);
700 static int event_loop_timer_func(
void *data);
701 static int event_loop_signal_func(
int signal_number,
void *data);
702 static void event_loop_idle_func(
void *data);
705 event_loop_t(wl_event_loop *p);
709 friend class display_t;
714 event_loop_t(
const event_loop_t& e);
715 event_loop_t(event_loop_t&& e)
noexcept;
716 event_loop_t &operator=(
const event_loop_t& e);
717 event_loop_t &operator=(event_loop_t&& e)
noexcept;
718 bool operator==(
const event_loop_t& e)
const;
719 wl_event_loop *c_ptr()
const;
720 wayland::detail::any &user_data();
740 event_source_t add_fd(
int fd,
const fd_event_mask_t& mask,
const std::function<
int(
int, uint32_t)> &func);
750 event_source_t add_timer(
const std::function<
int()> &func);
766 event_source_t add_signal(
int signal_number,
const std::function<
int(
int)> &func);
783 event_source_t add_idle(
const std::function<
void()> &func);
784 const std::function<void()> &on_destroy();
803 int dispatch(
int timeout)
const;
807 void dispatch_idle()
const;
828 wl_event_source *event_source =
nullptr;
831 event_source_t(wl_event_source *p);
832 friend class event_loop_t;
835 event_source_t() =
delete;
836 wl_event_source *c_ptr()
const;
851 int timer_update(
int ms_delay)
const;
868 int fd_update(
const fd_event_mask_t& mask)
const;
Refcounted wrapper for C objects.
global_t(display_t &display, unsigned int version=resource::max_version)
std::function< void(client_t, resource)> & on_bind()
void set_log_handler(log_handler handler)
Set C library log handler.
std::function< void(std::string)> log_handler
Type for functions that handle log messages.