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#include <X11/Xlib.h>
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <X11/Xutil.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mouse.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_cursor_c.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11dga_c.h"
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_x11mouse_c.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The implementation dependent data for the window manager cursor */
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct WMcursor {
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Cursor x_cursor;
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_FreeWMCursor(_THIS, WMcursor *cursor)
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Display != NULL ) {
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Lock_EventThread();
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XFreeCursor(SDL_Display, cursor->x_cursor);
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XSync(SDL_Display, False);
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Unlock_EventThread();
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_free(cursor);
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallWMcursor *X11_CreateWMCursor(_THIS,
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WMcursor *cursor;
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XGCValues GCvalues;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GC        GCcursor;
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XImage *data_image, *mask_image;
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Pixmap  data_pixmap, mask_pixmap;
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int       clen, i;
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char     *x_data, *x_mask;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static XColor black = {  0,  0,  0,  0 };
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	static XColor white = { 0xffff, 0xffff, 0xffff, 0xffff };
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allocate the cursor memory */
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cursor = (WMcursor *)SDL_malloc(sizeof(WMcursor));
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( cursor == NULL ) {
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Mix the mask and the data */
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	clen = (w/8)*h;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	x_data = (char *)SDL_malloc(clen);
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( x_data == NULL ) {
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(cursor);
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	x_mask = (char *)SDL_malloc(clen);
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( x_mask == NULL ) {
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(cursor);
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_free(x_data);
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_OutOfMemory();
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(NULL);
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; i<clen; ++i ) {
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* The mask is OR'd with the data to turn inverted color
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   pixels black since inverted color cursors aren't supported
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   under X11.
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		 */
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		x_mask[i] = data[i] | mask[i];
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		x_data[i] = data[i];
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Prevent the event thread from running while we use the X server */
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Lock_EventThread();
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the data image */
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	data_image = XCreateImage(SDL_Display,
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DefaultVisual(SDL_Display, SDL_Screen),
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					1, XYBitmap, 0, x_data, w, h, 8, w/8);
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	data_image->byte_order = MSBFirst;
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	data_image->bitmap_bit_order = MSBFirst;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	data_pixmap = XCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the data mask */
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mask_image = XCreateImage(SDL_Display,
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DefaultVisual(SDL_Display, SDL_Screen),
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					1, XYBitmap, 0, x_mask, w, h, 8, w/8);
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mask_image->byte_order = MSBFirst;
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mask_image->bitmap_bit_order = MSBFirst;
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mask_pixmap = XCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the graphics context */
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GCvalues.function = GXcopy;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GCvalues.foreground = ~0;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GCvalues.background =  0;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GCvalues.plane_mask = AllPlanes;
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	GCcursor = XCreateGC(SDL_Display, data_pixmap,
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(GCFunction|GCForeground|GCBackground|GCPlaneMask),
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								&GCvalues);
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Blit the images to the pixmaps */
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XPutImage(SDL_Display, data_pixmap, GCcursor, data_image,
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							0, 0, 0, 0, w, h);
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XPutImage(SDL_Display, mask_pixmap, GCcursor, mask_image,
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							0, 0, 0, 0, w, h);
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XFreeGC(SDL_Display, GCcursor);
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* These free the x_data and x_mask memory pointers */
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XDestroyImage(data_image);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XDestroyImage(mask_image);
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Create the cursor */
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	cursor->x_cursor = XCreatePixmapCursor(SDL_Display, data_pixmap,
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				mask_pixmap, &black, &white, hot_x, hot_y);
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XFreePixmap(SDL_Display, data_pixmap);
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XFreePixmap(SDL_Display, mask_pixmap);
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Release the event thread */
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	XSync(SDL_Display, False);
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Unlock_EventThread();
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(cursor);
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint X11_ShowWMCursor(_THIS, WMcursor *cursor)
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Don't do anything if the display is gone */
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Display == NULL ) {
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return(0);
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set the X11 cursor cursor, or blank if cursor is NULL */
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Window ) {
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Lock_EventThread();
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( cursor == NULL ) {
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( SDL_BlankCursor != NULL ) {
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				XDefineCursor(SDL_Display, SDL_Window,
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_BlankCursor->x_cursor);
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			XDefineCursor(SDL_Display, SDL_Window, cursor->x_cursor);
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XSync(SDL_Display, False);
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Unlock_EventThread();
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return(1);
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( using_dga & DGA_MOUSE ) {
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_PrivateMouseMotion(0, 0, x, y);
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else if ( mouse_relative) {
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/*	RJR: March 28, 2000
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			leave physical cursor at center of screen if
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse hidden and grabbed */
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_PrivateMouseMotion(0, 0, x, y);
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Lock_EventThread();
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XWarpPointer(SDL_Display, None, SDL_Window, 0, 0, 0, 0, x, y);
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XSync(SDL_Display, False);
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_Unlock_EventThread();
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Sets the mouse acceleration from a string of the form:
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	2/1/0
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   The first number is the numerator, followed by the acceleration
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   denumenator and threshold.
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void SetMouseAccel(_THIS, const char *accel_param)
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	size_t len;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int accel_value[3];
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *mouse_param, *mouse_param_buf, *pin;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	len = SDL_strlen(accel_param)+1;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mouse_param_buf = SDL_stack_alloc(char, len);
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( ! mouse_param_buf ) {
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_strlcpy(mouse_param_buf, accel_param, len);
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mouse_param = mouse_param_buf;
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for ( i=0; (i < 3) && mouse_param; ++i ) {
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pin = SDL_strchr(mouse_param, '/');
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( pin ) {
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*pin = '\0';
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		accel_value[i] = atoi(mouse_param);
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( pin ) {
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse_param = pin+1;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		} else {
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse_param = NULL;
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( i == 3 ) {
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		XChangePointerControl(SDL_Display, True, True,
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			accel_value[0], accel_value[1], accel_value[2]);
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_stack_free(mouse_param_buf);
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Check to see if we need to enter or leave mouse relative mode */
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_CheckMouseModeNoLock(_THIS)
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	const Uint8 full_focus = (SDL_APPACTIVE|SDL_APPINPUTFOCUS|SDL_APPMOUSEFOCUS);
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char *env_override;
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int enable_relative = 1;
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* This happens when quiting after an xio error */
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( SDL_Display == NULL )
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        return;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Allow the user to override the relative mouse mode.
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   They almost never want to do this, as it seriously affects
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   applications that rely on continuous relative mouse motion.
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*/
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	env_override = SDL_getenv("SDL_MOUSE_RELATIVE");
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( env_override ) {
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		enable_relative = atoi(env_override);
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* If the mouse is hidden and input is grabbed, we use relative mode */
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ( enable_relative &&
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     !(SDL_cursorstate & CURSOR_VISIBLE) &&
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	     (this->input_grab != SDL_GRAB_OFF) &&
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             (SDL_GetAppState() & full_focus) == full_focus ) {
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( ! mouse_relative ) {
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			X11_EnableDGAMouse(this);
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( ! (using_dga & DGA_MOUSE) ) {
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				char *xmouse_accel;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_GetMouseState(&mouse_last.x, &mouse_last.y);
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Use as raw mouse mickeys as possible */
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				XGetPointerControl(SDL_Display,
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						&mouse_accel.numerator,
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						&mouse_accel.denominator,
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						&mouse_accel.threshold);
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				xmouse_accel=SDL_getenv("SDL_VIDEO_X11_MOUSEACCEL");
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if ( xmouse_accel ) {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SetMouseAccel(this, xmouse_accel);
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse_relative = 1;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	} else {
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( mouse_relative ) {
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if ( using_dga & DGA_MOUSE ) {
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				X11_DisableDGAMouse(this);
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else {
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				XChangePointerControl(SDL_Display, True, True,
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						mouse_accel.numerator,
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						mouse_accel.denominator,
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						mouse_accel.threshold);
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			mouse_relative = 0;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid X11_CheckMouseMode(_THIS)
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Lock_EventThread();
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	X11_CheckMouseModeNoLock(this);
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Unlock_EventThread();
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
289