39 #include <wayland-client-protocol-extra.hpp> 40 #include <linux/input.h> 41 #include <wayland-cursor.hpp> 51 template <
typename R,
typename T,
typename... Args>
52 std::function<R(Args...)> bind_mem_fn(R(T::* func)(Args...), T *t)
54 return [func, t] (Args... args)
56 return (t->*func)(args...);
74 shared_mem_t(
size_t size)
85 std::srand(std::time(0));
86 ss <<
'/' << std::rand();
90 fd = shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
92 throw std::runtime_error(
"shm_open failed.");
95 if(ftruncate(fd, size) < 0)
96 throw std::runtime_error(
"ftruncate failed.");
99 mem = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
100 if(mem == MAP_FAILED)
101 throw std::runtime_error(
"mmap failed.");
104 ~shared_mem_t() noexcept(false)
108 if(munmap(mem, len) < 0)
109 throw std::runtime_error(
"munmap failed.");
111 throw std::runtime_error(
"close failed.");
112 if(shm_unlink(name.c_str()) < 0)
113 throw std::runtime_error(
"shm_unlink failed");
135 compositor_t compositor;
137 xdg_wm_base_t xdg_wm_base;
143 shell_surface_t shell_surface;
144 xdg_surface_t xdg_surface;
145 xdg_toplevel_t xdg_toplevel;
149 cursor_image_t cursor_image;
150 buffer_t cursor_buffer;
151 surface_t cursor_surface;
153 std::shared_ptr<shared_mem_t> shared_mem;
161 void draw(uint32_t serial = 0)
163 float h = ((serial >> 4) & 0xFF)/255.0;
170 float t = v*(1-s*(1-f));
196 uint32_t pixel = (0x80 << 24)
197 | (static_cast<uint32_t>(r * 255.0) << 16)
198 | (static_cast<uint32_t>(g * 255.0) << 8)
199 | static_cast<uint32_t>(b * 255.0);
201 std::fill_n(static_cast<uint32_t*>(shared_mem->get_mem())+cur_buf*320*240, 320*240, pixel);
202 surface.attach(buffer[cur_buf], 0, 0);
203 surface.damage(0, 0, 320, 240);
210 frame_cb = surface.frame();
211 frame_cb.on_done() = bind_mem_fn(&example::draw,
this);
220 registry.on_global() = [&] (uint32_t name, std::string interface, uint32_t version)
222 if(interface == compositor_t::interface_name)
223 registry.bind(name, compositor, version);
224 else if(interface == shell_t::interface_name)
225 registry.bind(name, shell, version);
226 else if(interface == xdg_wm_base_t::interface_name)
227 registry.bind(name, xdg_wm_base, version);
228 else if(interface == seat_t::interface_name)
229 registry.bind(name, seat, version);
230 else if(interface == shm_t::interface_name)
231 registry.bind(name, shm, version);
235 seat.on_capabilities() = [&] (seat_capability capability)
237 has_keyboard = capability & seat_capability::keyboard;
238 has_pointer = capability & seat_capability::pointer;
243 throw std::runtime_error(
"No keyboard found.");
245 throw std::runtime_error(
"No pointer found.");
248 shared_mem = std::shared_ptr<shared_mem_t>(
new shared_mem_t(2*320*240*4));
249 auto pool = shm.create_pool(shared_mem->get_fd(), 2*320*240*4);
250 for(
unsigned int c = 0; c < 2; c++)
251 buffer[c] = pool.create_buffer(c*320*240*4, 320, 240, 320*4, shm_format::argb8888);
255 surface = compositor.create_surface();
260 xdg_wm_base.on_ping() = [&] (uint32_t serial) { xdg_wm_base.pong(serial); };
261 xdg_surface = xdg_wm_base.get_xdg_surface(surface);
262 xdg_toplevel = xdg_surface.get_toplevel();
263 xdg_toplevel.set_title(
"Window");
264 xdg_toplevel.on_close() = [&] () { running =
false; };
268 shell_surface = shell.get_shell_surface(surface);
269 shell_surface.on_ping() = [&] (uint32_t serial) { shell_surface.pong(serial); };
270 shell_surface.set_title(
"Window");
271 shell_surface.set_toplevel();
275 pointer = seat.get_pointer();
276 keyboard = seat.get_keyboard();
279 cursor_theme_t cursor_theme = cursor_theme_t(
"default", 16, shm);
280 cursor_t cursor = cursor_theme.get_cursor(
"cross");
281 cursor_image = cursor.image(0);
282 cursor_buffer = cursor_image.get_buffer();
285 cursor_surface = compositor.create_surface();
288 pointer.on_enter() = [&] (uint32_t serial, surface_t, int32_t, int32_t)
290 cursor_surface.attach(cursor_buffer, 0, 0);
291 cursor_surface.damage(0, 0, cursor_image.width(), cursor_image.height());
292 cursor_surface.commit();
293 pointer.set_cursor(serial, cursor_surface, 0, 0);
297 pointer.on_button() = [&] (uint32_t serial, uint32_t time, uint32_t button, pointer_button_state state)
299 if(button == BTN_LEFT && state == pointer_button_state::pressed)
302 xdg_toplevel.move(seat, serial);
304 shell_surface.move(seat, serial);
309 keyboard.on_key() = [&] (uint32_t, uint32_t, uint32_t key, keyboard_key_state state)
311 if(key == KEY_Q && state == keyboard_key_state::pressed)
registry_t get_registry()
get global registry object
Represents a connection to the compositor and acts as a proxy to the display singleton object...
int dispatch()
Process incoming events.
int roundtrip()
Block until all pending request are processed by the server.