diff options
-rw-r--r-- | DOCS/man/en/vo.rst | 4 | ||||
-rw-r--r-- | Makefile | 1 | ||||
-rwxr-xr-x | configure | 38 | ||||
-rw-r--r-- | video/out/gl_common.c | 274 | ||||
-rw-r--r-- | video/out/gl_common.h | 1 | ||||
-rw-r--r-- | video/out/vo.h | 1 | ||||
-rw-r--r-- | video/out/vo_opengl.c | 1 | ||||
-rw-r--r-- | video/out/vo_opengl_old.c | 1 | ||||
-rw-r--r-- | video/out/wayland_common.c | 1036 | ||||
-rw-r--r-- | video/out/wayland_common.h | 153 |
10 files changed, 1506 insertions, 4 deletions
diff --git a/DOCS/man/en/vo.rst b/DOCS/man/en/vo.rst index 00a598e41b..07c08bb1f4 100644 --- a/DOCS/man/en/vo.rst +++ b/DOCS/man/en/vo.rst @@ -386,6 +386,8 @@ opengl Win32/WGL x11 X11/GLX + wayland + Wayland/EGL indirect Do YUV conversion and scaling as separate passes. This will @@ -643,6 +645,8 @@ opengl-old Win32/WGL x11 X11/GLX + waylnad + Wayland/EGL sdl SDL 2.0+ Render video output driver, depending on system with or without @@ -99,6 +99,7 @@ SOURCES-$(ENCODING) += video/out/vo_lavc.c audio/out/ao_lavc.c \ core/encode_lavc.c SOURCES-$(GL_WIN32) += video/out/w32_common.c SOURCES-$(GL_X11) += video/out/x11_common.c +SOURCES-$(GL_WAYLAND) += video/out/wayland_common.c SOURCES-$(JACK) += audio/out/ao_jack.c SOURCES-$(JOYSTICK) += core/input/joystick.c @@ -353,6 +353,7 @@ Video output: --enable-vm enable XF86VidMode support [autodetect] --enable-xinerama enable Xinerama support [autodetect] --enable-x11 enable X11 video output [autodetect] + --enable-wayland enable Wayland video output [autodetect] --disable-xss disable screensaver support via xss [autodetect] --disable-corevideo disable CoreVideo video output [autodetect] --disable-cocoa disable Cocoa OpenGL backend [autodetect] @@ -416,6 +417,7 @@ _prefix="/usr/local" ffmpeg=auto _encoding=yes _x11=auto +_wayland=auto _xss=auto _xv=auto _vdpau=auto @@ -576,6 +578,8 @@ for ac_option do --disable-cross-compile) _cross_compile=no ;; --enable-encoding) _encoding=yes ;; --disable-encoding) _encoding=no ;; + --enable-wayland) _wayland=yes ;; + --disable-wayland) _wayland=no ;; --enable-x11) _x11=yes ;; --disable-x11) _x11=no ;; --enable-xss) _xss=yes ;; @@ -1829,6 +1833,17 @@ depends_on_application_services(){ fi #if darwin +echocheck "Wayland" +if test "$_wayland" != no; then + _wayland="no" + pkg_config_add "wayland-client >= 1.0.0 wayland-egl >= 1.0.0 wayland-cursor >= 1.0.0 xkbcommon >= 0.2.0" \ + && _wayland="yes" + res_comment="" +else + _wayland="no" + res_comment="" +fi +echores "$_wayland" echocheck "X11 headers presence" _x11_headers="no" @@ -2105,20 +2120,23 @@ fi # conflicts between -lGL and -framework OpenGL echocheck "OpenGL" #Note: this test is run even with --enable-gl since we autodetect linker flags -if (test "$_x11" = yes || test "$_cocoa" = yes || win32) && test "$_gl" != no ; then +if (test "$_x11" = yes || test "$_wayland" = yes || test "$_cocoa" = yes || win32) && test "$_gl" != no ; then cat > $TMPC << EOF #ifdef GL_WIN32 #include <windows.h> -#include <GL/gl.h> +#elif defined(GL_WAYLAND) +#include <EGL/egl.h> #else -#include <GL/gl.h> #include <X11/Xlib.h> #include <GL/glx.h> #endif +#include <GL/gl.h> int main(int argc, char *argv[]) { #ifdef GL_WIN32 HDC dc; wglCreateContext(dc); +#elif defined(GL_WAYLAND) + eglCreateContext(NULL, NULL, EGL_NO_CONTEXT, NULL); #else glXCreateContext(NULL, NULL, NULL, True); #endif @@ -2137,6 +2155,11 @@ EOF fi done fi + if test "$_wayland" = yes && cc_check -DGL_WAYLAND -lGL -lEGL ; then + _gl=yes + _gl_wayland=yes + libs_mplayer="$libs_mplayer -lGL -lEGL" + fi if win32 && cc_check -DGL_WIN32 -lopengl32 ; then _gl=yes _gl_win32=yes @@ -2164,12 +2187,17 @@ if test "$_gl" = yes ; then def_gl_x11='#define CONFIG_GL_X11 1' res_comment="$res_comment x11" fi + if test "$_gl_wayland" = yes ; then + def_gl_wayland='#define CONFIG_GL_WAYLAND' + res_comment="$res_comment wayland" + fi vomodules="opengl $vomodules" else def_gl='#undef CONFIG_GL' def_gl_cocoa='#undef CONFIG_GL_COCOA' def_gl_win32='#undef CONFIG_GL_WIN32' def_gl_x11='#undef CONFIG_GL_X11' + def_gl_wayland='#undef CONFIG_GL_WAYLAND' novomodules="opengl $novomodules" fi echores "$_gl" @@ -3015,6 +3043,7 @@ GL = $_gl GL_COCOA = $_gl_cocoa GL_WIN32 = $_gl_win32 GL_X11 = $_gl_x11 +GL_WAYLAND = $_gl_wayland HAVE_POSIX_SELECT = $_posix_select HAVE_SYS_MMAN_H = $_mman JACK = $_jack @@ -3056,6 +3085,7 @@ VCD = $_vcd VDPAU = $_vdpau VSTREAM = $_vstream X11 = $_x11 +WAYLAND = $_wayland XV = $_xv # FFmpeg @@ -3251,12 +3281,14 @@ $def_gl $def_gl_cocoa $def_gl_win32 $def_gl_x11 +$def_gl_wayland $def_jpeg $def_mng $def_v4l2 $def_vdpau $def_vm $def_x11 +$def_wayland $def_xdpms $def_xf86keysym $def_xinerama diff --git a/video/out/gl_common.c b/video/out/gl_common.c index b72f9ae0d2..c416f20900 100644 --- a/video/out/gl_common.c +++ b/video/out/gl_common.c @@ -1302,6 +1302,259 @@ static void swapGlBuffers_x11(MPGLContext *ctx) } #endif +#ifdef CONFIG_GL_WAYLAND + +#include "wayland_common.h" +#include <wayland-egl.h> +#include <EGL/egl.h> +#include <EGL/eglext.h> +#include <assert.h> + +struct egl_context { + EGLSurface egl_surface; + + struct wl_egl_window *egl_window; + + struct { + EGLDisplay dpy; + EGLContext ctx; + EGLConfig conf; + } egl; +}; + +static void egl_resize_func(struct vo_wayland_state *wl, + struct egl_context *ctx) +{ + int32_t x, y, scaled_height; + double ratio; + int minimum_size = 50; + + if (wl->window->pending_width < minimum_size) + wl->window->pending_width = minimum_size; + if (wl->window->pending_height < minimum_size) + wl->window->pending_height = minimum_size; + + ratio = (double) wl->vo->aspdat.orgw / wl->vo->aspdat.orgh; + scaled_height = wl->window->pending_height * ratio; + if (wl->window->pending_width > scaled_height) { + wl->window->pending_height = wl->window->pending_width / ratio; + } else { + wl->window->pending_width = scaled_height; + } + + if (wl->window->edges & WL_SHELL_SURFACE_RESIZE_LEFT) + x = wl->window->width - wl->window->pending_width; + else + x = 0; + + if (wl->window->edges & WL_SHELL_SURFACE_RESIZE_TOP) + y = wl->window->height - wl->window->pending_height; + else + y = 0; + + wl_egl_window_resize(ctx->egl_window, + wl->window->pending_width, + wl->window->pending_height, + x, y); + + wl->window->width = wl->window->pending_width; + wl->window->height = wl->window->pending_height; + + /* set size for mplayer */ + wl->vo->dwidth = wl->window->pending_width; + wl->vo->dheight = wl->window->pending_height; + wl->window->events |= VO_EVENT_RESIZE; + wl->window->edges = 0; + wl->window->resize_needed = 0; +} + +static bool egl_create_context(struct vo_wayland_state *wl, + struct egl_context *egl_ctx, + MPGLContext *ctx) +{ + EGLint major, minor, n; + EGLBoolean ret; + + GL *gl = ctx->gl; + + void *(*getProcAddress)(const GLubyte *); + const char *(*eglExtStr)(EGLDisplay *, int); + const char *eglstr = ""; + + egl_ctx->egl.dpy = eglGetDisplay(wl->display->display); + if (!egl_ctx->egl.dpy) + return false; + + EGLint config_attribs[] = { + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, + EGL_RED_SIZE, 1, + EGL_GREEN_SIZE, 1, + EGL_BLUE_SIZE, 1, + EGL_ALPHA_SIZE, 0, + EGL_DEPTH_SIZE, 1, + EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, + EGL_NONE + }; + + /* major and minor here returns the supported EGL version (e.g.: 1.4) */ + ret = eglInitialize(egl_ctx->egl.dpy, &major, &minor); + if (ret != EGL_TRUE) + return false; + + EGLint context_attribs[] = { + EGL_CONTEXT_MAJOR_VERSION_KHR, + MPGL_VER_GET_MAJOR(ctx->requested_gl_version), + /* EGL_CONTEXT_MINOR_VERSION_KHR, */ + /* MPGL_VER_GET_MINOR(ctx->requested_gl_version), */ + /* EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR, 0, */ + /* Segfaults on anything else than the major version */ + EGL_NONE + }; + + ret = eglBindAPI(EGL_OPENGL_API); + assert(ret == EGL_TRUE); + + ret = eglChooseConfig(egl_ctx->egl.dpy, config_attribs, + &egl_ctx->egl.conf, 1, &n); + assert(ret && n == 1); + + egl_ctx->egl.ctx = eglCreateContext(egl_ctx->egl.dpy, + egl_ctx->egl.conf, + EGL_NO_CONTEXT, + context_attribs); + if (!egl_ctx->egl.ctx) + return false; + + ret = eglMakeCurrent(egl_ctx->egl.dpy, + NULL, + NULL, + egl_ctx->egl.ctx); + + assert(ret == EGL_TRUE); + + getProcAddress = getdladdr("eglGetProcAddress"); + if (!getProcAddress) + mp_msg(MSGT_VO, MSGL_WARN, "[egl] No eglGetProcAdress"); + + eglExtStr = getdladdr("eglQueryString"); + if (eglExtStr) + eglstr = eglExtStr(egl_ctx->egl.dpy, EGL_EXTENSIONS); + + getFunctions(gl, (void*(*)(const GLubyte*))eglGetProcAddress, eglstr); + if (!gl->BindProgram) + getFunctions(gl, NULL, eglstr); + + return true; +} + +static void egl_create_window(struct vo_wayland_state *wl, + struct egl_context *egl_ctx, + uint32_t width, + uint32_t height) +{ + EGLBoolean ret; + + wl->window->pending_width = width; + wl->window->pending_height = height; + wl->window->width = width; + wl->window->height = height; + + egl_ctx->egl_window = wl_egl_window_create(wl->window->surface, + wl->window->width, + wl->window->height); + + egl_ctx->egl_surface = eglCreateWindowSurface(egl_ctx->egl.dpy, + egl_ctx->egl.conf, + egl_ctx->egl_window, + NULL); + + ret = eglMakeCurrent(egl_ctx->egl.dpy, + egl_ctx->egl_surface, + egl_ctx->egl_surface, + egl_ctx->egl.ctx); + + assert(ret == EGL_TRUE); + + wl_display_dispatch_pending(wl->display->display); +} + +static bool config_window_wayland(struct MPGLContext *ctx, + uint32_t d_width, + uint32_t d_height, + uint32_t flags) +{ + struct egl_context * egl_ctx = ctx->priv; + struct vo_wayland_state * wl = ctx->vo->wayland; + bool ret = false; + + wl->window->pending_width = d_width; + wl->window->pending_height = d_height; + wl->window->width = d_width; + wl->window->height = d_height; + + vo_wayland_update_window_title(ctx->vo); + + if ((VOFLAG_FULLSCREEN & flags) && wl->window->type != TYPE_FULLSCREEN) + vo_wayland_fullscreen(ctx->vo); + + if (!egl_ctx->egl.ctx) { + /* Create OpenGL context */ + ret = egl_create_context(wl, egl_ctx, ctx); + + /* If successfully created the context and we don't want to hide the + * window than also create the window immediately */ + if (ret && !(VOFLAG_HIDDEN & flags)) + egl_create_window(wl, egl_ctx, d_width, d_height); + + return ret; + } + else { + /* If the window exists just resize it */ + if (egl_ctx->egl_window) + egl_resize_func(wl, egl_ctx); + + else { + /* If the context exists and the hidden flag is unset then + * create the window */ + if (!(VOFLAG_HIDDEN & flags)) + egl_create_window(wl, egl_ctx, d_width, d_height); + } + return true; + } +} + +static void releaseGlContext_wayland(MPGLContext *ctx) +{ + GL *gl = ctx->gl; + struct egl_context * egl_ctx = ctx->priv; + + gl->Finish(); + eglMakeCurrent(egl_ctx->egl.dpy, NULL, NULL, EGL_NO_CONTEXT); + eglDestroyContext(egl_ctx->egl.dpy, egl_ctx->egl.ctx); + eglTerminate(egl_ctx->egl.dpy); + eglReleaseThread(); + wl_egl_window_destroy(egl_ctx->egl_window); + egl_ctx->egl.ctx = NULL; +} + +static void swapGlBuffers_wayland(MPGLContext *ctx) +{ + struct egl_context * egl_ctx = ctx->priv; + struct vo_wayland_state *wl = ctx->vo->wayland; + + eglSwapBuffers(egl_ctx->egl.dpy, egl_ctx->egl_surface); + + /* resize window after the buffers have swapped + * makes resizing more fluid */ + if (wl->window->resize_needed) { + wl_egl_window_get_attached_size(egl_ctx->egl_window, + &wl->window->width, + &wl->window->height); + egl_resize_func(wl, egl_ctx); + } +} + +#endif struct backend { const char *name; @@ -1313,6 +1566,7 @@ static struct backend backends[] = { {"cocoa", GLTYPE_COCOA}, {"win", GLTYPE_W32}, {"x11", GLTYPE_X11}, + {"wayland", GLTYPE_WAYLAND}, {0} }; @@ -1335,7 +1589,10 @@ MPGLContext *mpgl_init(enum MPGLType type, struct vo *vo) ctx = mpgl_init(GLTYPE_W32, vo); if (ctx) return ctx; - return mpgl_init(GLTYPE_X11, vo); + ctx = mpgl_init(GLTYPE_X11, vo); + if (ctx) + return ctx; + return mpgl_init(GLTYPE_WAYLAND, vo); } ctx = talloc_zero(NULL, MPGLContext); *ctx = (MPGLContext) { @@ -1390,6 +1647,21 @@ MPGLContext *mpgl_init(enum MPGLType type, struct vo *vo) ctx->vo_uninit = vo_x11_uninit; break; #endif +#ifdef CONFIG_GL_WAYLAND + case GLTYPE_WAYLAND: + ctx->priv = talloc_zero(ctx, struct egl_context); + ctx->config_window = config_window_wayland; + ctx->releaseGlContext = releaseGlContext_wayland; + ctx->swapGlBuffers = swapGlBuffers_wayland; + ctx->update_xinerama_info = vo_wayland_update_screeninfo; + ctx->border = vo_wayland_border; + ctx->check_events = vo_wayland_check_events; + ctx->fullscreen = vo_wayland_fullscreen; + ctx->ontop = vo_wayland_ontop; + ctx->vo_init = vo_wayland_init; + ctx->vo_uninit = vo_wayland_uninit; + break; +#endif } if (ctx->vo_init && ctx->vo_init(vo)) return ctx; diff --git a/video/out/gl_common.h b/video/out/gl_common.h index cda99b9bff..d3e049cc28 100644 --- a/video/out/gl_common.h +++ b/video/out/gl_common.h @@ -77,6 +77,7 @@ enum MPGLType { GLTYPE_COCOA, GLTYPE_W32, GLTYPE_X11, + GLTYPE_WAYLAND, }; enum { diff --git a/video/out/vo.h b/video/out/vo.h index 50a30f82ea..81cd61b9cc 100644 --- a/video/out/vo.h +++ b/video/out/vo.h @@ -248,6 +248,7 @@ struct vo { struct vo_x11_state *x11; struct vo_w32_state *w32; struct vo_cocoa_state *cocoa; + struct vo_wayland_state *wayland; struct mp_fifo *key_fifo; struct encode_lavc_context *encode_lavc_ctx; struct input_ctx *input_ctx; diff --git a/video/out/vo_opengl.c b/video/out/vo_opengl.c index f2c78392ba..4a34d0f03a 100644 --- a/video/out/vo_opengl.c +++ b/video/out/vo_opengl.c @@ -2313,6 +2313,7 @@ static const char help_text[] = " cocoa: Cocoa/OSX\n" " win: Win32/WGL\n" " x11: X11/GLX\n" +" wayland: Wayland/EGL\n" " indirect\n" " Do YUV conversion and scaling as separate passes. This will\n" " first render the video into a video-sized RGB texture, and\n" diff --git a/video/out/vo_opengl_old.c b/video/out/vo_opengl_old.c index 3934bb3806..80175c516b 100644 --- a/video/out/vo_opengl_old.c +++ b/video/out/vo_opengl_old.c @@ -2243,6 +2243,7 @@ static int preinit(struct vo *vo, const char *arg) " cocoa: Cocoa/OSX\n" " win: Win32/WGL\n" " x11: X11/GLX\n" + " wayland: Wayland/EGL\n" "\n"); return -1; } diff --git a/video/out/wayland_common.c b/video/out/wayland_common.c new file mode 100644 index 0000000000..6904d3edb5 --- /dev/null +++ b/video/out/wayland_common.c @@ -0,0 +1,1036 @@ +/* + * This file is part of MPlayer. + * Copyright © 2008 Kristian Høgsberg + * Copyright © 2012-2013 Collabora, Ltd. + * Copyright © 2012-2013 Scott Moreau <oreaus@gmail.com> + * Copyright © 2012-2013 Alexander Preisinger <alexander.preisinger@gmail.com> + * + * MPlayer is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * MPlayer is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with MPlayer; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <math.h> +#include <inttypes.h> +#include <limits.h> +#include <assert.h> +#include <fcntl.h> +#include <unistd.h> + +#include <sys/mman.h> +#include <sys/epoll.h> +#include <sys/timerfd.h> +#include <linux/input.h> + +#include "config.h" +#include "core/bstr.h" +#include "core/options.h" +#include "core/mp_msg.h" +#include "core/mp_fifo.h" +#include "libavutil/common.h" +#include "talloc.h" + +#include "wayland_common.h" + +#include "vo.h" +#include "aspect.h" +#include "osdep/timer.h" + +#include "core/subopt-helper.h" + +#include "core/input/input.h" +#include "core/input/keycodes.h" + +#define MOD_SHIFT_MASK 0x01 +#define MOD_ALT_MASK 0x02 +#define MOD_CONTROL_MASK 0x04 + +#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) + +static int lookupkey(int key); + +static void hide_cursor(struct vo_wayland_display * display); +static void show_cursor(struct vo_wayland_display * display); + + +/*** wayland interface ***/ + +/* SHELL SURFACE LISTENER */ +static void ssurface_handle_ping(void *data, + struct wl_shell_surface *shell_surface, + uint32_t serial) +{ + wl_shell_surface_pong(shell_surface, serial); +} + +static void ssurface_schedule_resize(struct vo_wayland_window *window, + int32_t width, + int32_t height) +{ + window->pending_width = width; + window->pending_height = height; + window->resize_needed = 1; + window->events |= VO_EVENT_RESIZE; +} + +static void ssurface_handle_configure(void *data, + struct wl_shell_surface *shell_surface, + uint32_t edges, + int32_t width, + int32_t height) +{ + struct vo_wayland_state *wl = data; + + wl->window->edges = edges; + ssurface_schedule_resize(wl->window, width, height); +} + +static void ssurface_handle_popup_done(void *data, + struct wl_shell_surface *shell_surface) +{ +} + +const struct wl_shell_surface_listener shell_surface_listener = { + ssurface_handle_ping, + ssurface_handle_configure, + ssurface_handle_popup_done +}; + +/* OUTPUT LISTENER */ +static void output_handle_geometry(void *data, + struct wl_output *wl_output, + int32_t x, + int32_t y, + int32_t physical_width, + int32_t physical_height, + int32_t subpixel, + const char *make, + const char *model, + int32_t transform) +{ + /* Ignore transforms for now */ + switch (transform) { + case WL_OUTPUT_TRANSFORM_NORMAL: + case WL_OUTPUT_TRANSFORM_90: + case WL_OUTPUT_TRANSFORM_180: + case WL_OUTPUT_TRANSFORM_270: + case WL_OUTPUT_TRANSFORM_FLIPPED: + case WL_OUTPUT_TRANSFORM_FLIPPED_90: + case WL_OUTPUT_TRANSFORM_FLIPPED_180: + case WL_OUTPUT_TRANSFORM_FLIPPED_270: + default: + break; + } +} + +static void output_handle_mode(void *data, + struct wl_output *wl_output, + uint32_t flags, + int32_t width, + int32_t height, + int32_t refresh) +{ + struct vo_wayland_display *d = data; + struct vo_wayland_output *output; + + wl_list_for_each(output, &d->output_list, link) { + if (wl_output == output->output) { + output->width = width; + output->height = height; + if (flags) + output->flags = flags; + } + } + + /* one output is enough */ + d->output_mode_received = 1; +} + +const struct wl_output_listener output_listener = { + output_handle_geometry, + output_handle_mode +}; + +/* KEY LOOKUP */ +static const struct mp_keymap keymap[] = { + // special keys + {XKB_KEY_Pause, MP_KEY_PAUSE}, {XKB_KEY_Escape, MP_KEY_ESC}, + {XKB_KEY_BackSpace, MP_KEY_BS}, {XKB_KEY_Tab, MP_KEY_TAB}, + {XKB_KEY_Return, MP_KEY_ENTER}, {XKB_KEY_Menu, MP_KEY_MENU}, + {XKB_KEY_Print, MP_KEY_PRINT}, + + // cursor keys + {XKB_KEY_Left, MP_KEY_LEFT}, {XKB_KEY_Right, MP_KEY_RIGHT}, + {XKB_KEY_Up, MP_KEY_UP}, {XKB_KEY_Down, MP_KEY_DOWN}, + + // navigation block + {XKB_KEY_Insert, MP_KEY_INSERT}, {XKB_KEY_Delete, MP_KEY_DELETE}, + {XKB_KEY_Home, MP_KEY_HOME}, {XKB_KEY_End, MP_KEY_END}, + {XKB_KEY_Page_Up, MP_KEY_PAGE_UP}, {XKB_KEY_Page_Down, MP_KEY_PAGE_DOWN}, + + // F-keys + {XKB_KEY_F1, MP_KEY_F+1}, {XKB_KEY_F2, MP_KEY_F+2}, + {XKB_KEY_F3, MP_KEY_F+3}, {XKB_KEY_F4, MP_KEY_F+4}, + {XKB_KEY_F5, MP_KEY_F+5}, {XKB_KEY_F6, MP_KEY_F+6}, + {XKB_KEY_F7, MP_KEY_F+7}, {XKB_KEY_F8, MP_KEY_F+8}, + {XKB_KEY_F9, MP_KEY_F+9}, {XKB_KEY_F10, MP_KEY_F+10}, + {XKB_KEY_F11, MP_KEY_F+11}, {XKB_KEY_F12, MP_KEY_F+12}, + + // numpad independent of numlock + {XKB_KEY_KP_Subtract, '-'}, {XKB_KEY_KP_Add, '+'}, + {XKB_KEY_KP_Multiply, '*'}, {XKB_KEY_KP_Divide, '/'}, + {XKB_KEY_KP_Enter, MP_KEY_KPENTER}, + + // numpad with numlock + {XKB_KEY_KP_0, MP_KEY_KP0}, {XKB_KEY_KP_1, MP_KEY_KP1}, + {XKB_KEY_KP_2, MP_KEY_KP2}, {XKB_KEY_KP_3, MP_KEY_KP3}, + {XKB_KEY_KP_4, MP_KEY_KP4}, {XKB_KEY_KP_5, MP_KEY_KP5}, + {XKB_KEY_KP_6, MP_KEY_KP6}, {XKB_KEY_KP_7, MP_KEY_KP7}, + {XKB_KEY_KP_8, MP_KEY_KP8}, {XKB_KEY_KP_9, MP_KEY_KP9}, + {XKB_KEY_KP_Decimal, MP_KEY_KPDEC}, {XKB_KEY_KP_Separator, MP_KEY_KPDEC}, + + // numpad without numlock + {XKB_KEY_KP_Insert, MP_KEY_KPINS}, {XKB_KEY_KP_End, MP_KEY_KP1}, + {XKB_KEY_KP_Down, MP_KEY_KP2}, {XKB_KEY_KP_Page_Down, MP_KEY_KP3}, + {XKB_KEY_KP_Left, MP_KEY_KP4}, {XKB_KEY_KP_Begin, MP_KEY_KP5}, + {XKB_KEY_KP_Right, MP_KEY_KP6}, {XKB_KEY_KP_Home, MP_KEY_KP7}, + {XKB_KEY_KP_Up, MP_KEY_KP8}, {XKB_KEY_KP_Page_Up, MP_KEY_KP9}, + {XKB_KEY_KP_Delete, MP_KEY_KPDEL}, + + {0, 0} +}; + +/* KEYBOARD LISTENER */ +static void keyboard_handle_keymap(void *data, + struct wl_keyboard *wl_keyboard, + uint32_t format, + int32_t fd, + uint32_t size) +{ + struct vo_wayland_input *input = ((struct vo_wayland_state *) data)->input; + char *map_str; + + if(!data) { + close(fd); + return; + } + + if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { + close(fd); + return; + } + + map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + if (map_str == MAP_FAILED) { + close(fd); + return; + } + + input->xkb.keymap = xkb_map_new_from_string(input->xkb.context, + map_str, XKB_KEYMAP_FORMAT_TEXT_V1, 0); + + munmap(map_str, size); + close(fd); + + if (!input->xkb.keymap) { + mp_msg(MSGT_VO, MSGL_ERR, "[wayland] failed to compile keymap.\n"); + return; + } + + input->xkb.state = xkb_state_new(input->xkb.keymap); + if (!input->xkb.state) { + mp_msg(MSGT_VO, MSGL_ERR, "[wayland] failed to create XKB state.\n"); + xkb_map_unref(input->xkb.keymap); + input->xkb.keymap = NULL; + return; + } + + input->xkb.control_mask = + 1 << xkb_map_mod_get_index(input->xkb.keymap, "Control"); + input->xkb.alt_mask = + 1 << xkb_map_mod_get_index(input->xkb.keymap, "Mod1"); + input->xkb.shift_mask = + 1 << xkb_map_mod_get_index(input->xkb.keymap, "Shift"); +} + +static void keyboard_handle_enter(void *data, + struct wl_keyboard *wl_keyboard, + uint32_t serial, + struct wl_surface *surface, + struct wl_array *keys) +{ +} + +static void keyboard_handle_leave(void *data, + struct wl_keyboard *wl_keyboard, + uint32_t serial, + struct wl_surface *surface) +{ +} + +static void keyboard_handle_key(void *data, + struct wl_keyboard *wl_keyboard, + uint32_t serial, + uint32_t time, + uint32_t key, + uint32_t state) +{ + struct vo_wayland_state *wl = data; + struct vo_wayland_input *input = wl->input; + uint32_t code, num_syms; + + struct itimerspec its = {{0, 0}, {0, 0}}; + + const xkb_keysym_t *syms; + xkb_keysym_t sym; + xkb_mod_mask_t mask; + + code = key + 8; + num_syms = xkb_key_get_syms(input->xkb.state, code, &syms); + + mask = xkb_state_serialize_mods(input->xkb.state, + XKB_STATE_DEPRESSED | XKB_STATE_LATCHED); + + input->modifiers = 0; + if (mask & input->xkb.control_mask) + input->modifiers |= MOD_CONTROL_MASK; + if (mask & input->xkb.alt_mask) + input->modifiers |= MOD_ALT_MASK; + if (mask & input->xkb.shift_mask) + input->modifiers |= MOD_SHIFT_MASK; + + sym = XKB_KEY_NoSymbol; + if (num_syms == 1) + sym = syms[0]; + + if (sym != XKB_KEY_NoSymbol && state == WL_KEYBOARD_KEY_STATE_PRESSED) { + int mpkey = lookupkey(sym); + if (mpkey) + mplayer_put_key(wl->vo->key_fifo, mpkey); + input->events |= VO_EVENT_KEYPRESS; + } + + if (state == WL_KEYBOARD_KEY_STATE_RELEASED && key == input->repeat.key) { + input->repeat.sym = 0; + input->repeat.key = 0; + input->repeat.time = 0; + } + else if (state == WL_KEYBOARD_KEY_STATE_PRESSED) { + if (input->repeat.key == key) { + its.it_interval.tv_sec = 0; + its.it_interval.tv_nsec = 20 * 1000 * 1000; + its.it_value.tv_sec = 0; + its.it_value.tv_nsec = 100 * 1000 * 1000; + } + else { + input->repeat.sym = sym; + input->repeat.key = key; + input->repeat.time = time; + its.it_interval.tv_sec = 0; + its.it_interval.tv_nsec = 25 * 1000 * 1000; + its.it_value.tv_sec = 0; + its.it_value.tv_nsec = 400 * 1000 * 1000; + } + } + timerfd_settime(input->repeat.timer_fd, 0, &its, NULL); +} + +static void keyboard_handle_modifiers(void *data, + struct wl_keyboard *wl_keyboard, + uint32_t serial, + uint32_t mods_depressed, + uint32_t mods_latched, + uint32_t mods_locked, + uint32_t group) +{ + struct vo_wayland_input *input = ((struct vo_wayland_state *) data)->input; + + xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched, + mods_locked, 0, 0, group); +} + +const struct wl_keyboard_listener keyboard_listener = { + keyboard_handle_keymap, + keyboard_handle_enter, + keyboard_handle_leave, + keyboard_handle_key, + keyboard_handle_modifiers +}; + +/* POINTER LISTENER */ +static void pointer_handle_enter(void *data, + struct wl_pointer *pointer, + uint32_t serial, + struct wl_surface *surface, + wl_fixed_t sx_w, + wl_fixed_t sy_w) +{ + struct vo_wayland_state *wl = data; + struct vo_wayland_display * display = wl->display; + + display->cursor.serial = serial; + display->cursor.pointer = pointer; + + if (wl->window->type == TYPE_FULLSCREEN) + hide_cursor(display); + else if (display->cursor.default_cursor) { + show_cursor(display); + } +} + +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_w, + wl_fixed_t sy_w) +{ + struct vo_wayland_state *wl = data; + struct vo_wayland_display * display = wl->display; + + display->cursor.pointer = pointer; + + struct itimerspec its; + + if (wl->window->type == TYPE_FULLSCREEN) { + show_cursor(display); + + its.it_interval.tv_sec = 1; + its.it_interval.tv_nsec = 0; + its.it_value.tv_sec = 1; + its.it_value.tv_nsec = 0; + timerfd_settime(display->cursor.timer_fd, 0, &its, NULL); + } +} + +static void pointer_handle_button(void *data, + struct wl_pointer *pointer, + uint32_t serial, + uint32_t time, + uint32_t button, + uint32_t state) +{ + struct vo_wayland_state *wl = data; + + mplayer_put_key(wl->vo->key_fifo, MP_MOUSE_BTN0 + (button - BTN_LEFT) | + ((state == WL_POINTER_BUTTON_STATE_PRESSED) ? MP_KEY_STATE_DOWN : 0)); +} + +static void pointer_handle_axis(void *data, + struct wl_pointer *pointer, + uint32_t time, + uint32_t axis, + wl_fixed_t value) +{ + struct vo_wayland_state *wl = data; + + if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) { + if (value > 0) + mplayer_put_key(wl->vo->key_fifo, MP_MOUSE_BTN4 |