xf86Cursors.c 17.8 KB
Newer Older
1 2
/*
 * Copyright © 2007 Keith Packard
3
 * Copyright © 2010 Aaron Plattner
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that copyright
 * notice and this permission notice appear in supporting documentation, and
 * that the name of the copyright holders not be used in advertising or
 * publicity pertaining to distribution of the software without specific,
 * written prior permission.  The copyright holders make no representations
 * about the suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THIS SOFTWARE.
 */

#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#else
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#endif

#include <stddef.h>
#include <string.h>
#include <stdio.h>

#include "xf86.h"
#include "xf86DDC.h"
#include "xf86Crtc.h"
#include "xf86Modes.h"
#include "xf86RandR12.h"
41
#include "xf86CursorPriv.h"
42
#include "X11/extensions/render.h"
Peter Hutterer's avatar
Peter Hutterer committed
43
#include "X11/extensions/dpmsconst.h"
44 45 46
#include "X11/Xatom.h"
#include "picturestr.h"
#include "cursorstr.h"
47
#include "inputstr.h"
48 49 50 51 52 53 54 55 56 57 58 59 60

/*
 * Given a screen coordinate, rotate back to a cursor source coordinate
 */
static void
xf86_crtc_rotate_coord (Rotation    rotation,
			int	    width,
			int	    height,
			int	    x_dst,
			int	    y_dst,
			int	    *x_src,
			int	    *y_src)
{
Keith Packard's avatar
Keith Packard committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    int t;
    
    switch (rotation & 0xf) {
    case RR_Rotate_0:
	break;
    case RR_Rotate_90:
	t = x_dst;
	x_dst = height - y_dst - 1;
	y_dst = t;
	break;
    case RR_Rotate_180:
	x_dst = width - x_dst - 1;
	y_dst = height - y_dst - 1;
	break;
    case RR_Rotate_270:
	t = x_dst;
	x_dst = y_dst;
	y_dst = width - t - 1;
	break;
    }
81 82 83 84
    if (rotation & RR_Reflect_X)
	x_dst = width - x_dst - 1;
    if (rotation & RR_Reflect_Y)
	y_dst = height - y_dst - 1;
Keith Packard's avatar
Keith Packard committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    *x_src = x_dst;
    *y_src = y_dst;
}

/*
 * Given a cursor source  coordinate, rotate to a screen coordinate
 */
static void
xf86_crtc_rotate_coord_back (Rotation    rotation,
			     int	    width,
			     int	    height,
			     int	    x_dst,
			     int	    y_dst,
			     int	    *x_src,
			     int	    *y_src)
{
    int t;
102
    
Keith Packard's avatar
Keith Packard committed
103 104 105 106 107
    if (rotation & RR_Reflect_X)
	x_dst = width - x_dst - 1;
    if (rotation & RR_Reflect_Y)
	y_dst = height - y_dst - 1;

108 109 110 111
    switch (rotation & 0xf) {
    case RR_Rotate_0:
	break;
    case RR_Rotate_90:
Keith Packard's avatar
Keith Packard committed
112 113 114
	t = x_dst;
	x_dst = y_dst;
	y_dst = width - t - 1;
115 116
	break;
    case RR_Rotate_180:
Keith Packard's avatar
Keith Packard committed
117 118
	x_dst = width - x_dst - 1;
	y_dst = height - y_dst - 1;
119 120
	break;
    case RR_Rotate_270:
Keith Packard's avatar
Keith Packard committed
121 122 123
	t = x_dst;
	x_dst = height - y_dst - 1;
	y_dst = t;
124 125
	break;
    }
Keith Packard's avatar
Keith Packard committed
126 127
    *x_src = x_dst;
    *y_src = y_dst;
128 129
}

130 131 132 133 134
struct cursor_bit {
    CARD8 *byte;
    char bitpos;
};

135 136 137
/*
 * Convert an x coordinate to a position within the cursor bitmap
 */
138 139 140
static struct cursor_bit
cursor_bitpos (CARD8 *image, xf86CursorInfoPtr cursor_info, int x, int y,
	       Bool mask)
141
{
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    const int flags = cursor_info->Flags;
    const Bool interleaved =
	!!(flags & (HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_1 |
		    HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_8 |
		    HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_16 |
		    HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_32 |
		    HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64));
    const int width = cursor_info->MaxWidth;
    const int height = cursor_info->MaxHeight;
    const int stride = interleaved ? width / 4 : width / 8;

    struct cursor_bit ret;

    image += y * stride;

157 158 159 160
    if (flags & HARDWARE_CURSOR_SWAP_SOURCE_AND_MASK)
	mask = !mask;
    if (flags & HARDWARE_CURSOR_NIBBLE_SWAPPED)
	x = (x & ~3) | (3 - (x & 3));
161 162
    if (((flags & HARDWARE_CURSOR_BIT_ORDER_MSBFIRST) == 0) ==
	(X_BYTE_ORDER == X_BIG_ENDIAN))
163 164 165 166 167 168 169 170 171 172 173
	x = (x & ~7) | (7 - (x & 7));
    if (flags & HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_1)
	x = (x << 1) + mask;
    else if (flags & HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_8)
	x = ((x & ~7) << 1) | (mask << 3) | (x & 7);
    else if (flags & HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_16)
	x = ((x & ~15) << 1) | (mask << 4) | (x & 15);
    else if (flags & HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_32)
	x = ((x & ~31) << 1) | (mask << 5) | (x & 31);
    else if (flags & HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64)
	x = ((x & ~63) << 1) | (mask << 6) | (x & 63);
174 175 176 177 178 179 180
    else if (mask)
	image += stride * height;

    ret.byte = image + (x / 8);
    ret.bitpos = x & 7;

    return ret;
181 182 183 184 185 186
}

/*
 * Fetch one bit from a cursor bitmap
 */
static CARD8
187
get_bit (CARD8 *image, xf86CursorInfoPtr cursor_info, int x, int y, Bool mask)
188
{
189 190
    struct cursor_bit bit = cursor_bitpos(image, cursor_info, x, y, mask);
    return (*bit.byte >> bit.bitpos) & 1;
191 192 193 194 195 196
}

/*
 * Set one bit in a cursor bitmap
 */
static void
197
set_bit (CARD8 *image, xf86CursorInfoPtr cursor_info, int x, int y, Bool mask)
198
{
199 200
    struct cursor_bit bit = cursor_bitpos(image, cursor_info, x, y, mask);
    *bit.byte |= 1 << bit.bitpos;
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
}
    
/*
 * Load a two color cursor into a driver that supports only ARGB cursors
 */
static void
xf86_crtc_convert_cursor_to_argb (xf86CrtcPtr crtc, unsigned char *src)
{
    ScrnInfoPtr		scrn = crtc->scrn;
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    xf86CursorInfoPtr	cursor_info = xf86_config->cursor_info;
    CARD32		*cursor_image = (CARD32 *) xf86_config->cursor_image;
    int			x, y;
    int			xin, yin;
    int			flags = cursor_info->Flags;
    CARD32		bits;

#ifdef ARGB_CURSOR
    crtc->cursor_argb = FALSE;
#endif

    for (y = 0; y < cursor_info->MaxHeight; y++)
	for (x = 0; x < cursor_info->MaxWidth; x++) 
	{
	    xf86_crtc_rotate_coord (crtc->rotation,
				    cursor_info->MaxWidth,
				    cursor_info->MaxHeight,
				    x, y, &xin, &yin);
229
	    if (get_bit (src, cursor_info, xin, yin, TRUE) ==
230 231
		((flags & HARDWARE_CURSOR_INVERT_MASK) == 0))
	    {
232
		if (get_bit (src, cursor_info, xin, yin, FALSE))
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
		    bits = xf86_config->cursor_fg;
		else
		    bits = xf86_config->cursor_bg;
	    }
	    else
		bits = 0;
	    cursor_image[y * cursor_info->MaxWidth + x] = bits;
	}
    crtc->funcs->load_cursor_argb (crtc, cursor_image);
}

/*
 * Set the colors for a two-color cursor (ignore for ARGB cursors)
 */
static void
xf86_set_cursor_colors (ScrnInfoPtr scrn, int bg, int fg)
{
    ScreenPtr		screen = scrn->pScreen;
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    CursorPtr		cursor = xf86_config->cursor;
    int			c;
254
    CARD8		*bits = cursor ?
255
        dixLookupPrivate(&cursor->devPrivates, CursorScreenKey(screen))
256
      : NULL;
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

    /* Save ARGB versions of these colors */
    xf86_config->cursor_fg = (CARD32) fg | 0xff000000;
    xf86_config->cursor_bg = (CARD32) bg | 0xff000000;
    
    for (c = 0; c < xf86_config->num_crtc; c++)
    {
	xf86CrtcPtr crtc = xf86_config->crtc[c];

	if (crtc->enabled && !crtc->cursor_argb)
	{
	    if (crtc->funcs->load_cursor_image)
		crtc->funcs->set_cursor_colors (crtc, bg, fg);
	    else if (bits)
		xf86_crtc_convert_cursor_to_argb (crtc, bits);
	}
    }
}

static void
xf86_crtc_hide_cursor (xf86CrtcPtr crtc)
{
    if (crtc->cursor_shown)
    {
	crtc->funcs->hide_cursor (crtc);
	crtc->cursor_shown = FALSE;
    }
}

286
void
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
xf86_hide_cursors (ScrnInfoPtr scrn)
{
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    int			c;

    xf86_config->cursor_on = FALSE;
    for (c = 0; c < xf86_config->num_crtc; c++)
    {
	xf86CrtcPtr crtc = xf86_config->crtc[c];

	if (crtc->enabled)
	    xf86_crtc_hide_cursor (crtc);
    }
}
    
static void
xf86_crtc_show_cursor (xf86CrtcPtr crtc)
{
    if (!crtc->cursor_shown && crtc->cursor_in_range)
    {
	crtc->funcs->show_cursor (crtc);
	crtc->cursor_shown = TRUE;
    }
}

312
void
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
xf86_show_cursors (ScrnInfoPtr scrn)
{
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    int			c;

    xf86_config->cursor_on = TRUE;
    for (c = 0; c < xf86_config->num_crtc; c++)
    {
	xf86CrtcPtr crtc = xf86_config->crtc[c];

	if (crtc->enabled)
	    xf86_crtc_show_cursor (crtc);
    }
}
    
static void
xf86_crtc_set_cursor_position (xf86CrtcPtr crtc, int x, int y)
{
    ScrnInfoPtr		scrn = crtc->scrn;
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    xf86CursorInfoPtr	cursor_info = xf86_config->cursor_info;
    DisplayModePtr	mode = &crtc->mode;
    Bool		in_range;
Keith Packard's avatar
Keith Packard committed
336
    int			dx, dy;
337 338

    /*
Keith Packard's avatar
Keith Packard committed
339
     * Transform position of cursor on screen
340
     */
341
    if (crtc->transform_in_use)
Keith Packard's avatar
Keith Packard committed
342
    {
343 344 345 346
	ScreenPtr	screen = scrn->pScreen;
	xf86CursorScreenPtr ScreenPriv =
	    (xf86CursorScreenPtr)dixLookupPrivate(&screen->devPrivates,
						  xf86CursorScreenKey);
347 348
	struct pict_f_vector   v;

349 350 351
	v.v[0] = (x + ScreenPriv->HotX) + 0.5;
	v.v[1] = (y + ScreenPriv->HotY) + 0.5;
	v.v[2] = 1;
352
	pixman_f_transform_point (&crtc->f_framebuffer_to_crtc, &v);
353 354 355
	/* cursor will have 0.5 added to it already so floor is sufficent */
	x = floor (v.v[0]);
	y = floor (v.v[1]);
356 357 358 359 360 361 362 363 364
	/*
	 * Transform position of cursor upper left corner
	 */
	xf86_crtc_rotate_coord_back (crtc->rotation,
				     cursor_info->MaxWidth,
				     cursor_info->MaxHeight,
				     ScreenPriv->HotX, ScreenPriv->HotY, &dx, &dy);
	x -= dx;
	y -= dy;
365
   }
Keith Packard's avatar
Keith Packard committed
366 367 368 369
    else
    {
	x -= crtc->x;
	y -= crtc->y;
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
    }

    /*
     * Disable the cursor when it is outside the viewport
     */
    in_range = TRUE;
    if (x >= mode->HDisplay || y >= mode->VDisplay ||
	x <= -cursor_info->MaxWidth || y <= -cursor_info->MaxHeight) 
    {
	in_range = FALSE;
	x = 0;
	y = 0;
    }

    crtc->cursor_in_range = in_range;
    
    if (in_range)
    {
	crtc->funcs->set_cursor_position (crtc, x, y);
	xf86_crtc_show_cursor (crtc);
    }
    else
	xf86_crtc_hide_cursor (crtc);
}

static void
xf86_set_cursor_position (ScrnInfoPtr scrn, int x, int y)
{
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    int			c;

    /* undo what xf86HWCurs did to the coordinates */
    x += scrn->frameX0;
    y += scrn->frameY0;
    for (c = 0; c < xf86_config->num_crtc; c++)
    {
	xf86CrtcPtr crtc = xf86_config->crtc[c];

	if (crtc->enabled)
	    xf86_crtc_set_cursor_position (crtc, x, y);
    }
}
    
/*
 * Load a two-color cursor into a crtc, performing rotation as needed
 */
static void
xf86_crtc_load_cursor_image (xf86CrtcPtr crtc, CARD8 *src)
{
    ScrnInfoPtr		scrn = crtc->scrn;
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    xf86CursorInfoPtr	cursor_info = xf86_config->cursor_info;
    CARD8		*cursor_image;

#ifdef ARGB_CURSOR
    crtc->cursor_argb = FALSE;
#endif

    if (crtc->rotation == RR_Rotate_0)
	cursor_image = src;
    else
    {
        int x, y;
    	int xin, yin;
	int stride = cursor_info->MaxWidth >> 2;
	
	cursor_image = xf86_config->cursor_image;
437
	memset(cursor_image, 0, cursor_info->MaxHeight * stride);
438 439 440 441 442 443 444 445
	
        for (y = 0; y < cursor_info->MaxHeight; y++)
	    for (x = 0; x < cursor_info->MaxWidth; x++) 
	    {
		xf86_crtc_rotate_coord (crtc->rotation,
					cursor_info->MaxWidth,
					cursor_info->MaxHeight,
					x, y, &xin, &yin);
446 447 448 449
		if (get_bit(src, cursor_info, xin, yin, FALSE))
		    set_bit(cursor_image, cursor_info, x, y, FALSE);
		if (get_bit(src, cursor_info, xin, yin, TRUE))
		    set_bit(cursor_image, cursor_info, x, y, TRUE);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	    }
    }
    crtc->funcs->load_cursor_image (crtc, cursor_image);
}
    
/*
 * Load a cursor image into all active CRTCs
 */
static void
xf86_load_cursor_image (ScrnInfoPtr scrn, unsigned char *src)
{
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    int			c;

    for (c = 0; c < xf86_config->num_crtc; c++)
    {
	xf86CrtcPtr crtc = xf86_config->crtc[c];

	if (crtc->enabled)
	{
	    if (crtc->funcs->load_cursor_image)
		xf86_crtc_load_cursor_image (crtc, src);
	    else if (crtc->funcs->load_cursor_argb)
		xf86_crtc_convert_cursor_to_argb (crtc, src);
	}
    }
}

static Bool
xf86_use_hw_cursor (ScreenPtr screen, CursorPtr cursor)
{
    ScrnInfoPtr		scrn = xf86Screens[screen->myNum];
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    xf86CursorInfoPtr	cursor_info = xf86_config->cursor_info;

485
    ++cursor->refcnt;
486 487
    if (xf86_config->cursor)
	FreeCursor (xf86_config->cursor, None);
488
    xf86_config->cursor = cursor;
489

490 491 492 493 494 495 496 497 498 499 500 501 502 503
    if (cursor->bits->width > cursor_info->MaxWidth ||
	cursor->bits->height> cursor_info->MaxHeight)
	return FALSE;

    return TRUE;
}

static Bool
xf86_use_hw_cursor_argb (ScreenPtr screen, CursorPtr cursor)
{
    ScrnInfoPtr		scrn = xf86Screens[screen->myNum];
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    xf86CursorInfoPtr	cursor_info = xf86_config->cursor_info;
    
504
    ++cursor->refcnt;
505 506
    if (xf86_config->cursor)
	FreeCursor (xf86_config->cursor, None);
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
    xf86_config->cursor = cursor;
    
    /* Make sure ARGB support is available */
    if ((cursor_info->Flags & HARDWARE_CURSOR_ARGB) == 0)
	return FALSE;
    
    if (cursor->bits->width > cursor_info->MaxWidth ||
	cursor->bits->height> cursor_info->MaxHeight)
	return FALSE;

    return TRUE;
}

static void
xf86_crtc_load_cursor_argb (xf86CrtcPtr crtc, CursorPtr cursor)
{
    ScrnInfoPtr		scrn = crtc->scrn;
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    xf86CursorInfoPtr	cursor_info = xf86_config->cursor_info;
    CARD32		*cursor_image = (CARD32 *) xf86_config->cursor_image;
    CARD32		*cursor_source = (CARD32 *) cursor->bits->argb;
    int			x, y;
    int			xin, yin;
    CARD32		bits;
    int			source_width = cursor->bits->width;
    int			source_height = cursor->bits->height;
    int			image_width = cursor_info->MaxWidth;
    int			image_height = cursor_info->MaxHeight;
    
    for (y = 0; y < image_height; y++)
	for (x = 0; x < image_width; x++)
	{
	    xf86_crtc_rotate_coord (crtc->rotation, image_width, image_height,
				    x, y, &xin, &yin);
	    if (xin < source_width && yin < source_height)
		bits = cursor_source[yin * source_width + xin];
	    else
		bits = 0;
	    cursor_image[y * image_width + x] = bits;
	}
    
    crtc->funcs->load_cursor_argb (crtc, cursor_image);
}

static void
xf86_load_cursor_argb (ScrnInfoPtr scrn, CursorPtr cursor)
{
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    int			c;

    for (c = 0; c < xf86_config->num_crtc; c++)
    {
	xf86CrtcPtr crtc = xf86_config->crtc[c];

	if (crtc->enabled)
	    xf86_crtc_load_cursor_argb (crtc, cursor);
    }
}

566
Bool
567 568 569 570 571 572 573 574 575 576
xf86_cursors_init (ScreenPtr screen, int max_width, int max_height, int flags)
{
    ScrnInfoPtr		scrn = xf86Screens[screen->myNum];
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    xf86CursorInfoPtr	cursor_info;

    cursor_info = xf86CreateCursorInfoRec();
    if (!cursor_info)
	return FALSE;

577
    xf86_config->cursor_image = malloc(max_width * max_height * 4);
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616

    if (!xf86_config->cursor_image)
    {
	xf86DestroyCursorInfoRec (cursor_info);
	return FALSE;
    }
	
    xf86_config->cursor_info = cursor_info;

    cursor_info->MaxWidth = max_width;
    cursor_info->MaxHeight = max_height;
    cursor_info->Flags = flags;

    cursor_info->SetCursorColors = xf86_set_cursor_colors;
    cursor_info->SetCursorPosition = xf86_set_cursor_position;
    cursor_info->LoadCursorImage = xf86_load_cursor_image;
    cursor_info->HideCursor = xf86_hide_cursors;
    cursor_info->ShowCursor = xf86_show_cursors;
    cursor_info->UseHWCursor = xf86_use_hw_cursor;
#ifdef ARGB_CURSOR
    if (flags & HARDWARE_CURSOR_ARGB)
    {
	cursor_info->UseHWCursorARGB = xf86_use_hw_cursor_argb;
	cursor_info->LoadCursorARGB = xf86_load_cursor_argb;
    }
#endif
    
    xf86_config->cursor = NULL;
    xf86_hide_cursors (scrn);
    
    return xf86InitCursor (screen, cursor_info);
}

/**
 * Called when anything on the screen is reconfigured.
 *
 * Reloads cursor images as needed, then adjusts cursor positions
 */

617
void
618 619
xf86_reload_cursors (ScreenPtr screen)
{
620 621 622 623
    ScrnInfoPtr		scrn;
    xf86CrtcConfigPtr   xf86_config;
    xf86CursorInfoPtr   cursor_info;
    CursorPtr		cursor;
624
    int			x, y;
625
    xf86CursorScreenPtr cursor_screen_priv;
626
    
627 628 629 630
    /* initial mode setting will not have set a screen yet.
       May be called before the devices are initialised.
     */
    if (!screen || !inputInfo.pointer)
631
	return;
632 633 634
    cursor_screen_priv = dixLookupPrivate(&screen->devPrivates,
					  xf86CursorScreenKey);
    /* return if HW cursor is inactive, to avoid displaying two cursors */
635
    if (!cursor_screen_priv || !cursor_screen_priv->isUp)
636 637
	return;

638 639 640 641 642 643 644
    scrn = xf86Screens[screen->myNum];
    xf86_config = XF86_CRTC_CONFIG_PTR(scrn);

    /* make sure the cursor code has been initialized */
    cursor_info = xf86_config->cursor_info;
    if (!cursor_info)
	return;
645

646
    cursor = xf86_config->cursor;
647
    GetSpritePosition (inputInfo.pointer, &x, &y);
648 649 650 651 652
    if (!(cursor_info->Flags & HARDWARE_CURSOR_UPDATE_UNHIDDEN))
	(*cursor_info->HideCursor)(scrn);

    if (cursor)
    {
653
	void *src = dixLookupPrivate(&cursor->devPrivates, CursorScreenKey(screen));
654 655 656
#ifdef ARGB_CURSOR
	if (cursor->bits->argb && cursor_info->LoadCursorARGB)
	    (*cursor_info->LoadCursorARGB) (scrn, cursor);
657
	else if (src)
658
#endif
659
	    (*cursor_info->LoadCursorImage)(scrn, src);
660

661 662 663
	x += scrn->frameX0 + cursor_screen_priv->HotX;
	y += scrn->frameY0 + cursor_screen_priv->HotY;
	(*cursor_info->SetCursorPosition)(scrn, x, y);
664 665 666 667 668 669
    }
}

/**
 * Clean up CRTC-based cursor code
 */
670
void
671 672 673 674 675 676 677 678 679 680
xf86_cursors_fini (ScreenPtr screen)
{
    ScrnInfoPtr		scrn = xf86Screens[screen->myNum];
    xf86CrtcConfigPtr   xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
    
    if (xf86_config->cursor_info)
    {
	xf86DestroyCursorInfoRec (xf86_config->cursor_info);
	xf86_config->cursor_info = NULL;
    }
681 682
    free(xf86_config->cursor_image);
    xf86_config->cursor_image = NULL;
683 684 685 686 687
    if (xf86_config->cursor)
    {
	FreeCursor (xf86_config->cursor, None);
	xf86_config->cursor = NULL;
    }
688
}