src/events/SDL_mouse.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 17 Oct 2006 09:15:21 +0000
changeset 2049 5f6550e5184f
parent 1895 c121d94672cb
child 2152 003c1b5b07da
permissions -rw-r--r--
Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* General mouse handling code for SDL */
slouken@0
    25
slouken@0
    26
#include "SDL_events.h"
slouken@0
    27
#include "SDL_events_c.h"
slouken@1895
    28
#include "default_cursor.h"
slouken@0
    29
slouken@0
    30
slouken@1895
    31
static int SDL_num_mice;
slouken@1895
    32
static int SDL_current_mouse;
slouken@1895
    33
static SDL_Mouse **SDL_mice;
slouken@0
    34
slouken@0
    35
slouken@0
    36
/* Public functions */
slouken@1895
    37
int
slouken@1895
    38
SDL_MouseInit(void)
slouken@0
    39
{
slouken@1895
    40
    return (0);
slouken@1123
    41
}
slouken@0
    42
slouken@1895
    43
SDL_Mouse *
slouken@1895
    44
SDL_GetMouse(int index)
slouken@460
    45
{
slouken@1895
    46
    if (index < 0 || index >= SDL_num_mice) {
slouken@1895
    47
        return NULL;
slouken@1895
    48
    }
slouken@1895
    49
    return SDL_mice[index];
slouken@460
    50
}
slouken@460
    51
slouken@1895
    52
int
slouken@1895
    53
SDL_AddMouse(const SDL_Mouse * mouse, int index)
slouken@0
    54
{
slouken@1895
    55
    SDL_Mouse **mice;
slouken@1895
    56
    int selected_mouse;
slouken@1895
    57
slouken@1895
    58
    /* Add the mouse to the list of mice */
slouken@1895
    59
    if (index < 0 || index >= SDL_num_mice || SDL_mice[index]) {
slouken@1895
    60
        mice =
slouken@1895
    61
            (SDL_Mouse **) SDL_realloc(SDL_mice,
slouken@1895
    62
                                       (SDL_num_mice + 1) * sizeof(*mice));
slouken@1895
    63
        if (!mice) {
slouken@1895
    64
            SDL_OutOfMemory();
slouken@1895
    65
            return -1;
slouken@1895
    66
        }
slouken@1895
    67
slouken@1895
    68
        SDL_mice = mice;
slouken@1895
    69
        index = SDL_num_mice++;
slouken@1895
    70
    }
slouken@1895
    71
    SDL_mice[index] = (SDL_Mouse *) SDL_malloc(sizeof(*SDL_mice[index]));
slouken@1895
    72
    if (!SDL_mice[index]) {
slouken@1895
    73
        SDL_OutOfMemory();
slouken@1895
    74
        return -1;
slouken@1895
    75
    }
slouken@1895
    76
    *SDL_mice[index] = *mouse;
slouken@1895
    77
slouken@1895
    78
    /* Create the default cursor for the mouse */
slouken@1895
    79
    SDL_mice[index]->cursor_shown = SDL_TRUE;
slouken@1895
    80
    selected_mouse = SDL_SelectMouse(index);
slouken@1895
    81
    SDL_mice[index]->cur_cursor = NULL;
slouken@1895
    82
    SDL_mice[index]->def_cursor =
slouken@1895
    83
        SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH,
slouken@1895
    84
                         DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY);
slouken@1895
    85
    SDL_SetCursor(SDL_mice[index]->def_cursor);
slouken@1895
    86
    SDL_SelectMouse(selected_mouse);
slouken@1895
    87
slouken@1895
    88
    return index;
slouken@0
    89
}
slouken@0
    90
slouken@1895
    91
void
slouken@1895
    92
SDL_DelMouse(int index)
slouken@0
    93
{
slouken@1895
    94
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
    95
slouken@1895
    96
    if (!mouse) {
slouken@1895
    97
        return;
slouken@1895
    98
    }
slouken@1895
    99
slouken@1895
   100
    mouse->def_cursor = NULL;
slouken@1895
   101
    while (mouse->cursors) {
slouken@1895
   102
        SDL_FreeCursor(mouse->cursors);
slouken@1895
   103
    }
slouken@1895
   104
slouken@1895
   105
    if (mouse->FreeMouse) {
slouken@1895
   106
        mouse->FreeMouse(mouse);
slouken@1895
   107
    }
slouken@1895
   108
    SDL_free(mouse);
slouken@1895
   109
slouken@1895
   110
    SDL_mice[index] = NULL;
slouken@0
   111
}
slouken@0
   112
slouken@1895
   113
void
slouken@1895
   114
SDL_ResetMouse(int index)
slouken@0
   115
{
slouken@1895
   116
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   117
slouken@1895
   118
    if (!mouse) {
slouken@1895
   119
        return;
slouken@1895
   120
    }
slouken@1895
   121
slouken@1895
   122
    /* FIXME */
slouken@0
   123
}
slouken@0
   124
slouken@1895
   125
void
slouken@1895
   126
SDL_MouseQuit(void)
slouken@0
   127
{
slouken@1895
   128
    int i;
slouken@0
   129
slouken@1895
   130
    for (i = 0; i < SDL_num_mice; ++i) {
slouken@1895
   131
        SDL_DelMouse(i);
slouken@1895
   132
    }
slouken@1895
   133
    SDL_num_mice = 0;
slouken@1895
   134
    SDL_current_mouse = 0;
slouken@0
   135
slouken@1895
   136
    if (SDL_mice) {
slouken@1895
   137
        SDL_free(SDL_mice);
slouken@1895
   138
        SDL_mice = NULL;
slouken@1895
   139
    }
slouken@0
   140
}
slouken@0
   141
slouken@1895
   142
int
slouken@1895
   143
SDL_GetNumMice(void)
slouken@0
   144
{
slouken@1895
   145
    return SDL_num_mice;
slouken@0
   146
}
slouken@0
   147
slouken@1895
   148
int
slouken@1895
   149
SDL_SelectMouse(int index)
slouken@1895
   150
{
slouken@1895
   151
    if (index >= 0 && index < SDL_num_mice) {
slouken@1895
   152
        SDL_current_mouse = index;
slouken@1895
   153
    }
slouken@1895
   154
    return SDL_current_mouse;
slouken@1895
   155
}
slouken@1895
   156
slouken@1895
   157
SDL_WindowID
slouken@1895
   158
SDL_GetMouseFocusWindow()
slouken@1895
   159
{
slouken@1895
   160
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   161
slouken@1895
   162
    if (!mouse) {
slouken@1895
   163
        return 0;
slouken@1895
   164
    }
slouken@1895
   165
    return mouse->focus;
slouken@1895
   166
}
slouken@1895
   167
icculus@2049
   168
static int SDLCALL
slouken@1895
   169
FlushMouseMotion(void *param, SDL_Event * event)
slouken@1895
   170
{
slouken@1895
   171
    if (event->type == SDL_MOUSEMOTION
slouken@1895
   172
        && event->motion.which == (Uint8) SDL_current_mouse) {
slouken@1895
   173
        return 0;
slouken@1895
   174
    } else {
slouken@1895
   175
        return 1;
slouken@1895
   176
    }
slouken@1895
   177
}
slouken@1895
   178
slouken@1895
   179
int
slouken@1895
   180
SDL_SetRelativeMouseMode(SDL_bool enabled)
slouken@1895
   181
{
slouken@1895
   182
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   183
slouken@1895
   184
    if (!mouse) {
slouken@1895
   185
        return -1;
slouken@1895
   186
    }
slouken@1895
   187
slouken@1895
   188
    /* Flush pending mouse motion */
slouken@1895
   189
    mouse->flush_motion = SDL_TRUE;
slouken@1895
   190
    SDL_PumpEvents();
slouken@1895
   191
    mouse->flush_motion = SDL_FALSE;
slouken@1895
   192
    SDL_FilterEvents(FlushMouseMotion, mouse);
slouken@1895
   193
slouken@1895
   194
    /* Set the relative mode */
slouken@1895
   195
    mouse->relative_mode = enabled;
slouken@1895
   196
slouken@1895
   197
    /* Update cursor visibility */
slouken@1895
   198
    SDL_SetCursor(NULL);
slouken@1895
   199
slouken@1895
   200
    if (!enabled) {
slouken@1895
   201
        /* Restore the expected mouse position */
slouken@1895
   202
        SDL_WarpMouseInWindow(mouse->focus, mouse->x, mouse->y);
slouken@1895
   203
    }
slouken@1895
   204
    return 0;
slouken@1895
   205
}
slouken@1895
   206
slouken@1895
   207
SDL_bool
slouken@1895
   208
SDL_GetRelativeMouseMode()
slouken@1895
   209
{
slouken@1895
   210
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   211
slouken@1895
   212
    if (!mouse) {
slouken@1895
   213
        return SDL_FALSE;
slouken@1895
   214
    }
slouken@1895
   215
    return mouse->relative_mode;
slouken@1895
   216
}
slouken@1895
   217
slouken@1895
   218
Uint8
slouken@1895
   219
SDL_GetMouseState(int *x, int *y)
slouken@1895
   220
{
slouken@1895
   221
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   222
slouken@1895
   223
    if (!mouse) {
slouken@1895
   224
        if (x) {
slouken@1895
   225
            *x = 0;
slouken@1895
   226
        }
slouken@1895
   227
        if (y) {
slouken@1895
   228
            *y = 0;
slouken@1895
   229
        }
slouken@1895
   230
        return 0;
slouken@1895
   231
    }
slouken@1895
   232
slouken@1895
   233
    if (x) {
slouken@1895
   234
        *x = mouse->x;
slouken@1895
   235
    }
slouken@1895
   236
    if (y) {
slouken@1895
   237
        *y = mouse->y;
slouken@1895
   238
    }
slouken@1895
   239
    return mouse->buttonstate;
slouken@1895
   240
}
slouken@1895
   241
slouken@1895
   242
Uint8
slouken@1895
   243
SDL_GetRelativeMouseState(int *x, int *y)
slouken@1895
   244
{
slouken@1895
   245
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   246
slouken@1895
   247
    if (!mouse) {
slouken@1895
   248
        if (x) {
slouken@1895
   249
            *x = 0;
slouken@1895
   250
        }
slouken@1895
   251
        if (y) {
slouken@1895
   252
            *y = 0;
slouken@1895
   253
        }
slouken@1895
   254
        return 0;
slouken@1895
   255
    }
slouken@1895
   256
slouken@1895
   257
    if (x) {
slouken@1895
   258
        *x = mouse->xdelta;
slouken@1895
   259
    }
slouken@1895
   260
    if (y) {
slouken@1895
   261
        *y = mouse->ydelta;
slouken@1895
   262
    }
slouken@1895
   263
    mouse->xdelta = 0;
slouken@1895
   264
    mouse->ydelta = 0;
slouken@1895
   265
    return mouse->buttonstate;
slouken@1895
   266
}
slouken@1895
   267
slouken@1895
   268
void
slouken@1895
   269
SDL_SetMouseFocus(int index, SDL_WindowID windowID)
slouken@1895
   270
{
slouken@1895
   271
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   272
    int i;
slouken@1895
   273
    SDL_bool focus;
slouken@1895
   274
slouken@1895
   275
    if (!mouse || (mouse->focus == windowID)) {
slouken@1895
   276
        return;
slouken@1895
   277
    }
slouken@1895
   278
slouken@1895
   279
    /* See if the current window has lost focus */
slouken@1895
   280
    if (mouse->focus) {
slouken@1895
   281
        focus = SDL_FALSE;
slouken@1895
   282
        for (i = 0; i < SDL_num_mice; ++i) {
slouken@1895
   283
            SDL_Mouse *check;
slouken@1895
   284
            if (i != index) {
slouken@1895
   285
                check = SDL_GetMouse(i);
slouken@1895
   286
                if (check && check->focus == mouse->focus) {
slouken@1895
   287
                    focus = SDL_TRUE;
slouken@1895
   288
                    break;
slouken@1895
   289
                }
slouken@1895
   290
            }
slouken@1895
   291
        }
slouken@1895
   292
        if (!focus) {
slouken@1895
   293
            SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_LEAVE, 0, 0);
slouken@1895
   294
        }
slouken@1895
   295
    }
slouken@1895
   296
slouken@1895
   297
    mouse->focus = windowID;
slouken@1895
   298
slouken@1895
   299
    if (mouse->focus) {
slouken@1895
   300
        focus = SDL_FALSE;
slouken@1895
   301
        for (i = 0; i < SDL_num_mice; ++i) {
slouken@1895
   302
            SDL_Mouse *check;
slouken@1895
   303
            if (i != index) {
slouken@1895
   304
                check = SDL_GetMouse(i);
slouken@1895
   305
                if (check && check->focus == mouse->focus) {
slouken@1895
   306
                    focus = SDL_TRUE;
slouken@1895
   307
                    break;
slouken@1895
   308
                }
slouken@1895
   309
            }
slouken@1895
   310
        }
slouken@1895
   311
        if (!focus) {
slouken@1895
   312
            SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_ENTER, 0, 0);
slouken@1895
   313
        }
slouken@1895
   314
    }
slouken@1895
   315
}
slouken@1895
   316
slouken@1895
   317
int
slouken@1895
   318
SDL_SendMouseMotion(int index, int relative, int x, int y)
slouken@1895
   319
{
slouken@1895
   320
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   321
    int posted;
slouken@1895
   322
    int xrel;
slouken@1895
   323
    int yrel;
slouken@1895
   324
slouken@1895
   325
    if (!mouse || mouse->flush_motion) {
slouken@1895
   326
        return 0;
slouken@1895
   327
    }
slouken@1895
   328
slouken@1895
   329
    if (relative) {
slouken@1895
   330
        /* Push the cursor around */
slouken@1895
   331
        xrel = x;
slouken@1895
   332
        yrel = y;
slouken@1895
   333
        x = (mouse->x + xrel);
slouken@1895
   334
        y = (mouse->y + yrel);
slouken@1895
   335
    } else {
slouken@1895
   336
        xrel = x - mouse->x;
slouken@1895
   337
        yrel = y - mouse->y;
slouken@1895
   338
    }
slouken@1895
   339
slouken@1895
   340
    /* Drop events that don't change state */
slouken@1895
   341
    if (!xrel && !yrel) {
slouken@1895
   342
#if 0
slouken@1895
   343
        printf("Mouse event didn't change state - dropped!\n");
slouken@1895
   344
#endif
slouken@1895
   345
        return 0;
slouken@1895
   346
    }
slouken@1895
   347
slouken@1895
   348
    /* Update internal mouse state */
slouken@1895
   349
    if (!mouse->relative_mode) {
slouken@1895
   350
        mouse->x = x;
slouken@1895
   351
        mouse->y = y;
slouken@1895
   352
    }
slouken@1895
   353
    mouse->xdelta += xrel;
slouken@1895
   354
    mouse->ydelta += yrel;
slouken@1895
   355
slouken@1895
   356
    /* Move the mouse cursor, if needed */
slouken@1895
   357
    if (mouse->cursor_shown && !mouse->relative_mode &&
slouken@1895
   358
        mouse->MoveCursor && mouse->cur_cursor) {
slouken@1895
   359
        mouse->MoveCursor(mouse->cur_cursor);
slouken@1895
   360
    }
slouken@1895
   361
slouken@1895
   362
    /* Post the event, if desired */
slouken@1895
   363
    posted = 0;
slouken@1895
   364
    if (SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE) {
slouken@1895
   365
        SDL_Event event;
slouken@1895
   366
        event.motion.type = SDL_MOUSEMOTION;
slouken@1895
   367
        event.motion.which = (Uint8) index;
slouken@1895
   368
        event.motion.state = mouse->buttonstate;
slouken@1895
   369
        event.motion.x = mouse->x;
slouken@1895
   370
        event.motion.y = mouse->y;
slouken@1895
   371
        event.motion.xrel = xrel;
slouken@1895
   372
        event.motion.yrel = yrel;
slouken@1895
   373
        event.motion.windowID = mouse->focus;
slouken@1895
   374
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   375
    }
slouken@1895
   376
    return posted;
slouken@1895
   377
}
slouken@1895
   378
slouken@1895
   379
int
slouken@1895
   380
SDL_SendMouseButton(int index, Uint8 state, Uint8 button)
slouken@1895
   381
{
slouken@1895
   382
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   383
    int posted;
slouken@1895
   384
    Uint8 type;
slouken@1895
   385
slouken@1895
   386
    if (!mouse) {
slouken@1895
   387
        return 0;
slouken@1895
   388
    }
slouken@1895
   389
slouken@1895
   390
    /* Figure out which event to perform */
slouken@1895
   391
    switch (state) {
slouken@1895
   392
    case SDL_PRESSED:
slouken@1895
   393
        if (mouse->buttonstate & SDL_BUTTON(button)) {
slouken@1895
   394
            /* Ignore this event, no state change */
slouken@1895
   395
            return 0;
slouken@1895
   396
        }
slouken@1895
   397
        type = SDL_MOUSEBUTTONDOWN;
slouken@1895
   398
        mouse->buttonstate |= SDL_BUTTON(button);
slouken@1895
   399
        break;
slouken@1895
   400
    case SDL_RELEASED:
slouken@1895
   401
        if (!(mouse->buttonstate & SDL_BUTTON(button))) {
slouken@1895
   402
            /* Ignore this event, no state change */
slouken@1895
   403
            return 0;
slouken@1895
   404
        }
slouken@1895
   405
        type = SDL_MOUSEBUTTONUP;
slouken@1895
   406
        mouse->buttonstate &= ~SDL_BUTTON(button);
slouken@1895
   407
        break;
slouken@1895
   408
    default:
slouken@1895
   409
        /* Invalid state -- bail */
slouken@1895
   410
        return 0;
slouken@1895
   411
    }
slouken@1895
   412
slouken@1895
   413
    /* Post the event, if desired */
slouken@1895
   414
    posted = 0;
slouken@1895
   415
    if (SDL_ProcessEvents[type] == SDL_ENABLE) {
slouken@1895
   416
        SDL_Event event;
slouken@1895
   417
        event.type = type;
slouken@1895
   418
        event.button.which = (Uint8) index;
slouken@1895
   419
        event.button.state = state;
slouken@1895
   420
        event.button.button = button;
slouken@1895
   421
        event.button.x = mouse->x;
slouken@1895
   422
        event.button.y = mouse->y;
slouken@1895
   423
        event.button.windowID = mouse->focus;
slouken@1895
   424
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   425
    }
slouken@1895
   426
    return posted;
slouken@1895
   427
}
slouken@1895
   428
slouken@1895
   429
int
slouken@1895
   430
SDL_SendMouseWheel(int index, int motion)
slouken@1895
   431
{
slouken@1895
   432
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   433
    int posted;
slouken@1895
   434
slouken@1895
   435
    if (!mouse || !motion) {
slouken@1895
   436
        return 0;
slouken@1895
   437
    }
slouken@1895
   438
slouken@1895
   439
    /* Post the event, if desired */
slouken@1895
   440
    posted = 0;
slouken@1895
   441
    if (SDL_ProcessEvents[SDL_MOUSEWHEEL] == SDL_ENABLE) {
slouken@1895
   442
        SDL_Event event;
slouken@1895
   443
        event.type = SDL_MOUSEWHEEL;
slouken@1895
   444
        event.wheel.which = (Uint8) index;
slouken@1895
   445
        event.wheel.motion = motion;
slouken@1895
   446
        event.wheel.windowID = mouse->focus;
slouken@1895
   447
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   448
    }
slouken@1895
   449
    return posted;
slouken@1895
   450
}
slouken@1895
   451
slouken@1895
   452
void
slouken@1895
   453
SDL_WarpMouseInWindow(SDL_WindowID windowID, int x, int y)
slouken@1895
   454
{
slouken@1895
   455
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   456
slouken@1895
   457
    if (!mouse) {
slouken@1895
   458
        return;
slouken@1895
   459
    }
slouken@1895
   460
slouken@1895
   461
    if (mouse->WarpMouse) {
slouken@1895
   462
        mouse->WarpMouse(mouse, windowID, x, y);
slouken@1895
   463
    } else {
slouken@1895
   464
        SDL_SetMouseFocus(SDL_current_mouse, windowID);
slouken@1895
   465
        SDL_SendMouseMotion(SDL_current_mouse, 0, x, y);
slouken@1895
   466
    }
slouken@1895
   467
}
slouken@1895
   468
slouken@1895
   469
SDL_Cursor *
slouken@1895
   470
SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
slouken@1895
   471
                 int w, int h, int hot_x, int hot_y)
slouken@1895
   472
{
slouken@1895
   473
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   474
    SDL_Surface *surface;
slouken@1895
   475
    SDL_Cursor *cursor;
slouken@1895
   476
    int x, y;
slouken@1895
   477
    Uint32 *pixel;
slouken@1895
   478
    Uint8 datab, maskb;
slouken@1895
   479
    const Uint32 black = 0xFF000000;
slouken@1895
   480
    const Uint32 white = 0xFFFFFFFF;
slouken@1895
   481
    const Uint32 transparent = 0x00000000;
slouken@1895
   482
slouken@1895
   483
    if (!mouse) {
slouken@1895
   484
        SDL_SetError("No mice are initialized");
slouken@1895
   485
        return NULL;
slouken@1895
   486
    }
slouken@1895
   487
slouken@1895
   488
    if (!mouse->CreateCursor) {
slouken@1895
   489
        SDL_SetError("Current mouse doesn't have cursor support");
slouken@1895
   490
        return NULL;
slouken@1895
   491
    }
slouken@1895
   492
slouken@1895
   493
    /* Sanity check the hot spot */
slouken@1895
   494
    if ((hot_x < 0) || (hot_y < 0) || (hot_x >= w) || (hot_y >= h)) {
slouken@1895
   495
        SDL_SetError("Cursor hot spot doesn't lie within cursor");
slouken@1895
   496
        return NULL;
slouken@1895
   497
    }
slouken@1895
   498
slouken@1895
   499
    /* Make sure the width is a multiple of 8 */
slouken@1895
   500
    w = ((w + 7) & ~7);
slouken@1895
   501
slouken@1895
   502
    /* Create the surface from a bitmap */
slouken@1895
   503
    surface =
slouken@1895
   504
        SDL_CreateRGBSurface(0, w, h, 32, 0x00FF0000, 0x0000FF00, 0x000000FF,
slouken@1895
   505
                             0xFF000000);
slouken@1895
   506
    if (!surface) {
slouken@1895
   507
        return NULL;
slouken@1895
   508
    }
slouken@1895
   509
    for (y = 0; y < h; ++y) {
slouken@1895
   510
        pixel = (Uint32 *) ((Uint8 *) surface->pixels + y * surface->pitch);
slouken@1895
   511
        for (x = 0; x < w; ++x) {
slouken@1895
   512
            if ((x % 8) == 0) {
slouken@1895
   513
                datab = *data++;
slouken@1895
   514
                maskb = *mask++;
slouken@1895
   515
            }
slouken@1895
   516
            if (maskb & 0x80) {
slouken@1895
   517
                *pixel++ = (datab & 0x80) ? black : white;
slouken@1895
   518
            } else {
slouken@1895
   519
                *pixel++ = (datab & 0x80) ? black : transparent;
slouken@1895
   520
            }
slouken@1895
   521
            datab <<= 1;
slouken@1895
   522
            maskb <<= 1;
slouken@1895
   523
        }
slouken@1895
   524
    }
slouken@1895
   525
slouken@1895
   526
    cursor = mouse->CreateCursor(surface, hot_x, hot_y);
slouken@1895
   527
    if (cursor) {
slouken@1895
   528
        cursor->mouse = mouse;
slouken@1895
   529
        cursor->next = mouse->cursors;
slouken@1895
   530
        mouse->cursors = cursor;
slouken@1895
   531
    }
slouken@1895
   532
slouken@1895
   533
    SDL_FreeSurface(surface);
slouken@1895
   534
slouken@1895
   535
    return cursor;
slouken@1895
   536
}
slouken@1895
   537
slouken@1895
   538
/* SDL_SetCursor(NULL) can be used to force the cursor redraw,
slouken@1895
   539
   if this is desired for any reason.  This is used when setting
slouken@1895
   540
   the video mode and when the SDL window gains the mouse focus.
slouken@1895
   541
 */
slouken@1895
   542
void
slouken@1895
   543
SDL_SetCursor(SDL_Cursor * cursor)
slouken@1895
   544
{
slouken@1895
   545
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   546
slouken@1895
   547
    if (!mouse) {
slouken@1895
   548
        SDL_SetError("No mice are initialized");
slouken@1895
   549
        return;
slouken@1895
   550
    }
slouken@1895
   551
slouken@1895
   552
    /* Set the new cursor */
slouken@1895
   553
    if (cursor) {
slouken@1895
   554
        /* Make sure the cursor is still valid for this mouse */
slouken@1895
   555
        SDL_Cursor *found;
slouken@1895
   556
        for (found = mouse->cursors; found; found = found->next) {
slouken@1895
   557
            if (found == cursor) {
slouken@1895
   558
                break;
slouken@1895
   559
            }
slouken@1895
   560
        }
slouken@1895
   561
        if (!found) {
slouken@1895
   562
            SDL_SetError("Cursor not associated with the current mouse");
slouken@1895
   563
            return;
slouken@1895
   564
        }
slouken@1895
   565
        mouse->cur_cursor = cursor;
slouken@1895
   566
    } else {
slouken@1895
   567
        cursor = mouse->cur_cursor;
slouken@1895
   568
    }
slouken@1895
   569
slouken@1895
   570
    if (cursor && mouse->cursor_shown && !mouse->relative_mode) {
slouken@1895
   571
        if (mouse->ShowCursor) {
slouken@1895
   572
            mouse->ShowCursor(cursor);
slouken@1895
   573
        }
slouken@1895
   574
    } else {
slouken@1895
   575
        if (mouse->ShowCursor) {
slouken@1895
   576
            mouse->ShowCursor(NULL);
slouken@1895
   577
        }
slouken@1895
   578
    }
slouken@1895
   579
}
slouken@1895
   580
slouken@1895
   581
SDL_Cursor *
slouken@1895
   582
SDL_GetCursor(void)
slouken@1895
   583
{
slouken@1895
   584
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   585
slouken@1895
   586
    if (!mouse) {
slouken@1895
   587
        return NULL;
slouken@1895
   588
    }
slouken@1895
   589
    return mouse->cur_cursor;
slouken@1895
   590
}
slouken@1895
   591
slouken@1895
   592
void
slouken@1895
   593
SDL_FreeCursor(SDL_Cursor * cursor)
slouken@1895
   594
{
slouken@1895
   595
    SDL_Mouse *mouse;
slouken@1895
   596
    SDL_Cursor *curr, *prev;
slouken@1895
   597
slouken@1895
   598
    if (!cursor) {
slouken@1895
   599
        return;
slouken@1895
   600
    }
slouken@1895
   601
    mouse = cursor->mouse;
slouken@1895
   602
slouken@1895
   603
    if (cursor == mouse->def_cursor) {
slouken@1895
   604
        return;
slouken@1895
   605
    }
slouken@1895
   606
    if (cursor == mouse->cur_cursor) {
slouken@1895
   607
        SDL_SetCursor(mouse->def_cursor);
slouken@1895
   608
    }
slouken@1895
   609
slouken@1895
   610
    for (prev = NULL, curr = mouse->cursors; curr;
slouken@1895
   611
         prev = curr, curr = curr->next) {
slouken@1895
   612
        if (curr == cursor) {
slouken@1895
   613
            if (prev) {
slouken@1895
   614
                prev->next = curr->next;
slouken@1895
   615
            } else {
slouken@1895
   616
                mouse->cursors = curr->next;
slouken@1895
   617
            }
slouken@1895
   618
slouken@1895
   619
            if (mouse->FreeCursor) {
slouken@1895
   620
                mouse->FreeCursor(curr);
slouken@1895
   621
            }
slouken@1895
   622
            return;
slouken@1895
   623
        }
slouken@1895
   624
    }
slouken@1895
   625
}
slouken@1895
   626
slouken@1895
   627
int
slouken@1895
   628
SDL_ShowCursor(int toggle)
slouken@1895
   629
{
slouken@1895
   630
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   631
    SDL_bool shown;
slouken@1895
   632
slouken@1895
   633
    if (!mouse) {
slouken@1895
   634
        return 0;
slouken@1895
   635
    }
slouken@1895
   636
slouken@1895
   637
    shown = mouse->cursor_shown;
slouken@1895
   638
    if (toggle >= 0) {
slouken@1895
   639
        if (toggle) {
slouken@1895
   640
            mouse->cursor_shown = SDL_TRUE;
slouken@1895
   641
        } else {
slouken@1895
   642
            mouse->cursor_shown = SDL_FALSE;
slouken@1895
   643
        }
slouken@1895
   644
        if (mouse->cursor_shown != shown) {
slouken@1895
   645
            SDL_SetCursor(NULL);
slouken@1895
   646
        }
slouken@1895
   647
    }
slouken@1895
   648
    return shown;
slouken@1895
   649
}
slouken@1895
   650
slouken@1895
   651
/* vi: set ts=4 sw=4 expandtab: */