Wayland++  0.2.3
C++ Bindings for Wayland
egl.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 <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, NULL);
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 = 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  display.roundtrip();
207 
208  if(!has_keyboard)
209  throw std::runtime_error("No keyboard found.");
210  if(!has_pointer)
211  throw std::runtime_error("No pointer found.");
212 
213  // create a surface
214  surface = compositor.create_surface();
215 
216  // create a shell surface
217  if(xdg_wm_base)
218  {
219  xdg_wm_base.on_ping() = [&] (uint32_t serial) { xdg_wm_base.pong(serial); };
220  xdg_surface = xdg_wm_base.get_xdg_surface(surface);
221  xdg_toplevel = xdg_surface.get_toplevel();
222  xdg_toplevel.set_title("Window");
223  xdg_toplevel.on_close() = [&] () { running = false; };
224  }
225  else
226  {
227  shell_surface = shell.get_shell_surface(surface);
228  shell_surface.on_ping() = [&] (uint32_t serial) { shell_surface.pong(serial); };
229  shell_surface.set_title("Window");
230  shell_surface.set_toplevel();
231  }
232 
233  // Get input devices
234  pointer = seat.get_pointer();
235  keyboard = seat.get_keyboard();
236 
237  // load cursor theme
238  cursor_theme_t cursor_theme = cursor_theme_t("default", 16, shm);
239  cursor_t cursor = cursor_theme.get_cursor("cross");
240  cursor_image = cursor.image(0);
241  cursor_buffer = cursor_image.get_buffer();
242 
243  // create cursor surface
244  cursor_surface = compositor.create_surface();
245 
246  // draw cursor
247  pointer.on_enter() = [&] (uint32_t serial, surface_t, int32_t, int32_t)
248  {
249  cursor_surface.attach(cursor_buffer, 0, 0);
250  cursor_surface.damage(0, 0, cursor_image.width(), cursor_image.height());
251  cursor_surface.commit();
252  pointer.set_cursor(serial, cursor_surface, 0, 0);
253  };
254 
255  // window movement
256  pointer.on_button() = [&] (uint32_t serial, uint32_t time, uint32_t button, pointer_button_state state)
257  {
258  if(button == BTN_LEFT && state == pointer_button_state::pressed)
259  {
260  if(xdg_toplevel)
261  xdg_toplevel.move(seat, serial);
262  else
263  shell_surface.move(seat, serial);
264  }
265  };
266 
267  // press 'q' to exit
268  keyboard.on_key() = [&] (uint32_t, uint32_t, uint32_t key, keyboard_key_state state)
269  {
270  if(key == KEY_Q && state == keyboard_key_state::pressed)
271  running = false;
272  };
273 
274  // intitialize egl
275  egl_window = egl_window_t(surface, 320, 240);
276  init_egl();
277 
278  // draw stuff
279  draw();
280  }
281 
282  ~example() noexcept(false)
283  {
284  // finialize EGL
285  if(eglDestroyContext(egldisplay, eglcontext) == EGL_FALSE)
286  throw std::runtime_error("eglDestroyContext");
287  if(eglTerminate(egldisplay) == EGL_FALSE)
288  throw std::runtime_error("eglTerminate");
289  }
290 
291  void run()
292  {
293  // event loop
294  running = true;
295  while(running)
296  display.dispatch();
297  }
298 };
299 
300 int main()
301 {
302  example e;
303  e.run();
304  return 0;
305 }
create desktop-style surfaces
shared memory support
std::function< void(uint32_t)> & on_ping()
ping client
void pong(uint32_t serial)
respond to a ping event
std::function< void(uint32_t)> & on_done()
done event
void move(seat_t seat, uint32_t serial)
start an interactive move
content for a wl_surface
registry_t get_registry()
get global registry object
group of input devices
Represents a connection to the compositor and acts as a proxy to the display singleton object...
desktop user interface surface base interface
std::function< void(uint32_t, uint32_t, uint32_t, keyboard_key_state)> & on_key()
key event
std::function< void(uint32_t)> & on_ping()
check if the client is alive
proxy_t bind(uint32_t name, proxy_t &interface, uint32_t version)
bind an object to the display
void set_title(std::string title)
set surface title
Native EGL window.
Definition: wayland-egl.hpp:42
desktop-style metadata interface
std::function< void()> & on_close()
surface wants to be closed
shell_surface_t get_shell_surface(surface_t surface)
create a shell surface from a surface
void set_cursor(uint32_t serial, surface_t surface, int32_t hotspot_x, int32_t hotspot_y)
set the pointer surface
void set_title(std::string title)
set surface title
void commit()
commit pending surface state
void pong(uint32_t serial)
respond to a ping event
std::function< void(uint32_t, uint32_t, uint32_t, pointer_button_state)> & on_button()
pointer button event
std::function< void(uint32_t, std::string, uint32_t)> & on_global()
announce global object
pointer_t get_pointer()
return pointer object
global registry object
int dispatch()
Process incoming events.
the compositor singleton
static const detail::bitfield< 3, 12 > pointer
the seat has pointer devices
void move(seat_t seat, uint32_t serial)
start an interactive move
static const detail::bitfield< 3, 12 > keyboard
the seat has one or more keyboards
keyboard input device
void set_toplevel()
make the surface a toplevel surface
create desktop-style surfaces
void damage(int32_t x, int32_t y, int32_t width, int32_t height)
mark part of the surface damaged
surface_t create_surface()
create new surface
xdg_surface_t get_xdg_surface(surface_t surface)
create a shell surface from a surface
xdg_toplevel_t get_toplevel()
assign the xdg_toplevel surface role
void attach(buffer_t buffer, int32_t x, int32_t y)
set the surface contents
std::function< void(seat_capability)> & on_capabilities()
seat capabilities changed
int roundtrip()
Block until all pending request are processed by the server.
callback_t frame()
request a frame throttling hint
keyboard_t get_keyboard()
return keyboard object
std::function< void(uint32_t, surface_t, double, double)> & on_enter()
enter event