Remove .gitignore do nothing is ignored.
[synfig.git] / synfig-osx / trunk / launcher / quartz-cursor.c
1 /**************************************************************
2  *
3  * Support for using the Quartz Window Manager cursor
4  *
5  **************************************************************/
6 /*
7  * Copyright (c) 2001 Torrey T. Lyons and Greg Parker.
8  * Copyright (c) 2002 Apple Computer, Inc.
9  *                 All Rights Reserved.
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  *
29  * Except as contained in this notice, the name(s) of the above copyright
30  * holders shall not be used in advertising or otherwise to promote the sale,
31  * use or other dealings in this Software without prior written authorization.
32  */
33 /* $XFree86: xc/programs/Xserver/hw/darwin/bundle/quartzCursor.c,v 1.15 2001/12/22 05:28:35 torrey Exp $ */
34
35 #include "quartz.h"
36 #include "quartz-cursor.h"
37 #include "Xplugin.h"
38
39 #include "mi.h"
40 #include "scrnintstr.h"
41 #include "cursorstr.h"
42 #include "mipointrst.h"
43 #include "windowstr.h"
44 #include "globals.h"
45 #include "servermd.h"
46 #include "dixevents.h"
47
48 #include <CoreGraphics/CoreGraphics.h>
49
50 typedef struct {
51     int                     CursorVisible;
52     QueryBestSizeProcPtr    QueryBestSize;
53     miPointerSpriteFuncPtr  spriteFuncs;
54 } QuartzCursorScreenRec, *QuartzCursorScreenPtr;
55
56 static int darwinCursorScreenIndex = -1;
57 static unsigned long darwinCursorGeneration = 0;
58
59 static Bool movedCursor = FALSE;
60
61 #define CURSOR_PRIV(pScreen) \
62     ((QuartzCursorScreenPtr)pScreen->devPrivates[darwinCursorScreenIndex].ptr)
63
64 static Bool
65 load_cursor (CursorPtr src, int screen)
66 {
67     uint32_t *data;
68     uint32_t rowbytes;
69     int width, height;
70     int hot_x, hot_y;
71
72     uint32_t fg_color, bg_color;
73     uint8_t *srow, *sptr;
74     uint8_t *mrow, *mptr;
75     uint32_t *drow, *dptr;
76     unsigned xcount, ycount;
77
78     xp_error err;
79
80     width = src->bits->width;
81     height = src->bits->height;
82     hot_x = src->bits->xhot;
83     hot_y = src->bits->yhot;
84
85 #ifdef ARGB_CURSOR
86     if (src->bits->argb != NULL)
87     {
88         rowbytes = src->bits->width * sizeof (CARD32);
89         data = (uint32_t *) src->bits->argb;
90     }
91     else
92 #endif
93     {
94         fg_color = 0xFF00 | (src->foreRed >> 8);
95         fg_color <<= 16;
96         fg_color |= src->foreGreen & 0xFF00;
97         fg_color |= src->foreBlue >> 8;
98
99         bg_color = 0xFF00 | (src->backRed >> 8);
100         bg_color <<= 16;
101         bg_color |= src->backGreen & 0xFF00;
102         bg_color |= src->backBlue >> 8;
103
104         fg_color = htonl (fg_color);
105         bg_color = htonl (bg_color);
106
107         /* round up to 8 pixel boundary so we can convert whole bytes */
108         rowbytes = ((src->bits->width * 4) + 31) & ~31;
109         data = alloca (rowbytes * src->bits->height);
110
111         if (!src->bits->emptyMask)
112         {
113             ycount = src->bits->height;
114             srow = src->bits->source; mrow = src->bits->mask;
115             drow = data;
116
117             while (ycount-- > 0)
118             {
119                 xcount = (src->bits->width + 7) / 8;
120                 sptr = srow; mptr = mrow;
121                 dptr = drow;
122
123                 while (xcount-- > 0)
124                 {
125                     uint8_t s, m;
126                     int i;
127
128                     s = *sptr++; m = *mptr++;
129                     for (i = 0; i < 8; i++)
130                     {
131 #if BITMAP_BIT_ORDER == MSBFirst
132                         if (m & 128)
133                             *dptr++ = (s & 128) ? fg_color : bg_color;
134                         else
135                             *dptr++ = 0;
136                         s <<= 1; m <<= 1;
137 #else
138                         if (m & 1)
139                             *dptr++ = (s & 1) ? fg_color : bg_color;
140                         else
141                             *dptr++ = 0;
142                         s >>= 1; m >>= 1;
143 #endif
144                     }
145                 }
146
147                 srow += BitmapBytePad (src->bits->width);
148                 mrow += BitmapBytePad (src->bits->width);
149                 drow = (uint32_t *) ((char *) drow + rowbytes);
150             }
151         }
152         else
153         {
154             memset (data, 0, src->bits->height * rowbytes);
155         }
156     }
157
158     err = xp_set_cursor (width, height, hot_x, hot_y, data, rowbytes);
159     return err == Success;
160 }
161
162 /* Convert the X cursor representation to native format if possible. */
163 static Bool
164 QuartzRealizeCursor (ScreenPtr pScreen, CursorPtr pCursor)
165 {
166     if(pCursor == NULL || pCursor->bits == NULL)
167         return FALSE;
168
169     /* FIXME: cache ARGB8888 representation? */
170
171     return TRUE;
172 }
173
174 /* Free the storage space associated with a realized cursor. */
175 static Bool
176 QuartzUnrealizeCursor (ScreenPtr pScreen, CursorPtr pCursor)
177 {
178     return TRUE;
179 }
180
181 /* Set the cursor sprite and position. */
182 static void
183 QuartzSetCursor (ScreenPtr pScreen, CursorPtr pCursor, int x, int y)
184 {
185     QuartzCursorScreenPtr ScreenPriv = CURSOR_PRIV(pScreen);
186
187     if (!quartzServerVisible)
188         return;
189
190     if (pCursor == NULL)
191     {
192         if (ScreenPriv->CursorVisible)
193         {
194             xp_hide_cursor ();
195             ScreenPriv->CursorVisible = FALSE;
196         }
197     }
198     else
199     {
200         load_cursor (pCursor, pScreen->myNum);
201
202         if (!ScreenPriv->CursorVisible)
203         {
204             xp_show_cursor ();
205             ScreenPriv->CursorVisible = TRUE;
206         }
207     }
208 }
209
210 /* Move the cursor. This is a noop for us. */
211 static void
212 QuartzMoveCursor (ScreenPtr pScreen, int x, int y)
213 {
214 }
215
216 static miPointerSpriteFuncRec quartzSpriteFuncsRec = {
217     QuartzRealizeCursor,
218     QuartzUnrealizeCursor,
219     QuartzSetCursor,
220     QuartzMoveCursor
221 };
222
223 static Bool
224 QuartzCursorOffScreen (ScreenPtr *pScreen, int *x, int *y)
225 {
226     return FALSE;
227 }
228
229 static void
230 QuartzCrossScreen (ScreenPtr pScreen, Bool entering)
231 {
232     return;
233 }
234
235 /* Change the cursor position without generating an event or motion history.
236    The input coordinates (x,y) are in pScreen-local X11 coordinates. */
237 static void
238 QuartzWarpCursor (ScreenPtr pScreen, int x, int y)
239 {
240     if (!movedCursor)
241     {
242         /* Don't move the cursor the first time. This is the
243            jump-to-center initialization, and it's annoying. */
244
245         movedCursor = TRUE;
246         return;
247     }
248
249     if (quartzServerVisible)
250     {
251         int sx, sy;
252
253         sx = dixScreenOrigins[pScreen->myNum].x + darwinMainScreenX;
254         sy = dixScreenOrigins[pScreen->myNum].y + darwinMainScreenY;
255
256         CGWarpMouseCursorPosition (CGPointMake (sx + x, sy + y));
257     }
258
259     miPointerWarpCursor (pScreen, x, y);
260 }
261
262 void
263 QuartzIgnoreNextWarpCursor (void)
264 {
265     movedCursor = FALSE;
266 }
267
268 static miPointerScreenFuncRec quartzScreenFuncsRec = {
269     QuartzCursorOffScreen,
270     QuartzCrossScreen,
271     QuartzWarpCursor,
272     DarwinEnqueuePointerEvent,
273 };
274
275 /* Handle queries for best cursor size */
276 static void
277 QuartzCursorQueryBestSize (int class, unsigned short *width,
278                            unsigned short *height, ScreenPtr pScreen)
279 {
280     QuartzCursorScreenPtr ScreenPriv = CURSOR_PRIV(pScreen);
281
282     if (class == CursorShape)
283     {
284         /* FIXME: query window server? */
285         *width = 32;
286         *height = 32;
287     }
288     else
289     {
290         (*ScreenPriv->QueryBestSize) (class, width, height, pScreen);
291     }
292 }
293
294 /* Initialize cursor support */
295 Bool
296 QuartzInitCursor (ScreenPtr pScreen)
297 {
298     QuartzCursorScreenPtr ScreenPriv;
299     miPointerScreenPtr PointPriv;
300
301     /* initialize software cursor handling (always needed as backup) */
302     if (!miDCInitialize (pScreen, &quartzScreenFuncsRec))
303         return FALSE;
304
305     /* allocate private storage for this screen's QuickDraw cursor info */
306     if (darwinCursorGeneration != serverGeneration)
307     {
308         if ((darwinCursorScreenIndex = AllocateScreenPrivateIndex ()) < 0)
309             return FALSE;
310
311         darwinCursorGeneration = serverGeneration;
312     }
313
314     ScreenPriv = xcalloc (1, sizeof(QuartzCursorScreenRec));
315     if (ScreenPriv == NULL)
316         return FALSE;
317
318     CURSOR_PRIV (pScreen) = ScreenPriv;
319
320     /* override some screen procedures */
321     ScreenPriv->QueryBestSize = pScreen->QueryBestSize;
322     pScreen->QueryBestSize = QuartzCursorQueryBestSize;
323
324     PointPriv = (miPointerScreenPtr) pScreen->devPrivates[miPointerScreenIndex].ptr;
325
326     ScreenPriv->spriteFuncs = PointPriv->spriteFuncs;
327     PointPriv->spriteFuncs = &quartzSpriteFuncsRec;
328
329     ScreenPriv->CursorVisible = TRUE;
330     return TRUE;
331 }
332
333 /* X server is hiding. Restore the Aqua cursor. */
334 void
335 QuartzSuspendXCursor (ScreenPtr pScreen)
336 {
337 }
338
339 /* X server is showing. Restore the X cursor. */
340 void
341 QuartzResumeXCursor (ScreenPtr pScreen)
342 {
343     WindowPtr pWin;
344     CursorPtr pCursor;
345     int x, y;
346
347     pWin = GetSpriteWindow ();
348     if (pWin->drawable.pScreen != pScreen)
349         return;
350
351     pCursor = GetSpriteCursor ();
352     if (pCursor == NULL)
353         return;
354
355     GetSpritePosition (&x, &y);
356     QuartzSetCursor (pScreen, pCursor, x, y);
357 }