src/events/SDL_mouse.c
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Mon, 22 Apr 2013 18:14:55 -0700
changeset 7089 257fc4e541e1
parent 7037 3fedf1f25b94
child 7097 84bbd421d62f
permissions -rw-r--r--
Send mouse leave updates for Windows and X11.

We now generate a final SDL_MOUSEMOTION before the cursor leaves the
window, followed by a SDL_WINDOWEVENT_LEAVE.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
*/
slouken@1402
    21
#include "SDL_config.h"
slouken@0
    22
slouken@0
    23
/* General mouse handling code for SDL */
slouken@0
    24
slouken@6666
    25
#include "SDL_assert.h"
slouken@0
    26
#include "SDL_events.h"
slouken@0
    27
#include "SDL_events_c.h"
slouken@1895
    28
#include "default_cursor.h"
slouken@3685
    29
#include "../video/SDL_sysvideo.h"
slouken@0
    30
slouken@6666
    31
/*#define DEBUG_MOUSE*/
slouken@0
    32
slouken@5371
    33
/* The mouse state */
slouken@4465
    34
static SDL_Mouse SDL_mouse;
slouken@0
    35
jorgen@7089
    36
static int
jorgen@7089
    37
SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y);
slouken@0
    38
slouken@0
    39
/* Public functions */
slouken@1895
    40
int
slouken@1895
    41
SDL_MouseInit(void)
slouken@0
    42
{
slouken@5376
    43
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5376
    44
slouken@5376
    45
    mouse->cursor_shown = SDL_TRUE;
slouken@5376
    46
slouken@1895
    47
    return (0);
slouken@1123
    48
}
slouken@0
    49
slouken@5405
    50
void
slouken@5405
    51
SDL_SetDefaultCursor(SDL_Cursor * cursor)
slouken@5405
    52
{
slouken@5405
    53
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5405
    54
slouken@5405
    55
    mouse->def_cursor = cursor;
slouken@5405
    56
    if (!mouse->cur_cursor) {
slouken@5405
    57
        SDL_SetCursor(cursor);
slouken@5405
    58
    }
slouken@5405
    59
}
slouken@5405
    60
slouken@5371
    61
SDL_Mouse *
slouken@5371
    62
SDL_GetMouse(void)
slouken@5371
    63
{
slouken@5371
    64
    return &SDL_mouse;
slouken@5371
    65
}
slouken@5371
    66
slouken@4465
    67
SDL_Window *
slouken@4465
    68
SDL_GetMouseFocus(void)
slouken@2710
    69
{
slouken@5371
    70
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@2940
    71
slouken@4465
    72
    return mouse->focus;
slouken@0
    73
}
slouken@0
    74
slouken@1895
    75
void
slouken@6666
    76
SDL_ResetMouse(void)
slouken@6666
    77
{
slouken@6666
    78
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@6666
    79
    Uint8 i;
slouken@6666
    80
slouken@6666
    81
#ifdef DEBUG_MOUSE
slouken@6666
    82
    printf("Resetting mouse\n");
slouken@6666
    83
#endif
slouken@6666
    84
    for (i = 1; i <= sizeof(mouse->buttonstate)*8; ++i) {
slouken@6666
    85
        if (mouse->buttonstate & SDL_BUTTON(i)) {
slouken@6950
    86
            SDL_SendMouseButton(mouse->focus, mouse->mouseID, SDL_RELEASED, i);
slouken@6666
    87
        }
slouken@6666
    88
    }
slouken@6666
    89
    SDL_assert(mouse->buttonstate == 0);
slouken@6666
    90
}
slouken@6666
    91
slouken@6666
    92
void
slouken@4465
    93
SDL_SetMouseFocus(SDL_Window * window)
slouken@0
    94
{
slouken@5371
    95
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
    96
slouken@4465
    97
    if (mouse->focus == window) {
slouken@1895
    98
        return;
slouken@1895
    99
    }
slouken@1895
   100
slouken@6829
   101
    /* Actually, this ends up being a bad idea, because most operating
slouken@6829
   102
       systems have an implicit grab when you press the mouse button down
slouken@6829
   103
       so you can drag things out of the window and then get the mouse up
slouken@6829
   104
       when it happens.  So, #if 0...
slouken@6829
   105
    */
slouken@6829
   106
#if 0
slouken@6666
   107
    if (mouse->focus && !window) {
slouken@6666
   108
        /* We won't get anymore mouse messages, so reset mouse state */
slouken@6666
   109
        SDL_ResetMouse();
slouken@6666
   110
    }
slouken@6829
   111
#endif
slouken@6666
   112
slouken@1895
   113
    /* See if the current window has lost focus */
slouken@1895
   114
    if (mouse->focus) {
slouken@4465
   115
        SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_LEAVE, 0, 0);
slouken@1895
   116
    }
slouken@1895
   117
slouken@3685
   118
    mouse->focus = window;
slouken@1895
   119
slouken@1895
   120
    if (mouse->focus) {
slouken@4465
   121
        SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_ENTER, 0, 0);
slouken@1895
   122
    }
slouken@6666
   123
slouken@6666
   124
    /* Update cursor visibility */
slouken@6666
   125
    SDL_SetCursor(NULL);
slouken@6666
   126
}
slouken@6666
   127
slouken@6666
   128
/* Check to see if we need to synthesize focus events */
slouken@6666
   129
static SDL_bool
slouken@6673
   130
SDL_UpdateMouseFocus(SDL_Window * window, int x, int y, Uint32 buttonstate)
slouken@6666
   131
{
slouken@6666
   132
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@6666
   133
    int w, h;
slouken@6666
   134
    SDL_bool inWindow;
slouken@6666
   135
slouken@6666
   136
    SDL_GetWindowSize(window, &w, &h);
slouken@6666
   137
    if (x < 0 || y < 0 || x >= w || y >= h) {
slouken@6666
   138
        inWindow = SDL_FALSE;
slouken@6666
   139
    } else {
slouken@6666
   140
        inWindow = SDL_TRUE;
slouken@6666
   141
    }
slouken@6667
   142
slouken@6667
   143
/* Linux doesn't give you mouse events outside your window unless you grab
slouken@6667
   144
   the pointer.
slouken@6667
   145
slouken@6667
   146
   Windows doesn't give you mouse events outside your window unless you call
slouken@6667
   147
   SetCapture().
slouken@6667
   148
slouken@6667
   149
   Both of these are slightly scary changes, so for now we'll punt and if the
slouken@6667
   150
   mouse leaves the window you'll lose mouse focus and reset button state.
slouken@6667
   151
*/
slouken@6667
   152
#ifdef SUPPORT_DRAG_OUTSIDE_WINDOW
slouken@6666
   153
    if (!inWindow && !buttonstate) {
slouken@6667
   154
#else
slouken@6667
   155
    if (!inWindow) {
slouken@6667
   156
#endif
slouken@6666
   157
        if (window == mouse->focus) {
slouken@6666
   158
#ifdef DEBUG_MOUSE
jorgen@7089
   159
            printf("Mouse left window, synthesizing move & focus lost event\n");
slouken@6666
   160
#endif
jorgen@7089
   161
            SDL_PrivateSendMouseMotion(window, mouse->mouseID, 0, x, y);
slouken@6666
   162
            SDL_SetMouseFocus(NULL);
slouken@6666
   163
        }
slouken@6666
   164
        return SDL_FALSE;
slouken@6666
   165
    }
slouken@6666
   166
slouken@6666
   167
    if (window != mouse->focus) {
slouken@6666
   168
        mouse->last_x = x;
slouken@6666
   169
        mouse->last_y = y;
slouken@6666
   170
slouken@6666
   171
#ifdef DEBUG_MOUSE
slouken@6666
   172
        printf("Mouse entered window, synthesizing focus gain event\n");
slouken@6666
   173
#endif
slouken@6666
   174
        SDL_SetMouseFocus(window);
slouken@6666
   175
    }
slouken@6666
   176
    return SDL_TRUE;
slouken@1895
   177
}
slouken@1895
   178
slouken@1895
   179
int
slouken@6950
   180
SDL_SendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y)
slouken@1895
   181
{
jorgen@7089
   182
    if (window && !relative) {
jorgen@7089
   183
        SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7089
   184
        if (!SDL_UpdateMouseFocus(window, x, y, mouse->buttonstate)) {
jorgen@7089
   185
            return 0;
jorgen@7089
   186
        }
jorgen@7089
   187
    }
jorgen@7089
   188
jorgen@7089
   189
    return SDL_PrivateSendMouseMotion(window, mouseID, relative, x, y);
jorgen@7089
   190
}
jorgen@7089
   191
jorgen@7089
   192
static int
jorgen@7089
   193
SDL_PrivateSendMouseMotion(SDL_Window * window, SDL_MouseID mouseID, int relative, int x, int y)
jorgen@7089
   194
{
slouken@5371
   195
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   196
    int posted;
slouken@1895
   197
    int xrel;
slouken@1895
   198
    int yrel;
slouken@2860
   199
    int x_max = 0, y_max = 0;
slouken@1895
   200
slouken@6666
   201
    /* relative motion is calculated regarding the system cursor last position */
slouken@2842
   202
    if (relative) {
slouken@2842
   203
        xrel = x;
slouken@2842
   204
        yrel = y;
slouken@2860
   205
        x = (mouse->last_x + x);
slouken@2860
   206
        y = (mouse->last_y + y);
slouken@2842
   207
    } else {
slouken@2860
   208
        xrel = x - mouse->last_x;
slouken@2860
   209
        yrel = y - mouse->last_y;
slouken@2842
   210
    }
slouken@2710
   211
slouken@1895
   212
    /* Drop events that don't change state */
slouken@1895
   213
    if (!xrel && !yrel) {
slouken@6666
   214
#ifdef DEBUG_MOUSE
slouken@1895
   215
        printf("Mouse event didn't change state - dropped!\n");
slouken@1895
   216
#endif
slouken@1895
   217
        return 0;
slouken@1895
   218
    }
slouken@1895
   219
slouken@2710
   220
    /* Update internal mouse coordinates */
slouken@2710
   221
    if (mouse->relative_mode == SDL_FALSE) {
slouken@1895
   222
        mouse->x = x;
slouken@1895
   223
        mouse->y = y;
slouken@2710
   224
    } else {
slouken@2860
   225
        mouse->x += xrel;
slouken@2860
   226
        mouse->y += yrel;
slouken@2860
   227
    }
slouken@2849
   228
slouken@2860
   229
    SDL_GetWindowSize(mouse->focus, &x_max, &y_max);
slouken@5370
   230
    --x_max;
slouken@5370
   231
    --y_max;
slouken@2849
   232
slouken@2860
   233
    /* make sure that the pointers find themselves inside the windows */
slouken@5370
   234
    if (mouse->x > x_max) {
slouken@2860
   235
        mouse->x = x_max;
slouken@5370
   236
    }
slouken@5370
   237
    if (mouse->x < 0) {
slouken@2860
   238
        mouse->x = 0;
slouken@1895
   239
    }
slouken@2860
   240
slouken@5370
   241
    if (mouse->y > y_max) {
slouken@2860
   242
        mouse->y = y_max;
slouken@5370
   243
    }
slouken@5370
   244
    if (mouse->y < 0) {
slouken@2860
   245
        mouse->y = 0;
slouken@2860
   246
    }
slouken@2860
   247
slouken@1895
   248
    mouse->xdelta += xrel;
slouken@1895
   249
    mouse->ydelta += yrel;
slouken@1895
   250
slouken@4465
   251
#if 0 /* FIXME */
slouken@1895
   252
    /* Move the mouse cursor, if needed */
slouken@1895
   253
    if (mouse->cursor_shown && !mouse->relative_mode &&
slouken@1895
   254
        mouse->MoveCursor && mouse->cur_cursor) {
slouken@1895
   255
        mouse->MoveCursor(mouse->cur_cursor);
slouken@1895
   256
    }
slouken@4465
   257
#endif
slouken@1895
   258
slouken@1895
   259
    /* Post the event, if desired */
slouken@1895
   260
    posted = 0;
slouken@4465
   261
    if (SDL_GetEventState(SDL_MOUSEMOTION) == SDL_ENABLE) {
slouken@1895
   262
        SDL_Event event;
slouken@1895
   263
        event.motion.type = SDL_MOUSEMOTION;
slouken@4465
   264
        event.motion.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@6950
   265
        event.motion.which = mouseID;
slouken@1895
   266
        event.motion.state = mouse->buttonstate;
slouken@1895
   267
        event.motion.x = mouse->x;
slouken@1895
   268
        event.motion.y = mouse->y;
slouken@1895
   269
        event.motion.xrel = xrel;
slouken@1895
   270
        event.motion.yrel = yrel;
slouken@1895
   271
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   272
    }
slouken@6666
   273
    /* Use unclamped values if we're getting events outside the window */
slouken@6666
   274
    mouse->last_x = x;
slouken@6666
   275
    mouse->last_y = y;
slouken@1895
   276
    return posted;
slouken@1895
   277
}
slouken@1895
   278
slouken@1895
   279
int
slouken@6950
   280
SDL_SendMouseButton(SDL_Window * window, SDL_MouseID mouseID, Uint8 state, Uint8 button)
slouken@1895
   281
{
slouken@5371
   282
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   283
    int posted;
slouken@4429
   284
    Uint32 type;
slouken@6673
   285
    Uint32 buttonstate = mouse->buttonstate;
slouken@4484
   286
slouken@1895
   287
    /* Figure out which event to perform */
slouken@1895
   288
    switch (state) {
slouken@1895
   289
    case SDL_PRESSED:
slouken@1895
   290
        type = SDL_MOUSEBUTTONDOWN;
slouken@6666
   291
        buttonstate |= SDL_BUTTON(button);
slouken@1895
   292
        break;
slouken@1895
   293
    case SDL_RELEASED:
slouken@1895
   294
        type = SDL_MOUSEBUTTONUP;
slouken@6666
   295
        buttonstate &= ~SDL_BUTTON(button);
slouken@1895
   296
        break;
slouken@1895
   297
    default:
slouken@1895
   298
        /* Invalid state -- bail */
slouken@1895
   299
        return 0;
slouken@1895
   300
    }
slouken@1895
   301
slouken@6666
   302
    /* We do this after calculating buttonstate so button presses gain focus */
slouken@6666
   303
    if (window && state == SDL_PRESSED) {
slouken@6666
   304
        SDL_UpdateMouseFocus(window, mouse->x, mouse->y, buttonstate);
slouken@6666
   305
    }
slouken@6666
   306
slouken@6666
   307
    if (buttonstate == mouse->buttonstate) {
slouken@6666
   308
        /* Ignore this event, no state change */
slouken@6666
   309
        return 0;
slouken@6666
   310
    }
slouken@6666
   311
    mouse->buttonstate = buttonstate;
slouken@6666
   312
slouken@1895
   313
    /* Post the event, if desired */
slouken@1895
   314
    posted = 0;
slouken@4429
   315
    if (SDL_GetEventState(type) == SDL_ENABLE) {
slouken@1895
   316
        SDL_Event event;
slouken@1895
   317
        event.type = type;
slouken@6950
   318
        event.button.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@6950
   319
        event.button.which = mouseID;
slouken@1895
   320
        event.button.state = state;
slouken@1895
   321
        event.button.button = button;
slouken@1895
   322
        event.button.x = mouse->x;
slouken@1895
   323
        event.button.y = mouse->y;
slouken@1895
   324
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   325
    }
slouken@6666
   326
slouken@6666
   327
    /* We do this after dispatching event so button releases can lose focus */
slouken@6666
   328
    if (window && state == SDL_RELEASED) {
slouken@6666
   329
        SDL_UpdateMouseFocus(window, mouse->x, mouse->y, buttonstate);
slouken@6666
   330
    }
slouken@6666
   331
slouken@1895
   332
    return posted;
slouken@1895
   333
}
slouken@1895
   334
slouken@1895
   335
int
slouken@6950
   336
SDL_SendMouseWheel(SDL_Window * window, SDL_MouseID mouseID, int x, int y)
slouken@1895
   337
{
slouken@5371
   338
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   339
    int posted;
slouken@1895
   340
slouken@4484
   341
    if (window) {
slouken@4484
   342
        SDL_SetMouseFocus(window);
slouken@4484
   343
    }
slouken@4484
   344
slouken@4465
   345
    if (!x && !y) {
slouken@1895
   346
        return 0;
slouken@1895
   347
    }
slouken@1895
   348
slouken@1895
   349
    /* Post the event, if desired */
slouken@1895
   350
    posted = 0;
slouken@4429
   351
    if (SDL_GetEventState(SDL_MOUSEWHEEL) == SDL_ENABLE) {
slouken@1895
   352
        SDL_Event event;
slouken@1895
   353
        event.type = SDL_MOUSEWHEEL;
slouken@4465
   354
        event.wheel.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@6950
   355
        event.wheel.which = mouseID;
slouken@2152
   356
        event.wheel.x = x;
slouken@2152
   357
        event.wheel.y = y;
slouken@1895
   358
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   359
    }
slouken@1895
   360
    return posted;
slouken@1895
   361
}
slouken@1895
   362
slouken@1895
   363
void
slouken@4465
   364
SDL_MouseQuit(void)
slouken@4465
   365
{
slouken@4465
   366
}
slouken@4465
   367
slouken@6673
   368
Uint32
slouken@4465
   369
SDL_GetMouseState(int *x, int *y)
slouken@4465
   370
{
slouken@5371
   371
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@4465
   372
slouken@4465
   373
    if (x) {
slouken@4465
   374
        *x = mouse->x;
slouken@4465
   375
    }
slouken@4465
   376
    if (y) {
slouken@4465
   377
        *y = mouse->y;
slouken@4465
   378
    }
slouken@4465
   379
    return mouse->buttonstate;
slouken@4465
   380
}
slouken@4465
   381
slouken@6673
   382
Uint32
slouken@4465
   383
SDL_GetRelativeMouseState(int *x, int *y)
slouken@4465
   384
{
slouken@5371
   385
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@4465
   386
slouken@4465
   387
    if (x) {
slouken@4465
   388
        *x = mouse->xdelta;
slouken@4465
   389
    }
slouken@4465
   390
    if (y) {
slouken@4465
   391
        *y = mouse->ydelta;
slouken@4465
   392
    }
slouken@4465
   393
    mouse->xdelta = 0;
slouken@4465
   394
    mouse->ydelta = 0;
slouken@4465
   395
    return mouse->buttonstate;
slouken@4465
   396
}
slouken@4465
   397
slouken@4465
   398
void
slouken@3685
   399
SDL_WarpMouseInWindow(SDL_Window * window, int x, int y)
slouken@1895
   400
{
slouken@5371
   401
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@6755
   402
	
slouken@6755
   403
	if ( window == NULL )
slouken@6755
   404
		window = mouse->focus;
slouken@6755
   405
	
slouken@6755
   406
	if ( window == NULL )
slouken@6755
   407
		return;
slouken@1895
   408
slouken@1895
   409
    if (mouse->WarpMouse) {
slouken@5371
   410
        mouse->WarpMouse(window, x, y);
slouken@1895
   411
    } else {
slouken@6950
   412
        SDL_SendMouseMotion(window, mouse->mouseID, 0, x, y);
slouken@1895
   413
    }
slouken@1895
   414
}
slouken@1895
   415
slouken@4465
   416
int
slouken@4465
   417
SDL_SetRelativeMouseMode(SDL_bool enabled)
slouken@4465
   418
{
slouken@5371
   419
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@4465
   420
slouken@5406
   421
    if (enabled == mouse->relative_mode) {
slouken@5406
   422
        return 0;
slouken@5406
   423
    }
slouken@5406
   424
slouken@5406
   425
    if (!mouse->SetRelativeMouseMode) {
icculus@7037
   426
        return SDL_Unsupported();
slouken@5406
   427
    }
slouken@5406
   428
slouken@5406
   429
    if (mouse->SetRelativeMouseMode(enabled) < 0) {
slouken@5406
   430
        return -1;
slouken@5406
   431
    }
slouken@4465
   432
slouken@4465
   433
    /* Set the relative mode */
slouken@4465
   434
    mouse->relative_mode = enabled;
slouken@4465
   435
ghostunderscore@6302
   436
    if (enabled) {
ghostunderscore@6302
   437
        /* Save the expected mouse position */
ghostunderscore@6302
   438
        mouse->original_x = mouse->x;
ghostunderscore@6302
   439
        mouse->original_y = mouse->y;
ghostunderscore@6302
   440
    } else if (mouse->focus) {
slouken@4465
   441
        /* Restore the expected mouse position */
ghostunderscore@6302
   442
        SDL_WarpMouseInWindow(mouse->focus, mouse->original_x, mouse->original_y);
slouken@4465
   443
    }
slouken@4465
   444
slouken@5406
   445
    /* Flush pending mouse motion */
slouken@5406
   446
    SDL_FlushEvent(SDL_MOUSEMOTION);
slouken@5406
   447
slouken@4465
   448
    /* Update cursor visibility */
slouken@4465
   449
    SDL_SetCursor(NULL);
slouken@4465
   450
slouken@4465
   451
    return 0;
slouken@4465
   452
}
slouken@4465
   453
slouken@4465
   454
SDL_bool
slouken@4465
   455
SDL_GetRelativeMouseMode()
slouken@4465
   456
{
slouken@5371
   457
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@4465
   458
slouken@4465
   459
    return mouse->relative_mode;
slouken@4465
   460
}
slouken@4465
   461
slouken@1895
   462
SDL_Cursor *
slouken@1895
   463
SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
slouken@1895
   464
                 int w, int h, int hot_x, int hot_y)
slouken@1895
   465
{
slouken@1895
   466
    SDL_Surface *surface;
slouken@1895
   467
    SDL_Cursor *cursor;
slouken@1895
   468
    int x, y;
slouken@1895
   469
    Uint32 *pixel;
slouken@4465
   470
    Uint8 datab = 0, maskb = 0;
slouken@1895
   471
    const Uint32 black = 0xFF000000;
slouken@1895
   472
    const Uint32 white = 0xFFFFFFFF;
slouken@1895
   473
    const Uint32 transparent = 0x00000000;
slouken@1895
   474
slouken@1895
   475
    /* Make sure the width is a multiple of 8 */
slouken@1895
   476
    w = ((w + 7) & ~7);
slouken@1895
   477
slouken@1895
   478
    /* Create the surface from a bitmap */
slouken@5473
   479
    surface = SDL_CreateRGBSurface(0, w, h, 32,
slouken@5473
   480
                                   0x00FF0000,
slouken@5473
   481
                                   0x0000FF00,
slouken@5473
   482
                                   0x000000FF,
slouken@5473
   483
                                   0xFF000000);
slouken@1895
   484
    if (!surface) {
slouken@1895
   485
        return NULL;
slouken@1895
   486
    }
slouken@1895
   487
    for (y = 0; y < h; ++y) {
slouken@1895
   488
        pixel = (Uint32 *) ((Uint8 *) surface->pixels + y * surface->pitch);
slouken@1895
   489
        for (x = 0; x < w; ++x) {
slouken@1895
   490
            if ((x % 8) == 0) {
slouken@1895
   491
                datab = *data++;
slouken@1895
   492
                maskb = *mask++;
slouken@1895
   493
            }
slouken@1895
   494
            if (maskb & 0x80) {
slouken@1895
   495
                *pixel++ = (datab & 0x80) ? black : white;
slouken@1895
   496
            } else {
slouken@1895
   497
                *pixel++ = (datab & 0x80) ? black : transparent;
slouken@1895
   498
            }
slouken@1895
   499
            datab <<= 1;
slouken@1895
   500
            maskb <<= 1;
slouken@1895
   501
        }
slouken@1895
   502
    }
slouken@1895
   503
slouken@5473
   504
    cursor = SDL_CreateColorCursor(surface, hot_x, hot_y);
slouken@5473
   505
slouken@5473
   506
    SDL_FreeSurface(surface);
slouken@5473
   507
slouken@5473
   508
    return cursor;
slouken@5473
   509
}
slouken@5473
   510
slouken@5473
   511
SDL_Cursor *
slouken@5473
   512
SDL_CreateColorCursor(SDL_Surface *surface, int hot_x, int hot_y)
slouken@5473
   513
{
slouken@5473
   514
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5473
   515
    SDL_Surface *temp = NULL;
slouken@5473
   516
    SDL_Cursor *cursor;
slouken@5473
   517
slouken@5473
   518
    if (!surface) {
slouken@5473
   519
        SDL_SetError("Passed NULL cursor surface");
slouken@5473
   520
        return NULL;
slouken@5473
   521
    }
slouken@5473
   522
slouken@5473
   523
    if (!mouse->CreateCursor) {
slouken@5473
   524
        SDL_SetError("Cursors are not currently supported");
slouken@5473
   525
        return NULL;
slouken@5473
   526
    }
slouken@5473
   527
slouken@5473
   528
    /* Sanity check the hot spot */
slouken@5473
   529
    if ((hot_x < 0) || (hot_y < 0) ||
slouken@5473
   530
        (hot_x >= surface->w) || (hot_y >= surface->h)) {
slouken@5473
   531
        SDL_SetError("Cursor hot spot doesn't lie within cursor");
slouken@5473
   532
        return NULL;
slouken@5473
   533
    }
slouken@5473
   534
slouken@5473
   535
    if (surface->format->format != SDL_PIXELFORMAT_ARGB8888) {
slouken@5473
   536
        temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_ARGB8888, 0);
slouken@5473
   537
        if (!temp) {
slouken@5473
   538
            return NULL;
slouken@5473
   539
        }
slouken@5473
   540
        surface = temp;
slouken@5473
   541
    }
slouken@5473
   542
slouken@1895
   543
    cursor = mouse->CreateCursor(surface, hot_x, hot_y);
slouken@1895
   544
    if (cursor) {
slouken@1895
   545
        cursor->next = mouse->cursors;
slouken@1895
   546
        mouse->cursors = cursor;
slouken@1895
   547
    }
slouken@1895
   548
slouken@5473
   549
    if (temp) {
slouken@5473
   550
        SDL_FreeSurface(temp);
slouken@5473
   551
    }
slouken@1895
   552
slouken@1895
   553
    return cursor;
slouken@1895
   554
}
slouken@1895
   555
mikesart@6675
   556
SDL_Cursor *
mikesart@6675
   557
SDL_CreateSystemCursor(SDL_SystemCursor id)
mikesart@6675
   558
{
mikesart@6675
   559
    SDL_Mouse *mouse = SDL_GetMouse();
mikesart@6675
   560
    SDL_Cursor *cursor;
mikesart@6675
   561
mikesart@6675
   562
    if (!mouse->CreateSystemCursor) {
mikesart@6675
   563
        SDL_SetError("CreateSystemCursor is not currently supported");
mikesart@6675
   564
        return NULL;
mikesart@6675
   565
    }
mikesart@6675
   566
mikesart@6675
   567
	cursor = mouse->CreateSystemCursor(id);
mikesart@6675
   568
    if (cursor) {
mikesart@6675
   569
        cursor->next = mouse->cursors;
mikesart@6675
   570
        mouse->cursors = cursor;
mikesart@6675
   571
    }
mikesart@6675
   572
mikesart@6675
   573
	return cursor;
mikesart@6675
   574
}
mikesart@6675
   575
slouken@1895
   576
/* SDL_SetCursor(NULL) can be used to force the cursor redraw,
slouken@1895
   577
   if this is desired for any reason.  This is used when setting
slouken@1895
   578
   the video mode and when the SDL window gains the mouse focus.
slouken@1895
   579
 */
slouken@1895
   580
void
slouken@1895
   581
SDL_SetCursor(SDL_Cursor * cursor)
slouken@1895
   582
{
slouken@5371
   583
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   584
slouken@1895
   585
    /* Set the new cursor */
slouken@1895
   586
    if (cursor) {
slouken@1895
   587
        /* Make sure the cursor is still valid for this mouse */
slouken@5405
   588
        if (cursor != mouse->def_cursor) {
slouken@5405
   589
            SDL_Cursor *found;
slouken@5405
   590
            for (found = mouse->cursors; found; found = found->next) {
slouken@5405
   591
                if (found == cursor) {
slouken@5405
   592
                    break;
slouken@5405
   593
                }
slouken@1895
   594
            }
slouken@5405
   595
            if (!found) {
slouken@5405
   596
                SDL_SetError("Cursor not associated with the current mouse");
slouken@5405
   597
                return;
slouken@5405
   598
            }
slouken@1895
   599
        }
slouken@1895
   600
        mouse->cur_cursor = cursor;
slouken@1895
   601
    } else {
ghostunderscore@6301
   602
        if (mouse->focus) {
ghostunderscore@6301
   603
            cursor = mouse->cur_cursor;
ghostunderscore@6301
   604
        } else {
ghostunderscore@6301
   605
            cursor = mouse->def_cursor;
ghostunderscore@6301
   606
        }
slouken@1895
   607
    }
slouken@1895
   608
slouken@1895
   609
    if (cursor && mouse->cursor_shown && !mouse->relative_mode) {
slouken@1895
   610
        if (mouse->ShowCursor) {
slouken@1895
   611
            mouse->ShowCursor(cursor);
slouken@1895
   612
        }
slouken@1895
   613
    } else {
slouken@1895
   614
        if (mouse->ShowCursor) {
slouken@1895
   615
            mouse->ShowCursor(NULL);
slouken@1895
   616
        }
slouken@1895
   617
    }
slouken@1895
   618
}
slouken@1895
   619
slouken@1895
   620
SDL_Cursor *
slouken@1895
   621
SDL_GetCursor(void)
slouken@1895
   622
{
slouken@5371
   623
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   624
slouken@1895
   625
    if (!mouse) {
slouken@1895
   626
        return NULL;
slouken@1895
   627
    }
slouken@1895
   628
    return mouse->cur_cursor;
slouken@1895
   629
}
slouken@1895
   630
slouken@1895
   631
void
slouken@1895
   632
SDL_FreeCursor(SDL_Cursor * cursor)
slouken@1895
   633
{
slouken@5371
   634
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   635
    SDL_Cursor *curr, *prev;
slouken@1895
   636
slouken@1895
   637
    if (!cursor) {
slouken@1895
   638
        return;
slouken@1895
   639
    }
slouken@1895
   640
slouken@1895
   641
    if (cursor == mouse->def_cursor) {
slouken@1895
   642
        return;
slouken@1895
   643
    }
slouken@1895
   644
    if (cursor == mouse->cur_cursor) {
slouken@1895
   645
        SDL_SetCursor(mouse->def_cursor);
slouken@1895
   646
    }
slouken@1895
   647
slouken@1895
   648
    for (prev = NULL, curr = mouse->cursors; curr;
slouken@1895
   649
         prev = curr, curr = curr->next) {
slouken@1895
   650
        if (curr == cursor) {
slouken@1895
   651
            if (prev) {
slouken@1895
   652
                prev->next = curr->next;
slouken@1895
   653
            } else {
slouken@1895
   654
                mouse->cursors = curr->next;
slouken@1895
   655
            }
slouken@1895
   656
slouken@1895
   657
            if (mouse->FreeCursor) {
slouken@1895
   658
                mouse->FreeCursor(curr);
slouken@1895
   659
            }
slouken@1895
   660
            return;
slouken@1895
   661
        }
slouken@1895
   662
    }
slouken@1895
   663
}
slouken@1895
   664
slouken@1895
   665
int
slouken@1895
   666
SDL_ShowCursor(int toggle)
slouken@1895
   667
{
slouken@5371
   668
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@1895
   669
    SDL_bool shown;
slouken@1895
   670
slouken@1895
   671
    if (!mouse) {
slouken@1895
   672
        return 0;
slouken@1895
   673
    }
slouken@1895
   674
slouken@1895
   675
    shown = mouse->cursor_shown;
slouken@1895
   676
    if (toggle >= 0) {
slouken@1895
   677
        if (toggle) {
slouken@1895
   678
            mouse->cursor_shown = SDL_TRUE;
slouken@1895
   679
        } else {
slouken@1895
   680
            mouse->cursor_shown = SDL_FALSE;
slouken@1895
   681
        }
slouken@1895
   682
        if (mouse->cursor_shown != shown) {
slouken@1895
   683
            SDL_SetCursor(NULL);
slouken@1895
   684
        }
slouken@1895
   685
    }
slouken@1895
   686
    return shown;
slouken@1895
   687
}
slouken@1895
   688
jorgen@7089
   689
/* vi: set ts=4 sw=4 expandtab: */