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