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