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