Wayland++  0.2.6
C++ Bindings for Wayland
egl.cpp
1 /*
2  * Copyright (c) 2014-2019, Nils Christopher Brause, Philipp Kerling, Zsolt Bölöny
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 <wayland-client.hpp>
34 #include <wayland-client-protocol-extra.hpp>
35 #include <wayland-egl.hpp>
36 #include <GL/gl.h>
37 #include <linux/input.h>
38 #include <wayland-cursor.hpp>
39 
40 using namespace wayland;
41 
42 // helper to create a std::function out of a member function and an object
43 template <typename R, typename T, typename... Args>
44 std::function<R(Args...)> bind_mem_fn(R(T::* func)(Args...), T *t)
45 {
46  return [func, t] (Args... args)
47  {
48  return (t->*func)(args...);
49  };
50 }
51 
52 // example Wayland client
53 class example
54 {
55 private:
56  // global objects
57  display_t display;
58  registry_t registry;
59  compositor_t compositor;
60  shell_t shell;
61  xdg_wm_base_t xdg_wm_base;
62  seat_t seat;
63  shm_t shm;
64 
65  // local objects
66  surface_t surface;
67  shell_surface_t shell_surface;
68  xdg_surface_t xdg_surface;
69  xdg_toplevel_t xdg_toplevel;
70  pointer_t pointer;
71  keyboard_t keyboard;
72  callback_t frame_cb;
73  cursor_image_t cursor_image;
74  buffer_t cursor_buffer;
75  surface_t cursor_surface;
76 
77  // EGL
78  egl_window_t egl_window;
79  EGLDisplay egldisplay;
80  EGLSurface eglsurface;
81  EGLContext eglcontext;
82 
83  bool running;
84  bool has_pointer;
85  bool has_keyboard;
86 
87  void init_egl()
88  {
89  egldisplay = eglGetDisplay(display);
90  if(egldisplay == EGL_NO_DISPLAY)
91  throw std::runtime_error("eglGetDisplay");
92 
93  EGLint major, minor;
94  if(eglInitialize(egldisplay, &major, &minor) == EGL_FALSE)
95  throw std::runtime_error("eglInitialize");
96  if(!((major == 1 && minor >= 4) || major >= 2))
97  throw std::runtime_error("EGL version too old");
98 
99  if(eglBindAPI(EGL_OPENGL_API) == EGL_FALSE)
100  throw std::runtime_error("eglBindAPI");
101 
102  std::array<EGLint, 13> config_attribs = {{
103  EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
104  EGL_RED_SIZE, 8,
105  EGL_GREEN_SIZE, 8,
106  EGL_BLUE_SIZE, 8,
107  EGL_ALPHA_SIZE, 8,
108  EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
109  EGL_NONE
110  }};
111 
112  EGLConfig config;
113  EGLint num;
114  if(eglChooseConfig(egldisplay, config_attribs.data(), &config, 1, &num) == EGL_FALSE || num == 0)
115  throw std::runtime_error("eglChooseConfig");
116 
117  std::array<EGLint, 3> context_attribs = {{
118  EGL_CONTEXT_CLIENT_VERSION, 2,
119  EGL_NONE
120  }};
121 
122  eglcontext = eglCreateContext(egldisplay, config, EGL_NO_CONTEXT, context_attribs.data());
123  if(eglcontext == EGL_NO_CONTEXT)
124  throw std::runtime_error("eglCreateContext");
125 
126  eglsurface = eglCreateWindowSurface(egldisplay, config, egl_window, nullptr);
127  if(eglsurface == EGL_NO_SURFACE)
128  throw std::runtime_error("eglCreateWindowSurface");
129 
130  if(eglMakeCurrent(egldisplay, eglsurface, eglsurface, eglcontext) == EGL_FALSE)
131  throw std::runtime_error("eglMakeCurrent");
132  }
133 
134  void draw(uint32_t serial = 0)
135  {
136  float h = ((serial >> 4) & 0xFF)/255.0;
137  float s = 1, v = 1;
138 
139  int hi = static_cast<int>(h*6);
140  float f = h*6 - hi;
141  float p = v*(1-s);
142  float q = v*(1-s*f);
143  float t = v*(1-s*(1-f));
144  float r, g, b;
145 
146  switch(hi)
147  {
148  case 1:
149  r = q; g = v; b = p;
150  break;
151  case 2:
152  r = p; g = v; b = t;
153  break;
154  case 3:
155  r = p; g = q; b = v;
156  break;
157  case 4:
158  r = t; g = p; b = v;
159  break;
160  case 5:
161  r = v; g = p; b = q;
162  break;
163  default: // 0,6
164  r = v; g = t; b = p;
165  break;
166  }
167 
168  // draw stuff
169  glClearColor(r, g, b, 0.5f);
170  glClear(GL_COLOR_BUFFER_BIT);
171 
172  // schedule next draw
173  frame_cb = surface.frame();
174  frame_cb.on_done() = bind_mem_fn(&example::draw, this);
175 
176  // swap buffers
177  if(eglSwapBuffers(egldisplay, eglsurface) == EGL_FALSE)
178  throw std::runtime_error("eglSwapBuffers");
179  }
180 
181 public:
182  example()
183  {
184  // retrieve global objects
185  registry = display.get_registry();
186  registry.on_global() = [&] (uint32_t name, std::string interface, uint32_t version)
187  {
188  if(interface == compositor_t::interface_name)
189  registry.bind(name, compositor, version);
190  else if(interface == shell_t::interface_name)
191  registry.bind(name, shell, version);
192  else if(interface == xdg_wm_base_t::interface_name)
193  registry.bind(name, xdg_wm_base, version);
194  else if(interface == seat_t::interface_name)
195  registry.bind(name, seat, version);
196  else if(interface == shm_t::interface_name)
197  registry.bind(name, shm, version);
198  };
199  display.roundtrip();
200 
201  seat.on_capabilities() = [&] (seat_capability capability)
202  {
203  has_keyboard = capability & seat_capability::keyboard;
204  has_pointer = capability & seat_capability::pointer;
205  };
206 
207  // create a surface
208  surface = compositor.create_surface();
209 
210  // create a shell surface
211  if(xdg_wm_base)
212  {
213  xdg_wm_base.on_ping() = [&] (uint32_t serial) { xdg_wm_base.pong(serial); };
214  xdg_surface = xdg_wm_base.get_xdg_surface(surface);
215  xdg_surface.on_configure() = [&] (uint32_t serial) { xdg_surface.ack_configure(serial); };
216  xdg_toplevel = xdg_surface.get_toplevel();
217  xdg_toplevel.set_title("Window");
218  xdg_toplevel.on_close() = [&] () { running = false; };
219  }
220  else
221  {
222  shell_surface = shell.get_shell_surface(surface);
223  shell_surface.on_ping() = [&] (uint32_t serial) { shell_surface.pong(serial); };
224  shell_surface.set_title("Window");
225  shell_surface.set_toplevel();
226  }
227  surface.commit();
228 
229  display.roundtrip();
230 
231  // Get input devices
232  if(!has_keyboard)
233  throw std::runtime_error("No keyboard found.");
234  if(!has_pointer)
235  throw std::runtime_error("No pointer found.");
236 
237  pointer = seat.get_pointer();
238  keyboard = seat.get_keyboard();
239 
240  // load cursor theme
241  cursor_theme_t cursor_theme = cursor_theme_t("default", 16, shm);
242  cursor_t cursor = cursor_theme.get_cursor("cross");
243  cursor_image = cursor.image(0);
244  cursor_buffer = cursor_image.get_buffer();
245 
246  // create cursor surface
247  cursor_surface = compositor.create_surface();
248 
249  // draw cursor
250  pointer.on_enter() = [&] (uint32_t serial, surface_t, int32_t, int32_t)
251  {
252  cursor_surface.attach(cursor_buffer, 0, 0);
253  cursor_surface.damage(0, 0, cursor_image.width(), cursor_image.height());
254  cursor_surface.commit();
255  pointer.set_cursor(serial, cursor_surface, 0, 0);
256  };
257 
258  // window movement
259  pointer.on_button() = [&] (uint32_t serial, uint32_t time, uint32_t button, pointer_button_state state)
260  {
261  if(button == BTN_LEFT && state == pointer_button_state::pressed)
262  {
263  if(xdg_toplevel)
264  xdg_toplevel.move(seat, serial);
265  else
266  shell_surface.move(seat, serial);
267  }
268  };
269 
270  // press 'q' to exit
271  keyboard.on_key() = [&] (uint32_t, uint32_t, uint32_t key, keyboard_key_state state)
272  {
273  if(key == KEY_Q && state == keyboard_key_state::pressed)
274  running = false;
275  };
276 
277  // intitialize egl
278  egl_window = egl_window_t(surface, 320, 240);
279  init_egl();
280 
281  // draw stuff
282  draw();
283  }
284 
285  ~example() noexcept(false)
286  {
287  // finialize EGL
288  if(eglDestroyContext(egldisplay, eglcontext) == EGL_FALSE)
289  throw std::runtime_error("eglDestroyContext");
290  if(eglTerminate(egldisplay) == EGL_FALSE)
291  throw std::runtime_error("eglTerminate");
292  }
293 
294  void run()
295  {
296  // event loop
297  running = true;
298  while(running)
299  display.dispatch();
300  }
301 };
302 
303 int main()
304 {
305  example e;
306  e.run();
307  return 0;
308 }
wayland::egl_window_t
Native EGL window.
Definition: wayland-egl.hpp:42
wayland::xdg_wm_base_t::pong
void pong(uint32_t serial)
respond to a ping event
Definition: wayland-client-protocol-extra.cpp:968
wayland::surface_t::frame
callback_t frame()
request a frame throttling hint
Definition: wayland-client-protocol.cpp:2633
wayland::display_t
Represents a connection to the compositor and acts as a proxy to the display singleton object.
Definition: wayland-client.hpp:463
wayland::xdg_wm_base_t::get_xdg_surface
xdg_surface_t get_xdg_surface(surface_t surface)
create a shell surface from a surface
Definition: wayland-client-protocol-extra.cpp:962
wayland::pointer_t::on_button
std::function< void(uint32_t, uint32_t, uint32_t, pointer_button_state)> & on_button()
pointer button event
Definition: wayland-client-protocol.cpp:2890
wayland::display_t::dispatch
int dispatch()
Process incoming events.
wayland::display_t::get_registry
registry_t get_registry()
get global registry object
wayland::pointer_t::on_enter
std::function< void(uint32_t, surface_t, double, double)> & on_enter()
enter event
Definition: wayland-client-protocol.cpp:2875
wayland::shell_t
create desktop-style surfaces
Definition: wayland-client-protocol.hpp:1419
wayland::shell_surface_t::pong
void pong(uint32_t serial)
respond to a ping event
Definition: wayland-client-protocol.cpp:2476
wayland::pointer_t::set_cursor
void set_cursor(uint32_t serial, surface_t surface, int32_t hotspot_x, int32_t hotspot_y)
set the pointer surface
Definition: wayland-client-protocol.cpp:2861
wayland::callback_t::on_done
std::function< void(uint32_t)> & on_done()
done event
Definition: wayland-client-protocol.cpp:1641
wayland::xdg_surface_t
desktop user interface surface base interface
Definition: wayland-client-protocol-extra.hpp:959
wayland::buffer_t
content for a wl_surface
Definition: wayland-client-protocol.hpp:602
wayland::shm_t
shared memory support
Definition: wayland-client-protocol.hpp:391
wayland::surface_t::commit
void commit()
commit pending surface state
Definition: wayland-client-protocol.cpp:2649
wayland::keyboard_t::on_key
std::function< void(uint32_t, uint32_t, uint32_t, keyboard_key_state)> & on_key()
key event
Definition: wayland-client-protocol.cpp:3033
wayland::shell_surface_t::set_title
void set_title(std::string title)
set surface title
Definition: wayland-client-protocol.cpp:2516
wayland::surface_t::attach
void attach(buffer_t buffer, int32_t x, int32_t y)
set the surface contents
Definition: wayland-client-protocol.cpp:2623
wayland::surface_t
an onscreen surface
Definition: wayland-client-protocol.hpp:1898
wayland::seat_capability::pointer
static const detail::bitfield< 3, 12 > pointer
the seat has pointer devices
Definition: wayland-client-protocol.hpp:2485
wayland::xdg_surface_t::on_configure
std::function< void(uint32_t)> & on_configure()
suggest a surface change
Definition: wayland-client-protocol-extra.cpp:1161
wayland::surface_t::damage
void damage(int32_t x, int32_t y, int32_t width, int32_t height)
mark part of the surface damaged
Definition: wayland-client-protocol.cpp:2628
wayland::seat_capability
seat capability bitmask
Definition: wayland-client-protocol.hpp:2478
wayland::xdg_toplevel_t::on_close
std::function< void()> & on_close()
surface wants to be closed
Definition: wayland-client-protocol-extra.cpp:1300
wayland::shell_surface_t
desktop-style metadata interface
Definition: wayland-client-protocol.hpp:1485
wayland::xdg_toplevel_t::set_title
void set_title(std::string title)
set surface title
Definition: wayland-client-protocol-extra.cpp:1235
wayland::xdg_surface_t::get_toplevel
xdg_toplevel_t get_toplevel()
assign the xdg_toplevel surface role
Definition: wayland-client-protocol-extra.cpp:1139
wayland::shell_surface_t::move
void move(seat_t seat, uint32_t serial)
start an interactive move
Definition: wayland-client-protocol.cpp:2481
wayland::xdg_toplevel_t::move
void move(seat_t seat, uint32_t serial)
start an interactive move
Definition: wayland-client-protocol-extra.cpp:1250
wayland::xdg_toplevel_t
toplevel surface
Definition: wayland-client-protocol-extra.hpp:1143
wayland::registry_t
global registry object
Definition: wayland-client-protocol.hpp:130
wayland::registry_t::bind
proxy_t bind(uint32_t name, proxy_t &interface, uint32_t version)
bind an object to the display
Definition: wayland-client-protocol.cpp:1560
wayland::seat_t
group of input devices
Definition: wayland-client-protocol.hpp:2334
wayland::compositor_t::create_surface
surface_t create_surface()
create new surface
Definition: wayland-client-protocol.cpp:1707
wayland::shell_t::get_shell_surface
shell_surface_t get_shell_surface(surface_t surface)
create a shell surface from a surface
Definition: wayland-client-protocol.cpp:2415
wayland::display_t::roundtrip
int roundtrip()
Block until all pending request are processed by the server.
wayland::shell_surface_t::on_ping
std::function< void(uint32_t)> & on_ping()
ping client
Definition: wayland-client-protocol.cpp:2526
wayland::seat_t::get_pointer
pointer_t get_pointer()
return pointer object
Definition: wayland-client-protocol.cpp:2756
wayland::compositor_t
the compositor singleton
Definition: wayland-client-protocol.hpp:251
wayland::xdg_surface_t::ack_configure
void ack_configure(uint32_t serial)
ack a configure event
Definition: wayland-client-protocol-extra.cpp:1156
wayland::xdg_wm_base_t
create desktop-style surfaces
Definition: wayland-client-protocol-extra.hpp:564
wayland::xdg_wm_base_t::on_ping
std::function< void(uint32_t)> & on_ping()
check if the client is alive
Definition: wayland-client-protocol-extra.cpp:973
wayland::seat_t::get_keyboard
keyboard_t get_keyboard()
return keyboard object
Definition: wayland-client-protocol.cpp:2762
wayland::callback_t
callback object
Definition: wayland-client-protocol.hpp:210
wayland::shell_surface_t::set_toplevel
void set_toplevel()
make the surface a toplevel surface
Definition: wayland-client-protocol.cpp:2491
wayland::registry_t::on_global
std::function< void(uint32_t, std::string, uint32_t)> & on_global()
announce global object
Definition: wayland-client-protocol.cpp:1567
wayland::seat_t::on_capabilities
std::function< void(seat_capability)> & on_capabilities()
seat capabilities changed
Definition: wayland-client-protocol.cpp:2783
wayland::seat_capability::keyboard
static const detail::bitfield< 3, 12 > keyboard
the seat has one or more keyboards
Definition: wayland-client-protocol.hpp:2487
wayland::keyboard_t
keyboard input device
Definition: wayland-client-protocol.hpp:2896
wayland-client.hpp
wayland::pointer_t
pointer input device
Definition: wayland-client-protocol.hpp:2505