]> git.sesse.net Git - rdpsrv/blob - Xserver/programs/Xserver/hw/vnc/dispcur.c
Support RDP5 logon packets.
[rdpsrv] / Xserver / programs / Xserver / hw / vnc / dispcur.c
1 /*
2  * dispcur.c
3  *
4  * cursor display routines - based on midispcur.c
5  */
6
7 /*
8  *  Copyright (C) 1999 AT&T Laboratories Cambridge.  All Rights Reserved.
9  *
10  *  This is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This software is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this software; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
23  *  USA.
24  */
25
26 /*
27
28 Copyright (c) 1989  X Consortium
29
30 Permission is hereby granted, free of charge, to any person obtaining a copy
31 of this software and associated documentation files (the "Software"), to deal
32 in the Software without restriction, including without limitation the rights
33 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
34 copies of the Software, and to permit persons to whom the Software is
35 furnished to do so, subject to the following conditions:
36
37 The above copyright notice and this permission notice shall be included in
38 all copies or substantial portions of the Software.
39
40 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
43 X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
44 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
45 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46
47 Except as contained in this notice, the name of the X Consortium shall not be
48 used in advertising or otherwise to promote the sale, use or other dealings
49 in this Software without prior written authorization from the X Consortium.
50 */
51
52 #define NEED_EVENTS
53 # include   "X.h"
54 # include   "misc.h"
55 # include   "input.h"
56 # include   "cursorstr.h"
57 # include   "windowstr.h"
58 # include   "regionstr.h"
59 # include   "dixstruct.h"
60 # include   "scrnintstr.h"
61 # include   "servermd.h"
62 # include   "mipointer.h"
63 # include   "sprite.h"
64 # include   "gcstruct.h"
65
66 extern WindowPtr    *WindowTable;
67
68 /* per-screen private data */
69
70 static int      rfbDCScreenIndex;
71 static unsigned long rfbDCGeneration = 0;
72
73 static Bool     rfbDCCloseScreen();
74
75 typedef struct {
76     GCPtr           pSourceGC, pMaskGC;
77     GCPtr           pSaveGC, pRestoreGC;
78     GCPtr           pPixSourceGC, pPixMaskGC;
79     CloseScreenProcPtr CloseScreen;
80     PixmapPtr       pSave;
81 } rfbDCScreenRec, *rfbDCScreenPtr;
82
83 /* per-cursor per-screen private data */
84 typedef struct {
85     PixmapPtr           sourceBits;         /* source bits */
86     PixmapPtr           maskBits;           /* mask bits */
87 } rfbDCCursorRec, *rfbDCCursorPtr;
88
89 /*
90  * sprite/cursor method table
91  */
92
93 static Bool     rfbDCRealizeCursor(),       rfbDCUnrealizeCursor();
94 static Bool     rfbDCPutUpCursor(),         rfbDCSaveUnderCursor();
95 static Bool     rfbDCRestoreUnderCursor();
96
97 static rfbSpriteCursorFuncRec rfbDCFuncs = {
98     rfbDCRealizeCursor,
99     rfbDCUnrealizeCursor,
100     rfbDCPutUpCursor,
101     rfbDCSaveUnderCursor,
102     rfbDCRestoreUnderCursor,
103 };
104
105 Bool
106 rfbDCInitialize (pScreen, screenFuncs)
107     ScreenPtr               pScreen;
108     miPointerScreenFuncPtr  screenFuncs;
109 {
110     rfbDCScreenPtr   pScreenPriv;
111
112     if (rfbDCGeneration != serverGeneration)
113     {
114         rfbDCScreenIndex = AllocateScreenPrivateIndex ();
115         if (rfbDCScreenIndex < 0)
116             return FALSE;
117         rfbDCGeneration = serverGeneration;
118     }
119     pScreenPriv = (rfbDCScreenPtr) xalloc (sizeof (rfbDCScreenRec));
120     if (!pScreenPriv)
121         return FALSE;
122
123     /*
124      * initialize the entire private structure to zeros
125      */
126
127     pScreenPriv->pSourceGC =
128         pScreenPriv->pMaskGC =
129         pScreenPriv->pSaveGC =
130         pScreenPriv->pRestoreGC =
131         pScreenPriv->pPixSourceGC =
132         pScreenPriv->pPixMaskGC = NULL;
133     
134     pScreenPriv->pSave = NULL;
135
136     pScreenPriv->CloseScreen = pScreen->CloseScreen;
137     pScreen->CloseScreen = rfbDCCloseScreen;
138     
139     pScreen->devPrivates[rfbDCScreenIndex].ptr = (pointer) pScreenPriv;
140
141     if (!rfbSpriteInitialize (pScreen, &rfbDCFuncs, screenFuncs))
142     {
143         xfree ((pointer) pScreenPriv);
144         return FALSE;
145     }
146     return TRUE;
147 }
148
149 #define tossGC(gc)  (gc ? FreeGC (gc, (GContext) 0) : 0)
150 #define tossPix(pix)    (pix ? (*pScreen->DestroyPixmap) (pix) : TRUE)
151
152 static Bool
153 rfbDCCloseScreen (index, pScreen)
154     ScreenPtr   pScreen;
155 {
156     rfbDCScreenPtr   pScreenPriv;
157
158     pScreenPriv = (rfbDCScreenPtr) pScreen->devPrivates[rfbDCScreenIndex].ptr;
159     pScreen->CloseScreen = pScreenPriv->CloseScreen;
160     tossGC (pScreenPriv->pSourceGC);
161     tossGC (pScreenPriv->pMaskGC);
162     tossGC (pScreenPriv->pSaveGC);
163     tossGC (pScreenPriv->pRestoreGC);
164     tossGC (pScreenPriv->pPixSourceGC);
165     tossGC (pScreenPriv->pPixMaskGC);
166     tossPix (pScreenPriv->pSave);
167     xfree ((pointer) pScreenPriv);
168     return (*pScreen->CloseScreen) (index, pScreen);
169 }
170
171 static Bool
172 rfbDCRealizeCursor (pScreen, pCursor)
173     ScreenPtr   pScreen;
174     CursorPtr   pCursor;
175 {
176     if (pCursor->bits->refcnt <= 1)
177         pCursor->bits->devPriv[pScreen->myNum] = (pointer)NULL;
178     return TRUE;
179 }
180
181 static rfbDCCursorPtr
182 rfbDCRealize (pScreen, pCursor)
183     ScreenPtr   pScreen;
184     CursorPtr   pCursor;
185 {
186     rfbDCCursorPtr   pPriv;
187     GCPtr           pGC;
188     XID             gcvals[3];
189
190     pPriv = (rfbDCCursorPtr) xalloc (sizeof (rfbDCCursorRec));
191     if (!pPriv)
192         return (rfbDCCursorPtr)NULL;
193     pPriv->sourceBits = (*pScreen->CreatePixmap) (pScreen, pCursor->bits->width, pCursor->bits->height, 1);
194     if (!pPriv->sourceBits)
195     {
196         xfree ((pointer) pPriv);
197         return (rfbDCCursorPtr)NULL;
198     }
199     pPriv->maskBits =  (*pScreen->CreatePixmap) (pScreen, pCursor->bits->width, pCursor->bits->height, 1);
200     if (!pPriv->maskBits)
201     {
202         (*pScreen->DestroyPixmap) (pPriv->sourceBits);
203         xfree ((pointer) pPriv);
204         return (rfbDCCursorPtr)NULL;
205     }
206     pCursor->bits->devPriv[pScreen->myNum] = (pointer) pPriv;
207
208     /* create the two sets of bits, clipping as appropriate */
209
210     pGC = GetScratchGC (1, pScreen);
211     if (!pGC)
212     {
213         (void) rfbDCUnrealizeCursor (pScreen, pCursor);
214         return (rfbDCCursorPtr)NULL;
215     }
216
217     ValidateGC ((DrawablePtr)pPriv->sourceBits, pGC);
218     (*pGC->ops->PutImage) ((DrawablePtr)pPriv->sourceBits, pGC, 1,
219                            0, 0, pCursor->bits->width, pCursor->bits->height,
220                            0, XYPixmap, (char *)pCursor->bits->source);
221     gcvals[0] = GXand;
222     ChangeGC (pGC, GCFunction, gcvals);
223     ValidateGC ((DrawablePtr)pPriv->sourceBits, pGC);
224     (*pGC->ops->PutImage) ((DrawablePtr)pPriv->sourceBits, pGC, 1,
225                            0, 0, pCursor->bits->width, pCursor->bits->height,
226                            0, XYPixmap, (char *)pCursor->bits->mask);
227
228     /* mask bits -- pCursor->mask & ~pCursor->source */
229     gcvals[0] = GXcopy;
230     ChangeGC (pGC, GCFunction, gcvals);
231     ValidateGC ((DrawablePtr)pPriv->maskBits, pGC);
232     (*pGC->ops->PutImage) ((DrawablePtr)pPriv->maskBits, pGC, 1,
233                            0, 0, pCursor->bits->width, pCursor->bits->height,
234                            0, XYPixmap, (char *)pCursor->bits->mask);
235     gcvals[0] = GXandInverted;
236     ChangeGC (pGC, GCFunction, gcvals);
237     ValidateGC ((DrawablePtr)pPriv->maskBits, pGC);
238     (*pGC->ops->PutImage) ((DrawablePtr)pPriv->maskBits, pGC, 1,
239                            0, 0, pCursor->bits->width, pCursor->bits->height,
240                            0, XYPixmap, (char *)pCursor->bits->source);
241     FreeScratchGC (pGC);
242     return pPriv;
243 }
244
245 static Bool
246 rfbDCUnrealizeCursor (pScreen, pCursor)
247     ScreenPtr   pScreen;
248     CursorPtr   pCursor;
249 {
250     rfbDCCursorPtr   pPriv;
251
252     pPriv = (rfbDCCursorPtr) pCursor->bits->devPriv[pScreen->myNum];
253     if (pPriv && (pCursor->bits->refcnt <= 1))
254     {
255         (*pScreen->DestroyPixmap) (pPriv->sourceBits);
256         (*pScreen->DestroyPixmap) (pPriv->maskBits);
257         xfree ((pointer) pPriv);
258         pCursor->bits->devPriv[pScreen->myNum] = (pointer)NULL;
259     }
260     return TRUE;
261 }
262
263 static void
264 rfbDCPutBits (pDrawable, pPriv, sourceGC, maskGC, x, y, w, h, source, mask)
265     DrawablePtr     pDrawable;
266     GCPtr           sourceGC, maskGC;
267     int             x, y;
268     unsigned        w, h;
269     rfbDCCursorPtr   pPriv;
270     unsigned long   source, mask;
271 {
272     XID     gcvals[1];
273
274     if (sourceGC->fgPixel != source)
275     {
276         gcvals[0] = source;
277         DoChangeGC (sourceGC, GCForeground, gcvals, 0);
278     }
279     if (sourceGC->serialNumber != pDrawable->serialNumber)
280         ValidateGC (pDrawable, sourceGC);
281     (*sourceGC->ops->PushPixels) (sourceGC, pPriv->sourceBits, pDrawable, w, h, x, y);
282     if (maskGC->fgPixel != mask)
283     {
284         gcvals[0] = mask;
285         DoChangeGC (maskGC, GCForeground, gcvals, 0);
286     }
287     if (maskGC->serialNumber != pDrawable->serialNumber)
288         ValidateGC (pDrawable, maskGC);
289     (*maskGC->ops->PushPixels) (maskGC, pPriv->maskBits, pDrawable, w, h, x, y);
290 }
291
292 #define EnsureGC(gc,win) (gc || rfbDCMakeGC(&gc, win))
293
294 static GCPtr
295 rfbDCMakeGC(ppGC, pWin)
296     GCPtr       *ppGC;
297     WindowPtr   pWin;
298 {
299     GCPtr pGC;
300     int   status;
301     XID   gcvals[2];
302
303     gcvals[0] = IncludeInferiors;
304     gcvals[1] = FALSE;
305     pGC = CreateGC((DrawablePtr)pWin,
306                    GCSubwindowMode|GCGraphicsExposures, gcvals, &status);
307     if (pGC)
308         (*pWin->drawable.pScreen->DrawGuarantee) (pWin, pGC, GuaranteeVisBack);
309     *ppGC = pGC;
310     return pGC;
311 }
312
313 static Bool
314 rfbDCPutUpCursor (pScreen, pCursor, x, y, source, mask)
315     ScreenPtr       pScreen;
316     CursorPtr       pCursor;
317     int             x, y;
318     unsigned long   source, mask;
319 {
320     rfbDCScreenPtr   pScreenPriv;
321     rfbDCCursorPtr   pPriv;
322     WindowPtr       pWin;
323
324     pPriv = (rfbDCCursorPtr) pCursor->bits->devPriv[pScreen->myNum];
325     if (!pPriv)
326     {
327         pPriv = rfbDCRealize(pScreen, pCursor);
328         if (!pPriv)
329             return FALSE;
330     }
331     pScreenPriv = (rfbDCScreenPtr) pScreen->devPrivates[rfbDCScreenIndex].ptr;
332     pWin = WindowTable[pScreen->myNum];
333     if (!EnsureGC(pScreenPriv->pSourceGC, pWin))
334         return FALSE;
335     if (!EnsureGC(pScreenPriv->pMaskGC, pWin))
336     {
337         FreeGC (pScreenPriv->pSourceGC, (GContext) 0);
338         pScreenPriv->pSourceGC = 0;
339         return FALSE;
340     }
341     rfbDCPutBits ((DrawablePtr)pWin, pPriv,
342                  pScreenPriv->pSourceGC, pScreenPriv->pMaskGC,
343                  x, y, pCursor->bits->width, pCursor->bits->height,
344                  source, mask);
345     return TRUE;
346 }
347
348 static Bool
349 rfbDCSaveUnderCursor (pScreen, x, y, w, h)
350     ScreenPtr   pScreen;
351     int         x, y, w, h;
352 {
353     rfbDCScreenPtr   pScreenPriv;
354     PixmapPtr       pSave;
355     WindowPtr       pWin;
356     GCPtr           pGC;
357
358     pScreenPriv = (rfbDCScreenPtr) pScreen->devPrivates[rfbDCScreenIndex].ptr;
359     pSave = pScreenPriv->pSave;
360     pWin = WindowTable[pScreen->myNum];
361     if (!pSave || pSave->drawable.width < w || pSave->drawable.height < h)
362     {
363         if (pSave)
364             (*pScreen->DestroyPixmap) (pSave);
365         pScreenPriv->pSave = pSave =
366                 (*pScreen->CreatePixmap) (pScreen, w, h, pScreen->rootDepth);
367         if (!pSave)
368             return FALSE;
369     }
370     if (!EnsureGC(pScreenPriv->pSaveGC, pWin))
371         return FALSE;
372     pGC = pScreenPriv->pSaveGC;
373     if (pSave->drawable.serialNumber != pGC->serialNumber)
374         ValidateGC ((DrawablePtr) pSave, pGC);
375     (*pGC->ops->CopyArea) ((DrawablePtr) pWin, (DrawablePtr) pSave, pGC,
376                             x, y, w, h, 0, 0);
377     return TRUE;
378 }
379
380 static Bool
381 rfbDCRestoreUnderCursor (pScreen, x, y, w, h)
382     ScreenPtr   pScreen;
383     int         x, y, w, h;
384 {
385     rfbDCScreenPtr   pScreenPriv;
386     PixmapPtr       pSave;
387     WindowPtr       pWin;
388     GCPtr           pGC;
389
390     pScreenPriv = (rfbDCScreenPtr) pScreen->devPrivates[rfbDCScreenIndex].ptr;
391     pSave = pScreenPriv->pSave;
392     pWin = WindowTable[pScreen->myNum];
393     if (!pSave)
394         return FALSE;
395     if (!EnsureGC(pScreenPriv->pRestoreGC, pWin))
396         return FALSE;
397     pGC = pScreenPriv->pRestoreGC;
398     if (pWin->drawable.serialNumber != pGC->serialNumber)
399         ValidateGC ((DrawablePtr) pWin, pGC);
400     (*pGC->ops->CopyArea) ((DrawablePtr) pSave, (DrawablePtr) pWin, pGC,
401                             0, 0, w, h, x, y);
402     return TRUE;
403 }