src/video/x11/SDL_x11mouse.c
changeset 0 74212992fb08
child 78 e9582f471c02
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/x11/SDL_x11mouse.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,273 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +#include <stdlib.h>
    1.32 +#include <stdio.h>
    1.33 +#include <string.h>
    1.34 +
    1.35 +#include <X11/Xlib.h>
    1.36 +#include <X11/Xutil.h>
    1.37 +
    1.38 +#include "SDL_error.h"
    1.39 +#include "SDL_mouse.h"
    1.40 +#include "SDL_events_c.h"
    1.41 +#include "SDL_cursor_c.h"
    1.42 +#include "SDL_x11dga_c.h"
    1.43 +#include "SDL_x11mouse_c.h"
    1.44 +
    1.45 +
    1.46 +/* The implementation dependent data for the window manager cursor */
    1.47 +struct WMcursor {
    1.48 +	Cursor x_cursor;
    1.49 +};
    1.50 +
    1.51 +
    1.52 +void X11_FreeWMCursor(_THIS, WMcursor *cursor)
    1.53 +{
    1.54 +	if ( SDL_Display != NULL ) {
    1.55 +		SDL_Lock_EventThread();
    1.56 +		XFreeCursor(SDL_Display, cursor->x_cursor);
    1.57 +		XSync(SDL_Display, False);
    1.58 +		SDL_Unlock_EventThread();
    1.59 +	}
    1.60 +	free(cursor);
    1.61 +}
    1.62 +
    1.63 +WMcursor *X11_CreateWMCursor(_THIS,
    1.64 +		Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
    1.65 +{
    1.66 +	WMcursor *cursor;
    1.67 +	XGCValues GCvalues;
    1.68 +	GC        GCcursor;
    1.69 +	XImage *data_image, *mask_image;
    1.70 +	Pixmap  data_pixmap, mask_pixmap;
    1.71 +	int       clen, i;
    1.72 +	char     *x_data, *x_mask;
    1.73 +	static XColor black = {  0,  0,  0,  0 };
    1.74 +	static XColor white = { 0xffff, 0xffff, 0xffff, 0xffff };
    1.75 +
    1.76 +	/* Allocate the cursor memory */
    1.77 +	cursor = (WMcursor *)malloc(sizeof(WMcursor));
    1.78 +	if ( cursor == NULL ) {
    1.79 +		SDL_OutOfMemory();
    1.80 +		return(NULL);
    1.81 +	}
    1.82 +
    1.83 +	/* Mix the mask and the data */
    1.84 +	clen = (w/8)*h;
    1.85 +	x_data = (char *)malloc(clen);
    1.86 +	if ( x_data == NULL ) {
    1.87 +		free(cursor);
    1.88 +		SDL_OutOfMemory();
    1.89 +		return(NULL);
    1.90 +	}
    1.91 +	x_mask = (char *)malloc(clen);
    1.92 +	if ( x_mask == NULL ) {
    1.93 +		free(cursor);
    1.94 +		free(x_data);
    1.95 +		SDL_OutOfMemory();
    1.96 +		return(NULL);
    1.97 +	}
    1.98 +	for ( i=0; i<clen; ++i ) {
    1.99 +		/* The mask is OR'd with the data to turn inverted color
   1.100 +		   pixels black since inverted color cursors aren't supported
   1.101 +		   under X11.
   1.102 +		 */
   1.103 +		x_mask[i] = data[i] | mask[i];
   1.104 +		x_data[i] = data[i];
   1.105 +	}
   1.106 +
   1.107 +	/* Prevent the event thread from running while we use the X server */
   1.108 +	SDL_Lock_EventThread();
   1.109 +
   1.110 +	/* Create the data image */
   1.111 +	data_image = XCreateImage(SDL_Display, 
   1.112 +			DefaultVisual(SDL_Display, SDL_Screen),
   1.113 +					1, XYBitmap, 0, x_data, w, h, 8, w/8);
   1.114 +	data_image->byte_order = MSBFirst;
   1.115 +	data_image->bitmap_bit_order = MSBFirst;
   1.116 +	data_pixmap = XCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
   1.117 +
   1.118 +	/* Create the data mask */
   1.119 +	mask_image = XCreateImage(SDL_Display, 
   1.120 +			DefaultVisual(SDL_Display, SDL_Screen),
   1.121 +					1, XYBitmap, 0, x_mask, w, h, 8, w/8);
   1.122 +	mask_image->byte_order = MSBFirst;
   1.123 +	mask_image->bitmap_bit_order = MSBFirst;
   1.124 +	mask_pixmap = XCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
   1.125 +
   1.126 +	/* Create the graphics context */
   1.127 +	GCvalues.function = GXcopy;
   1.128 +	GCvalues.foreground = ~0;
   1.129 +	GCvalues.background =  0;
   1.130 +	GCvalues.plane_mask = AllPlanes;
   1.131 +	GCcursor = XCreateGC(SDL_Display, data_pixmap,
   1.132 +			(GCFunction|GCForeground|GCBackground|GCPlaneMask),
   1.133 +								&GCvalues);
   1.134 +
   1.135 +	/* Blit the images to the pixmaps */
   1.136 +	XPutImage(SDL_Display, data_pixmap, GCcursor, data_image,
   1.137 +							0, 0, 0, 0, w, h);
   1.138 +	XPutImage(SDL_Display, mask_pixmap, GCcursor, mask_image,
   1.139 +							0, 0, 0, 0, w, h);
   1.140 +	XFreeGC(SDL_Display, GCcursor);
   1.141 +	/* These free the x_data and x_mask memory pointers */
   1.142 +	XDestroyImage(data_image);
   1.143 +	XDestroyImage(mask_image);
   1.144 +
   1.145 +	/* Create the cursor */
   1.146 +	cursor->x_cursor = XCreatePixmapCursor(SDL_Display, data_pixmap,
   1.147 +				mask_pixmap, &black, &white, hot_x, hot_y);
   1.148 +
   1.149 +	/* Release the event thread */
   1.150 +	XSync(SDL_Display, False);
   1.151 +	SDL_Unlock_EventThread();
   1.152 +
   1.153 +	return(cursor);
   1.154 +}
   1.155 +
   1.156 +int X11_ShowWMCursor(_THIS, WMcursor *cursor)
   1.157 +{
   1.158 +	/* Don't do anything if the display is gone */
   1.159 +	if ( SDL_Display == NULL ) {
   1.160 +		return(0);
   1.161 +	}
   1.162 +
   1.163 +	/* Set the X11 cursor cursor, or blank if cursor is NULL */
   1.164 +	if ( SDL_Window ) {
   1.165 +		SDL_Lock_EventThread();
   1.166 +		if ( cursor == NULL ) {
   1.167 +			if ( SDL_BlankCursor != NULL ) {
   1.168 +				XDefineCursor(SDL_Display, SDL_Window,
   1.169 +					SDL_BlankCursor->x_cursor);
   1.170 +			}
   1.171 +		} else {
   1.172 +			XDefineCursor(SDL_Display, SDL_Window, cursor->x_cursor);
   1.173 +		}
   1.174 +		XSync(SDL_Display, False);
   1.175 +		SDL_Unlock_EventThread();
   1.176 +	}
   1.177 +	return(1);
   1.178 +}
   1.179 +
   1.180 +void X11_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
   1.181 +{
   1.182 +	if ( using_dga & DGA_MOUSE ) {
   1.183 +		x += (this->screen->offset % this->screen->pitch) /
   1.184 +		      this->screen->format->BytesPerPixel;
   1.185 +		y += (this->screen->offset / this->screen->pitch);
   1.186 +		SDL_PrivateMouseMotion(0, 0, x, y);
   1.187 +	} else {
   1.188 +		SDL_Lock_EventThread();
   1.189 +		XWarpPointer(SDL_Display, None, SDL_Window, 0, 0, 0, 0, x, y);
   1.190 +		XSync(SDL_Display, False);
   1.191 +		SDL_Unlock_EventThread();
   1.192 +	}
   1.193 +}
   1.194 +
   1.195 +/* Sets the mouse acceleration from a string of the form:
   1.196 +	2/1/0
   1.197 +   The first number is the numerator, followed by the acceleration
   1.198 +   denumenator and threshold.
   1.199 +*/
   1.200 +static void SetMouseAccel(_THIS, const char *accel_param)
   1.201 +{
   1.202 +	int i;
   1.203 +	int accel_value[3];
   1.204 +	char *mouse_param, *mouse_param_buf, *pin;
   1.205 +
   1.206 +	mouse_param_buf = (char *)malloc(strlen(accel_param)+1);
   1.207 +	if ( ! mouse_param_buf ) {
   1.208 +		return;
   1.209 +	}
   1.210 +	strcpy(mouse_param_buf, accel_param);
   1.211 +	mouse_param = mouse_param_buf;
   1.212 +
   1.213 +	for ( i=0; (i < 3) && mouse_param; ++i ) {
   1.214 +		pin = strchr(mouse_param, '/');
   1.215 +		if ( pin ) {
   1.216 +			*pin = '\0';
   1.217 +		}
   1.218 +		accel_value[i] = atoi(mouse_param);
   1.219 +		if ( pin ) {
   1.220 +			mouse_param = pin+1;
   1.221 +		} else {
   1.222 +			mouse_param = NULL;
   1.223 +		}
   1.224 +	}
   1.225 +	if ( mouse_param_buf ) {
   1.226 +		XChangePointerControl(SDL_Display, True, True,
   1.227 +			accel_value[0], accel_value[1], accel_value[2]);
   1.228 +		free(mouse_param_buf);
   1.229 +	}
   1.230 +}
   1.231 +
   1.232 +/* Check to see if we need to enter or leave mouse relative mode */
   1.233 +void X11_CheckMouseModeNoLock(_THIS)
   1.234 +{
   1.235 +	/* If the mouse is hidden and input is grabbed, we use relative mode */
   1.236 +	if ( !(SDL_cursorstate & CURSOR_VISIBLE) &&
   1.237 +	     (this->input_grab != SDL_GRAB_OFF) &&
   1.238 +             (SDL_GetAppState() & SDL_APPACTIVE) ) {
   1.239 +		if ( ! mouse_relative ) {
   1.240 +			X11_EnableDGAMouse(this);
   1.241 +			if ( ! (using_dga & DGA_MOUSE) ) {
   1.242 +				char *xmouse_accel;
   1.243 +
   1.244 +				SDL_GetMouseState(&mouse_last.x, &mouse_last.y);
   1.245 +				/* Use as raw mouse mickeys as possible */
   1.246 +				XGetPointerControl(SDL_Display,
   1.247 +						&mouse_accel.numerator, 
   1.248 +						&mouse_accel.denominator,
   1.249 +						&mouse_accel.threshold);
   1.250 +				xmouse_accel=getenv("SDL_VIDEO_X11_MOUSEACCEL");
   1.251 +				if ( xmouse_accel ) {
   1.252 +					SetMouseAccel(this, xmouse_accel);
   1.253 +				}
   1.254 +			}
   1.255 +			mouse_relative = 1;
   1.256 +		}
   1.257 +	} else {
   1.258 +		if ( mouse_relative ) {
   1.259 +			if ( using_dga & DGA_MOUSE ) {
   1.260 +				X11_DisableDGAMouse(this);
   1.261 +			} else {
   1.262 +				XChangePointerControl(SDL_Display, True, True,
   1.263 +						mouse_accel.numerator, 
   1.264 +						mouse_accel.denominator,
   1.265 +						mouse_accel.threshold);
   1.266 +			}
   1.267 +			mouse_relative = 0;
   1.268 +		}
   1.269 +	}
   1.270 +}
   1.271 +void X11_CheckMouseMode(_THIS)
   1.272 +{
   1.273 +	SDL_Lock_EventThread();
   1.274 +	X11_CheckMouseModeNoLock(this);
   1.275 +	SDL_Unlock_EventThread();
   1.276 +}