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