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