src/video/x11/SDL_x11mouse.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 22 Mar 2006 05:00:59 +0000
changeset 1575 3ba88cb7eb1b
parent 1402 d910939febfa
child 1657 5b0805ceb50f
permissions -rw-r--r--
Updated dynamic X11 code. See details in Bugzilla #170.
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
#include <X11/Xlib.h>
slouken@0
    25
#include <X11/Xutil.h>
slouken@0
    26
slouken@0
    27
#include "SDL_mouse.h"
slouken@1361
    28
#include "../../events/SDL_events_c.h"
slouken@1361
    29
#include "../SDL_cursor_c.h"
slouken@0
    30
#include "SDL_x11dga_c.h"
slouken@0
    31
#include "SDL_x11mouse_c.h"
slouken@0
    32
slouken@0
    33
slouken@0
    34
/* The implementation dependent data for the window manager cursor */
slouken@0
    35
struct WMcursor {
slouken@0
    36
	Cursor x_cursor;
slouken@0
    37
};
slouken@0
    38
slouken@0
    39
slouken@0
    40
void X11_FreeWMCursor(_THIS, WMcursor *cursor)
slouken@0
    41
{
slouken@0
    42
	if ( SDL_Display != NULL ) {
slouken@0
    43
		SDL_Lock_EventThread();
icculus@1575
    44
		XFreeCursor(SDL_Display, cursor->x_cursor);
icculus@1575
    45
		XSync(SDL_Display, False);
slouken@0
    46
		SDL_Unlock_EventThread();
slouken@0
    47
	}
slouken@1336
    48
	SDL_free(cursor);
slouken@0
    49
}
slouken@0
    50
slouken@0
    51
WMcursor *X11_CreateWMCursor(_THIS,
slouken@0
    52
		Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
slouken@0
    53
{
slouken@0
    54
	WMcursor *cursor;
slouken@0
    55
	XGCValues GCvalues;
slouken@0
    56
	GC        GCcursor;
slouken@0
    57
	XImage *data_image, *mask_image;
slouken@0
    58
	Pixmap  data_pixmap, mask_pixmap;
slouken@0
    59
	int       clen, i;
slouken@0
    60
	char     *x_data, *x_mask;
slouken@0
    61
	static XColor black = {  0,  0,  0,  0 };
slouken@0
    62
	static XColor white = { 0xffff, 0xffff, 0xffff, 0xffff };
slouken@0
    63
slouken@0
    64
	/* Allocate the cursor memory */
slouken@1336
    65
	cursor = (WMcursor *)SDL_malloc(sizeof(WMcursor));
slouken@0
    66
	if ( cursor == NULL ) {
slouken@0
    67
		SDL_OutOfMemory();
slouken@0
    68
		return(NULL);
slouken@0
    69
	}
slouken@0
    70
slouken@0
    71
	/* Mix the mask and the data */
slouken@0
    72
	clen = (w/8)*h;
slouken@1336
    73
	x_data = (char *)SDL_malloc(clen);
slouken@0
    74
	if ( x_data == NULL ) {
slouken@1336
    75
		SDL_free(cursor);
slouken@0
    76
		SDL_OutOfMemory();
slouken@0
    77
		return(NULL);
slouken@0
    78
	}
slouken@1336
    79
	x_mask = (char *)SDL_malloc(clen);
slouken@0
    80
	if ( x_mask == NULL ) {
slouken@1336
    81
		SDL_free(cursor);
slouken@1336
    82
		SDL_free(x_data);
slouken@0
    83
		SDL_OutOfMemory();
slouken@0
    84
		return(NULL);
slouken@0
    85
	}
slouken@0
    86
	for ( i=0; i<clen; ++i ) {
slouken@0
    87
		/* The mask is OR'd with the data to turn inverted color
slouken@0
    88
		   pixels black since inverted color cursors aren't supported
slouken@0
    89
		   under X11.
slouken@0
    90
		 */
slouken@0
    91
		x_mask[i] = data[i] | mask[i];
slouken@0
    92
		x_data[i] = data[i];
slouken@0
    93
	}
slouken@0
    94
slouken@0
    95
	/* Prevent the event thread from running while we use the X server */
slouken@0
    96
	SDL_Lock_EventThread();
slouken@0
    97
slouken@0
    98
	/* Create the data image */
icculus@1575
    99
	data_image = XCreateImage(SDL_Display, 
slouken@0
   100
			DefaultVisual(SDL_Display, SDL_Screen),
slouken@0
   101
					1, XYBitmap, 0, x_data, w, h, 8, w/8);
slouken@0
   102
	data_image->byte_order = MSBFirst;
slouken@0
   103
	data_image->bitmap_bit_order = MSBFirst;
icculus@1575
   104
	data_pixmap = XCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
slouken@0
   105
slouken@0
   106
	/* Create the data mask */
icculus@1575
   107
	mask_image = XCreateImage(SDL_Display, 
slouken@0
   108
			DefaultVisual(SDL_Display, SDL_Screen),
slouken@0
   109
					1, XYBitmap, 0, x_mask, w, h, 8, w/8);
slouken@0
   110
	mask_image->byte_order = MSBFirst;
slouken@0
   111
	mask_image->bitmap_bit_order = MSBFirst;
icculus@1575
   112
	mask_pixmap = XCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
slouken@0
   113
slouken@0
   114
	/* Create the graphics context */
slouken@0
   115
	GCvalues.function = GXcopy;
slouken@0
   116
	GCvalues.foreground = ~0;
slouken@0
   117
	GCvalues.background =  0;
slouken@0
   118
	GCvalues.plane_mask = AllPlanes;
icculus@1575
   119
	GCcursor = XCreateGC(SDL_Display, data_pixmap,
slouken@0
   120
			(GCFunction|GCForeground|GCBackground|GCPlaneMask),
slouken@0
   121
								&GCvalues);
slouken@0
   122
slouken@0
   123
	/* Blit the images to the pixmaps */
icculus@1575
   124
	XPutImage(SDL_Display, data_pixmap, GCcursor, data_image,
slouken@0
   125
							0, 0, 0, 0, w, h);
icculus@1575
   126
	XPutImage(SDL_Display, mask_pixmap, GCcursor, mask_image,
slouken@0
   127
							0, 0, 0, 0, w, h);
icculus@1575
   128
	XFreeGC(SDL_Display, GCcursor);
slouken@0
   129
	/* These free the x_data and x_mask memory pointers */
icculus@1575
   130
	XDestroyImage(data_image);
icculus@1575
   131
	XDestroyImage(mask_image);
slouken@0
   132
slouken@0
   133
	/* Create the cursor */
icculus@1575
   134
	cursor->x_cursor = XCreatePixmapCursor(SDL_Display, data_pixmap,
slouken@0
   135
				mask_pixmap, &black, &white, hot_x, hot_y);
icculus@1575
   136
	XFreePixmap(SDL_Display, data_pixmap);
icculus@1575
   137
	XFreePixmap(SDL_Display, mask_pixmap);
slouken@0
   138
slouken@0
   139
	/* Release the event thread */
icculus@1575
   140
	XSync(SDL_Display, False);
slouken@0
   141
	SDL_Unlock_EventThread();
slouken@0
   142
slouken@0
   143
	return(cursor);
slouken@0
   144
}
slouken@0
   145
slouken@0
   146
int X11_ShowWMCursor(_THIS, WMcursor *cursor)
slouken@0
   147
{
slouken@0
   148
	/* Don't do anything if the display is gone */
slouken@0
   149
	if ( SDL_Display == NULL ) {
slouken@0
   150
		return(0);
slouken@0
   151
	}
slouken@0
   152
slouken@0
   153
	/* Set the X11 cursor cursor, or blank if cursor is NULL */
slouken@0
   154
	if ( SDL_Window ) {
slouken@0
   155
		SDL_Lock_EventThread();
slouken@0
   156
		if ( cursor == NULL ) {
slouken@0
   157
			if ( SDL_BlankCursor != NULL ) {
icculus@1575
   158
				XDefineCursor(SDL_Display, SDL_Window,
slouken@0
   159
					SDL_BlankCursor->x_cursor);
slouken@0
   160
			}
slouken@0
   161
		} else {
icculus@1575
   162
			XDefineCursor(SDL_Display, SDL_Window, cursor->x_cursor);
slouken@0
   163
		}
icculus@1575
   164
		XSync(SDL_Display, False);
slouken@0
   165
		SDL_Unlock_EventThread();
slouken@0
   166
	}
slouken@0
   167
	return(1);
slouken@0
   168
}
slouken@0
   169
slouken@0
   170
void X11_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
slouken@0
   171
{
slouken@0
   172
	if ( using_dga & DGA_MOUSE ) {
slouken@527
   173
		SDL_PrivateMouseMotion(0, 0, x, y);
slouken@527
   174
	} else if ( mouse_relative) {
slouken@527
   175
		/*	RJR: March 28, 2000
slouken@527
   176
			leave physical cursor at center of screen if
slouken@527
   177
			mouse hidden and grabbed */
slouken@0
   178
		SDL_PrivateMouseMotion(0, 0, x, y);
slouken@0
   179
	} else {
slouken@0
   180
		SDL_Lock_EventThread();
icculus@1575
   181
		XWarpPointer(SDL_Display, None, SDL_Window, 0, 0, 0, 0, x, y);
icculus@1575
   182
		XSync(SDL_Display, False);
slouken@0
   183
		SDL_Unlock_EventThread();
slouken@0
   184
	}
slouken@0
   185
}
slouken@0
   186
slouken@0
   187
/* Sets the mouse acceleration from a string of the form:
slouken@0
   188
	2/1/0
slouken@0
   189
   The first number is the numerator, followed by the acceleration
slouken@0
   190
   denumenator and threshold.
slouken@0
   191
*/
slouken@0
   192
static void SetMouseAccel(_THIS, const char *accel_param)
slouken@0
   193
{
slouken@0
   194
	int i;
slouken@1379
   195
	size_t len;
slouken@0
   196
	int accel_value[3];
slouken@0
   197
	char *mouse_param, *mouse_param_buf, *pin;
slouken@0
   198
slouken@1379
   199
	len = SDL_strlen(accel_param)+1;
slouken@1379
   200
	mouse_param_buf = SDL_stack_alloc(char, len);
slouken@0
   201
	if ( ! mouse_param_buf ) {
slouken@0
   202
		return;
slouken@0
   203
	}
slouken@1379
   204
	SDL_strlcpy(mouse_param_buf, accel_param, len);
slouken@0
   205
	mouse_param = mouse_param_buf;
slouken@0
   206
slouken@0
   207
	for ( i=0; (i < 3) && mouse_param; ++i ) {
slouken@1336
   208
		pin = SDL_strchr(mouse_param, '/');
slouken@0
   209
		if ( pin ) {
slouken@0
   210
			*pin = '\0';
slouken@0
   211
		}
slouken@0
   212
		accel_value[i] = atoi(mouse_param);
slouken@0
   213
		if ( pin ) {
slouken@0
   214
			mouse_param = pin+1;
slouken@0
   215
		} else {
slouken@0
   216
			mouse_param = NULL;
slouken@0
   217
		}
slouken@0
   218
	}
slouken@0
   219
	if ( mouse_param_buf ) {
icculus@1575
   220
		XChangePointerControl(SDL_Display, True, True,
slouken@0
   221
			accel_value[0], accel_value[1], accel_value[2]);
slouken@1336
   222
		SDL_free(mouse_param_buf);
slouken@0
   223
	}
slouken@0
   224
}
slouken@0
   225
slouken@0
   226
/* Check to see if we need to enter or leave mouse relative mode */
slouken@0
   227
void X11_CheckMouseModeNoLock(_THIS)
slouken@0
   228
{
slouken@79
   229
	char *env_override;
slouken@79
   230
	int enable_relative = 1;
slouken@79
   231
slouken@79
   232
	/* Allow the user to override the relative mouse mode.
slouken@79
   233
	   They almost never want to do this, as it seriously affects
slouken@79
   234
	   applications that rely on continuous relative mouse motion.
slouken@79
   235
	*/
slouken@1336
   236
	env_override = SDL_getenv("SDL_MOUSE_RELATIVE");
slouken@79
   237
	if ( env_override ) {
slouken@79
   238
		enable_relative = atoi(env_override);
slouken@79
   239
	}
slouken@79
   240
slouken@0
   241
	/* If the mouse is hidden and input is grabbed, we use relative mode */
slouken@79
   242
	if ( enable_relative &&
slouken@79
   243
	     !(SDL_cursorstate & CURSOR_VISIBLE) &&
slouken@0
   244
	     (this->input_grab != SDL_GRAB_OFF) &&
slouken@79
   245
             (SDL_GetAppState() & SDL_APPACTIVE) ) {
slouken@0
   246
		if ( ! mouse_relative ) {
slouken@0
   247
			X11_EnableDGAMouse(this);
slouken@0
   248
			if ( ! (using_dga & DGA_MOUSE) ) {
slouken@0
   249
				char *xmouse_accel;
slouken@0
   250
slouken@0
   251
				SDL_GetMouseState(&mouse_last.x, &mouse_last.y);
slouken@0
   252
				/* Use as raw mouse mickeys as possible */
icculus@1575
   253
				XGetPointerControl(SDL_Display,
slouken@0
   254
						&mouse_accel.numerator, 
slouken@0
   255
						&mouse_accel.denominator,
slouken@0
   256
						&mouse_accel.threshold);
slouken@1336
   257
				xmouse_accel=SDL_getenv("SDL_VIDEO_X11_MOUSEACCEL");
slouken@0
   258
				if ( xmouse_accel ) {
slouken@0
   259
					SetMouseAccel(this, xmouse_accel);
slouken@0
   260
				}
slouken@0
   261
			}
slouken@0
   262
			mouse_relative = 1;
slouken@0
   263
		}
slouken@0
   264
	} else {
slouken@0
   265
		if ( mouse_relative ) {
slouken@0
   266
			if ( using_dga & DGA_MOUSE ) {
slouken@0
   267
				X11_DisableDGAMouse(this);
slouken@0
   268
			} else {
icculus@1575
   269
				XChangePointerControl(SDL_Display, True, True,
slouken@0
   270
						mouse_accel.numerator, 
slouken@0
   271
						mouse_accel.denominator,
slouken@0
   272
						mouse_accel.threshold);
slouken@0
   273
			}
slouken@0
   274
			mouse_relative = 0;
slouken@0
   275
		}
slouken@0
   276
	}
slouken@0
   277
}
slouken@0
   278
void X11_CheckMouseMode(_THIS)
slouken@0
   279
{
slouken@0
   280
	SDL_Lock_EventThread();
slouken@0
   281
	X11_CheckMouseModeNoLock(this);
slouken@0
   282
	SDL_Unlock_EventThread();
slouken@0
   283
}