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