xref: /petsc/src/sys/classes/draw/impls/x/xops.c (revision 15d5bc7987684e36335e48c93f0d6e15cde33d97)
1 /*
2     Defines the operations for the X PetscDraw implementation.
3 */
4 
5 #include <../src/sys/classes/draw/impls/x/ximpl.h>         /*I  "petscsys.h" I*/
6 
7 /*
8      These macros transform from the users coordinates to the  X-window pixel coordinates.
9 */
10 #define XTRANS(draw,xwin,x)  (int)(((xwin)->w)*((draw)->port_xl + (((x - (draw)->coor_xl)*((draw)->port_xr - (draw)->port_xl))/((draw)->coor_xr - (draw)->coor_xl))))
11 #define YTRANS(draw,xwin,y)  (int)(((xwin)->h)*(1.0-(draw)->port_yl - (((y - (draw)->coor_yl)*((draw)->port_yr - (draw)->port_yl))/((draw)->coor_yr - (draw)->coor_yl))))
12 
13 #define ITRANS(draw,xwin,i)  (draw)->coor_xl + (i*((draw)->coor_xr - (draw)->coor_xl)/((xwin)->w) - (draw)->port_xl)/((draw)->port_xr - (draw)->port_xl)
14 #define JTRANS(draw,xwin,j)  draw->coor_yl + (((double)j)/xwin->h + draw->port_yl - 1.0)*(draw->coor_yr - draw->coor_yl)/(draw->port_yl - draw->port_yr)
15 
16 #undef __FUNCT__
17 #define __FUNCT__ "PetscDrawCoordinateToPixel_X"
18 PetscErrorCode PetscDrawCoordinateToPixel_X(PetscDraw draw,PetscReal x,PetscReal y,PetscInt *i,PetscInt *j)
19 {
20   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
21 
22   PetscFunctionBegin;
23   *i = XTRANS(draw,XiWin,x);
24   *j = YTRANS(draw,XiWin,y);
25   PetscFunctionReturn(0);
26 }
27 
28 #undef __FUNCT__
29 #define __FUNCT__ "PetscDrawPixelToCoordinate_X"
30 PetscErrorCode PetscDrawPixelToCoordinate_X(PetscDraw draw,PetscInt i,PetscInt j,PetscReal *x,PetscReal *y)
31 {
32   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
33 
34   PetscFunctionBegin;
35   *x = ITRANS(draw,XiWin,i);
36   *y = JTRANS(draw,XiWin,j);
37   PetscFunctionReturn(0);
38 }
39 
40 #undef __FUNCT__
41 #define __FUNCT__ "PetscDrawLine_X"
42 PetscErrorCode PetscDrawLine_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
43 {
44   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
45   int         x1,y_1,x2,y2;
46 
47   PetscFunctionBegin;
48   PetscDrawXiSetColor(XiWin,cl);
49   x1  = XTRANS(draw,XiWin,xl);   x2  = XTRANS(draw,XiWin,xr);
50   y_1 = YTRANS(draw,XiWin,yl);   y2  = YTRANS(draw,XiWin,yr);
51   if (x1 == x2 && y_1 == y2) PetscFunctionReturn(0);
52   XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x1,y_1,x2,y2);
53   PetscFunctionReturn(0);
54 }
55 
56 #undef __FUNCT__
57 #define __FUNCT__ "PetscDrawArrow_X"
58 PetscErrorCode PetscDrawArrow_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
59 {
60   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
61   int         x1,y_1,x2,y2;
62 
63   PetscFunctionBegin;
64   PetscDrawXiSetColor(XiWin,cl);
65   x1  = XTRANS(draw,XiWin,xl);   x2  = XTRANS(draw,XiWin,xr);
66   y_1 = YTRANS(draw,XiWin,yl);   y2  = YTRANS(draw,XiWin,yr);
67   if (x1 == x2 && y_1 == y2) PetscFunctionReturn(0);
68   XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x1,y_1,x2,y2);
69   if (x1 == x2 && PetscAbs(y_1 - y2) > 7) {
70     if (y2 > y_1) {
71       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x2,y2,x2-3,y2-3);
72       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x2,y2,x2+3,y2-3);
73     } else {
74       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x2,y2,x2-3,y2+3);
75       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x2,y2,x2+3,y2+3);
76     }
77   }
78   PetscFunctionReturn(0);
79 }
80 
81 #undef __FUNCT__
82 #define __FUNCT__ "PetscDrawPoint_X"
83 static PetscErrorCode PetscDrawPoint_X(PetscDraw draw,PetscReal x,PetscReal y,int c)
84 {
85   int         xx,yy,i,j;
86   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
87 
88   PetscFunctionBegin;
89   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
90   PetscDrawXiSetColor(XiWin,c);
91   for (i=-1; i<2; i++) {
92     for (j=-1; j<2; j++) {
93       XDrawPoint(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx+i,yy+j);
94     }
95   }
96   PetscFunctionReturn(0);
97 }
98 
99 #undef __FUNCT__
100 #define __FUNCT__ "PetscDrawPointPixel_X"
101 static PetscErrorCode PetscDrawPointPixel_X(PetscDraw draw,PetscInt x,PetscInt y,int c)
102 {
103   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
104 
105   PetscFunctionBegin;
106   PetscDrawXiSetColor(XiWin,c);
107   XDrawPoint(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x,y);
108   PetscFunctionReturn(0);
109 }
110 
111 #undef __FUNCT__
112 #define __FUNCT__ "PetscDrawRectangle_X"
113 static PetscErrorCode PetscDrawRectangle_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c1,int c2,int c3,int c4)
114 {
115   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
116   int         x1,y_1,w,h,c = (c1 + c2 + c3 + c4)/4;
117 
118   PetscFunctionBegin;
119   PetscDrawXiSetColor(XiWin,c);
120   x1  = XTRANS(draw,XiWin,xl);   w  = XTRANS(draw,XiWin,xr) - x1;
121   y_1 = YTRANS(draw,XiWin,yr);   h  = YTRANS(draw,XiWin,yl) - y_1;
122   if (w <= 0) w = 1;
123   if (h <= 0) h = 1;
124   XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x1,y_1,w,h);
125   PetscFunctionReturn(0);
126 }
127 
128 #undef __FUNCT__
129 #define __FUNCT__ "PetscDrawEllipse_X"
130 static PetscErrorCode PetscDrawEllipse_X(PetscDraw Win, PetscReal x, PetscReal y, PetscReal a, PetscReal b, int c)
131 {
132   PetscDraw_X *XiWin = (PetscDraw_X*) Win->data;
133   int         xA, yA, w, h;
134 
135   PetscFunctionBegin;
136   PetscDrawXiSetColor(XiWin, c);
137   xA = XTRANS(Win, XiWin, x - a/2.0); w = XTRANS(Win, XiWin, x + a/2.0) - xA;
138   yA = YTRANS(Win, XiWin, y + b/2.0); h = PetscAbs(YTRANS(Win, XiWin, y - b/2.0) - yA);
139   XFillArc(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, xA, yA, w, h, 0, 23040);
140   PetscFunctionReturn(0);
141 }
142 
143 extern PetscErrorCode PetscDrawInterpolatedTriangle_X(PetscDraw_X*,int,int,int,int,int,int,int,int,int);
144 
145 #undef __FUNCT__
146 #define __FUNCT__ "PetscDrawTriangle_X"
147 static PetscErrorCode PetscDrawTriangle_X(PetscDraw draw,PetscReal X1,PetscReal Y_1,PetscReal X2,PetscReal Y2,PetscReal X3,PetscReal Y3,int c1,int c2,int c3)
148 {
149   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
150   PetscErrorCode ierr;
151 
152   PetscFunctionBegin;
153   if (c1 == c2 && c2 == c3) {
154     XPoint pt[3];
155     PetscDrawXiSetColor(XiWin,c1);
156     pt[0].x = XTRANS(draw,XiWin,X1);
157     pt[0].y = YTRANS(draw,XiWin,Y_1);
158     pt[1].x = XTRANS(draw,XiWin,X2);
159     pt[1].y = YTRANS(draw,XiWin,Y2);
160     pt[2].x = XTRANS(draw,XiWin,X3);
161     pt[2].y = YTRANS(draw,XiWin,Y3);
162     XFillPolygon(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,pt,3,Convex,CoordModeOrigin);
163   } else {
164     int x1,y_1,x2,y2,x3,y3;
165     x1   = XTRANS(draw,XiWin,X1);
166     y_1  = YTRANS(draw,XiWin,Y_1);
167     x2   = XTRANS(draw,XiWin,X2);
168     y2   = YTRANS(draw,XiWin,Y2);
169     x3   = XTRANS(draw,XiWin,X3);
170     y3   = YTRANS(draw,XiWin,Y3);
171     ierr = PetscDrawInterpolatedTriangle_X(XiWin,x1,y_1,c1,x2,y2,c2,x3,y3,c3);CHKERRQ(ierr);
172   }
173   PetscFunctionReturn(0);
174 }
175 
176 #undef __FUNCT__
177 #define __FUNCT__ "PetscDrawString_X"
178 static PetscErrorCode PetscDrawString_X(PetscDraw draw,PetscReal x,PetscReal y,int c,const char chrs[])
179 {
180   PetscErrorCode ierr;
181   int            xx,yy;
182   size_t         len;
183   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
184   char           *substr;
185   PetscToken     token;
186 
187   PetscFunctionBegin;
188   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
189   PetscDrawXiSetColor(XiWin,c);
190 
191   ierr = PetscTokenCreate(chrs,'\n',&token);CHKERRQ(ierr);
192   ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr);
193   ierr = PetscStrlen(substr,&len);CHKERRQ(ierr);
194   XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
195   ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr);
196   while (substr) {
197     yy  += 4*XiWin->font->font_descent;
198     ierr = PetscStrlen(substr,&len);CHKERRQ(ierr);
199     XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
200     ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr);
201   }
202   ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
203   PetscFunctionReturn(0);
204 }
205 
206 extern PetscErrorCode PetscDrawXiFontFixed(PetscDraw_X*,int,int,PetscDrawXiFont**);
207 
208 #undef __FUNCT__
209 #define __FUNCT__ "PetscDrawStringSetSize_X"
210 static PetscErrorCode PetscDrawStringSetSize_X(PetscDraw draw,PetscReal x,PetscReal y)
211 {
212   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
213   PetscErrorCode ierr;
214   int            w,h;
215 
216   PetscFunctionBegin;
217   w    = (int)((XiWin->w)*x*(draw->port_xr - draw->port_xl)/(draw->coor_xr - draw->coor_xl));
218   h    = (int)((XiWin->h)*y*(draw->port_yr - draw->port_yl)/(draw->coor_yr - draw->coor_yl));
219   ierr = PetscFree(XiWin->font);CHKERRQ(ierr);
220   ierr = PetscDrawXiFontFixed(XiWin,w,h,&XiWin->font);CHKERRQ(ierr);
221   PetscFunctionReturn(0);
222 }
223 
224 #undef __FUNCT__
225 #define __FUNCT__ "PetscDrawStringGetSize_X"
226 PetscErrorCode PetscDrawStringGetSize_X(PetscDraw draw,PetscReal *x,PetscReal  *y)
227 {
228   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
229   PetscReal   w,h;
230 
231   PetscFunctionBegin;
232   w = XiWin->font->font_w; h = XiWin->font->font_h;
233   if (x) *x = w*(draw->coor_xr - draw->coor_xl)/((XiWin->w)*(draw->port_xr - draw->port_xl));
234   if (y) *y = h*(draw->coor_yr - draw->coor_yl)/((XiWin->h)*(draw->port_yr - draw->port_yl));
235   PetscFunctionReturn(0);
236 }
237 
238 #undef __FUNCT__
239 #define __FUNCT__ "PetscDrawStringVertical_X"
240 PetscErrorCode PetscDrawStringVertical_X(PetscDraw draw,PetscReal x,PetscReal y,int c,const char chrs[])
241 {
242   PetscErrorCode ierr;
243   int            xx,yy;
244   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
245   char           tmp[2];
246   PetscReal      tw,th;
247   size_t         i,n;
248 
249   PetscFunctionBegin;
250   ierr   = PetscStrlen(chrs,&n);CHKERRQ(ierr);
251   tmp[1] = 0;
252   PetscDrawXiSetColor(XiWin,c);
253   ierr = PetscDrawStringGetSize_X(draw,&tw,&th);CHKERRQ(ierr);
254   xx   = XTRANS(draw,XiWin,x);
255   for (i=0; i<n; i++) {
256     tmp[0] = chrs[i];
257     yy     = YTRANS(draw,XiWin,y-th*i);
258     XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set, xx,yy - XiWin->font->font_descent,tmp,1);
259   }
260   PetscFunctionReturn(0);
261 }
262 
263 #undef __FUNCT__
264 #define __FUNCT__ "PetscDrawFlush_X"
265 static PetscErrorCode PetscDrawFlush_X(PetscDraw draw)
266 {
267   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;
268   PetscErrorCode ierr;
269 
270   PetscFunctionBegin;
271   if (XiWin->drw && XiWin->win) XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
272   XFlush(XiWin->disp);
273   XSync(XiWin->disp,False);
274   if (draw->saveonflush) {ierr = PetscDrawSave(draw);CHKERRQ(ierr);}
275   PetscFunctionReturn(0);
276 }
277 
278 #undef __FUNCT__
279 #define __FUNCT__ "PetscDrawSynchronizedFlush_X"
280 static PetscErrorCode PetscDrawSynchronizedFlush_X(PetscDraw draw)
281 {
282   PetscErrorCode ierr;
283   PetscMPIInt    rank;
284   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
285 
286   PetscFunctionBegin;
287   if (XiWin->drw && XiWin->win) {
288     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
289     /* make sure data has actually arrived at server */
290     XSync(XiWin->disp,False);
291     ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
292     if (!rank) {
293       XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
294       XFlush(XiWin->disp);
295       XSync(XiWin->disp,False);
296     }
297     ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
298   } else {
299     ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
300     XFlush(XiWin->disp);
301     XSync(XiWin->disp,False);
302     ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
303   }
304   if (draw->saveonflush) {ierr = PetscDrawSave(draw);CHKERRQ(ierr);}
305   PetscFunctionReturn(0);
306 }
307 
308 #undef __FUNCT__
309 #define __FUNCT__ "PetscDrawSetViewport_X"
310 static PetscErrorCode PetscDrawSetViewport_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
311 {
312   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
313   XRectangle  box;
314 
315   PetscFunctionBegin;
316   box.x     = (int)(xl*XiWin->w);     box.y      = (int)((1.0-yr)*XiWin->h);
317   box.width = (int)((xr-xl)*XiWin->w);box.height = (int)((yr-yl)*XiWin->h);
318   XSetClipRectangles(XiWin->disp,XiWin->gc.set,0,0,&box,1,Unsorted);
319   PetscFunctionReturn(0);
320 }
321 
322 #undef __FUNCT__
323 #define __FUNCT__ "PetscDrawClear_X"
324 static PetscErrorCode PetscDrawClear_X(PetscDraw draw)
325 {
326   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
327   int            x, y, w, h;
328   PetscErrorCode ierr;
329 
330   PetscFunctionBegin;
331   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
332   x    = (int)(draw->port_xl*XiWin->w);
333   w    = (int)((draw->port_xr - draw->port_xl)*XiWin->w);
334   y    = (int)((1.0-draw->port_yr)*XiWin->h);
335   h    = (int)((draw->port_yr - draw->port_yl)*XiWin->h);
336   PetscDrawXiSetPixVal(XiWin,XiWin->background);
337   XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x,y,w,h);
338   PetscFunctionReturn(0);
339 }
340 
341 #undef __FUNCT__
342 #define __FUNCT__ "PetscDrawSynchronizedClear_X"
343 static PetscErrorCode PetscDrawSynchronizedClear_X(PetscDraw draw)
344 {
345   PetscErrorCode ierr;
346   PetscMPIInt    rank;
347   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
348 
349   PetscFunctionBegin;
350   ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
351   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
352   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
353   if (!rank) {ierr = PetscDrawClear_X(draw);CHKERRQ(ierr);}
354   XFlush(XiWin->disp);
355   XSync(XiWin->disp,False);
356   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
357   ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
358   PetscFunctionReturn(0);
359 }
360 
361 #undef __FUNCT__
362 #define __FUNCT__ "PetscDrawSetDoubleBuffer_X"
363 static PetscErrorCode PetscDrawSetDoubleBuffer_X(PetscDraw draw)
364 {
365   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
366   PetscErrorCode ierr;
367   PetscMPIInt    rank;
368 
369   PetscFunctionBegin;
370   if (win->drw) PetscFunctionReturn(0);
371   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
372 
373   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
374   if (!rank) {
375     win->drw = XCreatePixmap(win->disp,RootWindow(win->disp,win->screen),win->w,win->h,win->depth);
376     /* try to make sure it is actually done before passing info to all */
377     XFlush(win->disp);
378     XSync(win->disp,False);
379   }
380   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
381 
382   ierr = MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
383   PetscFunctionReturn(0);
384 }
385 
386 #include <X11/cursorfont.h>
387 
388 #undef __FUNCT__
389 #define __FUNCT__ "PetscDrawGetMouseButton_X"
390 static PetscErrorCode PetscDrawGetMouseButton_X(PetscDraw draw,PetscDrawButton *button,PetscReal *x_user,PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys)
391 {
392   Cursor       cursor;
393   XEvent       report;
394   PetscDraw_X  *win = (PetscDraw_X*)draw->data;
395   Window       root,child;
396   int          root_x,root_y,px=0,py=0;
397   unsigned int w,h,border,depth;
398   unsigned int keys_button;
399 
400   PetscFunctionBegin;
401   *button = PETSC_BUTTON_NONE;
402   if (!win->win) PetscFunctionReturn(0);
403 
404   /* change cursor to indicate input */
405   cursor = XCreateFontCursor(win->disp,XC_hand2); if (!cursor) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to create X cursor");
406   XDefineCursor(win->disp,win->win,cursor);
407   /* wait for mouse button events */
408   XSelectInput(win->disp,win->win,ButtonPressMask | ButtonReleaseMask);
409   while (XCheckTypedEvent(win->disp,ButtonPress,&report));
410   XMaskEvent(win->disp,ButtonReleaseMask,&report);
411   switch (report.xbutton.button) {
412   case Button1:
413     if (report.xbutton.state & ShiftMask) *button = PETSC_BUTTON_LEFT_SHIFT;
414     else                                  *button = PETSC_BUTTON_LEFT;
415     break;
416   case Button2:
417     if (report.xbutton.state & ShiftMask) *button = PETSC_BUTTON_CENTER_SHIFT;
418     else                                  *button = PETSC_BUTTON_CENTER;
419     break;
420   case Button3:
421     if (report.xbutton.state & ShiftMask) *button = PETSC_BUTTON_RIGHT_SHIFT;
422     else                                  *button = PETSC_BUTTON_RIGHT;
423     break;
424   case Button4: *button = PETSC_BUTTON_WHEEL_UP;   break;
425   case Button5: *button = PETSC_BUTTON_WHEEL_DOWN; break;
426   }
427   /* get mouse pointer coordinates */
428   XQueryPointer(win->disp,report.xmotion.window,&root,&child,&root_x,&root_y,&px,&py,&keys_button);
429   /* cleanup input event handler and cursor  */
430   XSelectInput(win->disp,win->win,NoEventMask);
431   XUndefineCursor(win->disp,win->win);
432   XFreeCursor(win->disp, cursor);
433   XFlush(win->disp);
434   XSync(win->disp,False);
435   /* the user may resize the window before pressing the mouse button */
436   XGetGeometry(win->disp,win->win,&root,&root_x,&root_y,&w,&h,&border,&depth);
437 
438   if (x_phys) *x_phys = ((double)px)/((double)w);
439   if (y_phys) *y_phys = 1.0 - ((double)py)/((double)h);
440   if (x_user) *x_user = draw->coor_xl + ((((double)px)/((double)w)-draw->port_xl))*(draw->coor_xr - draw->coor_xl)/(draw->port_xr - draw->port_xl);
441   if (y_user) *y_user = draw->coor_yl + ((1.0 - ((double)py)/((double)h)-draw->port_yl))*(draw->coor_yr - draw->coor_yl)/(draw->port_yr - draw->port_yl);
442   PetscFunctionReturn(0);
443 }
444 
445 #undef __FUNCT__
446 #define __FUNCT__ "PetscDrawPause_X"
447 static PetscErrorCode PetscDrawPause_X(PetscDraw draw)
448 {
449   PetscErrorCode ierr;
450   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
451 
452   PetscFunctionBegin;
453   if (!win->win) PetscFunctionReturn(0);
454   if (draw->pause > 0) PetscSleep(draw->pause);
455   else if (draw->pause == -1) {
456     PetscDrawButton button = PETSC_BUTTON_NONE;
457     ierr = PetscDrawGetMouseButton(draw,&button,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
458     if (button == PETSC_BUTTON_CENTER) draw->pause = 0;
459   }
460   PetscFunctionReturn(0);
461 }
462 
463 #undef __FUNCT__
464 #define __FUNCT__ "PetscDrawGetPopup_X"
465 static PetscErrorCode PetscDrawGetPopup_X(PetscDraw draw,PetscDraw *popup)
466 {
467   PetscErrorCode ierr;
468   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
469   PetscBool      flg   = PETSC_TRUE;
470 
471   PetscFunctionBegin;
472   ierr = PetscOptionsGetBool(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-draw_popup",&flg,NULL);CHKERRQ(ierr);
473   if (flg) {
474     ierr = PetscDrawOpenX(PetscObjectComm((PetscObject)draw),NULL,NULL,win->x,win->y+win->h+36,220,220,popup);CHKERRQ(ierr);
475     draw->popup = *popup;
476   } else {
477     *popup = NULL;
478   }
479   PetscFunctionReturn(0);
480 }
481 
482 #undef __FUNCT__
483 #define __FUNCT__ "PetscDrawSetTitle_X"
484 static PetscErrorCode PetscDrawSetTitle_X(PetscDraw draw,const char title[])
485 {
486   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
487   XTextProperty  prop;
488   PetscErrorCode ierr;
489   size_t         len;
490 
491   PetscFunctionBegin;
492   if (win->win) {
493     XGetWMName(win->disp,win->win,&prop);
494     XFree((void*)prop.value);
495     prop.value  = (unsigned char*)title;
496     ierr        = PetscStrlen(title,&len);CHKERRQ(ierr);
497     prop.nitems = (long) len;
498     XSetWMName(win->disp,win->win,&prop);
499   }
500   PetscFunctionReturn(0);
501 }
502 
503 #undef __FUNCT__
504 #define __FUNCT__ "PetscDrawResizeWindow_X"
505 static PetscErrorCode PetscDrawResizeWindow_X(PetscDraw draw,int w,int h)
506 {
507   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
508   PetscMPIInt    rank;
509   PetscErrorCode ierr;
510 
511   PetscFunctionBegin;
512   if (!win->win) PetscFunctionReturn(0);
513   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
514   if (!rank) {
515     Window       root;
516     int          x,y;
517     unsigned int ww,hh,border,depth;
518     XResizeWindow(win->disp,win->win,w,h);
519     XGetGeometry(win->disp,win->win,&root,&x,&y,&ww,&hh,&border,&depth);
520   }
521   ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr);
522   PetscFunctionReturn(0);
523 }
524 
525 #undef __FUNCT__
526 #define __FUNCT__ "PetscDrawCheckResizedWindow_X"
527 static PetscErrorCode PetscDrawCheckResizedWindow_X(PetscDraw draw)
528 {
529   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
530   PetscErrorCode ierr;
531   int            xywh[4];
532   PetscMPIInt    rank;
533   PetscReal      xl,xr,yl,yr;
534   XRectangle     box;
535 
536   PetscFunctionBegin;
537   if (!win->win) PetscFunctionReturn(0);
538   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
539 
540   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
541   if (!rank) {
542     Window       root;
543     int          x,y;
544     unsigned int w,h,border,depth;
545     XGetGeometry(win->disp,win->win,&root,&x,&y,&w,&h,&border,&depth);
546     xywh[0] = x; xywh[1] = y; xywh[2] = (int)w; xywh[3] = (int)h;
547   }
548   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
549 
550   ierr = MPI_Bcast(xywh,4,MPI_INT,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
551   /* record new window position */
552   draw->x = win->x = xywh[0];
553   draw->y = win->y = xywh[1];
554   if (xywh[2] == win->w && xywh[3] == win->h) PetscFunctionReturn(0);
555   /* record new window sizes */
556   draw->w = win->w = xywh[2];
557   draw->h = win->h = xywh[3];
558 
559   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
560 
561   /* Free buffer space and create new version (only first processor does this) */
562   if (!rank && win->drw) {
563     XFreePixmap(win->disp,win->drw);
564     win->drw = XCreatePixmap(win->disp,RootWindow(win->disp,win->screen),win->w,win->h,win->depth);
565   }
566 
567   /* reset the clipping */
568   xl = draw->port_xl; yl = draw->port_yl;
569   xr = draw->port_xr; yr = draw->port_yr;
570   box.x     = (int)(xl*win->w);     box.y      = (int)((1.0-yr)*win->h);
571   box.width = (int)((xr-xl)*win->w);box.height = (int)((yr-yl)*win->h);
572   XSetClipRectangles(win->disp,win->gc.set,0,0,&box,1,Unsorted);
573 
574   /* try to make sure it is actually done before passing info to all */
575   XFlush(win->disp);
576   XSync(win->disp,False);
577 
578   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
579 
580   ierr = MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
581   PetscFunctionReturn(0);
582 }
583 
584 static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw,PetscDraw*);
585 static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw,PetscDraw*);
586 
587 #undef __FUNCT__
588 #define __FUNCT__ "PetscDrawXiClose"
589 static PetscErrorCode PetscDrawXiClose(PetscDraw_X *XiWin)
590 {
591   PetscErrorCode ierr;
592 
593   PetscFunctionBegin;
594   if (!XiWin) PetscFunctionReturn(0);
595   ierr = PetscFree(XiWin->font);CHKERRQ(ierr);
596   if (XiWin->disp) {
597 #if defined(PETSC_HAVE_SETJMP_H)
598     jmp_buf              jmpbuf;
599     PetscXIOErrorHandler xioerrhdl;
600     ierr = PetscMemcpy(&jmpbuf,&PetscXIOErrorHandlerJumpBuf,sizeof(jmpbuf));CHKERRQ(ierr);
601     xioerrhdl = PetscSetXIOErrorHandler(PetscXIOErrorHandlerJump);
602     if (!setjmp(PetscXIOErrorHandlerJumpBuf))
603 #endif
604     {
605       XFreeGC(XiWin->disp,XiWin->gc.set);
606       XCloseDisplay(XiWin->disp);
607     }
608     XiWin->disp = NULL;
609 #if defined(PETSC_HAVE_SETJMP_H)
610     (void)PetscSetXIOErrorHandler(xioerrhdl);
611     ierr = PetscMemcpy(&PetscXIOErrorHandlerJumpBuf,&jmpbuf,sizeof(jmpbuf));CHKERRQ(ierr);
612 #endif
613   }
614   PetscFunctionReturn(0);
615 }
616 
617 #undef __FUNCT__
618 #define __FUNCT__ "PetscDrawDestroy_X"
619 PetscErrorCode PetscDrawDestroy_X(PetscDraw draw)
620 {
621   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
622   PetscErrorCode ierr;
623 
624   PetscFunctionBegin;
625   ierr = PetscDrawDestroy(&draw->popup);CHKERRQ(ierr);
626   ierr = PetscDrawXiClose(win);CHKERRQ(ierr);
627   ierr = PetscFree(draw->data);CHKERRQ(ierr);
628   PetscFunctionReturn(0);
629 }
630 
631 PetscErrorCode PetscDrawSave_X(PetscDraw);
632 PetscErrorCode PetscDrawSetSave_X(PetscDraw,const char*);
633 
634 static struct _PetscDrawOps DvOps = { PetscDrawSetDoubleBuffer_X,
635                                       PetscDrawFlush_X,
636                                       PetscDrawLine_X,
637                                       0,
638                                       0,
639                                       PetscDrawPoint_X,
640                                       0,
641                                       PetscDrawString_X,
642                                       PetscDrawStringVertical_X,
643                                       PetscDrawStringSetSize_X,
644                                       PetscDrawStringGetSize_X,
645                                       PetscDrawSetViewport_X,
646                                       PetscDrawClear_X,
647                                       PetscDrawSynchronizedFlush_X,
648                                       PetscDrawRectangle_X,
649                                       PetscDrawTriangle_X,
650                                       PetscDrawEllipse_X,
651                                       PetscDrawGetMouseButton_X,
652                                       PetscDrawPause_X,
653                                       PetscDrawSynchronizedClear_X,
654                                       0,
655                                       0,
656                                       PetscDrawGetPopup_X,
657                                       PetscDrawSetTitle_X,
658                                       PetscDrawCheckResizedWindow_X,
659                                       PetscDrawResizeWindow_X,
660                                       PetscDrawDestroy_X,
661                                       0,
662                                       PetscDrawGetSingleton_X,
663                                       PetscDrawRestoreSingleton_X,
664 #if defined(PETSC_HAVE_AFTERIMAGE)
665                                       PetscDrawSave_X,
666 #else
667                                       0,
668 #endif
669                                       PetscDrawSetSave_X,
670                                       0,
671                                       PetscDrawArrow_X,
672                                       PetscDrawCoordinateToPixel_X,
673                                       PetscDrawPixelToCoordinate_X,
674                                       PetscDrawPointPixel_X,
675                                       0};
676 
677 
678 extern PetscErrorCode PetscDrawXiInitialize(PetscDraw_X*,const char[]);
679 extern PetscErrorCode PetscDrawXiColormap(PetscDraw_X*);
680 extern PetscErrorCode PetscDrawXiQuickWindow(PetscDraw_X*,char*,int,int,int,int);
681 extern PetscErrorCode PetscDrawXiQuickWindowFromWindow(PetscDraw_X*,Window);
682 
683 #undef __FUNCT__
684 #define __FUNCT__ "PetscDrawGetSingleton_X"
685 static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw draw,PetscDraw *sdraw)
686 {
687   PetscErrorCode ierr;
688   PetscDraw_X    *Xwin = (PetscDraw_X*)draw->data,*sXwin;
689 
690   PetscFunctionBegin;
691   ierr = PetscDrawCreate(PETSC_COMM_SELF,draw->display,draw->title,draw->x,draw->y,draw->w,draw->h,sdraw);CHKERRQ(ierr);
692   ierr = PetscObjectChangeTypeName((PetscObject)*sdraw,PETSC_DRAW_X);CHKERRQ(ierr);
693   ierr = PetscMemcpy((*sdraw)->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr);
694   (*sdraw)->ops->destroy = NULL;
695 
696   (*sdraw)->pause   = draw->pause;
697   (*sdraw)->coor_xl = draw->coor_xl;
698   (*sdraw)->coor_xr = draw->coor_xr;
699   (*sdraw)->coor_yl = draw->coor_yl;
700   (*sdraw)->coor_yr = draw->coor_yr;
701   (*sdraw)->port_xl = draw->port_xl;
702   (*sdraw)->port_xr = draw->port_xr;
703   (*sdraw)->port_yl = draw->port_yl;
704   (*sdraw)->port_yr = draw->port_yr;
705 
706   if (draw->popup) {
707     ierr = PetscObjectReference((PetscObject)draw->popup);CHKERRQ(ierr);
708     (*sdraw)->popup = draw->popup;
709   }
710 
711   /* actually create and open the window */
712   ierr = PetscNewLog(*sdraw,&sXwin);CHKERRQ(ierr);
713   (*sdraw)->data = (void*)sXwin;
714   ierr = PetscDrawXiInitialize(sXwin,draw->display);CHKERRQ(ierr);
715   if (Xwin->win) {ierr = PetscDrawXiQuickWindowFromWindow(sXwin,Xwin->win);CHKERRQ(ierr);}
716   if (Xwin->drw) {sXwin->drw = Xwin->drw; ierr = PetscDrawXiColormap(sXwin);CHKERRQ(ierr);}
717   PetscFunctionReturn(0);
718 }
719 
720 #undef __FUNCT__
721 #define __FUNCT__ "PetscDrawRestoreSingleton_X"
722 static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw draw,PetscDraw *sdraw)
723 {
724   PetscErrorCode ierr;
725   PetscDraw_X    *sXwin = (PetscDraw_X*)(*sdraw)->data;
726 
727   PetscFunctionBegin;
728   ierr = PetscDrawDestroy(&(*sdraw)->popup);CHKERRQ(ierr);
729   ierr = PetscFree((*sdraw)->title);CHKERRQ(ierr);
730   ierr = PetscFree((*sdraw)->display);CHKERRQ(ierr);
731   ierr = PetscDrawXiClose(sXwin);CHKERRQ(ierr);
732   ierr = PetscFree(sXwin);CHKERRQ(ierr);
733   ierr = PetscHeaderDestroy(sdraw);CHKERRQ(ierr);
734   PetscFunctionReturn(0);
735 }
736 
737 #undef __FUNCT__
738 #define __FUNCT__ "PetscDrawXGetDisplaySize_Private"
739 PetscErrorCode PetscDrawXGetDisplaySize_Private(const char name[],int *width,int *height)
740 {
741   Display *display;
742 
743   PetscFunctionBegin;
744   display = XOpenDisplay(name);
745   if (!display) {
746     *width  = *height = 0;
747     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to open display on %s\n\
748     Make sure your COMPUTE NODES are authorized to connect \n\
749     to this X server and either your DISPLAY variable\n\
750     is set or you use the -display name option\n",name);
751   }
752   *width  = (int)DisplayWidth(display,DefaultScreen(display));
753   *height = (int)DisplayHeight(display,DefaultScreen(display));
754   XCloseDisplay(display);
755   PetscFunctionReturn(0);
756 }
757 
758 /*MC
759      PETSC_DRAW_X  - PETSc graphics device that uses either X windows or its virtual version Xvfb
760 
761    Options Database Keys:
762 +  -display <display> - sets the display to use
763 .  -x_virtual - forces use of a X virtual display Xvfb that will not display anything but -draw_save will still work.
764                 Xvfb is automatically started up in PetscSetDisplay() with this option
765 .  -draw_size w,h - percentage of screeen (either 1, .5, .3, .25), or size in pixels
766 .  -geometry x,y,w,h - set location and size in pixels
767 .  -draw_virtual - do not open a window (draw on a pixmap), -draw_save will still work
768 -  -draw_double_buffer - avoid window flickering (draw on pixmap and flush to window)
769 
770    Level: beginner
771 
772 .seealso:  PetscDrawOpenX(), PetscDrawSetDisplay(), PetscDrawSetFromOptions()
773 
774 M*/
775 
776 #undef __FUNCT__
777 #define __FUNCT__ "PetscDrawCreate_X"
778 PETSC_EXTERN PetscErrorCode PetscDrawCreate_X(PetscDraw draw)
779 {
780   PetscDraw_X    *Xwin;
781   PetscErrorCode ierr;
782   PetscMPIInt    rank;
783   int            x = draw->x,y = draw->y,w = draw->w,h = draw->h;
784   static int     xavailable = 0,yavailable = 0,ybottom = 0,xmax = 0,ymax = 0;
785   PetscBool      set,virtual = PETSC_FALSE,doublebuffer = PETSC_TRUE;
786   PetscInt       xywh[4],osize = 4,nsizes=2;
787   PetscReal      sizes[2] = {.3,.3};
788 
789   PetscFunctionBegin;
790   /* get the display variable */
791   if (!draw->display) {
792     ierr = PetscMalloc1(256,&draw->display);CHKERRQ(ierr);
793     ierr = PetscGetDisplay(draw->display,256);CHKERRQ(ierr);
794   }
795 
796   /* initialize the display size */
797   if (!xmax) {
798     ierr = PetscDrawXGetDisplaySize_Private(draw->display,&xmax,&ymax);
799     /* if some processors fail on this and others succed then this is a problem ! */
800     if (ierr) {
801       (*PetscErrorPrintf)("PETSc unable to use X windows\nproceeding without graphics\n");
802       ierr = PetscDrawSetType(draw,PETSC_DRAW_NULL);CHKERRQ(ierr);
803       PetscFunctionReturn(0);
804     }
805   }
806 
807   /* allow user to set size of drawable */
808   ierr = PetscOptionsGetRealArray(((PetscObject)draw)->options,NULL,"-draw_size",sizes,&nsizes,&set);CHKERRQ(ierr);
809   if (set && nsizes == 1 && sizes[0] > 1.0) sizes[1] = sizes[0];
810   if (set) {
811     if (sizes[0] > 1.0)       w = (int)sizes[0];
812     else if (sizes[0] == 1.0) w = PETSC_DRAW_FULL_SIZE;
813     else if (sizes[0] == .5)  w = PETSC_DRAW_HALF_SIZE;
814     else if (sizes[0] == .3)  w = PETSC_DRAW_THIRD_SIZE;
815     else if (sizes[0] == .25) w = PETSC_DRAW_QUARTER_SIZE;
816     if (sizes[1] > 1.0)       h = (int)sizes[1];
817     else if (sizes[1] == 1.0) h = PETSC_DRAW_FULL_SIZE;
818     else if (sizes[1] == .5)  h = PETSC_DRAW_HALF_SIZE;
819     else if (sizes[1] == .3)  h = PETSC_DRAW_THIRD_SIZE;
820     else if (sizes[1] == .25) h = PETSC_DRAW_QUARTER_SIZE;
821   }
822   if (w == PETSC_DECIDE || w == PETSC_DEFAULT) w = draw->w = 300;
823   if (h == PETSC_DECIDE || h == PETSC_DEFAULT) h = draw->h = 300;
824   switch (w) {
825   case PETSC_DRAW_FULL_SIZE:    w = draw->w = (xmax - 10);   break;
826   case PETSC_DRAW_HALF_SIZE:    w = draw->w = (xmax - 20)/2; break;
827   case PETSC_DRAW_THIRD_SIZE:   w = draw->w = (xmax - 30)/3; break;
828   case PETSC_DRAW_QUARTER_SIZE: w = draw->w = (xmax - 40)/4; break;
829   }
830   switch (h) {
831   case PETSC_DRAW_FULL_SIZE:    h = draw->h = (ymax - 10);   break;
832   case PETSC_DRAW_HALF_SIZE:    h = draw->h = (ymax - 20)/2; break;
833   case PETSC_DRAW_THIRD_SIZE:   h = draw->h = (ymax - 30)/3; break;
834   case PETSC_DRAW_QUARTER_SIZE: h = draw->h = (ymax - 40)/4; break;
835   }
836 
837   ierr = PetscOptionsGetBool(((PetscObject)draw)->options,NULL,"-draw_virtual",&virtual,NULL);CHKERRQ(ierr);
838 
839   if (!virtual) {
840 
841     /* allow user to set location and size of window */
842     xywh[0] = x; xywh[1] = y; xywh[2] = w; xywh[3] = h;
843     ierr = PetscOptionsGetIntArray(((PetscObject)draw)->options,NULL,"-geometry",xywh,&osize,NULL);CHKERRQ(ierr);
844     x = (int)xywh[0]; y = (int)xywh[1]; w = (int)xywh[2]; h = (int)xywh[3];
845     if (w == PETSC_DECIDE || w == PETSC_DEFAULT) w = 300;
846     if (h == PETSC_DECIDE || h == PETSC_DEFAULT) h = 300;
847     draw->x = x; draw->y = y; draw->w = w; draw->h = h;
848 
849     if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) {
850       /*
851        PETSc tries to place windows starting in the upper left corner
852         and moving across to the right.
853 
854        0,0+----------------------------------------------+
855        |  Region used so far  +xavailable,yavailable  |
856        |                      |                       |
857        |                      |                       |
858        +--------------------- +ybottom                |
859        |                                              |
860        |                                              |
861        +----------------------------------------------+xmax,ymax
862 
863        */
864       /*  First: can we add it to the right? */
865       if (xavailable+w+10 <= xmax) {
866         x       = xavailable;
867         y       = yavailable;
868         ybottom = PetscMax(ybottom,y + h + 30);
869       } else {
870         /* No, so add it below on the left */
871         x          = 0;
872         xavailable = 0;
873         y          = ybottom;
874         yavailable = ybottom;
875         ybottom    = ybottom + h + 30;
876       }
877     }
878     /* update available region */
879     xavailable = PetscMax(xavailable,x + w + 10);
880     if (xavailable >= xmax) {
881       xavailable = 0;
882       yavailable = yavailable + h + 30;
883       ybottom    = yavailable;
884     }
885     if (yavailable >= ymax) {
886       y          = 0;
887       yavailable = 0;
888       ybottom    = 0;
889     }
890 
891   }/*endif(!virtual)*/
892 
893   ierr = PetscNewLog(draw,&Xwin);CHKERRQ(ierr);
894   ierr = PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr);
895   draw->data = (void*)Xwin;
896 
897   ierr = PetscDrawXiInitialize(Xwin,draw->display);CHKERRQ(ierr);
898   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
899   if (!virtual) {
900     Xwin->x = x; Xwin->y = y;
901     Xwin->w = w; Xwin->h = h;
902     if (!rank) {
903       if (w <= 0 || h <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative window width or height");
904       ierr = PetscDrawXiQuickWindow(Xwin,draw->title,x,y,w,h);CHKERRQ(ierr);
905       ierr = MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
906     } else {
907       ierr = MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
908       ierr = PetscDrawXiQuickWindowFromWindow(Xwin,Xwin->win);CHKERRQ(ierr);
909     }
910   } else {
911     Xwin->x = 0; Xwin->y = 0;
912     Xwin->w = w; Xwin->h = h;
913     ierr = PetscDrawXiColormap(Xwin);CHKERRQ(ierr);
914     if (!rank) {
915       if (w <= 0 || h <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative pixmap width or height");
916       Xwin->drw = XCreatePixmap(Xwin->disp,RootWindow(Xwin->disp,Xwin->screen),Xwin->w,Xwin->h,Xwin->depth);
917       XFlush(Xwin->disp);
918       XSync(Xwin->disp,False);
919     }
920     ierr = MPI_Bcast(&Xwin->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
921   }
922   draw->x = Xwin->x; draw->y = Xwin->y;
923   draw->w = Xwin->w; draw->h = Xwin->h;
924 
925   /*
926     Need barrier here so processor 0 does not destroy the window before other
927     processors have completed PetscDrawXiQuickWindow()
928   */
929   ierr = PetscDrawSynchronizedFlush(draw);CHKERRQ(ierr);
930 
931   ierr = PetscOptionsGetBool(((PetscObject)draw)->options,NULL,"-draw_double_buffer",&doublebuffer,NULL);CHKERRQ(ierr);
932   if (doublebuffer) {ierr = PetscDrawSetDoubleBuffer(draw);CHKERRQ(ierr);}
933   PetscFunctionReturn(0);
934 }
935