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