#include #include #include #include #include #include #include #include #include struct display { struct wl_display *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_window *egl_window; 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_PIXMAP_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(display->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_window(struct display *display, struct window *window) { window->egl_window = wl_egl_window_create(window->surface, window->width, window->height); window->egl.surf = eglCreateWindowSurface(display->egl.dpy, display->egl.conf, window->egl_window, 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; static uint32_t last_time = 0; static int frames = 0; if (start_time == 0) start_time = time; if (last_time == 0) last_time = time; ++frames; if ((time - last_time) >= 5000) { printf("fps: %d\n", frames / 5); last_time = time; frames = 0; } glClearColor(0.0, 0.0, 0.0, 0.5); glClear(GL_COLOR_BUFFER_BIT); GLint foo; glGetIntegerv(GL_DRAW_BUFFER, &foo), 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); } 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 window *window = wl_surface_get_user_data(surface); int dx = 0; int dy = 0; int a_width, a_height; wl_egl_window_get_attached_size(window->egl_window, &a_width, &a_height); if (edges & WINDOW_RESIZING_LEFT) dx = a_width - width; if (edges & WINDOW_RESIZING_TOP) dy = a_height - height; wl_egl_window_resize(window->egl_window, width, height, dx, dy); window->width = width; window->height = 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, "wl_compositor") == 0) { d->interface.compositor = wl_display_bind(display, id, &wl_compositor_interface); } else if (strcmp(interface, "wl_shell") == 0) { d->interface.shell = wl_display_bind(display, id, &wl_shell_interface); 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 }; int fd; window.display = &display; window.width = 250; window.height = 250; display.display = wl_display_connect(NULL); assert(display.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_window(&display, &window); init_gl(&window); wl_shell_set_toplevel(display.interface.shell, window.surface); fd = wl_display_get_fd(display.display, event_mask_update, &display); while (true) { struct timeval tv; uint32_t msec; uint32_t mask; fd_set rfds; gettimeofday(&tv, NULL); redraw(&window, tv.tv_sec * 1000 + tv.tv_usec / 1000); FD_ZERO(&rfds); FD_SET(fd, &rfds); tv.tv_sec = 0; tv.tv_usec = 0; mask = display.mask; if (select(fd + 1, &rfds, NULL, NULL, &tv) != 1) mask &= ~WL_DISPLAY_READABLE; if (mask) wl_display_iterate(display.display, mask); } return 0; }