src/events/SDL_mouse.c
changeset 1895 c121d94672cb
parent 1525 23a347cfbed8
child 2049 5f6550e5184f
     1.1 --- a/src/events/SDL_mouse.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/src/events/SDL_mouse.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -25,239 +25,627 @@
     1.4  
     1.5  #include "SDL_events.h"
     1.6  #include "SDL_events_c.h"
     1.7 -#include "../video/SDL_cursor_c.h"
     1.8 -#include "../video/SDL_sysvideo.h"
     1.9 +#include "default_cursor.h"
    1.10  
    1.11  
    1.12 -/* These are static for our mouse handling code */
    1.13 -static Sint16 SDL_MouseX = 0;
    1.14 -static Sint16 SDL_MouseY = 0;
    1.15 -static Sint16 SDL_DeltaX = 0;
    1.16 -static Sint16 SDL_DeltaY = 0;
    1.17 -static Uint8  SDL_ButtonState = 0;
    1.18 +static int SDL_num_mice;
    1.19 +static int SDL_current_mouse;
    1.20 +static SDL_Mouse **SDL_mice;
    1.21  
    1.22  
    1.23  /* Public functions */
    1.24 -int SDL_MouseInit(void)
    1.25 +int
    1.26 +SDL_MouseInit(void)
    1.27 +{
    1.28 +    return (0);
    1.29 +}
    1.30 +
    1.31 +SDL_Mouse *
    1.32 +SDL_GetMouse(int index)
    1.33 +{
    1.34 +    if (index < 0 || index >= SDL_num_mice) {
    1.35 +        return NULL;
    1.36 +    }
    1.37 +    return SDL_mice[index];
    1.38 +}
    1.39 +
    1.40 +int
    1.41 +SDL_AddMouse(const SDL_Mouse * mouse, int index)
    1.42  {
    1.43 -	/* The mouse is at (0,0) */
    1.44 -	SDL_MouseX = 0;
    1.45 -	SDL_MouseY = 0;
    1.46 -	SDL_DeltaX = 0;
    1.47 -	SDL_DeltaY = 0;
    1.48 -	SDL_ButtonState = 0;
    1.49 +    SDL_Mouse **mice;
    1.50 +    int selected_mouse;
    1.51 +
    1.52 +    /* Add the mouse to the list of mice */
    1.53 +    if (index < 0 || index >= SDL_num_mice || SDL_mice[index]) {
    1.54 +        mice =
    1.55 +            (SDL_Mouse **) SDL_realloc(SDL_mice,
    1.56 +                                       (SDL_num_mice + 1) * sizeof(*mice));
    1.57 +        if (!mice) {
    1.58 +            SDL_OutOfMemory();
    1.59 +            return -1;
    1.60 +        }
    1.61  
    1.62 -	/* That's it! */
    1.63 -	return(0);
    1.64 -}
    1.65 -void SDL_MouseQuit(void)
    1.66 -{
    1.67 +        SDL_mice = mice;
    1.68 +        index = SDL_num_mice++;
    1.69 +    }
    1.70 +    SDL_mice[index] = (SDL_Mouse *) SDL_malloc(sizeof(*SDL_mice[index]));
    1.71 +    if (!SDL_mice[index]) {
    1.72 +        SDL_OutOfMemory();
    1.73 +        return -1;
    1.74 +    }
    1.75 +    *SDL_mice[index] = *mouse;
    1.76 +
    1.77 +    /* Create the default cursor for the mouse */
    1.78 +    SDL_mice[index]->cursor_shown = SDL_TRUE;
    1.79 +    selected_mouse = SDL_SelectMouse(index);
    1.80 +    SDL_mice[index]->cur_cursor = NULL;
    1.81 +    SDL_mice[index]->def_cursor =
    1.82 +        SDL_CreateCursor(default_cdata, default_cmask, DEFAULT_CWIDTH,
    1.83 +                         DEFAULT_CHEIGHT, DEFAULT_CHOTX, DEFAULT_CHOTY);
    1.84 +    SDL_SetCursor(SDL_mice[index]->def_cursor);
    1.85 +    SDL_SelectMouse(selected_mouse);
    1.86 +
    1.87 +    return index;
    1.88  }
    1.89  
    1.90 -/* We lost the mouse, so post button up messages for all pressed buttons */
    1.91 -void SDL_ResetMouse(void)
    1.92 +void
    1.93 +SDL_DelMouse(int index)
    1.94 +{
    1.95 +    SDL_Mouse *mouse = SDL_GetMouse(index);
    1.96 +
    1.97 +    if (!mouse) {
    1.98 +        return;
    1.99 +    }
   1.100 +
   1.101 +    mouse->def_cursor = NULL;
   1.102 +    while (mouse->cursors) {
   1.103 +        SDL_FreeCursor(mouse->cursors);
   1.104 +    }
   1.105 +
   1.106 +    if (mouse->FreeMouse) {
   1.107 +        mouse->FreeMouse(mouse);
   1.108 +    }
   1.109 +    SDL_free(mouse);
   1.110 +
   1.111 +    SDL_mice[index] = NULL;
   1.112 +}
   1.113 +
   1.114 +void
   1.115 +SDL_ResetMouse(int index)
   1.116 +{
   1.117 +    SDL_Mouse *mouse = SDL_GetMouse(index);
   1.118 +
   1.119 +    if (!mouse) {
   1.120 +        return;
   1.121 +    }
   1.122 +
   1.123 +    /* FIXME */
   1.124 +}
   1.125 +
   1.126 +void
   1.127 +SDL_MouseQuit(void)
   1.128  {
   1.129 -	Uint8 i;
   1.130 -	for ( i = 0; i < sizeof(SDL_ButtonState)*8; ++i ) {
   1.131 -		if ( SDL_ButtonState & SDL_BUTTON(i) ) {
   1.132 -			SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0);
   1.133 -		}
   1.134 -	}
   1.135 +    int i;
   1.136 +
   1.137 +    for (i = 0; i < SDL_num_mice; ++i) {
   1.138 +        SDL_DelMouse(i);
   1.139 +    }
   1.140 +    SDL_num_mice = 0;
   1.141 +    SDL_current_mouse = 0;
   1.142 +
   1.143 +    if (SDL_mice) {
   1.144 +        SDL_free(SDL_mice);
   1.145 +        SDL_mice = NULL;
   1.146 +    }
   1.147 +}
   1.148 +
   1.149 +int
   1.150 +SDL_GetNumMice(void)
   1.151 +{
   1.152 +    return SDL_num_mice;
   1.153 +}
   1.154 +
   1.155 +int
   1.156 +SDL_SelectMouse(int index)
   1.157 +{
   1.158 +    if (index >= 0 && index < SDL_num_mice) {
   1.159 +        SDL_current_mouse = index;
   1.160 +    }
   1.161 +    return SDL_current_mouse;
   1.162 +}
   1.163 +
   1.164 +SDL_WindowID
   1.165 +SDL_GetMouseFocusWindow()
   1.166 +{
   1.167 +    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
   1.168 +
   1.169 +    if (!mouse) {
   1.170 +        return 0;
   1.171 +    }
   1.172 +    return mouse->focus;
   1.173  }
   1.174  
   1.175 -Uint8 SDL_GetMouseState (int *x, int *y)
   1.176 +static int
   1.177 +FlushMouseMotion(void *param, SDL_Event * event)
   1.178 +{
   1.179 +    if (event->type == SDL_MOUSEMOTION
   1.180 +        && event->motion.which == (Uint8) SDL_current_mouse) {
   1.181 +        return 0;
   1.182 +    } else {
   1.183 +        return 1;
   1.184 +    }
   1.185 +}
   1.186 +
   1.187 +int
   1.188 +SDL_SetRelativeMouseMode(SDL_bool enabled)
   1.189  {
   1.190 -	if ( x ) {
   1.191 -		*x = SDL_MouseX;
   1.192 -	}
   1.193 -	if ( y ) {
   1.194 -		*y = SDL_MouseY;
   1.195 -	}
   1.196 -	return(SDL_ButtonState);
   1.197 +    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
   1.198 +
   1.199 +    if (!mouse) {
   1.200 +        return -1;
   1.201 +    }
   1.202 +
   1.203 +    /* Flush pending mouse motion */
   1.204 +    mouse->flush_motion = SDL_TRUE;
   1.205 +    SDL_PumpEvents();
   1.206 +    mouse->flush_motion = SDL_FALSE;
   1.207 +    SDL_FilterEvents(FlushMouseMotion, mouse);
   1.208 +
   1.209 +    /* Set the relative mode */
   1.210 +    mouse->relative_mode = enabled;
   1.211 +
   1.212 +    /* Update cursor visibility */
   1.213 +    SDL_SetCursor(NULL);
   1.214 +
   1.215 +    if (!enabled) {
   1.216 +        /* Restore the expected mouse position */
   1.217 +        SDL_WarpMouseInWindow(mouse->focus, mouse->x, mouse->y);
   1.218 +    }
   1.219 +    return 0;
   1.220 +}
   1.221 +
   1.222 +SDL_bool
   1.223 +SDL_GetRelativeMouseMode()
   1.224 +{
   1.225 +    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
   1.226 +
   1.227 +    if (!mouse) {
   1.228 +        return SDL_FALSE;
   1.229 +    }
   1.230 +    return mouse->relative_mode;
   1.231 +}
   1.232 +
   1.233 +Uint8
   1.234 +SDL_GetMouseState(int *x, int *y)
   1.235 +{
   1.236 +    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
   1.237 +
   1.238 +    if (!mouse) {
   1.239 +        if (x) {
   1.240 +            *x = 0;
   1.241 +        }
   1.242 +        if (y) {
   1.243 +            *y = 0;
   1.244 +        }
   1.245 +        return 0;
   1.246 +    }
   1.247 +
   1.248 +    if (x) {
   1.249 +        *x = mouse->x;
   1.250 +    }
   1.251 +    if (y) {
   1.252 +        *y = mouse->y;
   1.253 +    }
   1.254 +    return mouse->buttonstate;
   1.255  }
   1.256  
   1.257 -Uint8 SDL_GetRelativeMouseState (int *x, int *y)
   1.258 +Uint8
   1.259 +SDL_GetRelativeMouseState(int *x, int *y)
   1.260  {
   1.261 -	if ( x )
   1.262 -		*x = SDL_DeltaX;
   1.263 -	if ( y )
   1.264 -		*y = SDL_DeltaY;
   1.265 -	SDL_DeltaX = 0;
   1.266 -	SDL_DeltaY = 0;
   1.267 -	return(SDL_ButtonState);
   1.268 +    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
   1.269 +
   1.270 +    if (!mouse) {
   1.271 +        if (x) {
   1.272 +            *x = 0;
   1.273 +        }
   1.274 +        if (y) {
   1.275 +            *y = 0;
   1.276 +        }
   1.277 +        return 0;
   1.278 +    }
   1.279 +
   1.280 +    if (x) {
   1.281 +        *x = mouse->xdelta;
   1.282 +    }
   1.283 +    if (y) {
   1.284 +        *y = mouse->ydelta;
   1.285 +    }
   1.286 +    mouse->xdelta = 0;
   1.287 +    mouse->ydelta = 0;
   1.288 +    return mouse->buttonstate;
   1.289  }
   1.290  
   1.291 -static void ClipOffset(Sint16 *x, Sint16 *y)
   1.292 +void
   1.293 +SDL_SetMouseFocus(int index, SDL_WindowID windowID)
   1.294  {
   1.295 -	/* This clips absolute mouse coordinates when the apparent
   1.296 -	   display surface is smaller than the real display surface.
   1.297 -	 */
   1.298 -	if ( SDL_VideoSurface->offset ) {
   1.299 -		*y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch;
   1.300 -		*x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/
   1.301 -				SDL_VideoSurface->format->BytesPerPixel;
   1.302 -	}
   1.303 +    SDL_Mouse *mouse = SDL_GetMouse(index);
   1.304 +    int i;
   1.305 +    SDL_bool focus;
   1.306 +
   1.307 +    if (!mouse || (mouse->focus == windowID)) {
   1.308 +        return;
   1.309 +    }
   1.310 +
   1.311 +    /* See if the current window has lost focus */
   1.312 +    if (mouse->focus) {
   1.313 +        focus = SDL_FALSE;
   1.314 +        for (i = 0; i < SDL_num_mice; ++i) {
   1.315 +            SDL_Mouse *check;
   1.316 +            if (i != index) {
   1.317 +                check = SDL_GetMouse(i);
   1.318 +                if (check && check->focus == mouse->focus) {
   1.319 +                    focus = SDL_TRUE;
   1.320 +                    break;
   1.321 +                }
   1.322 +            }
   1.323 +        }
   1.324 +        if (!focus) {
   1.325 +            SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_LEAVE, 0, 0);
   1.326 +        }
   1.327 +    }
   1.328 +
   1.329 +    mouse->focus = windowID;
   1.330 +
   1.331 +    if (mouse->focus) {
   1.332 +        focus = SDL_FALSE;
   1.333 +        for (i = 0; i < SDL_num_mice; ++i) {
   1.334 +            SDL_Mouse *check;
   1.335 +            if (i != index) {
   1.336 +                check = SDL_GetMouse(i);
   1.337 +                if (check && check->focus == mouse->focus) {
   1.338 +                    focus = SDL_TRUE;
   1.339 +                    break;
   1.340 +                }
   1.341 +            }
   1.342 +        }
   1.343 +        if (!focus) {
   1.344 +            SDL_SendWindowEvent(mouse->focus, SDL_WINDOWEVENT_ENTER, 0, 0);
   1.345 +        }
   1.346 +    }
   1.347  }
   1.348  
   1.349 -/* These are global for SDL_eventloop.c */
   1.350 -int SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y)
   1.351 +int
   1.352 +SDL_SendMouseMotion(int index, int relative, int x, int y)
   1.353  {
   1.354 -	int posted;
   1.355 -	Uint16 X, Y;
   1.356 -	Sint16 Xrel;
   1.357 -	Sint16 Yrel;
   1.358 +    SDL_Mouse *mouse = SDL_GetMouse(index);
   1.359 +    int posted;
   1.360 +    int xrel;
   1.361 +    int yrel;
   1.362 +
   1.363 +    if (!mouse || mouse->flush_motion) {
   1.364 +        return 0;
   1.365 +    }
   1.366  
   1.367 -	/* Don't handle mouse motion if there's no cursor surface */
   1.368 -	if ( SDL_VideoSurface == NULL ) {
   1.369 -		return(0);
   1.370 -	}
   1.371 +    if (relative) {
   1.372 +        /* Push the cursor around */
   1.373 +        xrel = x;
   1.374 +        yrel = y;
   1.375 +        x = (mouse->x + xrel);
   1.376 +        y = (mouse->y + yrel);
   1.377 +    } else {
   1.378 +        xrel = x - mouse->x;
   1.379 +        yrel = y - mouse->y;
   1.380 +    }
   1.381  
   1.382 -	/* Default buttonstate is the current one */
   1.383 -	if ( ! buttonstate ) {
   1.384 -		buttonstate = SDL_ButtonState;
   1.385 -	}
   1.386 +    /* Drop events that don't change state */
   1.387 +    if (!xrel && !yrel) {
   1.388 +#if 0
   1.389 +        printf("Mouse event didn't change state - dropped!\n");
   1.390 +#endif
   1.391 +        return 0;
   1.392 +    }
   1.393  
   1.394 -	Xrel = x;
   1.395 -	Yrel = y;
   1.396 -	if ( relative ) {
   1.397 -		/* Push the cursor around */
   1.398 -		x = (SDL_MouseX+x);
   1.399 -		y = (SDL_MouseY+y);
   1.400 -	} else {
   1.401 -		/* Do we need to clip {x,y} ? */
   1.402 -		ClipOffset(&x, &y);
   1.403 -	}
   1.404 +    /* Update internal mouse state */
   1.405 +    if (!mouse->relative_mode) {
   1.406 +        mouse->x = x;
   1.407 +        mouse->y = y;
   1.408 +    }
   1.409 +    mouse->xdelta += xrel;
   1.410 +    mouse->ydelta += yrel;
   1.411 +
   1.412 +    /* Move the mouse cursor, if needed */
   1.413 +    if (mouse->cursor_shown && !mouse->relative_mode &&
   1.414 +        mouse->MoveCursor && mouse->cur_cursor) {
   1.415 +        mouse->MoveCursor(mouse->cur_cursor);
   1.416 +    }
   1.417  
   1.418 -	/* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
   1.419 -	if ( x < 0 )
   1.420 -		X = 0;
   1.421 -	else
   1.422 -	if ( x >= SDL_VideoSurface->w )
   1.423 -		X = SDL_VideoSurface->w-1;
   1.424 -	else
   1.425 -		X = (Uint16)x;
   1.426 +    /* Post the event, if desired */
   1.427 +    posted = 0;
   1.428 +    if (SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE) {
   1.429 +        SDL_Event event;
   1.430 +        event.motion.type = SDL_MOUSEMOTION;
   1.431 +        event.motion.which = (Uint8) index;
   1.432 +        event.motion.state = mouse->buttonstate;
   1.433 +        event.motion.x = mouse->x;
   1.434 +        event.motion.y = mouse->y;
   1.435 +        event.motion.xrel = xrel;
   1.436 +        event.motion.yrel = yrel;
   1.437 +        event.motion.windowID = mouse->focus;
   1.438 +        posted = (SDL_PushEvent(&event) > 0);
   1.439 +    }
   1.440 +    return posted;
   1.441 +}
   1.442 +
   1.443 +int
   1.444 +SDL_SendMouseButton(int index, Uint8 state, Uint8 button)
   1.445 +{
   1.446 +    SDL_Mouse *mouse = SDL_GetMouse(index);
   1.447 +    int posted;
   1.448 +    Uint8 type;
   1.449 +
   1.450 +    if (!mouse) {
   1.451 +        return 0;
   1.452 +    }
   1.453  
   1.454 -	if ( y < 0 )
   1.455 -		Y = 0;
   1.456 -	else
   1.457 -	if ( y >= SDL_VideoSurface->h )
   1.458 -		Y = SDL_VideoSurface->h-1;
   1.459 -	else
   1.460 -		Y = (Uint16)y;
   1.461 +    /* Figure out which event to perform */
   1.462 +    switch (state) {
   1.463 +    case SDL_PRESSED:
   1.464 +        if (mouse->buttonstate & SDL_BUTTON(button)) {
   1.465 +            /* Ignore this event, no state change */
   1.466 +            return 0;
   1.467 +        }
   1.468 +        type = SDL_MOUSEBUTTONDOWN;
   1.469 +        mouse->buttonstate |= SDL_BUTTON(button);
   1.470 +        break;
   1.471 +    case SDL_RELEASED:
   1.472 +        if (!(mouse->buttonstate & SDL_BUTTON(button))) {
   1.473 +            /* Ignore this event, no state change */
   1.474 +            return 0;
   1.475 +        }
   1.476 +        type = SDL_MOUSEBUTTONUP;
   1.477 +        mouse->buttonstate &= ~SDL_BUTTON(button);
   1.478 +        break;
   1.479 +    default:
   1.480 +        /* Invalid state -- bail */
   1.481 +        return 0;
   1.482 +    }
   1.483  
   1.484 -	/* If not relative mode, generate relative motion from clamped X/Y.
   1.485 -	   This prevents lots of extraneous large delta relative motion when
   1.486 -	   the screen is windowed mode and the mouse is outside the window.
   1.487 -	*/
   1.488 -	if ( ! relative ) {
   1.489 -		Xrel = X-SDL_MouseX;
   1.490 -		Yrel = Y-SDL_MouseY;
   1.491 -	}
   1.492 -
   1.493 -	/* Drop events that don't change state */
   1.494 -	if ( ! Xrel && ! Yrel ) {
   1.495 -#if 0
   1.496 -printf("Mouse event didn't change state - dropped!\n");
   1.497 -#endif
   1.498 -		return(0);
   1.499 -	}
   1.500 +    /* Post the event, if desired */
   1.501 +    posted = 0;
   1.502 +    if (SDL_ProcessEvents[type] == SDL_ENABLE) {
   1.503 +        SDL_Event event;
   1.504 +        event.type = type;
   1.505 +        event.button.which = (Uint8) index;
   1.506 +        event.button.state = state;
   1.507 +        event.button.button = button;
   1.508 +        event.button.x = mouse->x;
   1.509 +        event.button.y = mouse->y;
   1.510 +        event.button.windowID = mouse->focus;
   1.511 +        posted = (SDL_PushEvent(&event) > 0);
   1.512 +    }
   1.513 +    return posted;
   1.514 +}
   1.515  
   1.516 -	/* Update internal mouse state */
   1.517 -	SDL_ButtonState = buttonstate;
   1.518 -	SDL_MouseX = X;
   1.519 -	SDL_MouseY = Y;
   1.520 -	SDL_DeltaX += Xrel;
   1.521 -	SDL_DeltaY += Yrel;
   1.522 -        SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
   1.523 +int
   1.524 +SDL_SendMouseWheel(int index, int motion)
   1.525 +{
   1.526 +    SDL_Mouse *mouse = SDL_GetMouse(index);
   1.527 +    int posted;
   1.528 +
   1.529 +    if (!mouse || !motion) {
   1.530 +        return 0;
   1.531 +    }
   1.532  
   1.533 -	/* Post the event, if desired */
   1.534 -	posted = 0;
   1.535 -	if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) {
   1.536 -		SDL_Event event;
   1.537 -		SDL_memset(&event, 0, sizeof(event));
   1.538 -		event.type = SDL_MOUSEMOTION;
   1.539 -		event.motion.state = buttonstate;
   1.540 -		event.motion.x = X;
   1.541 -		event.motion.y = Y;
   1.542 -		event.motion.xrel = Xrel;
   1.543 -		event.motion.yrel = Yrel;
   1.544 -		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
   1.545 -			posted = 1;
   1.546 -			SDL_PushEvent(&event);
   1.547 -		}
   1.548 -	}
   1.549 -	return(posted);
   1.550 +    /* Post the event, if desired */
   1.551 +    posted = 0;
   1.552 +    if (SDL_ProcessEvents[SDL_MOUSEWHEEL] == SDL_ENABLE) {
   1.553 +        SDL_Event event;
   1.554 +        event.type = SDL_MOUSEWHEEL;
   1.555 +        event.wheel.which = (Uint8) index;
   1.556 +        event.wheel.motion = motion;
   1.557 +        event.wheel.windowID = mouse->focus;
   1.558 +        posted = (SDL_PushEvent(&event) > 0);
   1.559 +    }
   1.560 +    return posted;
   1.561 +}
   1.562 +
   1.563 +void
   1.564 +SDL_WarpMouseInWindow(SDL_WindowID windowID, int x, int y)
   1.565 +{
   1.566 +    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
   1.567 +
   1.568 +    if (!mouse) {
   1.569 +        return;
   1.570 +    }
   1.571 +
   1.572 +    if (mouse->WarpMouse) {
   1.573 +        mouse->WarpMouse(mouse, windowID, x, y);
   1.574 +    } else {
   1.575 +        SDL_SetMouseFocus(SDL_current_mouse, windowID);
   1.576 +        SDL_SendMouseMotion(SDL_current_mouse, 0, x, y);
   1.577 +    }
   1.578  }
   1.579  
   1.580 -int SDL_PrivateMouseButton(Uint8 state, Uint8 button, Sint16 x, Sint16 y)
   1.581 +SDL_Cursor *
   1.582 +SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
   1.583 +                 int w, int h, int hot_x, int hot_y)
   1.584  {
   1.585 -	SDL_Event event;
   1.586 -	int posted;
   1.587 -	int move_mouse;
   1.588 -	Uint8 buttonstate;
   1.589 -
   1.590 -	SDL_memset(&event, 0, sizeof(event));
   1.591 +    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
   1.592 +    SDL_Surface *surface;
   1.593 +    SDL_Cursor *cursor;
   1.594 +    int x, y;
   1.595 +    Uint32 *pixel;
   1.596 +    Uint8 datab, maskb;
   1.597 +    const Uint32 black = 0xFF000000;
   1.598 +    const Uint32 white = 0xFFFFFFFF;
   1.599 +    const Uint32 transparent = 0x00000000;
   1.600  
   1.601 -	/* Check parameters */
   1.602 -	if ( x || y ) {
   1.603 -		ClipOffset(&x, &y);
   1.604 -		move_mouse = 1;
   1.605 -		/* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
   1.606 -		if ( x < 0 )
   1.607 -			x = 0;
   1.608 -		else
   1.609 -		if ( x >= SDL_VideoSurface->w )
   1.610 -			x = SDL_VideoSurface->w-1;
   1.611 +    if (!mouse) {
   1.612 +        SDL_SetError("No mice are initialized");
   1.613 +        return NULL;
   1.614 +    }
   1.615  
   1.616 -		if ( y < 0 )
   1.617 -			y = 0;
   1.618 -		else
   1.619 -		if ( y >= SDL_VideoSurface->h )
   1.620 -			y = SDL_VideoSurface->h-1;
   1.621 -	} else {
   1.622 -		move_mouse = 0;
   1.623 -	}
   1.624 -	if ( ! x )
   1.625 -		x = SDL_MouseX;
   1.626 -	if ( ! y )
   1.627 -		y = SDL_MouseY;
   1.628 +    if (!mouse->CreateCursor) {
   1.629 +        SDL_SetError("Current mouse doesn't have cursor support");
   1.630 +        return NULL;
   1.631 +    }
   1.632 +
   1.633 +    /* Sanity check the hot spot */
   1.634 +    if ((hot_x < 0) || (hot_y < 0) || (hot_x >= w) || (hot_y >= h)) {
   1.635 +        SDL_SetError("Cursor hot spot doesn't lie within cursor");
   1.636 +        return NULL;
   1.637 +    }
   1.638 +
   1.639 +    /* Make sure the width is a multiple of 8 */
   1.640 +    w = ((w + 7) & ~7);
   1.641  
   1.642 -	/* Figure out which event to perform */
   1.643 -	buttonstate = SDL_ButtonState;
   1.644 -	switch ( state ) {
   1.645 -		case SDL_PRESSED:
   1.646 -			event.type = SDL_MOUSEBUTTONDOWN;
   1.647 -			buttonstate |= SDL_BUTTON(button);
   1.648 -			break;
   1.649 -		case SDL_RELEASED:
   1.650 -			event.type = SDL_MOUSEBUTTONUP;
   1.651 -			buttonstate &= ~SDL_BUTTON(button);
   1.652 -			break;
   1.653 -		default:
   1.654 -			/* Invalid state -- bail */
   1.655 -			return(0);
   1.656 -	}
   1.657 +    /* Create the surface from a bitmap */
   1.658 +    surface =
   1.659 +        SDL_CreateRGBSurface(0, w, h, 32, 0x00FF0000, 0x0000FF00, 0x000000FF,
   1.660 +                             0xFF000000);
   1.661 +    if (!surface) {
   1.662 +        return NULL;
   1.663 +    }
   1.664 +    for (y = 0; y < h; ++y) {
   1.665 +        pixel = (Uint32 *) ((Uint8 *) surface->pixels + y * surface->pitch);
   1.666 +        for (x = 0; x < w; ++x) {
   1.667 +            if ((x % 8) == 0) {
   1.668 +                datab = *data++;
   1.669 +                maskb = *mask++;
   1.670 +            }
   1.671 +            if (maskb & 0x80) {
   1.672 +                *pixel++ = (datab & 0x80) ? black : white;
   1.673 +            } else {
   1.674 +                *pixel++ = (datab & 0x80) ? black : transparent;
   1.675 +            }
   1.676 +            datab <<= 1;
   1.677 +            maskb <<= 1;
   1.678 +        }
   1.679 +    }
   1.680  
   1.681 -	/* Update internal mouse state */
   1.682 -	SDL_ButtonState = buttonstate;
   1.683 -	if ( move_mouse ) {
   1.684 -		SDL_MouseX = x;
   1.685 -		SDL_MouseY = y;
   1.686 -		SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
   1.687 -	}
   1.688 +    cursor = mouse->CreateCursor(surface, hot_x, hot_y);
   1.689 +    if (cursor) {
   1.690 +        cursor->mouse = mouse;
   1.691 +        cursor->next = mouse->cursors;
   1.692 +        mouse->cursors = cursor;
   1.693 +    }
   1.694  
   1.695 -	/* Post the event, if desired */
   1.696 -	posted = 0;
   1.697 -	if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) {
   1.698 -		event.button.state = state;
   1.699 -		event.button.button = button;
   1.700 -		event.button.x = x;
   1.701 -		event.button.y = y;
   1.702 -		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
   1.703 -			posted = 1;
   1.704 -			SDL_PushEvent(&event);
   1.705 -		}
   1.706 -	}
   1.707 -	return(posted);
   1.708 +    SDL_FreeSurface(surface);
   1.709 +
   1.710 +    return cursor;
   1.711  }
   1.712  
   1.713 +/* SDL_SetCursor(NULL) can be used to force the cursor redraw,
   1.714 +   if this is desired for any reason.  This is used when setting
   1.715 +   the video mode and when the SDL window gains the mouse focus.
   1.716 + */
   1.717 +void
   1.718 +SDL_SetCursor(SDL_Cursor * cursor)
   1.719 +{
   1.720 +    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
   1.721 +
   1.722 +    if (!mouse) {
   1.723 +        SDL_SetError("No mice are initialized");
   1.724 +        return;
   1.725 +    }
   1.726 +
   1.727 +    /* Set the new cursor */
   1.728 +    if (cursor) {
   1.729 +        /* Make sure the cursor is still valid for this mouse */
   1.730 +        SDL_Cursor *found;
   1.731 +        for (found = mouse->cursors; found; found = found->next) {
   1.732 +            if (found == cursor) {
   1.733 +                break;
   1.734 +            }
   1.735 +        }
   1.736 +        if (!found) {
   1.737 +            SDL_SetError("Cursor not associated with the current mouse");
   1.738 +            return;
   1.739 +        }
   1.740 +        mouse->cur_cursor = cursor;
   1.741 +    } else {
   1.742 +        cursor = mouse->cur_cursor;
   1.743 +    }
   1.744 +
   1.745 +    if (cursor && mouse->cursor_shown && !mouse->relative_mode) {
   1.746 +        if (mouse->ShowCursor) {
   1.747 +            mouse->ShowCursor(cursor);
   1.748 +        }
   1.749 +    } else {
   1.750 +        if (mouse->ShowCursor) {
   1.751 +            mouse->ShowCursor(NULL);
   1.752 +        }
   1.753 +    }
   1.754 +}
   1.755 +
   1.756 +SDL_Cursor *
   1.757 +SDL_GetCursor(void)
   1.758 +{
   1.759 +    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
   1.760 +
   1.761 +    if (!mouse) {
   1.762 +        return NULL;
   1.763 +    }
   1.764 +    return mouse->cur_cursor;
   1.765 +}
   1.766 +
   1.767 +void
   1.768 +SDL_FreeCursor(SDL_Cursor * cursor)
   1.769 +{
   1.770 +    SDL_Mouse *mouse;
   1.771 +    SDL_Cursor *curr, *prev;
   1.772 +
   1.773 +    if (!cursor) {
   1.774 +        return;
   1.775 +    }
   1.776 +    mouse = cursor->mouse;
   1.777 +
   1.778 +    if (cursor == mouse->def_cursor) {
   1.779 +        return;
   1.780 +    }
   1.781 +    if (cursor == mouse->cur_cursor) {
   1.782 +        SDL_SetCursor(mouse->def_cursor);
   1.783 +    }
   1.784 +
   1.785 +    for (prev = NULL, curr = mouse->cursors; curr;
   1.786 +         prev = curr, curr = curr->next) {
   1.787 +        if (curr == cursor) {
   1.788 +            if (prev) {
   1.789 +                prev->next = curr->next;
   1.790 +            } else {
   1.791 +                mouse->cursors = curr->next;
   1.792 +            }
   1.793 +
   1.794 +            if (mouse->FreeCursor) {
   1.795 +                mouse->FreeCursor(curr);
   1.796 +            }
   1.797 +            return;
   1.798 +        }
   1.799 +    }
   1.800 +}
   1.801 +
   1.802 +int
   1.803 +SDL_ShowCursor(int toggle)
   1.804 +{
   1.805 +    SDL_Mouse *mouse = SDL_GetMouse(SDL_current_mouse);
   1.806 +    SDL_bool shown;
   1.807 +
   1.808 +    if (!mouse) {
   1.809 +        return 0;
   1.810 +    }
   1.811 +
   1.812 +    shown = mouse->cursor_shown;
   1.813 +    if (toggle >= 0) {
   1.814 +        if (toggle) {
   1.815 +            mouse->cursor_shown = SDL_TRUE;
   1.816 +        } else {
   1.817 +            mouse->cursor_shown = SDL_FALSE;
   1.818 +        }
   1.819 +        if (mouse->cursor_shown != shown) {
   1.820 +            SDL_SetCursor(NULL);
   1.821 +        }
   1.822 +    }
   1.823 +    return shown;
   1.824 +}
   1.825 +
   1.826 +/* vi: set ts=4 sw=4 expandtab: */