src/events/SDL_mouse.c
author Sylvain Becker <sylvain.becker@gmail.com>
Mon, 11 Mar 2019 15:31:46 +0100
changeset 12620 0010b9a064d3
parent 12503 806492103856
child 12651 d188272453ce
permissions -rw-r--r--
SDL_MouseQuit(): clear mouse->cur_cursor (Bug 4530)
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;
sylvain@12620
   610
    mouse->cur_cursor = NULL;
slouken@7493
   611
slouken@7493
   612
    if (mouse->def_cursor && mouse->FreeCursor) {
slouken@7493
   613
        mouse->FreeCursor(mouse->def_cursor);
slouken@11622
   614
        mouse->def_cursor = NULL;
slouken@7493
   615
    }
slouken@7493
   616
slouken@8066
   617
    if (mouse->clickstate) {
slouken@8066
   618
        SDL_free(mouse->clickstate);
slouken@11622
   619
        mouse->clickstate = NULL;
slouken@8066
   620
    }
slouken@8066
   621
slouken@10673
   622
    SDL_DelHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
slouken@10673
   623
                        SDL_MouseNormalSpeedScaleChanged, mouse);
slouken@10673
   624
slouken@10673
   625
    SDL_DelHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
slouken@10673
   626
                        SDL_MouseRelativeSpeedScaleChanged, mouse);
slouken@4465
   627
}
slouken@4465
   628
slouken@6673
   629
Uint32
slouken@4465
   630
SDL_GetMouseState(int *x, int *y)
slouken@4465
   631
{
slouken@5371
   632
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@4465
   633
slouken@4465
   634
    if (x) {
slouken@4465
   635
        *x = mouse->x;
slouken@4465
   636
    }
slouken@4465
   637
    if (y) {
slouken@4465
   638
        *y = mouse->y;
slouken@4465
   639
    }
slouken@4465
   640
    return mouse->buttonstate;
slouken@4465
   641
}
slouken@4465
   642
slouken@6673
   643
Uint32
slouken@4465
   644
SDL_GetRelativeMouseState(int *x, int *y)
slouken@4465
   645
{
slouken@5371
   646
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@4465
   647
slouken@4465
   648
    if (x) {
slouken@4465
   649
        *x = mouse->xdelta;
slouken@4465
   650
    }
slouken@4465
   651
    if (y) {
slouken@4465
   652
        *y = mouse->ydelta;
slouken@4465
   653
    }
slouken@4465
   654
    mouse->xdelta = 0;
slouken@4465
   655
    mouse->ydelta = 0;
slouken@4465
   656
    return mouse->buttonstate;
slouken@4465
   657
}
slouken@4465
   658
icculus@8945
   659
Uint32
icculus@8952
   660
SDL_GetGlobalMouseState(int *x, int *y)
icculus@8945
   661
{
icculus@8945
   662
    SDL_Mouse *mouse = SDL_GetMouse();
icculus@8945
   663
    int tmpx, tmpy;
icculus@8945
   664
icculus@8945
   665
    /* make sure these are never NULL for the backend implementations... */
icculus@8945
   666
    if (!x) {
icculus@8945
   667
        x = &tmpx;
icculus@8945
   668
    }
icculus@8945
   669
    if (!y) {
icculus@8945
   670
        y = &tmpy;
icculus@8945
   671
    }
icculus@8945
   672
icculus@8945
   673
    *x = *y = 0;
icculus@8945
   674
icculus@8952
   675
    if (!mouse->GetGlobalMouseState) {
icculus@8945
   676
        return 0;
icculus@8945
   677
    }
icculus@8945
   678
icculus@8952
   679
    return mouse->GetGlobalMouseState(x, y);
icculus@8945
   680
}
icculus@8945
   681
slouken@4465
   682
void
slouken@3685
   683
SDL_WarpMouseInWindow(SDL_Window * window, int x, int y)
slouken@1895
   684
{
slouken@5371
   685
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@7191
   686
icculus@8953
   687
    if (window == NULL) {
slouken@7191
   688
        window = mouse->focus;
icculus@8944
   689
    }
slouken@7191
   690
icculus@8953
   691
    if (window == NULL) {
slouken@7191
   692
        return;
icculus@8944
   693
    }
slouken@1895
   694
slouken@1895
   695
    if (mouse->WarpMouse) {
slouken@5371
   696
        mouse->WarpMouse(window, x, y);
slouken@1895
   697
    } else {
slouken@6950
   698
        SDL_SendMouseMotion(window, mouse->mouseID, 0, x, y);
slouken@1895
   699
    }
slouken@1895
   700
}
slouken@1895
   701
icculus@9807
   702
int
slouken@8815
   703
SDL_WarpMouseGlobal(int x, int y)
slouken@8815
   704
{
slouken@8815
   705
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@8815
   706
slouken@8815
   707
    if (mouse->WarpMouseGlobal) {
icculus@9807
   708
        return mouse->WarpMouseGlobal(x, y);
slouken@8815
   709
    }
icculus@9807
   710
icculus@9807
   711
    return SDL_Unsupported();
slouken@8815
   712
}
slouken@8815
   713
slouken@8071
   714
static SDL_bool
slouken@8071
   715
ShouldUseRelativeModeWarp(SDL_Mouse *mouse)
slouken@8071
   716
{
slouken@8071
   717
    if (!mouse->SetRelativeMouseMode) {
slouken@12007
   718
        SDL_assert(mouse->WarpMouse);   /* Need this functionality for relative mode warp implementation */
slouken@8071
   719
        return SDL_TRUE;
slouken@8071
   720
    }
slouken@8071
   721
slouken@10499
   722
    return SDL_GetHintBoolean(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, SDL_FALSE);
slouken@8071
   723
}
slouken@8071
   724
slouken@4465
   725
int
slouken@4465
   726
SDL_SetRelativeMouseMode(SDL_bool enabled)
slouken@4465
   727
{
slouken@5371
   728
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7107
   729
    SDL_Window *focusWindow = SDL_GetKeyboardFocus();
slouken@4465
   730
slouken@5406
   731
    if (enabled == mouse->relative_mode) {
slouken@5406
   732
        return 0;
slouken@5406
   733
    }
slouken@5406
   734
jorgen@7107
   735
    if (enabled && focusWindow) {
jorgen@7107
   736
        /* Center it in the focused window to prevent clicks from going through
jorgen@7107
   737
         * to background windows.
jorgen@7107
   738
         */
jorgen@7107
   739
        SDL_SetMouseFocus(focusWindow);
jorgen@7107
   740
        SDL_WarpMouseInWindow(focusWindow, focusWindow->w/2, focusWindow->h/2);
jorgen@7107
   741
    }
jorgen@7107
   742
slouken@8071
   743
    /* Set the relative mode */
slouken@8071
   744
    if (!enabled && mouse->relative_mode_warp) {
slouken@8071
   745
        mouse->relative_mode_warp = SDL_FALSE;
slouken@8071
   746
    } else if (enabled && ShouldUseRelativeModeWarp(mouse)) {
slouken@8071
   747
        mouse->relative_mode_warp = SDL_TRUE;
slouken@8071
   748
    } else if (mouse->SetRelativeMouseMode(enabled) < 0) {
slouken@8269
   749
        if (enabled) {
philipp@8776
   750
            /* Fall back to warp mode if native relative mode failed */
slouken@12024
   751
            if (!mouse->WarpMouse) {
slouken@12024
   752
                return SDL_SetError("No relative mode implementation available");
slouken@12024
   753
            }
slouken@8269
   754
            mouse->relative_mode_warp = SDL_TRUE;
slouken@8269
   755
        }
slouken@5406
   756
    }
slouken@4465
   757
    mouse->relative_mode = enabled;
slouken@10673
   758
    mouse->scale_accum_x = 0.0f;
slouken@10673
   759
    mouse->scale_accum_y = 0.0f;
slouken@4465
   760
slouken@8071
   761
    if (mouse->focus) {
slouken@8071
   762
        SDL_UpdateWindowGrab(mouse->focus);
slouken@8071
   763
slouken@8071
   764
        /* Put the cursor back to where the application expects it */
slouken@8071
   765
        if (!enabled) {
slouken@8071
   766
            SDL_WarpMouseInWindow(mouse->focus, mouse->x, mouse->y);
slouken@8071
   767
        }
slouken@4465
   768
    }
slouken@4465
   769
slouken@8071
   770
    /* Flush pending mouse motion - ideally we would pump events, but that's not always safe */
slouken@5406
   771
    SDL_FlushEvent(SDL_MOUSEMOTION);
slouken@5406
   772
slouken@4465
   773
    /* Update cursor visibility */
slouken@4465
   774
    SDL_SetCursor(NULL);
slouken@4465
   775
slouken@4465
   776
    return 0;
slouken@4465
   777
}
slouken@4465
   778
slouken@4465
   779
SDL_bool
slouken@4465
   780
SDL_GetRelativeMouseMode()
slouken@4465
   781
{
slouken@5371
   782
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@4465
   783
slouken@4465
   784
    return mouse->relative_mode;
slouken@4465
   785
}
slouken@4465
   786
icculus@8955
   787
int
icculus@8955
   788
SDL_CaptureMouse(SDL_bool enabled)
icculus@8955
   789
{
icculus@8955
   790
    SDL_Mouse *mouse = SDL_GetMouse();
icculus@8955
   791
    SDL_Window *focusWindow;
icculus@8955
   792
    SDL_bool isCaptured;
icculus@8955
   793
icculus@8955
   794
    if (!mouse->CaptureMouse) {
icculus@8955
   795
        return SDL_Unsupported();
icculus@8955
   796
    }
icculus@8955
   797
icculus@8955
   798
    focusWindow = SDL_GetKeyboardFocus();
icculus@8955
   799
icculus@8955
   800
    isCaptured = focusWindow && (focusWindow->flags & SDL_WINDOW_MOUSE_CAPTURE);
icculus@8955
   801
    if (isCaptured == enabled) {
icculus@8955
   802
        return 0;  /* already done! */
icculus@8955
   803
    }
icculus@8955
   804
icculus@8955
   805
    if (enabled) {
icculus@8955
   806
        if (!focusWindow) {
icculus@8955
   807
            return SDL_SetError("No window has focus");
icculus@8955
   808
        } else if (mouse->CaptureMouse(focusWindow) == -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
    } else {
icculus@8955
   813
        if (mouse->CaptureMouse(NULL) == -1) {
icculus@8955
   814
            return -1;  /* CaptureMouse() should call SetError */
icculus@8955
   815
        }
icculus@8955
   816
        focusWindow->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
icculus@8955
   817
    }
icculus@8955
   818
icculus@8955
   819
    return 0;
icculus@8955
   820
}
icculus@8955
   821
slouken@1895
   822
SDL_Cursor *
slouken@1895
   823
SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
slouken@1895
   824
                 int w, int h, int hot_x, int hot_y)
slouken@1895
   825
{
slouken@1895
   826
    SDL_Surface *surface;
slouken@1895
   827
    SDL_Cursor *cursor;
slouken@1895
   828
    int x, y;
slouken@1895
   829
    Uint32 *pixel;
slouken@4465
   830
    Uint8 datab = 0, maskb = 0;
slouken@1895
   831
    const Uint32 black = 0xFF000000;
slouken@1895
   832
    const Uint32 white = 0xFFFFFFFF;
slouken@1895
   833
    const Uint32 transparent = 0x00000000;
slouken@1895
   834
slouken@1895
   835
    /* Make sure the width is a multiple of 8 */
slouken@1895
   836
    w = ((w + 7) & ~7);
slouken@1895
   837
slouken@1895
   838
    /* Create the surface from a bitmap */
slouken@5473
   839
    surface = SDL_CreateRGBSurface(0, w, h, 32,
slouken@5473
   840
                                   0x00FF0000,
slouken@5473
   841
                                   0x0000FF00,
slouken@5473
   842
                                   0x000000FF,
slouken@5473
   843
                                   0xFF000000);
slouken@1895
   844
    if (!surface) {
slouken@1895
   845
        return NULL;
slouken@1895
   846
    }
slouken@1895
   847
    for (y = 0; y < h; ++y) {
slouken@1895
   848
        pixel = (Uint32 *) ((Uint8 *) surface->pixels + y * surface->pitch);
slouken@1895
   849
        for (x = 0; x < w; ++x) {
slouken@1895
   850
            if ((x % 8) == 0) {
slouken@1895
   851
                datab = *data++;
slouken@1895
   852
                maskb = *mask++;
slouken@1895
   853
            }
slouken@1895
   854
            if (maskb & 0x80) {
slouken@1895
   855
                *pixel++ = (datab & 0x80) ? black : white;
slouken@1895
   856
            } else {
slouken@1895
   857
                *pixel++ = (datab & 0x80) ? black : transparent;
slouken@1895
   858
            }
slouken@1895
   859
            datab <<= 1;
slouken@1895
   860
            maskb <<= 1;
slouken@1895
   861
        }
slouken@1895
   862
    }
slouken@1895
   863
slouken@5473
   864
    cursor = SDL_CreateColorCursor(surface, hot_x, hot_y);
slouken@5473
   865
slouken@5473
   866
    SDL_FreeSurface(surface);
slouken@5473
   867
slouken@5473
   868
    return cursor;
slouken@5473
   869
}
slouken@5473
   870
slouken@5473
   871
SDL_Cursor *
slouken@5473
   872
SDL_CreateColorCursor(SDL_Surface *surface, int hot_x, int hot_y)
slouken@5473
   873
{
slouken@5473
   874
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5473
   875
    SDL_Surface *temp = NULL;
slouken@5473
   876
    SDL_Cursor *cursor;
slouken@5473
   877
slouken@5473
   878
    if (!surface) {
slouken@5473
   879
        SDL_SetError("Passed NULL cursor surface");
slouken@5473
   880
        return NULL;
slouken@5473
   881
    }
slouken@5473
   882
slouken@5473
   883
    if (!mouse->CreateCursor) {
slouken@5473
   884
        SDL_SetError("Cursors are not currently supported");
slouken@5473
   885
        return NULL;
slouken@5473
   886
    }
slouken@5473
   887
slouken@5473
   888
    /* Sanity check the hot spot */
slouken@5473
   889
    if ((hot_x < 0) || (hot_y < 0) ||
slouken@5473
   890
        (hot_x >= surface->w) || (hot_y >= surface->h)) {
slouken@5473
   891
        SDL_SetError("Cursor hot spot doesn't lie within cursor");
slouken@5473
   892
        return NULL;
slouken@5473
   893
    }
slouken@5473
   894
slouken@5473
   895
    if (surface->format->format != SDL_PIXELFORMAT_ARGB8888) {
slouken@5473
   896
        temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0);
slouken@5473
   897
        if (!temp) {
slouken@5473
   898
            return NULL;
slouken@5473
   899
        }
slouken@5473
   900
        surface = temp;
slouken@5473
   901
    }
slouken@5473
   902
slouken@1895
   903
    cursor = mouse->CreateCursor(surface, hot_x, hot_y);
slouken@1895
   904
    if (cursor) {
slouken@1895
   905
        cursor->next = mouse->cursors;
slouken@1895
   906
        mouse->cursors = cursor;
slouken@1895
   907
    }
slouken@1895
   908
slouken@7720
   909
    SDL_FreeSurface(temp);
slouken@1895
   910
slouken@1895
   911
    return cursor;
slouken@1895
   912
}
slouken@1895
   913
mikesart@6675
   914
SDL_Cursor *
mikesart@6675
   915
SDL_CreateSystemCursor(SDL_SystemCursor id)
mikesart@6675
   916
{
mikesart@6675
   917
    SDL_Mouse *mouse = SDL_GetMouse();
mikesart@6675
   918
    SDL_Cursor *cursor;
mikesart@6675
   919
mikesart@6675
   920
    if (!mouse->CreateSystemCursor) {
mikesart@6675
   921
        SDL_SetError("CreateSystemCursor is not currently supported");
mikesart@6675
   922
        return NULL;
mikesart@6675
   923
    }
mikesart@6675
   924
slouken@7191
   925
    cursor = mouse->CreateSystemCursor(id);
mikesart@6675
   926
    if (cursor) {
mikesart@6675
   927
        cursor->next = mouse->cursors;
mikesart@6675
   928
        mouse->cursors = cursor;
mikesart@6675
   929
    }
mikesart@6675
   930
slouken@7191
   931
    return cursor;
mikesart@6675
   932
}
mikesart@6675
   933
slouken@1895
   934
/* SDL_SetCursor(NULL) can be used to force the cursor redraw,
slouken@1895
   935
   if this is desired for any reason.  This is used when setting
slouken@1895
   936
   the video mode and when the SDL window gains the mouse focus.
slouken@1895
   937
 */
slouken@1895
   938
void
slouken@1895
   939
SDL_SetCursor(SDL_Cursor * cursor)
slouken@1895
   940
{
slouken@5371
   941
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   942
slouken@1895
   943
    /* Set the new cursor */
slouken@1895
   944
    if (cursor) {
slouken@1895
   945
        /* Make sure the cursor is still valid for this mouse */
slouken@5405
   946
        if (cursor != mouse->def_cursor) {
slouken@5405
   947
            SDL_Cursor *found;
slouken@5405
   948
            for (found = mouse->cursors; found; found = found->next) {
slouken@5405
   949
                if (found == cursor) {
slouken@5405
   950
                    break;
slouken@5405
   951
                }
slouken@1895
   952
            }
slouken@5405
   953
            if (!found) {
slouken@5405
   954
                SDL_SetError("Cursor not associated with the current mouse");
slouken@5405
   955
                return;
slouken@5405
   956
            }
slouken@1895
   957
        }
slouken@1895
   958
        mouse->cur_cursor = cursor;
slouken@1895
   959
    } else {
ghostunderscore@6301
   960
        if (mouse->focus) {
ghostunderscore@6301
   961
            cursor = mouse->cur_cursor;
ghostunderscore@6301
   962
        } else {
ghostunderscore@6301
   963
            cursor = mouse->def_cursor;
ghostunderscore@6301
   964
        }
slouken@1895
   965
    }
slouken@1895
   966
slouken@1895
   967
    if (cursor && mouse->cursor_shown && !mouse->relative_mode) {
slouken@1895
   968
        if (mouse->ShowCursor) {
slouken@1895
   969
            mouse->ShowCursor(cursor);
slouken@1895
   970
        }
slouken@1895
   971
    } else {
slouken@1895
   972
        if (mouse->ShowCursor) {
slouken@1895
   973
            mouse->ShowCursor(NULL);
slouken@1895
   974
        }
slouken@1895
   975
    }
slouken@1895
   976
}
slouken@1895
   977
slouken@1895
   978
SDL_Cursor *
slouken@1895
   979
SDL_GetCursor(void)
slouken@1895
   980
{
slouken@5371
   981
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   982
slouken@1895
   983
    if (!mouse) {
slouken@1895
   984
        return NULL;
slouken@1895
   985
    }
slouken@1895
   986
    return mouse->cur_cursor;
slouken@1895
   987
}
slouken@1895
   988
jorgen@7104
   989
SDL_Cursor *
jorgen@7104
   990
SDL_GetDefaultCursor(void)
jorgen@7104
   991
{
jorgen@7104
   992
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7104
   993
jorgen@7104
   994
    if (!mouse) {
jorgen@7104
   995
        return NULL;
jorgen@7104
   996
    }
jorgen@7104
   997
    return mouse->def_cursor;
jorgen@7104
   998
}
jorgen@7104
   999
slouken@1895
  1000
void
slouken@1895
  1001
SDL_FreeCursor(SDL_Cursor * cursor)
slouken@1895
  1002
{
slouken@5371
  1003
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
  1004
    SDL_Cursor *curr, *prev;
slouken@1895
  1005
slouken@1895
  1006
    if (!cursor) {
slouken@1895
  1007
        return;
slouken@1895
  1008
    }
slouken@1895
  1009
slouken@1895
  1010
    if (cursor == mouse->def_cursor) {
slouken@1895
  1011
        return;
slouken@1895
  1012
    }
slouken@1895
  1013
    if (cursor == mouse->cur_cursor) {
slouken@1895
  1014
        SDL_SetCursor(mouse->def_cursor);
slouken@1895
  1015
    }
slouken@1895
  1016
slouken@1895
  1017
    for (prev = NULL, curr = mouse->cursors; curr;
slouken@1895
  1018
         prev = curr, curr = curr->next) {
slouken@1895
  1019
        if (curr == cursor) {
slouken@1895
  1020
            if (prev) {
slouken@1895
  1021
                prev->next = curr->next;
slouken@1895
  1022
            } else {
slouken@1895
  1023
                mouse->cursors = curr->next;
slouken@1895
  1024
            }
slouken@1895
  1025
slouken@1895
  1026
            if (mouse->FreeCursor) {
slouken@1895
  1027
                mouse->FreeCursor(curr);
slouken@1895
  1028
            }
slouken@1895
  1029
            return;
slouken@1895
  1030
        }
slouken@1895
  1031
    }
slouken@1895
  1032
}
slouken@1895
  1033
slouken@1895
  1034
int
slouken@1895
  1035
SDL_ShowCursor(int toggle)
slouken@1895
  1036
{
slouken@5371
  1037
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
  1038
    SDL_bool shown;
slouken@1895
  1039
slouken@1895
  1040
    if (!mouse) {
slouken@1895
  1041
        return 0;
slouken@1895
  1042
    }
slouken@1895
  1043
slouken@1895
  1044
    shown = mouse->cursor_shown;
slouken@1895
  1045
    if (toggle >= 0) {
slouken@1895
  1046
        if (toggle) {
slouken@1895
  1047
            mouse->cursor_shown = SDL_TRUE;
slouken@1895
  1048
        } else {
slouken@1895
  1049
            mouse->cursor_shown = SDL_FALSE;
slouken@1895
  1050
        }
slouken@1895
  1051
        if (mouse->cursor_shown != shown) {
slouken@1895
  1052
            SDL_SetCursor(NULL);
slouken@1895
  1053
        }
slouken@1895
  1054
    }
slouken@1895
  1055
    return shown;
slouken@1895
  1056
}
slouken@1895
  1057
jorgen@7097
  1058
/* vi: set ts=4 sw=4 expandtab: */