src/video/x11/SDL_x11mouse.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 17 Apr 2013 01:35:10 -0700
changeset 7071 82f17e656125
parent 6885 700f1b25f77f
child 7191 75360622e65f
permissions -rw-r--r--
Fixed bug 1804 - Memory leak issue in src/video/x11/SDL_x11mouse.c Nitz In SDL_x11mouse.c file there is function named static Cursor X11_CreatePixmapCursor(SDL_Surface * surface, int hot_x, int hot_y) { // Some code data_bits = SDL_calloc(1, surface->h * width_bytes); mask_bits = SDL_calloc(1, surface->h * width_bytes); if (!data_bits || !mask_bits) { SDL_OutOfMemory(); return None; } // Some code } Here is the problem in if statement, suppose if !data_bits is false and !mask_bits is true then, data_bits will go out of scope and leaks the memory it points to. Solution is that data_bits and mask_bits should be checked separately, not by using OR operator.
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
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6834
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
1950
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
     7
  arising from the use of this software.
1950
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
    11
  freely, subject to the following restrictions:
1950
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5505
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
1950
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#include "SDL_config.h"
5471
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    22
5481
22dfc3958dc3 Fixed so code will compile with SDL_config_minimal.h
Sam Lantinga <slouken@libsdl.org>
parents: 5471
diff changeset
    23
#if SDL_VIDEO_DRIVER_X11
22dfc3958dc3 Fixed so code will compile with SDL_config_minimal.h
Sam Lantinga <slouken@libsdl.org>
parents: 5471
diff changeset
    24
6675
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
    25
#include <X11/cursorfont.h>
5471
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    26
#include "SDL_assert.h"
1950
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
#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
    28
#include "SDL_x11mouse.h"
6316
a89c79a3f0cd 1.Moved all xinput2 functionality to its own file
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6311
diff changeset
    29
#include "SDL_x11xinput2.h"
1950
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
#include "../../events/SDL_mouse_c.h"
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    32
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    33
/* FIXME: Find a better place to put this... */
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    34
static Cursor x11_empty_cursor = None;
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
static Display *
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    37
GetDisplay(void)
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    38
{
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    39
    return ((SDL_VideoData *)SDL_GetVideoDevice()->driverdata)->display;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    40
}
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    41
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    42
static Cursor
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    43
X11_CreateEmptyCursor()
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    44
{
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    45
    if (x11_empty_cursor == None) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    46
        Display *display = GetDisplay();
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    47
        char data[1];
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    48
        XColor color;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    49
        Pixmap pixmap;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    50
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    51
        SDL_zero(data);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    52
        color.red = color.green = color.blue = 0;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    53
        pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    54
                                       data, 1, 1);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    55
        if (pixmap) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    56
            x11_empty_cursor = XCreatePixmapCursor(display, pixmap, pixmap,
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    57
                                                   &color, &color, 0, 0);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    58
            XFreePixmap(display, pixmap);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    59
        }
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    60
    }
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    61
    return x11_empty_cursor;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    62
}
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    63
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    64
static void
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    65
X11_DestroyEmptyCursor(void)
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
    if (x11_empty_cursor != None) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    68
        XFreeCursor(GetDisplay(), x11_empty_cursor);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    69
        x11_empty_cursor = None;
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
}
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
static SDL_Cursor *
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    74
X11_CreateDefaultCursor()
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    75
{
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    76
    SDL_Cursor *cursor;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    77
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    78
    cursor = SDL_calloc(1, sizeof(*cursor));
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    79
    if (cursor) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    80
        /* None is used to indicate the default cursor */
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    81
        cursor->driverdata = (void*)None;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    82
    } else {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    83
        SDL_OutOfMemory();
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    84
    }
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    85
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    86
    return cursor;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    87
}
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    88
5471
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    89
#if SDL_VIDEO_DRIVER_X11_XCURSOR
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    90
static Cursor
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    91
X11_CreateXCursorCursor(SDL_Surface * surface, int hot_x, int hot_y)
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    92
{
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    93
    Display *display = GetDisplay();
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    94
    Cursor cursor = None;
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    95
    XcursorImage *image;
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    96
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    97
    image = XcursorImageCreate(surface->w, surface->h);
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    98
    if (!image) {
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
    99
        SDL_OutOfMemory();
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   100
        return None;
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   101
    }
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   102
    image->xhot = hot_x;
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   103
    image->yhot = hot_y;
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   104
    image->delay = 0;
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   105
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   106
    SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   107
    SDL_assert(surface->pitch == surface->w * 4);
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   108
    SDL_memcpy(image->pixels, surface->pixels, surface->h * surface->pitch);
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   109
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   110
    cursor = XcursorImageLoadCursor(display, image);
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   111
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   112
    XcursorImageDestroy(image);
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   113
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   114
    return cursor;
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   115
}
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   116
#endif /* SDL_VIDEO_DRIVER_X11_XCURSOR */
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   117
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   118
static Cursor
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   119
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
   120
{
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   121
    Display *display = GetDisplay();
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   122
    XColor fg, bg;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   123
    Cursor cursor = None;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   124
    Uint32 *ptr;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   125
    Uint8 *data_bits, *mask_bits;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   126
    Pixmap data_pixmap, mask_pixmap;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   127
    int x, y;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   128
    unsigned int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   129
    unsigned int width_bytes = ((surface->w + 7) & ~7) / 8;
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
    data_bits = SDL_calloc(1, surface->h * width_bytes);
7071
82f17e656125 Fixed bug 1804 - Memory leak issue in src/video/x11/SDL_x11mouse.c
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   132
    if (!data_bits) {
82f17e656125 Fixed bug 1804 - Memory leak issue in src/video/x11/SDL_x11mouse.c
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   133
        SDL_OutOfMemory();
82f17e656125 Fixed bug 1804 - Memory leak issue in src/video/x11/SDL_x11mouse.c
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   134
        return None;
82f17e656125 Fixed bug 1804 - Memory leak issue in src/video/x11/SDL_x11mouse.c
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   135
    }
82f17e656125 Fixed bug 1804 - Memory leak issue in src/video/x11/SDL_x11mouse.c
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   136
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   137
    mask_bits = SDL_calloc(1, surface->h * width_bytes);
7071
82f17e656125 Fixed bug 1804 - Memory leak issue in src/video/x11/SDL_x11mouse.c
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   138
    if (!mask_bits) {
82f17e656125 Fixed bug 1804 - Memory leak issue in src/video/x11/SDL_x11mouse.c
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   139
        SDL_free(data_bits);
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   140
        SDL_OutOfMemory();
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   141
        return None;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   142
    }
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   143
5471
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   144
    /* Code below assumes ARGB pixel format */
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   145
    SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   146
5981
75caa8a7d559 Fixed a whole slew of compiler warnings that -Wall exposed.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   147
    rfg = gfg = bfg = rbg = gbg = bbg = fgBits = bgBits = 0;
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   148
    for (y = 0; y < surface->h; ++y) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   149
        ptr = (Uint32 *)((Uint8 *)surface->pixels + y * surface->pitch);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   150
        for (x = 0; x < surface->w; ++x) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   151
            int alpha = (*ptr >> 24) & 0xff;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   152
            int red   = (*ptr >> 16) & 0xff;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   153
            int green = (*ptr >> 8) & 0xff;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   154
            int blue  = (*ptr >> 0) & 0xff;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   155
            if (alpha > 25) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   156
                mask_bits[y * width_bytes + x / 8] |= (0x01 << (x % 8));
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
                if ((red + green + blue) > 0x40) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   159
                    fgBits++;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   160
                    rfg += red;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   161
                    gfg += green;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   162
                    bfg += blue;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   163
                    data_bits[y * width_bytes + x / 8] |= (0x01 << (x % 8));
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   164
                } else {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   165
                    bgBits++;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   166
                    rbg += red;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   167
                    gbg += green;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   168
                    bbg += blue;
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
            }
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   171
            ++ptr;
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
    }
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   174
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   175
    if (fgBits) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   176
        fg.red   = rfg * 257 / fgBits;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   177
        fg.green = gfg * 257 / fgBits;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   178
        fg.blue  = bfg * 257 / fgBits;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   179
    }
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   180
    else fg.red = fg.green = fg.blue = 0;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   181
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   182
    if (bgBits) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   183
        bg.red   = rbg * 257 / bgBits;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   184
        bg.green = gbg * 257 / bgBits;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   185
        bg.blue  = bbg * 257 / bgBits;
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
    else bg.red = bg.green = bg.blue = 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
    data_pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
5505
c4bb4c8ea6fd Fixed signed/unsigned warning.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
   190
                                        (char*)data_bits,
c4bb4c8ea6fd Fixed signed/unsigned warning.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
   191
                                        surface->w, surface->h);
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   192
    mask_pixmap = XCreateBitmapFromData(display, DefaultRootWindow(display),
5505
c4bb4c8ea6fd Fixed signed/unsigned warning.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
   193
                                        (char*)mask_bits,
c4bb4c8ea6fd Fixed signed/unsigned warning.
Sam Lantinga <slouken@libsdl.org>
parents: 5481
diff changeset
   194
                                        surface->w, surface->h);
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   195
    cursor = XCreatePixmapCursor(display, data_pixmap, mask_pixmap,
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   196
                                 &fg, &bg, hot_x, hot_y);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   197
	XFreePixmap(display, data_pixmap);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   198
	XFreePixmap(display, mask_pixmap);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   199
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   200
    return cursor;
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
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   203
static SDL_Cursor *
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   204
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
   205
{
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   206
    SDL_Cursor *cursor;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   207
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   208
    cursor = SDL_calloc(1, sizeof(*cursor));
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   209
    if (cursor) {
5471
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   210
        Cursor x11_cursor = None;
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   211
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   212
#if SDL_VIDEO_DRIVER_X11_XCURSOR
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   213
        if (SDL_X11_HAVE_XCURSOR) {
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   214
            x11_cursor = X11_CreateXCursorCursor(surface, hot_x, hot_y);
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   215
        }
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   216
#endif
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   217
        if (x11_cursor == None) {
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   218
            x11_cursor = X11_CreatePixmapCursor(surface, hot_x, hot_y);
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   219
        }
179adad3ae6f Added support for the Xcursor library for color cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5470
diff changeset
   220
        cursor->driverdata = (void*)x11_cursor;
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   221
    } else {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   222
        SDL_OutOfMemory();
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
    return cursor;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   226
}
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   227
6675
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   228
static SDL_Cursor *
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   229
X11_CreateSystemCursor(SDL_SystemCursor id)
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   230
{
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   231
    SDL_Cursor *cursor;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   232
    unsigned int shape;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   233
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   234
    switch(id)
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   235
    {
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   236
    default:
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   237
        SDL_assert(0);
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   238
        return NULL;
6834
dce4e1d1eca3 Use left facing arrow instead of right facing arrow for system cursor.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6817
diff changeset
   239
	// X Font Cursors reference:
dce4e1d1eca3 Use left facing arrow instead of right facing arrow for system cursor.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6817
diff changeset
   240
	//   http://tronche.com/gui/x/xlib/appendix/b/
dce4e1d1eca3 Use left facing arrow instead of right facing arrow for system cursor.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6817
diff changeset
   241
    case SDL_SYSTEM_CURSOR_ARROW:     shape = XC_left_ptr; break;
6675
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   242
    case SDL_SYSTEM_CURSOR_IBEAM:     shape = XC_xterm; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   243
    case SDL_SYSTEM_CURSOR_WAIT:      shape = XC_watch; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   244
    case SDL_SYSTEM_CURSOR_CROSSHAIR: shape = XC_tcross; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   245
    case SDL_SYSTEM_CURSOR_WAITARROW: shape = XC_watch; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   246
    case SDL_SYSTEM_CURSOR_SIZENWSE:  shape = XC_fleur; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   247
    case SDL_SYSTEM_CURSOR_SIZENESW:  shape = XC_fleur; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   248
    case SDL_SYSTEM_CURSOR_SIZEWE:    shape = XC_sb_h_double_arrow; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   249
    case SDL_SYSTEM_CURSOR_SIZENS:    shape = XC_sb_v_double_arrow; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   250
    case SDL_SYSTEM_CURSOR_SIZEALL:   shape = XC_fleur; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   251
    case SDL_SYSTEM_CURSOR_NO:        shape = XC_pirate; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   252
    case SDL_SYSTEM_CURSOR_HAND:      shape = XC_hand2; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   253
    }
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   254
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   255
    cursor = SDL_calloc(1, sizeof(*cursor));
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   256
    if (cursor) {
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   257
        Cursor x11_cursor;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   258
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   259
        x11_cursor = XCreateFontCursor(GetDisplay(), shape);
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   260
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   261
        cursor->driverdata = (void*)x11_cursor;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   262
    } else {
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   263
        SDL_OutOfMemory();
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   264
    }
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   265
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   266
    return cursor;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   267
}
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   268
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   269
static void
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   270
X11_FreeCursor(SDL_Cursor * cursor)
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   271
{
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   272
    Cursor x11_cursor = (Cursor)cursor->driverdata;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   273
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   274
    if (x11_cursor != None) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   275
        XFreeCursor(GetDisplay(), x11_cursor);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   276
    }
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   277
    SDL_free(cursor);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   278
}
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   279
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   280
static int
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   281
X11_ShowCursor(SDL_Cursor * cursor)
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   282
{
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   283
    Cursor x11_cursor = 0;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   284
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   285
    if (cursor) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   286
        x11_cursor = (Cursor)cursor->driverdata;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   287
    } else {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   288
        x11_cursor = X11_CreateEmptyCursor();
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   289
    }
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   290
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   291
    /* FIXME: Is there a better way than this? */
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   292
    {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   293
        SDL_VideoDevice *video = SDL_GetVideoDevice();
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   294
        Display *display = GetDisplay();
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   295
        SDL_Window *window;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   296
        SDL_WindowData *data;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   297
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   298
        for (window = video->windows; window; window = window->next) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   299
            data = (SDL_WindowData *)window->driverdata;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   300
            if (x11_cursor != None) {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   301
                XDefineCursor(display, data->xwindow, x11_cursor);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   302
            } else {
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   303
                XUndefineCursor(display, data->xwindow);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   304
            }
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   305
        }
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   306
        XFlush(display);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   307
    }
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   308
    return 0;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   309
}
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   310
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   311
static void
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   312
X11_WarpMouse(SDL_Window * window, int x, int y)
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   313
{
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   314
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   315
    Display *display = data->videodata->display;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   316
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   317
    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
   318
    XSync(display, False);
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   319
}
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   320
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   321
static int
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   322
X11_SetRelativeMouseMode(SDL_bool enabled)
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   323
{
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   324
#if SDL_VIDEO_DRIVER_X11_XINPUT2
6316
a89c79a3f0cd 1.Moved all xinput2 functionality to its own file
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6311
diff changeset
   325
    if(X11_Xinput2IsInitialized())
a89c79a3f0cd 1.Moved all xinput2 functionality to its own file
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6311
diff changeset
   326
        return 0;
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   327
#else
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   328
    SDL_Unsupported();
6316
a89c79a3f0cd 1.Moved all xinput2 functionality to its own file
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6311
diff changeset
   329
#endif
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   330
    return -1;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   331
}
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   332
1950
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   333
void
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   334
X11_InitMouse(_THIS)
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   335
{
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   336
    SDL_Mouse *mouse = SDL_GetMouse();
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   337
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   338
    mouse->CreateCursor = X11_CreateCursor;
6675
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6316
diff changeset
   339
	mouse->CreateSystemCursor = X11_CreateSystemCursor;
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   340
    mouse->ShowCursor = X11_ShowCursor;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   341
    mouse->FreeCursor = X11_FreeCursor;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   342
    mouse->WarpMouse = X11_WarpMouse;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   343
    mouse->SetRelativeMouseMode = X11_SetRelativeMouseMode;
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   344
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   345
    SDL_SetDefaultCursor(X11_CreateDefaultCursor());
1950
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   346
}
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   347
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   348
void
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   349
X11_QuitMouse(_THIS)
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   350
{
5470
8f5e10ec4faf Implemented X11 cursor support.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
   351
    X11_DestroyEmptyCursor();
1950
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   352
}
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   353
5481
22dfc3958dc3 Fixed so code will compile with SDL_config_minimal.h
Sam Lantinga <slouken@libsdl.org>
parents: 5471
diff changeset
   354
#endif /* SDL_VIDEO_DRIVER_X11 */
22dfc3958dc3 Fixed so code will compile with SDL_config_minimal.h
Sam Lantinga <slouken@libsdl.org>
parents: 5471
diff changeset
   355
1950
a344e42bce3b Started work on the new X11 driver.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   356
/* vi: set ts=4 sw=4 expandtab: */