diff options
Diffstat (limited to 'egl-wayland-surface.c')
-rw-r--r-- | egl-wayland-surface.c | 265 |
1 files changed, 265 insertions, 0 deletions
diff --git a/egl-wayland-surface.c b/egl-wayland-surface.c new file mode 100644 index 0000000..8ea2002 --- /dev/null +++ b/egl-wayland-surface.c @@ -0,0 +1,265 @@ +#include <stdio.h> +#include <string.h> +#include <stdbool.h> +#include <assert.h> + +#include <GL/gl.h> +#include <EGL/egl.h> + +#include <wayland-client.h> +#include <wayland-egl.h> + +struct display { + struct wl_display *display; + struct wl_egl_display *egl_display; + struct { + struct wl_compositor *compositor; + struct wl_shell *shell; + } interface; + + struct { + EGLDisplay dpy; + EGLConfig conf; + EGLContext ctx; + } egl; + + uint32_t mask; +}; + +struct window { + struct display *display; + struct wl_surface *surface; + struct wl_egl_surface *egl_surface; + + int width, height; + + struct { + EGLSurface surf; + } egl; +}; + +static void +init_egl(struct display *display) +{ + EGLint major, minor; + EGLint num_config; + EGLint config_attribs[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + //EGL_DEPTH_SIZE, 1, + EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, + EGL_NONE + }; + + display->egl.dpy = eglGetDisplay((EGLNativeDisplayType) display->egl_display); + assert(display->egl.dpy); + + assert(eglInitialize(display->egl.dpy, &major, &minor)); + + assert(eglBindAPI(EGL_OPENGL_API)); + assert(eglChooseConfig(display->egl.dpy, config_attribs, + &display->egl.conf, 1, &num_config)); + assert(num_config == 1); + + display->egl.ctx = eglCreateContext(display->egl.dpy, display->egl.conf, + EGL_NO_CONTEXT, NULL); + assert(display->egl.ctx); +} + +static void +create_egl_surface(struct display *display, struct window *window) +{ + + window->egl_surface = wl_egl_create_native_surface(window->surface, + window->width, + window->height); + + printf("surface: %p\n", window->surface); + window->egl.surf = + eglCreateWindowSurface(display->egl.dpy, display->egl.conf, + (EGLNativeWindowType) window->egl_surface, + NULL); + assert(window->egl.surf); + assert(eglMakeCurrent(display->egl.dpy, + window->egl.surf, window->egl.surf, + display->egl.ctx)); +} + +static void +init_gl(struct window *window) +{ + GLfloat ar; + + glViewport(0, 0, window->width, window->height); + ar = (GLfloat)window->width / (GLfloat)window->height; + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glFrustum(-ar, ar, -1, 1, 5.0, 60.0); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -10.0); +} + +static void +redraw(void *data, uint32_t time) +{ + struct window *window = data; + static const GLfloat verts[3][2] = { + { -1, -1 }, + { 1, -1 }, + { 0, 1 } + }; + static const GLfloat colors[3][3] = { + { 1, 0, 1 }, + { 0, 1, 0 }, + { 0, 0, 1 } + }; + static GLfloat speed = 0.2; + static uint32_t start_time = 0; + + if (start_time == 0) + start_time = time; + + glClearColor(0.0, 0.0, 0.0, 0.5); + glClear(GL_COLOR_BUFFER_BIT); + + glPushMatrix(); + glRotatef(speed * (time-start_time), 0, 1, 0); + + glVertexPointer(2, GL_FLOAT, 0, verts); + glColorPointer(3, GL_FLOAT, 0, colors); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_COLOR_ARRAY); + + glDrawArrays(GL_TRIANGLES, 0, 3); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_COLOR_ARRAY); + + glPopMatrix(); + + eglSwapBuffers(window->display->egl.dpy, window->egl.surf); + + wl_display_frame_callback(window->display->display, redraw, window); +} + +enum window_location { + WINDOW_INTERIOR = 0, + WINDOW_RESIZING_TOP = 1, + WINDOW_RESIZING_BOTTOM = 2, + WINDOW_RESIZING_LEFT = 4, + WINDOW_RESIZING_TOP_LEFT = 5, + WINDOW_RESIZING_BOTTOM_LEFT = 6, + WINDOW_RESIZING_RIGHT = 8, + WINDOW_RESIZING_TOP_RIGHT = 9, + WINDOW_RESIZING_BOTTOM_RIGHT = 10, + WINDOW_RESIZING_MASK = 15, + WINDOW_EXTERIOR = 16, + WINDOW_TITLEBAR = 17, + WINDOW_CLIENT_AREA = 18, +}; +static void +handle_configure(void *data, struct wl_shell *shell, + uint32_t time, uint32_t edges, + struct wl_surface *surface, + int32_t width, int32_t height) +{ + struct display *d = data; + struct window *window = wl_surface_get_user_data(surface); + int dx = 0; + int dy = 0; + + int t_width, t_height; + + eglQuerySurface(d->egl.dpy, window->egl.surf, EGL_WIDTH, &t_width); + eglQuerySurface(d->egl.dpy, window->egl.surf, EGL_HEIGHT, &t_height); + + edges = 5; + if (edges & WINDOW_RESIZING_LEFT) + dx = t_width - width; + + if (edges & WINDOW_RESIZING_TOP) + dy = t_height - height; + + t_width = width + 2*dx; + t_height = height + 2*dy; + + wl_egl_surface_resize(window->egl_surface, t_width, t_height, -dx, -dy); + + window->width = t_width; + window->height = t_height; + + glViewport(0, 0, window->width, window->height); +} + +static const struct wl_shell_listener wayland_shell_listener = { + handle_configure, +}; + +static void +display_handle_global(struct wl_display *display, uint32_t id, + const char *interface, uint32_t version, void *data) +{ + struct display *d = data; + + if (strcmp(interface, "compositor") == 0) { + d->interface.compositor = wl_compositor_create(display, id); + } else if (strcmp(interface, "shell") == 0) { + d->interface.shell = wl_shell_create(display, id); + wl_shell_add_listener(d->interface.shell, &wayland_shell_listener, d); + } +} + +static int +event_mask_update(uint32_t mask, void *data) +{ + struct display *d = data; + + d->mask = mask; + + return 0; +} + +int +main(int argc, char **argv) +{ + struct display display = { 0 }; + struct window window = { 0 }; + + window.display = &display; + window.width = 250; + window.height = 250; + + display.display = wl_display_connect(NULL); + assert(display.display); + + display.egl_display = wl_egl_create_native_display(display.display); + assert(display.egl_display); + + wl_display_add_global_listener(display.display, + display_handle_global, &display); + /* process connection events */ + wl_display_iterate(display.display, WL_DISPLAY_READABLE); + + init_egl(&display); + window.surface = + wl_compositor_create_surface(display.interface.compositor); + wl_surface_set_user_data(window.surface, &window); + create_egl_surface(&display, &window); + + init_gl(&window); + + wl_surface_map_toplevel(window.surface); + + wl_display_frame_callback(display.display, redraw, &window); + + wl_display_get_fd(display.display, event_mask_update, &display); + while (true) + wl_display_iterate(display.display, display.mask); + + return 0; +} |