src/video/x11/SDL_x11mouse.c
author Dimitris Zenios <dimitris.zenios@gmail.com>
Thu, 31 May 2012 13:37:02 +0300
changeset 6316 a89c79a3f0cd
parent 6311 4e5e46f43438
child 6675 20f3cdea0fd2
permissions -rwxr-xr-x
1.Moved all xinput2 functionality to its own file
2.Implement touch events using Xinput2.Leave evtouch as a fallback when xinput2 is not supported
slouken@1950
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@1950
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1950
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1950
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1950
    20
*/
slouken@1950
    21
#include "SDL_config.h"
slouken@5471
    22
slouken@5481
    23
#if SDL_VIDEO_DRIVER_X11
slouken@5481
    24
slouken@5471
    25
#include "SDL_assert.h"
slouken@1950
    26
#include "SDL_x11video.h"
slouken@2940
    27
#include "SDL_x11mouse.h"
dimitris@6316
    28
#include "SDL_x11xinput2.h"
slouken@1950
    29
#include "../../events/SDL_mouse_c.h"
slouken@1950
    30
slouken@5470
    31
slouken@5470
    32
/* FIXME: Find a better place to put this... */
slouken@5470
    33
static Cursor x11_empty_cursor = None;
slouken@5470
    34
slouken@5470
    35
static Display *
slouken@5470
    36
GetDisplay(void)
slouken@5470
    37
{
slouken@5470
    38
    return ((SDL_VideoData *)SDL_GetVideoDevice()->driverdata)->display;
slouken@5470
    39
}
slouken@5470
    40
slouken@5470
    41
static Cursor
slouken@5470
    42
X11_CreateEmptyCursor()
slouken@5470
    43
{
slouken@5470
    44
    if (x11_empty_cursor == None) {
slouken@5470
    45
        Display *display = GetDisplay();
slouken@5470
    46
        char data[1];
slouken@5470
    47
        XColor color;
slouken@5470
    48
        Pixmap pixmap;
slouken@5470
    49
slouken@5470
    50
        SDL_zero(data);
slouken@5470
    51
        color.red = color.green = color.blue = 0;
slouken@5470
    52
        pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
slouken@5470
    53
                                       data, 1, 1);
slouken@5470
    54
        if (pixmap) {
slouken@5470
    55
            x11_empty_cursor = XCreatePixmapCursor(display, pixmap, pixmap,
slouken@5470
    56
                                                   &color, &color, 0, 0);
slouken@5470
    57
            XFreePixmap(display, pixmap);
slouken@5470
    58
        }
slouken@5470
    59
    }
slouken@5470
    60
    return x11_empty_cursor;
slouken@5470
    61
}
slouken@5470
    62
slouken@5470
    63
static void
slouken@5470
    64
X11_DestroyEmptyCursor(void)
slouken@5470
    65
{
slouken@5470
    66
    if (x11_empty_cursor != None) {
slouken@5470
    67
        XFreeCursor(GetDisplay(), x11_empty_cursor);
slouken@5470
    68
        x11_empty_cursor = None;
slouken@5470
    69
    }
slouken@5470
    70
}
slouken@5470
    71
slouken@5470
    72
static SDL_Cursor *
slouken@5470
    73
X11_CreateDefaultCursor()
slouken@5470
    74
{
slouken@5470
    75
    SDL_Cursor *cursor;
slouken@5470
    76
slouken@5470
    77
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5470
    78
    if (cursor) {
slouken@5470
    79
        /* None is used to indicate the default cursor */
slouken@5470
    80
        cursor->driverdata = (void*)None;
slouken@5470
    81
    } else {
slouken@5470
    82
        SDL_OutOfMemory();
slouken@5470
    83
    }
slouken@5470
    84
slouken@5470
    85
    return cursor;
slouken@5470
    86
}
slouken@5470
    87
slouken@5471
    88
#if SDL_VIDEO_DRIVER_X11_XCURSOR
slouken@5471
    89
static Cursor
slouken@5471
    90
X11_CreateXCursorCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5471
    91
{
slouken@5471
    92
    Display *display = GetDisplay();
slouken@5471
    93
    Cursor cursor = None;
slouken@5471
    94
    XcursorImage *image;
slouken@5471
    95
slouken@5471
    96
    image = XcursorImageCreate(surface->w, surface->h);
slouken@5471
    97
    if (!image) {
slouken@5471
    98
        SDL_OutOfMemory();
slouken@5471
    99
        return None;
slouken@5471
   100
    }
slouken@5471
   101
    image->xhot = hot_x;
slouken@5471
   102
    image->yhot = hot_y;
slouken@5471
   103
    image->delay = 0;
slouken@5471
   104
slouken@5471
   105
    SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@5471
   106
    SDL_assert(surface->pitch == surface->w * 4);
slouken@5471
   107
    SDL_memcpy(image->pixels, surface->pixels, surface->h * surface->pitch);
slouken@5471
   108
slouken@5471
   109
    cursor = XcursorImageLoadCursor(display, image);
slouken@5471
   110
slouken@5471
   111
    XcursorImageDestroy(image);
slouken@5471
   112
slouken@5471
   113
    return cursor;
slouken@5471
   114
}
slouken@5471
   115
#endif /* SDL_VIDEO_DRIVER_X11_XCURSOR */
slouken@5471
   116
slouken@5470
   117
static Cursor
slouken@5470
   118
X11_CreatePixmapCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5470
   119
{
slouken@5470
   120
    Display *display = GetDisplay();
slouken@5470
   121
    XColor fg, bg;
slouken@5470
   122
    Cursor cursor = None;
slouken@5470
   123
    Uint32 *ptr;
slouken@5470
   124
    Uint8 *data_bits, *mask_bits;
slouken@5470
   125
    Pixmap data_pixmap, mask_pixmap;
slouken@5470
   126
    int x, y;
slouken@5470
   127
    unsigned int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
slouken@5470
   128
    unsigned int width_bytes = ((surface->w + 7) & ~7) / 8;
slouken@5470
   129
slouken@5470
   130
    data_bits = SDL_calloc(1, surface->h * width_bytes);
slouken@5470
   131
    mask_bits = SDL_calloc(1, surface->h * width_bytes);
slouken@5470
   132
    if (!data_bits || !mask_bits) {
slouken@5470
   133
        SDL_OutOfMemory();
slouken@5470
   134
        return None;
slouken@5470
   135
    }
slouken@5470
   136
slouken@5471
   137
    /* Code below assumes ARGB pixel format */
slouken@5471
   138
    SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@5471
   139
icculus@5981
   140
    rfg = gfg = bfg = rbg = gbg = bbg = fgBits = bgBits = 0;
slouken@5470
   141
    for (y = 0; y < surface->h; ++y) {
slouken@5470
   142
        ptr = (Uint32 *)((Uint8 *)surface->pixels + y * surface->pitch);
slouken@5470
   143
        for (x = 0; x < surface->w; ++x) {
slouken@5470
   144
            int alpha = (*ptr >> 24) & 0xff;
slouken@5470
   145
            int red   = (*ptr >> 16) & 0xff;
slouken@5470
   146
            int green = (*ptr >> 8) & 0xff;
slouken@5470
   147
            int blue  = (*ptr >> 0) & 0xff;
slouken@5470
   148
            if (alpha > 25) {
slouken@5470
   149
                mask_bits[y * width_bytes + x / 8] |= (0x01 << (x % 8));
slouken@5470
   150
slouken@5470
   151
                if ((red + green + blue) > 0x40) {
slouken@5470
   152
                    fgBits++;
slouken@5470
   153
                    rfg += red;
slouken@5470
   154
                    gfg += green;
slouken@5470
   155
                    bfg += blue;
slouken@5470
   156
                    data_bits[y * width_bytes + x / 8] |= (0x01 << (x % 8));
slouken@5470
   157
                } else {
slouken@5470
   158
                    bgBits++;
slouken@5470
   159
                    rbg += red;
slouken@5470
   160
                    gbg += green;
slouken@5470
   161
                    bbg += blue;
slouken@5470
   162
                }
slouken@5470
   163
            }
slouken@5470
   164
            ++ptr;
slouken@5470
   165
        }
slouken@5470
   166
    }
slouken@5470
   167
slouken@5470
   168
    if (fgBits) {
slouken@5470
   169
        fg.red   = rfg * 257 / fgBits;
slouken@5470
   170
        fg.green = gfg * 257 / fgBits;
slouken@5470
   171
        fg.blue  = bfg * 257 / fgBits;
slouken@5470
   172
    }
slouken@5470
   173
    else fg.red = fg.green = fg.blue = 0;
slouken@5470
   174
slouken@5470
   175
    if (bgBits) {
slouken@5470
   176
        bg.red   = rbg * 257 / bgBits;
slouken@5470
   177
        bg.green = gbg * 257 / bgBits;
slouken@5470
   178
        bg.blue  = bbg * 257 / bgBits;
slouken@5470
   179
    }
slouken@5470
   180
    else bg.red = bg.green = bg.blue = 0;
slouken@5470
   181
slouken@5470
   182
    data_pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
slouken@5505
   183
                                        (char*)data_bits,
slouken@5505
   184
                                        surface->w, surface->h);
slouken@5470
   185
    mask_pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
slouken@5505
   186
                                        (char*)mask_bits,
slouken@5505
   187
                                        surface->w, surface->h);
slouken@5470
   188
    cursor = XCreatePixmapCursor(display, data_pixmap, mask_pixmap,
slouken@5470
   189
                                 &fg, &bg, hot_x, hot_y);
slouken@5470
   190
	XFreePixmap(display, data_pixmap);
slouken@5470
   191
	XFreePixmap(display, mask_pixmap);
slouken@5470
   192
slouken@5470
   193
    return cursor;
slouken@5470
   194
}
slouken@5470
   195
slouken@5470
   196
static SDL_Cursor *
slouken@5470
   197
X11_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5470
   198
{
slouken@5470
   199
    SDL_Cursor *cursor;
slouken@5470
   200
slouken@5470
   201
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5470
   202
    if (cursor) {
slouken@5471
   203
        Cursor x11_cursor = None;
slouken@5471
   204
slouken@5471
   205
#if SDL_VIDEO_DRIVER_X11_XCURSOR
slouken@5471
   206
        if (SDL_X11_HAVE_XCURSOR) {
slouken@5471
   207
            x11_cursor = X11_CreateXCursorCursor(surface, hot_x, hot_y);
slouken@5471
   208
        }
slouken@5471
   209
#endif
slouken@5471
   210
        if (x11_cursor == None) {
slouken@5471
   211
            x11_cursor = X11_CreatePixmapCursor(surface, hot_x, hot_y);
slouken@5471
   212
        }
slouken@5471
   213
        cursor->driverdata = (void*)x11_cursor;
slouken@5470
   214
    } else {
slouken@5470
   215
        SDL_OutOfMemory();
slouken@5470
   216
    }
slouken@5470
   217
slouken@5470
   218
    return cursor;
slouken@5470
   219
}
slouken@5470
   220
slouken@5470
   221
static void
slouken@5470
   222
X11_FreeCursor(SDL_Cursor * cursor)
slouken@5470
   223
{
slouken@5470
   224
    Cursor x11_cursor = (Cursor)cursor->driverdata;
slouken@5470
   225
slouken@5470
   226
    if (x11_cursor != None) {
slouken@5470
   227
        XFreeCursor(GetDisplay(), x11_cursor);
slouken@5470
   228
    }
slouken@5470
   229
    SDL_free(cursor);
slouken@5470
   230
}
slouken@5470
   231
slouken@5470
   232
static int
slouken@5470
   233
X11_ShowCursor(SDL_Cursor * cursor)
slouken@5470
   234
{
slouken@5470
   235
    Cursor x11_cursor = 0;
slouken@5470
   236
slouken@5470
   237
    if (cursor) {
slouken@5470
   238
        x11_cursor = (Cursor)cursor->driverdata;
slouken@5470
   239
    } else {
slouken@5470
   240
        x11_cursor = X11_CreateEmptyCursor();
slouken@5470
   241
    }
slouken@5470
   242
slouken@5470
   243
    /* FIXME: Is there a better way than this? */
slouken@5470
   244
    {
slouken@5470
   245
        SDL_VideoDevice *video = SDL_GetVideoDevice();
slouken@5470
   246
        Display *display = GetDisplay();
slouken@5470
   247
        SDL_Window *window;
slouken@5470
   248
        SDL_WindowData *data;
slouken@5470
   249
slouken@5470
   250
        for (window = video->windows; window; window = window->next) {
slouken@5470
   251
            data = (SDL_WindowData *)window->driverdata;
slouken@5470
   252
            if (x11_cursor != None) {
slouken@5470
   253
                XDefineCursor(display, data->xwindow, x11_cursor);
slouken@5470
   254
            } else {
slouken@5470
   255
                XUndefineCursor(display, data->xwindow);
slouken@5470
   256
            }
slouken@5470
   257
        }
slouken@5470
   258
        XFlush(display);
slouken@5470
   259
    }
slouken@5470
   260
    return 0;
slouken@5470
   261
}
slouken@5470
   262
slouken@5470
   263
static void
slouken@5470
   264
X11_WarpMouse(SDL_Window * window, int x, int y)
slouken@5470
   265
{
slouken@5470
   266
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5470
   267
    Display *display = data->videodata->display;
slouken@5470
   268
slouken@5470
   269
    XWarpPointer(display, None, data->xwindow, 0, 0, 0, 0, x, y);
slouken@5470
   270
    XSync(display, False);
slouken@5470
   271
}
slouken@5470
   272
slouken@5470
   273
static int
slouken@5470
   274
X11_SetRelativeMouseMode(SDL_bool enabled)
slouken@5470
   275
{
slouken@6311
   276
#if SDL_VIDEO_DRIVER_X11_XINPUT2
dimitris@6316
   277
    if(X11_Xinput2IsInitialized())
dimitris@6316
   278
        return 0;
slouken@6311
   279
#else
slouken@5470
   280
    SDL_Unsupported();
dimitris@6316
   281
#endif
slouken@5470
   282
    return -1;
slouken@5470
   283
}
slouken@5470
   284
slouken@1950
   285
void
slouken@1950
   286
X11_InitMouse(_THIS)
slouken@1950
   287
{
slouken@5470
   288
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5470
   289
slouken@5470
   290
    mouse->CreateCursor = X11_CreateCursor;
slouken@5470
   291
    mouse->ShowCursor = X11_ShowCursor;
slouken@5470
   292
    mouse->FreeCursor = X11_FreeCursor;
slouken@5470
   293
    mouse->WarpMouse = X11_WarpMouse;
slouken@5470
   294
    mouse->SetRelativeMouseMode = X11_SetRelativeMouseMode;
slouken@5470
   295
slouken@5470
   296
    SDL_SetDefaultCursor(X11_CreateDefaultCursor());
slouken@1950
   297
}
slouken@1950
   298
slouken@1950
   299
void
slouken@1950
   300
X11_QuitMouse(_THIS)
slouken@1950
   301
{
slouken@5470
   302
    X11_DestroyEmptyCursor();
slouken@1950
   303
}
slouken@1950
   304
slouken@5481
   305
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   306
slouken@1950
   307
/* vi: set ts=4 sw=4 expandtab: */