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