src/video/x11/SDL_x11mouse.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Mar 2011 13:27:25 -0800
changeset 5470 8f5e10ec4faf
parent 5262 b530ef003506
child 5471 179adad3ae6f
permissions -rw-r--r--
Implemented X11 cursor support.
slouken@1950
     1
/*
slouken@1950
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@1950
     4
slouken@1950
     5
    This library is free software; you can redistribute it and/or
slouken@1950
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1950
     7
    License as published by the Free Software Foundation; either
slouken@1950
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1950
     9
slouken@1950
    10
    This library is distributed in the hope that it will be useful,
slouken@1950
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1950
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1950
    13
    Lesser General Public License for more details.
slouken@1950
    14
slouken@1950
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1950
    16
    License along with this library; if not, write to the Free Software
slouken@1950
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1950
    18
slouken@1950
    19
    Sam Lantinga
slouken@1950
    20
    slouken@libsdl.org
slouken@1950
    21
*/
slouken@1950
    22
#include "SDL_config.h"
slouken@1950
    23
#include "SDL_x11video.h"
slouken@2940
    24
#include "SDL_x11mouse.h"
slouken@1950
    25
#include "../../events/SDL_mouse_c.h"
slouken@1950
    26
slouken@5470
    27
slouken@5470
    28
/* FIXME: Find a better place to put this... */
slouken@5470
    29
static Cursor x11_empty_cursor = None;
slouken@5470
    30
slouken@5470
    31
static Display *
slouken@5470
    32
GetDisplay(void)
slouken@5470
    33
{
slouken@5470
    34
    return ((SDL_VideoData *)SDL_GetVideoDevice()->driverdata)->display;
slouken@5470
    35
}
slouken@5470
    36
slouken@5470
    37
static Cursor
slouken@5470
    38
X11_CreateEmptyCursor()
slouken@5470
    39
{
slouken@5470
    40
    if (x11_empty_cursor == None) {
slouken@5470
    41
        Display *display = GetDisplay();
slouken@5470
    42
        char data[1];
slouken@5470
    43
        XColor color;
slouken@5470
    44
        Pixmap pixmap;
slouken@5470
    45
slouken@5470
    46
        SDL_zero(data);
slouken@5470
    47
        color.red = color.green = color.blue = 0;
slouken@5470
    48
        pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
slouken@5470
    49
                                       data, 1, 1);
slouken@5470
    50
        if (pixmap) {
slouken@5470
    51
            x11_empty_cursor = XCreatePixmapCursor(display, pixmap, pixmap,
slouken@5470
    52
                                                   &color, &color, 0, 0);
slouken@5470
    53
            XFreePixmap(display, pixmap);
slouken@5470
    54
        }
slouken@5470
    55
    }
slouken@5470
    56
    return x11_empty_cursor;
slouken@5470
    57
}
slouken@5470
    58
slouken@5470
    59
static void
slouken@5470
    60
X11_DestroyEmptyCursor(void)
slouken@5470
    61
{
slouken@5470
    62
    if (x11_empty_cursor != None) {
slouken@5470
    63
        XFreeCursor(GetDisplay(), x11_empty_cursor);
slouken@5470
    64
        x11_empty_cursor = None;
slouken@5470
    65
    }
slouken@5470
    66
}
slouken@5470
    67
slouken@5470
    68
static SDL_Cursor *
slouken@5470
    69
X11_CreateDefaultCursor()
slouken@5470
    70
{
slouken@5470
    71
    SDL_Cursor *cursor;
slouken@5470
    72
slouken@5470
    73
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5470
    74
    if (cursor) {
slouken@5470
    75
        /* None is used to indicate the default cursor */
slouken@5470
    76
        cursor->driverdata = (void*)None;
slouken@5470
    77
    } else {
slouken@5470
    78
        SDL_OutOfMemory();
slouken@5470
    79
    }
slouken@5470
    80
slouken@5470
    81
    return cursor;
slouken@5470
    82
}
slouken@5470
    83
slouken@5470
    84
static Cursor
slouken@5470
    85
X11_CreatePixmapCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5470
    86
{
slouken@5470
    87
    Display *display = GetDisplay();
slouken@5470
    88
    XColor fg, bg;
slouken@5470
    89
    Cursor cursor = None;
slouken@5470
    90
    Uint32 *ptr;
slouken@5470
    91
    Uint8 *data_bits, *mask_bits;
slouken@5470
    92
    Pixmap data_pixmap, mask_pixmap;
slouken@5470
    93
    int x, y;
slouken@5470
    94
    unsigned int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
slouken@5470
    95
    unsigned int width_bytes = ((surface->w + 7) & ~7) / 8;
slouken@5470
    96
slouken@5470
    97
    data_bits = SDL_calloc(1, surface->h * width_bytes);
slouken@5470
    98
    mask_bits = SDL_calloc(1, surface->h * width_bytes);
slouken@5470
    99
    if (!data_bits || !mask_bits) {
slouken@5470
   100
        SDL_OutOfMemory();
slouken@5470
   101
        return None;
slouken@5470
   102
    }
slouken@5470
   103
slouken@5470
   104
    rfg = gfg = bfg = rbg = gbg = bbg = fgBits = 0;
slouken@5470
   105
    for (y = 0; y < surface->h; ++y) {
slouken@5470
   106
        ptr = (Uint32 *)((Uint8 *)surface->pixels + y * surface->pitch);
slouken@5470
   107
        for (x = 0; x < surface->w; ++x) {
slouken@5470
   108
            int alpha = (*ptr >> 24) & 0xff;
slouken@5470
   109
            int red   = (*ptr >> 16) & 0xff;
slouken@5470
   110
            int green = (*ptr >> 8) & 0xff;
slouken@5470
   111
            int blue  = (*ptr >> 0) & 0xff;
slouken@5470
   112
            if (alpha > 25) {
slouken@5470
   113
                mask_bits[y * width_bytes + x / 8] |= (0x01 << (x % 8));
slouken@5470
   114
slouken@5470
   115
                if ((red + green + blue) > 0x40) {
slouken@5470
   116
                    fgBits++;
slouken@5470
   117
                    rfg += red;
slouken@5470
   118
                    gfg += green;
slouken@5470
   119
                    bfg += blue;
slouken@5470
   120
                    data_bits[y * width_bytes + x / 8] |= (0x01 << (x % 8));
slouken@5470
   121
                } else {
slouken@5470
   122
                    bgBits++;
slouken@5470
   123
                    rbg += red;
slouken@5470
   124
                    gbg += green;
slouken@5470
   125
                    bbg += blue;
slouken@5470
   126
                }
slouken@5470
   127
            }
slouken@5470
   128
            ++ptr;
slouken@5470
   129
        }
slouken@5470
   130
    }
slouken@5470
   131
slouken@5470
   132
    if (fgBits) {
slouken@5470
   133
        fg.red   = rfg * 257 / fgBits;
slouken@5470
   134
        fg.green = gfg * 257 / fgBits;
slouken@5470
   135
        fg.blue  = bfg * 257 / fgBits;
slouken@5470
   136
    }
slouken@5470
   137
    else fg.red = fg.green = fg.blue = 0;
slouken@5470
   138
slouken@5470
   139
    if (bgBits) {
slouken@5470
   140
        bg.red   = rbg * 257 / bgBits;
slouken@5470
   141
        bg.green = gbg * 257 / bgBits;
slouken@5470
   142
        bg.blue  = bbg * 257 / bgBits;
slouken@5470
   143
    }
slouken@5470
   144
    else bg.red = bg.green = bg.blue = 0;
slouken@5470
   145
slouken@5470
   146
    data_pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
slouken@5470
   147
                                        data_bits, surface->w, surface->h);
slouken@5470
   148
    mask_pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
slouken@5470
   149
                                        mask_bits, surface->w, surface->h);
slouken@5470
   150
    cursor = XCreatePixmapCursor(display, data_pixmap, mask_pixmap,
slouken@5470
   151
                                 &fg, &bg, hot_x, hot_y);
slouken@5470
   152
	XFreePixmap(display, data_pixmap);
slouken@5470
   153
	XFreePixmap(display, mask_pixmap);
slouken@5470
   154
slouken@5470
   155
    return cursor;
slouken@5470
   156
}
slouken@5470
   157
slouken@5470
   158
static SDL_Cursor *
slouken@5470
   159
X11_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5470
   160
{
slouken@5470
   161
    SDL_Cursor *cursor;
slouken@5470
   162
slouken@5470
   163
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5470
   164
    if (cursor) {
slouken@5470
   165
        cursor->driverdata = (void*)X11_CreatePixmapCursor(surface, hot_x, hot_y);
slouken@5470
   166
    } else {
slouken@5470
   167
        SDL_OutOfMemory();
slouken@5470
   168
    }
slouken@5470
   169
slouken@5470
   170
    return cursor;
slouken@5470
   171
}
slouken@5470
   172
slouken@5470
   173
static void
slouken@5470
   174
X11_FreeCursor(SDL_Cursor * cursor)
slouken@5470
   175
{
slouken@5470
   176
    Cursor x11_cursor = (Cursor)cursor->driverdata;
slouken@5470
   177
slouken@5470
   178
    if (x11_cursor != None) {
slouken@5470
   179
        XFreeCursor(GetDisplay(), x11_cursor);
slouken@5470
   180
    }
slouken@5470
   181
    SDL_free(cursor);
slouken@5470
   182
}
slouken@5470
   183
slouken@5470
   184
static int
slouken@5470
   185
X11_ShowCursor(SDL_Cursor * cursor)
slouken@5470
   186
{
slouken@5470
   187
    Cursor x11_cursor = 0;
slouken@5470
   188
slouken@5470
   189
    if (cursor) {
slouken@5470
   190
        x11_cursor = (Cursor)cursor->driverdata;
slouken@5470
   191
    } else {
slouken@5470
   192
        x11_cursor = X11_CreateEmptyCursor();
slouken@5470
   193
    }
slouken@5470
   194
slouken@5470
   195
    /* FIXME: Is there a better way than this? */
slouken@5470
   196
    {
slouken@5470
   197
        SDL_VideoDevice *video = SDL_GetVideoDevice();
slouken@5470
   198
        Display *display = GetDisplay();
slouken@5470
   199
        SDL_Window *window;
slouken@5470
   200
        SDL_WindowData *data;
slouken@5470
   201
slouken@5470
   202
        for (window = video->windows; window; window = window->next) {
slouken@5470
   203
            data = (SDL_WindowData *)window->driverdata;
slouken@5470
   204
            if (x11_cursor != None) {
slouken@5470
   205
                XDefineCursor(display, data->xwindow, x11_cursor);
slouken@5470
   206
            } else {
slouken@5470
   207
                XUndefineCursor(display, data->xwindow);
slouken@5470
   208
            }
slouken@5470
   209
        }
slouken@5470
   210
        XFlush(display);
slouken@5470
   211
    }
slouken@5470
   212
    return 0;
slouken@5470
   213
}
slouken@5470
   214
slouken@5470
   215
static void
slouken@5470
   216
X11_WarpMouse(SDL_Window * window, int x, int y)
slouken@5470
   217
{
slouken@5470
   218
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5470
   219
    Display *display = data->videodata->display;
slouken@5470
   220
slouken@5470
   221
    XWarpPointer(display, None, data->xwindow, 0, 0, 0, 0, x, y);
slouken@5470
   222
    XSync(display, False);
slouken@5470
   223
}
slouken@5470
   224
slouken@5470
   225
static int
slouken@5470
   226
X11_SetRelativeMouseMode(SDL_bool enabled)
slouken@5470
   227
{
slouken@5470
   228
    SDL_Unsupported();
slouken@5470
   229
    return -1;
slouken@5470
   230
}
slouken@5470
   231
slouken@1950
   232
void
slouken@1950
   233
X11_InitMouse(_THIS)
slouken@1950
   234
{
slouken@5470
   235
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5470
   236
slouken@5470
   237
    mouse->CreateCursor = X11_CreateCursor;
slouken@5470
   238
    mouse->ShowCursor = X11_ShowCursor;
slouken@5470
   239
    mouse->FreeCursor = X11_FreeCursor;
slouken@5470
   240
    mouse->WarpMouse = X11_WarpMouse;
slouken@5470
   241
    mouse->SetRelativeMouseMode = X11_SetRelativeMouseMode;
slouken@5470
   242
slouken@5470
   243
    SDL_SetDefaultCursor(X11_CreateDefaultCursor());
slouken@1950
   244
}
slouken@1950
   245
slouken@1950
   246
void
slouken@1950
   247
X11_QuitMouse(_THIS)
slouken@1950
   248
{
slouken@5470
   249
    X11_DestroyEmptyCursor();
slouken@1950
   250
}
slouken@1950
   251
slouken@1950
   252
/* vi: set ts=4 sw=4 expandtab: */