src/video/x11/SDL_x11mouse.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 07 Feb 2006 09:29:18 +0000
changeset 1338 604d73db6802
parent 1336 3692456e7b0f
child 1358 c71e05b4dc2e
permissions -rw-r--r--
Removed uses of stdlib.h and string.h
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@1338
    26
#include "SDL_stdlib.h"
slouken@1338
    27
#include "SDL_string.h"
slouken@0
    28
#include "SDL_error.h"
slouken@0
    29
#include "SDL_mouse.h"
slouken@0
    30
#include "SDL_events_c.h"
slouken@0
    31
#include "SDL_cursor_c.h"
slouken@0
    32
#include "SDL_x11dga_c.h"
slouken@0
    33
#include "SDL_x11mouse_c.h"
slouken@0
    34
slouken@0
    35
slouken@0
    36
/* The implementation dependent data for the window manager cursor */
slouken@0
    37
struct WMcursor {
slouken@0
    38
	Cursor x_cursor;
slouken@0
    39
};
slouken@0
    40
slouken@0
    41
slouken@0
    42
void X11_FreeWMCursor(_THIS, WMcursor *cursor)
slouken@0
    43
{
slouken@0
    44
	if ( SDL_Display != NULL ) {
slouken@0
    45
		SDL_Lock_EventThread();
icculus@1168
    46
		pXFreeCursor(SDL_Display, cursor->x_cursor);
icculus@1168
    47
		pXSync(SDL_Display, False);
slouken@0
    48
		SDL_Unlock_EventThread();
slouken@0
    49
	}
slouken@1336
    50
	SDL_free(cursor);
slouken@0
    51
}
slouken@0
    52
slouken@0
    53
WMcursor *X11_CreateWMCursor(_THIS,
slouken@0
    54
		Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
slouken@0
    55
{
slouken@0
    56
	WMcursor *cursor;
slouken@0
    57
	XGCValues GCvalues;
slouken@0
    58
	GC        GCcursor;
slouken@0
    59
	XImage *data_image, *mask_image;
slouken@0
    60
	Pixmap  data_pixmap, mask_pixmap;
slouken@0
    61
	int       clen, i;
slouken@0
    62
	char     *x_data, *x_mask;
slouken@0
    63
	static XColor black = {  0,  0,  0,  0 };
slouken@0
    64
	static XColor white = { 0xffff, 0xffff, 0xffff, 0xffff };
slouken@0
    65
slouken@0
    66
	/* Allocate the cursor memory */
slouken@1336
    67
	cursor = (WMcursor *)SDL_malloc(sizeof(WMcursor));
slouken@0
    68
	if ( cursor == NULL ) {
slouken@0
    69
		SDL_OutOfMemory();
slouken@0
    70
		return(NULL);
slouken@0
    71
	}
slouken@0
    72
slouken@0
    73
	/* Mix the mask and the data */
slouken@0
    74
	clen = (w/8)*h;
slouken@1336
    75
	x_data = (char *)SDL_malloc(clen);
slouken@0
    76
	if ( x_data == NULL ) {
slouken@1336
    77
		SDL_free(cursor);
slouken@0
    78
		SDL_OutOfMemory();
slouken@0
    79
		return(NULL);
slouken@0
    80
	}
slouken@1336
    81
	x_mask = (char *)SDL_malloc(clen);
slouken@0
    82
	if ( x_mask == NULL ) {
slouken@1336
    83
		SDL_free(cursor);
slouken@1336
    84
		SDL_free(x_data);
slouken@0
    85
		SDL_OutOfMemory();
slouken@0
    86
		return(NULL);
slouken@0
    87
	}
slouken@0
    88
	for ( i=0; i<clen; ++i ) {
slouken@0
    89
		/* The mask is OR'd with the data to turn inverted color
slouken@0
    90
		   pixels black since inverted color cursors aren't supported
slouken@0
    91
		   under X11.
slouken@0
    92
		 */
slouken@0
    93
		x_mask[i] = data[i] | mask[i];
slouken@0
    94
		x_data[i] = data[i];
slouken@0
    95
	}
slouken@0
    96
slouken@0
    97
	/* Prevent the event thread from running while we use the X server */
slouken@0
    98
	SDL_Lock_EventThread();
slouken@0
    99
slouken@0
   100
	/* Create the data image */
icculus@1168
   101
	data_image = pXCreateImage(SDL_Display, 
slouken@0
   102
			DefaultVisual(SDL_Display, SDL_Screen),
slouken@0
   103
					1, XYBitmap, 0, x_data, w, h, 8, w/8);
slouken@0
   104
	data_image->byte_order = MSBFirst;
slouken@0
   105
	data_image->bitmap_bit_order = MSBFirst;
icculus@1168
   106
	data_pixmap = pXCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
slouken@0
   107
slouken@0
   108
	/* Create the data mask */
icculus@1168
   109
	mask_image = pXCreateImage(SDL_Display, 
slouken@0
   110
			DefaultVisual(SDL_Display, SDL_Screen),
slouken@0
   111
					1, XYBitmap, 0, x_mask, w, h, 8, w/8);
slouken@0
   112
	mask_image->byte_order = MSBFirst;
slouken@0
   113
	mask_image->bitmap_bit_order = MSBFirst;
icculus@1168
   114
	mask_pixmap = pXCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
slouken@0
   115
slouken@0
   116
	/* Create the graphics context */
slouken@0
   117
	GCvalues.function = GXcopy;
slouken@0
   118
	GCvalues.foreground = ~0;
slouken@0
   119
	GCvalues.background =  0;
slouken@0
   120
	GCvalues.plane_mask = AllPlanes;
icculus@1168
   121
	GCcursor = pXCreateGC(SDL_Display, data_pixmap,
slouken@0
   122
			(GCFunction|GCForeground|GCBackground|GCPlaneMask),
slouken@0
   123
								&GCvalues);
slouken@0
   124
slouken@0
   125
	/* Blit the images to the pixmaps */
icculus@1168
   126
	pXPutImage(SDL_Display, data_pixmap, GCcursor, data_image,
slouken@0
   127
							0, 0, 0, 0, w, h);
icculus@1168
   128
	pXPutImage(SDL_Display, mask_pixmap, GCcursor, mask_image,
slouken@0
   129
							0, 0, 0, 0, w, h);
icculus@1168
   130
	pXFreeGC(SDL_Display, GCcursor);
slouken@0
   131
	/* These free the x_data and x_mask memory pointers */
icculus@1168
   132
	pXDestroyImage(data_image);
icculus@1168
   133
	pXDestroyImage(mask_image);
slouken@0
   134
slouken@0
   135
	/* Create the cursor */
icculus@1168
   136
	cursor->x_cursor = pXCreatePixmapCursor(SDL_Display, data_pixmap,
slouken@0
   137
				mask_pixmap, &black, &white, hot_x, hot_y);
icculus@1168
   138
	pXFreePixmap(SDL_Display, data_pixmap);
icculus@1168
   139
	pXFreePixmap(SDL_Display, mask_pixmap);
slouken@0
   140
slouken@0
   141
	/* Release the event thread */
icculus@1168
   142
	pXSync(SDL_Display, False);
slouken@0
   143
	SDL_Unlock_EventThread();
slouken@0
   144
slouken@0
   145
	return(cursor);
slouken@0
   146
}
slouken@0
   147
slouken@0
   148
int X11_ShowWMCursor(_THIS, WMcursor *cursor)
slouken@0
   149
{
slouken@0
   150
	/* Don't do anything if the display is gone */
slouken@0
   151
	if ( SDL_Display == NULL ) {
slouken@0
   152
		return(0);
slouken@0
   153
	}
slouken@0
   154
slouken@0
   155
	/* Set the X11 cursor cursor, or blank if cursor is NULL */
slouken@0
   156
	if ( SDL_Window ) {
slouken@0
   157
		SDL_Lock_EventThread();
slouken@0
   158
		if ( cursor == NULL ) {
slouken@0
   159
			if ( SDL_BlankCursor != NULL ) {
icculus@1168
   160
				pXDefineCursor(SDL_Display, SDL_Window,
slouken@0
   161
					SDL_BlankCursor->x_cursor);
slouken@0
   162
			}
slouken@0
   163
		} else {
icculus@1168
   164
			pXDefineCursor(SDL_Display, SDL_Window, cursor->x_cursor);
slouken@0
   165
		}
icculus@1168
   166
		pXSync(SDL_Display, False);
slouken@0
   167
		SDL_Unlock_EventThread();
slouken@0
   168
	}
slouken@0
   169
	return(1);
slouken@0
   170
}
slouken@0
   171
slouken@0
   172
void X11_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
slouken@0
   173
{
slouken@0
   174
	if ( using_dga & DGA_MOUSE ) {
slouken@527
   175
		SDL_PrivateMouseMotion(0, 0, x, y);
slouken@527
   176
	} else if ( mouse_relative) {
slouken@527
   177
		/*	RJR: March 28, 2000
slouken@527
   178
			leave physical cursor at center of screen if
slouken@527
   179
			mouse hidden and grabbed */
slouken@0
   180
		SDL_PrivateMouseMotion(0, 0, x, y);
slouken@0
   181
	} else {
slouken@0
   182
		SDL_Lock_EventThread();
icculus@1168
   183
		pXWarpPointer(SDL_Display, None, SDL_Window, 0, 0, 0, 0, x, y);
icculus@1168
   184
		pXSync(SDL_Display, False);
slouken@0
   185
		SDL_Unlock_EventThread();
slouken@0
   186
	}
slouken@0
   187
}
slouken@0
   188
slouken@0
   189
/* Sets the mouse acceleration from a string of the form:
slouken@0
   190
	2/1/0
slouken@0
   191
   The first number is the numerator, followed by the acceleration
slouken@0
   192
   denumenator and threshold.
slouken@0
   193
*/
slouken@0
   194
static void SetMouseAccel(_THIS, const char *accel_param)
slouken@0
   195
{
slouken@0
   196
	int i;
slouken@0
   197
	int accel_value[3];
slouken@0
   198
	char *mouse_param, *mouse_param_buf, *pin;
slouken@0
   199
slouken@1336
   200
	mouse_param_buf = (char *)SDL_malloc(SDL_strlen(accel_param)+1);
slouken@0
   201
	if ( ! mouse_param_buf ) {
slouken@0
   202
		return;
slouken@0
   203
	}
slouken@1336
   204
	SDL_strcpy(mouse_param_buf, accel_param);
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@1168
   220
		pXChangePointerControl(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@1168
   253
				pXGetPointerControl(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@1168
   269
				pXChangePointerControl(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
}