19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* General mouse handling code for SDL */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_events.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_events_c.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../video/SDL_cursor_c.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../video/SDL_sysvideo.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* These are static for our mouse handling code */
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Sint16 SDL_MouseX = 0;
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Sint16 SDL_MouseY = 0;
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Sint16 SDL_DeltaX = 0;
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Sint16 SDL_DeltaY = 0;
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Sint16 SDL_MouseMaxX = 0;
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Sint16 SDL_MouseMaxY = 0;
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8  SDL_ButtonState = 0;
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Public functions */
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_MouseInit(void)
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* The mouse is at (0,0) */
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MouseX = 0;
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MouseY = 0;
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_DeltaX = 0;
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_DeltaY = 0;
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MouseMaxX = 0;
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MouseMaxY = 0;
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_ButtonState = 0;
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* That's it! */
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(0);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_MouseQuit(void)
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* We lost the mouse, so post button up messages for all pressed buttons */
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_ResetMouse(void)
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 i;
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i = 0; i < sizeof(SDL_ButtonState)*8; ++i ) {
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( SDL_ButtonState & SDL_BUTTON(i) ) {
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint8 SDL_GetMouseState (int *x, int *y)
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( x ) {
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*x = SDL_MouseX;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( y ) {
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*y = SDL_MouseY;
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_ButtonState);
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint8 SDL_GetRelativeMouseState (int *x, int *y)
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( x )
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*x = SDL_DeltaX;
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( y )
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*y = SDL_DeltaY;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_DeltaX = 0;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_DeltaY = 0;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(SDL_ButtonState);
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void ClipOffset(Sint16 *x, Sint16 *y)
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* This clips absolute mouse coordinates when the apparent
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   display surface is smaller than the real display surface.
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_VideoSurface && SDL_VideoSurface->offset ) {
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*y -= SDL_VideoSurface->offset/SDL_VideoSurface->pitch;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*x -= (SDL_VideoSurface->offset%SDL_VideoSurface->pitch)/
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_VideoSurface->format->BytesPerPixel;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SetMouseRange(int maxX, int maxY)
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MouseMaxX = (Sint16)maxX;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MouseMaxY = (Sint16)maxY;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* These are global for SDL_eventloop.c */
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_PrivateMouseMotion(Uint8 buttonstate, int relative, Sint16 x, Sint16 y)
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int posted;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint16 X, Y;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint16 Xrel;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sint16 Yrel;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Default buttonstate is the current one */
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! buttonstate ) {
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		buttonstate = SDL_ButtonState;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Xrel = x;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Yrel = y;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( relative ) {
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Push the cursor around */
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		x = (SDL_MouseX+x);
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		y = (SDL_MouseY+y);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Do we need to clip {x,y} ? */
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ClipOffset(&x, &y);
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( x < 0 )
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		X = 0;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( x >= SDL_MouseMaxX )
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		X = SDL_MouseMaxX-1;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		X = (Uint16)x;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( y < 0 )
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Y = 0;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( y >= SDL_MouseMaxY )
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Y = SDL_MouseMaxY-1;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Y = (Uint16)y;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If not relative mode, generate relative motion from clamped X/Y.
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   This prevents lots of extraneous large delta relative motion when
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   the screen is windowed mode and the mouse is outside the window.
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! relative ) {
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Xrel = X-SDL_MouseX;
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Yrel = Y-SDL_MouseY;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Drop events that don't change state */
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! Xrel && ! Yrel ) {
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallprintf("Mouse event didn't change state - dropped!\n");
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Update internal mouse state */
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_ButtonState = buttonstate;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MouseX = X;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_MouseY = Y;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_DeltaX += Xrel;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_DeltaY += Yrel;
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Post the event, if desired */
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	posted = 0;
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_ProcessEvents[SDL_MOUSEMOTION] == SDL_ENABLE ) {
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Event event;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_memset(&event, 0, sizeof(event));
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		event.type = SDL_MOUSEMOTION;
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		event.motion.state = buttonstate;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		event.motion.x = X;
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		event.motion.y = Y;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		event.motion.xrel = Xrel;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		event.motion.yrel = Yrel;
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = 1;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PushEvent(&event);
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(posted);
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_PrivateMouseButton(Uint8 state, Uint8 button, Sint16 x, Sint16 y)
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Event event;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int posted;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int move_mouse;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Uint8 buttonstate;
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_memset(&event, 0, sizeof(event));
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check parameters */
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( x || y ) {
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		ClipOffset(&x, &y);
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		move_mouse = 1;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Mouse coordinates range from 0 - width-1 and 0 - height-1 */
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( x < 0 )
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			x = 0;
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( x >= SDL_MouseMaxX )
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			x = SDL_MouseMaxX-1;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( y < 0 )
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			y = 0;
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( y >= SDL_MouseMaxY )
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			y = SDL_MouseMaxY-1;
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		move_mouse = 0;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! x )
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		x = SDL_MouseX;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! y )
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		y = SDL_MouseY;
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Figure out which event to perform */
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	buttonstate = SDL_ButtonState;
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch ( state ) {
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_PRESSED:
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			event.type = SDL_MOUSEBUTTONDOWN;
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			buttonstate |= SDL_BUTTON(button);
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case SDL_RELEASED:
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			event.type = SDL_MOUSEBUTTONUP;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			buttonstate &= ~SDL_BUTTON(button);
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Invalid state -- bail */
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			return(0);
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Update internal mouse state */
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_ButtonState = buttonstate;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( move_mouse ) {
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_MouseX = x;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_MouseY = y;
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_MoveCursor(SDL_MouseX, SDL_MouseY);
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Post the event, if desired */
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	posted = 0;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) {
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		event.button.state = state;
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		event.button.button = button;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		event.button.x = x;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		event.button.y = y;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (SDL_EventOK == NULL) || (*SDL_EventOK)(&event) ) {
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			posted = 1;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PushEvent(&event);
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(posted);
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269