src/events/SDL_mouse.c
author Sylvain Becker <sylvain.becker@gmail.com>
Thu, 04 Apr 2019 15:19:00 +0200
changeset 12687 712d84dd14c9
parent 12651 d188272453ce
child 12688 cc45bcb16ef2
permissions -rw-r--r--
Bug 4576: track both FingerId and TrackId
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@12503
     3
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@0
    22
slouken@0
    23
/* General mouse handling code for SDL */
slouken@0
    24
slouken@6666
    25
#include "SDL_assert.h"
slouken@8071
    26
#include "SDL_hints.h"
slouken@8066
    27
#include "SDL_timer.h"
slouken@0
    28
#include "SDL_events.h"
slouken@0
    29
#include "SDL_events_c.h"
slouken@3685
    30
#include "../video/SDL_sysvideo.h"
slouken@12651
    31
#ifdef __WIN32__
slouken@12651
    32
#include "../core/windows/SDL_windows.h"    // For GetDoubleClickTime()
slouken@12651
    33
#endif
slouken@0
    34
gabomdq@7678
    35
/* #define DEBUG_MOUSE */
slouken@0
    36
slouken@5371
    37
/* The mouse state */
slouken@4465
    38
static SDL_Mouse SDL_mouse;
slouken@0
    39
jorgen@7089
    40
static int
jorgen@7089
    41
SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y);
slouken@0
    42
slouken@11284
    43
static void SDLCALL
slouken@12188
    44
SDL_MouseDoubleClickTimeChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
slouken@12188
    45
{
slouken@12188
    46
    SDL_Mouse *mouse = (SDL_Mouse *)userdata;
slouken@12188
    47
slouken@12188
    48
    if (hint && *hint) {
slouken@12188
    49
        mouse->double_click_time = SDL_atoi(hint);
slouken@12188
    50
    } else {
slouken@12188
    51
#ifdef __WIN32__
slouken@12188
    52
        mouse->double_click_time = GetDoubleClickTime();
slouken@12188
    53
#else
slouken@12188
    54
        mouse->double_click_time = 500;
slouken@12188
    55
#endif
slouken@12188
    56
    }
slouken@12188
    57
}
slouken@12188
    58
slouken@12188
    59
static void SDLCALL
slouken@12188
    60
SDL_MouseDoubleClickRadiusChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
slouken@12188
    61
{
slouken@12188
    62
    SDL_Mouse *mouse = (SDL_Mouse *)userdata;
slouken@12188
    63
slouken@12188
    64
    if (hint && *hint) {
slouken@12188
    65
        mouse->double_click_radius = SDL_atoi(hint);
slouken@12188
    66
    } else {
slouken@12188
    67
        mouse->double_click_radius = 32;    /* 32 pixels seems about right for touch interfaces */
slouken@12188
    68
    }
slouken@12188
    69
}
slouken@12188
    70
slouken@12188
    71
static void SDLCALL
slouken@10673
    72
SDL_MouseNormalSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
slouken@10673
    73
{
slouken@10673
    74
    SDL_Mouse *mouse = (SDL_Mouse *)userdata;
slouken@10673
    75
slouken@10676
    76
    if (hint && *hint) {
slouken@10676
    77
        mouse->normal_speed_scale = (float)SDL_atof(hint);
slouken@10676
    78
    } else {
slouken@10676
    79
        mouse->normal_speed_scale = 1.0f;
slouken@10676
    80
    }
slouken@10673
    81
}
slouken@10673
    82
slouken@11284
    83
static void SDLCALL
slouken@10673
    84
SDL_MouseRelativeSpeedScaleChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
slouken@10673
    85
{
slouken@10673
    86
    SDL_Mouse *mouse = (SDL_Mouse *)userdata;
slouken@10673
    87
slouken@10676
    88
    if (hint && *hint) {
slouken@10676
    89
        mouse->relative_speed_scale = (float)SDL_atof(hint);
slouken@10676
    90
    } else {
slouken@10676
    91
        mouse->relative_speed_scale = 1.0f;
slouken@10676
    92
    }
slouken@10673
    93
}
slouken@10673
    94
slouken@11284
    95
static void SDLCALL
slouken@11182
    96
SDL_TouchMouseEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
slouken@11182
    97
{
slouken@11182
    98
    SDL_Mouse *mouse = (SDL_Mouse *)userdata;
slouken@11182
    99
slouken@11182
   100
    if (hint && (*hint == '0' || SDL_strcasecmp(hint, "false") == 0)) {
slouken@11182
   101
        mouse->touch_mouse_events = SDL_FALSE;
slouken@11182
   102
    } else {
slouken@11182
   103
        mouse->touch_mouse_events = SDL_TRUE;
slouken@11182
   104
    }
slouken@11182
   105
}
slouken@11182
   106
slouken@0
   107
/* Public functions */
slouken@1895
   108
int
slouken@1895
   109
SDL_MouseInit(void)
slouken@0
   110
{
slouken@5376
   111
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5376
   112
slouken@11607
   113
    SDL_zerop(mouse);
slouken@11607
   114
slouken@12188
   115
    SDL_AddHintCallback(SDL_HINT_MOUSE_DOUBLE_CLICK_TIME,
slouken@12188
   116
                        SDL_MouseDoubleClickTimeChanged, mouse);
slouken@12188
   117
slouken@12188
   118
    SDL_AddHintCallback(SDL_HINT_MOUSE_DOUBLE_CLICK_RADIUS,
slouken@12188
   119
                        SDL_MouseDoubleClickRadiusChanged, mouse);
slouken@12188
   120
slouken@10673
   121
    SDL_AddHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
slouken@10673
   122
                        SDL_MouseNormalSpeedScaleChanged, mouse);
slouken@10673
   123
slouken@10673
   124
    SDL_AddHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
slouken@10673
   125
                        SDL_MouseRelativeSpeedScaleChanged, mouse);
slouken@10673
   126
slouken@11182
   127
    SDL_AddHintCallback(SDL_HINT_TOUCH_MOUSE_EVENTS,
slouken@11182
   128
                        SDL_TouchMouseEventsChanged, mouse);
slouken@11182
   129
slouken@5376
   130
    mouse->cursor_shown = SDL_TRUE;
slouken@5376
   131
slouken@1895
   132
    return (0);
slouken@1123
   133
}
slouken@0
   134
slouken@5405
   135
void
slouken@5405
   136
SDL_SetDefaultCursor(SDL_Cursor * cursor)
slouken@5405
   137
{
slouken@5405
   138
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5405
   139
slouken@5405
   140
    mouse->def_cursor = cursor;
slouken@5405
   141
    if (!mouse->cur_cursor) {
slouken@5405
   142
        SDL_SetCursor(cursor);
slouken@5405
   143
    }
slouken@5405
   144
}
slouken@5405
   145
slouken@5371
   146
SDL_Mouse *
slouken@5371
   147
SDL_GetMouse(void)
slouken@5371
   148
{
slouken@5371
   149
    return &SDL_mouse;
slouken@5371
   150
}
slouken@5371
   151
slouken@4465
   152
SDL_Window *
slouken@4465
   153
SDL_GetMouseFocus(void)
slouken@2710
   154
{
slouken@5371
   155
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@2940
   156
slouken@4465
   157
    return mouse->focus;
slouken@0
   158
}
slouken@0
   159
slouken@10609
   160
#if 0
slouken@1895
   161
void
slouken@6666
   162
SDL_ResetMouse(void)
slouken@6666
   163
{
slouken@6666
   164
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@6666
   165
    Uint8 i;
slouken@6666
   166
slouken@6666
   167
#ifdef DEBUG_MOUSE
slouken@6666
   168
    printf("Resetting mouse\n");
slouken@6666
   169
#endif
slouken@6666
   170
    for (i = 1; i <= sizeof(mouse->buttonstate)*8; ++i) {
slouken@6666
   171
        if (mouse->buttonstate & SDL_BUTTON(i)) {
slouken@6950
   172
            SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_RELEASED, i);
slouken@6666
   173
        }
slouken@6666
   174
    }
slouken@6666
   175
    SDL_assert(mouse->buttonstate == 0);
slouken@6666
   176
}
slouken@10609
   177
#endif
slouken@6666
   178
slouken@6666
   179
void
slouken@4465
   180
SDL_SetMouseFocus(SDL_Window * window)
slouken@0
   181
{
slouken@5371
   182
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   183
slouken@4465
   184
    if (mouse->focus == window) {
slouken@1895
   185
        return;
slouken@1895
   186
    }
slouken@1895
   187
slouken@6829
   188
    /* Actually, this ends up being a bad idea, because most operating
slouken@6829
   189
       systems have an implicit grab when you press the mouse button down
slouken@6829
   190
       so you can drag things out of the window and then get the mouse up
slouken@6829
   191
       when it happens.  So, #if 0...
slouken@6829
   192
    */
slouken@6829
   193
#if 0
slouken@6666
   194
    if (mouse->focus && !window) {
slouken@6666
   195
        /* We won't get anymore mouse messages, so reset mouse state */
slouken@6666
   196
        SDL_ResetMouse();
slouken@6666
   197
    }
slouken@6829
   198
#endif
slouken@6666
   199
slouken@1895
   200
    /* See if the current window has lost focus */
slouken@1895
   201
    if (mouse->focus) {
slouken@4465
   202
        SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_LEAVE, 0, 0);
slouken@1895
   203
    }
slouken@1895
   204
slouken@3685
   205
    mouse->focus = window;
slouken@11258
   206
    mouse->has_position = SDL_FALSE;
slouken@1895
   207
slouken@1895
   208
    if (mouse->focus) {
slouken@4465
   209
        SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_ENTER, 0, 0);
slouken@1895
   210
    }
slouken@6666
   211
slouken@6666
   212
    /* Update cursor visibility */
slouken@6666
   213
    SDL_SetCursor(NULL);
slouken@6666
   214
}
slouken@6666
   215
slouken@6666
   216
/* Check to see if we need to synthesize focus events */
slouken@6666
   217
static SDL_bool
slouken@6673
   218
SDL_UpdateMouseFocus(SDL_Window * window, int x, int y, Uint32 buttonstate)
slouken@6666
   219
{
slouken@6666
   220
    SDL_Mouse *mouse = SDL_GetMouse();
icculus@8927
   221
    SDL_bool inWindow = SDL_TRUE;
slouken@6666
   222
slouken@8983
   223
    if (window && ((window->flags & SDL_WINDOW_MOUSE_CAPTURE) == 0)) {
icculus@8927
   224
        int w, h;
icculus@8927
   225
        SDL_GetWindowSize(window, &w, &h);
icculus@8927
   226
        if (x < 0 || y < 0 || x >= w || y >= h) {
icculus@8927
   227
            inWindow = SDL_FALSE;
icculus@8927
   228
        }
slouken@6666
   229
    }
slouken@6667
   230
icculus@8783
   231
/* Linux doesn't give you mouse events outside your window unless you grab
icculus@8783
   232
   the pointer.
icculus@8783
   233
icculus@8783
   234
   Windows doesn't give you mouse events outside your window unless you call
icculus@8783
   235
   SetCapture().
icculus@8783
   236
icculus@8783
   237
   Both of these are slightly scary changes, so for now we'll punt and if the
icculus@8783
   238
   mouse leaves the window you'll lose mouse focus and reset button state.
icculus@8783
   239
*/
icculus@8783
   240
#ifdef SUPPORT_DRAG_OUTSIDE_WINDOW
icculus@8783
   241
    if (!inWindow && !buttonstate) {
icculus@8783
   242
#else
slouken@6667
   243
    if (!inWindow) {
icculus@8783
   244
#endif
slouken@6666
   245
        if (window == mouse->focus) {
slouken@6666
   246
#ifdef DEBUG_MOUSE
jorgen@7089
   247
            printf("Mouse left window, synthesizing move & focus lost event\n");
slouken@6666
   248
#endif
jorgen@7089
   249
            SDL_PrivateSendMouseMotion(window, mouse->mouseID, 0, x, y);
slouken@6666
   250
            SDL_SetMouseFocus(NULL);
slouken@6666
   251
        }
slouken@6666
   252
        return SDL_FALSE;
slouken@6666
   253
    }
slouken@6666
   254
slouken@6666
   255
    if (window != mouse->focus) {
slouken@6666
   256
#ifdef DEBUG_MOUSE
slouken@11258
   257
        printf("Mouse entered window, synthesizing focus gain & move event\n");
slouken@6666
   258
#endif
slouken@11258
   259
        SDL_SetMouseFocus(window);
slouken@11258
   260
        SDL_PrivateSendMouseMotion(window, mouse->mouseID, 0, x, y);
slouken@6666
   261
    }
slouken@6666
   262
    return SDL_TRUE;
slouken@1895
   263
}
slouken@1895
   264
slouken@1895
   265
int
slouken@6950
   266
SDL_SendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y)
slouken@1895
   267
{
jorgen@7089
   268
    if (window && !relative) {
jorgen@7089
   269
        SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7089
   270
        if (!SDL_UpdateMouseFocus(window, x, y, mouse->buttonstate)) {
jorgen@7089
   271
            return 0;
jorgen@7089
   272
        }
jorgen@7089
   273
    }
jorgen@7089
   274
jorgen@7089
   275
    return SDL_PrivateSendMouseMotion(window, mouseID, relative, x, y);
jorgen@7089
   276
}
jorgen@7089
   277
jorgen@7089
   278
static int
slouken@10673
   279
GetScaledMouseDelta(float scale, int value, float *accum)
slouken@10673
   280
{
slouken@10673
   281
    if (scale != 1.0f) {
slouken@10673
   282
        *accum += scale * value;
slouken@10673
   283
        if (*accum >= 0.0f) {
slouken@10673
   284
            value = (int)SDL_floor(*accum);
slouken@10673
   285
        } else {
slouken@10673
   286
            value = (int)SDL_ceil(*accum);
slouken@10673
   287
        }
slouken@10673
   288
        *accum -= value;
slouken@10673
   289
    }
slouken@10673
   290
    return value;
slouken@10673
   291
}
slouken@10673
   292
slouken@10673
   293
static int
jorgen@7089
   294
SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y)
jorgen@7089
   295
{
slouken@5371
   296
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   297
    int posted;
slouken@1895
   298
    int xrel;
slouken@1895
   299
    int yrel;
slouken@1895
   300
slouken@11258
   301
    if (mouseID != SDL_TOUCH_MOUSEID && mouse->relative_mode_warp) {
slouken@8071
   302
        int center_x = 0, center_y = 0;
slouken@8071
   303
        SDL_GetWindowSize(window, &center_x, &center_y);
slouken@8071
   304
        center_x /= 2;
slouken@8071
   305
        center_y /= 2;
slouken@8071
   306
        if (x == center_x && y == center_y) {
slouken@8071
   307
            mouse->last_x = center_x;
slouken@8071
   308
            mouse->last_y = center_y;
slouken@8071
   309
            return 0;
slouken@8071
   310
        }
slouken@8071
   311
        SDL_WarpMouseInWindow(window, center_x, center_y);
slouken@8071
   312
    }
slouken@8071
   313
slouken@2842
   314
    if (relative) {
slouken@10673
   315
        if (mouse->relative_mode) {
slouken@10673
   316
            x = GetScaledMouseDelta(mouse->relative_speed_scale, x, &mouse->scale_accum_x);
slouken@10673
   317
            y = GetScaledMouseDelta(mouse->relative_speed_scale, y, &mouse->scale_accum_y);
slouken@10673
   318
        } else {
slouken@10673
   319
            x = GetScaledMouseDelta(mouse->normal_speed_scale, x, &mouse->scale_accum_x);
slouken@10673
   320
            y = GetScaledMouseDelta(mouse->normal_speed_scale, y, &mouse->scale_accum_y);
slouken@10673
   321
        }
slouken@2842
   322
        xrel = x;
slouken@2842
   323
        yrel = y;
slouken@8071
   324
        x = (mouse->last_x + xrel);
slouken@8071
   325
        y = (mouse->last_y + yrel);
slouken@2842
   326
    } else {
slouken@2860
   327
        xrel = x - mouse->last_x;
slouken@2860
   328
        yrel = y - mouse->last_y;
slouken@2842
   329
    }
slouken@2710
   330
slouken@1895
   331
    /* Drop events that don't change state */
slouken@1895
   332
    if (!xrel && !yrel) {
slouken@6666
   333
#ifdef DEBUG_MOUSE
slouken@1895
   334
        printf("Mouse event didn't change state - dropped!\n");
slouken@1895
   335
#endif
slouken@1895
   336
        return 0;
slouken@1895
   337
    }
slouken@1895
   338
slouken@11258
   339
    /* Ignore relative motion when first positioning the mouse */
slouken@11258
   340
    if (!mouse->has_position) {
slouken@11258
   341
        xrel = 0;
slouken@11258
   342
        yrel = 0;
slouken@11258
   343
        mouse->has_position = SDL_TRUE;
slouken@11258
   344
    }
slouken@11258
   345
slouken@11258
   346
    /* Ignore relative motion positioning the first touch */
slouken@11258
   347
    if (mouseID == SDL_TOUCH_MOUSEID && !mouse->buttonstate) {
slouken@11258
   348
        xrel = 0;
slouken@11258
   349
        yrel = 0;
slouken@11258
   350
    }
slouken@11258
   351
slouken@2710
   352
    /* Update internal mouse coordinates */
slouken@8071
   353
    if (!mouse->relative_mode) {
slouken@1895
   354
        mouse->x = x;
slouken@1895
   355
        mouse->y = y;
slouken@2710
   356
    } else {
slouken@2860
   357
        mouse->x += xrel;
slouken@2860
   358
        mouse->y += yrel;
slouken@2860
   359
    }
slouken@2849
   360
icculus@8927
   361
    /* make sure that the pointers find themselves inside the windows,
icculus@8927
   362
       unless we have the mouse captured. */
slouken@8983
   363
    if (window && ((window->flags & SDL_WINDOW_MOUSE_CAPTURE) == 0)) {
icculus@8927
   364
        int x_max = 0, y_max = 0;
slouken@2849
   365
icculus@10780
   366
        /* !!! FIXME: shouldn't this be (window) instead of (mouse->focus)? */
icculus@8927
   367
        SDL_GetWindowSize(mouse->focus, &x_max, &y_max);
icculus@8927
   368
        --x_max;
icculus@8927
   369
        --y_max;
slouken@2860
   370
icculus@8927
   371
        if (mouse->x > x_max) {
icculus@8927
   372
            mouse->x = x_max;
icculus@8927
   373
        }
icculus@8927
   374
        if (mouse->x < 0) {
icculus@8927
   375
            mouse->x = 0;
icculus@8927
   376
        }
icculus@8927
   377
icculus@8927
   378
        if (mouse->y > y_max) {
icculus@8927
   379
            mouse->y = y_max;
icculus@8927
   380
        }
icculus@8927
   381
        if (mouse->y < 0) {
icculus@8927
   382
            mouse->y = 0;
icculus@8927
   383
        }
slouken@2860
   384
    }
slouken@2860
   385
slouken@1895
   386
    mouse->xdelta += xrel;
slouken@1895
   387
    mouse->ydelta += yrel;
slouken@1895
   388
slouken@1895
   389
    /* Move the mouse cursor, if needed */
slouken@1895
   390
    if (mouse->cursor_shown && !mouse->relative_mode &&
slouken@1895
   391
        mouse->MoveCursor && mouse->cur_cursor) {
slouken@1895
   392
        mouse->MoveCursor(mouse->cur_cursor);
slouken@1895
   393
    }
slouken@1895
   394
slouken@1895
   395
    /* Post the event, if desired */
slouken@1895
   396
    posted = 0;
slouken@4465
   397
    if (SDL_GetEventState(SDL_MOUSEMOTION) == SDL_ENABLE) {
slouken@1895
   398
        SDL_Event event;
slouken@1895
   399
        event.motion.type = SDL_MOUSEMOTION;
slouken@4465
   400
        event.motion.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@6950
   401
        event.motion.which = mouseID;
slouken@1895
   402
        event.motion.state = mouse->buttonstate;
slouken@1895
   403
        event.motion.x = mouse->x;
slouken@1895
   404
        event.motion.y = mouse->y;
slouken@1895
   405
        event.motion.xrel = xrel;
slouken@1895
   406
        event.motion.yrel = yrel;
slouken@1895
   407
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   408
    }
slouken@9448
   409
    if (relative) {
slouken@9448
   410
        mouse->last_x = mouse->x;
slouken@9448
   411
        mouse->last_y = mouse->y;
slouken@9448
   412
    } else {
slouken@9448
   413
        /* Use unclamped values if we're getting events outside the window */
slouken@9448
   414
        mouse->last_x = x;
slouken@9448
   415
        mouse->last_y = y;
slouken@9448
   416
    }
slouken@1895
   417
    return posted;
slouken@1895
   418
}
slouken@1895
   419
slouken@8066
   420
static SDL_MouseClickState *GetMouseClickState(SDL_Mouse *mouse, Uint8 button)
slouken@8066
   421
{
slouken@8066
   422
    if (button >= mouse->num_clickstates) {
slouken@8066
   423
        int i, count = button + 1;
philipp@9334
   424
        SDL_MouseClickState *clickstate = (SDL_MouseClickState *)SDL_realloc(mouse->clickstate, count * sizeof(*mouse->clickstate));
philipp@9334
   425
        if (!clickstate) {
slouken@8066
   426
            return NULL;
slouken@8066
   427
        }
philipp@9334
   428
        mouse->clickstate = clickstate;
slouken@8066
   429
slouken@8066
   430
        for (i = mouse->num_clickstates; i < count; ++i) {
slouken@8066
   431
            SDL_zero(mouse->clickstate[i]);
slouken@8066
   432
        }
slouken@8066
   433
        mouse->num_clickstates = count;
slouken@8066
   434
    }
slouken@8066
   435
    return &mouse->clickstate[button];
slouken@8066
   436
}
slouken@8066
   437
slime73@10366
   438
static int
slime73@10366
   439
SDL_PrivateSendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button, int clicks)
slouken@1895
   440
{
slouken@5371
   441
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   442
    int posted;
slouken@4429
   443
    Uint32 type;
slouken@6673
   444
    Uint32 buttonstate = mouse->buttonstate;
slouken@4484
   445
slouken@1895
   446
    /* Figure out which event to perform */
slouken@1895
   447
    switch (state) {
slouken@1895
   448
    case SDL_PRESSED:
slouken@1895
   449
        type = SDL_MOUSEBUTTONDOWN;
slouken@6666
   450
        buttonstate |= SDL_BUTTON(button);
slouken@1895
   451
        break;
slouken@1895
   452
    case SDL_RELEASED:
slouken@1895
   453
        type = SDL_MOUSEBUTTONUP;
slouken@6666
   454
        buttonstate &= ~SDL_BUTTON(button);
slouken@1895
   455
        break;
slouken@1895
   456
    default:
slouken@1895
   457
        /* Invalid state -- bail */
slouken@1895
   458
        return 0;
slouken@1895
   459
    }
slouken@1895
   460
slouken@6666
   461
    /* We do this after calculating buttonstate so button presses gain focus */
slouken@6666
   462
    if (window && state == SDL_PRESSED) {
slouken@6666
   463
        SDL_UpdateMouseFocus(window, mouse->x, mouse->y, buttonstate);
slouken@6666
   464
    }
slouken@6666
   465
slouken@6666
   466
    if (buttonstate == mouse->buttonstate) {
slouken@6666
   467
        /* Ignore this event, no state change */
slouken@6666
   468
        return 0;
slouken@6666
   469
    }
slouken@6666
   470
    mouse->buttonstate = buttonstate;
slouken@6666
   471
slime73@10366
   472
    if (clicks < 0) {
slime73@10366
   473
        SDL_MouseClickState *clickstate = GetMouseClickState(mouse, button);
philipp@10390
   474
        if (clickstate) {
philipp@10390
   475
            if (state == SDL_PRESSED) {
philipp@10390
   476
                Uint32 now = SDL_GetTicks();
slouken@8066
   477
slouken@12188
   478
                if (SDL_TICKS_PASSED(now, clickstate->last_timestamp + mouse->double_click_time) ||
slouken@12188
   479
                    SDL_abs(mouse->x - clickstate->last_x) > mouse->double_click_radius ||
slouken@12188
   480
                    SDL_abs(mouse->y - clickstate->last_y) > mouse->double_click_radius) {
philipp@10390
   481
                    clickstate->click_count = 0;
philipp@10390
   482
                }
philipp@10390
   483
                clickstate->last_timestamp = now;
philipp@10390
   484
                clickstate->last_x = mouse->x;
philipp@10390
   485
                clickstate->last_y = mouse->y;
philipp@10390
   486
                if (clickstate->click_count < 255) {
philipp@10390
   487
                    ++clickstate->click_count;
philipp@10390
   488
                }
slouken@8066
   489
            }
philipp@10390
   490
            clicks = clickstate->click_count;
philipp@10390
   491
        } else {
philipp@10390
   492
            clicks = 1;
slouken@8066
   493
        }
slouken@8066
   494
    }
slouken@8066
   495
slouken@1895
   496
    /* Post the event, if desired */
slouken@1895
   497
    posted = 0;
slouken@4429
   498
    if (SDL_GetEventState(type) == SDL_ENABLE) {
slouken@1895
   499
        SDL_Event event;
slouken@1895
   500
        event.type = type;
slouken@6950
   501
        event.button.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@6950
   502
        event.button.which = mouseID;
slouken@1895
   503
        event.button.state = state;
slouken@1895
   504
        event.button.button = button;
slime73@10366
   505
        event.button.clicks = (Uint8) SDL_min(clicks, 255);
slouken@1895
   506
        event.button.x = mouse->x;
slouken@1895
   507
        event.button.y = mouse->y;
slouken@1895
   508
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   509
    }
slouken@6666
   510
slouken@6666
   511
    /* We do this after dispatching event so button releases can lose focus */
slouken@6666
   512
    if (window && state == SDL_RELEASED) {
slouken@6666
   513
        SDL_UpdateMouseFocus(window, mouse->x, mouse->y, buttonstate);
slouken@6666
   514
    }
sylvain@12687
   515
slime73@10366
   516
    return posted;
slime73@10366
   517
}
slouken@6666
   518
slime73@10366
   519
int
slime73@10366
   520
SDL_SendMouseButtonClicks(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button, int clicks)
slime73@10366
   521
{
slime73@10366
   522
    clicks = SDL_max(clicks, 0);
slime73@10366
   523
    return SDL_PrivateSendMouseButton(window, mouseID, state, button, clicks);
slime73@10366
   524
}
slime73@10366
   525
slime73@10366
   526
int
slime73@10366
   527
SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button)
slime73@10366
   528
{
slime73@10366
   529
    return SDL_PrivateSendMouseButton(window, mouseID, state, button, -1);
slouken@1895
   530
}
slouken@1895
   531
slouken@1895
   532
int
slouken@11300
   533
SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, float x, float y, SDL_MouseWheelDirection direction)
slouken@1895
   534
{
slouken@5371
   535
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   536
    int posted;
slouken@11300
   537
    int integral_x, integral_y;
slouken@1895
   538
slouken@4484
   539
    if (window) {
slouken@4484
   540
        SDL_SetMouseFocus(window);
slouken@4484
   541
    }
slouken@4484
   542
slouken@12466
   543
    if (x == 0.0f && y == 0.0f) {
slouken@1895
   544
        return 0;
slouken@1895
   545
    }
slouken@1895
   546
slouken@11300
   547
    mouse->accumulated_wheel_x += x;
slouken@11300
   548
    if (mouse->accumulated_wheel_x > 0) {
slouken@11300
   549
        integral_x = (int)SDL_floor(mouse->accumulated_wheel_x);
slouken@11300
   550
    } else if (mouse->accumulated_wheel_x < 0) {
slouken@11300
   551
        integral_x = (int)SDL_ceil(mouse->accumulated_wheel_x);
slouken@11300
   552
    } else {
slouken@11300
   553
        integral_x = 0;
slouken@11300
   554
    }
slouken@11300
   555
    mouse->accumulated_wheel_x -= integral_x;
slouken@11300
   556
slouken@11300
   557
    mouse->accumulated_wheel_y += y;
slouken@11300
   558
    if (mouse->accumulated_wheel_y > 0) {
slouken@11300
   559
        integral_y = (int)SDL_floor(mouse->accumulated_wheel_y);
slouken@11300
   560
    } else if (mouse->accumulated_wheel_y < 0) {
slouken@11300
   561
        integral_y = (int)SDL_ceil(mouse->accumulated_wheel_y);
slouken@11300
   562
    } else {
slouken@11300
   563
        integral_y = 0;
slouken@11300
   564
    }
slouken@11300
   565
    mouse->accumulated_wheel_y -= integral_y;
slouken@11300
   566
slouken@1895
   567
    /* Post the event, if desired */
slouken@1895
   568
    posted = 0;
slouken@4429
   569
    if (SDL_GetEventState(SDL_MOUSEWHEEL) == SDL_ENABLE) {
slouken@1895
   570
        SDL_Event event;
slouken@1895
   571
        event.type = SDL_MOUSEWHEEL;
slouken@4465
   572
        event.wheel.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@6950
   573
        event.wheel.which = mouseID;
slouken@11300
   574
#if 0 /* Uncomment this when it goes in for SDL 2.1 */
slouken@11300
   575
        event.wheel.preciseX = x;
slouken@11300
   576
        event.wheel.preciseY = y;
slouken@11300
   577
#endif
slouken@11300
   578
        event.wheel.x = integral_x;
slouken@11300
   579
        event.wheel.y = integral_y;
urkle@9257
   580
        event.wheel.direction = (Uint32)direction;
slouken@1895
   581
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   582
    }
slouken@1895
   583
    return posted;
slouken@1895
   584
}
slouken@1895
   585
slouken@1895
   586
void
slouken@4465
   587
SDL_MouseQuit(void)
slouken@4465
   588
{
slouken@7493
   589
    SDL_Cursor *cursor, *next;
slouken@7493
   590
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@7493
   591
slouken@9161
   592
    if (mouse->CaptureMouse) {
slouken@9161
   593
        SDL_CaptureMouse(SDL_FALSE);
slouken@9161
   594
    }
slouken@8049
   595
    SDL_SetRelativeMouseMode(SDL_FALSE);
slouken@7493
   596
    SDL_ShowCursor(1);
slouken@7493
   597
slouken@7493
   598
    cursor = mouse->cursors;
slouken@7493
   599
    while (cursor) {
slouken@7493
   600
        next = cursor->next;
slouken@7493
   601
        SDL_FreeCursor(cursor);
slouken@7493
   602
        cursor = next;
slouken@7493
   603
    }
slouken@11622
   604
    mouse->cursors = NULL;
sylvain@12620
   605
    mouse->cur_cursor = NULL;
slouken@7493
   606
slouken@7493
   607
    if (mouse->def_cursor && mouse->FreeCursor) {
slouken@7493
   608
        mouse->FreeCursor(mouse->def_cursor);
slouken@11622
   609
        mouse->def_cursor = NULL;
slouken@7493
   610
    }
slouken@7493
   611
slouken@8066
   612
    if (mouse->clickstate) {
slouken@8066
   613
        SDL_free(mouse->clickstate);
slouken@11622
   614
        mouse->clickstate = NULL;
slouken@8066
   615
    }
slouken@8066
   616
slouken@10673
   617
    SDL_DelHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
slouken@10673
   618
                        SDL_MouseNormalSpeedScaleChanged, mouse);
slouken@10673
   619
slouken@10673
   620
    SDL_DelHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
slouken@10673
   621
                        SDL_MouseRelativeSpeedScaleChanged, mouse);
slouken@4465
   622
}
slouken@4465
   623
slouken@6673
   624
Uint32
slouken@4465
   625
SDL_GetMouseState(int *x, int *y)
slouken@4465
   626
{
slouken@5371
   627
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@4465
   628
slouken@4465
   629
    if (x) {
slouken@4465
   630
        *x = mouse->x;
slouken@4465
   631
    }
slouken@4465
   632
    if (y) {
slouken@4465
   633
        *y = mouse->y;
slouken@4465
   634
    }
slouken@4465
   635
    return mouse->buttonstate;
slouken@4465
   636
}
slouken@4465
   637
slouken@6673
   638
Uint32
slouken@4465
   639
SDL_GetRelativeMouseState(int *x, int *y)
slouken@4465
   640
{
slouken@5371
   641
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@4465
   642
slouken@4465
   643
    if (x) {
slouken@4465
   644
        *x = mouse->xdelta;
slouken@4465
   645
    }
slouken@4465
   646
    if (y) {
slouken@4465
   647
        *y = mouse->ydelta;
slouken@4465
   648
    }
slouken@4465
   649
    mouse->xdelta = 0;
slouken@4465
   650
    mouse->ydelta = 0;
slouken@4465
   651
    return mouse->buttonstate;
slouken@4465
   652
}
slouken@4465
   653
icculus@8945
   654
Uint32
icculus@8952
   655
SDL_GetGlobalMouseState(int *x, int *y)
icculus@8945
   656
{
icculus@8945
   657
    SDL_Mouse *mouse = SDL_GetMouse();
icculus@8945
   658
    int tmpx, tmpy;
icculus@8945
   659
icculus@8945
   660
    /* make sure these are never NULL for the backend implementations... */
icculus@8945
   661
    if (!x) {
icculus@8945
   662
        x = &tmpx;
icculus@8945
   663
    }
icculus@8945
   664
    if (!y) {
icculus@8945
   665
        y = &tmpy;
icculus@8945
   666
    }
icculus@8945
   667
icculus@8945
   668
    *x = *y = 0;
icculus@8945
   669
icculus@8952
   670
    if (!mouse->GetGlobalMouseState) {
icculus@8945
   671
        return 0;
icculus@8945
   672
    }
icculus@8945
   673
icculus@8952
   674
    return mouse->GetGlobalMouseState(x, y);
icculus@8945
   675
}
icculus@8945
   676
slouken@4465
   677
void
slouken@3685
   678
SDL_WarpMouseInWindow(SDL_Window * window, int x, int y)
slouken@1895
   679
{
slouken@5371
   680
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@7191
   681
icculus@8953
   682
    if (window == NULL) {
slouken@7191
   683
        window = mouse->focus;
icculus@8944
   684
    }
slouken@7191
   685
icculus@8953
   686
    if (window == NULL) {
slouken@7191
   687
        return;
icculus@8944
   688
    }
slouken@1895
   689
slouken@1895
   690
    if (mouse->WarpMouse) {
slouken@5371
   691
        mouse->WarpMouse(window, x, y);
slouken@1895
   692
    } else {
slouken@6950
   693
        SDL_SendMouseMotion(window, mouse->mouseID, 0, x, y);
slouken@1895
   694
    }
slouken@1895
   695
}
slouken@1895
   696
icculus@9807
   697
int
slouken@8815
   698
SDL_WarpMouseGlobal(int x, int y)
slouken@8815
   699
{
slouken@8815
   700
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@8815
   701
slouken@8815
   702
    if (mouse->WarpMouseGlobal) {
icculus@9807
   703
        return mouse->WarpMouseGlobal(x, y);
slouken@8815
   704
    }
icculus@9807
   705
icculus@9807
   706
    return SDL_Unsupported();
slouken@8815
   707
}
slouken@8815
   708
slouken@8071
   709
static SDL_bool
slouken@8071
   710
ShouldUseRelativeModeWarp(SDL_Mouse *mouse)
slouken@8071
   711
{
slouken@8071
   712
    if (!mouse->SetRelativeMouseMode) {
slouken@12007
   713
        SDL_assert(mouse->WarpMouse);   /* Need this functionality for relative mode warp implementation */
slouken@8071
   714
        return SDL_TRUE;
slouken@8071
   715
    }
slouken@8071
   716
slouken@10499
   717
    return SDL_GetHintBoolean(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, SDL_FALSE);
slouken@8071
   718
}
slouken@8071
   719
slouken@4465
   720
int
slouken@4465
   721
SDL_SetRelativeMouseMode(SDL_bool enabled)
slouken@4465
   722
{
slouken@5371
   723
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7107
   724
    SDL_Window *focusWindow = SDL_GetKeyboardFocus();
slouken@4465
   725
slouken@5406
   726
    if (enabled == mouse->relative_mode) {
slouken@5406
   727
        return 0;
slouken@5406
   728
    }
slouken@5406
   729
jorgen@7107
   730
    if (enabled && focusWindow) {
jorgen@7107
   731
        /* Center it in the focused window to prevent clicks from going through
jorgen@7107
   732
         * to background windows.
jorgen@7107
   733
         */
jorgen@7107
   734
        SDL_SetMouseFocus(focusWindow);
jorgen@7107
   735
        SDL_WarpMouseInWindow(focusWindow, focusWindow->w/2, focusWindow->h/2);
jorgen@7107
   736
    }
jorgen@7107
   737
slouken@8071
   738
    /* Set the relative mode */
slouken@8071
   739
    if (!enabled && mouse->relative_mode_warp) {
slouken@8071
   740
        mouse->relative_mode_warp = SDL_FALSE;
slouken@8071
   741
    } else if (enabled && ShouldUseRelativeModeWarp(mouse)) {
slouken@8071
   742
        mouse->relative_mode_warp = SDL_TRUE;
slouken@8071
   743
    } else if (mouse->SetRelativeMouseMode(enabled) < 0) {
slouken@8269
   744
        if (enabled) {
philipp@8776
   745
            /* Fall back to warp mode if native relative mode failed */
slouken@12024
   746
            if (!mouse->WarpMouse) {
slouken@12024
   747
                return SDL_SetError("No relative mode implementation available");
slouken@12024
   748
            }
slouken@8269
   749
            mouse->relative_mode_warp = SDL_TRUE;
slouken@8269
   750
        }
slouken@5406
   751
    }
slouken@4465
   752
    mouse->relative_mode = enabled;
slouken@10673
   753
    mouse->scale_accum_x = 0.0f;
slouken@10673
   754
    mouse->scale_accum_y = 0.0f;
slouken@4465
   755
slouken@8071
   756
    if (mouse->focus) {
slouken@8071
   757
        SDL_UpdateWindowGrab(mouse->focus);
slouken@8071
   758
slouken@8071
   759
        /* Put the cursor back to where the application expects it */
slouken@8071
   760
        if (!enabled) {
slouken@8071
   761
            SDL_WarpMouseInWindow(mouse->focus, mouse->x, mouse->y);
slouken@8071
   762
        }
slouken@4465
   763
    }
slouken@4465
   764
slouken@8071
   765
    /* Flush pending mouse motion - ideally we would pump events, but that's not always safe */
slouken@5406
   766
    SDL_FlushEvent(SDL_MOUSEMOTION);
slouken@5406
   767
slouken@4465
   768
    /* Update cursor visibility */
slouken@4465
   769
    SDL_SetCursor(NULL);
slouken@4465
   770
slouken@4465
   771
    return 0;
slouken@4465
   772
}
slouken@4465
   773
slouken@4465
   774
SDL_bool
slouken@4465
   775
SDL_GetRelativeMouseMode()
slouken@4465
   776
{
slouken@5371
   777
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@4465
   778
slouken@4465
   779
    return mouse->relative_mode;
slouken@4465
   780
}
slouken@4465
   781
icculus@8955
   782
int
icculus@8955
   783
SDL_CaptureMouse(SDL_bool enabled)
icculus@8955
   784
{
icculus@8955
   785
    SDL_Mouse *mouse = SDL_GetMouse();
icculus@8955
   786
    SDL_Window *focusWindow;
icculus@8955
   787
    SDL_bool isCaptured;
icculus@8955
   788
icculus@8955
   789
    if (!mouse->CaptureMouse) {
icculus@8955
   790
        return SDL_Unsupported();
icculus@8955
   791
    }
icculus@8955
   792
icculus@8955
   793
    focusWindow = SDL_GetKeyboardFocus();
icculus@8955
   794
icculus@8955
   795
    isCaptured = focusWindow && (focusWindow->flags & SDL_WINDOW_MOUSE_CAPTURE);
icculus@8955
   796
    if (isCaptured == enabled) {
icculus@8955
   797
        return 0;  /* already done! */
icculus@8955
   798
    }
icculus@8955
   799
icculus@8955
   800
    if (enabled) {
icculus@8955
   801
        if (!focusWindow) {
icculus@8955
   802
            return SDL_SetError("No window has focus");
icculus@8955
   803
        } else if (mouse->CaptureMouse(focusWindow) == -1) {
icculus@8955
   804
            return -1;  /* CaptureMouse() should call SetError */
icculus@8955
   805
        }
icculus@8955
   806
        focusWindow->flags |= SDL_WINDOW_MOUSE_CAPTURE;
icculus@8955
   807
    } else {
icculus@8955
   808
        if (mouse->CaptureMouse(NULL) == -1) {
icculus@8955
   809
            return -1;  /* CaptureMouse() should call SetError */
icculus@8955
   810
        }
icculus@8955
   811
        focusWindow->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
icculus@8955
   812
    }
icculus@8955
   813
icculus@8955
   814
    return 0;
icculus@8955
   815
}
icculus@8955
   816
slouken@1895
   817
SDL_Cursor *
slouken@1895
   818
SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
slouken@1895
   819
                 int w, int h, int hot_x, int hot_y)
slouken@1895
   820
{
slouken@1895
   821
    SDL_Surface *surface;
slouken@1895
   822
    SDL_Cursor *cursor;
slouken@1895
   823
    int x, y;
slouken@1895
   824
    Uint32 *pixel;
slouken@4465
   825
    Uint8 datab = 0, maskb = 0;
slouken@1895
   826
    const Uint32 black = 0xFF000000;
slouken@1895
   827
    const Uint32 white = 0xFFFFFFFF;
slouken@1895
   828
    const Uint32 transparent = 0x00000000;
slouken@1895
   829
slouken@1895
   830
    /* Make sure the width is a multiple of 8 */
slouken@1895
   831
    w = ((w + 7) & ~7);
slouken@1895
   832
slouken@1895
   833
    /* Create the surface from a bitmap */
slouken@5473
   834
    surface = SDL_CreateRGBSurface(0, w, h, 32,
slouken@5473
   835
                                   0x00FF0000,
slouken@5473
   836
                                   0x0000FF00,
slouken@5473
   837
                                   0x000000FF,
slouken@5473
   838
                                   0xFF000000);
slouken@1895
   839
    if (!surface) {
slouken@1895
   840
        return NULL;
slouken@1895
   841
    }
slouken@1895
   842
    for (y = 0; y < h; ++y) {
slouken@1895
   843
        pixel = (Uint32 *) ((Uint8 *) surface->pixels + y * surface->pitch);
slouken@1895
   844
        for (x = 0; x < w; ++x) {
slouken@1895
   845
            if ((x % 8) == 0) {
slouken@1895
   846
                datab = *data++;
slouken@1895
   847
                maskb = *mask++;
slouken@1895
   848
            }
slouken@1895
   849
            if (maskb & 0x80) {
slouken@1895
   850
                *pixel++ = (datab & 0x80) ? black : white;
slouken@1895
   851
            } else {
slouken@1895
   852
                *pixel++ = (datab & 0x80) ? black : transparent;
slouken@1895
   853
            }
slouken@1895
   854
            datab <<= 1;
slouken@1895
   855
            maskb <<= 1;
slouken@1895
   856
        }
slouken@1895
   857
    }
slouken@1895
   858
slouken@5473
   859
    cursor = SDL_CreateColorCursor(surface, hot_x, hot_y);
slouken@5473
   860
slouken@5473
   861
    SDL_FreeSurface(surface);
slouken@5473
   862
slouken@5473
   863
    return cursor;
slouken@5473
   864
}
slouken@5473
   865
slouken@5473
   866
SDL_Cursor *
slouken@5473
   867
SDL_CreateColorCursor(SDL_Surface *surface, int hot_x, int hot_y)
slouken@5473
   868
{
slouken@5473
   869
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5473
   870
    SDL_Surface *temp = NULL;
slouken@5473
   871
    SDL_Cursor *cursor;
slouken@5473
   872
slouken@5473
   873
    if (!surface) {
slouken@5473
   874
        SDL_SetError("Passed NULL cursor surface");
slouken@5473
   875
        return NULL;
slouken@5473
   876
    }
slouken@5473
   877
slouken@5473
   878
    if (!mouse->CreateCursor) {
slouken@5473
   879
        SDL_SetError("Cursors are not currently supported");
slouken@5473
   880
        return NULL;
slouken@5473
   881
    }
slouken@5473
   882
slouken@5473
   883
    /* Sanity check the hot spot */
slouken@5473
   884
    if ((hot_x < 0) || (hot_y < 0) ||
slouken@5473
   885
        (hot_x >= surface->w) || (hot_y >= surface->h)) {
slouken@5473
   886
        SDL_SetError("Cursor hot spot doesn't lie within cursor");
slouken@5473
   887
        return NULL;
slouken@5473
   888
    }
slouken@5473
   889
slouken@5473
   890
    if (surface->format->format != SDL_PIXELFORMAT_ARGB8888) {
slouken@5473
   891
        temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0);
slouken@5473
   892
        if (!temp) {
slouken@5473
   893
            return NULL;
slouken@5473
   894
        }
slouken@5473
   895
        surface = temp;
slouken@5473
   896
    }
slouken@5473
   897
slouken@1895
   898
    cursor = mouse->CreateCursor(surface, hot_x, hot_y);
slouken@1895
   899
    if (cursor) {
slouken@1895
   900
        cursor->next = mouse->cursors;
slouken@1895
   901
        mouse->cursors = cursor;
slouken@1895
   902
    }
slouken@1895
   903
slouken@7720
   904
    SDL_FreeSurface(temp);
slouken@1895
   905
slouken@1895
   906
    return cursor;
slouken@1895
   907
}
slouken@1895
   908
mikesart@6675
   909
SDL_Cursor *
mikesart@6675
   910
SDL_CreateSystemCursor(SDL_SystemCursor id)
mikesart@6675
   911
{
mikesart@6675
   912
    SDL_Mouse *mouse = SDL_GetMouse();
mikesart@6675
   913
    SDL_Cursor *cursor;
mikesart@6675
   914
mikesart@6675
   915
    if (!mouse->CreateSystemCursor) {
mikesart@6675
   916
        SDL_SetError("CreateSystemCursor is not currently supported");
mikesart@6675
   917
        return NULL;
mikesart@6675
   918
    }
mikesart@6675
   919
slouken@7191
   920
    cursor = mouse->CreateSystemCursor(id);
mikesart@6675
   921
    if (cursor) {
mikesart@6675
   922
        cursor->next = mouse->cursors;
mikesart@6675
   923
        mouse->cursors = cursor;
mikesart@6675
   924
    }
mikesart@6675
   925
slouken@7191
   926
    return cursor;
mikesart@6675
   927
}
mikesart@6675
   928
slouken@1895
   929
/* SDL_SetCursor(NULL) can be used to force the cursor redraw,
slouken@1895
   930
   if this is desired for any reason.  This is used when setting
slouken@1895
   931
   the video mode and when the SDL window gains the mouse focus.
slouken@1895
   932
 */
slouken@1895
   933
void
slouken@1895
   934
SDL_SetCursor(SDL_Cursor * cursor)
slouken@1895
   935
{
slouken@5371
   936
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   937
slouken@1895
   938
    /* Set the new cursor */
slouken@1895
   939
    if (cursor) {
slouken@1895
   940
        /* Make sure the cursor is still valid for this mouse */
slouken@5405
   941
        if (cursor != mouse->def_cursor) {
slouken@5405
   942
            SDL_Cursor *found;
slouken@5405
   943
            for (found = mouse->cursors; found; found = found->next) {
slouken@5405
   944
                if (found == cursor) {
slouken@5405
   945
                    break;
slouken@5405
   946
                }
slouken@1895
   947
            }
slouken@5405
   948
            if (!found) {
slouken@5405
   949
                SDL_SetError("Cursor not associated with the current mouse");
slouken@5405
   950
                return;
slouken@5405
   951
            }
slouken@1895
   952
        }
slouken@1895
   953
        mouse->cur_cursor = cursor;
slouken@1895
   954
    } else {
ghostunderscore@6301
   955
        if (mouse->focus) {
ghostunderscore@6301
   956
            cursor = mouse->cur_cursor;
ghostunderscore@6301
   957
        } else {
ghostunderscore@6301
   958
            cursor = mouse->def_cursor;
ghostunderscore@6301
   959
        }
slouken@1895
   960
    }
slouken@1895
   961
slouken@1895
   962
    if (cursor && mouse->cursor_shown && !mouse->relative_mode) {
slouken@1895
   963
        if (mouse->ShowCursor) {
slouken@1895
   964
            mouse->ShowCursor(cursor);
slouken@1895
   965
        }
slouken@1895
   966
    } else {
slouken@1895
   967
        if (mouse->ShowCursor) {
slouken@1895
   968
            mouse->ShowCursor(NULL);
slouken@1895
   969
        }
slouken@1895
   970
    }
slouken@1895
   971
}
slouken@1895
   972
slouken@1895
   973
SDL_Cursor *
slouken@1895
   974
SDL_GetCursor(void)
slouken@1895
   975
{
slouken@5371
   976
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   977
slouken@1895
   978
    if (!mouse) {
slouken@1895
   979
        return NULL;
slouken@1895
   980
    }
slouken@1895
   981
    return mouse->cur_cursor;
slouken@1895
   982
}
slouken@1895
   983
jorgen@7104
   984
SDL_Cursor *
jorgen@7104
   985
SDL_GetDefaultCursor(void)
jorgen@7104
   986
{
jorgen@7104
   987
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7104
   988
jorgen@7104
   989
    if (!mouse) {
jorgen@7104
   990
        return NULL;
jorgen@7104
   991
    }
jorgen@7104
   992
    return mouse->def_cursor;
jorgen@7104
   993
}
jorgen@7104
   994
slouken@1895
   995
void
slouken@1895
   996
SDL_FreeCursor(SDL_Cursor * cursor)
slouken@1895
   997
{
slouken@5371
   998
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   999
    SDL_Cursor *curr, *prev;
slouken@1895
  1000
slouken@1895
  1001
    if (!cursor) {
slouken@1895
  1002
        return;
slouken@1895
  1003
    }
slouken@1895
  1004
slouken@1895
  1005
    if (cursor == mouse->def_cursor) {
slouken@1895
  1006
        return;
slouken@1895
  1007
    }
slouken@1895
  1008
    if (cursor == mouse->cur_cursor) {
slouken@1895
  1009
        SDL_SetCursor(mouse->def_cursor);
slouken@1895
  1010
    }
slouken@1895
  1011
slouken@1895
  1012
    for (prev = NULL, curr = mouse->cursors; curr;
slouken@1895
  1013
         prev = curr, curr = curr->next) {
slouken@1895
  1014
        if (curr == cursor) {
slouken@1895
  1015
            if (prev) {
slouken@1895
  1016
                prev->next = curr->next;
slouken@1895
  1017
            } else {
slouken@1895
  1018
                mouse->cursors = curr->next;
slouken@1895
  1019
            }
slouken@1895
  1020
slouken@1895
  1021
            if (mouse->FreeCursor) {
slouken@1895
  1022
                mouse->FreeCursor(curr);
slouken@1895
  1023
            }
slouken@1895
  1024
            return;
slouken@1895
  1025
        }
slouken@1895
  1026
    }
slouken@1895
  1027
}
slouken@1895
  1028
slouken@1895
  1029
int
slouken@1895
  1030
SDL_ShowCursor(int toggle)
slouken@1895
  1031
{
slouken@5371
  1032
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
  1033
    SDL_bool shown;
slouken@1895
  1034
slouken@1895
  1035
    if (!mouse) {
slouken@1895
  1036
        return 0;
slouken@1895
  1037
    }
slouken@1895
  1038
slouken@1895
  1039
    shown = mouse->cursor_shown;
slouken@1895
  1040
    if (toggle >= 0) {
slouken@1895
  1041
        if (toggle) {
slouken@1895
  1042
            mouse->cursor_shown = SDL_TRUE;
slouken@1895
  1043
        } else {
slouken@1895
  1044
            mouse->cursor_shown = SDL_FALSE;
slouken@1895
  1045
        }
slouken@1895
  1046
        if (mouse->cursor_shown != shown) {
slouken@1895
  1047
            SDL_SetCursor(NULL);
slouken@1895
  1048
        }
slouken@1895
  1049
    }
slouken@1895
  1050
    return shown;
slouken@1895
  1051
}
slouken@1895
  1052
jorgen@7097
  1053
/* vi: set ts=4 sw=4 expandtab: */