src/events/SDL_mouse.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 21 Jan 2010 16:11:55 +0000
changeset 3689 af25b5586af7
parent 3685 64ce267332c6
child 3697 f7b03b6838cb
permissions -rw-r--r--
Fixed crash when there was no mouse focus for some reason (iPhone bug?)
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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@3685
    29
#include "../video/SDL_sysvideo.h"
slouken@0
    30
slouken@0
    31
slouken@2710
    32
static int SDL_num_mice = 0;
slouken@2710
    33
static int SDL_current_mouse = -1;
slouken@2710
    34
static SDL_Mouse **SDL_mice = NULL;
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@1895
    41
    return (0);
slouken@1123
    42
}
slouken@0
    43
slouken@1895
    44
SDL_Mouse *
slouken@1895
    45
SDL_GetMouse(int index)
slouken@460
    46
{
slouken@1895
    47
    if (index < 0 || index >= SDL_num_mice) {
slouken@1895
    48
        return NULL;
slouken@1895
    49
    }
slouken@1895
    50
    return SDL_mice[index];
slouken@460
    51
}
slouken@460
    52
slouken@2940
    53
static int
slouken@2940
    54
SDL_GetMouseIndexId(int id)
slouken@2710
    55
{
slouken@2940
    56
    int index;
slouken@2940
    57
    SDL_Mouse *mouse;
slouken@2940
    58
slouken@2940
    59
    for (index = 0; index < SDL_num_mice; ++index) {
slouken@2940
    60
        mouse = SDL_GetMouse(index);
slouken@2940
    61
        if (mouse->id == id) {
slouken@2940
    62
            return index;
slouken@2940
    63
        }
slouken@2710
    64
    }
slouken@2940
    65
    return -1;
slouken@2710
    66
}
slouken@2710
    67
slouken@2712
    68
int
slouken@2940
    69
SDL_AddMouse(const SDL_Mouse * mouse, char *name, int pressure_max,
slouken@2710
    70
             int pressure_min, int ends)
slouken@0
    71
{
slouken@1895
    72
    SDL_Mouse **mice;
slouken@1895
    73
    int selected_mouse;
slouken@3253
    74
    int index;
slouken@3253
    75
    size_t length;
slouken@2940
    76
slouken@2940
    77
    if (SDL_GetMouseIndexId(mouse->id) != -1) {
slouken@2940
    78
        SDL_SetError("Mouse ID already in use");
slouken@2940
    79
    }
slouken@1895
    80
slouken@1895
    81
    /* Add the mouse to the list of mice */
slouken@2940
    82
    mice = (SDL_Mouse **) SDL_realloc(SDL_mice,
slouken@2940
    83
                                      (SDL_num_mice + 1) * sizeof(*mice));
slouken@2940
    84
    if (!mice) {
slouken@2940
    85
        SDL_OutOfMemory();
slouken@2940
    86
        return -1;
slouken@2940
    87
    }
slouken@1895
    88
slouken@2940
    89
    SDL_mice = mice;
slouken@2940
    90
    index = SDL_num_mice++;
slouken@2940
    91
slouken@1895
    92
    SDL_mice[index] = (SDL_Mouse *) SDL_malloc(sizeof(*SDL_mice[index]));
slouken@1895
    93
    if (!SDL_mice[index]) {
slouken@1895
    94
        SDL_OutOfMemory();
slouken@1895
    95
        return -1;
slouken@1895
    96
    }
slouken@1895
    97
    *SDL_mice[index] = *mouse;
slouken@1895
    98
slouken@2710
    99
    /* we're setting the mouse properties */
slouken@2710
   100
    length = 0;
slouken@2710
   101
    length = SDL_strlen(name);
bob@3195
   102
    SDL_mice[index]->focus = 0;
kazeuser@2718
   103
    SDL_mice[index]->name = SDL_malloc((length + 2) * sizeof(char));
slouken@2725
   104
    SDL_strlcpy(SDL_mice[index]->name, name, length + 1);
slouken@2710
   105
    SDL_mice[index]->pressure_max = pressure_max;
slouken@2710
   106
    SDL_mice[index]->pressure_min = pressure_min;
slouken@1895
   107
    SDL_mice[index]->cursor_shown = SDL_TRUE;
slouken@1895
   108
    selected_mouse = SDL_SelectMouse(index);
slouken@1895
   109
    SDL_mice[index]->cur_cursor = NULL;
slouken@1895
   110
    SDL_mice[index]->def_cursor =
slouken@1895
   111
        SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH,
slouken@1895
   112
                         DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY);
slouken@1895
   113
    SDL_SetCursor(SDL_mice[index]->def_cursor);
slouken@2710
   114
    /* we're assuming that all mice are in the computer sensing zone */
slouken@2710
   115
    SDL_mice[index]->proximity = SDL_TRUE;
slouken@2710
   116
    /* we're assuming that all mice are working in the absolute position mode
slouken@2710
   117
       thanx to that, the users that don't want to use many mice don't have to
slouken@2710
   118
       worry about anything */
slouken@2710
   119
    SDL_mice[index]->relative_mode = SDL_FALSE;
slouken@2710
   120
    SDL_mice[index]->current_end = 0;
slouken@2710
   121
    SDL_mice[index]->total_ends = ends;
slouken@1895
   122
    SDL_SelectMouse(selected_mouse);
slouken@1895
   123
slouken@1895
   124
    return index;
slouken@0
   125
}
slouken@0
   126
slouken@1895
   127
void
slouken@1895
   128
SDL_DelMouse(int index)
slouken@0
   129
{
slouken@1895
   130
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   131
slouken@1895
   132
    if (!mouse) {
slouken@1895
   133
        return;
slouken@1895
   134
    }
slouken@1895
   135
slouken@1895
   136
    mouse->def_cursor = NULL;
slouken@2710
   137
    SDL_free(mouse->name);
slouken@1895
   138
    while (mouse->cursors) {
slouken@1895
   139
        SDL_FreeCursor(mouse->cursors);
slouken@1895
   140
    }
slouken@1895
   141
slouken@1895
   142
    if (mouse->FreeMouse) {
slouken@1895
   143
        mouse->FreeMouse(mouse);
slouken@1895
   144
    }
slouken@1895
   145
    SDL_free(mouse);
slouken@1895
   146
slouken@1895
   147
    SDL_mice[index] = NULL;
slouken@0
   148
}
slouken@0
   149
slouken@1895
   150
void
slouken@1895
   151
SDL_ResetMouse(int index)
slouken@0
   152
{
slouken@1895
   153
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   154
slouken@1895
   155
    if (!mouse) {
slouken@1895
   156
        return;
slouken@1895
   157
    }
slouken@1895
   158
slouken@1895
   159
    /* FIXME */
slouken@0
   160
}
slouken@0
   161
slouken@1895
   162
void
slouken@1895
   163
SDL_MouseQuit(void)
slouken@0
   164
{
slouken@1895
   165
    int i;
slouken@0
   166
slouken@1895
   167
    for (i = 0; i < SDL_num_mice; ++i) {
slouken@1895
   168
        SDL_DelMouse(i);
slouken@1895
   169
    }
slouken@1895
   170
    SDL_num_mice = 0;
slouken@2765
   171
    SDL_current_mouse = -1;
slouken@0
   172
slouken@1895
   173
    if (SDL_mice) {
slouken@1895
   174
        SDL_free(SDL_mice);
slouken@1895
   175
        SDL_mice = NULL;
slouken@1895
   176
    }
slouken@0
   177
}
slouken@0
   178
slouken@1895
   179
int
slouken@1895
   180
SDL_GetNumMice(void)
slouken@0
   181
{
slouken@1895
   182
    return SDL_num_mice;
slouken@0
   183
}
slouken@0
   184
slouken@1895
   185
int
slouken@1895
   186
SDL_SelectMouse(int index)
slouken@1895
   187
{
slouken@1895
   188
    if (index >= 0 && index < SDL_num_mice) {
slouken@1895
   189
        SDL_current_mouse = index;
slouken@1895
   190
    }
slouken@1895
   191
    return SDL_current_mouse;
slouken@1895
   192
}
slouken@1895
   193
slouken@3685
   194
SDL_Window *
slouken@2710
   195
SDL_GetMouseFocusWindow(int index)
slouken@1895
   196
{
slouken@2710
   197
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   198
slouken@1895
   199
    if (!mouse) {
slouken@1895
   200
        return 0;
slouken@1895
   201
    }
slouken@1895
   202
    return mouse->focus;
slouken@1895
   203
}
slouken@1895
   204
icculus@2049
   205
static int SDLCALL
slouken@1895
   206
FlushMouseMotion(void *param, SDL_Event * event)
slouken@1895
   207
{
slouken@1895
   208
    if (event->type == SDL_MOUSEMOTION
slouken@1895
   209
        && event->motion.which == (Uint8) SDL_current_mouse) {
slouken@1895
   210
        return 0;
slouken@1895
   211
    } else {
slouken@1895
   212
        return 1;
slouken@1895
   213
    }
slouken@1895
   214
}
slouken@1895
   215
slouken@1895
   216
int
slouken@2710
   217
SDL_SetRelativeMouseMode(int index, SDL_bool enabled)
slouken@1895
   218
{
slouken@2710
   219
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   220
slouken@1895
   221
    if (!mouse) {
slouken@1895
   222
        return -1;
slouken@1895
   223
    }
slouken@1895
   224
slouken@1895
   225
    /* Flush pending mouse motion */
slouken@1895
   226
    mouse->flush_motion = SDL_TRUE;
slouken@1895
   227
    SDL_PumpEvents();
slouken@1895
   228
    mouse->flush_motion = SDL_FALSE;
slouken@1895
   229
    SDL_FilterEvents(FlushMouseMotion, mouse);
slouken@1895
   230
slouken@1895
   231
    /* Set the relative mode */
slouken@1895
   232
    mouse->relative_mode = enabled;
slouken@1895
   233
slouken@1895
   234
    /* Update cursor visibility */
slouken@1895
   235
    SDL_SetCursor(NULL);
slouken@1895
   236
slouken@1895
   237
    if (!enabled) {
slouken@1895
   238
        /* Restore the expected mouse position */
slouken@1895
   239
        SDL_WarpMouseInWindow(mouse->focus, mouse->x, mouse->y);
slouken@1895
   240
    }
slouken@1895
   241
    return 0;
slouken@1895
   242
}
slouken@1895
   243
slouken@1895
   244
SDL_bool
slouken@2710
   245
SDL_GetRelativeMouseMode(int index)
slouken@1895
   246
{
slouken@2710
   247
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   248
slouken@1895
   249
    if (!mouse) {
slouken@1895
   250
        return SDL_FALSE;
slouken@1895
   251
    }
slouken@1895
   252
    return mouse->relative_mode;
slouken@1895
   253
}
slouken@1895
   254
slouken@1895
   255
Uint8
icculus@3585
   256
SDL_GetMouseState(int *x, int *y)
slouken@1895
   257
{
icculus@3585
   258
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   259
slouken@1895
   260
    if (!mouse) {
slouken@1895
   261
        if (x) {
slouken@1895
   262
            *x = 0;
slouken@1895
   263
        }
slouken@1895
   264
        if (y) {
slouken@1895
   265
            *y = 0;
slouken@1895
   266
        }
slouken@1895
   267
        return 0;
slouken@1895
   268
    }
slouken@1895
   269
slouken@1895
   270
    if (x) {
slouken@1895
   271
        *x = mouse->x;
slouken@1895
   272
    }
slouken@1895
   273
    if (y) {
slouken@1895
   274
        *y = mouse->y;
slouken@1895
   275
    }
slouken@1895
   276
    return mouse->buttonstate;
slouken@1895
   277
}
slouken@1895
   278
slouken@1895
   279
Uint8
slouken@2710
   280
SDL_GetRelativeMouseState(int index, int *x, int *y)
slouken@1895
   281
{
slouken@2710
   282
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   283
slouken@1895
   284
    if (!mouse) {
slouken@1895
   285
        if (x) {
slouken@1895
   286
            *x = 0;
slouken@1895
   287
        }
slouken@1895
   288
        if (y) {
slouken@1895
   289
            *y = 0;
slouken@1895
   290
        }
slouken@1895
   291
        return 0;
slouken@1895
   292
    }
slouken@1895
   293
slouken@1895
   294
    if (x) {
slouken@1895
   295
        *x = mouse->xdelta;
slouken@1895
   296
    }
slouken@1895
   297
    if (y) {
slouken@1895
   298
        *y = mouse->ydelta;
slouken@1895
   299
    }
slouken@1895
   300
    mouse->xdelta = 0;
slouken@1895
   301
    mouse->ydelta = 0;
slouken@1895
   302
    return mouse->buttonstate;
slouken@1895
   303
}
slouken@1895
   304
slouken@1895
   305
void
slouken@3685
   306
SDL_SetMouseFocus(int id, SDL_Window * window)
slouken@1895
   307
{
slouken@2712
   308
    int index = SDL_GetMouseIndexId(id);
slouken@2712
   309
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@2712
   310
    int i;
slouken@1895
   311
    SDL_bool focus;
slouken@1895
   312
slouken@3685
   313
    if (!mouse || (mouse->focus == window)) {
slouken@1895
   314
        return;
slouken@1895
   315
    }
slouken@1895
   316
slouken@1895
   317
    /* See if the current window has lost focus */
slouken@1895
   318
    if (mouse->focus) {
slouken@1895
   319
        focus = SDL_FALSE;
slouken@1895
   320
        for (i = 0; i < SDL_num_mice; ++i) {
slouken@1895
   321
            SDL_Mouse *check;
slouken@1895
   322
            if (i != index) {
slouken@1895
   323
                check = SDL_GetMouse(i);
slouken@1895
   324
                if (check && check->focus == mouse->focus) {
slouken@1895
   325
                    focus = SDL_TRUE;
slouken@1895
   326
                    break;
slouken@1895
   327
                }
slouken@1895
   328
            }
slouken@1895
   329
        }
slouken@1895
   330
        if (!focus) {
slouken@1895
   331
            SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_LEAVE, 0, 0);
slouken@1895
   332
        }
slouken@1895
   333
    }
slouken@1895
   334
slouken@3685
   335
    mouse->focus = window;
slouken@1895
   336
slouken@1895
   337
    if (mouse->focus) {
slouken@1895
   338
        focus = SDL_FALSE;
slouken@1895
   339
        for (i = 0; i < SDL_num_mice; ++i) {
slouken@1895
   340
            SDL_Mouse *check;
slouken@1895
   341
            if (i != index) {
slouken@1895
   342
                check = SDL_GetMouse(i);
slouken@1895
   343
                if (check && check->focus == mouse->focus) {
slouken@1895
   344
                    focus = SDL_TRUE;
slouken@1895
   345
                    break;
slouken@1895
   346
                }
slouken@1895
   347
            }
slouken@1895
   348
        }
slouken@1895
   349
        if (!focus) {
slouken@1895
   350
            SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_ENTER, 0, 0);
slouken@1895
   351
        }
slouken@1895
   352
    }
slouken@1895
   353
}
slouken@1895
   354
slouken@1895
   355
int
slouken@2710
   356
SDL_SendProximity(int id, int x, int y, int type)
slouken@1895
   357
{
slouken@2712
   358
    int index = SDL_GetMouseIndexId(id);
slouken@2712
   359
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@2710
   360
    int posted = 0;
slouken@2727
   361
slouken@2727
   362
    if (!mouse) {
slouken@2727
   363
        return 0;
slouken@2727
   364
    }
slouken@2727
   365
slouken@2860
   366
    mouse->last_x = x;
slouken@2860
   367
    mouse->last_y = y;
slouken@2710
   368
    if (SDL_ProcessEvents[type] == SDL_ENABLE) {
slouken@2710
   369
        SDL_Event event;
slouken@2710
   370
        event.proximity.which = (Uint8) index;
slouken@2710
   371
        event.proximity.x = x;
slouken@2710
   372
        event.proximity.y = y;
slouken@2710
   373
        event.proximity.cursor = mouse->current_end;
slouken@2710
   374
        event.proximity.type = type;
slouken@3191
   375
        /* FIXME: is this right? */
slouken@3689
   376
        event.proximity.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@2710
   377
        posted = (SDL_PushEvent(&event) > 0);
slouken@2710
   378
        if (type == SDL_PROXIMITYIN) {
slouken@2710
   379
            mouse->proximity = SDL_TRUE;
slouken@2710
   380
        } else {
slouken@2710
   381
            mouse->proximity = SDL_FALSE;
slouken@2710
   382
        }
slouken@2710
   383
    }
slouken@2710
   384
    return posted;
slouken@2710
   385
}
slouken@2710
   386
slouken@2710
   387
int
slouken@2710
   388
SDL_SendMouseMotion(int id, int relative, int x, int y, int pressure)
slouken@2710
   389
{
slouken@2712
   390
    int index = SDL_GetMouseIndexId(id);
slouken@2712
   391
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   392
    int posted;
slouken@1895
   393
    int xrel;
slouken@1895
   394
    int yrel;
slouken@2860
   395
    int x_max = 0, y_max = 0;
slouken@1895
   396
slouken@1895
   397
    if (!mouse || mouse->flush_motion) {
slouken@1895
   398
        return 0;
slouken@1895
   399
    }
slouken@1895
   400
slouken@2710
   401
    /* if the mouse is out of proximity we don't to want to have any motion from it */
slouken@2710
   402
    if (mouse->proximity == SDL_FALSE) {
slouken@2860
   403
        mouse->last_x = x;
slouken@2860
   404
        mouse->last_y = y;
slouken@2710
   405
        return 0;
slouken@1895
   406
    }
slouken@1895
   407
slouken@2710
   408
    /* the relative motion is calculated regarding the system cursor last position */
slouken@2842
   409
    if (relative) {
slouken@2842
   410
        xrel = x;
slouken@2842
   411
        yrel = y;
slouken@2860
   412
        x = (mouse->last_x + x);
slouken@2860
   413
        y = (mouse->last_y + y);
slouken@2842
   414
    } else {
slouken@2860
   415
        xrel = x - mouse->last_x;
slouken@2860
   416
        yrel = y - mouse->last_y;
slouken@2842
   417
    }
slouken@2710
   418
slouken@1895
   419
    /* Drop events that don't change state */
slouken@1895
   420
    if (!xrel && !yrel) {
slouken@1895
   421
#if 0
slouken@1895
   422
        printf("Mouse event didn't change state - dropped!\n");
slouken@1895
   423
#endif
slouken@1895
   424
        return 0;
slouken@1895
   425
    }
slouken@1895
   426
slouken@2710
   427
    /* Update internal mouse coordinates */
slouken@2710
   428
    if (mouse->relative_mode == SDL_FALSE) {
slouken@1895
   429
        mouse->x = x;
slouken@1895
   430
        mouse->y = y;
slouken@2710
   431
    } else {
slouken@2860
   432
        mouse->x += xrel;
slouken@2860
   433
        mouse->y += yrel;
slouken@2860
   434
    }
slouken@2849
   435
slouken@2860
   436
    SDL_GetWindowSize(mouse->focus, &x_max, &y_max);
slouken@2849
   437
slouken@2860
   438
    /* make sure that the pointers find themselves inside the windows */
slouken@2860
   439
    /* only check if mouse->xmax is set ! */
slouken@2860
   440
    if (x_max && mouse->x > x_max) {
slouken@2860
   441
        mouse->x = x_max;
slouken@2860
   442
    } else if (mouse->x < 0) {
slouken@2860
   443
        mouse->x = 0;
slouken@1895
   444
    }
slouken@2860
   445
slouken@2860
   446
    if (y_max && mouse->y > y_max) {
slouken@2860
   447
        mouse->y = y_max;
slouken@2860
   448
    } else if (mouse->y < 0) {
slouken@2860
   449
        mouse->y = 0;
slouken@2860
   450
    }
slouken@2860
   451
slouken@1895
   452
    mouse->xdelta += xrel;
slouken@1895
   453
    mouse->ydelta += yrel;
slouken@2710
   454
    mouse->pressure = pressure;
slouken@1895
   455
slouken@1895
   456
    /* Move the mouse cursor, if needed */
slouken@1895
   457
    if (mouse->cursor_shown && !mouse->relative_mode &&
slouken@1895
   458
        mouse->MoveCursor && mouse->cur_cursor) {
slouken@1895
   459
        mouse->MoveCursor(mouse->cur_cursor);
slouken@1895
   460
    }
slouken@1895
   461
slouken@1895
   462
    /* Post the event, if desired */
slouken@1895
   463
    posted = 0;
slouken@2710
   464
    if (SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE &&
slouken@2710
   465
        mouse->proximity == SDL_TRUE) {
slouken@1895
   466
        SDL_Event event;
slouken@1895
   467
        event.motion.type = SDL_MOUSEMOTION;
slouken@1895
   468
        event.motion.which = (Uint8) index;
slouken@1895
   469
        event.motion.state = mouse->buttonstate;
slouken@1895
   470
        event.motion.x = mouse->x;
slouken@1895
   471
        event.motion.y = mouse->y;
bob@3176
   472
        event.motion.z = mouse->z;
slouken@2710
   473
        event.motion.pressure = mouse->pressure;
bob@3179
   474
        event.motion.pressure_max = mouse->pressure_max;
bob@3179
   475
        event.motion.pressure_min = mouse->pressure_min;
bob@3179
   476
        event.motion.rotation = 0;
slouken@3572
   477
        event.motion.tilt_x = 0;
slouken@3572
   478
        event.motion.tilt_y = 0;
bob@3179
   479
        event.motion.cursor = mouse->current_end;
slouken@1895
   480
        event.motion.xrel = xrel;
slouken@1895
   481
        event.motion.yrel = yrel;
slouken@3689
   482
        event.motion.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@1895
   483
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   484
    }
lestat@3112
   485
    mouse->last_x = mouse->x;
lestat@3112
   486
    mouse->last_y = mouse->y;
slouken@1895
   487
    return posted;
slouken@1895
   488
}
slouken@1895
   489
slouken@1895
   490
int
slouken@2710
   491
SDL_SendMouseButton(int id, Uint8 state, Uint8 button)
slouken@1895
   492
{
slouken@2712
   493
    int index = SDL_GetMouseIndexId(id);
slouken@2712
   494
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   495
    int posted;
slouken@1895
   496
    Uint8 type;
slouken@1895
   497
slouken@1895
   498
    if (!mouse) {
slouken@1895
   499
        return 0;
slouken@1895
   500
    }
slouken@1895
   501
slouken@1895
   502
    /* Figure out which event to perform */
slouken@1895
   503
    switch (state) {
slouken@1895
   504
    case SDL_PRESSED:
slouken@1895
   505
        if (mouse->buttonstate & SDL_BUTTON(button)) {
slouken@1895
   506
            /* Ignore this event, no state change */
slouken@1895
   507
            return 0;
slouken@1895
   508
        }
slouken@1895
   509
        type = SDL_MOUSEBUTTONDOWN;
slouken@1895
   510
        mouse->buttonstate |= SDL_BUTTON(button);
slouken@1895
   511
        break;
slouken@1895
   512
    case SDL_RELEASED:
slouken@2725
   513
        if (!(mouse->buttonstate & SDL_BUTTON(button))) {
kazeuser@2718
   514
            /* Ignore this event, no state change */
kazeuser@2718
   515
            return 0;
kazeuser@2718
   516
        }
slouken@1895
   517
        type = SDL_MOUSEBUTTONUP;
slouken@1895
   518
        mouse->buttonstate &= ~SDL_BUTTON(button);
slouken@1895
   519
        break;
slouken@1895
   520
    default:
slouken@1895
   521
        /* Invalid state -- bail */
slouken@1895
   522
        return 0;
slouken@1895
   523
    }
slouken@1895
   524
slouken@1895
   525
    /* Post the event, if desired */
slouken@1895
   526
    posted = 0;
slouken@1895
   527
    if (SDL_ProcessEvents[type] == SDL_ENABLE) {
slouken@1895
   528
        SDL_Event event;
slouken@1895
   529
        event.type = type;
slouken@1895
   530
        event.button.which = (Uint8) index;
slouken@1895
   531
        event.button.state = state;
slouken@1895
   532
        event.button.button = button;
slouken@1895
   533
        event.button.x = mouse->x;
slouken@1895
   534
        event.button.y = mouse->y;
slouken@3689
   535
        event.button.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@1895
   536
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   537
    }
slouken@1895
   538
    return posted;
slouken@1895
   539
}
slouken@1895
   540
slouken@1895
   541
int
slouken@2152
   542
SDL_SendMouseWheel(int index, int x, int y)
slouken@1895
   543
{
slouken@1895
   544
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   545
    int posted;
slouken@1895
   546
slouken@2152
   547
    if (!mouse || (!x && !y)) {
slouken@1895
   548
        return 0;
slouken@1895
   549
    }
slouken@1895
   550
slouken@1895
   551
    /* Post the event, if desired */
slouken@1895
   552
    posted = 0;
slouken@1895
   553
    if (SDL_ProcessEvents[SDL_MOUSEWHEEL] == SDL_ENABLE) {
slouken@1895
   554
        SDL_Event event;
slouken@1895
   555
        event.type = SDL_MOUSEWHEEL;
slouken@1895
   556
        event.wheel.which = (Uint8) index;
slouken@2152
   557
        event.wheel.x = x;
slouken@2152
   558
        event.wheel.y = y;
slouken@3689
   559
        event.wheel.windowID = mouse->focus ? mouse->focus->id : 0;
slouken@1895
   560
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   561
    }
slouken@1895
   562
    return posted;
slouken@1895
   563
}
slouken@1895
   564
slouken@1895
   565
void
slouken@3685
   566
SDL_WarpMouseInWindow(SDL_Window * window, int x, int y)
slouken@1895
   567
{
slouken@1895
   568
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   569
slouken@1895
   570
    if (!mouse) {
slouken@1895
   571
        return;
slouken@1895
   572
    }
slouken@1895
   573
slouken@1895
   574
    if (mouse->WarpMouse) {
slouken@3685
   575
        mouse->WarpMouse(mouse, window, x, y);
slouken@1895
   576
    } else {
slouken@3685
   577
        SDL_SetMouseFocus(SDL_current_mouse, window);
slouken@2710
   578
        SDL_SendMouseMotion(SDL_current_mouse, 0, x, y, 0);
slouken@1895
   579
    }
slouken@1895
   580
}
slouken@1895
   581
slouken@1895
   582
SDL_Cursor *
slouken@1895
   583
SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
slouken@1895
   584
                 int w, int h, int hot_x, int hot_y)
slouken@1895
   585
{
slouken@1895
   586
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   587
    SDL_Surface *surface;
slouken@1895
   588
    SDL_Cursor *cursor;
slouken@1895
   589
    int x, y;
slouken@1895
   590
    Uint32 *pixel;
slouken@1895
   591
    Uint8 datab, maskb;
slouken@1895
   592
    const Uint32 black = 0xFF000000;
slouken@1895
   593
    const Uint32 white = 0xFFFFFFFF;
slouken@1895
   594
    const Uint32 transparent = 0x00000000;
slouken@1895
   595
slouken@1895
   596
    if (!mouse) {
slouken@1895
   597
        SDL_SetError("No mice are initialized");
slouken@1895
   598
        return NULL;
slouken@1895
   599
    }
slouken@1895
   600
slouken@1895
   601
    if (!mouse->CreateCursor) {
slouken@1895
   602
        SDL_SetError("Current mouse doesn't have cursor support");
slouken@1895
   603
        return NULL;
slouken@1895
   604
    }
slouken@1895
   605
slouken@1895
   606
    /* Sanity check the hot spot */
slouken@1895
   607
    if ((hot_x < 0) || (hot_y < 0) || (hot_x >= w) || (hot_y >= h)) {
slouken@1895
   608
        SDL_SetError("Cursor hot spot doesn't lie within cursor");
slouken@1895
   609
        return NULL;
slouken@1895
   610
    }
slouken@1895
   611
slouken@1895
   612
    /* Make sure the width is a multiple of 8 */
slouken@1895
   613
    w = ((w + 7) & ~7);
slouken@1895
   614
slouken@1895
   615
    /* Create the surface from a bitmap */
slouken@1895
   616
    surface =
slouken@1895
   617
        SDL_CreateRGBSurface(0, w, h, 32, 0x00FF0000, 0x0000FF00, 0x000000FF,
slouken@1895
   618
                             0xFF000000);
slouken@1895
   619
    if (!surface) {
slouken@1895
   620
        return NULL;
slouken@1895
   621
    }
slouken@1895
   622
    for (y = 0; y < h; ++y) {
slouken@1895
   623
        pixel = (Uint32 *) ((Uint8 *) surface->pixels + y * surface->pitch);
slouken@1895
   624
        for (x = 0; x < w; ++x) {
slouken@1895
   625
            if ((x % 8) == 0) {
slouken@1895
   626
                datab = *data++;
slouken@1895
   627
                maskb = *mask++;
slouken@1895
   628
            }
slouken@1895
   629
            if (maskb & 0x80) {
slouken@1895
   630
                *pixel++ = (datab & 0x80) ? black : white;
slouken@1895
   631
            } else {
slouken@1895
   632
                *pixel++ = (datab & 0x80) ? black : transparent;
slouken@1895
   633
            }
slouken@1895
   634
            datab <<= 1;
slouken@1895
   635
            maskb <<= 1;
slouken@1895
   636
        }
slouken@1895
   637
    }
slouken@1895
   638
slouken@1895
   639
    cursor = mouse->CreateCursor(surface, hot_x, hot_y);
slouken@1895
   640
    if (cursor) {
slouken@1895
   641
        cursor->mouse = mouse;
slouken@1895
   642
        cursor->next = mouse->cursors;
slouken@1895
   643
        mouse->cursors = cursor;
slouken@1895
   644
    }
slouken@1895
   645
slouken@1895
   646
    SDL_FreeSurface(surface);
slouken@1895
   647
slouken@1895
   648
    return cursor;
slouken@1895
   649
}
slouken@1895
   650
slouken@1895
   651
/* SDL_SetCursor(NULL) can be used to force the cursor redraw,
slouken@1895
   652
   if this is desired for any reason.  This is used when setting
slouken@1895
   653
   the video mode and when the SDL window gains the mouse focus.
slouken@1895
   654
 */
slouken@1895
   655
void
slouken@1895
   656
SDL_SetCursor(SDL_Cursor * cursor)
slouken@1895
   657
{
slouken@1895
   658
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   659
slouken@1895
   660
    if (!mouse) {
slouken@1895
   661
        SDL_SetError("No mice are initialized");
slouken@1895
   662
        return;
slouken@1895
   663
    }
slouken@1895
   664
slouken@1895
   665
    /* Set the new cursor */
slouken@1895
   666
    if (cursor) {
slouken@1895
   667
        /* Make sure the cursor is still valid for this mouse */
slouken@1895
   668
        SDL_Cursor *found;
slouken@1895
   669
        for (found = mouse->cursors; found; found = found->next) {
slouken@1895
   670
            if (found == cursor) {
slouken@1895
   671
                break;
slouken@1895
   672
            }
slouken@1895
   673
        }
slouken@1895
   674
        if (!found) {
slouken@1895
   675
            SDL_SetError("Cursor not associated with the current mouse");
slouken@1895
   676
            return;
slouken@1895
   677
        }
slouken@1895
   678
        mouse->cur_cursor = cursor;
slouken@1895
   679
    } else {
slouken@1895
   680
        cursor = mouse->cur_cursor;
slouken@1895
   681
    }
slouken@1895
   682
slouken@1895
   683
    if (cursor && mouse->cursor_shown && !mouse->relative_mode) {
slouken@1895
   684
        if (mouse->ShowCursor) {
slouken@1895
   685
            mouse->ShowCursor(cursor);
slouken@1895
   686
        }
slouken@1895
   687
    } else {
slouken@1895
   688
        if (mouse->ShowCursor) {
slouken@1895
   689
            mouse->ShowCursor(NULL);
slouken@1895
   690
        }
slouken@1895
   691
    }
slouken@1895
   692
}
slouken@1895
   693
slouken@1895
   694
SDL_Cursor *
slouken@1895
   695
SDL_GetCursor(void)
slouken@1895
   696
{
slouken@1895
   697
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   698
slouken@1895
   699
    if (!mouse) {
slouken@1895
   700
        return NULL;
slouken@1895
   701
    }
slouken@1895
   702
    return mouse->cur_cursor;
slouken@1895
   703
}
slouken@1895
   704
slouken@1895
   705
void
slouken@1895
   706
SDL_FreeCursor(SDL_Cursor * cursor)
slouken@1895
   707
{
slouken@1895
   708
    SDL_Mouse *mouse;
slouken@1895
   709
    SDL_Cursor *curr, *prev;
slouken@1895
   710
slouken@1895
   711
    if (!cursor) {
slouken@1895
   712
        return;
slouken@1895
   713
    }
slouken@1895
   714
    mouse = cursor->mouse;
slouken@1895
   715
slouken@1895
   716
    if (cursor == mouse->def_cursor) {
slouken@1895
   717
        return;
slouken@1895
   718
    }
slouken@1895
   719
    if (cursor == mouse->cur_cursor) {
slouken@1895
   720
        SDL_SetCursor(mouse->def_cursor);
slouken@1895
   721
    }
slouken@1895
   722
slouken@1895
   723
    for (prev = NULL, curr = mouse->cursors; curr;
slouken@1895
   724
         prev = curr, curr = curr->next) {
slouken@1895
   725
        if (curr == cursor) {
slouken@1895
   726
            if (prev) {
slouken@1895
   727
                prev->next = curr->next;
slouken@1895
   728
            } else {
slouken@1895
   729
                mouse->cursors = curr->next;
slouken@1895
   730
            }
slouken@1895
   731
slouken@1895
   732
            if (mouse->FreeCursor) {
slouken@1895
   733
                mouse->FreeCursor(curr);
slouken@1895
   734
            }
slouken@1895
   735
            return;
slouken@1895
   736
        }
slouken@1895
   737
    }
slouken@1895
   738
}
slouken@1895
   739
slouken@1895
   740
int
slouken@1895
   741
SDL_ShowCursor(int toggle)
slouken@1895
   742
{
slouken@1895
   743
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   744
    SDL_bool shown;
slouken@1895
   745
slouken@1895
   746
    if (!mouse) {
slouken@1895
   747
        return 0;
slouken@1895
   748
    }
slouken@1895
   749
slouken@1895
   750
    shown = mouse->cursor_shown;
slouken@1895
   751
    if (toggle >= 0) {
slouken@1895
   752
        if (toggle) {
slouken@1895
   753
            mouse->cursor_shown = SDL_TRUE;
slouken@1895
   754
        } else {
slouken@1895
   755
            mouse->cursor_shown = SDL_FALSE;
slouken@1895
   756
        }
slouken@1895
   757
        if (mouse->cursor_shown != shown) {
slouken@1895
   758
            SDL_SetCursor(NULL);
slouken@1895
   759
        }
slouken@1895
   760
    }
slouken@1895
   761
    return shown;
slouken@1895
   762
}
slouken@1895
   763
slouken@2710
   764
char *
slouken@2710
   765
SDL_GetMouseName(int index)
slouken@2710
   766
{
slouken@2710
   767
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@2710
   768
    if (!mouse) {
slouken@2710
   769
        return NULL;
slouken@2710
   770
    }
slouken@2710
   771
    return mouse->name;
slouken@2710
   772
}
slouken@2710
   773
slouken@2710
   774
void
slouken@2710
   775
SDL_ChangeEnd(int id, int end)
slouken@2710
   776
{
slouken@2712
   777
    int index = SDL_GetMouseIndexId(id);
slouken@2712
   778
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@2710
   779
slouken@2710
   780
    if (mouse) {
slouken@2710
   781
        mouse->current_end = end;
slouken@2710
   782
    }
slouken@2710
   783
}
slouken@2710
   784
slouken@2710
   785
int
slouken@2710
   786
SDL_GetCursorsNumber(int index)
slouken@2710
   787
{
slouken@2710
   788
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@2710
   789
slouken@2710
   790
    if (!mouse) {
slouken@2710
   791
        return -1;
slouken@2710
   792
    }
slouken@2710
   793
    return mouse->total_ends;
slouken@2710
   794
}
slouken@2710
   795
slouken@2710
   796
int
slouken@2710
   797
SDL_GetCurrentCursor(int index)
slouken@2710
   798
{
slouken@2710
   799
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@2710
   800
slouken@2710
   801
    if (!mouse) {
slouken@2710
   802
        return -1;
slouken@2710
   803
    }
slouken@2710
   804
    return mouse->current_end;
slouken@2710
   805
}
slouken@2710
   806
slouken@1895
   807
/* vi: set ts=4 sw=4 expandtab: */