src/video/x11/SDL_x11mouse.c
changeset 1895 c121d94672cb
parent 1740 db7e15a99cb3
     1.1 --- a/src/video/x11/SDL_x11mouse.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/src/video/x11/SDL_x11mouse.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -32,156 +32,163 @@
     1.4  
     1.5  
     1.6  /* The implementation dependent data for the window manager cursor */
     1.7 -struct WMcursor {
     1.8 -	Cursor x_cursor;
     1.9 +struct WMcursor
    1.10 +{
    1.11 +    Cursor x_cursor;
    1.12  };
    1.13  
    1.14  
    1.15 -void X11_FreeWMCursor(_THIS, WMcursor *cursor)
    1.16 +void
    1.17 +X11_FreeWMCursor(_THIS, WMcursor * cursor)
    1.18  {
    1.19 -	if ( SDL_Display != NULL ) {
    1.20 -		SDL_Lock_EventThread();
    1.21 -		XFreeCursor(SDL_Display, cursor->x_cursor);
    1.22 -		XSync(SDL_Display, False);
    1.23 -		SDL_Unlock_EventThread();
    1.24 -	}
    1.25 -	SDL_free(cursor);
    1.26 +    if (SDL_Display != NULL) {
    1.27 +        SDL_Lock_EventThread();
    1.28 +        XFreeCursor(SDL_Display, cursor->x_cursor);
    1.29 +        XSync(SDL_Display, False);
    1.30 +        SDL_Unlock_EventThread();
    1.31 +    }
    1.32 +    SDL_free(cursor);
    1.33  }
    1.34  
    1.35 -WMcursor *X11_CreateWMCursor(_THIS,
    1.36 -		Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
    1.37 +WMcursor *
    1.38 +X11_CreateWMCursor(_THIS,
    1.39 +                   Uint8 * data, Uint8 * mask, int w, int h, int hot_x,
    1.40 +                   int hot_y)
    1.41  {
    1.42 -	WMcursor *cursor;
    1.43 -	XGCValues GCvalues;
    1.44 -	GC        GCcursor;
    1.45 -	XImage *data_image, *mask_image;
    1.46 -	Pixmap  data_pixmap, mask_pixmap;
    1.47 -	int       clen, i;
    1.48 -	char     *x_data, *x_mask;
    1.49 -	static XColor black = {  0,  0,  0,  0 };
    1.50 -	static XColor white = { 0xffff, 0xffff, 0xffff, 0xffff };
    1.51 +    WMcursor *cursor;
    1.52 +    XGCValues GCvalues;
    1.53 +    GC GCcursor;
    1.54 +    XImage *data_image, *mask_image;
    1.55 +    Pixmap data_pixmap, mask_pixmap;
    1.56 +    int clen, i;
    1.57 +    char *x_data, *x_mask;
    1.58 +    static XColor black = { 0, 0, 0, 0 };
    1.59 +    static XColor white = { 0xffff, 0xffff, 0xffff, 0xffff };
    1.60  
    1.61 -	/* Allocate the cursor memory */
    1.62 -	cursor = (WMcursor *)SDL_malloc(sizeof(WMcursor));
    1.63 -	if ( cursor == NULL ) {
    1.64 -		SDL_OutOfMemory();
    1.65 -		return(NULL);
    1.66 -	}
    1.67 +    /* Allocate the cursor memory */
    1.68 +    cursor = (WMcursor *) SDL_malloc(sizeof(WMcursor));
    1.69 +    if (cursor == NULL) {
    1.70 +        SDL_OutOfMemory();
    1.71 +        return (NULL);
    1.72 +    }
    1.73  
    1.74 -	/* Mix the mask and the data */
    1.75 -	clen = (w/8)*h;
    1.76 -	x_data = (char *)SDL_malloc(clen);
    1.77 -	if ( x_data == NULL ) {
    1.78 -		SDL_free(cursor);
    1.79 -		SDL_OutOfMemory();
    1.80 -		return(NULL);
    1.81 -	}
    1.82 -	x_mask = (char *)SDL_malloc(clen);
    1.83 -	if ( x_mask == NULL ) {
    1.84 -		SDL_free(cursor);
    1.85 -		SDL_free(x_data);
    1.86 -		SDL_OutOfMemory();
    1.87 -		return(NULL);
    1.88 -	}
    1.89 -	for ( i=0; i<clen; ++i ) {
    1.90 -		/* The mask is OR'd with the data to turn inverted color
    1.91 -		   pixels black since inverted color cursors aren't supported
    1.92 -		   under X11.
    1.93 -		 */
    1.94 -		x_mask[i] = data[i] | mask[i];
    1.95 -		x_data[i] = data[i];
    1.96 -	}
    1.97 +    /* Mix the mask and the data */
    1.98 +    clen = (w / 8) * h;
    1.99 +    x_data = (char *) SDL_malloc(clen);
   1.100 +    if (x_data == NULL) {
   1.101 +        SDL_free(cursor);
   1.102 +        SDL_OutOfMemory();
   1.103 +        return (NULL);
   1.104 +    }
   1.105 +    x_mask = (char *) SDL_malloc(clen);
   1.106 +    if (x_mask == NULL) {
   1.107 +        SDL_free(cursor);
   1.108 +        SDL_free(x_data);
   1.109 +        SDL_OutOfMemory();
   1.110 +        return (NULL);
   1.111 +    }
   1.112 +    for (i = 0; i < clen; ++i) {
   1.113 +        /* The mask is OR'd with the data to turn inverted color
   1.114 +           pixels black since inverted color cursors aren't supported
   1.115 +           under X11.
   1.116 +         */
   1.117 +        x_mask[i] = data[i] | mask[i];
   1.118 +        x_data[i] = data[i];
   1.119 +    }
   1.120  
   1.121 -	/* Prevent the event thread from running while we use the X server */
   1.122 -	SDL_Lock_EventThread();
   1.123 +    /* Prevent the event thread from running while we use the X server */
   1.124 +    SDL_Lock_EventThread();
   1.125  
   1.126 -	/* Create the data image */
   1.127 -	data_image = XCreateImage(SDL_Display, 
   1.128 -			DefaultVisual(SDL_Display, SDL_Screen),
   1.129 -					1, XYBitmap, 0, x_data, w, h, 8, w/8);
   1.130 -	data_image->byte_order = MSBFirst;
   1.131 -	data_image->bitmap_bit_order = MSBFirst;
   1.132 -	data_pixmap = XCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
   1.133 +    /* Create the data image */
   1.134 +    data_image = XCreateImage(SDL_Display,
   1.135 +                              DefaultVisual(SDL_Display, SDL_Screen),
   1.136 +                              1, XYBitmap, 0, x_data, w, h, 8, w / 8);
   1.137 +    data_image->byte_order = MSBFirst;
   1.138 +    data_image->bitmap_bit_order = MSBFirst;
   1.139 +    data_pixmap = XCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
   1.140  
   1.141 -	/* Create the data mask */
   1.142 -	mask_image = XCreateImage(SDL_Display, 
   1.143 -			DefaultVisual(SDL_Display, SDL_Screen),
   1.144 -					1, XYBitmap, 0, x_mask, w, h, 8, w/8);
   1.145 -	mask_image->byte_order = MSBFirst;
   1.146 -	mask_image->bitmap_bit_order = MSBFirst;
   1.147 -	mask_pixmap = XCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
   1.148 +    /* Create the data mask */
   1.149 +    mask_image = XCreateImage(SDL_Display,
   1.150 +                              DefaultVisual(SDL_Display, SDL_Screen),
   1.151 +                              1, XYBitmap, 0, x_mask, w, h, 8, w / 8);
   1.152 +    mask_image->byte_order = MSBFirst;
   1.153 +    mask_image->bitmap_bit_order = MSBFirst;
   1.154 +    mask_pixmap = XCreatePixmap(SDL_Display, SDL_Root, w, h, 1);
   1.155  
   1.156 -	/* Create the graphics context */
   1.157 -	GCvalues.function = GXcopy;
   1.158 -	GCvalues.foreground = ~0;
   1.159 -	GCvalues.background =  0;
   1.160 -	GCvalues.plane_mask = AllPlanes;
   1.161 -	GCcursor = XCreateGC(SDL_Display, data_pixmap,
   1.162 -			(GCFunction|GCForeground|GCBackground|GCPlaneMask),
   1.163 -								&GCvalues);
   1.164 +    /* Create the graphics context */
   1.165 +    GCvalues.function = GXcopy;
   1.166 +    GCvalues.foreground = ~0;
   1.167 +    GCvalues.background = 0;
   1.168 +    GCvalues.plane_mask = AllPlanes;
   1.169 +    GCcursor = XCreateGC(SDL_Display, data_pixmap,
   1.170 +                         (GCFunction | GCForeground | GCBackground |
   1.171 +                          GCPlaneMask), &GCvalues);
   1.172  
   1.173 -	/* Blit the images to the pixmaps */
   1.174 -	XPutImage(SDL_Display, data_pixmap, GCcursor, data_image,
   1.175 -							0, 0, 0, 0, w, h);
   1.176 -	XPutImage(SDL_Display, mask_pixmap, GCcursor, mask_image,
   1.177 -							0, 0, 0, 0, w, h);
   1.178 -	XFreeGC(SDL_Display, GCcursor);
   1.179 -	/* These free the x_data and x_mask memory pointers */
   1.180 -	XDestroyImage(data_image);
   1.181 -	XDestroyImage(mask_image);
   1.182 +    /* Blit the images to the pixmaps */
   1.183 +    XPutImage(SDL_Display, data_pixmap, GCcursor, data_image,
   1.184 +              0, 0, 0, 0, w, h);
   1.185 +    XPutImage(SDL_Display, mask_pixmap, GCcursor, mask_image,
   1.186 +              0, 0, 0, 0, w, h);
   1.187 +    XFreeGC(SDL_Display, GCcursor);
   1.188 +    /* These free the x_data and x_mask memory pointers */
   1.189 +    XDestroyImage(data_image);
   1.190 +    XDestroyImage(mask_image);
   1.191  
   1.192 -	/* Create the cursor */
   1.193 -	cursor->x_cursor = XCreatePixmapCursor(SDL_Display, data_pixmap,
   1.194 -				mask_pixmap, &black, &white, hot_x, hot_y);
   1.195 -	XFreePixmap(SDL_Display, data_pixmap);
   1.196 -	XFreePixmap(SDL_Display, mask_pixmap);
   1.197 +    /* Create the cursor */
   1.198 +    cursor->x_cursor = XCreatePixmapCursor(SDL_Display, data_pixmap,
   1.199 +                                           mask_pixmap, &black, &white,
   1.200 +                                           hot_x, hot_y);
   1.201 +    XFreePixmap(SDL_Display, data_pixmap);
   1.202 +    XFreePixmap(SDL_Display, mask_pixmap);
   1.203  
   1.204 -	/* Release the event thread */
   1.205 -	XSync(SDL_Display, False);
   1.206 -	SDL_Unlock_EventThread();
   1.207 +    /* Release the event thread */
   1.208 +    XSync(SDL_Display, False);
   1.209 +    SDL_Unlock_EventThread();
   1.210  
   1.211 -	return(cursor);
   1.212 +    return (cursor);
   1.213  }
   1.214  
   1.215 -int X11_ShowWMCursor(_THIS, WMcursor *cursor)
   1.216 +int
   1.217 +X11_ShowWMCursor(_THIS, WMcursor * cursor)
   1.218  {
   1.219 -	/* Don't do anything if the display is gone */
   1.220 -	if ( SDL_Display == NULL ) {
   1.221 -		return(0);
   1.222 -	}
   1.223 +    /* Don't do anything if the display is gone */
   1.224 +    if (SDL_Display == NULL) {
   1.225 +        return (0);
   1.226 +    }
   1.227  
   1.228 -	/* Set the X11 cursor cursor, or blank if cursor is NULL */
   1.229 -	if ( SDL_Window ) {
   1.230 -		SDL_Lock_EventThread();
   1.231 -		if ( cursor == NULL ) {
   1.232 -			if ( SDL_BlankCursor != NULL ) {
   1.233 -				XDefineCursor(SDL_Display, SDL_Window,
   1.234 -					SDL_BlankCursor->x_cursor);
   1.235 -			}
   1.236 -		} else {
   1.237 -			XDefineCursor(SDL_Display, SDL_Window, cursor->x_cursor);
   1.238 -		}
   1.239 -		XSync(SDL_Display, False);
   1.240 -		SDL_Unlock_EventThread();
   1.241 -	}
   1.242 -	return(1);
   1.243 +    /* Set the X11 cursor cursor, or blank if cursor is NULL */
   1.244 +    if (SDL_Window) {
   1.245 +        SDL_Lock_EventThread();
   1.246 +        if (cursor == NULL) {
   1.247 +            if (SDL_BlankCursor != NULL) {
   1.248 +                XDefineCursor(SDL_Display, SDL_Window,
   1.249 +                              SDL_BlankCursor->x_cursor);
   1.250 +            }
   1.251 +        } else {
   1.252 +            XDefineCursor(SDL_Display, SDL_Window, cursor->x_cursor);
   1.253 +        }
   1.254 +        XSync(SDL_Display, False);
   1.255 +        SDL_Unlock_EventThread();
   1.256 +    }
   1.257 +    return (1);
   1.258  }
   1.259  
   1.260 -void X11_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
   1.261 +void
   1.262 +X11_WarpWMCursor(_THIS, Uint16 x, Uint16 y)
   1.263  {
   1.264 -	if ( using_dga & DGA_MOUSE ) {
   1.265 -		SDL_PrivateMouseMotion(0, 0, x, y);
   1.266 -	} else if ( mouse_relative) {
   1.267 -		/*	RJR: March 28, 2000
   1.268 -			leave physical cursor at center of screen if
   1.269 -			mouse hidden and grabbed */
   1.270 -		SDL_PrivateMouseMotion(0, 0, x, y);
   1.271 -	} else {
   1.272 -		SDL_Lock_EventThread();
   1.273 -		XWarpPointer(SDL_Display, None, SDL_Window, 0, 0, 0, 0, x, y);
   1.274 -		XSync(SDL_Display, False);
   1.275 -		SDL_Unlock_EventThread();
   1.276 -	}
   1.277 +    if (using_dga & DGA_MOUSE) {
   1.278 +        SDL_PrivateMouseMotion(0, 0, x, y);
   1.279 +    } else if (mouse_relative) {
   1.280 +        /*      RJR: March 28, 2000
   1.281 +           leave physical cursor at center of screen if
   1.282 +           mouse hidden and grabbed */
   1.283 +        SDL_PrivateMouseMotion(0, 0, x, y);
   1.284 +    } else {
   1.285 +        SDL_Lock_EventThread();
   1.286 +        XWarpPointer(SDL_Display, None, SDL_Window, 0, 0, 0, 0, x, y);
   1.287 +        XSync(SDL_Display, False);
   1.288 +        SDL_Unlock_EventThread();
   1.289 +    }
   1.290  }
   1.291  
   1.292  /* Sets the mouse acceleration from a string of the form:
   1.293 @@ -189,96 +196,102 @@
   1.294     The first number is the numerator, followed by the acceleration
   1.295     denumenator and threshold.
   1.296  */
   1.297 -static void SetMouseAccel(_THIS, const char *accel_param)
   1.298 +static void
   1.299 +SetMouseAccel(_THIS, const char *accel_param)
   1.300  {
   1.301 -	int i;
   1.302 -	size_t len;
   1.303 -	int accel_value[3];
   1.304 -	char *mouse_param, *mouse_param_buf, *pin;
   1.305 +    int i;
   1.306 +    size_t len;
   1.307 +    int accel_value[3];
   1.308 +    char *mouse_param, *mouse_param_buf, *pin;
   1.309  
   1.310 -	len = SDL_strlen(accel_param)+1;
   1.311 -	mouse_param_buf = SDL_stack_alloc(char, len);
   1.312 -	if ( ! mouse_param_buf ) {
   1.313 -		return;
   1.314 -	}
   1.315 -	SDL_strlcpy(mouse_param_buf, accel_param, len);
   1.316 -	mouse_param = mouse_param_buf;
   1.317 +    len = SDL_strlen(accel_param) + 1;
   1.318 +    mouse_param_buf = SDL_stack_alloc(char, len);
   1.319 +    if (!mouse_param_buf) {
   1.320 +        return;
   1.321 +    }
   1.322 +    SDL_strlcpy(mouse_param_buf, accel_param, len);
   1.323 +    mouse_param = mouse_param_buf;
   1.324  
   1.325 -	for ( i=0; (i < 3) && mouse_param; ++i ) {
   1.326 -		pin = SDL_strchr(mouse_param, '/');
   1.327 -		if ( pin ) {
   1.328 -			*pin = '\0';
   1.329 -		}
   1.330 -		accel_value[i] = atoi(mouse_param);
   1.331 -		if ( pin ) {
   1.332 -			mouse_param = pin+1;
   1.333 -		} else {
   1.334 -			mouse_param = NULL;
   1.335 -		}
   1.336 -	}
   1.337 -	if ( mouse_param_buf ) {
   1.338 -		XChangePointerControl(SDL_Display, True, True,
   1.339 -			accel_value[0], accel_value[1], accel_value[2]);
   1.340 -		SDL_free(mouse_param_buf);
   1.341 -	}
   1.342 +    for (i = 0; (i < 3) && mouse_param; ++i) {
   1.343 +        pin = SDL_strchr(mouse_param, '/');
   1.344 +        if (pin) {
   1.345 +            *pin = '\0';
   1.346 +        }
   1.347 +        accel_value[i] = atoi(mouse_param);
   1.348 +        if (pin) {
   1.349 +            mouse_param = pin + 1;
   1.350 +        } else {
   1.351 +            mouse_param = NULL;
   1.352 +        }
   1.353 +    }
   1.354 +    if (mouse_param_buf) {
   1.355 +        XChangePointerControl(SDL_Display, True, True,
   1.356 +                              accel_value[0], accel_value[1], accel_value[2]);
   1.357 +        SDL_free(mouse_param_buf);
   1.358 +    }
   1.359  }
   1.360  
   1.361  /* Check to see if we need to enter or leave mouse relative mode */
   1.362 -void X11_CheckMouseModeNoLock(_THIS)
   1.363 +void
   1.364 +X11_CheckMouseModeNoLock(_THIS)
   1.365  {
   1.366 -	const Uint8 full_focus = (SDL_APPACTIVE|SDL_APPINPUTFOCUS|SDL_APPMOUSEFOCUS);
   1.367 -	char *env_override;
   1.368 -	int enable_relative = 1;
   1.369 +    const Uint8 full_focus =
   1.370 +        (SDL_APPACTIVE | SDL_APPINPUTFOCUS | SDL_APPMOUSEFOCUS);
   1.371 +    char *env_override;
   1.372 +    int enable_relative = 1;
   1.373  
   1.374 -	/* Allow the user to override the relative mouse mode.
   1.375 -	   They almost never want to do this, as it seriously affects
   1.376 -	   applications that rely on continuous relative mouse motion.
   1.377 -	*/
   1.378 -	env_override = SDL_getenv("SDL_MOUSE_RELATIVE");
   1.379 -	if ( env_override ) {
   1.380 -		enable_relative = atoi(env_override);
   1.381 -	}
   1.382 +    /* Allow the user to override the relative mouse mode.
   1.383 +       They almost never want to do this, as it seriously affects
   1.384 +       applications that rely on continuous relative mouse motion.
   1.385 +     */
   1.386 +    env_override = SDL_getenv("SDL_MOUSE_RELATIVE");
   1.387 +    if (env_override) {
   1.388 +        enable_relative = atoi(env_override);
   1.389 +    }
   1.390  
   1.391 -	/* If the mouse is hidden and input is grabbed, we use relative mode */
   1.392 -	if ( enable_relative &&
   1.393 -	     !(SDL_cursorstate & CURSOR_VISIBLE) &&
   1.394 -	     (this->input_grab != SDL_GRAB_OFF) &&
   1.395 -             (SDL_GetAppState() & full_focus) == full_focus ) {
   1.396 -		if ( ! mouse_relative ) {
   1.397 -			X11_EnableDGAMouse(this);
   1.398 -			if ( ! (using_dga & DGA_MOUSE) ) {
   1.399 -				char *xmouse_accel;
   1.400 +    /* If the mouse is hidden and input is grabbed, we use relative mode */
   1.401 +    if (enable_relative &&
   1.402 +        !(SDL_cursorstate & CURSOR_VISIBLE) &&
   1.403 +        (SDL_CurrentWindow.input_grab != SDL_GRAB_OFF) &&
   1.404 +        (SDL_GetAppState() & full_focus) == full_focus) {
   1.405 +        if (!mouse_relative) {
   1.406 +            X11_EnableDGAMouse(this);
   1.407 +            if (!(using_dga & DGA_MOUSE)) {
   1.408 +                char *xmouse_accel;
   1.409  
   1.410 -				SDL_GetMouseState(&mouse_last.x, &mouse_last.y);
   1.411 -				/* Use as raw mouse mickeys as possible */
   1.412 -				XGetPointerControl(SDL_Display,
   1.413 -						&mouse_accel.numerator, 
   1.414 -						&mouse_accel.denominator,
   1.415 -						&mouse_accel.threshold);
   1.416 -				xmouse_accel=SDL_getenv("SDL_VIDEO_X11_MOUSEACCEL");
   1.417 -				if ( xmouse_accel ) {
   1.418 -					SetMouseAccel(this, xmouse_accel);
   1.419 -				}
   1.420 -			}
   1.421 -			mouse_relative = 1;
   1.422 -		}
   1.423 -	} else {
   1.424 -		if ( mouse_relative ) {
   1.425 -			if ( using_dga & DGA_MOUSE ) {
   1.426 -				X11_DisableDGAMouse(this);
   1.427 -			} else {
   1.428 -				XChangePointerControl(SDL_Display, True, True,
   1.429 -						mouse_accel.numerator, 
   1.430 -						mouse_accel.denominator,
   1.431 -						mouse_accel.threshold);
   1.432 -			}
   1.433 -			mouse_relative = 0;
   1.434 -		}
   1.435 -	}
   1.436 +                SDL_GetMouseState(&mouse_last.x, &mouse_last.y);
   1.437 +                /* Use as raw mouse mickeys as possible */
   1.438 +                XGetPointerControl(SDL_Display,
   1.439 +                                   &mouse_accel.numerator,
   1.440 +                                   &mouse_accel.denominator,
   1.441 +                                   &mouse_accel.threshold);
   1.442 +                xmouse_accel = SDL_getenv("SDL_VIDEO_X11_MOUSEACCEL");
   1.443 +                if (xmouse_accel) {
   1.444 +                    SetMouseAccel(this, xmouse_accel);
   1.445 +                }
   1.446 +            }
   1.447 +            mouse_relative = 1;
   1.448 +        }
   1.449 +    } else {
   1.450 +        if (mouse_relative) {
   1.451 +            if (using_dga & DGA_MOUSE) {
   1.452 +                X11_DisableDGAMouse(this);
   1.453 +            } else {
   1.454 +                XChangePointerControl(SDL_Display, True, True,
   1.455 +                                      mouse_accel.numerator,
   1.456 +                                      mouse_accel.denominator,
   1.457 +                                      mouse_accel.threshold);
   1.458 +            }
   1.459 +            mouse_relative = 0;
   1.460 +        }
   1.461 +    }
   1.462  }
   1.463 -void X11_CheckMouseMode(_THIS)
   1.464 +void
   1.465 +X11_CheckMouseMode(_THIS)
   1.466  {
   1.467 -	SDL_Lock_EventThread();
   1.468 -	X11_CheckMouseModeNoLock(this);
   1.469 -	SDL_Unlock_EventThread();
   1.470 +    SDL_Lock_EventThread();
   1.471 +    X11_CheckMouseModeNoLock(this);
   1.472 +    SDL_Unlock_EventThread();
   1.473  }
   1.474 +
   1.475 +/* vi: set ts=4 sw=4 expandtab: */