From 5ab6c7681ac947e657fca937e609cef95d14b42f Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Thu, 12 Jan 2017 21:46:22 +0100 Subject: [PATCH] [DEV] BASIC sample work corectly ... --- gale/context/wayland/Context.cpp | 971 ++++++++++++++++++++++++++++++- 1 file changed, 965 insertions(+), 6 deletions(-) diff --git a/gale/context/wayland/Context.cpp b/gale/context/wayland/Context.cpp index 41b0c66..03617ec 100644 --- a/gale/context/wayland/Context.cpp +++ b/gale/context/wayland/Context.cpp @@ -4,6 +4,7 @@ * @license MPL v2.0 (see license file) */ +#if 1 #include #include @@ -23,11 +24,11 @@ #include extern "C" { - #include #include #include #include #include + #include #include #include } @@ -52,16 +53,56 @@ bool hasDisplay = false; static void global_registry_handler(void* _data, struct wl_registry* _registry, uint32_t _id, const char* _interface, uint32_t _version); static void global_registry_remover(void* _data, struct wl_registry* _registry, uint32_t _id); - static const struct wl_registry_listener registry_listener = { global_registry_handler, global_registry_remover }; +static void seat_handle_capabilities(void* _data, struct wl_seat* _seat, uint32_t _caps); +static const struct wl_seat_listener seat_listener = { + seat_handle_capabilities, +}; + +static void pointer_handle_enter(void* _data, struct wl_pointer* _pointer, uint32_t _serial, struct wl_surface* _surface, wl_fixed_t _sx, wl_fixed_t _sy); +static void pointer_handle_leave(void* _data, struct wl_pointer* _pointer, uint32_t _serial, struct wl_surface* _surface); +static void pointer_handle_motion(void* _data, struct wl_pointer* _pointer, uint32_t _time, wl_fixed_t _sx, wl_fixed_t _sy); +static void pointer_handle_button(void* _data, struct wl_pointer* _wl_pointer, uint32_t _serial, uint32_t _time, uint32_t _button, uint32_t _state); +static void pointer_handle_axis(void* _data, struct wl_pointer* _wl_pointer, uint32_t _time, uint32_t _axis, wl_fixed_t _value); +static const struct wl_pointer_listener pointer_listener = { + pointer_handle_enter, + pointer_handle_leave, + pointer_handle_motion, + pointer_handle_button, + pointer_handle_axis, +}; + + +static void redraw(void* _data, struct wl_callback* _callback, uint32_t _time); +static const struct wl_callback_listener frame_listener = { + redraw +}; + +static void configure_callback(void* _data, struct wl_callback* _callback, uint32_t _time); +static struct wl_callback_listener configure_callback_listener = { + configure_callback, +}; + + +static void handle_ping(void* _data, struct wl_shell_surface* _shell_surface, uint32_t _serial); +static void handle_configure(void* _data, struct wl_shell_surface* _shell_surface, uint32_t _edges, int32_t _width, int32_t _height); +static void handle_popup_done(void* _data, struct wl_shell_surface* _shell_surface); +static const struct wl_shell_surface_listener shell_surface_listener = { + handle_ping, + handle_configure, + handle_popup_done +}; + + class WAYLANDInterface : public gale::Context { private: gale::key::Special m_guiKeyBoardMode; + ivec2 m_size; bool m_inputIsPressed[MAX_MANAGE_INPUT]; struct wl_display* m_display; std::string m_uniqueWindowsName; @@ -74,6 +115,14 @@ class WAYLANDInterface : public gale::Context { struct wl_region* m_region; struct wl_shell* m_shell; struct wl_shell_surface* m_shell_surface; + struct wl_seat* m_seat; + struct wl_pointer* m_pointer; + struct wl_callback* m_callback; + struct wl_keyboard* m_keyboard; + struct wl_shm* m_shm; + struct wl_cursor_theme* m_cursor_theme; + struct wl_cursor* m_default_cursor; + struct wl_surface* m_cursor_surface; EGLDisplay m_egl_display; EGLConfig m_egl_conf; @@ -83,6 +132,7 @@ class WAYLANDInterface : public gale::Context { public: WAYLANDInterface(gale::Application* _application, int32_t _argc, const char* _argv[]) : gale::Context(_application, _argc, _argv), + m_size(800,600), m_display(nullptr), m_run(false), m_compositor(nullptr), @@ -90,7 +140,15 @@ class WAYLANDInterface : public gale::Context { m_egl_window(nullptr), m_region(nullptr), m_shell(nullptr), - m_shell_surface(nullptr) { + m_shell_surface(nullptr), + m_seat(nullptr), + m_pointer(nullptr), + m_callback(nullptr), + m_keyboard(nullptr), + m_shm(nullptr), + m_cursor_theme(nullptr), + m_default_cursor(nullptr), + m_cursor_surface(nullptr) { WAYLAND_INFO("WAYLAND:INIT"); for (int32_t iii=0; iii Not used ..."); } } @@ -133,6 +210,98 @@ class WAYLANDInterface : public gale::Context { GALE_WARNING("Got a registry losing event for " << _id); } + /****************************************************************************************/ + // screen capabilities + void seatHandleCapabilities(struct wl_seat* _seat, enum wl_seat_capability _caps) { + if ((_caps & WL_SEAT_CAPABILITY_POINTER) && !m_pointer) { + GALE_WARNING("Display has a pointer"); + m_pointer = wl_seat_get_pointer(m_seat); + wl_pointer_add_listener(m_pointer, &pointer_listener, this); + } else if (!(_caps & WL_SEAT_CAPABILITY_POINTER) && m_pointer) { + GALE_WARNING("Display has No more pointer"); + wl_pointer_destroy(m_pointer); + m_pointer = nullptr; + } + + if (_caps & WL_SEAT_CAPABILITY_KEYBOARD) { + GALE_WARNING("Display has a keyboard"); + } + if (_caps & WL_SEAT_CAPABILITY_TOUCH) { + GALE_WARNING("Display has a touch screen"); + } + } + + /****************************************************************************************/ + // Pointer section event ... + + void pointerHandleEnter(struct wl_pointer* _pointer, uint32_t _serial, struct wl_surface* _surface, ivec2 _pos) { + GALE_WARNING("Pointer Enter surface" << _surface << " at pos=" << _pos); + /* + struct wl_buffer *buffer; + */ + + /*if (display->window->fullscreen) + wl_pointer_set_cursor(pointer, serial, NULL, 0, 0); + else */ + + if (m_default_cursor != nullptr) { + struct wl_cursor_image* image = m_default_cursor->images[0]; + //buffer = wl_cursor_image_get_buffer(image); + wl_pointer_set_cursor(_pointer, _serial, m_cursor_surface, image->hotspot_x, image->hotspot_y); + //wl_surface_attach(m_display->cursor_surface, buffer, 0, 0); + //wl_surface_damage(m_display->cursor_surface, 0, 0, image->width, image->height); + //wl_surface_commit(m_display->cursor_surface); + } + } + void pointerHandleLeave(struct wl_pointer* _pointer, uint32_t _serial, struct wl_surface* _surface) { + GALE_WARNING("Pointer left surface" << _surface); + } + void pointerHandleMotion(struct wl_pointer* _pointer, uint32_t _time, ivec2 _pos) { + GALE_WARNING("Pointer moved at " << _pos); + } + void pointerHandleButton(struct wl_pointer* _wl_pointer, uint32_t _serial, uint32_t _time, uint32_t _button, uint32_t _state) { + GALE_WARNING("Pointer button"); + } + void pointerHandleAxis(struct wl_pointer* _wl_pointer, uint32_t _time, uint32_t _axis, wl_fixed_t _value) { + GALE_WARNING("Pointer handle axis"); + } + /****************************************************************************************/ + + void redraw(struct wl_callback* _callback, uint32_t _time) { + GALE_WARNING("REDRAW"); + } + /* + void configure_callback(struct wl_callback* _callback, uint32_t _time) { + if (_callback == nullptr) { + //redraw(_data, this, _time); + + } + } + */ + + /****************************************************************************************/ + void handlePing(struct wl_shell_surface* _shell_surface, uint32_t _serial) { + wl_shell_surface_pong(_shell_surface, _serial); + GALE_WARNING("Ping ==> pong"); + } + + void handleConfigure(struct wl_shell_surface* _shell_surface, uint32_t _edges, ivec2 _size) { + GALE_WARNING("configure surface : _edges=" << _edges << " size=" << _size); + if (m_egl_window != nullptr) { + wl_egl_window_resize(m_egl_window, _size.x(), _size.y(), 0, 0); + } + m_size = _size; + /* + if (!window->fullscreen) { + window->window_size = window->geometry; + } + */ + } + + void handlePopupDone(struct wl_shell_surface* _shell_surface) { + GALE_WARNING("Pop-up done"); + } + /****************************************************************************************/ bool getServerReferences() { WAYLAND_INFO("WAYLAND: CreateWAYLANDContext"); @@ -174,7 +343,7 @@ class WAYLANDInterface : public gale::Context { void createOpaqueRegion() { m_region = wl_compositor_create_region(m_compositor); - wl_region_add(m_region, 0, 0, 480, 360); + wl_region_add(m_region, 0, 0, m_size.x(), m_size.y()); wl_surface_set_opaque_region(m_surface, m_region); } @@ -212,7 +381,7 @@ class WAYLANDInterface : public gale::Context { EGLint nnn = 0; eglChooseConfig(m_egl_display, configAttribs, configs, count, &nnn); GALE_INFO("Display all configs:"); - for (size_t iii=0; iiiregistryRemover(_registry, _id); } +static void seat_handle_capabilities(void* _data, struct wl_seat* _seat, uint32_t _caps) { + WAYLANDInterface* interface = (WAYLANDInterface*)_data; + if (interface == nullptr) { + return; + } + interface->seatHandleCapabilities(_seat, (enum wl_seat_capability)_caps); +} + +static void pointer_handle_enter(void* _data, struct wl_pointer* _pointer, uint32_t _serial, struct wl_surface* _surface, wl_fixed_t _sx, wl_fixed_t _sy) { + WAYLANDInterface* interface = (WAYLANDInterface*)_data; + if (interface == nullptr) { + return; + } + interface->pointerHandleEnter(_pointer, _serial, _surface, ivec2(_sx, _sy)); +} +static void pointer_handle_leave(void* _data, struct wl_pointer* _pointer, uint32_t _serial, struct wl_surface* _surface) { + WAYLANDInterface* interface = (WAYLANDInterface*)_data; + if (interface == nullptr) { + return; + } + interface->pointerHandleLeave(_pointer, _serial, _surface); +} + +static void pointer_handle_motion(void* _data, struct wl_pointer* _pointer, uint32_t _time, wl_fixed_t _sx, wl_fixed_t _sy) { + WAYLANDInterface* interface = (WAYLANDInterface*)_data; + if (interface == nullptr) { + return; + } + interface->pointerHandleMotion(_pointer, _time, ivec2(_sx, _sy)); +} + +static void pointer_handle_button(void* _data, struct wl_pointer* _wl_pointer, uint32_t _serial, uint32_t _time, uint32_t _button, uint32_t _state) { + WAYLANDInterface* interface = (WAYLANDInterface*)_data; + if (interface == nullptr) { + return; + } + interface->pointerHandleButton(_wl_pointer, _serial, _time, _button, _state); +} + +static void pointer_handle_axis(void* _data, struct wl_pointer* _wl_pointer, uint32_t _time, uint32_t _axis, wl_fixed_t _value) { + WAYLANDInterface* interface = (WAYLANDInterface*)_data; + if (interface == nullptr) { + return; + } + interface->pointerHandleAxis(_wl_pointer, _time, _axis, _value); +} + +static void redraw(void* _data, struct wl_callback* _callback, uint32_t _time) { + WAYLANDInterface* interface = (WAYLANDInterface*)_data; + if (interface == nullptr) { + return; + } + interface->redraw(_callback, _time); +} + +static void configure_callback(void* _data, struct wl_callback* _callback, uint32_t _time) { + if (_callback == nullptr) { + redraw(_data, nullptr, _time); + } +} + +static void handle_ping(void* _data, struct wl_shell_surface* _shell_surface, uint32_t _serial) { + WAYLANDInterface* interface = (WAYLANDInterface*)_data; + if (interface == nullptr) { + return; + } + interface->handlePing(_shell_surface, _serial); +} + +static void handle_configure(void* _data, struct wl_shell_surface* _shell_surface, uint32_t _edges, int32_t _width, int32_t _height) { + WAYLANDInterface* interface = (WAYLANDInterface*)_data; + if (interface == nullptr) { + return; + } + interface->handleConfigure(_shell_surface, _edges, ivec2(_width, _height)); +} + +static void handle_popup_done(void* _data, struct wl_shell_surface* _shell_surface) { + WAYLANDInterface* interface = (WAYLANDInterface*)_data; + if (interface == nullptr) { + return; + } + interface->handlePopupDone(_shell_surface); +} + /** * @brief Main of the program @@ -1413,3 +1667,708 @@ int gale::run(gale::Application* _application, int _argc, const char *_argv[]) { interface = nullptr; return retValue; } + +#else + +/* + * Copyright © 2011 Benjamin Franzke + * + * Permission to use, copy, modify, distribute, and sell this software and its + * documentation for any purpose is hereby granted without fee, provided that + * the above copyright notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting documentation, and + * that the name of the copyright holders not be used in advertising or + * publicity pertaining to distribution of the software without specific, + * written prior permission. The copyright holders make no representations + * about the suitability of this software for any purpose. It is provided "as + * is" without express or implied warranty. + * + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE + * OF THIS SOFTWARE. + */ +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include + +struct window; +struct seat; + +struct display { + struct wl_display *display; + struct wl_registry *registry; + struct wl_compositor *compositor; + struct wl_shell *shell; + struct wl_seat *seat; + struct wl_pointer *pointer; + struct wl_keyboard *keyboard; + struct wl_shm *shm; + struct wl_cursor_theme *cursor_theme; + struct wl_cursor *default_cursor; + struct wl_surface *cursor_surface; + struct { + EGLDisplay dpy; + EGLContext ctx; + EGLConfig conf; + } egl; + struct window *window; +}; + +struct geometry { + int width, height; +}; + +struct window { + struct display *display; + struct geometry geometry, window_size; + struct { + GLuint rotation_uniform; + GLuint pos; + GLuint col; + } gl; + + struct wl_egl_window *native; + struct wl_surface *surface; + struct wl_shell_surface *shell_surface; + EGLSurface egl_surface; + struct wl_callback *callback; + int fullscreen, configured, opaque; +}; + +static const char *vert_shader_text = + "uniform mat4 rotation;\n" + "attribute vec4 pos;\n" + "attribute vec4 color;\n" + "varying vec4 v_color;\n" + "void main() {\n" + " gl_Position = rotation * pos;\n" + " v_color = color;\n" + "}\n"; + +static const char *frag_shader_text = + "precision mediump float;\n" + "varying vec4 v_color;\n" + "void main() {\n" + " gl_FragColor = v_color;\n" + "}\n"; + +static int running = 1; + +static void +init_egl(struct display *display, int opaque) +{ + static const EGLint context_attribs[] = { + EGL_CONTEXT_CLIENT_VERSION, 2, + EGL_NONE + }; + + EGLint config_attribs[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_ALPHA_SIZE, 1, + EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, + EGL_NONE + }; + + EGLint major, minor, n; + EGLBoolean ret; + + if (opaque) + config_attribs[9] = 0; + + display->egl.dpy = eglGetDisplay(display->display); + assert(display->egl.dpy); + + ret = eglInitialize(display->egl.dpy, &major, &minor); + assert(ret == EGL_TRUE); + ret = eglBindAPI(EGL_OPENGL_ES_API); + assert(ret == EGL_TRUE); + + ret = eglChooseConfig(display->egl.dpy, config_attribs, + &display->egl.conf, 1, &n); + assert(ret && n == 1); + + display->egl.ctx = eglCreateContext(display->egl.dpy, + display->egl.conf, + EGL_NO_CONTEXT, context_attribs); + assert(display->egl.ctx); + +} + +static void +fini_egl(struct display *display) +{ + /* Required, otherwise segfault in egl_dri2.c: dri2_make_current() + * on eglReleaseThread(). */ + eglMakeCurrent(display->egl.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, + EGL_NO_CONTEXT); + + eglTerminate(display->egl.dpy); + eglReleaseThread(); +} + +static GLuint +create_shader(struct window *window, const char *source, GLenum shader_type) +{ + GLuint shader; + GLint status; + + shader = glCreateShader(shader_type); + assert(shader != 0); + + glShaderSource(shader, 1, (const char **) &source, NULL); + glCompileShader(shader); + + glGetShaderiv(shader, GL_COMPILE_STATUS, &status); + if (!status) { + char log[1000]; + GLsizei len; + glGetShaderInfoLog(shader, 1000, &len, log); + fprintf(stderr, "Error: compiling %s: %*s\n", + shader_type == GL_VERTEX_SHADER ? "vertex" : "fragment", + len, log); + exit(1); + } + + return shader; +} + +static void +init_gl(struct window *window) +{ + GLuint frag, vert; + GLuint program; + GLint status; + + frag = create_shader(window, frag_shader_text, GL_FRAGMENT_SHADER); + vert = create_shader(window, vert_shader_text, GL_VERTEX_SHADER); + + program = glCreateProgram(); + glAttachShader(program, frag); + glAttachShader(program, vert); + glLinkProgram(program); + + glGetProgramiv(program, GL_LINK_STATUS, &status); + if (!status) { + char log[1000]; + GLsizei len; + glGetProgramInfoLog(program, 1000, &len, log); + fprintf(stderr, "Error: linking:\n%*s\n", len, log); + exit(1); + } + + glUseProgram(program); + + window->gl.pos = 0; + window->gl.col = 1; + + glBindAttribLocation(program, window->gl.pos, "pos"); + glBindAttribLocation(program, window->gl.col, "color"); + glLinkProgram(program); + + window->gl.rotation_uniform = + glGetUniformLocation(program, "rotation"); +} + +static void +handle_ping(void *data, struct wl_shell_surface *shell_surface, + uint32_t serial) +{ + wl_shell_surface_pong(shell_surface, serial); +} + +static void +handle_configure(void *data, struct wl_shell_surface *shell_surface, + uint32_t edges, int32_t width, int32_t height) +{ + struct window *window = (struct window *)data; + + if (window->native) + wl_egl_window_resize(window->native, width, height, 0, 0); + + window->geometry.width = width; + window->geometry.height = height; + + if (!window->fullscreen) + window->window_size = window->geometry; +} + +static void +handle_popup_done(void *data, struct wl_shell_surface *shell_surface) +{ +} + +static const struct wl_shell_surface_listener shell_surface_listener = { + handle_ping, + handle_configure, + handle_popup_done +}; + +static void +redraw(void *data, struct wl_callback *callback, uint32_t time); + +static void +configure_callback(void *data, struct wl_callback *callback, uint32_t time) +{ + struct window *window = (struct window *)data; + + wl_callback_destroy(callback); + + window->configured = 1; + + if (window->callback == NULL) + redraw(data, NULL, time); +} + +static struct wl_callback_listener configure_callback_listener = { + configure_callback, +}; + +static void +toggle_fullscreen(struct window *window, int fullscreen) +{ + struct wl_callback *callback; + + window->fullscreen = fullscreen; + window->configured = 0; + + if (fullscreen) { + wl_shell_surface_set_fullscreen(window->shell_surface, + WL_SHELL_SURFACE_FULLSCREEN_METHOD_DEFAULT, + 0, NULL); + } else { + wl_shell_surface_set_toplevel(window->shell_surface); + handle_configure(window, window->shell_surface, 0, + window->window_size.width, + window->window_size.height); + } + + callback = wl_display_sync(window->display->display); + wl_callback_add_listener(callback, &configure_callback_listener, + window); +} + +static void +create_surface(struct window *window) +{ + struct display *display = window->display; + EGLBoolean ret; + + window->surface = wl_compositor_create_surface(display->compositor); + window->shell_surface = wl_shell_get_shell_surface(display->shell, + window->surface); + + wl_shell_surface_add_listener(window->shell_surface, + &shell_surface_listener, window); + + window->native = + wl_egl_window_create(window->surface, + window->window_size.width, + window->window_size.height); + window->egl_surface = + eglCreateWindowSurface(display->egl.dpy, + display->egl.conf, + window->native, NULL); + + wl_shell_surface_set_title(window->shell_surface, "simple-egl"); + + ret = eglMakeCurrent(window->display->egl.dpy, window->egl_surface, + window->egl_surface, window->display->egl.ctx); + assert(ret == EGL_TRUE); + + toggle_fullscreen(window, window->fullscreen); +} + +static void +destroy_surface(struct window *window) +{ + wl_egl_window_destroy(window->native); + + wl_shell_surface_destroy(window->shell_surface); + wl_surface_destroy(window->surface); + + if (window->callback) + wl_callback_destroy(window->callback); +} + +extern const struct wl_callback_listener frame_listener; + +static void +redraw(void *data, struct wl_callback *callback, uint32_t time) +{ + struct window *window = (struct window *)data; + static const GLfloat verts[3][2] = { + { -0.5, -0.5 }, + { 0.5, -0.5 }, + { 0, 0.5 } + }; + static const GLfloat colors[3][3] = { + { 1, 0, 0 }, + { 0, 1, 0 }, + { 0, 0, 1 } + }; + GLfloat angle; + GLfloat rotation[4][4] = { + { 1, 0, 0, 0 }, + { 0, 1, 0, 0 }, + { 0, 0, 1, 0 }, + { 0, 0, 0, 1 } + }; + static const int32_t speed_div = 5; + static uint32_t start_time = 0; + struct wl_region *region; + + assert(window->callback == callback); + window->callback = NULL; + + if (callback) + wl_callback_destroy(callback); + + if (!window->configured) + return; + + if (start_time == 0) + start_time = time; + + angle = ((time-start_time) / speed_div) % 360 * M_PI / 180.0; + rotation[0][0] = cos(angle); + rotation[0][2] = sin(angle); + rotation[2][0] = -sin(angle); + rotation[2][2] = cos(angle); + + glViewport(0, 0, window->geometry.width, window->geometry.height); + + glUniformMatrix4fv(window->gl.rotation_uniform, 1, GL_FALSE, + (GLfloat *) rotation); + + glClearColor(0.0, 0.0, 0.0, 0.5); + glClear(GL_COLOR_BUFFER_BIT); + + glVertexAttribPointer(window->gl.pos, 2, GL_FLOAT, GL_FALSE, 0, verts); + glVertexAttribPointer(window->gl.col, 3, GL_FLOAT, GL_FALSE, 0, colors); + glEnableVertexAttribArray(window->gl.pos); + glEnableVertexAttribArray(window->gl.col); + + glDrawArrays(GL_TRIANGLES, 0, 3); + + glDisableVertexAttribArray(window->gl.pos); + glDisableVertexAttribArray(window->gl.col); + + if (window->opaque || window->fullscreen) { + region = wl_compositor_create_region(window->display->compositor); + wl_region_add(region, 0, 0, + window->geometry.width, + window->geometry.height); + wl_surface_set_opaque_region(window->surface, region); + wl_region_destroy(region); + } else { + wl_surface_set_opaque_region(window->surface, NULL); + } + + window->callback = wl_surface_frame(window->surface); + wl_callback_add_listener(window->callback, &frame_listener, window); + + eglSwapBuffers(window->display->egl.dpy, window->egl_surface); +} + +const struct wl_callback_listener frame_listener = { + redraw +}; + +static void +pointer_handle_enter(void *data, struct wl_pointer *pointer, + uint32_t serial, struct wl_surface *surface, + wl_fixed_t sx, wl_fixed_t sy) +{ + struct display *display = (struct display *)data; + struct wl_buffer *buffer; + struct wl_cursor *cursor = display->default_cursor; + struct wl_cursor_image *image; + + if (display->window->fullscreen) + wl_pointer_set_cursor(pointer, serial, NULL, 0, 0); + else if (cursor) { + image = display->default_cursor->images[0]; + buffer = wl_cursor_image_get_buffer(image); + wl_pointer_set_cursor(pointer, serial, + display->cursor_surface, + image->hotspot_x, + image->hotspot_y); + wl_surface_attach(display->cursor_surface, buffer, 0, 0); + wl_surface_damage(display->cursor_surface, 0, 0, + image->width, image->height); + wl_surface_commit(display->cursor_surface); + } +} + +static void +pointer_handle_leave(void *data, struct wl_pointer *pointer, + uint32_t serial, struct wl_surface *surface) +{ +} + +static void +pointer_handle_motion(void *data, struct wl_pointer *pointer, + uint32_t time, wl_fixed_t sx, wl_fixed_t sy) +{ + GALE_INFO("motion: " << vec2(float(sx)/256.0f, float(sy)/256.0f)); +} + +static void +pointer_handle_button(void *data, struct wl_pointer *wl_pointer, + uint32_t serial, uint32_t time, uint32_t button, + uint32_t state) +{ + struct display *display = (struct display *)data; + + if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) + wl_shell_surface_move(display->window->shell_surface, + display->seat, serial); +} + +static void +pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, + uint32_t time, uint32_t axis, wl_fixed_t value) +{ + +} + +static const struct wl_pointer_listener pointer_listener = { + pointer_handle_enter, + pointer_handle_leave, + pointer_handle_motion, + pointer_handle_button, + pointer_handle_axis, +}; + +static void +keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard, + uint32_t format, int fd, uint32_t size) +{ + GALE_INFO("KEY MAP : '" << format << "'"); +} + +static void +keyboard_handle_enter(void *data, struct wl_keyboard *keyboard, + uint32_t serial, struct wl_surface *surface, + struct wl_array *keys) +{ +} + +static void +keyboard_handle_leave(void *data, struct wl_keyboard *keyboard, + uint32_t serial, struct wl_surface *surface) +{ +} + +static void +keyboard_handle_key(void *data, struct wl_keyboard *keyboard, + uint32_t serial, uint32_t time, uint32_t key, + uint32_t state) +{ + struct display *d = (struct display *)data; + + GALE_INFO("KEY : '" << key << "'"); + + if (key == KEY_F11 && state) + toggle_fullscreen(d->window, d->window->fullscreen ^ 1); + else if (key == KEY_ESC && state) + running = 0; +} + +static void +keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard, + uint32_t serial, uint32_t mods_depressed, + uint32_t mods_latched, uint32_t mods_locked, + uint32_t group) +{ +} + +static const struct wl_keyboard_listener keyboard_listener = { + keyboard_handle_keymap, + keyboard_handle_enter, + keyboard_handle_leave, + keyboard_handle_key, + keyboard_handle_modifiers, +}; + +static void +seat_handle_capabilities(void *data, struct wl_seat *seat, + uint32_t _caps) +{ + enum wl_seat_capability caps = (enum wl_seat_capability) _caps; + struct display *d = (struct display *)data; + + if ((caps & WL_SEAT_CAPABILITY_POINTER) && !d->pointer) { + d->pointer = wl_seat_get_pointer(seat); + wl_pointer_add_listener(d->pointer, &pointer_listener, d); + } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && d->pointer) { + wl_pointer_destroy(d->pointer); + d->pointer = NULL; + } + + if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) { + d->keyboard = wl_seat_get_keyboard(seat); + wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d); + } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) { + wl_keyboard_destroy(d->keyboard); + d->keyboard = NULL; + } +} + +static const struct wl_seat_listener seat_listener = { + seat_handle_capabilities, +}; + +static void +registry_handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) +{ + struct display *d = (struct display *)data; + + if (strcmp(interface, "wl_compositor") == 0) { + d->compositor = (struct wl_compositor *) + wl_registry_bind(registry, name, + &wl_compositor_interface, 1); + } else if (strcmp(interface, "wl_shell") == 0) { + d->shell = (struct wl_shell*)wl_registry_bind(registry, name, + &wl_shell_interface, 1); + } else if (strcmp(interface, "wl_seat") == 0) { + d->seat = (struct wl_seat*)wl_registry_bind(registry, name, + &wl_seat_interface, 1); + wl_seat_add_listener(d->seat, &seat_listener, d); + } else if (strcmp(interface, "wl_shm") == 0) { + d->shm = (struct wl_shm*)wl_registry_bind(registry, name, + &wl_shm_interface, 1); + d->cursor_theme = wl_cursor_theme_load(NULL, 32, d->shm); + d->default_cursor = + wl_cursor_theme_get_cursor(d->cursor_theme, "left_ptr"); + } +} + +static void +registry_handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) +{ +} + +static const struct wl_registry_listener registry_listener = { + registry_handle_global, + registry_handle_global_remove +}; + +static void +signal_int(int signum) +{ + running = 0; +} + +static void +usage(int error_code) +{ + fprintf(stderr, "Usage: simple-egl [OPTIONS]\n\n" + " -f\tRun in fullscreen mode\n" + " -o\tCreate an opaque surface\n" + " -h\tThis help text\n\n"); + + exit(error_code); +} + + +int gale::run(gale::Application* _application, int _argc, const char *_argv[]) { + etk::init(_argc, _argv); + struct sigaction sigint; + struct display display = { 0 }; + struct window window = { 0 }; + int i, ret = 0; + + window.display = &display; + display.window = &window; + window.window_size.width = 250; + window.window_size.height = 250; + + for (i = 1; i < _argc; i++) { + if (strcmp("-f", _argv[i]) == 0) + window.fullscreen = 1; + else if (strcmp("-o", _argv[i]) == 0) + window.opaque = 1; + else if (strcmp("-h", _argv[i]) == 0) + usage(EXIT_SUCCESS); + else + usage(EXIT_FAILURE); + } + + display.display = wl_display_connect(NULL); + assert(display.display); + + display.registry = wl_display_get_registry(display.display); + wl_registry_add_listener(display.registry, + ®istry_listener, &display); + + wl_display_dispatch(display.display); + + init_egl(&display, window.opaque); + create_surface(&window); + init_gl(&window); + + display.cursor_surface = + wl_compositor_create_surface(display.compositor); + + sigint.sa_handler = signal_int; + sigemptyset(&sigint.sa_mask); + sigint.sa_flags = SA_RESETHAND; + sigaction(SIGINT, &sigint, NULL); + + while (running && ret != -1) + ret = wl_display_dispatch(display.display); + + fprintf(stderr, "simple-egl exiting\n"); + + destroy_surface(&window); + fini_egl(&display); + + wl_surface_destroy(display.cursor_surface); + if (display.cursor_theme) + wl_cursor_theme_destroy(display.cursor_theme); + + if (display.shell) + wl_shell_destroy(display.shell); + + if (display.compositor) + wl_compositor_destroy(display.compositor); + + wl_registry_destroy(display.registry); + wl_display_flush(display.display); + wl_display_disconnect(display.display); + + return 0; +} + + + + +#endif +