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