xref: /petsc/src/sys/classes/draw/impls/x/xops.c (revision e3d8bae51cf6992097be94a398154bd49ed41a98)
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;
124   if (h <= 0) h = 1;
125   XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x1,y_1,w,h);
126   PetscFunctionReturn(0);
127 }
128 
129 #undef __FUNCT__
130 #define __FUNCT__ "PetscDrawEllipse_X"
131 static PetscErrorCode PetscDrawEllipse_X(PetscDraw Win, PetscReal x, PetscReal y, PetscReal a, PetscReal b, int c)
132 {
133   PetscDraw_X *XiWin = (PetscDraw_X*) Win->data;
134   int         xA, yA, w, h;
135 
136   PetscFunctionBegin;
137   PetscDrawXiSetColor(XiWin, c);
138   xA = XTRANS(Win, XiWin, x - a/2.0); w = XTRANS(Win, XiWin, x + a/2.0) - xA;
139   yA = YTRANS(Win, XiWin, y + b/2.0); h = PetscAbs(YTRANS(Win, XiWin, y - b/2.0) - yA);
140   XFillArc(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, xA, yA, w, h, 0, 23040);
141   PetscFunctionReturn(0);
142 }
143 
144 extern PetscErrorCode PetscDrawInterpolatedTriangle_X(PetscDraw_X*,int,int,int,int,int,int,int,int,int);
145 
146 #undef __FUNCT__
147 #define __FUNCT__ "PetscDrawTriangle_X"
148 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)
149 {
150   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
151   PetscErrorCode ierr;
152 
153   PetscFunctionBegin;
154   if (c1 == c2 && c2 == c3) {
155     XPoint pt[3];
156     PetscDrawXiSetColor(XiWin,c1);
157     pt[0].x = XTRANS(draw,XiWin,X1);
158     pt[0].y = YTRANS(draw,XiWin,Y_1);
159     pt[1].x = XTRANS(draw,XiWin,X2);
160     pt[1].y = YTRANS(draw,XiWin,Y2);
161     pt[2].x = XTRANS(draw,XiWin,X3);
162     pt[2].y = YTRANS(draw,XiWin,Y3);
163     XFillPolygon(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,pt,3,Convex,CoordModeOrigin);
164   } else {
165     int x1,y_1,x2,y2,x3,y3;
166     x1   = XTRANS(draw,XiWin,X1);
167     y_1  = YTRANS(draw,XiWin,Y_1);
168     x2   = XTRANS(draw,XiWin,X2);
169     y2   = YTRANS(draw,XiWin,Y2);
170     x3   = XTRANS(draw,XiWin,X3);
171     y3   = YTRANS(draw,XiWin,Y3);
172     ierr = PetscDrawInterpolatedTriangle_X(XiWin,x1,y_1,c1,x2,y2,c2,x3,y3,c3);CHKERRQ(ierr);
173   }
174   PetscFunctionReturn(0);
175 }
176 
177 #undef __FUNCT__
178 #define __FUNCT__ "PetscDrawString_X"
179 static PetscErrorCode PetscDrawString_X(PetscDraw draw,PetscReal x,PetscReal y,int c,const char chrs[])
180 {
181   PetscErrorCode ierr;
182   int            xx,yy;
183   size_t         len;
184   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
185   char           *substr;
186   PetscToken     token;
187 
188   PetscFunctionBegin;
189   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
190   PetscDrawXiSetColor(XiWin,c);
191 
192   ierr = PetscTokenCreate(chrs,'\n',&token);CHKERRQ(ierr);
193   ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr);
194   ierr = PetscStrlen(substr,&len);CHKERRQ(ierr);
195   XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
196   ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr);
197   while (substr) {
198     yy  += 4*XiWin->font->font_descent;
199     ierr = PetscStrlen(substr,&len);CHKERRQ(ierr);
200     XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
201     ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr);
202   }
203   ierr = PetscTokenDestroy(&token);CHKERRQ(ierr);
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   PetscErrorCode ierr;
270 
271   PetscFunctionBegin;
272   if (XiWin->drw && XiWin->win) XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
273   XFlush(XiWin->disp);
274   XSync(XiWin->disp,False);
275   if (draw->saveonflush) {ierr = PetscDrawSave(draw);CHKERRQ(ierr);}
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(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
290     /* make sure data has actually arrived at server */
291     XSync(XiWin->disp,False);
292     ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));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(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
299   } else {
300     ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
301     XFlush(XiWin->disp);
302     XSync(XiWin->disp,False);
303     ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
304   }
305   if (draw->saveonflush) {ierr = PetscDrawSave(draw);CHKERRQ(ierr);}
306   PetscFunctionReturn(0);
307 }
308 
309 #undef __FUNCT__
310 #define __FUNCT__ "PetscDrawSetViewport_X"
311 static PetscErrorCode PetscDrawSetViewport_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
312 {
313   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
314   XRectangle  box;
315 
316   PetscFunctionBegin;
317   box.x     = (int)(xl*XiWin->w);     box.y      = (int)((1.0-yr)*XiWin->h);
318   box.width = (int)((xr-xl)*XiWin->w);box.height = (int)((yr-yl)*XiWin->h);
319   XSetClipRectangles(XiWin->disp,XiWin->gc.set,0,0,&box,1,Unsorted);
320   PetscFunctionReturn(0);
321 }
322 
323 #undef __FUNCT__
324 #define __FUNCT__ "PetscDrawClear_X"
325 static PetscErrorCode PetscDrawClear_X(PetscDraw draw)
326 {
327   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
328   int            x, y, w, h;
329   PetscErrorCode ierr;
330 
331   PetscFunctionBegin;
332   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
333   x    = (int)(draw->port_xl*XiWin->w);
334   w    = (int)((draw->port_xr - draw->port_xl)*XiWin->w);
335   y    = (int)((1.0-draw->port_yr)*XiWin->h);
336   h    = (int)((draw->port_yr - draw->port_yl)*XiWin->h);
337   PetscDrawXiSetPixVal(XiWin,XiWin->background);
338   XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x,y,w,h);
339   PetscFunctionReturn(0);
340 }
341 
342 #undef __FUNCT__
343 #define __FUNCT__ "PetscDrawSynchronizedClear_X"
344 static PetscErrorCode PetscDrawSynchronizedClear_X(PetscDraw draw)
345 {
346   PetscErrorCode ierr;
347   PetscMPIInt    rank;
348   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
349 
350   PetscFunctionBegin;
351   ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
352   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
353   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
354   if (!rank) {ierr = PetscDrawClear_X(draw);CHKERRQ(ierr);}
355   XFlush(XiWin->disp);
356   XSync(XiWin->disp,False);
357   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
358   ierr = MPI_Barrier(PetscObjectComm((PetscObject)draw));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   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
373 
374   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
375   if (!rank) win->drw = XCreatePixmap(win->disp,win->win,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   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
380 
381   ierr = MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
382   PetscFunctionReturn(0);
383 }
384 
385 #include <X11/cursorfont.h>
386 
387 #undef __FUNCT__
388 #define __FUNCT__ "PetscDrawGetMouseButton_X"
389 static PetscErrorCode PetscDrawGetMouseButton_X(PetscDraw draw,PetscDrawButton *button,PetscReal *x_user,PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys)
390 {
391   Cursor       cursor;
392   XEvent       report;
393   PetscDraw_X  *win = (PetscDraw_X*)draw->data;
394   Window       root,child;
395   int          root_x,root_y,px=0,py=0;
396   unsigned int keys_button;
397 
398   PetscFunctionBegin;
399   *button = PETSC_BUTTON_NONE;
400   if (!win->win) PetscFunctionReturn(0);
401 
402   /* change cursor to indicate input */
403   cursor = XCreateFontCursor(win->disp,XC_hand2); if (!cursor) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to create X cursor");
404   XDefineCursor(win->disp,win->win,cursor);
405   /* wait for mouse button events */
406   XSelectInput(win->disp,win->win,ButtonPressMask | ButtonReleaseMask);
407   while (XCheckTypedEvent(win->disp,ButtonPress,&report));
408   XMaskEvent(win->disp,ButtonReleaseMask,&report);
409   switch (report.xbutton.button) {
410   case Button1:
411     if (report.xbutton.state & ShiftMask) *button = PETSC_BUTTON_LEFT_SHIFT;
412     else                                  *button = PETSC_BUTTON_LEFT;
413     break;
414   case Button2:
415     if (report.xbutton.state & ShiftMask) *button = PETSC_BUTTON_CENTER_SHIFT;
416     else                                  *button = PETSC_BUTTON_CENTER;
417     break;
418   case Button3:
419     if (report.xbutton.state & ShiftMask) *button = PETSC_BUTTON_RIGHT_SHIFT;
420     else                                  *button = PETSC_BUTTON_RIGHT;
421     break;
422   case Button4: *button = PETSC_BUTTON_WHEEL_UP;   break;
423   case Button5: *button = PETSC_BUTTON_WHEEL_DOWN; break;
424   }
425   /* get mouse pointer coordinates */
426   XQueryPointer(win->disp,report.xmotion.window,&root,&child,&root_x,&root_y,&px,&py,&keys_button);
427   /* cleanup input event handler and cursor  */
428   XSelectInput(win->disp,win->win,NoEventMask);
429   XUndefineCursor(win->disp,win->win);
430   XFreeCursor(win->disp, cursor);
431   XFlush(win->disp);
432   XSync(win->disp,False);
433 
434   if (x_phys) *x_phys = ((double)px)/((double)win->w);
435   if (y_phys) *y_phys = 1.0 - ((double)py)/((double)win->h);
436   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);
437   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);
438   PetscFunctionReturn(0);
439 }
440 
441 #undef __FUNCT__
442 #define __FUNCT__ "PetscDrawPause_X"
443 static PetscErrorCode PetscDrawPause_X(PetscDraw draw)
444 {
445   PetscErrorCode ierr;
446   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
447 
448   PetscFunctionBegin;
449   if (!win->win) PetscFunctionReturn(0);
450   if (draw->pause > 0) PetscSleep(draw->pause);
451   else if (draw->pause == -1) {
452     PetscMPIInt     rank;
453     PetscDrawButton button = PETSC_BUTTON_NONE;
454     ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
455     ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
456     if (!rank) {
457       ierr = PetscDrawGetMouseButton(draw,&button,NULL,NULL,NULL,NULL);CHKERRQ(ierr);
458       if (button == PETSC_BUTTON_CENTER) draw->pause = 0;
459     }
460     ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
461     ierr = MPI_Bcast(&draw->pause,1,MPIU_REAL,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
462   }
463   PetscFunctionReturn(0);
464 }
465 
466 #undef __FUNCT__
467 #define __FUNCT__ "PetscDrawGetPopup_X"
468 static PetscErrorCode PetscDrawGetPopup_X(PetscDraw draw,PetscDraw *popup)
469 {
470   PetscErrorCode ierr;
471   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
472   PetscBool      flg   = PETSC_TRUE;
473 
474   PetscFunctionBegin;
475   ierr = PetscOptionsGetBool(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-draw_popup",&flg,NULL);CHKERRQ(ierr);
476   if (flg) {
477     ierr = PetscDrawOpenX(PetscObjectComm((PetscObject)draw),NULL,NULL,win->x,win->y+win->h+36,220,220,popup);CHKERRQ(ierr);
478     draw->popup = *popup;
479   } else {
480     *popup = NULL;
481   }
482   PetscFunctionReturn(0);
483 }
484 
485 #undef __FUNCT__
486 #define __FUNCT__ "PetscDrawSetTitle_X"
487 static PetscErrorCode PetscDrawSetTitle_X(PetscDraw draw,const char title[])
488 {
489   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
490   XTextProperty  prop;
491   PetscErrorCode ierr;
492   size_t         len;
493 
494   PetscFunctionBegin;
495   if (win->win) {
496     XGetWMName(win->disp,win->win,&prop);
497     XFree((void*)prop.value);
498     prop.value  = (unsigned char*)title;
499     ierr        = PetscStrlen(title,&len);CHKERRQ(ierr);
500     prop.nitems = (long) len;
501     XSetWMName(win->disp,win->win,&prop);
502   }
503   PetscFunctionReturn(0);
504 }
505 
506 #undef __FUNCT__
507 #define __FUNCT__ "PetscDrawResizeWindow_X"
508 static PetscErrorCode PetscDrawResizeWindow_X(PetscDraw draw,int w,int h)
509 {
510   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
511   PetscMPIInt    rank;
512   PetscErrorCode ierr;
513 
514   PetscFunctionBegin;
515   if (!win->win) PetscFunctionReturn(0);
516   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
517   if (!rank) {
518     Window       root;
519     int          x,y;
520     unsigned int ww,hh,border,depth;
521     XResizeWindow(win->disp,win->win,w,h);
522     XGetGeometry(win->disp,win->win,&root,&x,&y,&ww,&hh,&border,&depth);
523   }
524   ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr);
525   PetscFunctionReturn(0);
526 }
527 
528 #undef __FUNCT__
529 #define __FUNCT__ "PetscDrawCheckResizedWindow_X"
530 static PetscErrorCode PetscDrawCheckResizedWindow_X(PetscDraw draw)
531 {
532   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
533   PetscErrorCode ierr;
534   int            x,y;
535   PetscMPIInt    rank;
536   Window         root;
537   unsigned int   w,h,border,depth,geo[2];
538   PetscReal      xl,xr,yl,yr;
539   XRectangle     box;
540 
541   PetscFunctionBegin;
542   if (!win->win) PetscFunctionReturn(0);
543   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
544 
545   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
546   if (!rank) {
547     XFlush(win->disp);
548     XSync(win->disp,False);
549     XGetGeometry(win->disp,win->win,&root,&x,&y,geo,geo+1,&border,&depth);
550   }
551   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
552 
553   ierr = MPI_Bcast(geo,2,MPI_UNSIGNED,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
554   w    = geo[0];
555   h    = geo[1];
556   if (w == (unsigned int) win->w && h == (unsigned int) win->h) PetscFunctionReturn(0);
557 
558   /* record new window sizes */
559   win->h = h; win->w = w;
560 
561   ierr = PetscDrawCollectiveBegin(draw);CHKERRQ(ierr);
562 
563   /* Free buffer space and create new version (only first processor does this) */
564   if (!rank && win->drw) {
565     XFreePixmap(win->disp, win->drw);
566     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
567   }
568 
569   /* reset the clipping */
570   xl = draw->port_xl; yl = draw->port_yl;
571   xr = draw->port_xr; yr = draw->port_yr;
572   box.x     = (int)(xl*win->w);     box.y      = (int)((1.0-yr)*win->h);
573   box.width = (int)((xr-xl)*win->w);box.height = (int)((yr-yl)*win->h);
574   XSetClipRectangles(win->disp,win->gc.set,0,0,&box,1,Unsorted);
575 
576   /* try to make sure it is actually done before passing info to all */
577   XFlush(win->disp);
578   XSync(win->disp,False);
579 
580   ierr = PetscDrawCollectiveEnd(draw);CHKERRQ(ierr);
581 
582   ierr = MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
583   PetscFunctionReturn(0);
584 }
585 
586 static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw,PetscDraw*);
587 static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw,PetscDraw*);
588 
589 #undef __FUNCT__
590 #define __FUNCT__ "PetscDrawXiClose"
591 static PetscErrorCode PetscDrawXiClose(PetscDraw_X *XiWin)
592 {
593   PetscErrorCode ierr;
594 
595   PetscFunctionBegin;
596   if (!XiWin) PetscFunctionReturn(0);
597   ierr = PetscFree(XiWin->font);CHKERRQ(ierr);
598   if (XiWin->disp) {
599 #if defined(PETSC_HAVE_SETJMP_H)
600     jmp_buf              jmpbuf;
601     PetscXIOErrorHandler xioerrhdl;
602     ierr = PetscMemcpy(&jmpbuf,&PetscXIOErrorHandlerJumpBuf,sizeof(jmpbuf));CHKERRQ(ierr);
603     xioerrhdl = PetscSetXIOErrorHandler(PetscXIOErrorHandlerJump);
604     if (!setjmp(PetscXIOErrorHandlerJumpBuf))
605 #endif
606     {
607       XFreeGC(XiWin->disp,XiWin->gc.set);
608       XCloseDisplay(XiWin->disp);
609     }
610     XiWin->disp = NULL;
611 #if defined(PETSC_HAVE_SETJMP_H)
612     (void)PetscSetXIOErrorHandler(xioerrhdl);
613     ierr = PetscMemcpy(&PetscXIOErrorHandlerJumpBuf,&jmpbuf,sizeof(jmpbuf));CHKERRQ(ierr);
614 #endif
615   }
616   PetscFunctionReturn(0);
617 }
618 
619 #undef __FUNCT__
620 #define __FUNCT__ "PetscDrawDestroy_X"
621 PetscErrorCode PetscDrawDestroy_X(PetscDraw draw)
622 {
623   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
624   PetscErrorCode ierr;
625 
626   PetscFunctionBegin;
627   ierr = PetscDrawDestroy(&draw->popup);CHKERRQ(ierr);
628   ierr = PetscDrawXiClose(win);CHKERRQ(ierr);
629   ierr = PetscFree(draw->data);CHKERRQ(ierr);
630   PetscFunctionReturn(0);
631 }
632 
633 PetscErrorCode PetscDrawSave_X(PetscDraw);
634 PetscErrorCode PetscDrawSetSave_X(PetscDraw,const char*);
635 
636 static struct _PetscDrawOps DvOps = { PetscDrawSetDoubleBuffer_X,
637                                       PetscDrawFlush_X,
638                                       PetscDrawLine_X,
639                                       0,
640                                       0,
641                                       PetscDrawPoint_X,
642                                       0,
643                                       PetscDrawString_X,
644                                       PetscDrawStringVertical_X,
645                                       PetscDrawStringSetSize_X,
646                                       PetscDrawStringGetSize_X,
647                                       PetscDrawSetViewport_X,
648                                       PetscDrawClear_X,
649                                       PetscDrawSynchronizedFlush_X,
650                                       PetscDrawRectangle_X,
651                                       PetscDrawTriangle_X,
652                                       PetscDrawEllipse_X,
653                                       PetscDrawGetMouseButton_X,
654                                       PetscDrawPause_X,
655                                       PetscDrawSynchronizedClear_X,
656                                       0,
657                                       0,
658                                       PetscDrawGetPopup_X,
659                                       PetscDrawSetTitle_X,
660                                       PetscDrawCheckResizedWindow_X,
661                                       PetscDrawResizeWindow_X,
662                                       PetscDrawDestroy_X,
663                                       0,
664                                       PetscDrawGetSingleton_X,
665                                       PetscDrawRestoreSingleton_X,
666 #if defined(PETSC_HAVE_AFTERIMAGE)
667                                       PetscDrawSave_X,
668 #else
669                                       0,
670 #endif
671                                       PetscDrawSetSave_X,
672                                       0,
673                                       PetscDrawArrow_X,
674                                       PetscDrawCoordinateToPixel_X,
675                                       PetscDrawPixelToCoordinate_X,
676                                       PetscDrawPointPixel_X,
677                                       0};
678 
679 
680 extern PetscErrorCode PetscDrawXiQuickWindow(PetscDraw_X*,char*,char*,int,int,int,int);
681 extern PetscErrorCode PetscDrawXiQuickWindowFromWindow(PetscDraw_X*,char*,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 
695   (*sdraw)->ops->destroy = NULL;
696 
697   (*sdraw)->pause   = draw->pause;
698   (*sdraw)->coor_xl = draw->coor_xl;
699   (*sdraw)->coor_xr = draw->coor_xr;
700   (*sdraw)->coor_yl = draw->coor_yl;
701   (*sdraw)->coor_yr = draw->coor_yr;
702   (*sdraw)->port_xl = draw->port_xl;
703   (*sdraw)->port_xr = draw->port_xr;
704   (*sdraw)->port_yl = draw->port_yl;
705   (*sdraw)->port_yr = draw->port_yr;
706 
707   if (draw->popup) {
708     ierr = PetscObjectReference((PetscObject)draw->popup);CHKERRQ(ierr);
709     (*sdraw)->popup = draw->popup;
710   }
711 
712   /* actually create and open the window */
713   ierr = PetscNew(&sXwin);CHKERRQ(ierr);
714   ierr = PetscDrawXiQuickWindowFromWindow(sXwin,draw->display,Xwin->win);CHKERRQ(ierr);
715 
716   sXwin->x       = Xwin->x;
717   sXwin->y       = Xwin->y;
718   sXwin->w       = Xwin->w;
719   sXwin->h       = Xwin->h;
720   (*sdraw)->data = (void*)sXwin;
721   PetscFunctionReturn(0);
722 }
723 
724 #undef __FUNCT__
725 #define __FUNCT__ "PetscDrawRestoreSingleton_X"
726 static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw draw,PetscDraw *sdraw)
727 {
728   PetscErrorCode ierr;
729   PetscDraw_X    *sXwin = (PetscDraw_X*)(*sdraw)->data;
730 
731   PetscFunctionBegin;
732   ierr = PetscDrawDestroy(&(*sdraw)->popup);CHKERRQ(ierr);
733   ierr = PetscFree((*sdraw)->title);CHKERRQ(ierr);
734   ierr = PetscFree((*sdraw)->display);CHKERRQ(ierr);
735   ierr = PetscDrawXiClose(sXwin);CHKERRQ(ierr);
736   ierr = PetscFree(sXwin);CHKERRQ(ierr);
737   ierr = PetscHeaderDestroy(sdraw);CHKERRQ(ierr);
738   PetscFunctionReturn(0);
739 }
740 
741 #undef __FUNCT__
742 #define __FUNCT__ "PetscDrawXGetDisplaySize_Private"
743 PetscErrorCode PetscDrawXGetDisplaySize_Private(const char name[],int *width,int *height)
744 {
745   Display *display;
746 
747   PetscFunctionBegin;
748   display = XOpenDisplay(name);
749   if (!display) {
750     *width  = 0;
751     *height = 0;
752     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to open display on %s\n.  Make sure your COMPUTE NODES are authorized to connect \n\
753     to this X server and either your DISPLAY variable\n\
754     is set or you use the -display name option\n",name);
755   }
756   *width  = DisplayWidth(display,0);
757   *height = DisplayHeight(display,0);
758   XCloseDisplay(display);
759   PetscFunctionReturn(0);
760 }
761 
762 /*MC
763      PETSC_DRAW_X  - PETSc graphics device that uses either X windows or its virtual version Xvfb
764 
765    Options Database Keys:
766 +  -display <display> - sets the display to use
767 .  -x_virtual - forces use of a X virtual display Xvfb that will not display anything but -draw_save will still work. Xvfb is automatically
768                 started up in PetscSetDisplay() with this option
769 .  -geometry x,y,w,h - set location and size in pixels
770 -  -draw_size w,h - percentage of screeen, either 1, .5, .3, .25
771 
772    Level: beginner
773 
774 .seealso:  PetscDrawOpenX(), PetscDrawSetDisplay(), PetscDrawSetFromOptions()
775 
776 M*/
777 
778 #undef __FUNCT__
779 #define __FUNCT__ "PetscDrawCreate_X"
780 PETSC_EXTERN PetscErrorCode PetscDrawCreate_X(PetscDraw draw)
781 {
782   PetscDraw_X    *Xwin;
783   PetscErrorCode ierr;
784   PetscMPIInt    rank;
785   PetscInt       xywh[4],osize = 4,nsizes=2;
786   int            x          = draw->x,y = draw->y,w = draw->w,h = draw->h;
787   static int     xavailable = 0,yavailable = 0,xmax = 0,ymax = 0,ybottom = 0;
788   PetscBool      flg        = PETSC_FALSE,set;
789   PetscReal      sizes[2] = {.3,.3};
790 
791   PetscFunctionBegin;
792   if (!draw->display) {
793     ierr = PetscMalloc1(256,&draw->display);CHKERRQ(ierr);
794     ierr = PetscGetDisplay(draw->display,256);CHKERRQ(ierr);
795   }
796 
797   /*
798       Initialize the display size
799   */
800   if (!xmax) {
801     ierr = PetscDrawXGetDisplaySize_Private(draw->display,&xmax,&ymax);
802     /* if some processors fail on this and others succed then this is a problem ! */
803     if (ierr) {
804       (*PetscErrorPrintf)("PETSc unable to use X windows\nproceeding without graphics\n");
805       ierr = PetscDrawSetType(draw,PETSC_DRAW_NULL);CHKERRQ(ierr);
806       PetscFunctionReturn(0);
807     }
808   }
809 
810   ierr = PetscOptionsGetRealArray(((PetscObject)draw)->options,NULL,"-draw_size",sizes,&nsizes,&set);CHKERRQ(ierr);
811   if (set) {
812     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 = PETSC_DRAW_FULL_SIZE;
817     else if (sizes[1] == .5)  h = PETSC_DRAW_HALF_SIZE;
818     else if (sizes[1] == .3)  h = PETSC_DRAW_THIRD_SIZE;
819     else if (sizes[1] == .25) h = PETSC_DRAW_QUARTER_SIZE;
820   }
821 
822   if (w == PETSC_DECIDE) w = draw->w = 300;
823   if (h == PETSC_DECIDE) 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   /* allow user to set location and size of window */
838   xywh[0] = x; xywh[1] = y; xywh[2] = w; xywh[3] = h;
839   ierr = PetscOptionsGetIntArray(((PetscObject)draw)->options,NULL,"-geometry",xywh,&osize,NULL);CHKERRQ(ierr);
840 
841   x = (int) xywh[0]; y = (int) xywh[1]; w = (int) xywh[2]; h = (int) xywh[3];
842 
843 
844   if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) {
845     /*
846        PETSc tries to place windows starting in the upper left corner and
847        moving across to the right.
848 
849               --------------------------------------------
850               |  Region used so far +xavailable,yavailable |
851               |                     +                      |
852               |                     +                      |
853               |++++++++++++++++++++++ybottom               |
854               |                                            |
855               |                                            |
856               |--------------------------------------------|
857     */
858     /*  First: can we add it to the right? */
859     if (xavailable+w+10 <= xmax) {
860       x       = xavailable;
861       y       = yavailable;
862       ybottom = PetscMax(ybottom,y + h + 30);
863     } else {
864       /* No, so add it below on the left */
865       xavailable = 0;
866       x          = 0;
867       yavailable = ybottom;
868       y          = ybottom;
869       ybottom    = ybottom + h + 30;
870     }
871   }
872   /* update available region */
873   xavailable = PetscMax(xavailable,x + w + 10);
874   if (xavailable >= xmax) {
875     xavailable = 0;
876     yavailable = yavailable + h + 30;
877     ybottom    = yavailable;
878   }
879   if (yavailable >= ymax) {
880     y          = 0;
881     yavailable = 0;
882     ybottom    = 0;
883   }
884 
885   ierr = PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr);
886 
887   /* actually create and open the window */
888   ierr = PetscNew(&Xwin);CHKERRQ(ierr);
889   ierr = PetscLogObjectMemory((PetscObject)draw,sizeof(PetscDraw_X));CHKERRQ(ierr);
890   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);CHKERRQ(ierr);
891 
892   if (!rank) {
893     if (x < 0 || y < 0)   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative corner of window");
894     if (w <= 0 || h <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative window width or height");
895     ierr = PetscDrawXiQuickWindow(Xwin,draw->display,draw->title,x,y,w,h);CHKERRQ(ierr);
896     ierr = MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
897   } else {
898     unsigned long win = 0;
899     ierr = MPI_Bcast(&win,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));CHKERRQ(ierr);
900     ierr = PetscDrawXiQuickWindowFromWindow(Xwin,draw->display,win);CHKERRQ(ierr);
901   }
902 
903   Xwin->x    = x;
904   Xwin->y    = y;
905   Xwin->w    = w;
906   Xwin->h    = h;
907   draw->data = (void*)Xwin;
908 
909   /*
910     Need barrier here so processor 0 does not destroy the window before other
911     processors have completed PetscDrawXiQuickWindow()
912   */
913   ierr = PetscDrawSynchronizedFlush(draw);CHKERRQ(ierr);
914 
915   flg  = PETSC_TRUE;
916   ierr = PetscOptionsGetBool(((PetscObject)draw)->options,NULL,"-draw_double_buffer",&flg,NULL);CHKERRQ(ierr);
917   if (flg) {
918     ierr = PetscDrawSetDoubleBuffer(draw);CHKERRQ(ierr);
919   }
920   PetscFunctionReturn(0);
921 }
922