Wayland++  0.2.2
C++ Bindings for Wayland
shm.cpp
1 /*
2  * Copyright (c) 2014-2017, Nils Christopher Brause
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
30 #include <stdexcept>
31 #include <iostream>
32 #include <array>
33 #include <memory>
34 #include <sstream>
35 #include <ctime>
36 #include <algorithm>
37 
38 #include <wayland-client.hpp>
39 #include <wayland-client-protocol-extra.hpp>
40 #include <linux/input.h>
41 #include <wayland-cursor.hpp>
42 
43 #include <sys/mman.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <unistd.h>
47 
48 using namespace wayland;
49 
50 // helper to create a std::function out of a member function and an object
51 template <typename R, typename T, typename... Args>
52 std::function<R(Args...)> bind_mem_fn(R(T::* func)(Args...), T *t)
53 {
54  return [func, t] (Args... args)
55  {
56  return (t->*func)(args...);
57  };
58 }
59 
60 // shared memory helper class
61 class shared_mem_t
62 {
63 private:
64  std::string name;
65  int fd = 0;
66  size_t len = 0;
67  void *mem = nullptr;
68 
69 public:
70  shared_mem_t()
71  {
72  }
73 
74  shared_mem_t(size_t size)
75  : len(size)
76  {
77  // Very simple shared memory wrapper - do not use this in production code!
78  // The generated memory regions are visible in the file system and can be
79  // stolen by other running processes.
80  // Linux code should use memfd_create when possible (ommited here for
81  // simplicity).
82 
83  // create random filename
84  std::stringstream ss;
85  std::srand(std::time(0));
86  ss << '/' << std::rand();
87  name = ss.str();
88 
89  // open shared memory file
90  fd = shm_open(name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0600);
91  if(fd < 0)
92  throw std::runtime_error("shm_open failed.");
93 
94  // set size
95  if(ftruncate(fd, size) < 0)
96  throw std::runtime_error("ftruncate failed.");
97 
98  // map memory
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.");
102  }
103 
104  ~shared_mem_t() noexcept(false)
105  {
106  if(fd)
107  {
108  if(munmap(mem, len) < 0)
109  throw std::runtime_error("munmap failed.");
110  if(close(fd) < 0)
111  throw std::runtime_error("close failed.");
112  if(shm_unlink(name.c_str()) < 0)
113  throw std::runtime_error("shm_unlink failed");
114  }
115  }
116 
117  int get_fd()
118  {
119  return fd;
120  }
121 
122  void *get_mem()
123  {
124  return mem;
125  }
126 };
127 
128 // example Wayland client
129 class example
130 {
131 private:
132  // global objects
133  display_t display;
134  registry_t registry;
135  compositor_t compositor;
136  shell_t shell;
137  xdg_wm_base_t xdg_wm_base;
138  seat_t seat;
139  shm_t shm;
140 
141  // local objects
142  surface_t surface;
143  shell_surface_t shell_surface;
144  xdg_surface_t xdg_surface;
145  xdg_toplevel_t xdg_toplevel;
146  pointer_t pointer;
147  keyboard_t keyboard;
148  callback_t frame_cb;
149  cursor_image_t cursor_image;
150  buffer_t cursor_buffer;
151  surface_t cursor_surface;
152 
153  std::shared_ptr<shared_mem_t> shared_mem;
154  buffer_t buffer[2];
155  int cur_buf;
156 
157  bool running;
158  bool has_pointer;
159  bool has_keyboard;
160 
161  void draw(uint32_t serial = 0)
162  {
163  float h = ((serial >> 4) & 0xFF)/255.0;
164  float s = 1, v = 1;
165 
166  int hi = h*6;
167  float f = h*6 - hi;
168  float p = v*(1-s);
169  float q = v*(1-s*f);
170  float t = v*(1-s*(1-f));
171  float r, g, b;
172 
173  switch(hi)
174  {
175  case 1:
176  r = q; g = v; b = p;
177  break;
178  case 2:
179  r = p; g = v; b = t;
180  break;
181  case 3:
182  r = p; g = q; b = v;
183  break;
184  case 4:
185  r = t; g = p; b = v;
186  break;
187  case 5:
188  r = v; g = p; b = q;
189  break;
190  default: // 0,6
191  r = v; g = t; b = p;
192  break;
193  }
194 
195  // draw stuff
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);
200 
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);
204  if(!cur_buf)
205  cur_buf = 1;
206  else
207  cur_buf = 0;
208 
209  // schedule next draw
210  frame_cb = surface.frame();
211  frame_cb.on_done() = bind_mem_fn(&example::draw, this);
212  surface.commit();
213  }
214 
215 public:
216  example()
217  {
218  // retrieve global objects
219  registry = display.get_registry();
220  registry.on_global() = [&] (uint32_t name, std::string interface, uint32_t version)
221  {
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);
232  };
233  display.roundtrip();
234 
235  seat.on_capabilities() = [&] (seat_capability capability)
236  {
237  has_keyboard = capability & seat_capability::keyboard;
238  has_pointer = capability & seat_capability::pointer;
239  };
240  display.roundtrip();
241 
242  if(!has_keyboard)
243  throw std::runtime_error("No keyboard found.");
244  if(!has_pointer)
245  throw std::runtime_error("No pointer found.");
246 
247  // create shared memory
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);
252  cur_buf = 0;
253 
254  // create a surface
255  surface = compositor.create_surface();
256 
257  // create a shell surface
258  if(xdg_wm_base)
259  {
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; };
265  }
266  else
267  {
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();
272  }
273 
274  // Get input devices
275  pointer = seat.get_pointer();
276  keyboard = seat.get_keyboard();
277 
278  // load cursor theme
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();
283 
284  // create cursor surface
285  cursor_surface = compositor.create_surface();
286 
287  // draw cursor
288  pointer.on_enter() = [&] (uint32_t serial, surface_t, int32_t, int32_t)
289  {
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);
294  };
295 
296  // window movement
297  pointer.on_button() = [&] (uint32_t serial, uint32_t time, uint32_t button, pointer_button_state state)
298  {
299  if(button == BTN_LEFT && state == pointer_button_state::pressed)
300  {
301  if(xdg_toplevel)
302  xdg_toplevel.move(seat, serial);
303  else
304  shell_surface.move(seat, serial);
305  }
306  };
307 
308  // press 'q' to exit
309  keyboard.on_key() = [&] (uint32_t, uint32_t, uint32_t key, keyboard_key_state state)
310  {
311  if(key == KEY_Q && state == keyboard_key_state::pressed)
312  running = false;
313  };
314 
315  // draw stuff
316  draw();
317  }
318 
319  ~example()
320  {
321  }
322 
323  void run()
324  {
325  // event loop
326  running = true;
327  while(running)
328  display.dispatch();
329  }
330 };
331 
332 int main()
333 {
334  example e;
335  e.run();
336  return 0;
337 }
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.