1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "window.hpp"
  2. static void shell_surface_ping (void *data, struct wl_shell_surface *shell_surface, uint32_t serial) {
  3. wl_shell_surface_pong (shell_surface, serial);
  4. }
  5. static void shell_surface_configure (void *data, struct wl_shell_surface *shell_surface, uint32_t edges, int32_t width, int32_t height) {
  6. Window* window = (Window*)data;
  7. wl_egl_window_resize (window->egl_window, width, height, 0, 0);
  8. window->camera = glm::ortho( 0.0f, (float)width, (float)height, 0.0f, -1.0f, 1.0f);
  9. window->width = width;
  10. window->height = height;
  11. glViewport(0,0,width,height);
  12. window->framebuffer->resize( width, height );
  13. }
  14. static void shell_surface_popup_done (void *data, struct wl_shell_surface *shell_surface) {
  15. }
  16. static struct wl_shell_surface_listener shell_surface_listener = {&shell_surface_ping, &shell_surface_configure, &shell_surface_popup_done};
  17. Window::Window( unsigned int w, unsigned int h ) {
  18. eglBindAPI (EGL_OPENGL_API);
  19. EGLint attributes[] = {
  20. EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  21. EGL_RED_SIZE, 8,
  22. EGL_GREEN_SIZE, 8,
  23. EGL_BLUE_SIZE, 8,
  24. EGL_ALPHA_SIZE, 8,
  25. EGL_NONE};
  26. camera = glm::ortho( 0.0f, (float)w, (float)h, 0.0f, -1.0f, 1.0f);
  27. EGLConfig config;
  28. EGLint num_config;
  29. eglChooseConfig (wayland->egl_display, attributes, &config, 1, &num_config);
  30. egl_context = eglCreateContext (wayland->egl_display, config, EGL_NO_CONTEXT, NULL);
  31. surface = wl_compositor_create_surface (wayland->compositor);
  32. shell_surface = wl_shell_get_shell_surface (wayland->shell, surface);
  33. wl_shell_surface_add_listener (shell_surface, &shell_surface_listener, this);
  34. wl_shell_surface_set_toplevel (shell_surface);
  35. egl_window = wl_egl_window_create (surface, w, h);
  36. egl_surface = eglCreateWindowSurface (wayland->egl_display, config, egl_window, NULL);
  37. if(ogl_LoadFunctions() == ogl_LOAD_FAILED)
  38. {
  39. throw new std::runtime_error("Failed to load function pointers for OpenGL.");
  40. }
  41. setCurrent();
  42. framebuffer = new Framebuffer( w, h );
  43. }
  44. Window::~Window() {
  45. delete framebuffer;
  46. eglDestroySurface (wayland->egl_display, egl_surface);
  47. wl_egl_window_destroy (egl_window);
  48. wl_surface_destroy (surface);
  49. wl_shell_surface_destroy (shell_surface);
  50. eglDestroyContext (wayland->egl_display, egl_context);
  51. }
  52. void Window::display() {
  53. if (!eglSwapBuffers(wayland->egl_display, egl_surface)) {
  54. throw new std::runtime_error("Failed to swap buffers.");
  55. }
  56. }
  57. void Window::setFullScreen() {
  58. wl_shell_surface_set_fullscreen(shell_surface, WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, 0, NULL);
  59. }
  60. void Window::setCurrent() {
  61. if (!eglMakeCurrent (wayland->egl_display, egl_surface, egl_surface, egl_context)) {
  62. throw new std::runtime_error("Failed set EGL's current surface for rendering.");
  63. }
  64. }
  65. void Window::setTitle(std::string title) {
  66. wl_shell_surface_set_title(shell_surface, title.c_str());
  67. }
  68. void Window::setClass(std::string title) {
  69. wl_shell_surface_set_class(shell_surface, title.c_str());
  70. }