1 /**************************************************************
3 * Xplugin cursor support
5 * Copyright (c) 2001 Torrey T. Lyons and Greg Parker.
6 * Copyright (c) 2002 Apple Computer, Inc.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
27 * Except as contained in this notice, the name(s) of the above copyright
28 * holders shall not be used in advertising or otherwise to promote the sale,
29 * use or other dealings in this Software without prior written authorization.
32 #include "sanitizedCarbon.h"
34 #ifdef HAVE_DIX_CONFIG_H
35 #include <dix-config.h>
41 #include "darwinEvents.h"
45 #include "scrnintstr.h"
46 #include "cursorstr.h"
47 #include "mipointrst.h"
48 #include "windowstr.h"
51 #include "dixevents.h"
56 QueryBestSizeProcPtr QueryBestSize;
57 miPointerSpriteFuncPtr spriteFuncs;
58 } QuartzCursorScreenRec, *QuartzCursorScreenPtr;
60 static DevPrivateKeyRec darwinCursorScreenKeyRec;
61 #define darwinCursorScreenKey (&darwinCursorScreenKeyRec)
63 #define CURSOR_PRIV(pScreen) ((QuartzCursorScreenPtr) \
64 dixLookupPrivate(&pScreen->devPrivates, darwinCursorScreenKey))
67 load_cursor(CursorPtr src, int screen)
70 Bool free_data = FALSE;
75 uint32_t fg_color, bg_color;
78 uint32_t *drow, *dptr;
79 unsigned xcount, ycount;
83 width = src->bits->width;
84 height = src->bits->height;
85 hot_x = src->bits->xhot;
86 hot_y = src->bits->yhot;
89 if (src->bits->argb != NULL)
91 #if BITMAP_BIT_ORDER == MSBFirst
92 rowbytes = src->bits->width * sizeof (CARD32);
93 data = (uint32_t *) src->bits->argb;
95 const uint32_t *be_data=(uint32_t *) src->bits->argb;
97 rowbytes = src->bits->width * sizeof (CARD32);
98 data = malloc(rowbytes * src->bits->height);
101 FatalError("Failed to allocate memory in %s\n", __func__);
103 for(i=0;i<(src->bits->width*src->bits->height);i++)
104 data[i]=ntohl(be_data[i]);
110 fg_color = 0xFF00 | (src->foreRed >> 8);
112 fg_color |= src->foreGreen & 0xFF00;
113 fg_color |= src->foreBlue >> 8;
115 bg_color = 0xFF00 | (src->backRed >> 8);
117 bg_color |= src->backGreen & 0xFF00;
118 bg_color |= src->backBlue >> 8;
120 fg_color = htonl(fg_color);
121 bg_color = htonl(bg_color);
123 /* round up to 8 pixel boundary so we can convert whole bytes */
124 rowbytes = ((src->bits->width * 4) + 31) & ~31;
125 data = malloc(rowbytes * src->bits->height);
128 FatalError("Failed to allocate memory in %s\n", __func__);
131 if (!src->bits->emptyMask)
133 ycount = src->bits->height;
134 srow = src->bits->source; mrow = src->bits->mask;
139 xcount = bits_to_bytes(src->bits->width);
140 sptr = srow; mptr = mrow;
148 s = *sptr++; m = *mptr++;
149 for (i = 0; i < 8; i++)
151 #if BITMAP_BIT_ORDER == MSBFirst
153 *dptr++ = (s & 128) ? fg_color : bg_color;
159 *dptr++ = (s & 1) ? fg_color : bg_color;
167 srow += BitmapBytePad(src->bits->width);
168 mrow += BitmapBytePad(src->bits->width);
169 drow = (uint32_t *) ((char *) drow + rowbytes);
174 memset(data, 0, src->bits->height * rowbytes);
178 err = xp_set_cursor(width, height, hot_x, hot_y, data, rowbytes);
181 return err == Success;
186 ===========================================================================
188 Pointer sprite functions
190 ===========================================================================
194 * QuartzRealizeCursor
195 * Convert the X cursor representation to native format if possible.
198 QuartzRealizeCursor(DeviceIntPtr pDev, ScreenPtr pScreen, CursorPtr pCursor)
200 if(pCursor == NULL || pCursor->bits == NULL)
203 /* FIXME: cache ARGB8888 representation? */
210 * QuartzUnrealizeCursor
211 * Free the storage space associated with a realized cursor.
214 QuartzUnrealizeCursor(DeviceIntPtr pDev, ScreenPtr pScreen, CursorPtr pCursor)
222 * Set the cursor sprite and position.
225 QuartzSetCursor(DeviceIntPtr pDev, ScreenPtr pScreen, CursorPtr pCursor, int x, int y)
227 QuartzCursorScreenPtr ScreenPriv = CURSOR_PRIV(pScreen);
229 if (!XQuartzServerVisible)
234 if (ScreenPriv->cursorVisible)
237 ScreenPriv->cursorVisible = FALSE;
242 load_cursor(pCursor, pScreen->myNum);
244 if (!ScreenPriv->cursorVisible)
247 ScreenPriv->cursorVisible = TRUE;
254 * Move the cursor. This is a noop for us.
257 QuartzMoveCursor(DeviceIntPtr pDev, ScreenPtr pScreen, int x, int y)
262 ===========================================================================
264 Pointer screen functions
266 ===========================================================================
270 * QuartzCursorOffScreen
273 QuartzCursorOffScreen(ScreenPtr *pScreen, int *x, int *y)
283 QuartzCrossScreen(ScreenPtr pScreen, Bool entering)
291 * Change the cursor position without generating an event or motion history.
292 * The input coordinates (x,y) are in pScreen-local X11 coordinates.
296 QuartzWarpCursor(DeviceIntPtr pDev, ScreenPtr pScreen, int x, int y)
298 if (XQuartzServerVisible)
302 sx = pScreen->x + darwinMainScreenX;
303 sy = pScreen->y + darwinMainScreenY;
305 CGWarpMouseCursorPosition(CGPointMake(sx + x, sy + y));
308 miPointerWarpCursor(pDev, pScreen, x, y);
309 miPointerUpdateSprite(pDev);
313 static miPointerScreenFuncRec quartzScreenFuncsRec = {
314 QuartzCursorOffScreen,
323 ===========================================================================
325 Other screen functions
327 ===========================================================================
331 * QuartzCursorQueryBestSize
332 * Handle queries for best cursor size
335 QuartzCursorQueryBestSize(int class, unsigned short *width,
336 unsigned short *height, ScreenPtr pScreen)
338 QuartzCursorScreenPtr ScreenPriv = CURSOR_PRIV(pScreen);
340 if (class == CursorShape)
342 /* FIXME: query window server? */
348 (*ScreenPriv->QueryBestSize)(class, width, height, pScreen);
354 * Initialize cursor support
357 QuartzInitCursor(ScreenPtr pScreen)
359 QuartzCursorScreenPtr ScreenPriv;
360 miPointerScreenPtr PointPriv;
362 /* initialize software cursor handling (always needed as backup) */
363 if (!miDCInitialize(pScreen, &quartzScreenFuncsRec))
366 if (!dixRegisterPrivateKey(&darwinCursorScreenKeyRec, PRIVATE_SCREEN, 0))
369 ScreenPriv = calloc(1, sizeof(QuartzCursorScreenRec));
370 if (ScreenPriv == NULL)
373 /* CURSOR_PRIV(pScreen) = ScreenPriv; */
374 dixSetPrivate(&pScreen->devPrivates, darwinCursorScreenKey, ScreenPriv);
376 /* override some screen procedures */
377 ScreenPriv->QueryBestSize = pScreen->QueryBestSize;
378 pScreen->QueryBestSize = QuartzCursorQueryBestSize;
380 PointPriv = dixLookupPrivate(&pScreen->devPrivates, miPointerScreenKey);
382 ScreenPriv->spriteFuncs = PointPriv->spriteFuncs;
384 PointPriv->spriteFuncs->RealizeCursor = QuartzRealizeCursor;
385 PointPriv->spriteFuncs->UnrealizeCursor = QuartzUnrealizeCursor;
386 PointPriv->spriteFuncs->SetCursor = QuartzSetCursor;
387 PointPriv->spriteFuncs->MoveCursor = QuartzMoveCursor;
389 ScreenPriv->cursorVisible = TRUE;
394 * QuartzSuspendXCursor
395 * X server is hiding. Restore the Aqua cursor.
398 QuartzSuspendXCursor(ScreenPtr pScreen)
404 * QuartzResumeXCursor
405 * X server is showing. Restore the X cursor.
408 QuartzResumeXCursor(ScreenPtr pScreen)
415 pWin = GetSpriteWindow(darwinPointer);
416 if (pWin->drawable.pScreen != pScreen)
419 pCursor = GetSpriteCursor(darwinPointer);
423 QuartzSetCursor(darwinPointer, pScreen, pCursor, /* x */ 0, /* y */ 0);