26#ifndef WAYLAND_SERVER_HPP
27#define WAYLAND_SERVER_HPP
36#include <wayland-server-core.h>
37#include <wayland-util.hpp>
49 wl_listener listener = { {
nullptr,
nullptr },
nullptr };
72 template <
class resource>
class global_t;
81 std::function<void()> destroy;
82 std::function<void(client_t&)> client_created;
83 detail::listener_t destroy_listener;
84 detail::listener_t client_created_listener;
86 wayland::detail::any user_data;
87 std::atomic<unsigned int> counter{1};
90 wl_display *display =
nullptr;
91 data_t *data =
nullptr;
93 static void destroy_func(wl_listener *listener,
void *data);
94 static void client_created_func(wl_listener *listener,
void *cl);
95 static data_t *wl_display_get_user_data(wl_display *display);
96 static bool c_filter_func(
const wl_client *client,
const wl_global *global,
void *data);
99 display_t(wl_display *c);
103 friend class client_t;
122 display_t(
const display_t& d);
123 display_t(display_t&& d)
noexcept;
124 display_t &operator=(
const display_t& d);
125 display_t &operator=(display_t&& d)
noexcept;
126 bool operator==(
const display_t& d)
const;
127 wl_display *c_ptr()
const;
128 wayland::detail::any &user_data();
135 event_loop_t get_event_loop()
const;
158 int add_socket(
const std::string& name)
const;
165 std::string add_socket_auto()
const;
176 int add_socket_fd(
int sock_fd)
const;
183 void terminate()
const;
198 void flush_clients()
const;
205 uint32_t get_serial()
const;
212 uint32_t next_serial()
const;
213 std::function<void()> &on_destroy();
219 std::function<void(client_t&)> &on_client_created();
227 client_t client_create(
int fd);
236 std::list<client_t> get_client_list()
const;
252 void set_global_filter(
const std::function<
bool(client_t, global_base_t)>& filter);
262 wl_client *client =
nullptr;
263 std::function<void()> destroy;
264 detail::listener_t destroy_listener;
265 wayland::detail::any user_data;
266 std::atomic<unsigned int> counter{1};
267 bool destroyed =
false;
270 wl_client *client =
nullptr;
271 data_t *data =
nullptr;
273 static void destroy_func(wl_listener *listener,
void *data);
274 static wl_iterator_result resource_iterator(wl_resource *resource,
void *data);
275 static data_t *wl_client_get_user_data(wl_client *client);
278 client_t(wl_client *c);
282 friend class display_t;
283 friend class resource_t;
284 template <
class resource>
friend class global_t;
310 client_t(display_t &display,
int fd);
313 client_t(
const client_t &c);
314 client_t(client_t &&c)
noexcept;
315 client_t &operator=(
const client_t& c);
316 client_t &operator=(client_t&& c)
noexcept;
317 bool operator==(
const client_t &c)
const;
318 wl_client *c_ptr()
const;
319 wayland::detail::any &user_data();
345 void get_credentials(pid_t &pid, uid_t &uid, gid_t &gid)
const;
374 std::function<void()> &on_destroy();
384 resource_t get_object(uint32_t
id);
392 void post_no_memory()
const;
401 void post_implementation_error(std::string
const& msg)
const;
411 template <
typename...types>
412 void post_implementation_error(std::string
const& msg, types&&... args)
414 wl_client_post_implementation_error(c_ptr(), msg.c_str(), std::forward<types...>(args...));
421 display_t get_display()
const;
427 std::list<resource_t> get_resource_list()
const;
436 events_base_t() =
default;
437 events_base_t(
const events_base_t& e) =
default;
438 events_base_t(events_base_t&& e) =
default;
439 events_base_t& operator=(
const events_base_t& e) =
default;
440 events_base_t& operator=(events_base_t&& e) =
default;
441 virtual ~events_base_t() =
default;
447 std::shared_ptr<events_base_t> events;
448 std::function<void()> destroy;
449 detail::listener_t destroy_listener;
450 wayland::detail::any user_data;
451 std::atomic<unsigned int> counter{1};
452 bool destroyed =
false;
455 wl_resource *resource =
nullptr;
456 data_t *data =
nullptr;
458 static void destroy_func(wl_listener *listener,
void *data);
459 static int c_dispatcher(
const void *implementation,
void *target,
460 uint32_t opcode,
const wl_message *message,
462 static int dummy_dispatcher(
int opcode,
const std::vector<wayland::detail::any>& args,
const std::shared_ptr<resource_t::events_base_t>& events);
466 static constexpr const wl_interface *
interface = nullptr;
473 void set_events(
const std::shared_ptr<events_base_t>& events,
474 int(*dispatcher)(
int,
const std::vector<wayland::detail::any>&,
const std::shared_ptr<resource_t::events_base_t>&));
477 std::shared_ptr<events_base_t> get_events()
const;
479 void post_event_array(uint32_t opcode,
const std::vector<wayland::detail::argument_t>& v)
const;
480 void queue_event_array(uint32_t opcode,
const std::vector<wayland::detail::argument_t>& v)
const;
482 template <
typename...T>
483 void post_event(uint32_t opcode, T...args)
const
485 std::vector<wayland::detail::argument_t> v = { wayland::detail::argument_t(args)... };
487 post_event_array(opcode, v);
490 template <
typename...T>
491 void queue_event(uint32_t opcode, T...args)
const
493 std::vector<wayland::detail::argument_t> v = { wayland::detail::argument_t(args)... };
495 queue_event_array(opcode, v);
498 template <
typename...T>
499 void send_event(
bool post, uint32_t opcode, T...args)
const
502 post_event(opcode, args...);
504 queue_event(opcode, args...);
507 void post_error(uint32_t code,
const std::string& msg)
const;
509 resource_t(wl_resource *c);
513 friend class client_t;
516 resource_t() =
default;
528 resource_t(
const client_t& client,
const wl_interface *interface,
int version, uint32_t
id);
530 resource_t(
const resource_t &r);
531 resource_t(resource_t &&r)
noexcept;
532 resource_t &operator=(
const resource_t& r);
533 resource_t &operator=(resource_t&& r)
noexcept;
534 bool operator==(
const resource_t& r)
const;
535 operator bool()
const;
536 wl_resource *c_ptr()
const;
537 wayland::detail::any &user_data();
543 bool proxy_has_object()
const;
551 void post_no_memory()
const;
557 uint32_t get_id()
const;
563 client_t get_client()
const;
569 unsigned int get_version()
const;
575 std::string get_class();
576 std::function<void()> &on_destroy();
586 wl_global *global =
nullptr;
591 wayland::detail::any user_data;
592 std::atomic<unsigned int> counter{1};
595 global_base_t(display_t &display,
const wl_interface* interface,
int version, data_t *dat, wl_global_bind_func_t func);
605 wl_global *c_ptr()
const;
606 wayland::detail::any &user_data();
613 template <
typename resource>
624 template <
class resource>
628 struct data_t :
public global_base_t::data_t
630 std::function<void(client_t, resource)> bind;
633 static void bind_func(wl_client *cl,
void *d, uint32_t ver, uint32_t
id)
635 auto *data =
reinterpret_cast<data_t*
>(d);
637 resource res(client, ver,
id);
639 data->bind(client, res);
650 global_t(display_t &display,
unsigned int version = resource::max_version)
651 :
global_base_t(display, resource::interface, version, new data_t, bind_func)
661 std::function<void(client_t, resource)> &
on_bind()
663 return static_cast<data_t*
>(data)->bind;
667 struct fd_event_mask_t :
public wayland::detail::bitfield<2, -1>
669 fd_event_mask_t(
const wayland::detail::bitfield<2, -1> &b)
670 : wayland::detail::bitfield<2, -1>(b) {}
671 fd_event_mask_t(
const uint32_t value)
672 : wayland::detail::bitfield<2, -1>(value) {}
673 static const wayland::detail::bitfield<2, -1> readable;
674 static const wayland::detail::bitfield<2, -1> writable;
675 static const wayland::detail::bitfield<2, -1> hangup;
676 static const wayland::detail::bitfield<2, -1> error;
684 std::function<void()> destroy;
685 detail::listener_t destroy_listener;
686 std::list<std::function<int(
int, uint32_t)>> fd_funcs;
687 std::list<std::function<int()>> timer_funcs;
688 std::list<std::function<int(
int)>> signal_funcs;
689 std::list<std::function<void()>> idle_funcs;
690 wayland::detail::any user_data;
691 bool do_delete =
true;
692 std::atomic<unsigned int> counter{1};
695 wl_event_loop *event_loop =
nullptr;
696 data_t *data =
nullptr;
698 static data_t *wl_event_loop_get_user_data(wl_event_loop *client);
699 static void destroy_func(wl_listener *listener,
void *data);
700 static int event_loop_fd_func(
int fd, uint32_t mask,
void *data);
701 static int event_loop_timer_func(
void *data);
702 static int event_loop_signal_func(
int signal_number,
void *data);
703 static void event_loop_idle_func(
void *data);
706 event_loop_t(wl_event_loop *p);
710 friend class display_t;
715 event_loop_t(
const event_loop_t& e);
716 event_loop_t(event_loop_t&& e)
noexcept;
717 event_loop_t &operator=(
const event_loop_t& e);
718 event_loop_t &operator=(event_loop_t&& e)
noexcept;
719 bool operator==(
const event_loop_t& e)
const;
720 wl_event_loop *c_ptr()
const;
721 wayland::detail::any &user_data();
741 event_source_t add_fd(
int fd,
const fd_event_mask_t& mask,
const std::function<
int(
int, uint32_t)> &func);
751 event_source_t add_timer(
const std::function<
int()> &func);
767 event_source_t add_signal(
int signal_number,
const std::function<
int(
int)> &func);
784 event_source_t add_idle(
const std::function<
void()> &func);
785 const std::function<void()> &on_destroy();
804 int dispatch(
int timeout)
const;
808 void dispatch_idle()
const;
829 wl_event_source *event_source =
nullptr;
832 event_source_t(wl_event_source *p);
833 friend class event_loop_t;
836 event_source_t() =
delete;
837 wl_event_source *c_ptr()
const;
852 int timer_update(
int ms_delay)
const;
869 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.