src/events/SDL_mouse.c
author Sylvain Becker <sylvain.becker@gmail.com>
Tue, 09 Jul 2019 11:46:42 +0200
changeset 12934 d03900e991a2
parent 12929 d6c5eb7a0afb
child 12943 49190e92b7d1
permissions -rw-r--r--
SDL_Mouse/Touch: discard synthetic events when hints are not set.
Those are generated/flagged by platform layer.
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
sylvain@12934
   341
    /* SDL_HINT_TOUCH_MOUSE_EVENTS: if not set, discard synthetic mouse events coming from platform layer */
sylvain@12934
   342
    if (mouse->touch_mouse_events == 0) {
sylvain@12934
   343
        if (mouseID == SDL_TOUCH_MOUSEID) {
sylvain@12934
   344
            return 0;
sylvain@12934
   345
        }
sylvain@12934
   346
    }
sylvain@12934
   347
slouken@11258
   348
    if (mouseID != SDL_TOUCH_MOUSEID && mouse->relative_mode_warp) {
slouken@8071
   349
        int center_x = 0, center_y = 0;
slouken@8071
   350
        SDL_GetWindowSize(window, &center_x, &center_y);
slouken@8071
   351
        center_x /= 2;
slouken@8071
   352
        center_y /= 2;
slouken@8071
   353
        if (x == center_x && y == center_y) {
slouken@8071
   354
            mouse->last_x = center_x;
slouken@8071
   355
            mouse->last_y = center_y;
slouken@8071
   356
            return 0;
slouken@8071
   357
        }
slouken@8071
   358
        SDL_WarpMouseInWindow(window, center_x, center_y);
slouken@8071
   359
    }
slouken@8071
   360
slouken@2842
   361
    if (relative) {
slouken@10673
   362
        if (mouse->relative_mode) {
slouken@10673
   363
            x = GetScaledMouseDelta(mouse->relative_speed_scale, x, &mouse->scale_accum_x);
slouken@10673
   364
            y = GetScaledMouseDelta(mouse->relative_speed_scale, y, &mouse->scale_accum_y);
slouken@10673
   365
        } else {
slouken@10673
   366
            x = GetScaledMouseDelta(mouse->normal_speed_scale, x, &mouse->scale_accum_x);
slouken@10673
   367
            y = GetScaledMouseDelta(mouse->normal_speed_scale, y, &mouse->scale_accum_y);
slouken@10673
   368
        }
slouken@2842
   369
        xrel = x;
slouken@2842
   370
        yrel = y;
slouken@8071
   371
        x = (mouse->last_x + xrel);
slouken@8071
   372
        y = (mouse->last_y + yrel);
slouken@2842
   373
    } else {
slouken@2860
   374
        xrel = x - mouse->last_x;
slouken@2860
   375
        yrel = y - mouse->last_y;
slouken@2842
   376
    }
slouken@2710
   377
slouken@1895
   378
    /* Drop events that don't change state */
slouken@1895
   379
    if (!xrel && !yrel) {
slouken@6666
   380
#ifdef DEBUG_MOUSE
slouken@1895
   381
        printf("Mouse event didn't change state - dropped!\n");
slouken@1895
   382
#endif
slouken@1895
   383
        return 0;
slouken@1895
   384
    }
slouken@1895
   385
slouken@11258
   386
    /* Ignore relative motion when first positioning the mouse */
slouken@11258
   387
    if (!mouse->has_position) {
slouken@11258
   388
        xrel = 0;
slouken@11258
   389
        yrel = 0;
slouken@11258
   390
        mouse->has_position = SDL_TRUE;
slouken@11258
   391
    }
slouken@11258
   392
slouken@11258
   393
    /* Ignore relative motion positioning the first touch */
slouken@11258
   394
    if (mouseID == SDL_TOUCH_MOUSEID && !mouse->buttonstate) {
slouken@11258
   395
        xrel = 0;
slouken@11258
   396
        yrel = 0;
slouken@11258
   397
    }
slouken@11258
   398
slouken@2710
   399
    /* Update internal mouse coordinates */
slouken@8071
   400
    if (!mouse->relative_mode) {
slouken@1895
   401
        mouse->x = x;
slouken@1895
   402
        mouse->y = y;
slouken@2710
   403
    } else {
slouken@2860
   404
        mouse->x += xrel;
slouken@2860
   405
        mouse->y += yrel;
slouken@2860
   406
    }
slouken@2849
   407
icculus@8927
   408
    /* make sure that the pointers find themselves inside the windows,
icculus@8927
   409
       unless we have the mouse captured. */
slouken@8983
   410
    if (window && ((window->flags & SDL_WINDOW_MOUSE_CAPTURE) == 0)) {
icculus@8927
   411
        int x_max = 0, y_max = 0;
slouken@2849
   412
icculus@10780
   413
        /* !!! FIXME: shouldn't this be (window) instead of (mouse->focus)? */
icculus@8927
   414
        SDL_GetWindowSize(mouse->focus, &x_max, &y_max);
icculus@8927
   415
        --x_max;
icculus@8927
   416
        --y_max;
slouken@2860
   417
icculus@8927
   418
        if (mouse->x > x_max) {
icculus@8927
   419
            mouse->x = x_max;
icculus@8927
   420
        }
icculus@8927
   421
        if (mouse->x < 0) {
icculus@8927
   422
            mouse->x = 0;
icculus@8927
   423
        }
icculus@8927
   424
icculus@8927
   425
        if (mouse->y > y_max) {
icculus@8927
   426
            mouse->y = y_max;
icculus@8927
   427
        }
icculus@8927
   428
        if (mouse->y < 0) {
icculus@8927
   429
            mouse->y = 0;
icculus@8927
   430
        }
slouken@2860
   431
    }
slouken@2860
   432
slouken@1895
   433
    mouse->xdelta += xrel;
slouken@1895
   434
    mouse->ydelta += yrel;
slouken@1895
   435
slouken@1895
   436
    /* Move the mouse cursor, if needed */
slouken@1895
   437
    if (mouse->cursor_shown && !mouse->relative_mode &&
slouken@1895
   438
        mouse->MoveCursor && mouse->cur_cursor) {
slouken@1895
   439
        mouse->MoveCursor(mouse->cur_cursor);
slouken@1895
   440
    }
slouken@1895
   441
slouken@1895
   442
    /* Post the event, if desired */
slouken@1895
   443
    posted = 0;
slouken@4465
   444
    if (SDL_GetEventState(SDL_MOUSEMOTION) == SDL_ENABLE) {
slouken@1895
   445
        SDL_Event event;
slouken@1895
   446
        event.motion.type = SDL_MOUSEMOTION;
slouken@4465
   447
        event.motion.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@6950
   448
        event.motion.which = mouseID;
slouken@1895
   449
        event.motion.state = mouse->buttonstate;
slouken@1895
   450
        event.motion.x = mouse->x;
slouken@1895
   451
        event.motion.y = mouse->y;
slouken@1895
   452
        event.motion.xrel = xrel;
slouken@1895
   453
        event.motion.yrel = yrel;
slouken@1895
   454
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   455
    }
slouken@9448
   456
    if (relative) {
slouken@9448
   457
        mouse->last_x = mouse->x;
slouken@9448
   458
        mouse->last_y = mouse->y;
slouken@9448
   459
    } else {
slouken@9448
   460
        /* Use unclamped values if we're getting events outside the window */
slouken@9448
   461
        mouse->last_x = x;
slouken@9448
   462
        mouse->last_y = y;
slouken@9448
   463
    }
slouken@1895
   464
    return posted;
slouken@1895
   465
}
slouken@1895
   466
slouken@8066
   467
static SDL_MouseClickState *GetMouseClickState(SDL_Mouse *mouse, Uint8 button)
slouken@8066
   468
{
slouken@8066
   469
    if (button >= mouse->num_clickstates) {
slouken@8066
   470
        int i, count = button + 1;
philipp@9334
   471
        SDL_MouseClickState *clickstate = (SDL_MouseClickState *)SDL_realloc(mouse->clickstate, count * sizeof(*mouse->clickstate));
philipp@9334
   472
        if (!clickstate) {
slouken@8066
   473
            return NULL;
slouken@8066
   474
        }
philipp@9334
   475
        mouse->clickstate = clickstate;
slouken@8066
   476
slouken@8066
   477
        for (i = mouse->num_clickstates; i < count; ++i) {
slouken@8066
   478
            SDL_zero(mouse->clickstate[i]);
slouken@8066
   479
        }
slouken@8066
   480
        mouse->num_clickstates = count;
slouken@8066
   481
    }
slouken@8066
   482
    return &mouse->clickstate[button];
slouken@8066
   483
}
slouken@8066
   484
slime73@10366
   485
static int
slime73@10366
   486
SDL_PrivateSendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button, int clicks)
slouken@1895
   487
{
slouken@5371
   488
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   489
    int posted;
slouken@4429
   490
    Uint32 type;
slouken@6673
   491
    Uint32 buttonstate = mouse->buttonstate;
slouken@4484
   492
sylvain@12688
   493
    /* SDL_HINT_MOUSE_TOUCH_EVENTS: controlling whether mouse events should generate synthetic touch events */
sylvain@12688
   494
    if (mouse->mouse_touch_events) {
sylvain@12688
   495
        if (mouseID != SDL_TOUCH_MOUSEID && button == SDL_BUTTON_LEFT) {
sylvain@12698
   496
            if (state == SDL_PRESSED) {
sylvain@12698
   497
                track_mouse_down = SDL_TRUE;
sylvain@12698
   498
            } else {
sylvain@12698
   499
                track_mouse_down = SDL_FALSE;
sylvain@12698
   500
            }
sylvain@12688
   501
            if (window) {
sylvain@12688
   502
                float fx = (float)mouse->x / (float)window->w;
sylvain@12688
   503
                float fy = (float)mouse->y / (float)window->h;
sylvain@12688
   504
                SDL_SendTouch(SDL_MOUSE_TOUCHID, 0, track_mouse_down, fx, fy, 1.0f);
sylvain@12688
   505
            }
sylvain@12688
   506
        }
sylvain@12688
   507
    }
sylvain@12688
   508
sylvain@12934
   509
    /* SDL_HINT_TOUCH_MOUSE_EVENTS: if not set, discard synthetic mouse events coming from platform layer */
sylvain@12934
   510
    if (mouse->touch_mouse_events == 0) {
sylvain@12934
   511
        if (mouseID == SDL_TOUCH_MOUSEID) {
sylvain@12934
   512
            return 0;
sylvain@12934
   513
        }
sylvain@12934
   514
    }
sylvain@12934
   515
slouken@1895
   516
    /* Figure out which event to perform */
slouken@1895
   517
    switch (state) {
slouken@1895
   518
    case SDL_PRESSED:
slouken@1895
   519
        type = SDL_MOUSEBUTTONDOWN;
slouken@6666
   520
        buttonstate |= SDL_BUTTON(button);
slouken@1895
   521
        break;
slouken@1895
   522
    case SDL_RELEASED:
slouken@1895
   523
        type = SDL_MOUSEBUTTONUP;
slouken@6666
   524
        buttonstate &= ~SDL_BUTTON(button);
slouken@1895
   525
        break;
slouken@1895
   526
    default:
slouken@1895
   527
        /* Invalid state -- bail */
slouken@1895
   528
        return 0;
slouken@1895
   529
    }
slouken@1895
   530
slouken@6666
   531
    /* We do this after calculating buttonstate so button presses gain focus */
slouken@6666
   532
    if (window && state == SDL_PRESSED) {
slouken@6666
   533
        SDL_UpdateMouseFocus(window, mouse->x, mouse->y, buttonstate);
slouken@6666
   534
    }
slouken@6666
   535
slouken@6666
   536
    if (buttonstate == mouse->buttonstate) {
slouken@6666
   537
        /* Ignore this event, no state change */
slouken@6666
   538
        return 0;
slouken@6666
   539
    }
slouken@6666
   540
    mouse->buttonstate = buttonstate;
slouken@6666
   541
slime73@10366
   542
    if (clicks < 0) {
slime73@10366
   543
        SDL_MouseClickState *clickstate = GetMouseClickState(mouse, button);
philipp@10390
   544
        if (clickstate) {
philipp@10390
   545
            if (state == SDL_PRESSED) {
philipp@10390
   546
                Uint32 now = SDL_GetTicks();
slouken@8066
   547
slouken@12188
   548
                if (SDL_TICKS_PASSED(now, clickstate->last_timestamp + mouse->double_click_time) ||
slouken@12188
   549
                    SDL_abs(mouse->x - clickstate->last_x) > mouse->double_click_radius ||
slouken@12188
   550
                    SDL_abs(mouse->y - clickstate->last_y) > mouse->double_click_radius) {
philipp@10390
   551
                    clickstate->click_count = 0;
philipp@10390
   552
                }
philipp@10390
   553
                clickstate->last_timestamp = now;
philipp@10390
   554
                clickstate->last_x = mouse->x;
philipp@10390
   555
                clickstate->last_y = mouse->y;
philipp@10390
   556
                if (clickstate->click_count < 255) {
philipp@10390
   557
                    ++clickstate->click_count;
philipp@10390
   558
                }
slouken@8066
   559
            }
philipp@10390
   560
            clicks = clickstate->click_count;
philipp@10390
   561
        } else {
philipp@10390
   562
            clicks = 1;
slouken@8066
   563
        }
slouken@8066
   564
    }
slouken@8066
   565
slouken@1895
   566
    /* Post the event, if desired */
slouken@1895
   567
    posted = 0;
slouken@4429
   568
    if (SDL_GetEventState(type) == SDL_ENABLE) {
slouken@1895
   569
        SDL_Event event;
slouken@1895
   570
        event.type = type;
slouken@6950
   571
        event.button.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@6950
   572
        event.button.which = mouseID;
slouken@1895
   573
        event.button.state = state;
slouken@1895
   574
        event.button.button = button;
slime73@10366
   575
        event.button.clicks = (Uint8) SDL_min(clicks, 255);
slouken@1895
   576
        event.button.x = mouse->x;
slouken@1895
   577
        event.button.y = mouse->y;
slouken@1895
   578
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   579
    }
slouken@6666
   580
slouken@6666
   581
    /* We do this after dispatching event so button releases can lose focus */
slouken@6666
   582
    if (window && state == SDL_RELEASED) {
slouken@6666
   583
        SDL_UpdateMouseFocus(window, mouse->x, mouse->y, buttonstate);
slouken@6666
   584
    }
sylvain@12687
   585
slime73@10366
   586
    return posted;
slime73@10366
   587
}
slouken@6666
   588
slime73@10366
   589
int
slime73@10366
   590
SDL_SendMouseButtonClicks(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button, int clicks)
slime73@10366
   591
{
slime73@10366
   592
    clicks = SDL_max(clicks, 0);
slime73@10366
   593
    return SDL_PrivateSendMouseButton(window, mouseID, state, button, clicks);
slime73@10366
   594
}
slime73@10366
   595
slime73@10366
   596
int
slime73@10366
   597
SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button)
slime73@10366
   598
{
slime73@10366
   599
    return SDL_PrivateSendMouseButton(window, mouseID, state, button, -1);
slouken@1895
   600
}
slouken@1895
   601
slouken@1895
   602
int
slouken@11300
   603
SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, float x, float y, SDL_MouseWheelDirection direction)
slouken@1895
   604
{
slouken@5371
   605
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   606
    int posted;
slouken@11300
   607
    int integral_x, integral_y;
slouken@1895
   608
slouken@4484
   609
    if (window) {
slouken@4484
   610
        SDL_SetMouseFocus(window);
slouken@4484
   611
    }
slouken@4484
   612
slouken@12466
   613
    if (x == 0.0f && y == 0.0f) {
slouken@1895
   614
        return 0;
slouken@1895
   615
    }
slouken@1895
   616
slouken@11300
   617
    mouse->accumulated_wheel_x += x;
slouken@11300
   618
    if (mouse->accumulated_wheel_x > 0) {
slouken@11300
   619
        integral_x = (int)SDL_floor(mouse->accumulated_wheel_x);
slouken@11300
   620
    } else if (mouse->accumulated_wheel_x < 0) {
slouken@11300
   621
        integral_x = (int)SDL_ceil(mouse->accumulated_wheel_x);
slouken@11300
   622
    } else {
slouken@11300
   623
        integral_x = 0;
slouken@11300
   624
    }
slouken@11300
   625
    mouse->accumulated_wheel_x -= integral_x;
slouken@11300
   626
slouken@11300
   627
    mouse->accumulated_wheel_y += y;
slouken@11300
   628
    if (mouse->accumulated_wheel_y > 0) {
slouken@11300
   629
        integral_y = (int)SDL_floor(mouse->accumulated_wheel_y);
slouken@11300
   630
    } else if (mouse->accumulated_wheel_y < 0) {
slouken@11300
   631
        integral_y = (int)SDL_ceil(mouse->accumulated_wheel_y);
slouken@11300
   632
    } else {
slouken@11300
   633
        integral_y = 0;
slouken@11300
   634
    }
slouken@11300
   635
    mouse->accumulated_wheel_y -= integral_y;
slouken@11300
   636
slouken@1895
   637
    /* Post the event, if desired */
slouken@1895
   638
    posted = 0;
slouken@4429
   639
    if (SDL_GetEventState(SDL_MOUSEWHEEL) == SDL_ENABLE) {
slouken@1895
   640
        SDL_Event event;
slouken@1895
   641
        event.type = SDL_MOUSEWHEEL;
slouken@4465
   642
        event.wheel.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@6950
   643
        event.wheel.which = mouseID;
slouken@11300
   644
#if 0 /* Uncomment this when it goes in for SDL 2.1 */
slouken@11300
   645
        event.wheel.preciseX = x;
slouken@11300
   646
        event.wheel.preciseY = y;
slouken@11300
   647
#endif
slouken@11300
   648
        event.wheel.x = integral_x;
slouken@11300
   649
        event.wheel.y = integral_y;
urkle@9257
   650
        event.wheel.direction = (Uint32)direction;
slouken@1895
   651
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   652
    }
slouken@1895
   653
    return posted;
slouken@1895
   654
}
slouken@1895
   655
slouken@1895
   656
void
slouken@4465
   657
SDL_MouseQuit(void)
slouken@4465
   658
{
slouken@7493
   659
    SDL_Cursor *cursor, *next;
slouken@7493
   660
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@7493
   661
slouken@9161
   662
    if (mouse->CaptureMouse) {
slouken@9161
   663
        SDL_CaptureMouse(SDL_FALSE);
slouken@9161
   664
    }
slouken@8049
   665
    SDL_SetRelativeMouseMode(SDL_FALSE);
slouken@7493
   666
    SDL_ShowCursor(1);
slouken@7493
   667
slouken@7493
   668
    cursor = mouse->cursors;
slouken@7493
   669
    while (cursor) {
slouken@7493
   670
        next = cursor->next;
slouken@7493
   671
        SDL_FreeCursor(cursor);
slouken@7493
   672
        cursor = next;
slouken@7493
   673
    }
slouken@11622
   674
    mouse->cursors = NULL;
sylvain@12620
   675
    mouse->cur_cursor = NULL;
slouken@7493
   676
slouken@7493
   677
    if (mouse->def_cursor && mouse->FreeCursor) {
slouken@7493
   678
        mouse->FreeCursor(mouse->def_cursor);
slouken@11622
   679
        mouse->def_cursor = NULL;
slouken@7493
   680
    }
slouken@7493
   681
slouken@8066
   682
    if (mouse->clickstate) {
slouken@8066
   683
        SDL_free(mouse->clickstate);
slouken@11622
   684
        mouse->clickstate = NULL;
slouken@8066
   685
    }
slouken@8066
   686
slouken@10673
   687
    SDL_DelHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE,
slouken@10673
   688
                        SDL_MouseNormalSpeedScaleChanged, mouse);
slouken@10673
   689
slouken@10673
   690
    SDL_DelHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE,
slouken@10673
   691
                        SDL_MouseRelativeSpeedScaleChanged, mouse);
slouken@4465
   692
}
slouken@4465
   693
slouken@6673
   694
Uint32
slouken@4465
   695
SDL_GetMouseState(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->x;
slouken@4465
   701
    }
slouken@4465
   702
    if (y) {
slouken@4465
   703
        *y = mouse->y;
slouken@4465
   704
    }
slouken@4465
   705
    return mouse->buttonstate;
slouken@4465
   706
}
slouken@4465
   707
slouken@6673
   708
Uint32
slouken@4465
   709
SDL_GetRelativeMouseState(int *x, int *y)
slouken@4465
   710
{
slouken@5371
   711
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@4465
   712
slouken@4465
   713
    if (x) {
slouken@4465
   714
        *x = mouse->xdelta;
slouken@4465
   715
    }
slouken@4465
   716
    if (y) {
slouken@4465
   717
        *y = mouse->ydelta;
slouken@4465
   718
    }
slouken@4465
   719
    mouse->xdelta = 0;
slouken@4465
   720
    mouse->ydelta = 0;
slouken@4465
   721
    return mouse->buttonstate;
slouken@4465
   722
}
slouken@4465
   723
icculus@8945
   724
Uint32
icculus@8952
   725
SDL_GetGlobalMouseState(int *x, int *y)
icculus@8945
   726
{
icculus@8945
   727
    SDL_Mouse *mouse = SDL_GetMouse();
icculus@8945
   728
    int tmpx, tmpy;
icculus@8945
   729
icculus@8945
   730
    /* make sure these are never NULL for the backend implementations... */
icculus@8945
   731
    if (!x) {
icculus@8945
   732
        x = &tmpx;
icculus@8945
   733
    }
icculus@8945
   734
    if (!y) {
icculus@8945
   735
        y = &tmpy;
icculus@8945
   736
    }
icculus@8945
   737
icculus@8945
   738
    *x = *y = 0;
icculus@8945
   739
icculus@8952
   740
    if (!mouse->GetGlobalMouseState) {
icculus@8945
   741
        return 0;
icculus@8945
   742
    }
icculus@8945
   743
icculus@8952
   744
    return mouse->GetGlobalMouseState(x, y);
icculus@8945
   745
}
icculus@8945
   746
slouken@4465
   747
void
slouken@3685
   748
SDL_WarpMouseInWindow(SDL_Window * window, int x, int y)
slouken@1895
   749
{
slouken@5371
   750
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@7191
   751
icculus@8953
   752
    if (window == NULL) {
slouken@7191
   753
        window = mouse->focus;
icculus@8944
   754
    }
slouken@7191
   755
icculus@8953
   756
    if (window == NULL) {
slouken@7191
   757
        return;
icculus@8944
   758
    }
slouken@1895
   759
slouken@1895
   760
    if (mouse->WarpMouse) {
slouken@5371
   761
        mouse->WarpMouse(window, x, y);
slouken@1895
   762
    } else {
slouken@6950
   763
        SDL_SendMouseMotion(window, mouse->mouseID, 0, x, y);
slouken@1895
   764
    }
slouken@1895
   765
}
slouken@1895
   766
icculus@9807
   767
int
slouken@8815
   768
SDL_WarpMouseGlobal(int x, int y)
slouken@8815
   769
{
slouken@8815
   770
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@8815
   771
slouken@8815
   772
    if (mouse->WarpMouseGlobal) {
icculus@9807
   773
        return mouse->WarpMouseGlobal(x, y);
slouken@8815
   774
    }
icculus@9807
   775
icculus@9807
   776
    return SDL_Unsupported();
slouken@8815
   777
}
slouken@8815
   778
slouken@8071
   779
static SDL_bool
slouken@8071
   780
ShouldUseRelativeModeWarp(SDL_Mouse *mouse)
slouken@8071
   781
{
slouken@12753
   782
    if (!mouse->WarpMouse) {
slouken@12753
   783
        /* Need this functionality for relative mode warp implementation */
slouken@12753
   784
        return SDL_FALSE;
slouken@8071
   785
    }
slouken@8071
   786
slouken@10499
   787
    return SDL_GetHintBoolean(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, SDL_FALSE);
slouken@8071
   788
}
slouken@8071
   789
slouken@4465
   790
int
slouken@4465
   791
SDL_SetRelativeMouseMode(SDL_bool enabled)
slouken@4465
   792
{
slouken@5371
   793
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7107
   794
    SDL_Window *focusWindow = SDL_GetKeyboardFocus();
slouken@4465
   795
slouken@5406
   796
    if (enabled == mouse->relative_mode) {
slouken@5406
   797
        return 0;
slouken@5406
   798
    }
slouken@5406
   799
slouken@8071
   800
    /* Set the relative mode */
slouken@8071
   801
    if (!enabled && mouse->relative_mode_warp) {
slouken@8071
   802
        mouse->relative_mode_warp = SDL_FALSE;
slouken@8071
   803
    } else if (enabled && ShouldUseRelativeModeWarp(mouse)) {
slouken@8071
   804
        mouse->relative_mode_warp = SDL_TRUE;
slouken@12753
   805
    } else if (!mouse->SetRelativeMouseMode || mouse->SetRelativeMouseMode(enabled) < 0) {
slouken@8269
   806
        if (enabled) {
philipp@8776
   807
            /* Fall back to warp mode if native relative mode failed */
slouken@12024
   808
            if (!mouse->WarpMouse) {
slouken@12024
   809
                return SDL_SetError("No relative mode implementation available");
slouken@12024
   810
            }
slouken@8269
   811
            mouse->relative_mode_warp = SDL_TRUE;
slouken@8269
   812
        }
slouken@5406
   813
    }
slouken@4465
   814
    mouse->relative_mode = enabled;
slouken@10673
   815
    mouse->scale_accum_x = 0.0f;
slouken@10673
   816
    mouse->scale_accum_y = 0.0f;
slouken@4465
   817
slouken@12886
   818
    if (enabled && focusWindow) {
slouken@12886
   819
        /* Center it in the focused window to prevent clicks from going through
slouken@12886
   820
         * to background windows.
slouken@12886
   821
         */
slouken@12886
   822
        SDL_SetMouseFocus(focusWindow);
slouken@12886
   823
        SDL_WarpMouseInWindow(focusWindow, focusWindow->w/2, focusWindow->h/2);
slouken@12886
   824
    }
slouken@12886
   825
slouken@8071
   826
    if (mouse->focus) {
slouken@8071
   827
        SDL_UpdateWindowGrab(mouse->focus);
slouken@8071
   828
slouken@8071
   829
        /* Put the cursor back to where the application expects it */
slouken@8071
   830
        if (!enabled) {
slouken@8071
   831
            SDL_WarpMouseInWindow(mouse->focus, mouse->x, mouse->y);
slouken@8071
   832
        }
slouken@4465
   833
    }
slouken@4465
   834
slouken@8071
   835
    /* Flush pending mouse motion - ideally we would pump events, but that's not always safe */
slouken@5406
   836
    SDL_FlushEvent(SDL_MOUSEMOTION);
slouken@5406
   837
slouken@4465
   838
    /* Update cursor visibility */
slouken@4465
   839
    SDL_SetCursor(NULL);
slouken@4465
   840
slouken@4465
   841
    return 0;
slouken@4465
   842
}
slouken@4465
   843
slouken@4465
   844
SDL_bool
slouken@4465
   845
SDL_GetRelativeMouseMode()
slouken@4465
   846
{
slouken@5371
   847
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@4465
   848
slouken@4465
   849
    return mouse->relative_mode;
slouken@4465
   850
}
slouken@4465
   851
icculus@8955
   852
int
icculus@8955
   853
SDL_CaptureMouse(SDL_bool enabled)
icculus@8955
   854
{
icculus@8955
   855
    SDL_Mouse *mouse = SDL_GetMouse();
icculus@8955
   856
    SDL_Window *focusWindow;
icculus@8955
   857
    SDL_bool isCaptured;
icculus@8955
   858
icculus@8955
   859
    if (!mouse->CaptureMouse) {
icculus@8955
   860
        return SDL_Unsupported();
icculus@8955
   861
    }
icculus@8955
   862
icculus@8955
   863
    focusWindow = SDL_GetKeyboardFocus();
icculus@8955
   864
icculus@8955
   865
    isCaptured = focusWindow && (focusWindow->flags & SDL_WINDOW_MOUSE_CAPTURE);
icculus@8955
   866
    if (isCaptured == enabled) {
icculus@8955
   867
        return 0;  /* already done! */
icculus@8955
   868
    }
icculus@8955
   869
icculus@8955
   870
    if (enabled) {
icculus@8955
   871
        if (!focusWindow) {
icculus@8955
   872
            return SDL_SetError("No window has focus");
icculus@8955
   873
        } else if (mouse->CaptureMouse(focusWindow) == -1) {
icculus@8955
   874
            return -1;  /* CaptureMouse() should call SetError */
icculus@8955
   875
        }
icculus@8955
   876
        focusWindow->flags |= SDL_WINDOW_MOUSE_CAPTURE;
icculus@8955
   877
    } else {
icculus@8955
   878
        if (mouse->CaptureMouse(NULL) == -1) {
icculus@8955
   879
            return -1;  /* CaptureMouse() should call SetError */
icculus@8955
   880
        }
icculus@8955
   881
        focusWindow->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
icculus@8955
   882
    }
icculus@8955
   883
icculus@8955
   884
    return 0;
icculus@8955
   885
}
icculus@8955
   886
slouken@1895
   887
SDL_Cursor *
slouken@1895
   888
SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
slouken@1895
   889
                 int w, int h, int hot_x, int hot_y)
slouken@1895
   890
{
slouken@1895
   891
    SDL_Surface *surface;
slouken@1895
   892
    SDL_Cursor *cursor;
slouken@1895
   893
    int x, y;
slouken@1895
   894
    Uint32 *pixel;
slouken@4465
   895
    Uint8 datab = 0, maskb = 0;
slouken@1895
   896
    const Uint32 black = 0xFF000000;
slouken@1895
   897
    const Uint32 white = 0xFFFFFFFF;
slouken@1895
   898
    const Uint32 transparent = 0x00000000;
slouken@1895
   899
slouken@1895
   900
    /* Make sure the width is a multiple of 8 */
slouken@1895
   901
    w = ((w + 7) & ~7);
slouken@1895
   902
slouken@1895
   903
    /* Create the surface from a bitmap */
slouken@5473
   904
    surface = SDL_CreateRGBSurface(0, w, h, 32,
slouken@5473
   905
                                   0x00FF0000,
slouken@5473
   906
                                   0x0000FF00,
slouken@5473
   907
                                   0x000000FF,
slouken@5473
   908
                                   0xFF000000);
slouken@1895
   909
    if (!surface) {
slouken@1895
   910
        return NULL;
slouken@1895
   911
    }
slouken@1895
   912
    for (y = 0; y < h; ++y) {
slouken@1895
   913
        pixel = (Uint32 *) ((Uint8 *) surface->pixels + y * surface->pitch);
slouken@1895
   914
        for (x = 0; x < w; ++x) {
slouken@1895
   915
            if ((x % 8) == 0) {
slouken@1895
   916
                datab = *data++;
slouken@1895
   917
                maskb = *mask++;
slouken@1895
   918
            }
slouken@1895
   919
            if (maskb & 0x80) {
slouken@1895
   920
                *pixel++ = (datab & 0x80) ? black : white;
slouken@1895
   921
            } else {
slouken@1895
   922
                *pixel++ = (datab & 0x80) ? black : transparent;
slouken@1895
   923
            }
slouken@1895
   924
            datab <<= 1;
slouken@1895
   925
            maskb <<= 1;
slouken@1895
   926
        }
slouken@1895
   927
    }
slouken@1895
   928
slouken@5473
   929
    cursor = SDL_CreateColorCursor(surface, hot_x, hot_y);
slouken@5473
   930
slouken@5473
   931
    SDL_FreeSurface(surface);
slouken@5473
   932
slouken@5473
   933
    return cursor;
slouken@5473
   934
}
slouken@5473
   935
slouken@5473
   936
SDL_Cursor *
slouken@5473
   937
SDL_CreateColorCursor(SDL_Surface *surface, int hot_x, int hot_y)
slouken@5473
   938
{
slouken@5473
   939
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5473
   940
    SDL_Surface *temp = NULL;
slouken@5473
   941
    SDL_Cursor *cursor;
slouken@5473
   942
slouken@5473
   943
    if (!surface) {
slouken@5473
   944
        SDL_SetError("Passed NULL cursor surface");
slouken@5473
   945
        return NULL;
slouken@5473
   946
    }
slouken@5473
   947
slouken@5473
   948
    if (!mouse->CreateCursor) {
slouken@5473
   949
        SDL_SetError("Cursors are not currently supported");
slouken@5473
   950
        return NULL;
slouken@5473
   951
    }
slouken@5473
   952
slouken@5473
   953
    /* Sanity check the hot spot */
slouken@5473
   954
    if ((hot_x < 0) || (hot_y < 0) ||
slouken@5473
   955
        (hot_x >= surface->w) || (hot_y >= surface->h)) {
slouken@5473
   956
        SDL_SetError("Cursor hot spot doesn't lie within cursor");
slouken@5473
   957
        return NULL;
slouken@5473
   958
    }
slouken@5473
   959
slouken@5473
   960
    if (surface->format->format != SDL_PIXELFORMAT_ARGB8888) {
slouken@5473
   961
        temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0);
slouken@5473
   962
        if (!temp) {
slouken@5473
   963
            return NULL;
slouken@5473
   964
        }
slouken@5473
   965
        surface = temp;
slouken@5473
   966
    }
slouken@5473
   967
slouken@1895
   968
    cursor = mouse->CreateCursor(surface, hot_x, hot_y);
slouken@1895
   969
    if (cursor) {
slouken@1895
   970
        cursor->next = mouse->cursors;
slouken@1895
   971
        mouse->cursors = cursor;
slouken@1895
   972
    }
slouken@1895
   973
slouken@7720
   974
    SDL_FreeSurface(temp);
slouken@1895
   975
slouken@1895
   976
    return cursor;
slouken@1895
   977
}
slouken@1895
   978
mikesart@6675
   979
SDL_Cursor *
mikesart@6675
   980
SDL_CreateSystemCursor(SDL_SystemCursor id)
mikesart@6675
   981
{
mikesart@6675
   982
    SDL_Mouse *mouse = SDL_GetMouse();
mikesart@6675
   983
    SDL_Cursor *cursor;
mikesart@6675
   984
mikesart@6675
   985
    if (!mouse->CreateSystemCursor) {
mikesart@6675
   986
        SDL_SetError("CreateSystemCursor is not currently supported");
mikesart@6675
   987
        return NULL;
mikesart@6675
   988
    }
mikesart@6675
   989
slouken@7191
   990
    cursor = mouse->CreateSystemCursor(id);
mikesart@6675
   991
    if (cursor) {
mikesart@6675
   992
        cursor->next = mouse->cursors;
mikesart@6675
   993
        mouse->cursors = cursor;
mikesart@6675
   994
    }
mikesart@6675
   995
slouken@7191
   996
    return cursor;
mikesart@6675
   997
}
mikesart@6675
   998
slouken@1895
   999
/* SDL_SetCursor(NULL) can be used to force the cursor redraw,
slouken@1895
  1000
   if this is desired for any reason.  This is used when setting
slouken@1895
  1001
   the video mode and when the SDL window gains the mouse focus.
slouken@1895
  1002
 */
slouken@1895
  1003
void
slouken@1895
  1004
SDL_SetCursor(SDL_Cursor * cursor)
slouken@1895
  1005
{
slouken@5371
  1006
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
  1007
slouken@1895
  1008
    /* Set the new cursor */
slouken@1895
  1009
    if (cursor) {
slouken@1895
  1010
        /* Make sure the cursor is still valid for this mouse */
slouken@5405
  1011
        if (cursor != mouse->def_cursor) {
slouken@5405
  1012
            SDL_Cursor *found;
slouken@5405
  1013
            for (found = mouse->cursors; found; found = found->next) {
slouken@5405
  1014
                if (found == cursor) {
slouken@5405
  1015
                    break;
slouken@5405
  1016
                }
slouken@1895
  1017
            }
slouken@5405
  1018
            if (!found) {
slouken@5405
  1019
                SDL_SetError("Cursor not associated with the current mouse");
slouken@5405
  1020
                return;
slouken@5405
  1021
            }
slouken@1895
  1022
        }
slouken@1895
  1023
        mouse->cur_cursor = cursor;
slouken@1895
  1024
    } else {
ghostunderscore@6301
  1025
        if (mouse->focus) {
ghostunderscore@6301
  1026
            cursor = mouse->cur_cursor;
ghostunderscore@6301
  1027
        } else {
ghostunderscore@6301
  1028
            cursor = mouse->def_cursor;
ghostunderscore@6301
  1029
        }
slouken@1895
  1030
    }
slouken@1895
  1031
slouken@1895
  1032
    if (cursor && mouse->cursor_shown && !mouse->relative_mode) {
slouken@1895
  1033
        if (mouse->ShowCursor) {
slouken@1895
  1034
            mouse->ShowCursor(cursor);
slouken@1895
  1035
        }
slouken@1895
  1036
    } else {
slouken@1895
  1037
        if (mouse->ShowCursor) {
slouken@1895
  1038
            mouse->ShowCursor(NULL);
slouken@1895
  1039
        }
slouken@1895
  1040
    }
slouken@1895
  1041
}
slouken@1895
  1042
slouken@1895
  1043
SDL_Cursor *
slouken@1895
  1044
SDL_GetCursor(void)
slouken@1895
  1045
{
slouken@5371
  1046
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
  1047
slouken@1895
  1048
    if (!mouse) {
slouken@1895
  1049
        return NULL;
slouken@1895
  1050
    }
slouken@1895
  1051
    return mouse->cur_cursor;
slouken@1895
  1052
}
slouken@1895
  1053
jorgen@7104
  1054
SDL_Cursor *
jorgen@7104
  1055
SDL_GetDefaultCursor(void)
jorgen@7104
  1056
{
jorgen@7104
  1057
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7104
  1058
jorgen@7104
  1059
    if (!mouse) {
jorgen@7104
  1060
        return NULL;
jorgen@7104
  1061
    }
jorgen@7104
  1062
    return mouse->def_cursor;
jorgen@7104
  1063
}
jorgen@7104
  1064
slouken@1895
  1065
void
slouken@1895
  1066
SDL_FreeCursor(SDL_Cursor * cursor)
slouken@1895
  1067
{
slouken@5371
  1068
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
  1069
    SDL_Cursor *curr, *prev;
slouken@1895
  1070
slouken@1895
  1071
    if (!cursor) {
slouken@1895
  1072
        return;
slouken@1895
  1073
    }
slouken@1895
  1074
slouken@1895
  1075
    if (cursor == mouse->def_cursor) {
slouken@1895
  1076
        return;
slouken@1895
  1077
    }
slouken@1895
  1078
    if (cursor == mouse->cur_cursor) {
slouken@1895
  1079
        SDL_SetCursor(mouse->def_cursor);
slouken@1895
  1080
    }
slouken@1895
  1081
slouken@1895
  1082
    for (prev = NULL, curr = mouse->cursors; curr;
slouken@1895
  1083
         prev = curr, curr = curr->next) {
slouken@1895
  1084
        if (curr == cursor) {
slouken@1895
  1085
            if (prev) {
slouken@1895
  1086
                prev->next = curr->next;
slouken@1895
  1087
            } else {
slouken@1895
  1088
                mouse->cursors = curr->next;
slouken@1895
  1089
            }
slouken@1895
  1090
slouken@1895
  1091
            if (mouse->FreeCursor) {
slouken@1895
  1092
                mouse->FreeCursor(curr);
slouken@1895
  1093
            }
slouken@1895
  1094
            return;
slouken@1895
  1095
        }
slouken@1895
  1096
    }
slouken@1895
  1097
}
slouken@1895
  1098
slouken@1895
  1099
int
slouken@1895
  1100
SDL_ShowCursor(int toggle)
slouken@1895
  1101
{
slouken@5371
  1102
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
  1103
    SDL_bool shown;
slouken@1895
  1104
slouken@1895
  1105
    if (!mouse) {
slouken@1895
  1106
        return 0;
slouken@1895
  1107
    }
slouken@1895
  1108
slouken@1895
  1109
    shown = mouse->cursor_shown;
slouken@1895
  1110
    if (toggle >= 0) {
slouken@1895
  1111
        if (toggle) {
slouken@1895
  1112
            mouse->cursor_shown = SDL_TRUE;
slouken@1895
  1113
        } else {
slouken@1895
  1114
            mouse->cursor_shown = SDL_FALSE;
slouken@1895
  1115
        }
slouken@1895
  1116
        if (mouse->cursor_shown != shown) {
slouken@1895
  1117
            SDL_SetCursor(NULL);
slouken@1895
  1118
        }
slouken@1895
  1119
    }
slouken@1895
  1120
    return shown;
slouken@1895
  1121
}
slouken@1895
  1122
jorgen@7097
  1123
/* vi: set ts=4 sw=4 expandtab: */