xref: /OK3568_Linux_fs/external/xserver/hw/dmx/dmxextension.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * All Rights Reserved.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining
7*4882a593Smuzhiyun  * a copy of this software and associated documentation files (the
8*4882a593Smuzhiyun  * "Software"), to deal in the Software without restriction, including
9*4882a593Smuzhiyun  * without limitation on the rights to use, copy, modify, merge,
10*4882a593Smuzhiyun  * publish, distribute, sublicense, and/or sell copies of the Software,
11*4882a593Smuzhiyun  * and to permit persons to whom the Software is furnished to do so,
12*4882a593Smuzhiyun  * subject to the following conditions:
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  * The above copyright notice and this permission notice (including the
15*4882a593Smuzhiyun  * next paragraph) shall be included in all copies or substantial
16*4882a593Smuzhiyun  * portions of the Software.
17*4882a593Smuzhiyun  *
18*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21*4882a593Smuzhiyun  * NON-INFRINGEMENT.  IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25*4882a593Smuzhiyun  * SOFTWARE.
26*4882a593Smuzhiyun  */
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun  * Author:
30*4882a593Smuzhiyun  *   Rickard E. (Rik) Faith <faith@redhat.com>
31*4882a593Smuzhiyun  *   Kevin E. Martin <kem@redhat.com>
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /** \file
36*4882a593Smuzhiyun  * This file provides the only interface to the X server extension support
37*4882a593Smuzhiyun  * in programs/Xserver/Xext.  Those programs should only include dmxext.h
38*4882a593Smuzhiyun  */
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun #ifdef HAVE_DMX_CONFIG_H
41*4882a593Smuzhiyun #include <dmx-config.h>
42*4882a593Smuzhiyun #endif
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #include <stdlib.h>
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #include "dmx.h"
47*4882a593Smuzhiyun #include "dmxinit.h"
48*4882a593Smuzhiyun #include "dmxextension.h"
49*4882a593Smuzhiyun #include "dmxwindow.h"
50*4882a593Smuzhiyun #include "dmxcb.h"
51*4882a593Smuzhiyun #include "dmxcursor.h"
52*4882a593Smuzhiyun #include "dmxpixmap.h"
53*4882a593Smuzhiyun #include "dmxgc.h"
54*4882a593Smuzhiyun #include "dmxfont.h"
55*4882a593Smuzhiyun #include "dmxcmap.h"
56*4882a593Smuzhiyun #include "dmxpict.h"
57*4882a593Smuzhiyun #include "dmxinput.h"
58*4882a593Smuzhiyun #include "dmxsync.h"
59*4882a593Smuzhiyun #include "dmxscrinit.h"
60*4882a593Smuzhiyun #include "input/dmxinputinit.h"
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun #include "windowstr.h"
63*4882a593Smuzhiyun #include "inputstr.h"           /* For DeviceIntRec */
64*4882a593Smuzhiyun #include <X11/extensions/dmxproto.h>    /* For DMX_BAD_* */
65*4882a593Smuzhiyun #include "cursorstr.h"
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun /* The default font is declared in dix/globals.c, but is not included in
68*4882a593Smuzhiyun  * _any_ header files. */
69*4882a593Smuzhiyun extern FontPtr defaultFont;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun /* Hack to get Present to build (present requires RandR) */
72*4882a593Smuzhiyun RESTYPE RRCrtcType;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun /** This routine provides information to the DMX protocol extension
75*4882a593Smuzhiyun  * about a particular screen. */
76*4882a593Smuzhiyun Bool
dmxGetScreenAttributes(int physical,DMXScreenAttributesPtr attr)77*4882a593Smuzhiyun dmxGetScreenAttributes(int physical, DMXScreenAttributesPtr attr)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun     if (physical < 0 || physical >= dmxNumScreens)
82*4882a593Smuzhiyun         return FALSE;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun     dmxScreen = &dmxScreens[physical];
85*4882a593Smuzhiyun     attr->displayName = dmxScreen->name;
86*4882a593Smuzhiyun #ifdef PANORAMIX
87*4882a593Smuzhiyun     attr->logicalScreen = noPanoramiXExtension ? dmxScreen->index : 0;
88*4882a593Smuzhiyun #else
89*4882a593Smuzhiyun     attr->logicalScreen = dmxScreen->index;
90*4882a593Smuzhiyun #endif
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun     attr->screenWindowWidth = dmxScreen->scrnWidth;
93*4882a593Smuzhiyun     attr->screenWindowHeight = dmxScreen->scrnHeight;
94*4882a593Smuzhiyun     attr->screenWindowXoffset = dmxScreen->scrnX;
95*4882a593Smuzhiyun     attr->screenWindowYoffset = dmxScreen->scrnY;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun     attr->rootWindowWidth = dmxScreen->rootWidth;
98*4882a593Smuzhiyun     attr->rootWindowHeight = dmxScreen->rootHeight;
99*4882a593Smuzhiyun     attr->rootWindowXoffset = dmxScreen->rootX;
100*4882a593Smuzhiyun     attr->rootWindowYoffset = dmxScreen->rootY;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun     attr->rootWindowXorigin = dmxScreen->rootXOrigin;
103*4882a593Smuzhiyun     attr->rootWindowYorigin = dmxScreen->rootYOrigin;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun     return TRUE;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun /** This routine provides information to the DMX protocol extension
109*4882a593Smuzhiyun  * about a particular window. */
110*4882a593Smuzhiyun Bool
dmxGetWindowAttributes(WindowPtr pWindow,DMXWindowAttributesPtr attr)111*4882a593Smuzhiyun dmxGetWindowAttributes(WindowPtr pWindow, DMXWindowAttributesPtr attr)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun     dmxWinPrivPtr pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun     attr->screen = pWindow->drawable.pScreen->myNum;
116*4882a593Smuzhiyun     attr->window = pWinPriv->window;
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun     attr->pos.x = pWindow->drawable.x;
119*4882a593Smuzhiyun     attr->pos.y = pWindow->drawable.y;
120*4882a593Smuzhiyun     attr->pos.width = pWindow->drawable.width;
121*4882a593Smuzhiyun     attr->pos.height = pWindow->drawable.height;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun     if (!pWinPriv->window || pWinPriv->offscreen) {
124*4882a593Smuzhiyun         attr->vis.x = 0;
125*4882a593Smuzhiyun         attr->vis.y = 0;
126*4882a593Smuzhiyun         attr->vis.height = 0;
127*4882a593Smuzhiyun         attr->vis.width = 0;
128*4882a593Smuzhiyun         return pWinPriv->window ? TRUE : FALSE;
129*4882a593Smuzhiyun     }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun     /* Compute display-relative coordinates */
132*4882a593Smuzhiyun     attr->vis.x = pWindow->drawable.x;
133*4882a593Smuzhiyun     attr->vis.y = pWindow->drawable.y;
134*4882a593Smuzhiyun     attr->vis.width = pWindow->drawable.width;
135*4882a593Smuzhiyun     attr->vis.height = pWindow->drawable.height;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun     if (attr->pos.x < 0) {
138*4882a593Smuzhiyun         attr->vis.x -= attr->pos.x;
139*4882a593Smuzhiyun         attr->vis.width = attr->pos.x + attr->pos.width - attr->vis.x;
140*4882a593Smuzhiyun     }
141*4882a593Smuzhiyun     if (attr->pos.x + attr->pos.width > pWindow->drawable.pScreen->width) {
142*4882a593Smuzhiyun         if (attr->pos.x < 0)
143*4882a593Smuzhiyun             attr->vis.width = pWindow->drawable.pScreen->width;
144*4882a593Smuzhiyun         else
145*4882a593Smuzhiyun             attr->vis.width = pWindow->drawable.pScreen->width - attr->pos.x;
146*4882a593Smuzhiyun     }
147*4882a593Smuzhiyun     if (attr->pos.y < 0) {
148*4882a593Smuzhiyun         attr->vis.y -= attr->pos.y;
149*4882a593Smuzhiyun         attr->vis.height = attr->pos.y + attr->pos.height - attr->vis.y;
150*4882a593Smuzhiyun     }
151*4882a593Smuzhiyun     if (attr->pos.y + attr->pos.height > pWindow->drawable.pScreen->height) {
152*4882a593Smuzhiyun         if (attr->pos.y < 0)
153*4882a593Smuzhiyun             attr->vis.height = pWindow->drawable.pScreen->height;
154*4882a593Smuzhiyun         else
155*4882a593Smuzhiyun             attr->vis.height = pWindow->drawable.pScreen->height - attr->pos.y;
156*4882a593Smuzhiyun     }
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun     /* Convert to window-relative coordinates */
159*4882a593Smuzhiyun     attr->vis.x -= attr->pos.x;
160*4882a593Smuzhiyun     attr->vis.y -= attr->pos.y;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun     return TRUE;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun void
dmxGetDesktopAttributes(DMXDesktopAttributesPtr attr)166*4882a593Smuzhiyun dmxGetDesktopAttributes(DMXDesktopAttributesPtr attr)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun     attr->width = dmxGlobalWidth;
169*4882a593Smuzhiyun     attr->height = dmxGlobalHeight;
170*4882a593Smuzhiyun     attr->shiftX = 0;           /* NOTE: The upper left hand corner of */
171*4882a593Smuzhiyun     attr->shiftY = 0;           /*       the desktop is always <0,0>. */
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun /** Return the total number of devices, not just #dmxNumInputs.  The
175*4882a593Smuzhiyun  * number returned should be the same as that returned by
176*4882a593Smuzhiyun  * XListInputDevices. */
177*4882a593Smuzhiyun int
dmxGetInputCount(void)178*4882a593Smuzhiyun dmxGetInputCount(void)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun     int i, total;
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun     for (total = i = 0; i < dmxNumInputs; i++)
183*4882a593Smuzhiyun         total += dmxInputs[i].numDevs;
184*4882a593Smuzhiyun     return total;
185*4882a593Smuzhiyun }
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun /** Return information about the device with id = \a deviceId.  This
188*4882a593Smuzhiyun  * information is primarily for the #ProcDMXGetInputAttributes()
189*4882a593Smuzhiyun  * function, which does not have access to the appropriate data
190*4882a593Smuzhiyun  * structure. */
191*4882a593Smuzhiyun int
dmxGetInputAttributes(int deviceId,DMXInputAttributesPtr attr)192*4882a593Smuzhiyun dmxGetInputAttributes(int deviceId, DMXInputAttributesPtr attr)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun     int i, j;
195*4882a593Smuzhiyun     DMXInputInfo *dmxInput;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun     if (deviceId < 0)
198*4882a593Smuzhiyun         return -1;
199*4882a593Smuzhiyun     for (i = 0; i < dmxNumInputs; i++) {
200*4882a593Smuzhiyun         dmxInput = &dmxInputs[i];
201*4882a593Smuzhiyun         for (j = 0; j < dmxInput->numDevs; j++) {
202*4882a593Smuzhiyun             DMXLocalInputInfoPtr dmxLocal = dmxInput->devs[j];
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun             if (deviceId != dmxLocal->pDevice->id)
205*4882a593Smuzhiyun                 continue;
206*4882a593Smuzhiyun             attr->isCore = ! !dmxLocal->isCore;
207*4882a593Smuzhiyun             attr->sendsCore = ! !dmxLocal->sendsCore;
208*4882a593Smuzhiyun             attr->detached = ! !dmxInput->detached;
209*4882a593Smuzhiyun             attr->physicalScreen = -1;
210*4882a593Smuzhiyun             attr->physicalId = -1;
211*4882a593Smuzhiyun             attr->name = NULL;
212*4882a593Smuzhiyun             switch (dmxLocal->extType) {
213*4882a593Smuzhiyun             case DMX_LOCAL_TYPE_LOCAL:
214*4882a593Smuzhiyun                 attr->inputType = 0;
215*4882a593Smuzhiyun                 break;
216*4882a593Smuzhiyun             case DMX_LOCAL_TYPE_CONSOLE:
217*4882a593Smuzhiyun                 attr->inputType = 1;
218*4882a593Smuzhiyun                 attr->name = dmxInput->name;
219*4882a593Smuzhiyun                 attr->physicalId = dmxLocal->deviceId;
220*4882a593Smuzhiyun                 break;
221*4882a593Smuzhiyun             case DMX_LOCAL_TYPE_BACKEND:
222*4882a593Smuzhiyun             case DMX_LOCAL_TYPE_COMMON:
223*4882a593Smuzhiyun                 attr->inputType = 2;
224*4882a593Smuzhiyun                 attr->physicalScreen = dmxInput->scrnIdx;
225*4882a593Smuzhiyun                 attr->name = dmxInput->name;
226*4882a593Smuzhiyun                 attr->physicalId = dmxLocal->deviceId;
227*4882a593Smuzhiyun                 break;
228*4882a593Smuzhiyun             }
229*4882a593Smuzhiyun             return 0;           /* Success */
230*4882a593Smuzhiyun         }
231*4882a593Smuzhiyun     }
232*4882a593Smuzhiyun     return -1;                  /* Failure */
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun /** Reinitialized the cursor boundaries. */
236*4882a593Smuzhiyun static void
dmxAdjustCursorBoundaries(void)237*4882a593Smuzhiyun dmxAdjustCursorBoundaries(void)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun     int i;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun     dmxReInitOrigins();
242*4882a593Smuzhiyun     dmxInitOverlap();
243*4882a593Smuzhiyun     dmxComputeWidthHeight(DMX_NO_RECOMPUTE_BOUNDING_BOX);
244*4882a593Smuzhiyun     dmxConnectionBlockCallback();
245*4882a593Smuzhiyun     for (i = 0; i < dmxNumInputs; i++) {
246*4882a593Smuzhiyun         DMXInputInfo *dmxInput = &dmxInputs[i];
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun         if (!dmxInput->detached)
249*4882a593Smuzhiyun             dmxInputReInit(dmxInput);
250*4882a593Smuzhiyun     }
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun     dmxCheckCursor();
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun     for (i = 0; i < dmxNumInputs; i++) {
255*4882a593Smuzhiyun         DMXInputInfo *dmxInput = &dmxInputs[i];
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun         if (!dmxInput->detached)
258*4882a593Smuzhiyun             dmxInputLateReInit(dmxInput);
259*4882a593Smuzhiyun     }
260*4882a593Smuzhiyun }
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun /** Add an input with the specified attributes.  If the input is added,
263*4882a593Smuzhiyun  * the physical id is returned in \a deviceId. */
264*4882a593Smuzhiyun int
dmxAddInput(DMXInputAttributesPtr attr,int * id)265*4882a593Smuzhiyun dmxAddInput(DMXInputAttributesPtr attr, int *id)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun     int retcode = BadValue;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun     if (attr->inputType == 1)   /* console */
270*4882a593Smuzhiyun         retcode = dmxInputAttachConsole(attr->name, attr->sendsCore, id);
271*4882a593Smuzhiyun     else if (attr->inputType == 2)      /* backend */
272*4882a593Smuzhiyun         retcode = dmxInputAttachBackend(attr->physicalScreen,
273*4882a593Smuzhiyun                                         attr->sendsCore, id);
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun     if (retcode == Success) {
276*4882a593Smuzhiyun         /* Adjust the cursor boundaries */
277*4882a593Smuzhiyun         dmxAdjustCursorBoundaries();
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun         /* Force completion of the changes */
280*4882a593Smuzhiyun         dmxSync(NULL, TRUE);
281*4882a593Smuzhiyun     }
282*4882a593Smuzhiyun 
283*4882a593Smuzhiyun     return retcode;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun /** Remove the input with physical id \a id. */
287*4882a593Smuzhiyun int
dmxRemoveInput(int id)288*4882a593Smuzhiyun dmxRemoveInput(int id)
289*4882a593Smuzhiyun {
290*4882a593Smuzhiyun     return dmxInputDetachId(id);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun /** Return the value of #dmxNumScreens -- the total number of backend
294*4882a593Smuzhiyun  * screens in use (these are logical screens and may be larger than the
295*4882a593Smuzhiyun  * number of backend displays). */
296*4882a593Smuzhiyun unsigned long
dmxGetNumScreens(void)297*4882a593Smuzhiyun dmxGetNumScreens(void)
298*4882a593Smuzhiyun {
299*4882a593Smuzhiyun     return dmxNumScreens;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun /** Make sure that #dmxCreateAndRealizeWindow has been called for \a
303*4882a593Smuzhiyun  * pWindow. */
304*4882a593Smuzhiyun void
dmxForceWindowCreation(WindowPtr pWindow)305*4882a593Smuzhiyun dmxForceWindowCreation(WindowPtr pWindow)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun     dmxWinPrivPtr pWinPriv = DMX_GET_WINDOW_PRIV(pWindow);
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun     if (!pWinPriv->window)
310*4882a593Smuzhiyun         dmxCreateAndRealizeWindow(pWindow, TRUE);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun /** Flush pending syncs for all screens. */
314*4882a593Smuzhiyun void
dmxFlushPendingSyncs(void)315*4882a593Smuzhiyun dmxFlushPendingSyncs(void)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun     dmxSync(NULL, TRUE);
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun /** Update DMX's screen resources to match those of the newly moved
321*4882a593Smuzhiyun  *  and/or resized "root" window. */
322*4882a593Smuzhiyun void
dmxUpdateScreenResources(ScreenPtr pScreen,int x,int y,int w,int h)323*4882a593Smuzhiyun dmxUpdateScreenResources(ScreenPtr pScreen, int x, int y, int w, int h)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
326*4882a593Smuzhiyun     WindowPtr pRoot = pScreen->root;
327*4882a593Smuzhiyun     WindowPtr pChild;
328*4882a593Smuzhiyun     Bool anyMarked = FALSE;
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun     /* Handle special case where width and/or height are zero */
331*4882a593Smuzhiyun     if (w == 0 || h == 0) {
332*4882a593Smuzhiyun         w = 1;
333*4882a593Smuzhiyun         h = 1;
334*4882a593Smuzhiyun     }
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun     /* Change screen size */
337*4882a593Smuzhiyun     pScreen->width = w;
338*4882a593Smuzhiyun     pScreen->height = h;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun     /* Reset the root window's drawable's size */
341*4882a593Smuzhiyun     pRoot->drawable.width = w;
342*4882a593Smuzhiyun     pRoot->drawable.height = h;
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun     /* Set the root window's new winSize and borderSize */
345*4882a593Smuzhiyun     pRoot->winSize.extents.x1 = 0;
346*4882a593Smuzhiyun     pRoot->winSize.extents.y1 = 0;
347*4882a593Smuzhiyun     pRoot->winSize.extents.x2 = w;
348*4882a593Smuzhiyun     pRoot->winSize.extents.y2 = h;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun     pRoot->borderSize.extents.x1 = 0;
351*4882a593Smuzhiyun     pRoot->borderSize.extents.y1 = 0;
352*4882a593Smuzhiyun     pRoot->borderSize.extents.x2 = w;
353*4882a593Smuzhiyun     pRoot->borderSize.extents.y2 = h;
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun     /* Recompute this screen's mmWidth & mmHeight */
356*4882a593Smuzhiyun     pScreen->mmWidth =
357*4882a593Smuzhiyun         (w * 254 + dmxScreen->beXDPI * 5) / (dmxScreen->beXDPI * 10);
358*4882a593Smuzhiyun     pScreen->mmHeight =
359*4882a593Smuzhiyun         (h * 254 + dmxScreen->beYDPI * 5) / (dmxScreen->beYDPI * 10);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun     /* Recompute this screen's window's clip rects as follows: */
362*4882a593Smuzhiyun     /*   1. Mark all of root's children's windows */
363*4882a593Smuzhiyun     for (pChild = pRoot->firstChild; pChild; pChild = pChild->nextSib)
364*4882a593Smuzhiyun         anyMarked |= pScreen->MarkOverlappedWindows(pChild, pChild,
365*4882a593Smuzhiyun                                                     (WindowPtr *) NULL);
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun     /*   2. Set the root window's borderClip */
368*4882a593Smuzhiyun     pRoot->borderClip.extents.x1 = 0;
369*4882a593Smuzhiyun     pRoot->borderClip.extents.y1 = 0;
370*4882a593Smuzhiyun     pRoot->borderClip.extents.x2 = w;
371*4882a593Smuzhiyun     pRoot->borderClip.extents.y2 = h;
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun     /*   3. Set the root window's clipList */
374*4882a593Smuzhiyun     if (anyMarked) {
375*4882a593Smuzhiyun         /* If any windows have been marked, set the root window's
376*4882a593Smuzhiyun          * clipList to be broken since it will be recalculated in
377*4882a593Smuzhiyun          * ValidateTree()
378*4882a593Smuzhiyun          */
379*4882a593Smuzhiyun         RegionBreak(&pRoot->clipList);
380*4882a593Smuzhiyun     }
381*4882a593Smuzhiyun     else {
382*4882a593Smuzhiyun         /* Otherwise, we just set it directly since there are no
383*4882a593Smuzhiyun          * windows visible on this screen
384*4882a593Smuzhiyun          */
385*4882a593Smuzhiyun         pRoot->clipList.extents.x1 = 0;
386*4882a593Smuzhiyun         pRoot->clipList.extents.y1 = 0;
387*4882a593Smuzhiyun         pRoot->clipList.extents.x2 = w;
388*4882a593Smuzhiyun         pRoot->clipList.extents.y2 = h;
389*4882a593Smuzhiyun     }
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun     /*   4. Revalidate all clip rects and generate expose events */
392*4882a593Smuzhiyun     if (anyMarked) {
393*4882a593Smuzhiyun         pScreen->ValidateTree(pRoot, NULL, VTBroken);
394*4882a593Smuzhiyun         pScreen->HandleExposures(pRoot);
395*4882a593Smuzhiyun         if (pScreen->PostValidateTree)
396*4882a593Smuzhiyun             pScreen->PostValidateTree(pRoot, NULL, VTBroken);
397*4882a593Smuzhiyun     }
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun #ifdef PANORAMIX
401*4882a593Smuzhiyun #include "panoramiXsrv.h"
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun /** Change the "screen" window attributes by resizing the actual window
404*4882a593Smuzhiyun  *  on the back-end display (if necessary). */
405*4882a593Smuzhiyun static void
dmxConfigureScreenWindow(int idx,int x,int y,int w,int h)406*4882a593Smuzhiyun dmxConfigureScreenWindow(int idx, int x, int y, int w, int h)
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen = &dmxScreens[idx];
409*4882a593Smuzhiyun     ScreenPtr pScreen = screenInfo.screens[idx];
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun     /* Resize "screen" window */
412*4882a593Smuzhiyun     if (dmxScreen->scrnX != x ||
413*4882a593Smuzhiyun         dmxScreen->scrnY != y ||
414*4882a593Smuzhiyun         dmxScreen->scrnWidth != w || dmxScreen->scrnHeight != h) {
415*4882a593Smuzhiyun         dmxResizeScreenWindow(pScreen, x, y, w, h);
416*4882a593Smuzhiyun     }
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun     /* Change "screen" window values */
419*4882a593Smuzhiyun     dmxScreen->scrnX = x;
420*4882a593Smuzhiyun     dmxScreen->scrnY = y;
421*4882a593Smuzhiyun     dmxScreen->scrnWidth = w;
422*4882a593Smuzhiyun     dmxScreen->scrnHeight = h;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun /** Change the "root" window position and size by resizing the actual
426*4882a593Smuzhiyun  *  window on the back-end display (if necessary) and updating all of
427*4882a593Smuzhiyun  *  DMX's resources by calling #dmxUpdateScreenResources. */
428*4882a593Smuzhiyun static void
dmxConfigureRootWindow(int idx,int x,int y,int w,int h)429*4882a593Smuzhiyun dmxConfigureRootWindow(int idx, int x, int y, int w, int h)
430*4882a593Smuzhiyun {
431*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen = &dmxScreens[idx];
432*4882a593Smuzhiyun     WindowPtr pRoot = screenInfo.screens[idx]->root;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun     /* NOTE: Either this function or the ones that it calls must handle
435*4882a593Smuzhiyun      * the case where w == 0 || h == 0.  Currently, the functions that
436*4882a593Smuzhiyun      * this one calls handle that case. */
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun     /* 1. Resize "root" window */
439*4882a593Smuzhiyun     if (dmxScreen->rootX != x ||
440*4882a593Smuzhiyun         dmxScreen->rootY != y ||
441*4882a593Smuzhiyun         dmxScreen->rootWidth != w || dmxScreen->rootHeight != h) {
442*4882a593Smuzhiyun         dmxResizeRootWindow(pRoot, x, y, w, h);
443*4882a593Smuzhiyun     }
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun     /* 2. Update all of the screen's resources associated with this root
446*4882a593Smuzhiyun      *    window */
447*4882a593Smuzhiyun     if (dmxScreen->rootWidth != w || dmxScreen->rootHeight != h) {
448*4882a593Smuzhiyun         dmxUpdateScreenResources(screenInfo.screens[idx], x, y, w, h);
449*4882a593Smuzhiyun     }
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun     /* Change "root" window values */
452*4882a593Smuzhiyun     dmxScreen->rootX = x;
453*4882a593Smuzhiyun     dmxScreen->rootY = y;
454*4882a593Smuzhiyun     dmxScreen->rootWidth = w;
455*4882a593Smuzhiyun     dmxScreen->rootHeight = h;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun /** Change the "root" window's origin by updating DMX's internal data
459*4882a593Smuzhiyun  *  structures (dix and Xinerama) to use the new origin and adjust the
460*4882a593Smuzhiyun  *  positions of windows that overlap this "root" window. */
461*4882a593Smuzhiyun static void
dmxSetRootWindowOrigin(int idx,int x,int y)462*4882a593Smuzhiyun dmxSetRootWindowOrigin(int idx, int x, int y)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen = &dmxScreens[idx];
465*4882a593Smuzhiyun     ScreenPtr pScreen = screenInfo.screens[idx];
466*4882a593Smuzhiyun     WindowPtr pRoot = pScreen->root;
467*4882a593Smuzhiyun     WindowPtr pChild;
468*4882a593Smuzhiyun     int xoff;
469*4882a593Smuzhiyun     int yoff;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun     /* Change "root" window's origin */
472*4882a593Smuzhiyun     dmxScreen->rootXOrigin = x;
473*4882a593Smuzhiyun     dmxScreen->rootYOrigin = y;
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun     /* Compute offsets here in case <x,y> has been changed above */
476*4882a593Smuzhiyun     xoff = x - pScreen->x;
477*4882a593Smuzhiyun     yoff = y - pScreen->y;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun     /* Adjust the root window's position */
480*4882a593Smuzhiyun     pScreen->x = dmxScreen->rootXOrigin;
481*4882a593Smuzhiyun     pScreen->y = dmxScreen->rootYOrigin;
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun     /* Recalculate the Xinerama regions and data structs */
484*4882a593Smuzhiyun     XineramaReinitData();
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun     /* Adjust each of the root window's children */
487*4882a593Smuzhiyun     if (!idx)
488*4882a593Smuzhiyun         ReinitializeRootWindow(screenInfo.screens[0]->root, xoff, yoff);
489*4882a593Smuzhiyun     pChild = pRoot->firstChild;
490*4882a593Smuzhiyun     while (pChild) {
491*4882a593Smuzhiyun         /* Adjust child window's position */
492*4882a593Smuzhiyun         pScreen->MoveWindow(pChild,
493*4882a593Smuzhiyun                             pChild->origin.x - wBorderWidth(pChild) - xoff,
494*4882a593Smuzhiyun                             pChild->origin.y - wBorderWidth(pChild) - yoff,
495*4882a593Smuzhiyun                             pChild->nextSib, VTMove);
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun         /* Note that the call to MoveWindow will eventually call
498*4882a593Smuzhiyun          * dmxPositionWindow which will automatically create a
499*4882a593Smuzhiyun          * window if it is now exposed on screen (for lazy window
500*4882a593Smuzhiyun          * creation optimization) and it will properly set the
501*4882a593Smuzhiyun          * offscreen flag.
502*4882a593Smuzhiyun          */
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun         pChild = pChild->nextSib;
505*4882a593Smuzhiyun     }
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun /** Configure the attributes of each "screen" and "root" window. */
509*4882a593Smuzhiyun int
dmxConfigureScreenWindows(int nscreens,CARD32 * screens,DMXScreenAttributesPtr attribs,int * errorScreen)510*4882a593Smuzhiyun dmxConfigureScreenWindows(int nscreens,
511*4882a593Smuzhiyun                           CARD32 *screens,
512*4882a593Smuzhiyun                           DMXScreenAttributesPtr attribs, int *errorScreen)
513*4882a593Smuzhiyun {
514*4882a593Smuzhiyun     int i;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun     for (i = 0; i < nscreens; i++) {
517*4882a593Smuzhiyun         DMXScreenAttributesPtr attr = &attribs[i];
518*4882a593Smuzhiyun         int idx = screens[i];
519*4882a593Smuzhiyun         DMXScreenInfo *dmxScreen = &dmxScreens[idx];
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun         if (errorScreen)
522*4882a593Smuzhiyun             *errorScreen = i;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun         if (!dmxScreen->beDisplay)
525*4882a593Smuzhiyun             return DMX_BAD_VALUE;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun         /* Check for illegal values */
528*4882a593Smuzhiyun         if (idx < 0 || idx >= dmxNumScreens)
529*4882a593Smuzhiyun             return BadValue;
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun         /* The "screen" and "root" windows must have valid sizes */
532*4882a593Smuzhiyun         if (attr->screenWindowWidth <= 0 || attr->screenWindowHeight <= 0 ||
533*4882a593Smuzhiyun             attr->rootWindowWidth < 0 || attr->rootWindowHeight < 0)
534*4882a593Smuzhiyun             return DMX_BAD_VALUE;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun         /* The "screen" window must fit entirely within the BE display */
537*4882a593Smuzhiyun         if (attr->screenWindowXoffset < 0 ||
538*4882a593Smuzhiyun             attr->screenWindowYoffset < 0 ||
539*4882a593Smuzhiyun             attr->screenWindowXoffset
540*4882a593Smuzhiyun             + attr->screenWindowWidth > (unsigned) dmxScreen->beWidth ||
541*4882a593Smuzhiyun             attr->screenWindowYoffset
542*4882a593Smuzhiyun             + attr->screenWindowHeight > (unsigned) dmxScreen->beHeight)
543*4882a593Smuzhiyun             return DMX_BAD_VALUE;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun         /* The "root" window must fit entirely within the "screen" window */
546*4882a593Smuzhiyun         if (attr->rootWindowXoffset < 0 ||
547*4882a593Smuzhiyun             attr->rootWindowYoffset < 0 ||
548*4882a593Smuzhiyun             attr->rootWindowXoffset
549*4882a593Smuzhiyun             + attr->rootWindowWidth > attr->screenWindowWidth ||
550*4882a593Smuzhiyun             attr->rootWindowYoffset
551*4882a593Smuzhiyun             + attr->rootWindowHeight > attr->screenWindowHeight)
552*4882a593Smuzhiyun             return DMX_BAD_VALUE;
553*4882a593Smuzhiyun 
554*4882a593Smuzhiyun         /* The "root" window must not expose unaddressable coordinates */
555*4882a593Smuzhiyun         if (attr->rootWindowXorigin < 0 ||
556*4882a593Smuzhiyun             attr->rootWindowYorigin < 0 ||
557*4882a593Smuzhiyun             attr->rootWindowXorigin + attr->rootWindowWidth > 32767 ||
558*4882a593Smuzhiyun             attr->rootWindowYorigin + attr->rootWindowHeight > 32767)
559*4882a593Smuzhiyun             return DMX_BAD_VALUE;
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun         /* The "root" window must fit within the global bounding box */
562*4882a593Smuzhiyun         if (attr->rootWindowXorigin
563*4882a593Smuzhiyun             + attr->rootWindowWidth > (unsigned) dmxGlobalWidth ||
564*4882a593Smuzhiyun             attr->rootWindowYorigin
565*4882a593Smuzhiyun             + attr->rootWindowHeight > (unsigned) dmxGlobalHeight)
566*4882a593Smuzhiyun             return DMX_BAD_VALUE;
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun         /* FIXME: Handle the rest of the illegal value checking */
569*4882a593Smuzhiyun     }
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun     /* No illegal values found */
572*4882a593Smuzhiyun     if (errorScreen)
573*4882a593Smuzhiyun         *errorScreen = 0;
574*4882a593Smuzhiyun 
575*4882a593Smuzhiyun     for (i = 0; i < nscreens; i++) {
576*4882a593Smuzhiyun         DMXScreenAttributesPtr attr = &attribs[i];
577*4882a593Smuzhiyun         int idx = screens[i];
578*4882a593Smuzhiyun         DMXScreenInfo *dmxScreen = &dmxScreens[idx];
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun         dmxLog(dmxInfo, "Changing screen #%d attributes "
581*4882a593Smuzhiyun                "from %dx%d+%d+%d %dx%d+%d+%d +%d+%d "
582*4882a593Smuzhiyun                "to %dx%d+%d+%d %dx%d+%d+%d +%d+%d\n",
583*4882a593Smuzhiyun                idx,
584*4882a593Smuzhiyun                dmxScreen->scrnWidth, dmxScreen->scrnHeight,
585*4882a593Smuzhiyun                dmxScreen->scrnX, dmxScreen->scrnY,
586*4882a593Smuzhiyun                dmxScreen->rootWidth, dmxScreen->rootHeight,
587*4882a593Smuzhiyun                dmxScreen->rootX, dmxScreen->rootY,
588*4882a593Smuzhiyun                dmxScreen->rootXOrigin, dmxScreen->rootYOrigin,
589*4882a593Smuzhiyun                attr->screenWindowWidth, attr->screenWindowHeight,
590*4882a593Smuzhiyun                attr->screenWindowXoffset, attr->screenWindowYoffset,
591*4882a593Smuzhiyun                attr->rootWindowWidth, attr->rootWindowHeight,
592*4882a593Smuzhiyun                attr->rootWindowXoffset, attr->rootWindowYoffset,
593*4882a593Smuzhiyun                attr->rootWindowXorigin, attr->rootWindowYorigin);
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun         /* Configure "screen" window */
596*4882a593Smuzhiyun         dmxConfigureScreenWindow(idx,
597*4882a593Smuzhiyun                                  attr->screenWindowXoffset,
598*4882a593Smuzhiyun                                  attr->screenWindowYoffset,
599*4882a593Smuzhiyun                                  attr->screenWindowWidth,
600*4882a593Smuzhiyun                                  attr->screenWindowHeight);
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun         /* Configure "root" window */
603*4882a593Smuzhiyun         dmxConfigureRootWindow(idx,
604*4882a593Smuzhiyun                                attr->rootWindowXoffset,
605*4882a593Smuzhiyun                                attr->rootWindowYoffset,
606*4882a593Smuzhiyun                                attr->rootWindowWidth, attr->rootWindowHeight);
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun         /* Set "root" window's origin */
609*4882a593Smuzhiyun         dmxSetRootWindowOrigin(idx,
610*4882a593Smuzhiyun                                attr->rootWindowXorigin,
611*4882a593Smuzhiyun                                attr->rootWindowYorigin);
612*4882a593Smuzhiyun     }
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun     /* Adjust the cursor boundaries */
615*4882a593Smuzhiyun     dmxAdjustCursorBoundaries();
616*4882a593Smuzhiyun 
617*4882a593Smuzhiyun     /* Force completion of the changes */
618*4882a593Smuzhiyun     dmxSync(NULL, TRUE);
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun     return Success;
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun 
623*4882a593Smuzhiyun /** Configure the attributes of the global desktop. */
624*4882a593Smuzhiyun int
dmxConfigureDesktop(DMXDesktopAttributesPtr attribs)625*4882a593Smuzhiyun dmxConfigureDesktop(DMXDesktopAttributesPtr attribs)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun     if (attribs->width <= 0 || attribs->width >= 32767 ||
628*4882a593Smuzhiyun         attribs->height <= 0 || attribs->height >= 32767)
629*4882a593Smuzhiyun         return DMX_BAD_VALUE;
630*4882a593Smuzhiyun 
631*4882a593Smuzhiyun     /* If the desktop is shrinking, adjust the "root" windows on each
632*4882a593Smuzhiyun      * "screen" window to only show the visible desktop.  Also, handle
633*4882a593Smuzhiyun      * the special case where the desktop shrinks such that the it no
634*4882a593Smuzhiyun      * longer overlaps an portion of a "screen" window. */
635*4882a593Smuzhiyun     if (attribs->width < dmxGlobalWidth || attribs->height < dmxGlobalHeight) {
636*4882a593Smuzhiyun         int i;
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun         for (i = 0; i < dmxNumScreens; i++) {
639*4882a593Smuzhiyun             DMXScreenInfo *dmxScreen = &dmxScreens[i];
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun             if (dmxScreen->rootXOrigin
642*4882a593Smuzhiyun                 + dmxScreen->rootWidth > attribs->width ||
643*4882a593Smuzhiyun                 dmxScreen->rootYOrigin
644*4882a593Smuzhiyun                 + dmxScreen->rootHeight > attribs->height) {
645*4882a593Smuzhiyun                 int w, h;
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun                 if ((w = attribs->width - dmxScreen->rootXOrigin) < 0)
648*4882a593Smuzhiyun                     w = 0;
649*4882a593Smuzhiyun                 if ((h = attribs->height - dmxScreen->rootYOrigin) < 0)
650*4882a593Smuzhiyun                     h = 0;
651*4882a593Smuzhiyun                 if (w > dmxScreen->scrnWidth)
652*4882a593Smuzhiyun                     w = dmxScreen->scrnWidth;
653*4882a593Smuzhiyun                 if (h > dmxScreen->scrnHeight)
654*4882a593Smuzhiyun                     h = dmxScreen->scrnHeight;
655*4882a593Smuzhiyun                 if (w > dmxScreen->rootWidth)
656*4882a593Smuzhiyun                     w = dmxScreen->rootWidth;
657*4882a593Smuzhiyun                 if (h > dmxScreen->rootHeight)
658*4882a593Smuzhiyun                     h = dmxScreen->rootHeight;
659*4882a593Smuzhiyun                 dmxConfigureRootWindow(i,
660*4882a593Smuzhiyun                                        dmxScreen->rootX,
661*4882a593Smuzhiyun                                        dmxScreen->rootY, w, h);
662*4882a593Smuzhiyun             }
663*4882a593Smuzhiyun         }
664*4882a593Smuzhiyun     }
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun     /* Set the global width/height */
667*4882a593Smuzhiyun     dmxSetWidthHeight(attribs->width, attribs->height);
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun     /* Handle shift[XY] changes */
670*4882a593Smuzhiyun     if (attribs->shiftX || attribs->shiftY) {
671*4882a593Smuzhiyun         int i;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun         for (i = 0; i < dmxNumScreens; i++) {
674*4882a593Smuzhiyun             ScreenPtr pScreen = screenInfo.screens[i];
675*4882a593Smuzhiyun             WindowPtr pChild = pScreen->root->firstChild;
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun             while (pChild) {
678*4882a593Smuzhiyun                 /* Adjust child window's position */
679*4882a593Smuzhiyun                 pScreen->MoveWindow(pChild,
680*4882a593Smuzhiyun                                     pChild->origin.x - wBorderWidth(pChild)
681*4882a593Smuzhiyun                                     - attribs->shiftX,
682*4882a593Smuzhiyun                                     pChild->origin.y - wBorderWidth(pChild)
683*4882a593Smuzhiyun                                     - attribs->shiftY, pChild->nextSib, VTMove);
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun                 /* Note that the call to MoveWindow will eventually call
686*4882a593Smuzhiyun                  * dmxPositionWindow which will automatically create a
687*4882a593Smuzhiyun                  * window if it is now exposed on screen (for lazy
688*4882a593Smuzhiyun                  * window creation optimization) and it will properly
689*4882a593Smuzhiyun                  * set the offscreen flag.
690*4882a593Smuzhiyun                  */
691*4882a593Smuzhiyun 
692*4882a593Smuzhiyun                 pChild = pChild->nextSib;
693*4882a593Smuzhiyun             }
694*4882a593Smuzhiyun         }
695*4882a593Smuzhiyun     }
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun     /* Update connection block, Xinerama, etc. -- these appears to
698*4882a593Smuzhiyun      * already be handled in dmxConnectionBlockCallback(), which is
699*4882a593Smuzhiyun      * called from dmxAdjustCursorBoundaries() [below]. */
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun     /* Adjust the cursor boundaries */
702*4882a593Smuzhiyun     dmxAdjustCursorBoundaries();
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun     /* Force completion of the changes */
705*4882a593Smuzhiyun     dmxSync(NULL, TRUE);
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun     return Success;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun #endif
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun /** Create the scratch GCs per depth. */
712*4882a593Smuzhiyun static void
dmxBECreateScratchGCs(int scrnNum)713*4882a593Smuzhiyun dmxBECreateScratchGCs(int scrnNum)
714*4882a593Smuzhiyun {
715*4882a593Smuzhiyun     ScreenPtr pScreen = screenInfo.screens[scrnNum];
716*4882a593Smuzhiyun     GCPtr *ppGC = pScreen->GCperDepth;
717*4882a593Smuzhiyun     int i;
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun     for (i = 0; i <= pScreen->numDepths; i++)
720*4882a593Smuzhiyun         dmxBECreateGC(pScreen, ppGC[i]);
721*4882a593Smuzhiyun }
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun #ifdef PANORAMIX
724*4882a593Smuzhiyun static Bool FoundPixImage;
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun /** Search the Xinerama XRT_PIXMAP resources for the pixmap that needs
727*4882a593Smuzhiyun  *  to have its image restored.  When it is found, see if there is
728*4882a593Smuzhiyun  *  another screen with the same image.  If so, copy the pixmap image
729*4882a593Smuzhiyun  *  from the existing screen to the newly created pixmap. */
730*4882a593Smuzhiyun static void
dmxBERestorePixmapImage(void * value,XID id,RESTYPE type,void * p)731*4882a593Smuzhiyun dmxBERestorePixmapImage(void *value, XID id, RESTYPE type, void *p)
732*4882a593Smuzhiyun {
733*4882a593Smuzhiyun     if ((type & TypeMask) == (XRT_PIXMAP & TypeMask)) {
734*4882a593Smuzhiyun         PixmapPtr pDst = (PixmapPtr) p;
735*4882a593Smuzhiyun         int idx = pDst->drawable.pScreen->myNum;
736*4882a593Smuzhiyun         PanoramiXRes *pXinPix = (PanoramiXRes *) value;
737*4882a593Smuzhiyun         PixmapPtr pPix;
738*4882a593Smuzhiyun         int i;
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun         dixLookupResourceByType((void **) &pPix, pXinPix->info[idx].id,
741*4882a593Smuzhiyun                                 RT_PIXMAP, NullClient, DixUnknownAccess);
742*4882a593Smuzhiyun         if (pPix != pDst)
743*4882a593Smuzhiyun             return;             /* Not a match.... Next! */
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun         FOR_NSCREENS(i) {
746*4882a593Smuzhiyun             PixmapPtr pSrc;
747*4882a593Smuzhiyun             dmxPixPrivPtr pSrcPriv = NULL;
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun             if (i == idx)
750*4882a593Smuzhiyun                 continue;       /* Self replication is bad */
751*4882a593Smuzhiyun 
752*4882a593Smuzhiyun             dixLookupResourceByType((void **) &pSrc, pXinPix->info[i].id,
753*4882a593Smuzhiyun                                     RT_PIXMAP, NullClient, DixUnknownAccess);
754*4882a593Smuzhiyun             pSrcPriv = DMX_GET_PIXMAP_PRIV(pSrc);
755*4882a593Smuzhiyun             if (pSrcPriv->pixmap) {
756*4882a593Smuzhiyun                 DMXScreenInfo *dmxSrcScreen = &dmxScreens[i];
757*4882a593Smuzhiyun                 DMXScreenInfo *dmxDstScreen = &dmxScreens[idx];
758*4882a593Smuzhiyun                 dmxPixPrivPtr pDstPriv = DMX_GET_PIXMAP_PRIV(pDst);
759*4882a593Smuzhiyun                 XImage *img;
760*4882a593Smuzhiyun                 int j;
761*4882a593Smuzhiyun                 XlibGC gc = NULL;
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun                 /* This should never happen, but just in case.... */
764*4882a593Smuzhiyun                 if (pSrc->drawable.width != pDst->drawable.width ||
765*4882a593Smuzhiyun                     pSrc->drawable.height != pDst->drawable.height)
766*4882a593Smuzhiyun                     return;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun                 /* Copy from src pixmap to dst pixmap */
769*4882a593Smuzhiyun                 img = XGetImage(dmxSrcScreen->beDisplay,
770*4882a593Smuzhiyun                                 pSrcPriv->pixmap,
771*4882a593Smuzhiyun                                 0, 0,
772*4882a593Smuzhiyun                                 pSrc->drawable.width, pSrc->drawable.height,
773*4882a593Smuzhiyun                                 -1, ZPixmap);
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun                 for (j = 0; j < dmxDstScreen->beNumPixmapFormats; j++) {
776*4882a593Smuzhiyun                     if (dmxDstScreen->bePixmapFormats[j].depth == img->depth) {
777*4882a593Smuzhiyun                         unsigned long m;
778*4882a593Smuzhiyun                         XGCValues v;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun                         m = GCFunction | GCPlaneMask | GCClipMask;
781*4882a593Smuzhiyun                         v.function = GXcopy;
782*4882a593Smuzhiyun                         v.plane_mask = AllPlanes;
783*4882a593Smuzhiyun                         v.clip_mask = None;
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun                         gc = XCreateGC(dmxDstScreen->beDisplay,
786*4882a593Smuzhiyun                                        dmxDstScreen->scrnDefDrawables[j],
787*4882a593Smuzhiyun                                        m, &v);
788*4882a593Smuzhiyun                         break;
789*4882a593Smuzhiyun                     }
790*4882a593Smuzhiyun                 }
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun                 if (gc) {
793*4882a593Smuzhiyun                     XPutImage(dmxDstScreen->beDisplay,
794*4882a593Smuzhiyun                               pDstPriv->pixmap,
795*4882a593Smuzhiyun                               gc, img, 0, 0, 0, 0,
796*4882a593Smuzhiyun                               pDst->drawable.width, pDst->drawable.height);
797*4882a593Smuzhiyun                     XFreeGC(dmxDstScreen->beDisplay, gc);
798*4882a593Smuzhiyun                     FoundPixImage = True;
799*4882a593Smuzhiyun                 }
800*4882a593Smuzhiyun                 else {
801*4882a593Smuzhiyun                     dmxLog(dmxWarning, "Could not create GC\n");
802*4882a593Smuzhiyun                 }
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun                 XDestroyImage(img);
805*4882a593Smuzhiyun                 return;
806*4882a593Smuzhiyun             }
807*4882a593Smuzhiyun         }
808*4882a593Smuzhiyun     }
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun #endif
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun /** Restore the pixmap image either from another screen or from an image
813*4882a593Smuzhiyun  *  that was saved when the screen was previously detached. */
814*4882a593Smuzhiyun static void
dmxBERestorePixmap(PixmapPtr pPixmap)815*4882a593Smuzhiyun dmxBERestorePixmap(PixmapPtr pPixmap)
816*4882a593Smuzhiyun {
817*4882a593Smuzhiyun #ifdef PANORAMIX
818*4882a593Smuzhiyun     int i;
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun     /* If Xinerama is not active, there's nothing we can do (see comment
821*4882a593Smuzhiyun      * in #else below for more info). */
822*4882a593Smuzhiyun     if (noPanoramiXExtension) {
823*4882a593Smuzhiyun         dmxLog(dmxWarning, "Cannot restore pixmap image\n");
824*4882a593Smuzhiyun         return;
825*4882a593Smuzhiyun     }
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun     FoundPixImage = False;
828*4882a593Smuzhiyun     for (i = currentMaxClients; --i >= 0;)
829*4882a593Smuzhiyun         if (clients[i])
830*4882a593Smuzhiyun             FindAllClientResources(clients[i], dmxBERestorePixmapImage,
831*4882a593Smuzhiyun                                    (void *) pPixmap);
832*4882a593Smuzhiyun 
833*4882a593Smuzhiyun     /* No corresponding pixmap image was found on other screens, so we
834*4882a593Smuzhiyun      * need to copy it from the saved image when the screen was detached
835*4882a593Smuzhiyun      * (if available). */
836*4882a593Smuzhiyun     if (!FoundPixImage) {
837*4882a593Smuzhiyun         dmxPixPrivPtr pPixPriv = DMX_GET_PIXMAP_PRIV(pPixmap);
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun         if (pPixPriv->detachedImage) {
840*4882a593Smuzhiyun             ScreenPtr pScreen = pPixmap->drawable.pScreen;
841*4882a593Smuzhiyun             DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
842*4882a593Smuzhiyun             XlibGC gc = NULL;
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun             for (i = 0; i < dmxScreen->beNumPixmapFormats; i++) {
845*4882a593Smuzhiyun                 if (dmxScreen->bePixmapFormats[i].depth ==
846*4882a593Smuzhiyun                     pPixPriv->detachedImage->depth) {
847*4882a593Smuzhiyun                     unsigned long m;
848*4882a593Smuzhiyun                     XGCValues v;
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun                     m = GCFunction | GCPlaneMask | GCClipMask;
851*4882a593Smuzhiyun                     v.function = GXcopy;
852*4882a593Smuzhiyun                     v.plane_mask = AllPlanes;
853*4882a593Smuzhiyun                     v.clip_mask = None;
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun                     gc = XCreateGC(dmxScreen->beDisplay,
856*4882a593Smuzhiyun                                    dmxScreen->scrnDefDrawables[i], m, &v);
857*4882a593Smuzhiyun                     break;
858*4882a593Smuzhiyun                 }
859*4882a593Smuzhiyun             }
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun             if (gc) {
862*4882a593Smuzhiyun                 XPutImage(dmxScreen->beDisplay,
863*4882a593Smuzhiyun                           pPixPriv->pixmap,
864*4882a593Smuzhiyun                           gc,
865*4882a593Smuzhiyun                           pPixPriv->detachedImage,
866*4882a593Smuzhiyun                           0, 0, 0, 0,
867*4882a593Smuzhiyun                           pPixmap->drawable.width, pPixmap->drawable.height);
868*4882a593Smuzhiyun                 XFreeGC(dmxScreen->beDisplay, gc);
869*4882a593Smuzhiyun             }
870*4882a593Smuzhiyun             else {
871*4882a593Smuzhiyun                 dmxLog(dmxWarning, "Cannot restore pixmap image\n");
872*4882a593Smuzhiyun             }
873*4882a593Smuzhiyun 
874*4882a593Smuzhiyun             XDestroyImage(pPixPriv->detachedImage);
875*4882a593Smuzhiyun             pPixPriv->detachedImage = NULL;
876*4882a593Smuzhiyun         }
877*4882a593Smuzhiyun         else {
878*4882a593Smuzhiyun             dmxLog(dmxWarning, "Cannot restore pixmap image\n");
879*4882a593Smuzhiyun         }
880*4882a593Smuzhiyun     }
881*4882a593Smuzhiyun #else
882*4882a593Smuzhiyun     /* If Xinerama is not enabled, then there is no other copy of the
883*4882a593Smuzhiyun      * pixmap image that we can restore.  Saving all pixmap data is not
884*4882a593Smuzhiyun      * a feasible option since there is no mechanism for updating pixmap
885*4882a593Smuzhiyun      * data when a screen is detached, which means that the data that
886*4882a593Smuzhiyun      * was previously saved would most likely be out of date. */
887*4882a593Smuzhiyun     dmxLog(dmxWarning, "Cannot restore pixmap image\n");
888*4882a593Smuzhiyun     return;
889*4882a593Smuzhiyun #endif
890*4882a593Smuzhiyun }
891*4882a593Smuzhiyun 
892*4882a593Smuzhiyun /** Create resources on the back-end server.  This function is called
893*4882a593Smuzhiyun  *  from #dmxAttachScreen() via the dix layer's FindAllResources
894*4882a593Smuzhiyun  *  function.  It walks all resources, compares them to the screen
895*4882a593Smuzhiyun  *  number passed in as \a n and calls the appropriate DMX function to
896*4882a593Smuzhiyun  *  create the associated resource on the back-end server. */
897*4882a593Smuzhiyun static void
dmxBECreateResources(void * value,XID id,RESTYPE type,void * n)898*4882a593Smuzhiyun dmxBECreateResources(void *value, XID id, RESTYPE type, void *n)
899*4882a593Smuzhiyun {
900*4882a593Smuzhiyun     int scrnNum = (uintptr_t) n;
901*4882a593Smuzhiyun     ScreenPtr pScreen = screenInfo.screens[scrnNum];
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun     if ((type & TypeMask) == (RT_WINDOW & TypeMask)) {
904*4882a593Smuzhiyun         /* Window resources are created below in dmxBECreateWindowTree */
905*4882a593Smuzhiyun     }
906*4882a593Smuzhiyun     else if ((type & TypeMask) == (RT_PIXMAP & TypeMask)) {
907*4882a593Smuzhiyun         PixmapPtr pPix = value;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun         if (pPix->drawable.pScreen->myNum == scrnNum) {
910*4882a593Smuzhiyun             dmxBECreatePixmap(pPix);
911*4882a593Smuzhiyun             dmxBERestorePixmap(pPix);
912*4882a593Smuzhiyun         }
913*4882a593Smuzhiyun     }
914*4882a593Smuzhiyun     else if ((type & TypeMask) == (RT_GC & TypeMask)) {
915*4882a593Smuzhiyun         GCPtr pGC = value;
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun         if (pGC->pScreen->myNum == scrnNum) {
918*4882a593Smuzhiyun             /* Create the GC on the back-end server */
919*4882a593Smuzhiyun             dmxBECreateGC(pScreen, pGC);
920*4882a593Smuzhiyun             /* Create any pixmaps associated with this GC */
921*4882a593Smuzhiyun             if (!pGC->tileIsPixel) {
922*4882a593Smuzhiyun                 dmxBECreatePixmap(pGC->tile.pixmap);
923*4882a593Smuzhiyun                 dmxBERestorePixmap(pGC->tile.pixmap);
924*4882a593Smuzhiyun             }
925*4882a593Smuzhiyun             if (pGC->stipple != pScreen->defaultStipple) {
926*4882a593Smuzhiyun                 dmxBECreatePixmap(pGC->stipple);
927*4882a593Smuzhiyun                 dmxBERestorePixmap(pGC->stipple);
928*4882a593Smuzhiyun             }
929*4882a593Smuzhiyun             if (pGC->font != defaultFont) {
930*4882a593Smuzhiyun                 (void) dmxBELoadFont(pScreen, pGC->font);
931*4882a593Smuzhiyun             }
932*4882a593Smuzhiyun             /* Update the GC on the back-end server */
933*4882a593Smuzhiyun             dmxChangeGC(pGC, -1L);
934*4882a593Smuzhiyun         }
935*4882a593Smuzhiyun     }
936*4882a593Smuzhiyun     else if ((type & TypeMask) == (RT_FONT & TypeMask)) {
937*4882a593Smuzhiyun         (void) dmxBELoadFont(pScreen, (FontPtr) value);
938*4882a593Smuzhiyun     }
939*4882a593Smuzhiyun     else if ((type & TypeMask) == (RT_CURSOR & TypeMask)) {
940*4882a593Smuzhiyun         dmxBECreateCursor(pScreen, (CursorPtr) value);
941*4882a593Smuzhiyun     }
942*4882a593Smuzhiyun     else if ((type & TypeMask) == (RT_COLORMAP & TypeMask)) {
943*4882a593Smuzhiyun         ColormapPtr pCmap = value;
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun         if (pCmap->pScreen->myNum == scrnNum)
946*4882a593Smuzhiyun             (void) dmxBECreateColormap((ColormapPtr) value);
947*4882a593Smuzhiyun #if 0
948*4882a593Smuzhiyun         /* TODO: Recreate Picture and GlyphSet resources */
949*4882a593Smuzhiyun     }
950*4882a593Smuzhiyun     else if ((type & TypeMask) == (PictureType & TypeMask)) {
951*4882a593Smuzhiyun         /* Picture resources are created when windows are created */
952*4882a593Smuzhiyun     }
953*4882a593Smuzhiyun     else if ((type & TypeMask) == (GlyphSetType & TypeMask)) {
954*4882a593Smuzhiyun         dmxBEFreeGlyphSet(pScreen, (GlyphSetPtr) value);
955*4882a593Smuzhiyun #endif
956*4882a593Smuzhiyun     }
957*4882a593Smuzhiyun     else {
958*4882a593Smuzhiyun         /* Other resource types??? */
959*4882a593Smuzhiyun     }
960*4882a593Smuzhiyun }
961*4882a593Smuzhiyun 
962*4882a593Smuzhiyun /** Create window hierachy on back-end server.  The window tree is
963*4882a593Smuzhiyun  *  created in a special order (bottom most subwindow first) so that the
964*4882a593Smuzhiyun  *  #dmxCreateNonRootWindow() function does not need to recursively call
965*4882a593Smuzhiyun  *  itself to create each window's parents.  This is required so that we
966*4882a593Smuzhiyun  *  have the opportunity to create each window's border and background
967*4882a593Smuzhiyun  *  pixmaps (where appropriate) before the window is created. */
968*4882a593Smuzhiyun static void
dmxBECreateWindowTree(int idx)969*4882a593Smuzhiyun dmxBECreateWindowTree(int idx)
970*4882a593Smuzhiyun {
971*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen = &dmxScreens[idx];
972*4882a593Smuzhiyun     WindowPtr pRoot = screenInfo.screens[idx]->root;
973*4882a593Smuzhiyun     dmxWinPrivPtr pWinPriv = DMX_GET_WINDOW_PRIV(pRoot);
974*4882a593Smuzhiyun     WindowPtr pWin;
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun     /* Create the pixmaps associated with the root window */
977*4882a593Smuzhiyun     if (!pRoot->borderIsPixel) {
978*4882a593Smuzhiyun         dmxBECreatePixmap(pRoot->border.pixmap);
979*4882a593Smuzhiyun         dmxBERestorePixmap(pRoot->border.pixmap);
980*4882a593Smuzhiyun     }
981*4882a593Smuzhiyun     if (pRoot->backgroundState == BackgroundPixmap) {
982*4882a593Smuzhiyun         dmxBECreatePixmap(pRoot->background.pixmap);
983*4882a593Smuzhiyun         dmxBERestorePixmap(pRoot->background.pixmap);
984*4882a593Smuzhiyun     }
985*4882a593Smuzhiyun 
986*4882a593Smuzhiyun     /* Create root window first */
987*4882a593Smuzhiyun     dmxScreen->rootWin = pWinPriv->window = dmxCreateRootWindow(pRoot);
988*4882a593Smuzhiyun     XMapWindow(dmxScreen->beDisplay, dmxScreen->rootWin);
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun     pWin = pRoot->lastChild;
991*4882a593Smuzhiyun     while (pWin) {
992*4882a593Smuzhiyun         pWinPriv = DMX_GET_WINDOW_PRIV(pWin);
993*4882a593Smuzhiyun 
994*4882a593Smuzhiyun         /* Create the pixmaps regardless of whether or not the
995*4882a593Smuzhiyun          * window is created or not due to lazy window creation.
996*4882a593Smuzhiyun          */
997*4882a593Smuzhiyun         if (!pWin->borderIsPixel) {
998*4882a593Smuzhiyun             dmxBECreatePixmap(pWin->border.pixmap);
999*4882a593Smuzhiyun             dmxBERestorePixmap(pWin->border.pixmap);
1000*4882a593Smuzhiyun         }
1001*4882a593Smuzhiyun         if (pWin->backgroundState == BackgroundPixmap) {
1002*4882a593Smuzhiyun             dmxBECreatePixmap(pWin->background.pixmap);
1003*4882a593Smuzhiyun             dmxBERestorePixmap(pWin->background.pixmap);
1004*4882a593Smuzhiyun         }
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun         /* Reset the window attributes */
1007*4882a593Smuzhiyun         dmxGetDefaultWindowAttributes(pWin, &pWinPriv->cmap, &pWinPriv->visual);
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun         /* Create the window */
1010*4882a593Smuzhiyun         if (pWinPriv->mapped && !pWinPriv->offscreen)
1011*4882a593Smuzhiyun             dmxCreateAndRealizeWindow(pWin, TRUE);
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun         /* Next, create the bottom-most child */
1014*4882a593Smuzhiyun         if (pWin->lastChild) {
1015*4882a593Smuzhiyun             pWin = pWin->lastChild;
1016*4882a593Smuzhiyun             continue;
1017*4882a593Smuzhiyun         }
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun         /* If the window has no children, move on to the next higher window */
1020*4882a593Smuzhiyun         while (!pWin->prevSib && (pWin != pRoot))
1021*4882a593Smuzhiyun             pWin = pWin->parent;
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun         if (pWin->prevSib) {
1024*4882a593Smuzhiyun             pWin = pWin->prevSib;
1025*4882a593Smuzhiyun             continue;
1026*4882a593Smuzhiyun         }
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun         /* When we reach the root window, we are finished */
1029*4882a593Smuzhiyun         if (pWin == pRoot)
1030*4882a593Smuzhiyun             break;
1031*4882a593Smuzhiyun     }
1032*4882a593Smuzhiyun }
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun /* Refresh screen by generating exposure events for all windows */
1035*4882a593Smuzhiyun static void
dmxForceExposures(int idx)1036*4882a593Smuzhiyun dmxForceExposures(int idx)
1037*4882a593Smuzhiyun {
1038*4882a593Smuzhiyun     ScreenPtr pScreen = screenInfo.screens[idx];
1039*4882a593Smuzhiyun     WindowPtr pRoot = pScreen->root;
1040*4882a593Smuzhiyun     Bool anyMarked = FALSE;
1041*4882a593Smuzhiyun     WindowPtr pChild;
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun     for (pChild = pRoot->firstChild; pChild; pChild = pChild->nextSib)
1044*4882a593Smuzhiyun         anyMarked |= pScreen->MarkOverlappedWindows(pChild, pChild,
1045*4882a593Smuzhiyun                                                     (WindowPtr *) NULL);
1046*4882a593Smuzhiyun     if (anyMarked) {
1047*4882a593Smuzhiyun         /* If any windows have been marked, set the root window's
1048*4882a593Smuzhiyun          * clipList to be broken since it will be recalculated in
1049*4882a593Smuzhiyun          * ValidateTree()
1050*4882a593Smuzhiyun          */
1051*4882a593Smuzhiyun         RegionBreak(&pRoot->clipList);
1052*4882a593Smuzhiyun         pScreen->ValidateTree(pRoot, NULL, VTBroken);
1053*4882a593Smuzhiyun         pScreen->HandleExposures(pRoot);
1054*4882a593Smuzhiyun         if (pScreen->PostValidateTree)
1055*4882a593Smuzhiyun             pScreen->PostValidateTree(pRoot, NULL, VTBroken);
1056*4882a593Smuzhiyun     }
1057*4882a593Smuzhiyun }
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun /** Compare the new and old screens to see if they are compatible. */
1060*4882a593Smuzhiyun static Bool
dmxCompareScreens(DMXScreenInfo * new,DMXScreenInfo * old)1061*4882a593Smuzhiyun dmxCompareScreens(DMXScreenInfo * new, DMXScreenInfo * old)
1062*4882a593Smuzhiyun {
1063*4882a593Smuzhiyun     int i;
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun     if (new->beWidth != old->beWidth)
1066*4882a593Smuzhiyun         return FALSE;
1067*4882a593Smuzhiyun     if (new->beHeight != old->beHeight)
1068*4882a593Smuzhiyun         return FALSE;
1069*4882a593Smuzhiyun     if (new->beDepth != old->beDepth)
1070*4882a593Smuzhiyun         return FALSE;
1071*4882a593Smuzhiyun     if (new->beBPP != old->beBPP)
1072*4882a593Smuzhiyun         return FALSE;
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun     if (new->beNumDepths != old->beNumDepths)
1075*4882a593Smuzhiyun         return FALSE;
1076*4882a593Smuzhiyun     for (i = 0; i < old->beNumDepths; i++)
1077*4882a593Smuzhiyun         if (new->beDepths[i] != old->beDepths[i])
1078*4882a593Smuzhiyun             return FALSE;
1079*4882a593Smuzhiyun 
1080*4882a593Smuzhiyun     if (new->beNumPixmapFormats != old->beNumPixmapFormats)
1081*4882a593Smuzhiyun         return FALSE;
1082*4882a593Smuzhiyun     for (i = 0; i < old->beNumPixmapFormats; i++) {
1083*4882a593Smuzhiyun         if (new->bePixmapFormats[i].depth != old->bePixmapFormats[i].depth)
1084*4882a593Smuzhiyun             return FALSE;
1085*4882a593Smuzhiyun         if (new->bePixmapFormats[i].bits_per_pixel !=
1086*4882a593Smuzhiyun             old->bePixmapFormats[i].bits_per_pixel)
1087*4882a593Smuzhiyun             return FALSE;
1088*4882a593Smuzhiyun         if (new->bePixmapFormats[i].scanline_pad !=
1089*4882a593Smuzhiyun             old->bePixmapFormats[i].scanline_pad)
1090*4882a593Smuzhiyun             return FALSE;
1091*4882a593Smuzhiyun     }
1092*4882a593Smuzhiyun 
1093*4882a593Smuzhiyun     if (new->beNumVisuals != old->beNumVisuals)
1094*4882a593Smuzhiyun         return FALSE;
1095*4882a593Smuzhiyun     for (i = 0; i < old->beNumVisuals; i++) {
1096*4882a593Smuzhiyun         if (new->beVisuals[i].visualid != old->beVisuals[i].visualid)
1097*4882a593Smuzhiyun             return FALSE;
1098*4882a593Smuzhiyun         if (new->beVisuals[i].screen != old->beVisuals[i].screen)
1099*4882a593Smuzhiyun             return FALSE;
1100*4882a593Smuzhiyun         if (new->beVisuals[i].depth != old->beVisuals[i].depth)
1101*4882a593Smuzhiyun             return FALSE;
1102*4882a593Smuzhiyun         if (new->beVisuals[i].class != old->beVisuals[i].class)
1103*4882a593Smuzhiyun             return FALSE;
1104*4882a593Smuzhiyun         if (new->beVisuals[i].red_mask != old->beVisuals[i].red_mask)
1105*4882a593Smuzhiyun             return FALSE;
1106*4882a593Smuzhiyun         if (new->beVisuals[i].green_mask != old->beVisuals[i].green_mask)
1107*4882a593Smuzhiyun             return FALSE;
1108*4882a593Smuzhiyun         if (new->beVisuals[i].blue_mask != old->beVisuals[i].blue_mask)
1109*4882a593Smuzhiyun             return FALSE;
1110*4882a593Smuzhiyun         if (new->beVisuals[i].colormap_size != old->beVisuals[i].colormap_size)
1111*4882a593Smuzhiyun             return FALSE;
1112*4882a593Smuzhiyun         if (new->beVisuals[i].bits_per_rgb != old->beVisuals[i].bits_per_rgb)
1113*4882a593Smuzhiyun             return FALSE;
1114*4882a593Smuzhiyun     }
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun     if (new->beDefVisualIndex != old->beDefVisualIndex)
1117*4882a593Smuzhiyun         return FALSE;
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun     return TRUE;
1120*4882a593Smuzhiyun }
1121*4882a593Smuzhiyun 
1122*4882a593Smuzhiyun /** Restore Render's picture */
1123*4882a593Smuzhiyun static void
dmxBERestoreRenderPict(void * value,XID id,void * n)1124*4882a593Smuzhiyun dmxBERestoreRenderPict(void *value, XID id, void *n)
1125*4882a593Smuzhiyun {
1126*4882a593Smuzhiyun     PicturePtr pPicture = value;        /* The picture */
1127*4882a593Smuzhiyun     DrawablePtr pDraw = pPicture->pDrawable;    /* The picture's drawable */
1128*4882a593Smuzhiyun     int scrnNum = (uintptr_t) n;
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun     if (pDraw->pScreen->myNum != scrnNum) {
1131*4882a593Smuzhiyun         /* Picture not on the screen we are restoring */
1132*4882a593Smuzhiyun         return;
1133*4882a593Smuzhiyun     }
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun     if (pDraw->type == DRAWABLE_PIXMAP) {
1136*4882a593Smuzhiyun         PixmapPtr pPixmap = (PixmapPtr) pDraw;
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun         /* Create and restore the pixmap drawable */
1139*4882a593Smuzhiyun         dmxBECreatePixmap(pPixmap);
1140*4882a593Smuzhiyun         dmxBERestorePixmap(pPixmap);
1141*4882a593Smuzhiyun     }
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun     dmxBECreatePicture(pPicture);
1144*4882a593Smuzhiyun }
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun /** Restore Render's glyphs */
1147*4882a593Smuzhiyun static void
dmxBERestoreRenderGlyph(void * value,XID id,void * n)1148*4882a593Smuzhiyun dmxBERestoreRenderGlyph(void *value, XID id, void *n)
1149*4882a593Smuzhiyun {
1150*4882a593Smuzhiyun     GlyphSetPtr glyphSet = value;
1151*4882a593Smuzhiyun     int scrnNum = (uintptr_t) n;
1152*4882a593Smuzhiyun     dmxGlyphPrivPtr glyphPriv = DMX_GET_GLYPH_PRIV(glyphSet);
1153*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen = &dmxScreens[scrnNum];
1154*4882a593Smuzhiyun     GlyphRefPtr table;
1155*4882a593Smuzhiyun     char *images;
1156*4882a593Smuzhiyun     Glyph *gids;
1157*4882a593Smuzhiyun     XGlyphInfo *glyphs;
1158*4882a593Smuzhiyun     char *pos;
1159*4882a593Smuzhiyun     int beret;
1160*4882a593Smuzhiyun     int len_images = 0;
1161*4882a593Smuzhiyun     int i;
1162*4882a593Smuzhiyun     int ctr;
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun     if (glyphPriv->glyphSets[scrnNum]) {
1165*4882a593Smuzhiyun         /* Only restore glyphs on the screen we are attaching */
1166*4882a593Smuzhiyun         return;
1167*4882a593Smuzhiyun     }
1168*4882a593Smuzhiyun 
1169*4882a593Smuzhiyun     /* First we must create the glyph set on the backend. */
1170*4882a593Smuzhiyun     if ((beret = dmxBECreateGlyphSet(scrnNum, glyphSet)) != Success) {
1171*4882a593Smuzhiyun         dmxLog(dmxWarning,
1172*4882a593Smuzhiyun                "\tdmxBERestoreRenderGlyph failed to create glyphset!\n");
1173*4882a593Smuzhiyun         return;
1174*4882a593Smuzhiyun     }
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun     /* Now for the complex part, restore the glyph data */
1177*4882a593Smuzhiyun     table = glyphSet->hash.table;
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun     /* We need to know how much memory to allocate for this part */
1180*4882a593Smuzhiyun     for (i = 0; i < glyphSet->hash.hashSet->size; i++) {
1181*4882a593Smuzhiyun         GlyphRefPtr gr = &table[i];
1182*4882a593Smuzhiyun         GlyphPtr gl = gr->glyph;
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun         if (!gl || gl == DeletedGlyph)
1185*4882a593Smuzhiyun             continue;
1186*4882a593Smuzhiyun         len_images += gl->size - sizeof(gl->info);
1187*4882a593Smuzhiyun     }
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun     /* Now allocate the memory we need */
1190*4882a593Smuzhiyun     images = calloc(len_images, sizeof(char));
1191*4882a593Smuzhiyun     gids = xallocarray(glyphSet->hash.tableEntries, sizeof(Glyph));
1192*4882a593Smuzhiyun     glyphs = xallocarray(glyphSet->hash.tableEntries, sizeof(XGlyphInfo));
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun     pos = images;
1195*4882a593Smuzhiyun     ctr = 0;
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun     /* Fill the allocated memory with the proper data */
1198*4882a593Smuzhiyun     for (i = 0; i < glyphSet->hash.hashSet->size; i++) {
1199*4882a593Smuzhiyun         GlyphRefPtr gr = &table[i];
1200*4882a593Smuzhiyun         GlyphPtr gl = gr->glyph;
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun         if (!gl || gl == DeletedGlyph)
1203*4882a593Smuzhiyun             continue;
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun         /* First lets put the data into gids */
1206*4882a593Smuzhiyun         gids[ctr] = gr->signature;
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun         /* Next do the glyphs data structures */
1209*4882a593Smuzhiyun         glyphs[ctr].width = gl->info.width;
1210*4882a593Smuzhiyun         glyphs[ctr].height = gl->info.height;
1211*4882a593Smuzhiyun         glyphs[ctr].x = gl->info.x;
1212*4882a593Smuzhiyun         glyphs[ctr].y = gl->info.y;
1213*4882a593Smuzhiyun         glyphs[ctr].xOff = gl->info.xOff;
1214*4882a593Smuzhiyun         glyphs[ctr].yOff = gl->info.yOff;
1215*4882a593Smuzhiyun 
1216*4882a593Smuzhiyun         /* Copy the images from the DIX's data into the buffer */
1217*4882a593Smuzhiyun         memcpy(pos, gl + 1, gl->size - sizeof(gl->info));
1218*4882a593Smuzhiyun         pos += gl->size - sizeof(gl->info);
1219*4882a593Smuzhiyun         ctr++;
1220*4882a593Smuzhiyun     }
1221*4882a593Smuzhiyun 
1222*4882a593Smuzhiyun     /* Now restore the glyph data */
1223*4882a593Smuzhiyun     XRenderAddGlyphs(dmxScreen->beDisplay, glyphPriv->glyphSets[scrnNum],
1224*4882a593Smuzhiyun                      gids, glyphs, glyphSet->hash.tableEntries, images,
1225*4882a593Smuzhiyun                      len_images);
1226*4882a593Smuzhiyun 
1227*4882a593Smuzhiyun     /* Clean up */
1228*4882a593Smuzhiyun     free(images);
1229*4882a593Smuzhiyun     free(gids);
1230*4882a593Smuzhiyun     free(glyphs);
1231*4882a593Smuzhiyun }
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun /** Reattach previously detached back-end screen. */
1234*4882a593Smuzhiyun int
dmxAttachScreen(int idx,DMXScreenAttributesPtr attr)1235*4882a593Smuzhiyun dmxAttachScreen(int idx, DMXScreenAttributesPtr attr)
1236*4882a593Smuzhiyun {
1237*4882a593Smuzhiyun     ScreenPtr pScreen;
1238*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen;
1239*4882a593Smuzhiyun     CARD32 scrnNum = idx;
1240*4882a593Smuzhiyun     DMXScreenInfo oldDMXScreen;
1241*4882a593Smuzhiyun     int i;
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun     /* Return failure if dynamic addition/removal of screens is disabled */
1244*4882a593Smuzhiyun     if (!dmxAddRemoveScreens) {
1245*4882a593Smuzhiyun         dmxLog(dmxWarning,
1246*4882a593Smuzhiyun                "Attempting to add a screen, but the AddRemoveScreen\n");
1247*4882a593Smuzhiyun         dmxLog(dmxWarning,
1248*4882a593Smuzhiyun                "extension has not been enabled.  To enable this extension\n");
1249*4882a593Smuzhiyun         dmxLog(dmxWarning,
1250*4882a593Smuzhiyun                "add the \"-addremovescreens\" option either to the command\n");
1251*4882a593Smuzhiyun         dmxLog(dmxWarning, "line or in the configuration file.\n");
1252*4882a593Smuzhiyun         return 1;
1253*4882a593Smuzhiyun     }
1254*4882a593Smuzhiyun 
1255*4882a593Smuzhiyun     /* Cannot add a screen that does not exist */
1256*4882a593Smuzhiyun     if (idx < 0 || idx >= dmxNumScreens)
1257*4882a593Smuzhiyun         return 1;
1258*4882a593Smuzhiyun     pScreen = screenInfo.screens[idx];
1259*4882a593Smuzhiyun     dmxScreen = &dmxScreens[idx];
1260*4882a593Smuzhiyun 
1261*4882a593Smuzhiyun     /* Cannot attach to a screen that is already opened */
1262*4882a593Smuzhiyun     if (dmxScreen->beDisplay) {
1263*4882a593Smuzhiyun         dmxLog(dmxWarning,
1264*4882a593Smuzhiyun                "Attempting to add screen #%d but a screen already exists\n",
1265*4882a593Smuzhiyun                idx);
1266*4882a593Smuzhiyun         return 1;
1267*4882a593Smuzhiyun     }
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun     dmxLogOutput(dmxScreen, "Attaching screen #%d\n", idx);
1270*4882a593Smuzhiyun 
1271*4882a593Smuzhiyun     /* Save old info */
1272*4882a593Smuzhiyun     oldDMXScreen = *dmxScreen;
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun     /* Copy the name to the new screen */
1275*4882a593Smuzhiyun     dmxScreen->name = strdup(attr->displayName);
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun     /* Open display and get all of the screen info */
1278*4882a593Smuzhiyun     if (!dmxOpenDisplay(dmxScreen)) {
1279*4882a593Smuzhiyun         dmxLog(dmxWarning,
1280*4882a593Smuzhiyun                "dmxOpenDisplay: Unable to open display %s\n", dmxScreen->name);
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun         /* Restore the old screen */
1283*4882a593Smuzhiyun         *dmxScreen = oldDMXScreen;
1284*4882a593Smuzhiyun         return 1;
1285*4882a593Smuzhiyun     }
1286*4882a593Smuzhiyun 
1287*4882a593Smuzhiyun     dmxSetErrorHandler(dmxScreen);
1288*4882a593Smuzhiyun     dmxCheckForWM(dmxScreen);
1289*4882a593Smuzhiyun     dmxGetScreenAttribs(dmxScreen);
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun     if (!dmxGetVisualInfo(dmxScreen)) {
1292*4882a593Smuzhiyun         dmxLog(dmxWarning, "dmxGetVisualInfo: No matching visuals found\n");
1293*4882a593Smuzhiyun         XFree(dmxScreen->beVisuals);
1294*4882a593Smuzhiyun         XCloseDisplay(dmxScreen->beDisplay);
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun         /* Restore the old screen */
1297*4882a593Smuzhiyun         *dmxScreen = oldDMXScreen;
1298*4882a593Smuzhiyun         return 1;
1299*4882a593Smuzhiyun     }
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun     dmxGetColormaps(dmxScreen);
1302*4882a593Smuzhiyun     dmxGetPixmapFormats(dmxScreen);
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun     /* Verify that the screen to be added has the same info as the
1305*4882a593Smuzhiyun      * previously added screen. */
1306*4882a593Smuzhiyun     if (!dmxCompareScreens(dmxScreen, &oldDMXScreen)) {
1307*4882a593Smuzhiyun         dmxLog(dmxWarning,
1308*4882a593Smuzhiyun                "New screen data (%s) does not match previously\n",
1309*4882a593Smuzhiyun                dmxScreen->name);
1310*4882a593Smuzhiyun         dmxLog(dmxWarning, "attached screen data (%s)\n", oldDMXScreen.name);
1311*4882a593Smuzhiyun         dmxLog(dmxWarning,
1312*4882a593Smuzhiyun                "All data must match in order to attach to screen #%d\n", idx);
1313*4882a593Smuzhiyun         XFree(dmxScreen->beVisuals);
1314*4882a593Smuzhiyun         XFree(dmxScreen->beDepths);
1315*4882a593Smuzhiyun         XFree(dmxScreen->bePixmapFormats);
1316*4882a593Smuzhiyun         XCloseDisplay(dmxScreen->beDisplay);
1317*4882a593Smuzhiyun 
1318*4882a593Smuzhiyun         /* Restore the old screen */
1319*4882a593Smuzhiyun         *dmxScreen = oldDMXScreen;
1320*4882a593Smuzhiyun         return 1;
1321*4882a593Smuzhiyun     }
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun     /* Initialize the BE screen resources */
1324*4882a593Smuzhiyun     dmxBEScreenInit(screenInfo.screens[idx]);
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun     /* TODO: Handle GLX visual initialization.  GLXProxy needs to be
1327*4882a593Smuzhiyun      * updated to handle dynamic addition/removal of screens. */
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun     /* Create default stipple */
1330*4882a593Smuzhiyun     dmxBECreatePixmap(pScreen->defaultStipple);
1331*4882a593Smuzhiyun     dmxBERestorePixmap(pScreen->defaultStipple);
1332*4882a593Smuzhiyun 
1333*4882a593Smuzhiyun     /* Create the scratch GCs */
1334*4882a593Smuzhiyun     dmxBECreateScratchGCs(idx);
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun     /* Create the default font */
1337*4882a593Smuzhiyun     (void) dmxBELoadFont(pScreen, defaultFont);
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun     /* Create all resources that don't depend on windows */
1340*4882a593Smuzhiyun     for (i = currentMaxClients; --i >= 0;)
1341*4882a593Smuzhiyun         if (clients[i])
1342*4882a593Smuzhiyun             FindAllClientResources(clients[i], dmxBECreateResources,
1343*4882a593Smuzhiyun                                    (void *) (uintptr_t) idx);
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun     /* Create window hierarchy (top down) */
1346*4882a593Smuzhiyun     dmxBECreateWindowTree(idx);
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun     /* Restore the picture state for RENDER */
1349*4882a593Smuzhiyun     for (i = currentMaxClients; --i >= 0;)
1350*4882a593Smuzhiyun         if (clients[i])
1351*4882a593Smuzhiyun             FindClientResourcesByType(clients[i], PictureType,
1352*4882a593Smuzhiyun                                       dmxBERestoreRenderPict,
1353*4882a593Smuzhiyun                                       (void *) (uintptr_t) idx);
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun     /* Restore the glyph state for RENDER */
1356*4882a593Smuzhiyun     for (i = currentMaxClients; --i >= 0;)
1357*4882a593Smuzhiyun         if (clients[i])
1358*4882a593Smuzhiyun             FindClientResourcesByType(clients[i], GlyphSetType,
1359*4882a593Smuzhiyun                                       dmxBERestoreRenderGlyph,
1360*4882a593Smuzhiyun                                       (void *) (uintptr_t) idx);
1361*4882a593Smuzhiyun 
1362*4882a593Smuzhiyun     /* Refresh screen by generating exposure events for all windows */
1363*4882a593Smuzhiyun     dmxForceExposures(idx);
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun     dmxSync(&dmxScreens[idx], TRUE);
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun     /* We used these to compare the old and new screens.  They are no
1368*4882a593Smuzhiyun      * longer needed since we have a newly attached screen, so we can
1369*4882a593Smuzhiyun      * now free the old screen's resources. */
1370*4882a593Smuzhiyun     XFree(oldDMXScreen.beVisuals);
1371*4882a593Smuzhiyun     XFree(oldDMXScreen.beDepths);
1372*4882a593Smuzhiyun     XFree(oldDMXScreen.bePixmapFormats);
1373*4882a593Smuzhiyun     /* TODO: should oldDMXScreen.name be freed?? */
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun #ifdef PANORAMIX
1376*4882a593Smuzhiyun     if (!noPanoramiXExtension)
1377*4882a593Smuzhiyun         return dmxConfigureScreenWindows(1, &scrnNum, attr, NULL);
1378*4882a593Smuzhiyun     else
1379*4882a593Smuzhiyun #endif
1380*4882a593Smuzhiyun         return 0;               /* Success */
1381*4882a593Smuzhiyun }
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun /*
1384*4882a593Smuzhiyun  * Resources that may have state on the BE server and need to be freed:
1385*4882a593Smuzhiyun  *
1386*4882a593Smuzhiyun  * RT_NONE
1387*4882a593Smuzhiyun  * RT_WINDOW
1388*4882a593Smuzhiyun  * RT_PIXMAP
1389*4882a593Smuzhiyun  * RT_GC
1390*4882a593Smuzhiyun  * RT_FONT
1391*4882a593Smuzhiyun  * RT_CURSOR
1392*4882a593Smuzhiyun  * RT_COLORMAP
1393*4882a593Smuzhiyun  * RT_CMAPENTRY
1394*4882a593Smuzhiyun  * RT_OTHERCLIENT
1395*4882a593Smuzhiyun  * RT_PASSIVEGRAB
1396*4882a593Smuzhiyun  * XRT_WINDOW
1397*4882a593Smuzhiyun  * XRT_PIXMAP
1398*4882a593Smuzhiyun  * XRT_GC
1399*4882a593Smuzhiyun  * XRT_COLORMAP
1400*4882a593Smuzhiyun  * XRT_PICTURE
1401*4882a593Smuzhiyun  * PictureType
1402*4882a593Smuzhiyun  * PictFormatType
1403*4882a593Smuzhiyun  * GlyphSetType
1404*4882a593Smuzhiyun  * ClientType
1405*4882a593Smuzhiyun  * EventType
1406*4882a593Smuzhiyun  * RT_INPUTCLIENT
1407*4882a593Smuzhiyun  * XETrapType
1408*4882a593Smuzhiyun  * RTCounter
1409*4882a593Smuzhiyun  * RTAwait
1410*4882a593Smuzhiyun  * RTAlarmClient
1411*4882a593Smuzhiyun  * RT_XKBCLIENT
1412*4882a593Smuzhiyun  * RTContext
1413*4882a593Smuzhiyun  * TagResType
1414*4882a593Smuzhiyun  * StalledResType
1415*4882a593Smuzhiyun  * SecurityAuthorizationResType
1416*4882a593Smuzhiyun  * RTEventClient
1417*4882a593Smuzhiyun  * __glXContextRes
1418*4882a593Smuzhiyun  * __glXClientRes
1419*4882a593Smuzhiyun  * __glXPixmapRes
1420*4882a593Smuzhiyun  * __glXWindowRes
1421*4882a593Smuzhiyun  * __glXPbufferRes
1422*4882a593Smuzhiyun  */
1423*4882a593Smuzhiyun 
1424*4882a593Smuzhiyun #ifdef PANORAMIX
1425*4882a593Smuzhiyun /** Search the Xinerama XRT_PIXMAP resources for the pixmap that needs
1426*4882a593Smuzhiyun  *  to have its image saved. */
1427*4882a593Smuzhiyun static void
dmxBEFindPixmapImage(void * value,XID id,RESTYPE type,void * p)1428*4882a593Smuzhiyun dmxBEFindPixmapImage(void *value, XID id, RESTYPE type, void *p)
1429*4882a593Smuzhiyun {
1430*4882a593Smuzhiyun     if ((type & TypeMask) == (XRT_PIXMAP & TypeMask)) {
1431*4882a593Smuzhiyun         PixmapPtr pDst = (PixmapPtr) p;
1432*4882a593Smuzhiyun         int idx = pDst->drawable.pScreen->myNum;
1433*4882a593Smuzhiyun         PanoramiXRes *pXinPix = (PanoramiXRes *) value;
1434*4882a593Smuzhiyun         PixmapPtr pPix;
1435*4882a593Smuzhiyun         int i;
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun         dixLookupResourceByType((void **) &pPix, pXinPix->info[idx].id,
1438*4882a593Smuzhiyun                                 RT_PIXMAP, NullClient, DixUnknownAccess);
1439*4882a593Smuzhiyun         if (pPix != pDst)
1440*4882a593Smuzhiyun             return;             /* Not a match.... Next! */
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun         FOR_NSCREENS(i) {
1443*4882a593Smuzhiyun             PixmapPtr pSrc;
1444*4882a593Smuzhiyun             dmxPixPrivPtr pSrcPriv = NULL;
1445*4882a593Smuzhiyun 
1446*4882a593Smuzhiyun             if (i == idx)
1447*4882a593Smuzhiyun                 continue;       /* Self replication is bad */
1448*4882a593Smuzhiyun 
1449*4882a593Smuzhiyun             dixLookupResourceByType((void **) &pSrc, pXinPix->info[i].id,
1450*4882a593Smuzhiyun                                     RT_PIXMAP, NullClient, DixUnknownAccess);
1451*4882a593Smuzhiyun             pSrcPriv = DMX_GET_PIXMAP_PRIV(pSrc);
1452*4882a593Smuzhiyun             if (pSrcPriv->pixmap) {
1453*4882a593Smuzhiyun                 FoundPixImage = True;
1454*4882a593Smuzhiyun                 return;
1455*4882a593Smuzhiyun             }
1456*4882a593Smuzhiyun         }
1457*4882a593Smuzhiyun     }
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun #endif
1460*4882a593Smuzhiyun 
1461*4882a593Smuzhiyun /** Save the pixmap image only when there is not another screen with
1462*4882a593Smuzhiyun  *  that pixmap from which the image can be read when the screen is
1463*4882a593Smuzhiyun  *  reattached.  To do this, we first try to find a pixmap on another
1464*4882a593Smuzhiyun  *  screen corresponding to the one we are trying to save.  If we find
1465*4882a593Smuzhiyun  *  one, then we do not need to save the image data since during
1466*4882a593Smuzhiyun  *  reattachment, the image data can be read from that other pixmap.
1467*4882a593Smuzhiyun  *  However, if we do not find one, then we need to save the image data.
1468*4882a593Smuzhiyun  *  The common case for these are for the default stipple and root
1469*4882a593Smuzhiyun  *  tile. */
1470*4882a593Smuzhiyun static void
dmxBESavePixmap(PixmapPtr pPixmap)1471*4882a593Smuzhiyun dmxBESavePixmap(PixmapPtr pPixmap)
1472*4882a593Smuzhiyun {
1473*4882a593Smuzhiyun #ifdef PANORAMIX
1474*4882a593Smuzhiyun     int i;
1475*4882a593Smuzhiyun 
1476*4882a593Smuzhiyun     /* If Xinerama is not active, there's nothing we can do (see comment
1477*4882a593Smuzhiyun      * in #else below for more info). */
1478*4882a593Smuzhiyun     if (noPanoramiXExtension)
1479*4882a593Smuzhiyun         return;
1480*4882a593Smuzhiyun 
1481*4882a593Smuzhiyun     FoundPixImage = False;
1482*4882a593Smuzhiyun     for (i = currentMaxClients; --i >= 0;)
1483*4882a593Smuzhiyun         if (clients[i])
1484*4882a593Smuzhiyun             FindAllClientResources(clients[i], dmxBEFindPixmapImage,
1485*4882a593Smuzhiyun                                    (void *) pPixmap);
1486*4882a593Smuzhiyun 
1487*4882a593Smuzhiyun     /* Save the image only if there is no other screens that have a
1488*4882a593Smuzhiyun      * pixmap that corresponds to the one we are trying to save. */
1489*4882a593Smuzhiyun     if (!FoundPixImage) {
1490*4882a593Smuzhiyun         dmxPixPrivPtr pPixPriv = DMX_GET_PIXMAP_PRIV(pPixmap);
1491*4882a593Smuzhiyun 
1492*4882a593Smuzhiyun         if (!pPixPriv->detachedImage) {
1493*4882a593Smuzhiyun             ScreenPtr pScreen = pPixmap->drawable.pScreen;
1494*4882a593Smuzhiyun             DMXScreenInfo *dmxScreen = &dmxScreens[pScreen->myNum];
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun             pPixPriv->detachedImage = XGetImage(dmxScreen->beDisplay,
1497*4882a593Smuzhiyun                                                 pPixPriv->pixmap,
1498*4882a593Smuzhiyun                                                 0, 0,
1499*4882a593Smuzhiyun                                                 pPixmap->drawable.width,
1500*4882a593Smuzhiyun                                                 pPixmap->drawable.height,
1501*4882a593Smuzhiyun                                                 -1, ZPixmap);
1502*4882a593Smuzhiyun             if (!pPixPriv->detachedImage)
1503*4882a593Smuzhiyun                 dmxLog(dmxWarning, "Cannot save pixmap image\n");
1504*4882a593Smuzhiyun         }
1505*4882a593Smuzhiyun     }
1506*4882a593Smuzhiyun #else
1507*4882a593Smuzhiyun     /* NOTE: The only time there is a pixmap on another screen that
1508*4882a593Smuzhiyun      * corresponds to the one we are trying to save is when Xinerama is
1509*4882a593Smuzhiyun      * active.  Otherwise, the pixmap image data is only stored on a
1510*4882a593Smuzhiyun      * single screen, which means that once it is detached, that data is
1511*4882a593Smuzhiyun      * lost.  We could save the data here, but then that would require
1512*4882a593Smuzhiyun      * us to implement the ability for Xdmx to keep the pixmap up to
1513*4882a593Smuzhiyun      * date while the screen is detached, which is beyond the scope of
1514*4882a593Smuzhiyun      * the current project. */
1515*4882a593Smuzhiyun     return;
1516*4882a593Smuzhiyun #endif
1517*4882a593Smuzhiyun }
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun /** Destroy resources on the back-end server.  This function is called
1520*4882a593Smuzhiyun  *  from #dmxDetachScreen() via the dix layer's FindAllResources
1521*4882a593Smuzhiyun  *  function.  It walks all resources, compares them to the screen
1522*4882a593Smuzhiyun  *  number passed in as \a n and calls the appropriate DMX function to
1523*4882a593Smuzhiyun  *  free the associated resource on the back-end server. */
1524*4882a593Smuzhiyun static void
dmxBEDestroyResources(void * value,XID id,RESTYPE type,void * n)1525*4882a593Smuzhiyun dmxBEDestroyResources(void *value, XID id, RESTYPE type, void *n)
1526*4882a593Smuzhiyun {
1527*4882a593Smuzhiyun     int scrnNum = (uintptr_t) n;
1528*4882a593Smuzhiyun     ScreenPtr pScreen = screenInfo.screens[scrnNum];
1529*4882a593Smuzhiyun 
1530*4882a593Smuzhiyun     if ((type & TypeMask) == (RT_WINDOW & TypeMask)) {
1531*4882a593Smuzhiyun         /* Window resources are destroyed below in dmxBEDestroyWindowTree */
1532*4882a593Smuzhiyun     }
1533*4882a593Smuzhiyun     else if ((type & TypeMask) == (RT_PIXMAP & TypeMask)) {
1534*4882a593Smuzhiyun         PixmapPtr pPix = value;
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun         if (pPix->drawable.pScreen->myNum == scrnNum) {
1537*4882a593Smuzhiyun             dmxBESavePixmap(pPix);
1538*4882a593Smuzhiyun             dmxBEFreePixmap(pPix);
1539*4882a593Smuzhiyun         }
1540*4882a593Smuzhiyun     }
1541*4882a593Smuzhiyun     else if ((type & TypeMask) == (RT_GC & TypeMask)) {
1542*4882a593Smuzhiyun         GCPtr pGC = value;
1543*4882a593Smuzhiyun 
1544*4882a593Smuzhiyun         if (pGC->pScreen->myNum == scrnNum)
1545*4882a593Smuzhiyun             dmxBEFreeGC(pGC);
1546*4882a593Smuzhiyun     }
1547*4882a593Smuzhiyun     else if ((type & TypeMask) == (RT_FONT & TypeMask)) {
1548*4882a593Smuzhiyun         dmxBEFreeFont(pScreen, (FontPtr) value);
1549*4882a593Smuzhiyun     }
1550*4882a593Smuzhiyun     else if ((type & TypeMask) == (RT_CURSOR & TypeMask)) {
1551*4882a593Smuzhiyun         dmxBEFreeCursor(pScreen, (CursorPtr) value);
1552*4882a593Smuzhiyun     }
1553*4882a593Smuzhiyun     else if ((type & TypeMask) == (RT_COLORMAP & TypeMask)) {
1554*4882a593Smuzhiyun         ColormapPtr pCmap = value;
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun         if (pCmap->pScreen->myNum == scrnNum)
1557*4882a593Smuzhiyun             dmxBEFreeColormap((ColormapPtr) value);
1558*4882a593Smuzhiyun     }
1559*4882a593Smuzhiyun     else if ((type & TypeMask) == (PictureType & TypeMask)) {
1560*4882a593Smuzhiyun         PicturePtr pPict = value;
1561*4882a593Smuzhiyun 
1562*4882a593Smuzhiyun         if (pPict->pDrawable->pScreen->myNum == scrnNum) {
1563*4882a593Smuzhiyun             /* Free the pixmaps on the backend if needed */
1564*4882a593Smuzhiyun             if (pPict->pDrawable->type == DRAWABLE_PIXMAP) {
1565*4882a593Smuzhiyun                 PixmapPtr pPixmap = (PixmapPtr) (pPict->pDrawable);
1566*4882a593Smuzhiyun 
1567*4882a593Smuzhiyun                 dmxBESavePixmap(pPixmap);
1568*4882a593Smuzhiyun                 dmxBEFreePixmap(pPixmap);
1569*4882a593Smuzhiyun             }
1570*4882a593Smuzhiyun             dmxBEFreePicture((PicturePtr) value);
1571*4882a593Smuzhiyun         }
1572*4882a593Smuzhiyun     }
1573*4882a593Smuzhiyun     else if ((type & TypeMask) == (GlyphSetType & TypeMask)) {
1574*4882a593Smuzhiyun         dmxBEFreeGlyphSet(pScreen, (GlyphSetPtr) value);
1575*4882a593Smuzhiyun     }
1576*4882a593Smuzhiyun     else {
1577*4882a593Smuzhiyun         /* Other resource types??? */
1578*4882a593Smuzhiyun     }
1579*4882a593Smuzhiyun }
1580*4882a593Smuzhiyun 
1581*4882a593Smuzhiyun /** Destroy the scratch GCs that are created per depth. */
1582*4882a593Smuzhiyun static void
dmxBEDestroyScratchGCs(int scrnNum)1583*4882a593Smuzhiyun dmxBEDestroyScratchGCs(int scrnNum)
1584*4882a593Smuzhiyun {
1585*4882a593Smuzhiyun     ScreenPtr pScreen = screenInfo.screens[scrnNum];
1586*4882a593Smuzhiyun     GCPtr *ppGC = pScreen->GCperDepth;
1587*4882a593Smuzhiyun     int i;
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun     for (i = 0; i <= pScreen->numDepths; i++)
1590*4882a593Smuzhiyun         dmxBEFreeGC(ppGC[i]);
1591*4882a593Smuzhiyun }
1592*4882a593Smuzhiyun 
1593*4882a593Smuzhiyun /** Destroy window hierachy on back-end server.  To ensure that all
1594*4882a593Smuzhiyun  *  XDestroyWindow() calls succeed, they must be performed in a bottom
1595*4882a593Smuzhiyun  *  up order so that windows are not destroyed before their children.
1596*4882a593Smuzhiyun  *  XDestroyWindow(), which is called from #dmxBEDestroyWindow(), will
1597*4882a593Smuzhiyun  *  destroy a window as well as all of it's children. */
1598*4882a593Smuzhiyun static void
dmxBEDestroyWindowTree(int idx)1599*4882a593Smuzhiyun dmxBEDestroyWindowTree(int idx)
1600*4882a593Smuzhiyun {
1601*4882a593Smuzhiyun     WindowPtr pWin = screenInfo.screens[idx]->root;
1602*4882a593Smuzhiyun     WindowPtr pChild = pWin;
1603*4882a593Smuzhiyun 
1604*4882a593Smuzhiyun     while (1) {
1605*4882a593Smuzhiyun         if (pChild->firstChild) {
1606*4882a593Smuzhiyun             pChild = pChild->firstChild;
1607*4882a593Smuzhiyun             continue;
1608*4882a593Smuzhiyun         }
1609*4882a593Smuzhiyun 
1610*4882a593Smuzhiyun         /* Destroy the window */
1611*4882a593Smuzhiyun         dmxBEDestroyWindow(pChild);
1612*4882a593Smuzhiyun 
1613*4882a593Smuzhiyun         /* Make sure we destroy the window's border and background
1614*4882a593Smuzhiyun          * pixmaps if they exist */
1615*4882a593Smuzhiyun         if (!pChild->borderIsPixel) {
1616*4882a593Smuzhiyun             dmxBESavePixmap(pChild->border.pixmap);
1617*4882a593Smuzhiyun             dmxBEFreePixmap(pChild->border.pixmap);
1618*4882a593Smuzhiyun         }
1619*4882a593Smuzhiyun         if (pChild->backgroundState == BackgroundPixmap) {
1620*4882a593Smuzhiyun             dmxBESavePixmap(pChild->background.pixmap);
1621*4882a593Smuzhiyun             dmxBEFreePixmap(pChild->background.pixmap);
1622*4882a593Smuzhiyun         }
1623*4882a593Smuzhiyun 
1624*4882a593Smuzhiyun         while (!pChild->nextSib && (pChild != pWin)) {
1625*4882a593Smuzhiyun             pChild = pChild->parent;
1626*4882a593Smuzhiyun             dmxBEDestroyWindow(pChild);
1627*4882a593Smuzhiyun             if (!pChild->borderIsPixel) {
1628*4882a593Smuzhiyun                 dmxBESavePixmap(pChild->border.pixmap);
1629*4882a593Smuzhiyun                 dmxBEFreePixmap(pChild->border.pixmap);
1630*4882a593Smuzhiyun             }
1631*4882a593Smuzhiyun             if (pChild->backgroundState == BackgroundPixmap) {
1632*4882a593Smuzhiyun                 dmxBESavePixmap(pChild->background.pixmap);
1633*4882a593Smuzhiyun                 dmxBEFreePixmap(pChild->background.pixmap);
1634*4882a593Smuzhiyun             }
1635*4882a593Smuzhiyun         }
1636*4882a593Smuzhiyun 
1637*4882a593Smuzhiyun         if (pChild == pWin)
1638*4882a593Smuzhiyun             break;
1639*4882a593Smuzhiyun 
1640*4882a593Smuzhiyun         pChild = pChild->nextSib;
1641*4882a593Smuzhiyun     }
1642*4882a593Smuzhiyun }
1643*4882a593Smuzhiyun 
1644*4882a593Smuzhiyun /** Detach back-end screen. */
1645*4882a593Smuzhiyun int
dmxDetachScreen(int idx)1646*4882a593Smuzhiyun dmxDetachScreen(int idx)
1647*4882a593Smuzhiyun {
1648*4882a593Smuzhiyun     DMXScreenInfo *dmxScreen = &dmxScreens[idx];
1649*4882a593Smuzhiyun     int i;
1650*4882a593Smuzhiyun 
1651*4882a593Smuzhiyun     /* Return failure if dynamic addition/removal of screens is disabled */
1652*4882a593Smuzhiyun     if (!dmxAddRemoveScreens) {
1653*4882a593Smuzhiyun         dmxLog(dmxWarning,
1654*4882a593Smuzhiyun                "Attempting to remove a screen, but the AddRemoveScreen\n");
1655*4882a593Smuzhiyun         dmxLog(dmxWarning,
1656*4882a593Smuzhiyun                "extension has not been enabled.  To enable this extension\n");
1657*4882a593Smuzhiyun         dmxLog(dmxWarning,
1658*4882a593Smuzhiyun                "add the \"-addremovescreens\" option either to the command\n");
1659*4882a593Smuzhiyun         dmxLog(dmxWarning, "line or in the configuration file.\n");
1660*4882a593Smuzhiyun         return 1;
1661*4882a593Smuzhiyun     }
1662*4882a593Smuzhiyun 
1663*4882a593Smuzhiyun     /* Cannot remove a screen that does not exist */
1664*4882a593Smuzhiyun     if (idx < 0 || idx >= dmxNumScreens)
1665*4882a593Smuzhiyun         return 1;
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun     /* Cannot detach from a screen that is not opened */
1668*4882a593Smuzhiyun     if (!dmxScreen->beDisplay) {
1669*4882a593Smuzhiyun         dmxLog(dmxWarning,
1670*4882a593Smuzhiyun                "Attempting to remove screen #%d but it has not been opened\n",
1671*4882a593Smuzhiyun                idx);
1672*4882a593Smuzhiyun         return 1;
1673*4882a593Smuzhiyun     }
1674*4882a593Smuzhiyun 
1675*4882a593Smuzhiyun     dmxLogOutput(dmxScreen, "Detaching screen #%d\n", idx);
1676*4882a593Smuzhiyun 
1677*4882a593Smuzhiyun     /* Detach input */
1678*4882a593Smuzhiyun     dmxInputDetachAll(dmxScreen);
1679*4882a593Smuzhiyun 
1680*4882a593Smuzhiyun     /* Save all relevant state (TODO) */
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun     /* Free all non-window resources related to this screen */
1683*4882a593Smuzhiyun     for (i = currentMaxClients; --i >= 0;)
1684*4882a593Smuzhiyun         if (clients[i])
1685*4882a593Smuzhiyun             FindAllClientResources(clients[i], dmxBEDestroyResources,
1686*4882a593Smuzhiyun                                    (void *) (uintptr_t) idx);
1687*4882a593Smuzhiyun 
1688*4882a593Smuzhiyun     /* Free scratch GCs */
1689*4882a593Smuzhiyun     dmxBEDestroyScratchGCs(idx);
1690*4882a593Smuzhiyun 
1691*4882a593Smuzhiyun     /* Free window resources related to this screen */
1692*4882a593Smuzhiyun     dmxBEDestroyWindowTree(idx);
1693*4882a593Smuzhiyun 
1694*4882a593Smuzhiyun     /* Free default stipple */
1695*4882a593Smuzhiyun     dmxBESavePixmap(screenInfo.screens[idx]->defaultStipple);
1696*4882a593Smuzhiyun     dmxBEFreePixmap(screenInfo.screens[idx]->defaultStipple);
1697*4882a593Smuzhiyun 
1698*4882a593Smuzhiyun     /* Free the remaining screen resources and close the screen */
1699*4882a593Smuzhiyun     dmxBECloseScreen(screenInfo.screens[idx]);
1700*4882a593Smuzhiyun 
1701*4882a593Smuzhiyun     /* Adjust the cursor boundaries (paints detached console window) */
1702*4882a593Smuzhiyun     dmxAdjustCursorBoundaries();
1703*4882a593Smuzhiyun 
1704*4882a593Smuzhiyun     return 0;                   /* Success */
1705*4882a593Smuzhiyun }
1706