src/events/SDL_mouse.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 16 Dec 2009 19:50:51 +0000
changeset 3585 f8816ffa210b
parent 3572 6bb9952d5029
child 3685 64ce267332c6
permissions -rw-r--r--
Initial band-aids on SDL_GetMouseState() API breakage. More work to come.

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