src/video/x11/SDL_x11mouse.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 13 Oct 2011 01:08:30 -0400
changeset 5981 75caa8a7d559
parent 5535 96594ac5fd1a
child 6138 4c64952a58fb
permissions -rw-r--r--
Fixed a whole slew of compiler warnings that -Wall exposed.
slouken@1950
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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"
slouken@1950
    28
#include "../../events/SDL_mouse_c.h"
slouken@1950
    29
slouken@5470
    30
slouken@5470
    31
/* FIXME: Find a better place to put this... */
slouken@5470
    32
static Cursor x11_empty_cursor = None;
slouken@5470
    33
slouken@5470
    34
static Display *
slouken@5470
    35
GetDisplay(void)
slouken@5470
    36
{
slouken@5470
    37
    return ((SDL_VideoData *)SDL_GetVideoDevice()->driverdata)->display;
slouken@5470
    38
}
slouken@5470
    39
slouken@5470
    40
static Cursor
slouken@5470
    41
X11_CreateEmptyCursor()
slouken@5470
    42
{
slouken@5470
    43
    if (x11_empty_cursor == None) {
slouken@5470
    44
        Display *display = GetDisplay();
slouken@5470
    45
        char data[1];
slouken@5470
    46
        XColor color;
slouken@5470
    47
        Pixmap pixmap;
slouken@5470
    48
slouken@5470
    49
        SDL_zero(data);
slouken@5470
    50
        color.red = color.green = color.blue = 0;
slouken@5470
    51
        pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
slouken@5470
    52
                                       data, 1, 1);
slouken@5470
    53
        if (pixmap) {
slouken@5470
    54
            x11_empty_cursor = XCreatePixmapCursor(display, pixmap, pixmap,
slouken@5470
    55
                                                   &color, &color, 0, 0);
slouken@5470
    56
            XFreePixmap(display, pixmap);
slouken@5470
    57
        }
slouken@5470
    58
    }
slouken@5470
    59
    return x11_empty_cursor;
slouken@5470
    60
}
slouken@5470
    61
slouken@5470
    62
static void
slouken@5470
    63
X11_DestroyEmptyCursor(void)
slouken@5470
    64
{
slouken@5470
    65
    if (x11_empty_cursor != None) {
slouken@5470
    66
        XFreeCursor(GetDisplay(), x11_empty_cursor);
slouken@5470
    67
        x11_empty_cursor = None;
slouken@5470
    68
    }
slouken@5470
    69
}
slouken@5470
    70
slouken@5470
    71
static SDL_Cursor *
slouken@5470
    72
X11_CreateDefaultCursor()
slouken@5470
    73
{
slouken@5470
    74
    SDL_Cursor *cursor;
slouken@5470
    75
slouken@5470
    76
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5470
    77
    if (cursor) {
slouken@5470
    78
        /* None is used to indicate the default cursor */
slouken@5470
    79
        cursor->driverdata = (void*)None;
slouken@5470
    80
    } else {
slouken@5470
    81
        SDL_OutOfMemory();
slouken@5470
    82
    }
slouken@5470
    83
slouken@5470
    84
    return cursor;
slouken@5470
    85
}
slouken@5470
    86
slouken@5471
    87
#if SDL_VIDEO_DRIVER_X11_XCURSOR
slouken@5471
    88
static Cursor
slouken@5471
    89
X11_CreateXCursorCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5471
    90
{
slouken@5471
    91
    Display *display = GetDisplay();
slouken@5471
    92
    Cursor cursor = None;
slouken@5471
    93
    XcursorImage *image;
slouken@5471
    94
slouken@5471
    95
    image = XcursorImageCreate(surface->w, surface->h);
slouken@5471
    96
    if (!image) {
slouken@5471
    97
        SDL_OutOfMemory();
slouken@5471
    98
        return None;
slouken@5471
    99
    }
slouken@5471
   100
    image->xhot = hot_x;
slouken@5471
   101
    image->yhot = hot_y;
slouken@5471
   102
    image->delay = 0;
slouken@5471
   103
slouken@5471
   104
    SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@5471
   105
    SDL_assert(surface->pitch == surface->w * 4);
slouken@5471
   106
    SDL_memcpy(image->pixels, surface->pixels, surface->h * surface->pitch);
slouken@5471
   107
slouken@5471
   108
    cursor = XcursorImageLoadCursor(display, image);
slouken@5471
   109
slouken@5471
   110
    XcursorImageDestroy(image);
slouken@5471
   111
slouken@5471
   112
    return cursor;
slouken@5471
   113
}
slouken@5471
   114
#endif /* SDL_VIDEO_DRIVER_X11_XCURSOR */
slouken@5471
   115
slouken@5470
   116
static Cursor
slouken@5470
   117
X11_CreatePixmapCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5470
   118
{
slouken@5470
   119
    Display *display = GetDisplay();
slouken@5470
   120
    XColor fg, bg;
slouken@5470
   121
    Cursor cursor = None;
slouken@5470
   122
    Uint32 *ptr;
slouken@5470
   123
    Uint8 *data_bits, *mask_bits;
slouken@5470
   124
    Pixmap data_pixmap, mask_pixmap;
slouken@5470
   125
    int x, y;
slouken@5470
   126
    unsigned int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
slouken@5470
   127
    unsigned int width_bytes = ((surface->w + 7) & ~7) / 8;
slouken@5470
   128
slouken@5470
   129
    data_bits = SDL_calloc(1, surface->h * width_bytes);
slouken@5470
   130
    mask_bits = SDL_calloc(1, surface->h * width_bytes);
slouken@5470
   131
    if (!data_bits || !mask_bits) {
slouken@5470
   132
        SDL_OutOfMemory();
slouken@5470
   133
        return None;
slouken@5470
   134
    }
slouken@5470
   135
slouken@5471
   136
    /* Code below assumes ARGB pixel format */
slouken@5471
   137
    SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@5471
   138
icculus@5981
   139
    rfg = gfg = bfg = rbg = gbg = bbg = fgBits = bgBits = 0;
slouken@5470
   140
    for (y = 0; y < surface->h; ++y) {
slouken@5470
   141
        ptr = (Uint32 *)((Uint8 *)surface->pixels + y * surface->pitch);
slouken@5470
   142
        for (x = 0; x < surface->w; ++x) {
slouken@5470
   143
            int alpha = (*ptr >> 24) & 0xff;
slouken@5470
   144
            int red   = (*ptr >> 16) & 0xff;
slouken@5470
   145
            int green = (*ptr >> 8) & 0xff;
slouken@5470
   146
            int blue  = (*ptr >> 0) & 0xff;
slouken@5470
   147
            if (alpha > 25) {
slouken@5470
   148
                mask_bits[y * width_bytes + x / 8] |= (0x01 << (x % 8));
slouken@5470
   149
slouken@5470
   150
                if ((red + green + blue) > 0x40) {
slouken@5470
   151
                    fgBits++;
slouken@5470
   152
                    rfg += red;
slouken@5470
   153
                    gfg += green;
slouken@5470
   154
                    bfg += blue;
slouken@5470
   155
                    data_bits[y * width_bytes + x / 8] |= (0x01 << (x % 8));
slouken@5470
   156
                } else {
slouken@5470
   157
                    bgBits++;
slouken@5470
   158
                    rbg += red;
slouken@5470
   159
                    gbg += green;
slouken@5470
   160
                    bbg += blue;
slouken@5470
   161
                }
slouken@5470
   162
            }
slouken@5470
   163
            ++ptr;
slouken@5470
   164
        }
slouken@5470
   165
    }
slouken@5470
   166
slouken@5470
   167
    if (fgBits) {
slouken@5470
   168
        fg.red   = rfg * 257 / fgBits;
slouken@5470
   169
        fg.green = gfg * 257 / fgBits;
slouken@5470
   170
        fg.blue  = bfg * 257 / fgBits;
slouken@5470
   171
    }
slouken@5470
   172
    else fg.red = fg.green = fg.blue = 0;
slouken@5470
   173
slouken@5470
   174
    if (bgBits) {
slouken@5470
   175
        bg.red   = rbg * 257 / bgBits;
slouken@5470
   176
        bg.green = gbg * 257 / bgBits;
slouken@5470
   177
        bg.blue  = bbg * 257 / bgBits;
slouken@5470
   178
    }
slouken@5470
   179
    else bg.red = bg.green = bg.blue = 0;
slouken@5470
   180
slouken@5470
   181
    data_pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
slouken@5505
   182
                                        (char*)data_bits,
slouken@5505
   183
                                        surface->w, surface->h);
slouken@5470
   184
    mask_pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
slouken@5505
   185
                                        (char*)mask_bits,
slouken@5505
   186
                                        surface->w, surface->h);
slouken@5470
   187
    cursor = XCreatePixmapCursor(display, data_pixmap, mask_pixmap,
slouken@5470
   188
                                 &fg, &bg, hot_x, hot_y);
slouken@5470
   189
	XFreePixmap(display, data_pixmap);
slouken@5470
   190
	XFreePixmap(display, mask_pixmap);
slouken@5470
   191
slouken@5470
   192
    return cursor;
slouken@5470
   193
}
slouken@5470
   194
slouken@5470
   195
static SDL_Cursor *
slouken@5470
   196
X11_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5470
   197
{
slouken@5470
   198
    SDL_Cursor *cursor;
slouken@5470
   199
slouken@5470
   200
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5470
   201
    if (cursor) {
slouken@5471
   202
        Cursor x11_cursor = None;
slouken@5471
   203
slouken@5471
   204
#if SDL_VIDEO_DRIVER_X11_XCURSOR
slouken@5471
   205
        if (SDL_X11_HAVE_XCURSOR) {
slouken@5471
   206
            x11_cursor = X11_CreateXCursorCursor(surface, hot_x, hot_y);
slouken@5471
   207
        }
slouken@5471
   208
#endif
slouken@5471
   209
        if (x11_cursor == None) {
slouken@5471
   210
            x11_cursor = X11_CreatePixmapCursor(surface, hot_x, hot_y);
slouken@5471
   211
        }
slouken@5471
   212
        cursor->driverdata = (void*)x11_cursor;
slouken@5470
   213
    } else {
slouken@5470
   214
        SDL_OutOfMemory();
slouken@5470
   215
    }
slouken@5470
   216
slouken@5470
   217
    return cursor;
slouken@5470
   218
}
slouken@5470
   219
slouken@5470
   220
static void
slouken@5470
   221
X11_FreeCursor(SDL_Cursor * cursor)
slouken@5470
   222
{
slouken@5470
   223
    Cursor x11_cursor = (Cursor)cursor->driverdata;
slouken@5470
   224
slouken@5470
   225
    if (x11_cursor != None) {
slouken@5470
   226
        XFreeCursor(GetDisplay(), x11_cursor);
slouken@5470
   227
    }
slouken@5470
   228
    SDL_free(cursor);
slouken@5470
   229
}
slouken@5470
   230
slouken@5470
   231
static int
slouken@5470
   232
X11_ShowCursor(SDL_Cursor * cursor)
slouken@5470
   233
{
slouken@5470
   234
    Cursor x11_cursor = 0;
slouken@5470
   235
slouken@5470
   236
    if (cursor) {
slouken@5470
   237
        x11_cursor = (Cursor)cursor->driverdata;
slouken@5470
   238
    } else {
slouken@5470
   239
        x11_cursor = X11_CreateEmptyCursor();
slouken@5470
   240
    }
slouken@5470
   241
slouken@5470
   242
    /* FIXME: Is there a better way than this? */
slouken@5470
   243
    {
slouken@5470
   244
        SDL_VideoDevice *video = SDL_GetVideoDevice();
slouken@5470
   245
        Display *display = GetDisplay();
slouken@5470
   246
        SDL_Window *window;
slouken@5470
   247
        SDL_WindowData *data;
slouken@5470
   248
slouken@5470
   249
        for (window = video->windows; window; window = window->next) {
slouken@5470
   250
            data = (SDL_WindowData *)window->driverdata;
slouken@5470
   251
            if (x11_cursor != None) {
slouken@5470
   252
                XDefineCursor(display, data->xwindow, x11_cursor);
slouken@5470
   253
            } else {
slouken@5470
   254
                XUndefineCursor(display, data->xwindow);
slouken@5470
   255
            }
slouken@5470
   256
        }
slouken@5470
   257
        XFlush(display);
slouken@5470
   258
    }
slouken@5470
   259
    return 0;
slouken@5470
   260
}
slouken@5470
   261
slouken@5470
   262
static void
slouken@5470
   263
X11_WarpMouse(SDL_Window * window, int x, int y)
slouken@5470
   264
{
slouken@5470
   265
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5470
   266
    Display *display = data->videodata->display;
slouken@5470
   267
slouken@5470
   268
    XWarpPointer(display, None, data->xwindow, 0, 0, 0, 0, x, y);
slouken@5470
   269
    XSync(display, False);
slouken@5470
   270
}
slouken@5470
   271
slouken@5470
   272
static int
slouken@5470
   273
X11_SetRelativeMouseMode(SDL_bool enabled)
slouken@5470
   274
{
slouken@5470
   275
    SDL_Unsupported();
slouken@5470
   276
    return -1;
slouken@5470
   277
}
slouken@5470
   278
slouken@1950
   279
void
slouken@1950
   280
X11_InitMouse(_THIS)
slouken@1950
   281
{
slouken@5470
   282
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5470
   283
slouken@5470
   284
    mouse->CreateCursor = X11_CreateCursor;
slouken@5470
   285
    mouse->ShowCursor = X11_ShowCursor;
slouken@5470
   286
    mouse->FreeCursor = X11_FreeCursor;
slouken@5470
   287
    mouse->WarpMouse = X11_WarpMouse;
slouken@5470
   288
    mouse->SetRelativeMouseMode = X11_SetRelativeMouseMode;
slouken@5470
   289
slouken@5470
   290
    SDL_SetDefaultCursor(X11_CreateDefaultCursor());
slouken@1950
   291
}
slouken@1950
   292
slouken@1950
   293
void
slouken@1950
   294
X11_QuitMouse(_THIS)
slouken@1950
   295
{
slouken@5470
   296
    X11_DestroyEmptyCursor();
slouken@1950
   297
}
slouken@1950
   298
slouken@5481
   299
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   300
slouken@1950
   301
/* vi: set ts=4 sw=4 expandtab: */