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