40#include <wayland-client-protocol-extra.hpp>
41#include <linux/input.h>
42#include <wayland-cursor.hpp>
49using namespace wayland;
52template <
typename R,
typename T,
typename... Args>
53std::function<R(Args...)> bind_mem_fn(R(T::* func)(Args...), T *t)
55 return [func, t] (Args... args)
57 return (t->*func)(args...);
71 shared_mem_t() =
default;
72 shared_mem_t(
const shared_mem_t&) =
delete;
73 shared_mem_t(shared_mem_t&&) noexcept = delete;
74 shared_mem_t& operator=(const shared_mem_t&) = delete;
75 shared_mem_t& operator=(shared_mem_t&&) noexcept = delete;
77 shared_mem_t(
size_t size)
82 std::random_device device;
83 std::default_random_engine engine(device());
84 std::uniform_int_distribution<unsigned int> distribution(0, std::numeric_limits<unsigned int>::max());
85 ss << distribution(engine);
89 fd = memfd_create(name.c_str(), 0);
91 throw std::runtime_error(
"shm_open failed.");
94 if(ftruncate(fd, size) < 0)
95 throw std::runtime_error(
"ftruncate failed.");
98 mem = mmap(
nullptr, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
100 throw std::runtime_error(
"mmap failed.");
103 ~shared_mem_t() noexcept
107 if(munmap(mem, len) < 0)
108 std::cerr <<
"munmap failed.";
110 std::cerr <<
"close failed.";
111 if(shm_unlink(name.c_str()) < 0)
112 std::cerr <<
"shm_unlink failed";
134 compositor_t compositor;
136 xdg_wm_base_t xdg_wm_base;
142 shell_surface_t shell_surface;
143 xdg_surface_t xdg_surface;
144 xdg_toplevel_t xdg_toplevel;
148 cursor_image_t cursor_image;
149 buffer_t cursor_buffer;
150 surface_t cursor_surface;
152 std::shared_ptr<shared_mem_t> shared_mem;
153 std::array<buffer_t, 2> buffer;
160 void draw(uint32_t serial = 0)
162 float h =
static_cast<float>((serial >> 4) & 0xFF)/255.0F;
166 int hi =
static_cast<int>(h*6);
167 float f = h*6 -
static_cast<float>(hi);
170 float t = v*(1-s*(1-f));
198 uint32_t pixel = (0x80 << 24)
199 | (
static_cast<uint32_t
>(r * 255.0) << 16)
200 | (
static_cast<uint32_t
>(g * 255.0) << 8)
201 |
static_cast<uint32_t
>(b * 255.0);
203 std::fill_n(
static_cast<uint32_t*
>(shared_mem->get_mem())+cur_buf*320*240, 320*240, pixel);
204 surface.attach(buffer.at(cur_buf), 0, 0);
205 surface.damage(0, 0, 320, 240);
212 frame_cb = surface.frame();
213 frame_cb.on_done() = bind_mem_fn(&example::draw,
this);
218 example(
const example&) =
delete;
219 example(example&&) noexcept = delete;
220 ~example() noexcept = default;
221 example& operator=(const example&) = delete;
222 example& operator=(example&&) noexcept = delete;
227 registry = display.get_registry();
228 registry.on_global() = [&] (uint32_t name,
const std::string& interface, uint32_t version)
230 if(interface == compositor_t::interface_name)
231 registry.bind(name, compositor, version);
232 else if(interface == shell_t::interface_name)
233 registry.bind(name, shell, version);
234 else if(interface == xdg_wm_base_t::interface_name)
235 registry.bind(name, xdg_wm_base, version);
236 else if(interface == seat_t::interface_name)
237 registry.bind(name, seat, version);
238 else if(interface == shm_t::interface_name)
239 registry.bind(name, shm, version);
243 seat.on_capabilities() = [&] (
const seat_capability& capability)
250 surface = compositor.create_surface();
255 xdg_wm_base.on_ping() = [&] (uint32_t serial) { xdg_wm_base.pong(serial); };
256 xdg_surface = xdg_wm_base.get_xdg_surface(surface);
257 xdg_surface.on_configure() = [&] (uint32_t serial) { xdg_surface.ack_configure(serial); };
258 xdg_toplevel = xdg_surface.get_toplevel();
259 xdg_toplevel.set_title(
"Window");
260 xdg_toplevel.on_close() = [&] () { running =
false; };
264 shell_surface = shell.get_shell_surface(surface);
265 shell_surface.on_ping() = [&] (uint32_t serial) { shell_surface.pong(serial); };
266 shell_surface.set_title(
"Window");
267 shell_surface.set_toplevel();
275 throw std::runtime_error(
"No keyboard found.");
277 throw std::runtime_error(
"No pointer found.");
279 pointer = seat.get_pointer();
280 keyboard = seat.get_keyboard();
283 shared_mem = std::make_shared<shared_mem_t>(2*320*240*4);
284 auto pool = shm.create_pool(shared_mem->get_fd(), 2*320*240*4);
285 for(
unsigned int c = 0; c < 2; c++)
286 buffer.at(c) = pool.create_buffer(c*320*240*4, 320, 240, 320*4, shm_format::argb8888);
290 cursor_theme_t cursor_theme = cursor_theme_t(
"default", 16, shm);
291 cursor_t cursor = cursor_theme.get_cursor(
"cross");
292 cursor_image = cursor.image(0);
293 cursor_buffer = cursor_image.get_buffer();
296 cursor_surface = compositor.create_surface();
299 pointer.on_enter() = [&] (uint32_t serial,
const surface_t& , int32_t , int32_t )
301 cursor_surface.attach(cursor_buffer, 0, 0);
302 cursor_surface.damage(0, 0, cursor_image.width(), cursor_image.height());
303 cursor_surface.commit();
304 pointer.set_cursor(serial, cursor_surface, 0, 0);
308 pointer.on_button() = [&] (uint32_t serial, uint32_t , uint32_t button, pointer_button_state state)
310 if(button == BTN_LEFT && state == pointer_button_state::pressed)
313 xdg_toplevel.move(seat, serial);
315 shell_surface.move(seat, serial);
320 keyboard.on_key() = [&] (uint32_t , uint32_t , uint32_t key, keyboard_key_state state)
322 if(key == KEY_Q && state == keyboard_key_state::pressed)
static const wayland::detail::bitfield< 3, 12 > keyboard
the seat has one or more keyboards
static const wayland::detail::bitfield< 3, 12 > pointer
the seat has pointer devices