src/events/SDL_mouse.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 13 Jun 2019 01:57:13 -0400
changeset 12854 d7d64c1a3969
parent 12753 ba1a66b57385
child 12855 ac0c65535817
permissions -rw-r--r--
cocoa: Revised synthesized mouse/touch event strategy.

I _think_ I understand what Sylvain is working on here now, so hopefully I
got this right.

Fixes Bugzilla #4576.

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