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