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