src/video/kmsdrm/SDL_kmsdrmmouse.c
changeset 11175 cbc6a8a5b701
child 11342 0b924e560249
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/kmsdrm/SDL_kmsdrmmouse.c	Wed Aug 02 10:22:48 2017 -0700
     1.3 @@ -0,0 +1,426 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "../../SDL_internal.h"
    1.25 +
    1.26 +#if SDL_VIDEO_DRIVER_KMSDRM
    1.27 +
    1.28 +#include "SDL_assert.h"
    1.29 +#include "SDL_surface.h"
    1.30 +
    1.31 +#include "SDL_kmsdrmvideo.h"
    1.32 +#include "SDL_kmsdrmmouse.h"
    1.33 +#include "SDL_kmsdrmdyn.h"
    1.34 +
    1.35 +#include "../SDL_sysvideo.h"
    1.36 +#include "../../events/SDL_mouse_c.h"
    1.37 +#include "../../events/default_cursor.h"
    1.38 +
    1.39 +
    1.40 +static SDL_Cursor *KMSDRM_CreateDefaultCursor(void);
    1.41 +static SDL_Cursor *KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y);
    1.42 +static int KMSDRM_ShowCursor(SDL_Cursor * cursor);
    1.43 +static void KMSDRM_MoveCursor(SDL_Cursor * cursor);
    1.44 +static void KMSDRM_FreeCursor(SDL_Cursor * cursor);
    1.45 +static void KMSDRM_WarpMouse(SDL_Window * window, int x, int y);
    1.46 +static int KMSDRM_WarpMouseGlobal(int x, int y);
    1.47 +
    1.48 +static SDL_Cursor *
    1.49 +KMSDRM_CreateDefaultCursor(void)
    1.50 +{
    1.51 +    return SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH, DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY);
    1.52 +}
    1.53 +
    1.54 +/* Create a cursor from a surface */
    1.55 +static SDL_Cursor *
    1.56 +KMSDRM_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
    1.57 +{
    1.58 +    SDL_VideoDevice *dev = SDL_GetVideoDevice();
    1.59 +    SDL_VideoData *vdata = ((SDL_VideoData *)dev->driverdata);
    1.60 +    SDL_PixelFormat *pixlfmt = surface->format;
    1.61 +    KMSDRM_CursorData *curdata;
    1.62 +    SDL_Cursor *cursor;
    1.63 +    int i, ret;
    1.64 +    uint32_t bo_format, bo_stride;
    1.65 +    char *buffer = NULL;
    1.66 +    size_t bufsize;
    1.67 +
    1.68 +    switch(pixlfmt->format) {
    1.69 +    case SDL_PIXELFORMAT_RGB332:
    1.70 +        bo_format = GBM_FORMAT_RGB332;
    1.71 +        break;
    1.72 +    case SDL_PIXELFORMAT_ARGB4444:
    1.73 +        bo_format = GBM_FORMAT_ARGB4444;
    1.74 +        break;
    1.75 +    case SDL_PIXELFORMAT_RGBA4444:
    1.76 +        bo_format = GBM_FORMAT_RGBA4444;
    1.77 +        break;
    1.78 +    case SDL_PIXELFORMAT_ABGR4444:
    1.79 +        bo_format = GBM_FORMAT_ABGR4444;
    1.80 +        break;
    1.81 +    case SDL_PIXELFORMAT_BGRA4444:
    1.82 +        bo_format = GBM_FORMAT_BGRA4444;
    1.83 +        break;
    1.84 +    case SDL_PIXELFORMAT_ARGB1555:
    1.85 +        bo_format = GBM_FORMAT_ARGB1555;
    1.86 +        break;
    1.87 +    case SDL_PIXELFORMAT_RGBA5551:
    1.88 +        bo_format = GBM_FORMAT_RGBA5551;
    1.89 +        break;
    1.90 +    case SDL_PIXELFORMAT_ABGR1555:
    1.91 +        bo_format = GBM_FORMAT_ABGR1555;
    1.92 +        break;
    1.93 +    case SDL_PIXELFORMAT_BGRA5551:
    1.94 +        bo_format = GBM_FORMAT_BGRA5551;
    1.95 +        break;
    1.96 +    case SDL_PIXELFORMAT_RGB565:
    1.97 +        bo_format = GBM_FORMAT_RGB565;
    1.98 +        break;
    1.99 +    case SDL_PIXELFORMAT_BGR565:
   1.100 +        bo_format = GBM_FORMAT_BGR565;
   1.101 +        break;
   1.102 +    case SDL_PIXELFORMAT_RGB888:
   1.103 +    case SDL_PIXELFORMAT_RGB24:
   1.104 +        bo_format = GBM_FORMAT_RGB888;
   1.105 +        break;
   1.106 +    case SDL_PIXELFORMAT_BGR888:
   1.107 +    case SDL_PIXELFORMAT_BGR24:
   1.108 +        bo_format = GBM_FORMAT_BGR888;
   1.109 +        break;
   1.110 +    case SDL_PIXELFORMAT_RGBX8888:
   1.111 +        bo_format = GBM_FORMAT_RGBX8888;
   1.112 +        break;
   1.113 +    case SDL_PIXELFORMAT_BGRX8888:
   1.114 +        bo_format = GBM_FORMAT_BGRX8888;
   1.115 +        break;
   1.116 +    case SDL_PIXELFORMAT_ARGB8888:
   1.117 +        bo_format = GBM_FORMAT_ARGB8888;
   1.118 +        break;
   1.119 +    case SDL_PIXELFORMAT_RGBA8888:
   1.120 +        bo_format = GBM_FORMAT_RGBA8888;
   1.121 +        break;
   1.122 +    case SDL_PIXELFORMAT_ABGR8888:
   1.123 +        bo_format = GBM_FORMAT_ABGR8888;
   1.124 +        break;
   1.125 +    case SDL_PIXELFORMAT_BGRA8888:
   1.126 +        bo_format = GBM_FORMAT_BGRA8888;
   1.127 +        break;
   1.128 +    case SDL_PIXELFORMAT_ARGB2101010:
   1.129 +        bo_format = GBM_FORMAT_ARGB2101010;
   1.130 +        break;
   1.131 +    default:
   1.132 +        SDL_SetError("Unsupported pixel format for cursor");
   1.133 +        return NULL;
   1.134 +    }
   1.135 +
   1.136 +    if (!KMSDRM_gbm_device_is_format_supported(vdata->gbm, bo_format, GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE)) {
   1.137 +        SDL_SetError("Unsupported pixel format for cursor");
   1.138 +        return NULL;
   1.139 +    }
   1.140 +
   1.141 +    cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
   1.142 +    if (cursor == NULL) {
   1.143 +        SDL_OutOfMemory();
   1.144 +        return NULL;
   1.145 +    }
   1.146 +    curdata = (KMSDRM_CursorData *) SDL_calloc(1, sizeof(*curdata));
   1.147 +    if (curdata == NULL) {
   1.148 +        SDL_OutOfMemory();
   1.149 +        SDL_free(cursor);
   1.150 +        return NULL;
   1.151 +    }
   1.152 +
   1.153 +    curdata->hot_x = hot_x;
   1.154 +    curdata->hot_y = hot_y;
   1.155 +    curdata->w = surface->w;
   1.156 +    curdata->h = surface->h;
   1.157 +
   1.158 +    curdata->bo = KMSDRM_gbm_bo_create(vdata->gbm, surface->w, surface->h, bo_format,
   1.159 +                                       GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
   1.160 +    if (curdata->bo == NULL) {
   1.161 +        SDL_SetError("Could not create GBM cursor BO");
   1.162 +        goto cleanup;
   1.163 +    }
   1.164 +
   1.165 +    bo_stride = KMSDRM_gbm_bo_get_stride(curdata->bo);
   1.166 +    bufsize = bo_stride * surface->h;
   1.167 +
   1.168 +    if (surface->pitch != bo_stride) {
   1.169 +        /* pitch doesn't match stride, must be copied to temp buffer  */
   1.170 +        buffer = SDL_malloc(bufsize);
   1.171 +        if (buffer == NULL) {
   1.172 +            SDL_OutOfMemory();
   1.173 +            goto cleanup;
   1.174 +        }
   1.175 +
   1.176 +        if (SDL_MUSTLOCK(surface)) {
   1.177 +            if (SDL_LockSurface(surface) < 0) {
   1.178 +                /* Could not lock surface */
   1.179 +                goto cleanup;
   1.180 +            }
   1.181 +        }
   1.182 +
   1.183 +        /* Copy to temporary buffer */
   1.184 +        for (i = 0; i < surface->h; i++) {
   1.185 +            SDL_memcpy(buffer + (i * bo_stride),
   1.186 +                       ((char *)surface->pixels) + (i * surface->pitch),
   1.187 +                       surface->w * pixlfmt->BytesPerPixel);
   1.188 +        }
   1.189 +
   1.190 +        if (SDL_MUSTLOCK(surface)) {
   1.191 +            SDL_UnlockSurface(surface);
   1.192 +        }
   1.193 +
   1.194 +        if (KMSDRM_gbm_bo_write(curdata->bo, buffer, bufsize)) {
   1.195 +            SDL_SetError("Could not write to GBM cursor BO");
   1.196 +            goto cleanup;
   1.197 +        }
   1.198 +
   1.199 +        /* Free temporary buffer */
   1.200 +        SDL_free(buffer);
   1.201 +        buffer = NULL;
   1.202 +    } else {
   1.203 +        /* surface matches BO format */
   1.204 +        if (SDL_MUSTLOCK(surface)) {
   1.205 +            if (SDL_LockSurface(surface) < 0) {
   1.206 +                /* Could not lock surface */
   1.207 +                goto cleanup;
   1.208 +            }
   1.209 +        }
   1.210 +
   1.211 +        ret = KMSDRM_gbm_bo_write(curdata->bo, surface->pixels, bufsize);
   1.212 +
   1.213 +        if (SDL_MUSTLOCK(surface)) {
   1.214 +            SDL_UnlockSurface(surface);
   1.215 +        }
   1.216 +
   1.217 +        if (ret) {
   1.218 +            SDL_SetError("Could not write to GBM cursor BO");
   1.219 +            goto cleanup;
   1.220 +        }
   1.221 +    }
   1.222 +
   1.223 +    cursor->driverdata = curdata;
   1.224 +
   1.225 +    return cursor;
   1.226 +
   1.227 +cleanup:
   1.228 +    if (buffer != NULL) {
   1.229 +        SDL_free(buffer);
   1.230 +    }
   1.231 +    if (cursor != NULL) {
   1.232 +        SDL_free(cursor);
   1.233 +    }
   1.234 +    if (curdata != NULL) {
   1.235 +        if (curdata->bo != NULL) {
   1.236 +            KMSDRM_gbm_bo_destroy(curdata->bo);
   1.237 +        }
   1.238 +        SDL_free(curdata);
   1.239 +    }
   1.240 +    return NULL;
   1.241 +}
   1.242 +
   1.243 +/* Show the specified cursor, or hide if cursor is NULL */
   1.244 +static int
   1.245 +KMSDRM_ShowCursor(SDL_Cursor * cursor)
   1.246 +{
   1.247 +    SDL_VideoDevice *dev = SDL_GetVideoDevice();
   1.248 +    SDL_VideoData *vdata = ((SDL_VideoData *)dev->driverdata);
   1.249 +    SDL_Mouse *mouse;
   1.250 +    KMSDRM_CursorData *curdata;
   1.251 +    SDL_VideoDisplay *display = NULL;
   1.252 +    SDL_DisplayData *ddata = NULL;
   1.253 +    int ret;
   1.254 +    uint32_t bo_handle;
   1.255 +
   1.256 +    mouse = SDL_GetMouse();
   1.257 +    if (mouse == NULL) {
   1.258 +        return SDL_SetError("No mouse.");
   1.259 +    }
   1.260 +
   1.261 +    if (mouse->focus != NULL) {
   1.262 +        display = SDL_GetDisplayForWindow(mouse->focus);
   1.263 +        if (display != NULL) {
   1.264 +            ddata = (SDL_DisplayData*) display->driverdata;
   1.265 +        }
   1.266 +    }
   1.267 +
   1.268 +    if (cursor == NULL) {
   1.269 +        /* Hide current cursor */
   1.270 +        if ( mouse->cur_cursor != NULL && mouse->cur_cursor->driverdata != NULL) {
   1.271 +            curdata = (KMSDRM_CursorData *) mouse->cur_cursor->driverdata;
   1.272 +
   1.273 +            if (curdata->crtc_id != 0) {
   1.274 +                ret = KMSDRM_drmModeSetCursor(vdata->drm_fd, curdata->crtc_id, 0, 0, 0);
   1.275 +                if (ret) {
   1.276 +                    SDL_SetError("Could not hide current cursor with drmModeSetCursor().");
   1.277 +                    return ret;
   1.278 +                }
   1.279 +                /* Mark previous cursor as not-displayed */
   1.280 +                curdata->crtc_id = 0;
   1.281 +
   1.282 +                return 0;
   1.283 +            }
   1.284 +        }
   1.285 +        /* otherwise if possible, hide global cursor */
   1.286 +        if (ddata != NULL && ddata->crtc_id != 0) {
   1.287 +            ret = KMSDRM_drmModeSetCursor(vdata->drm_fd, ddata->crtc_id, 0, 0, 0);
   1.288 +            if (ret) {
   1.289 +                SDL_SetError("Could not hide display's cursor with drmModeSetCursor().");
   1.290 +                return ret;
   1.291 +            }
   1.292 +            return 0;
   1.293 +        }
   1.294 +
   1.295 +        return SDL_SetError("Couldn't find cursor to hide.");
   1.296 +    }
   1.297 +    /* If cursor != NULL, show new cursor on display */
   1.298 +    if (display == NULL) {
   1.299 +        return SDL_SetError("Could not get display for mouse.");
   1.300 +    }
   1.301 +    if (ddata == NULL) {
   1.302 +        return SDL_SetError("Could not get display driverdata.");
   1.303 +    }
   1.304 +
   1.305 +    curdata = (KMSDRM_CursorData *) cursor->driverdata;
   1.306 +    if (curdata == NULL || curdata->bo == NULL) {
   1.307 +        return SDL_SetError("Cursor not initialized properly.");
   1.308 +    }
   1.309 +
   1.310 +    bo_handle = KMSDRM_gbm_bo_get_handle(curdata->bo).u32;
   1.311 +    if (curdata->hot_x == 0 && curdata->hot_y == 0) {
   1.312 +        ret = KMSDRM_drmModeSetCursor(vdata->drm_fd, ddata->crtc_id, bo_handle,
   1.313 +                                      curdata->w, curdata->h);
   1.314 +    } else {
   1.315 +        ret = KMSDRM_drmModeSetCursor2(vdata->drm_fd, ddata->crtc_id, bo_handle,
   1.316 +                                       curdata->w, curdata->h,
   1.317 +                                       curdata->hot_x, curdata->hot_y);
   1.318 +    }
   1.319 +    if (ret) {
   1.320 +        SDL_SetError("drmModeSetCursor failed.");
   1.321 +        return ret;
   1.322 +    }
   1.323 +
   1.324 +    curdata->crtc_id = ddata->crtc_id;
   1.325 +
   1.326 +    return 0;
   1.327 +}
   1.328 +
   1.329 +/* Free a window manager cursor */
   1.330 +static void
   1.331 +KMSDRM_FreeCursor(SDL_Cursor * cursor)
   1.332 +{
   1.333 +    KMSDRM_CursorData *curdata;
   1.334 +    int drm_fd;
   1.335 +
   1.336 +    if (cursor != NULL) {
   1.337 +        curdata = (KMSDRM_CursorData *) cursor->driverdata;
   1.338 +
   1.339 +        if (curdata != NULL) {
   1.340 +            if (curdata->bo != NULL) {
   1.341 +                if (curdata->crtc_id != 0) {
   1.342 +                    drm_fd = KMSDRM_gbm_device_get_fd(KMSDRM_gbm_bo_get_device(curdata->bo));
   1.343 +                    /* Hide the cursor if previously shown on a CRTC */
   1.344 +                    KMSDRM_drmModeSetCursor(drm_fd, curdata->crtc_id, 0, 0, 0);
   1.345 +                    curdata->crtc_id = 0;
   1.346 +                }
   1.347 +                KMSDRM_gbm_bo_destroy(curdata->bo);
   1.348 +                curdata->bo = NULL;
   1.349 +            }
   1.350 +            SDL_free(cursor->driverdata);
   1.351 +        }
   1.352 +        SDL_free(cursor);
   1.353 +    }
   1.354 +}
   1.355 +
   1.356 +/* Warp the mouse to (x,y) */
   1.357 +static void
   1.358 +KMSDRM_WarpMouse(SDL_Window * window, int x, int y)
   1.359 +{
   1.360 +    /* Only one global/fullscreen window is supported */
   1.361 +    KMSDRM_WarpMouseGlobal(x, y);
   1.362 +}
   1.363 +
   1.364 +/* Warp the mouse to (x,y) */
   1.365 +static int
   1.366 +KMSDRM_WarpMouseGlobal(int x, int y)
   1.367 +{
   1.368 +    KMSDRM_CursorData *curdata;
   1.369 +    SDL_Mouse *mouse = SDL_GetMouse();
   1.370 +
   1.371 +    if (mouse != NULL && mouse->cur_cursor != NULL && mouse->cur_cursor->driverdata != NULL) {
   1.372 +        curdata = (KMSDRM_CursorData *) mouse->cur_cursor->driverdata;
   1.373 +        if (curdata->bo != NULL) {
   1.374 +            if (curdata->crtc_id != 0) {
   1.375 +                int ret, drm_fd;
   1.376 +                drm_fd = KMSDRM_gbm_device_get_fd(KMSDRM_gbm_bo_get_device(curdata->bo));
   1.377 +                ret = KMSDRM_drmModeMoveCursor(drm_fd, curdata->crtc_id, x, y);
   1.378 +
   1.379 +                if (ret) {
   1.380 +                    SDL_SetError("drmModeMoveCursor() failed.");
   1.381 +                }
   1.382 +
   1.383 +                return ret;
   1.384 +            } else {
   1.385 +                return SDL_SetError("Cursor is not currently shown.");
   1.386 +            }
   1.387 +        } else {
   1.388 +            return SDL_SetError("Cursor not initialized properly.");
   1.389 +        }
   1.390 +    } else {
   1.391 +        return SDL_SetError("No mouse or current cursor.");
   1.392 +    }
   1.393 +}
   1.394 +
   1.395 +void
   1.396 +KMSDRM_InitMouse(_THIS)
   1.397 +{
   1.398 +    /* FIXME: Using UDEV it should be possible to scan all mice
   1.399 +     * but there's no point in doing so as there's no multimice support...yet!
   1.400 +     */
   1.401 +    SDL_Mouse *mouse = SDL_GetMouse();
   1.402 +
   1.403 +    mouse->CreateCursor = KMSDRM_CreateCursor;
   1.404 +    mouse->ShowCursor = KMSDRM_ShowCursor;
   1.405 +    mouse->MoveCursor = KMSDRM_MoveCursor;
   1.406 +    mouse->FreeCursor = KMSDRM_FreeCursor;
   1.407 +    mouse->WarpMouse = KMSDRM_WarpMouse;
   1.408 +    mouse->WarpMouseGlobal = KMSDRM_WarpMouseGlobal;
   1.409 +
   1.410 +    SDL_SetDefaultCursor(KMSDRM_CreateDefaultCursor());
   1.411 +}
   1.412 +
   1.413 +void
   1.414 +KMSDRM_QuitMouse(_THIS)
   1.415 +{
   1.416 +    /* TODO: ? */
   1.417 +}
   1.418 +
   1.419 +/* This is called when a mouse motion event occurs */
   1.420 +static void
   1.421 +KMSDRM_MoveCursor(SDL_Cursor * cursor)
   1.422 +{
   1.423 +    SDL_Mouse *mouse = SDL_GetMouse();
   1.424 +    KMSDRM_WarpMouse(mouse->focus, mouse->x, mouse->y);
   1.425 +}
   1.426 +
   1.427 +#endif /* SDL_VIDEO_DRIVER_KMSDRM */
   1.428 +
   1.429 +/* vi: set ts=4 sw=4 expandtab: */