summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/hw/darwin/bundle/quartzCursor.c
blob: 9435d96e91c5e074d12591913d2b535caf8643f2 (plain)
1
2
3
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**************************************************************
 *
 * Support for using the Quartz Window Manager cursor
 *
 **************************************************************/
/* $XFree86: xc/programs/Xserver/hw/darwin/bundle/quartzCursor.c,v 1.1 2001/04/02 05:18:50 torrey Exp $ */

#include "mi.h"
#include "scrnintstr.h"
#include "cursorstr.h"
#include "mipointrst.h"

#undef AllocCursor
#define Cursor QD_Cursor
#define WindowPtr QD_WindowPtr
#include <ApplicationServices/ApplicationServices.h>

// Size of the QuickDraw cursor
#define CURSORWIDTH 16
#define CURSORHEIGHT 16

typedef struct {
    int                     cursorMode;
    QueryBestSizeProcPtr    QueryBestSize;
    miPointerSpriteFuncPtr  spriteFuncs;
} QuartzCursorScreenRec, *QuartzCursorScreenPtr;

static int darwinCursorScreenIndex = -1;
static unsigned long darwinCursorGeneration = 0;

/*
===========================================================================

 Pointer sprite functions

===========================================================================
*/

/*
 * QuartzRealizeCursor
 * Convert the X cursor representation to QuickDraw format if possible.
 */
Bool
QuartzRealizeCursor(
    ScreenPtr pScreen,
    CursorPtr pCursor )
{
    int i;
    QD_Cursor *curs;
    QuartzCursorScreenPtr ScreenPriv = (QuartzCursorScreenPtr)
        pScreen->devPrivates[darwinCursorScreenIndex].ptr;

    if(!pCursor || !pCursor->bits)
        return FALSE;

    // if the cursor is too big we use a software cursor
    if ((pCursor->bits->height > CURSORHEIGHT) ||
        (pCursor->bits->width > CURSORWIDTH))
        return (*ScreenPriv->spriteFuncs->RealizeCursor)(pScreen, pCursor);

    // allocate memory for new cursor image
    curs = xalloc( sizeof(QD_Cursor) );
    if (!curs)
        return FALSE;

    // X cursor max size is 32x32 (rowbytes 4).
    // Copy top left 16x16 for now.
    for (i = 0; i < 16; i++)
    {
        curs->data[i] = (pCursor->bits->source[i*4]<<8) |
                        pCursor->bits->source[i*4+1];
        curs->mask[i] = (pCursor->bits->mask[i*4]<<8) |
                        pCursor->bits->mask[i*4+1];
    }
    curs->hotSpot.h = pCursor->bits->xhot;
    if(curs->hotSpot.h >= 16)
        curs->hotSpot.h = 15;
    curs->hotSpot.v = pCursor->bits->yhot;
    if(curs->hotSpot.v >= 16)
        curs->hotSpot.v = 15;

    // save the result
    pCursor->devPriv[pScreen->myNum] = (pointer) curs;
    return TRUE;
}


/*
 * QuartzUnrealizeCursor
 * Free the storage space associated with a realized cursor.
 */
Bool
QuartzUnrealizeCursor(
    ScreenPtr pScreen,
    CursorPtr pCursor )
{
    QuartzCursorScreenPtr ScreenPriv = (QuartzCursorScreenPtr)
        pScreen->devPrivates[darwinCursorScreenIndex].ptr;

    if ((pCursor->bits->height > CURSORHEIGHT) ||
        (pCursor->bits->width > CURSORWIDTH)) {
        return (*ScreenPriv->spriteFuncs->UnrealizeCursor)(pScreen, pCursor);
    } else {
        xfree( pCursor->devPriv[pScreen->myNum] );
        return TRUE;
    }
}


/*
 * QuartzSetCursor
 * Set the cursor sprite and position.
 * Use QuickDraw cursor if possible.
 */
static void
QuartzSetCursor(
    ScreenPtr       pScreen,
    CursorPtr       pCursor,
    int             x,
    int             y)
{
    QD_Cursor *curs;
    QuartzCursorScreenPtr ScreenPriv = (QuartzCursorScreenPtr)
        pScreen->devPrivates[darwinCursorScreenIndex].ptr;

    // are we supposed to remove the cursor?
    if (!pCursor) {
        if (ScreenPriv->cursorMode == 0)
            (*ScreenPriv->spriteFuncs->SetCursor)(pScreen, 0, x, y);
        else
            CGDisplayHideCursor(kCGDirectMainDisplay);
        return;
    }

    // can we use QuickDraw cursor?
    if ((pCursor->bits->height <= CURSORHEIGHT) &&
        (pCursor->bits->width <= CURSORWIDTH)) {

        if (ScreenPriv->cursorMode == 0)    // remove the X cursor
            (*ScreenPriv->spriteFuncs->SetCursor)(pScreen, 0, x, y);

        ScreenPriv->cursorMode = 1;
        curs = (QD_Cursor *) pCursor->devPriv[pScreen->myNum];
        SetCursor(curs);
        return;
    }

    // otherwise we use a software cursor
    if (ScreenPriv->cursorMode) {
        // remove the QuickDraw cursor
        QuartzSetCursor(pScreen, 0, x, y);
    }

    ScreenPriv->cursorMode = 0;
    (*ScreenPriv->spriteFuncs->SetCursor)(pScreen, pCursor, x, y);
}


/*
 * QuartzMoveCursor
 * Move the cursor. This is a noop for QuickDraw.
 */
static void
QuartzMoveCursor(
    ScreenPtr   pScreen,
    int         x,
    int         y)
{
    QuartzCursorScreenPtr ScreenPriv = (QuartzCursorScreenPtr)
        pScreen->devPrivates[darwinCursorScreenIndex].ptr;

    // only the X cursor needs to be explicitly moved
    if (!ScreenPriv->cursorMode)
        (*ScreenPriv->spriteFuncs->MoveCursor)(pScreen, x, y);
}


static miPointerSpriteFuncRec quartzSpriteFuncsRec = {
    QuartzRealizeCursor,
    QuartzUnrealizeCursor,
    QuartzSetCursor,
    QuartzMoveCursor
};

/*
===========================================================================

 Pointer screen functions

===========================================================================
*/

/*
 * QuartzCursorOffScreen
 */
static Bool QuartzCursorOffScreen(ScreenPtr *pScreen, int *x, int *y)
{
    return FALSE;
}


/*
 * QuartzCrossScreen
 */
static void QuartzCrossScreen(ScreenPtr pScreen, Bool entering)
{
    return;
}


/*
 * QuartzWarpCursor
 *  Change the cursor position without generating an event or motion history
 */
static void
QuartzWarpCursor(
    ScreenPtr               pScreen,
    int                     x,
    int                     y)
{
    CGDisplayErr            cgErr;
    CGPoint                 cgPoint;

    cgPoint = CGPointMake(x, y);
    cgErr = CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgPoint);
    if (cgErr != CGDisplayNoErr) {
        ErrorF("Could not set cursor position with error code 0x%x.\n", cgErr);
    }
    miPointerWarpCursor(pScreen, x, y);
}


static miPointerScreenFuncRec quartzScreenFuncsRec = {
    QuartzCursorOffScreen,
    QuartzCrossScreen,
    QuartzWarpCursor,
};

/*
===========================================================================

 Other screen functions

===========================================================================
*/

/*
 * QuartzCursorQueryBestSize
 * Handle queries for best cursor size
 */
static void
QuartzCursorQueryBestSize(
   int              class, 
   unsigned short   *width,
   unsigned short   *height,
   ScreenPtr        pScreen)
{
    QuartzCursorScreenPtr ScreenPriv = (QuartzCursorScreenPtr)
        pScreen->devPrivates[darwinCursorScreenIndex].ptr;

    if (class == CursorShape) {
        *width = CURSORWIDTH;
        *height = CURSORHEIGHT;
    } else
        (*ScreenPriv->QueryBestSize)(class, width, height, pScreen);
}


/*
 * QuartzInitCursor
 * Initialize cursor support
 */
Bool 
QuartzInitCursor(
    ScreenPtr	pScreen )
{
    QuartzCursorScreenPtr   ScreenPriv;
    miPointerScreenPtr	    PointPriv;

    // initialize software cursor handling (always needed as backup)
    if (!miDCInitialize(pScreen, &quartzScreenFuncsRec)) {
        return FALSE;
    }

    // allocate private storage for this screen's QuickDraw cursor info
    if (darwinCursorGeneration != serverGeneration) {
        if ((darwinCursorScreenIndex = AllocateScreenPrivateIndex()) < 0)
            return FALSE;
        darwinCursorGeneration = serverGeneration; 	
    }

    ScreenPriv = xcalloc( 1, sizeof(QuartzCursorScreenRec) );
    if (!ScreenPriv) return FALSE;

    pScreen->devPrivates[darwinCursorScreenIndex].ptr = (pointer) ScreenPriv;

    // override some screen procedures
    ScreenPriv->QueryBestSize = pScreen->QueryBestSize;
    pScreen->QueryBestSize = QuartzCursorQueryBestSize;

    // initialize QuickDraw cursor handling
    PointPriv = (miPointerScreenPtr)
                    pScreen->devPrivates[miPointerScreenIndex].ptr;

    ScreenPriv->spriteFuncs = PointPriv->spriteFuncs;
    PointPriv->spriteFuncs = &quartzSpriteFuncsRec; 

    ScreenPriv->cursorMode = 1;
    return TRUE;
}