1 /*
2  * Common internal rootless definitions and code
3  *
4  * Greg Parker     gparker@cs.stanford.edu
5  */
6 
7 /* Copyright (c) 2002 Apple Computer, Inc. All rights reserved.
8 
9    Permission is hereby granted, free of charge, to any person
10    obtaining a copy of this software and associated documentation files
11    (the "Software"), to deal in the Software without restriction,
12    including without limitation the rights to use, copy, modify, merge,
13    publish, distribute, sublicense, and/or sell copies of the Software,
14    and to permit persons to whom the Software is furnished to do so,
15    subject to the following conditions:
16 
17    The above copyright notice and this permission notice shall be
18    included in all copies or substantial portions of the Software.
19 
20    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23    NONINFRINGEMENT.  IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT
24    HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26    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
30    copyright holders shall not be used in advertising or otherwise to
31    promote the sale, use or other dealings in this Software without
32    prior written authorization. */
33 
34 /* $XFree86: xc/programs/Xserver/hw/darwin/quartz/rootlessCommon.h,v 1.6 2002/07/24 05:58:33 torrey Exp $ */
35 
36 #ifndef _ROOTLESSCOMMON_H
37 #define _ROOTLESSCOMMON_H
38 
39 #include "rootless.h"
40 
41 #include "pixmapstr.h"
42 #include "windowstr.h"
43 
44 #ifdef RENDER
45 #include "picturestr.h"
46 #endif
47 
48 /* Debug output, or not. */
49 #ifdef ROOTLESSDEBUG
50 #define RL_DEBUG_MSG ErrorF
51 #else
52 #define RL_DEBUG_MSG(a, ...)
53 #endif
54 
55 #undef MIN
56 #define MIN(x,y) ((x) < (y) ? (x) : (y))
57 #undef MAX
58 #define MAX(x,y) ((x) > (y) ? (x) : (y))
59 
60 /* Global variables */
61 extern int rootlessGCPrivateIndex;
62 extern int rootlessScreenPrivateIndex;
63 extern int rootlessWindowPrivateIndex;
64 extern int rootlessNoDRIDrawing;
65 
66 /* RootlessGCRec: private per-gc data */
67 typedef struct {
68     GCFuncs *originalFuncs;
69     GCOps *originalOps;
70 } RootlessGCRec;
71 
72 /* RootlessWindowRec: private per-window data */
73 typedef struct RootlessWindowRec {
74     int x, y;
75     unsigned int width, height;
76     unsigned int borderWidth;
77     int level;
78 
79     xp_window_id wid;
80     WindowPtr win;
81 
82     /* Valid when locked (i.e. is_drawing is set) */
83     void *data;
84     unsigned int rowbytes;
85 
86     PixmapPtr pixmap;
87     PixmapPtr oldPixmap;
88 
89     unsigned long unrealize_time;	/* in seconds */
90 
91     unsigned int is_drawing :1;
92     unsigned int is_update_disabled :1;
93     unsigned int is_reorder_pending :1;
94     unsigned int is_offscreen :1;
95     unsigned int is_obscured :1;
96 } RootlessWindowRec;
97 
98 /* RootlessScreenRec: per-screen private data */
99 typedef struct {
100     ScreenPtr pScreen;
101 
102     CreateScreenResourcesProcPtr CreateScreenResources;
103     CloseScreenProcPtr CloseScreen;
104 
105     CreateWindowProcPtr CreateWindow;
106     DestroyWindowProcPtr DestroyWindow;
107     RealizeWindowProcPtr RealizeWindow;
108     UnrealizeWindowProcPtr UnrealizeWindow;
109     ReparentWindowProcPtr ReparentWindow;
110     MoveWindowProcPtr MoveWindow;
111     ResizeWindowProcPtr ResizeWindow;
112     RestackWindowProcPtr RestackWindow;
113     ChangeBorderWidthProcPtr ChangeBorderWidth;
114     PositionWindowProcPtr PositionWindow;
115     ChangeWindowAttributesProcPtr ChangeWindowAttributes;
116 
117     CreateGCProcPtr CreateGC;
118     PaintWindowBackgroundProcPtr PaintWindowBackground;
119     PaintWindowBorderProcPtr PaintWindowBorder;
120     CopyWindowProcPtr CopyWindow;
121     GetImageProcPtr GetImage;
122     SourceValidateProcPtr SourceValidate;
123 
124     MarkOverlappedWindowsProcPtr MarkOverlappedWindows;
125     ValidateTreeProcPtr ValidateTree;
126 
127 #ifdef SHAPE
128     SetShapeProcPtr SetShape;
129 #endif
130 
131 #ifdef RENDER
132     CompositeProcPtr Composite;
133     GlyphsProcPtr Glyphs;
134 #endif
135 
136     InstallColormapProcPtr InstallColormap;
137     UninstallColormapProcPtr UninstallColormap;
138     StoreColorsProcPtr StoreColors;
139 
140     void *pixmap_data;
141     unsigned int pixmap_data_size;
142 
143     ColormapPtr colormap;
144 
145     void *redisplay_timer;
146     CARD32 last_redisplay;
147 
148     unsigned int redisplay_timer_set :1;
149     unsigned int redisplay_queued :1;
150     unsigned int redisplay_expired :1;
151     unsigned int colormap_changed :1;
152 } RootlessScreenRec;
153 
154 
155 /* "Definition of the Porting Layer for the X11 Sample Server" says
156    unwrap and rewrap of screen functions is unnecessary, but
157    screen->CreateGC changes after a call to cfbCreateGC. */
158 
159 #define SCREEN_UNWRAP(screen, fn) \
160     screen->fn = SCREENREC(screen)->fn;
161 
162 #define SCREEN_WRAP(screen, fn) \
163     SCREENREC(screen)->fn = screen->fn; \
164     screen->fn = Rootless##fn
165 
166 /* Accessors for screen and window privates */
167 
168 #define SCREENREC(pScreen) \
169    ((RootlessScreenRec*)(pScreen)->devPrivates[rootlessScreenPrivateIndex].ptr)
170 
171 #define WINREC(pWin) \
172     ((RootlessWindowRec *)(pWin)->devPrivates[rootlessWindowPrivateIndex].ptr)
173 
174 /* BoxRec manipulators (Copied from shadowfb) */
175 
176 #define TRIM_BOX(box, pGC) { \
177     BoxPtr extents = &pGC->pCompositeClip->extents;\
178     if(box.x1 < extents->x1) box.x1 = extents->x1; \
179     if(box.x2 > extents->x2) box.x2 = extents->x2; \
180     if(box.y1 < extents->y1) box.y1 = extents->y1; \
181     if(box.y2 > extents->y2) box.y2 = extents->y2; \
182 }
183 
184 #define TRANSLATE_BOX(box, pDraw) { \
185     box.x1 += pDraw->x; \
186     box.x2 += pDraw->x; \
187     box.y1 += pDraw->y; \
188     box.y2 += pDraw->y; \
189 }
190 
191 #define TRIM_AND_TRANSLATE_BOX(box, pDraw, pGC) { \
192     TRANSLATE_BOX(box, pDraw); \
193     TRIM_BOX(box, pGC); \
194 }
195 
196 #define BOX_NOT_EMPTY(box) \
197     (((box.x2 - box.x1) > 0) && ((box.y2 - box.y1) > 0))
198 
199 /* We don't want to clip windows to the edge of the screen. HUGE_ROOT
200    temporarily makes the root window really big. This is needed as a
201    wrapper around any function that calls SetWinSize or SetBorderSize
202    which clip a window against its parents, including the root. */
203 
204 extern RegionRec rootlessHugeRoot;
205 
206 #define HUGE_ROOT(pWin) 			\
207     do { 					\
208 	WindowPtr w = pWin; 			\
209 	while (w->parent != NULL)		\
210 	    w = w->parent; 			\
211         saveRoot = w->winSize; 			\
212         w->winSize = rootlessHugeRoot; 		\
213     } while (0)
214 
215 #define NORMAL_ROOT(pWin) 		\
216     do { 				\
217 	WindowPtr w = pWin; 		\
218 	while (w->parent != NULL)	\
219 	    w = w->parent;		\
220 	w->winSize = saveRoot; 		\
221     } while (0)
222 
223 /* Returns TRUE if this window is a top-level window (i.e. child of the root)
224    The root is not a top-level window. */
225 #define IsTopLevel(pWin) \
226    ((pWin) != NULL && (pWin)->parent != NULL && (pWin)->parent->parent == NULL)
227 
228 /* Returns TRUE if this window is a root window */
229 #define IsRoot(pWin) \
230    ((pWin) == WindowTable[(pWin)->drawable.pScreen->myNum])
231 
232 /* Returns the top-level parent of pWindow.
233    The root is the top-level parent of itself, even though the root is
234    not otherwise considered to be a top-level window. */
235 extern WindowPtr TopLevelParent (WindowPtr pWindow);
236 
237 /* Returns TRUE if this window is visible inside a frame (e.g. it is
238    visible and has a top-level or root parent) */
239 extern Bool IsFramedWindow (WindowPtr pWin);
240 
241 /* Adjust base address of pixmap by DX,DY */
242 extern void TranslatePixmapBase (PixmapPtr pPix, int dx, int dy);
243 
244 /* Prepare a window for direct access to its backing buffer. */
245 extern void RootlessStartDrawing (WindowPtr pWindow);
246 
247 /* Mark that no more drawing operations will hit the window until ``start
248    drawing'' is issued again. */
249 extern void RootlessFinishedDrawing (WindowPtr pWindow);
250 
251 /* Finish drawing to a window's backing buffer. */
252 extern void RootlessStopDrawing (WindowPtr pWindow, Bool flush);
253 
254 /* Routines that cause regions to get redrawn.
255    DamageRegion and DamageRect are in global coordinates.
256    DamageBox is in window-local coordinates. */
257 extern void RootlessDamageRegion (WindowPtr pWindow, RegionPtr pRegion);
258 extern void RootlessDamageRect (WindowPtr pWindow, int x, int y, int w, int h);
259 extern void RootlessDamageBox (WindowPtr pWindow, BoxPtr pBox);
260 extern void RootlessRedisplay (WindowPtr pWindow);
261 extern void RootlessRedisplayScreen (ScreenPtr pScreen);
262 
263 extern void RootlessQueueRedisplay (ScreenPtr pScreen);
264 extern Bool RootlessMayRedisplay (ScreenPtr pScreen);
265 extern void RootlessScreenExpose (ScreenPtr pScreen);
266 
267 /* Return the colormap currently installed on the given screen. */
268 extern ColormapPtr RootlessGetColormap (ScreenPtr pScreen);
269 
270 /* Convert colormap to ARGB. */
271 extern Bool RootlessResolveColormap (ScreenPtr pScreen, int first_color,
272 				     int n_colors, uint32_t *colors);
273 
274 extern void RootlessFlushWindowColormap (WindowPtr pWin);
275 extern void RootlessFlushScreenColormaps (ScreenPtr pScreen);
276 
277 /* Move windows back to their position relative to the screen origin. */
278 extern void RootlessRepositionWindow (WindowPtr pWin);
279 extern void RootlessRepositionWindows (ScreenPtr pScreen);
280 
281 /* Move the window to it's correct place in the physical stacking order */
282 extern void RootlessReorderWindow (WindowPtr pWin);
283 
284 /* Bit mask for alpha channel with a particular number of bits per
285    pixel. Note that we only care for 32bpp data. OS X uses planar alpha
286    for 16bpp. */
287 #define RootlessAlphaMask(bpp) ((bpp) == 32 ? 0xFF000000 : 0)
288 
289 #ifdef RENDER
290 extern void RootlessComposite(CARD8 op, PicturePtr pSrc,
291 			      PicturePtr pMask, PicturePtr pDst,
292 			      INT16 xSrc, INT16 ySrc,
293 			      INT16 xMask, INT16 yMask,
294 			      INT16 xDst, INT16 yDst,
295 			      CARD16 width, CARD16 height);
296 #endif
297 
298 extern void RootlessNativeWindowStateChanged (xp_window_id id,
299 					      unsigned int state);
300 
301 extern void RootlessEnableRoot (ScreenPtr pScreen);
302 extern void RootlessDisableRoot (ScreenPtr pScreen);
303 extern void RootlessSetWindowLevel (WindowPtr pWin, int level);
304 
305 #endif /* _ROOTLESSCOMMON_H */
306