src/events/SDL_mouse.c
author Szymon Wilczek <kazeuser@gmail.com>
Thu, 03 Jul 2008 22:03:58 +0000
branchgsoc2008_manymouse
changeset 3764 2970fcfbdd54
parent 3763 81ea7d9a6624
child 3765 ed9b7fe8f902
permissions -rw-r--r--
Relative mode for tablets. Info on wiki.
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@0
    41
{
slouken@1895
    42
    return (0);
slouken@1123
    43
}
slouken@0
    44
slouken@1895
    45
SDL_Mouse *
slouken@1895
    46
SDL_GetMouse(int index)
slouken@460
    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@460
    52
}
slouken@460
    53
slouken@1895
    54
int
kazeuser@3760
    55
SDL_AddMouse(const SDL_Mouse * mouse, int index, char* name)
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@1895
    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);
slouken@1895
    81
    SDL_mice[index]->cursor_shown = SDL_TRUE;
slouken@1895
    82
    selected_mouse = SDL_SelectMouse(index);
slouken@1895
    83
    SDL_mice[index]->cur_cursor = NULL;
slouken@1895
    84
    SDL_mice[index]->def_cursor =
slouken@1895
    85
        SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH,
slouken@1895
    86
                         DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY);
slouken@1895
    87
    SDL_SetCursor(SDL_mice[index]->def_cursor);
kazeuser@3764
    88
    SDL_mice[index]->proximity=SDL_TRUE;
kazeuser@3764
    89
    SDL_mice[index]->relative_mode=SDL_FALSE;
slouken@1895
    90
    SDL_SelectMouse(selected_mouse);
slouken@1895
    91
slouken@1895
    92
    return index;
slouken@0
    93
}
slouken@0
    94
slouken@1895
    95
void
slouken@1895
    96
SDL_DelMouse(int index)
slouken@0
    97
{
slouken@1895
    98
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
    99
slouken@1895
   100
    if (!mouse) {
slouken@1895
   101
        return;
slouken@1895
   102
    }
slouken@1895
   103
slouken@1895
   104
    mouse->def_cursor = NULL;
kazeuser@3760
   105
    SDL_free(mouse->name);
slouken@1895
   106
    while (mouse->cursors) {
slouken@1895
   107
        SDL_FreeCursor(mouse->cursors);
slouken@1895
   108
    }
slouken@1895
   109
slouken@1895
   110
    if (mouse->FreeMouse) {
slouken@1895
   111
        mouse->FreeMouse(mouse);
slouken@1895
   112
    }
slouken@1895
   113
    SDL_free(mouse);
slouken@1895
   114
slouken@1895
   115
    SDL_mice[index] = NULL;
slouken@0
   116
}
slouken@0
   117
slouken@1895
   118
void
slouken@1895
   119
SDL_ResetMouse(int index)
slouken@0
   120
{
slouken@1895
   121
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   122
slouken@1895
   123
    if (!mouse) {
slouken@1895
   124
        return;
slouken@1895
   125
    }
slouken@1895
   126
slouken@1895
   127
    /* FIXME */
slouken@0
   128
}
slouken@0
   129
slouken@1895
   130
void
slouken@1895
   131
SDL_MouseQuit(void)
slouken@0
   132
{
slouken@1895
   133
    int i;
slouken@0
   134
slouken@1895
   135
    for (i = 0; i < SDL_num_mice; ++i) {
slouken@1895
   136
        SDL_DelMouse(i);
slouken@1895
   137
    }
slouken@1895
   138
    SDL_num_mice = 0;
slouken@1895
   139
    SDL_current_mouse = 0;
slouken@0
   140
slouken@1895
   141
    if (SDL_mice) {
slouken@1895
   142
        SDL_free(SDL_mice);
slouken@1895
   143
        SDL_mice = NULL;
slouken@1895
   144
    }
slouken@0
   145
}
slouken@0
   146
slouken@1895
   147
int
slouken@1895
   148
SDL_GetNumMice(void)
slouken@0
   149
{
slouken@1895
   150
    return SDL_num_mice;
slouken@0
   151
}
slouken@0
   152
slouken@1895
   153
int
slouken@1895
   154
SDL_SelectMouse(int index)
slouken@1895
   155
{
slouken@1895
   156
    if (index >= 0 && index < SDL_num_mice) {
slouken@1895
   157
        SDL_current_mouse = index;
slouken@1895
   158
    }
slouken@1895
   159
    return SDL_current_mouse;
slouken@1895
   160
}
slouken@1895
   161
slouken@1895
   162
SDL_WindowID
slouken@1895
   163
SDL_GetMouseFocusWindow()
slouken@1895
   164
{
slouken@1895
   165
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   166
slouken@1895
   167
    if (!mouse) {
slouken@1895
   168
        return 0;
slouken@1895
   169
    }
slouken@1895
   170
    return mouse->focus;
slouken@1895
   171
}
slouken@1895
   172
icculus@2049
   173
static int SDLCALL
slouken@1895
   174
FlushMouseMotion(void *param, SDL_Event * event)
slouken@1895
   175
{
slouken@1895
   176
    if (event->type == SDL_MOUSEMOTION
slouken@1895
   177
        && event->motion.which == (Uint8) SDL_current_mouse) {
slouken@1895
   178
        return 0;
slouken@1895
   179
    } else {
slouken@1895
   180
        return 1;
slouken@1895
   181
    }
slouken@1895
   182
}
slouken@1895
   183
slouken@1895
   184
int
kazeuser@3764
   185
SDL_SetRelativeMouseMode(SDL_bool enabled, int index)
slouken@1895
   186
{
kazeuser@3764
   187
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   188
slouken@1895
   189
    if (!mouse) {
slouken@1895
   190
        return -1;
slouken@1895
   191
    }
slouken@1895
   192
slouken@1895
   193
    /* Flush pending mouse motion */
slouken@1895
   194
    mouse->flush_motion = SDL_TRUE;
slouken@1895
   195
    SDL_PumpEvents();
slouken@1895
   196
    mouse->flush_motion = SDL_FALSE;
slouken@1895
   197
    SDL_FilterEvents(FlushMouseMotion, mouse);
slouken@1895
   198
slouken@1895
   199
    /* Set the relative mode */
slouken@1895
   200
    mouse->relative_mode = enabled;
slouken@1895
   201
slouken@1895
   202
    /* Update cursor visibility */
slouken@1895
   203
    SDL_SetCursor(NULL);
slouken@1895
   204
slouken@1895
   205
    if (!enabled) {
slouken@1895
   206
        /* Restore the expected mouse position */
slouken@1895
   207
        SDL_WarpMouseInWindow(mouse->focus, mouse->x, mouse->y);
slouken@1895
   208
    }
slouken@1895
   209
    return 0;
slouken@1895
   210
}
slouken@1895
   211
slouken@1895
   212
SDL_bool
slouken@1895
   213
SDL_GetRelativeMouseMode()
slouken@1895
   214
{
slouken@1895
   215
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   216
slouken@1895
   217
    if (!mouse) {
slouken@1895
   218
        return SDL_FALSE;
slouken@1895
   219
    }
slouken@1895
   220
    return mouse->relative_mode;
slouken@1895
   221
}
slouken@1895
   222
slouken@1895
   223
Uint8
slouken@1895
   224
SDL_GetMouseState(int *x, int *y)
slouken@1895
   225
{
slouken@1895
   226
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   227
slouken@1895
   228
    if (!mouse) {
slouken@1895
   229
        if (x) {
slouken@1895
   230
            *x = 0;
slouken@1895
   231
        }
slouken@1895
   232
        if (y) {
slouken@1895
   233
            *y = 0;
slouken@1895
   234
        }
slouken@1895
   235
        return 0;
slouken@1895
   236
    }
slouken@1895
   237
slouken@1895
   238
    if (x) {
slouken@1895
   239
        *x = mouse->x;
slouken@1895
   240
    }
slouken@1895
   241
    if (y) {
slouken@1895
   242
        *y = mouse->y;
slouken@1895
   243
    }
slouken@1895
   244
    return mouse->buttonstate;
slouken@1895
   245
}
slouken@1895
   246
slouken@1895
   247
Uint8
slouken@1895
   248
SDL_GetRelativeMouseState(int *x, int *y)
slouken@1895
   249
{
slouken@1895
   250
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   251
slouken@1895
   252
    if (!mouse) {
slouken@1895
   253
        if (x) {
slouken@1895
   254
            *x = 0;
slouken@1895
   255
        }
slouken@1895
   256
        if (y) {
slouken@1895
   257
            *y = 0;
slouken@1895
   258
        }
slouken@1895
   259
        return 0;
slouken@1895
   260
    }
slouken@1895
   261
slouken@1895
   262
    if (x) {
slouken@1895
   263
        *x = mouse->xdelta;
slouken@1895
   264
    }
slouken@1895
   265
    if (y) {
slouken@1895
   266
        *y = mouse->ydelta;
slouken@1895
   267
    }
slouken@1895
   268
    mouse->xdelta = 0;
slouken@1895
   269
    mouse->ydelta = 0;
slouken@1895
   270
    return mouse->buttonstate;
slouken@1895
   271
}
slouken@1895
   272
slouken@1895
   273
void
kazeuser@3760
   274
SDL_SetMouseFocus(int id, SDL_WindowID windowID)
slouken@1895
   275
{
kazeuser@3760
   276
    int index = SDL_GetIndexById(id);
slouken@1895
   277
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   278
    int i;
slouken@1895
   279
    SDL_bool focus;
slouken@1895
   280
slouken@1895
   281
    if (!mouse || (mouse->focus == windowID)) {
slouken@1895
   282
        return;
slouken@1895
   283
    }
slouken@1895
   284
slouken@1895
   285
    /* See if the current window has lost focus */
slouken@1895
   286
    if (mouse->focus) {
slouken@1895
   287
        focus = SDL_FALSE;
slouken@1895
   288
        for (i = 0; i < SDL_num_mice; ++i) {
slouken@1895
   289
            SDL_Mouse *check;
slouken@1895
   290
            if (i != index) {
slouken@1895
   291
                check = SDL_GetMouse(i);
slouken@1895
   292
                if (check && check->focus == mouse->focus) {
slouken@1895
   293
                    focus = SDL_TRUE;
slouken@1895
   294
                    break;
slouken@1895
   295
                }
slouken@1895
   296
            }
slouken@1895
   297
        }
slouken@1895
   298
        if (!focus) {
slouken@1895
   299
            SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_LEAVE, 0, 0);
slouken@1895
   300
        }
slouken@1895
   301
    }
slouken@1895
   302
slouken@1895
   303
    mouse->focus = windowID;
slouken@1895
   304
slouken@1895
   305
    if (mouse->focus) {
slouken@1895
   306
        focus = SDL_FALSE;
slouken@1895
   307
        for (i = 0; i < SDL_num_mice; ++i) {
slouken@1895
   308
            SDL_Mouse *check;
slouken@1895
   309
            if (i != index) {
slouken@1895
   310
                check = SDL_GetMouse(i);
slouken@1895
   311
                if (check && check->focus == mouse->focus) {
slouken@1895
   312
                    focus = SDL_TRUE;
slouken@1895
   313
                    break;
slouken@1895
   314
                }
slouken@1895
   315
            }
slouken@1895
   316
        }
slouken@1895
   317
        if (!focus) {
slouken@1895
   318
            SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_ENTER, 0, 0);
slouken@1895
   319
        }
slouken@1895
   320
    }
slouken@1895
   321
}
slouken@1895
   322
slouken@1895
   323
int
kazeuser@3763
   324
SDL_SendProximity(int id, int x, int y, int type)
kazeuser@3763
   325
{
kazeuser@3763
   326
    int index=SDL_GetIndexById(id);
kazeuser@3763
   327
    int posted=0;
kazeuser@3763
   328
    if(SDL_ProcessEvents[type]==SDL_ENABLE)
kazeuser@3763
   329
    {
kazeuser@3763
   330
        SDL_Event event;
kazeuser@3764
   331
        event.proximity.which=(Uint8)index;
kazeuser@3763
   332
        event.proximity.x=x;
kazeuser@3763
   333
        event.proximity.y=y;
kazeuser@3763
   334
        event.type=type;
kazeuser@3763
   335
        event.proximity.type=type;
kazeuser@3763
   336
        posted = (SDL_PushEvent(&event) > 0);
kazeuser@3764
   337
        if(type==SDL_PROXIMITYIN)
kazeuser@3764
   338
        {
kazeuser@3764
   339
            SDL_mice[index]->proximity=SDL_TRUE;
kazeuser@3764
   340
        }
kazeuser@3764
   341
        else
kazeuser@3764
   342
        {
kazeuser@3764
   343
            SDL_mice[index]->proximity=SDL_FALSE;
kazeuser@3764
   344
        }
kazeuser@3763
   345
    }
kazeuser@3763
   346
    return posted;
kazeuser@3763
   347
}
kazeuser@3763
   348
kazeuser@3763
   349
int
kazeuser@3760
   350
SDL_SendMouseMotion(int id, int relative, int x, int y,int z)
slouken@1895
   351
{
kazeuser@3760
   352
    int index=SDL_GetIndexById(id);
slouken@1895
   353
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   354
    int posted;
slouken@1895
   355
    int xrel;
slouken@1895
   356
    int yrel;
slouken@1895
   357
slouken@1895
   358
    if (!mouse || mouse->flush_motion) {
slouken@1895
   359
        return 0;
slouken@1895
   360
    }
kazeuser@3764
   361
    if(mouse->proximity==SDL_FALSE)
kazeuser@3764
   362
    {
kazeuser@3764
   363
        last_x=x;
kazeuser@3764
   364
        last_y=y;
kazeuser@3764
   365
        return 0;
kazeuser@3764
   366
    }
kazeuser@3764
   367
    if (mouse->relative_mode==SDL_TRUE && mouse->proximity==SDL_TRUE) {
slouken@1895
   368
        /* Push the cursor around */
kazeuser@3764
   369
        xrel = x - last_x;
kazeuser@3764
   370
        yrel = y - last_y;
kazeuser@3764
   371
        //x = (mouse->x + xrel);
kazeuser@3764
   372
        //y = (mouse->y + yrel);
slouken@1895
   373
    } else {
kazeuser@3764
   374
        xrel = x - last_x;
kazeuser@3764
   375
        yrel = y - last_y;
slouken@1895
   376
    }
slouken@1895
   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@1895
   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@3760
   420
    mouse->z=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@1895
   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@3760
   437
        event.motion.z = mouse->z;
slouken@1895
   438
        event.motion.xrel = xrel;
slouken@1895
   439
        event.motion.yrel = yrel;
slouken@1895
   440
        event.motion.windowID = mouse->focus;
slouken@1895
   441
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   442
    }
kazeuser@3764
   443
    last_x=x;
kazeuser@3764
   444
    last_y=y;
slouken@1895
   445
    return posted;
slouken@1895
   446
}
slouken@1895
   447
slouken@1895
   448
int
kazeuser@3760
   449
SDL_SendMouseButton(int id, Uint8 state, Uint8 button)
slouken@1895
   450
{
kazeuser@3760
   451
    int index=SDL_GetIndexById(id);
slouken@1895
   452
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   453
    int posted;
slouken@1895
   454
    Uint8 type;
slouken@1895
   455
slouken@1895
   456
    if (!mouse) {
slouken@1895
   457
        return 0;
slouken@1895
   458
    }
slouken@1895
   459
slouken@1895
   460
    /* Figure out which event to perform */
slouken@1895
   461
    switch (state) {
slouken@1895
   462
    case SDL_PRESSED:
slouken@1895
   463
        if (mouse->buttonstate & SDL_BUTTON(button)) {
slouken@1895
   464
            /* Ignore this event, no state change */
slouken@1895
   465
            return 0;
slouken@1895
   466
        }
slouken@1895
   467
        type = SDL_MOUSEBUTTONDOWN;
slouken@1895
   468
        mouse->buttonstate |= SDL_BUTTON(button);
slouken@1895
   469
        break;
slouken@1895
   470
    case SDL_RELEASED:
kazeuser@3760
   471
        //if (!(mouse->buttonstate & SDL_BUTTON(button))) {
kazeuser@3760
   472
        //    /* Ignore this event, no state change */
kazeuser@3760
   473
        //    return 0;
kazeuser@3760
   474
        //}*/
slouken@1895
   475
        type = SDL_MOUSEBUTTONUP;
slouken@1895
   476
        mouse->buttonstate &= ~SDL_BUTTON(button);
slouken@1895
   477
        break;
slouken@1895
   478
    default:
slouken@1895
   479
        /* Invalid state -- bail */
slouken@1895
   480
        return 0;
slouken@1895
   481
    }
slouken@1895
   482
slouken@1895
   483
    /* Post the event, if desired */
slouken@1895
   484
    posted = 0;
slouken@1895
   485
    if (SDL_ProcessEvents[type] == SDL_ENABLE) {
slouken@1895
   486
        SDL_Event event;
slouken@1895
   487
        event.type = type;
slouken@1895
   488
        event.button.which = (Uint8) index;
slouken@1895
   489
        event.button.state = state;
slouken@1895
   490
        event.button.button = button;
slouken@1895
   491
        event.button.x = mouse->x;
slouken@1895
   492
        event.button.y = mouse->y;
slouken@1895
   493
        event.button.windowID = mouse->focus;
slouken@1895
   494
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   495
    }
slouken@1895
   496
    return posted;
slouken@1895
   497
}
slouken@1895
   498
slouken@1895
   499
int
slouken@2152
   500
SDL_SendMouseWheel(int index, int x, int y)
slouken@1895
   501
{
slouken@1895
   502
    SDL_Mouse *mouse = SDL_GetMouse(index);
slouken@1895
   503
    int posted;
slouken@1895
   504
slouken@2152
   505
    if (!mouse || (!x && !y)) {
slouken@1895
   506
        return 0;
slouken@1895
   507
    }
slouken@1895
   508
slouken@1895
   509
    /* Post the event, if desired */
slouken@1895
   510
    posted = 0;
slouken@1895
   511
    if (SDL_ProcessEvents[SDL_MOUSEWHEEL] == SDL_ENABLE) {
slouken@1895
   512
        SDL_Event event;
slouken@1895
   513
        event.type = SDL_MOUSEWHEEL;
slouken@1895
   514
        event.wheel.which = (Uint8) index;
slouken@2152
   515
        event.wheel.x = x;
slouken@2152
   516
        event.wheel.y = y;
slouken@1895
   517
        event.wheel.windowID = mouse->focus;
slouken@1895
   518
        posted = (SDL_PushEvent(&event) > 0);
slouken@1895
   519
    }
slouken@1895
   520
    return posted;
slouken@1895
   521
}
slouken@1895
   522
slouken@1895
   523
void
slouken@1895
   524
SDL_WarpMouseInWindow(SDL_WindowID windowID, int x, int y)
slouken@1895
   525
{
slouken@1895
   526
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   527
slouken@1895
   528
    if (!mouse) {
slouken@1895
   529
        return;
slouken@1895
   530
    }
slouken@1895
   531
slouken@1895
   532
    if (mouse->WarpMouse) {
slouken@1895
   533
        mouse->WarpMouse(mouse, windowID, x, y);
slouken@1895
   534
    } else {
slouken@1895
   535
        SDL_SetMouseFocus(SDL_current_mouse, windowID);
kazeuser@3760
   536
        SDL_SendMouseMotion(SDL_current_mouse, 0, x, y,0);
slouken@1895
   537
    }
slouken@1895
   538
}
slouken@1895
   539
slouken@1895
   540
SDL_Cursor *
slouken@1895
   541
SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
slouken@1895
   542
                 int w, int h, int hot_x, int hot_y)
slouken@1895
   543
{
slouken@1895
   544
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   545
    SDL_Surface *surface;
slouken@1895
   546
    SDL_Cursor *cursor;
slouken@1895
   547
    int x, y;
slouken@1895
   548
    Uint32 *pixel;
slouken@1895
   549
    Uint8 datab, maskb;
slouken@1895
   550
    const Uint32 black = 0xFF000000;
slouken@1895
   551
    const Uint32 white = 0xFFFFFFFF;
slouken@1895
   552
    const Uint32 transparent = 0x00000000;
slouken@1895
   553
slouken@1895
   554
    if (!mouse) {
slouken@1895
   555
        SDL_SetError("No mice are initialized");
slouken@1895
   556
        return NULL;
slouken@1895
   557
    }
slouken@1895
   558
slouken@1895
   559
    if (!mouse->CreateCursor) {
slouken@1895
   560
        SDL_SetError("Current mouse doesn't have cursor support");
slouken@1895
   561
        return NULL;
slouken@1895
   562
    }
slouken@1895
   563
slouken@1895
   564
    /* Sanity check the hot spot */
slouken@1895
   565
    if ((hot_x < 0) || (hot_y < 0) || (hot_x >= w) || (hot_y >= h)) {
slouken@1895
   566
        SDL_SetError("Cursor hot spot doesn't lie within cursor");
slouken@1895
   567
        return NULL;
slouken@1895
   568
    }
slouken@1895
   569
slouken@1895
   570
    /* Make sure the width is a multiple of 8 */
slouken@1895
   571
    w = ((w + 7) & ~7);
slouken@1895
   572
slouken@1895
   573
    /* Create the surface from a bitmap */
slouken@1895
   574
    surface =
slouken@1895
   575
        SDL_CreateRGBSurface(0, w, h, 32, 0x00FF0000, 0x0000FF00, 0x000000FF,
slouken@1895
   576
                             0xFF000000);
slouken@1895
   577
    if (!surface) {
slouken@1895
   578
        return NULL;
slouken@1895
   579
    }
slouken@1895
   580
    for (y = 0; y < h; ++y) {
slouken@1895
   581
        pixel = (Uint32 *) ((Uint8 *) surface->pixels + y * surface->pitch);
slouken@1895
   582
        for (x = 0; x < w; ++x) {
slouken@1895
   583
            if ((x % 8) == 0) {
slouken@1895
   584
                datab = *data++;
slouken@1895
   585
                maskb = *mask++;
slouken@1895
   586
            }
slouken@1895
   587
            if (maskb & 0x80) {
slouken@1895
   588
                *pixel++ = (datab & 0x80) ? black : white;
slouken@1895
   589
            } else {
slouken@1895
   590
                *pixel++ = (datab & 0x80) ? black : transparent;
slouken@1895
   591
            }
slouken@1895
   592
            datab <<= 1;
slouken@1895
   593
            maskb <<= 1;
slouken@1895
   594
        }
slouken@1895
   595
    }
slouken@1895
   596
slouken@1895
   597
    cursor = mouse->CreateCursor(surface, hot_x, hot_y);
slouken@1895
   598
    if (cursor) {
slouken@1895
   599
        cursor->mouse = mouse;
slouken@1895
   600
        cursor->next = mouse->cursors;
slouken@1895
   601
        mouse->cursors = cursor;
slouken@1895
   602
    }
slouken@1895
   603
slouken@1895
   604
    SDL_FreeSurface(surface);
slouken@1895
   605
slouken@1895
   606
    return cursor;
slouken@1895
   607
}
slouken@1895
   608
slouken@1895
   609
/* SDL_SetCursor(NULL) can be used to force the cursor redraw,
slouken@1895
   610
   if this is desired for any reason.  This is used when setting
slouken@1895
   611
   the video mode and when the SDL window gains the mouse focus.
slouken@1895
   612
 */
slouken@1895
   613
void
slouken@1895
   614
SDL_SetCursor(SDL_Cursor * cursor)
slouken@1895
   615
{
slouken@1895
   616
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   617
slouken@1895
   618
    if (!mouse) {
slouken@1895
   619
        SDL_SetError("No mice are initialized");
slouken@1895
   620
        return;
slouken@1895
   621
    }
slouken@1895
   622
slouken@1895
   623
    /* Set the new cursor */
slouken@1895
   624
    if (cursor) {
slouken@1895
   625
        /* Make sure the cursor is still valid for this mouse */
slouken@1895
   626
        SDL_Cursor *found;
slouken@1895
   627
        for (found = mouse->cursors; found; found = found->next) {
slouken@1895
   628
            if (found == cursor) {
slouken@1895
   629
                break;
slouken@1895
   630
            }
slouken@1895
   631
        }
slouken@1895
   632
        if (!found) {
slouken@1895
   633
            SDL_SetError("Cursor not associated with the current mouse");
slouken@1895
   634
            return;
slouken@1895
   635
        }
slouken@1895
   636
        mouse->cur_cursor = cursor;
slouken@1895
   637
    } else {
slouken@1895
   638
        cursor = mouse->cur_cursor;
slouken@1895
   639
    }
slouken@1895
   640
slouken@1895
   641
    if (cursor && mouse->cursor_shown && !mouse->relative_mode) {
slouken@1895
   642
        if (mouse->ShowCursor) {
slouken@1895
   643
            mouse->ShowCursor(cursor);
slouken@1895
   644
        }
slouken@1895
   645
    } else {
slouken@1895
   646
        if (mouse->ShowCursor) {
slouken@1895
   647
            mouse->ShowCursor(NULL);
slouken@1895
   648
        }
slouken@1895
   649
    }
slouken@1895
   650
}
slouken@1895
   651
slouken@1895
   652
SDL_Cursor *
slouken@1895
   653
SDL_GetCursor(void)
slouken@1895
   654
{
slouken@1895
   655
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   656
slouken@1895
   657
    if (!mouse) {
slouken@1895
   658
        return NULL;
slouken@1895
   659
    }
slouken@1895
   660
    return mouse->cur_cursor;
slouken@1895
   661
}
slouken@1895
   662
slouken@1895
   663
void
slouken@1895
   664
SDL_FreeCursor(SDL_Cursor * cursor)
slouken@1895
   665
{
slouken@1895
   666
    SDL_Mouse *mouse;
slouken@1895
   667
    SDL_Cursor *curr, *prev;
slouken@1895
   668
slouken@1895
   669
    if (!cursor) {
slouken@1895
   670
        return;
slouken@1895
   671
    }
slouken@1895
   672
    mouse = cursor->mouse;
slouken@1895
   673
slouken@1895
   674
    if (cursor == mouse->def_cursor) {
slouken@1895
   675
        return;
slouken@1895
   676
    }
slouken@1895
   677
    if (cursor == mouse->cur_cursor) {
slouken@1895
   678
        SDL_SetCursor(mouse->def_cursor);
slouken@1895
   679
    }
slouken@1895
   680
slouken@1895
   681
    for (prev = NULL, curr = mouse->cursors; curr;
slouken@1895
   682
         prev = curr, curr = curr->next) {
slouken@1895
   683
        if (curr == cursor) {
slouken@1895
   684
            if (prev) {
slouken@1895
   685
                prev->next = curr->next;
slouken@1895
   686
            } else {
slouken@1895
   687
                mouse->cursors = curr->next;
slouken@1895
   688
            }
slouken@1895
   689
slouken@1895
   690
            if (mouse->FreeCursor) {
slouken@1895
   691
                mouse->FreeCursor(curr);
slouken@1895
   692
            }
slouken@1895
   693
            return;
slouken@1895
   694
        }
slouken@1895
   695
    }
slouken@1895
   696
}
slouken@1895
   697
slouken@1895
   698
int
slouken@1895
   699
SDL_ShowCursor(int toggle)
slouken@1895
   700
{
slouken@1895
   701
    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
slouken@1895
   702
    SDL_bool shown;
slouken@1895
   703
slouken@1895
   704
    if (!mouse) {
slouken@1895
   705
        return 0;
slouken@1895
   706
    }
slouken@1895
   707
slouken@1895
   708
    shown = mouse->cursor_shown;
slouken@1895
   709
    if (toggle >= 0) {
slouken@1895
   710
        if (toggle) {
slouken@1895
   711
            mouse->cursor_shown = SDL_TRUE;
slouken@1895
   712
        } else {
slouken@1895
   713
            mouse->cursor_shown = SDL_FALSE;
slouken@1895
   714
        }
slouken@1895
   715
        if (mouse->cursor_shown != shown) {
slouken@1895
   716
            SDL_SetCursor(NULL);
slouken@1895
   717
        }
slouken@1895
   718
    }
slouken@1895
   719
    return shown;
slouken@1895
   720
}
slouken@1895
   721
kazeuser@3760
   722
void SDL_SetIndexId(int id, int index)
kazeuser@3760
   723
{
kazeuser@3760
   724
    if(id>SDL_highestId)
kazeuser@3760
   725
    {
kazeuser@3760
   726
        int *indexes;
kazeuser@3760
   727
        indexes =
kazeuser@3760
   728
            (int*) SDL_realloc(SDL_IdIndex,
kazeuser@3760
   729
                                       (id + 1) * sizeof(int));
kazeuser@3760
   730
        if (!indexes) {
kazeuser@3760
   731
            SDL_OutOfMemory();
kazeuser@3760
   732
            return -1;
kazeuser@3760
   733
        }
kazeuser@3760
   734
        SDL_IdIndex=indexes;
kazeuser@3760
   735
        SDL_IdIndex[id]=index;
kazeuser@3760
   736
        SDL_highestId=id;
kazeuser@3760
   737
    }
kazeuser@3760
   738
    else
kazeuser@3760
   739
    {
kazeuser@3760
   740
        SDL_IdIndex[id]=index;
kazeuser@3760
   741
    }
kazeuser@3760
   742
}
kazeuser@3760
   743
kazeuser@3760
   744
int SDL_GetIndexById(int id)
kazeuser@3760
   745
{
kazeuser@3760
   746
    if(id>SDL_highestId)
kazeuser@3760
   747
    {
kazeuser@3760
   748
        return -1;
kazeuser@3760
   749
    }
kazeuser@3760
   750
    else
kazeuser@3760
   751
    {
kazeuser@3760
   752
        return SDL_IdIndex[id];
kazeuser@3760
   753
    }
kazeuser@3760
   754
}
kazeuser@3760
   755
kazeuser@3760
   756
int SDL_GetNumOfMice(void)
kazeuser@3760
   757
{
kazeuser@3760
   758
    return SDL_num_mice;
kazeuser@3760
   759
}
kazeuser@3760
   760
kazeuser@3760
   761
char* SDL_GetMouseName(int index)
kazeuser@3760
   762
{
kazeuser@3760
   763
    SDL_Mouse* mouse = SDL_GetMouse(index);
kazeuser@3760
   764
    if(!mouse)
kazeuser@3760
   765
    {
kazeuser@3760
   766
        return NULL;
kazeuser@3760
   767
    }
kazeuser@3760
   768
    return mouse->name;
kazeuser@3760
   769
}
kazeuser@3760
   770
kazeuser@3764
   771
void SDL_UpdateCoordinates(int x, int y)
kazeuser@3764
   772
{
kazeuser@3764
   773
    x_max=x;
kazeuser@3764
   774
    y_max=y;
kazeuser@3764
   775
}
slouken@1895
   776
/* vi: set ts=4 sw=4 expandtab: */