xref: /petsc/src/sys/classes/draw/impls/x/xops.c (revision b6bfaf9a0a178e626c17e7b8aafd5e3bedcd9a6d)
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   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 
269   PetscFunctionBegin;
270   if (XiWin->drw && XiWin->win) {
271     XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
272   }
273   XFlush(XiWin->disp);
274   XSync(XiWin->disp,False);
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(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr);
289     /* make sure data has actually arrived at server */
290     XSync(XiWin->disp,False);
291     ierr = MPI_Barrier(((PetscObject)draw)->comm);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(((PetscObject)draw)->comm);CHKERRQ(ierr);
298   } else {
299     ierr = MPI_Barrier(((PetscObject)draw)->comm);CHKERRQ(ierr);
300     XSync(XiWin->disp,False);
301     ierr = MPI_Barrier(((PetscObject)draw)->comm);CHKERRQ(ierr);
302   }
303   PetscFunctionReturn(0);
304 }
305 
306 #undef __FUNCT__
307 #define __FUNCT__ "PetscDrawSetViewport_X"
308 static PetscErrorCode PetscDrawSetViewport_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
309 {
310   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
311   XRectangle   box;
312 
313   PetscFunctionBegin;
314   box.x = (int)(xl*XiWin->w);   box.y = (int)((1.0-yr)*XiWin->h);
315   box.width = (int)((xr-xl)*XiWin->w);box.height = (int)((yr-yl)*XiWin->h);
316   XSetClipRectangles(XiWin->disp,XiWin->gc.set,0,0,&box,1,Unsorted);
317   PetscFunctionReturn(0);
318 }
319 
320 #undef __FUNCT__
321 #define __FUNCT__ "PetscDrawClear_X"
322 static PetscErrorCode PetscDrawClear_X(PetscDraw draw)
323 {
324   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;
325   int            x, y, w, h;
326   PetscErrorCode ierr;
327 
328   PetscFunctionBegin;
329   ierr = PetscDrawSave(draw);CHKERRQ(ierr);
330   x = (int)(draw->port_xl*XiWin->w);
331   w = (int)((draw->port_xr - draw->port_xl)*XiWin->w);
332   y = (int)((1.0-draw->port_yr)*XiWin->h);
333   h = (int)((draw->port_yr - draw->port_yl)*XiWin->h);
334   PetscDrawXiSetPixVal(XiWin,XiWin->background);
335   XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x,y,w,h);
336   PetscFunctionReturn(0);
337 }
338 
339 #undef __FUNCT__
340 #define __FUNCT__ "PetscDrawSynchronizedClear_X"
341 static PetscErrorCode PetscDrawSynchronizedClear_X(PetscDraw draw)
342 {
343   PetscErrorCode ierr;
344   PetscMPIInt    rank;
345   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;
346 
347   PetscFunctionBegin;
348   ierr = MPI_Barrier(((PetscObject)draw)->comm);CHKERRQ(ierr);
349   ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr);
350   if (!rank) {
351     ierr = PetscDrawClear_X(draw);CHKERRQ(ierr);
352   }
353   XFlush(XiWin->disp);
354   XFlush(XiWin->disp);
355   ierr = MPI_Barrier(((PetscObject)draw)->comm);CHKERRQ(ierr);
356   /*  XSync(XiWin->disp,False); */
357   ierr = MPI_Barrier(((PetscObject)draw)->comm);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 
372   ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr);
373   if (!rank) {
374     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
375   }
376   /* try to make sure it is actually done before passing info to all */
377   XSync(win->disp,False);
378   ierr = MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);CHKERRQ(ierr);
379   PetscFunctionReturn(0);
380 }
381 
382 #include <X11/cursorfont.h>
383 
384 #undef __FUNCT__
385 #define __FUNCT__ "PetscDrawGetMouseButton_X"
386 static PetscErrorCode PetscDrawGetMouseButton_X(PetscDraw draw,PetscDrawButton *button,PetscReal* x_user,PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys)
387 {
388   XEvent       report;
389   PetscDraw_X* win = (PetscDraw_X*)draw->data;
390   Window       root,child;
391   int          root_x,root_y,px,py;
392   unsigned int keys_button;
393   Cursor       cursor = 0;
394 
395   PetscFunctionBegin;
396   /* change cursor to indicate input */
397   if (!cursor) {
398     cursor = XCreateFontCursor(win->disp,XC_hand2);
399     if (!cursor) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to create X cursor");
400   }
401   XDefineCursor(win->disp,win->win,cursor);
402   XSelectInput(win->disp,win->win,ButtonPressMask | ButtonReleaseMask);
403 
404   while (XCheckTypedEvent(win->disp,ButtonPress,&report));
405   XMaskEvent(win->disp,ButtonReleaseMask,&report);
406   switch (report.xbutton.button) {
407     case Button1:
408       if (report.xbutton.state & ShiftMask) *button = PETSC_BUTTON_LEFT_SHIFT;
409       else                                  *button = PETSC_BUTTON_LEFT;
410       break;
411     case Button2:
412       if (report.xbutton.state & ShiftMask) *button = PETSC_BUTTON_CENTER_SHIFT;
413       else                                  *button = PETSC_BUTTON_CENTER;
414       break;
415     case Button3:
416       if (report.xbutton.state & ShiftMask) *button = PETSC_BUTTON_RIGHT_SHIFT;
417       else                                  *button = PETSC_BUTTON_RIGHT;
418       break;
419   }
420   XQueryPointer(win->disp,report.xmotion.window,&root,&child,&root_x,&root_y,&px,&py,&keys_button);
421 
422   if (x_phys) *x_phys = ((double)px)/((double)win->w);
423   if (y_phys) *y_phys = 1.0 - ((double)py)/((double)win->h);
424 
425   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);
426   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);
427 
428   XUndefineCursor(win->disp,win->win);
429   XFlush(win->disp);
430   XSync(win->disp,False);
431   PetscFunctionReturn(0);
432 }
433 
434 #undef __FUNCT__
435 #define __FUNCT__ "PetscDrawPause_X"
436 static PetscErrorCode PetscDrawPause_X(PetscDraw draw)
437 {
438   PetscErrorCode ierr;
439   PetscDraw_X*   win = (PetscDraw_X*)draw->data;
440 
441   PetscFunctionBegin;
442   if (!win->win) PetscFunctionReturn(0);
443   if (draw->pause > 0) PetscSleep(draw->pause);
444   else if (draw->pause == -1) {
445     PetscDrawButton button;
446     PetscMPIInt     rank;
447     ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr);
448     if (!rank) {
449       ierr = PetscDrawGetMouseButton(draw,&button,0,0,0,0);CHKERRQ(ierr);
450       if (button == PETSC_BUTTON_CENTER) draw->pause = 0;
451     }
452     ierr = MPI_Bcast(&draw->pause,1,MPIU_REAL,0,((PetscObject)draw)->comm);CHKERRQ(ierr);
453   }
454   PetscFunctionReturn(0);
455 }
456 
457 #undef __FUNCT__
458 #define __FUNCT__ "PetscDrawGetPopup_X"
459 static PetscErrorCode PetscDrawGetPopup_X(PetscDraw draw,PetscDraw *popup)
460 {
461   PetscErrorCode ierr;
462   PetscDraw_X*   win = (PetscDraw_X*)draw->data;
463   PetscBool      flg = PETSC_TRUE;
464 
465   PetscFunctionBegin;
466   ierr = PetscOptionsGetBool(((PetscObject)draw)->prefix,"-draw_popup",&flg,PETSC_NULL);CHKERRQ(ierr);
467   if (flg) {
468     ierr = PetscDrawOpenX(((PetscObject)draw)->comm,PETSC_NULL,PETSC_NULL,win->x,win->y+win->h+36,220,220,popup);CHKERRQ(ierr);
469     draw->popup = *popup;
470   } else {
471     *popup = PETSC_NULL;
472   }
473   PetscFunctionReturn(0);
474 }
475 
476 #undef __FUNCT__
477 #define __FUNCT__ "PetscDrawSetTitle_X"
478 static PetscErrorCode PetscDrawSetTitle_X(PetscDraw draw,const char title[])
479 {
480   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
481   XTextProperty  prop;
482   PetscErrorCode ierr;
483   size_t         len;
484 
485   PetscFunctionBegin;
486   if (win->win) {
487     XGetWMName(win->disp,win->win,&prop);
488     XFree((void*)prop.value);
489     prop.value  = (unsigned char *)title;
490     ierr        = PetscStrlen(title,&len);CHKERRQ(ierr);
491     prop.nitems = (long) len;
492     XSetWMName(win->disp,win->win,&prop);
493   }
494   PetscFunctionReturn(0);
495 }
496 
497 #undef __FUNCT__
498 #define __FUNCT__ "PetscDrawResizeWindow_X"
499 static PetscErrorCode PetscDrawResizeWindow_X(PetscDraw draw,int w,int h)
500 {
501   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
502   unsigned int   ww,hh,border,depth;
503   int            x,y;
504   PetscErrorCode ierr;
505   Window         root;
506 
507   PetscFunctionBegin;
508   if (win->win) {
509     XResizeWindow(win->disp,win->win,w,h);
510     XGetGeometry(win->disp,win->win,&root,&x,&y,&ww,&hh,&border,&depth);
511     ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr);
512   }
513   PetscFunctionReturn(0);
514 }
515 
516 #undef __FUNCT__
517 #define __FUNCT__ "PetscDrawCheckResizedWindow_X"
518 static PetscErrorCode PetscDrawCheckResizedWindow_X(PetscDraw draw)
519 {
520   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
521   PetscErrorCode ierr;
522   int            x,y;
523   PetscMPIInt    rank;
524   Window         root;
525   unsigned int   w,h,border,depth,geo[2];
526   PetscReal      xl,xr,yl,yr;
527   XRectangle     box;
528 
529   PetscFunctionBegin;
530   if (!win->win) PetscFunctionReturn(0);
531   ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr);
532   if (!rank) {
533     XFlush(win->disp);
534     XSync(win->disp,False);
535     XSync(win->disp,False);
536     XGetGeometry(win->disp,win->win,&root,&x,&y,geo,geo+1,&border,&depth);
537   }
538   ierr = MPI_Bcast(geo,2,MPI_UNSIGNED,0,((PetscObject)draw)->comm);CHKERRQ(ierr);
539   w = geo[0];
540   h = geo[1];
541   if (w == (unsigned int) win->w && h == (unsigned int) win->h) PetscFunctionReturn(0);
542 
543   /* record new window sizes */
544 
545   win->h = h; win->w = w;
546 
547   /* Free buffer space and create new version (only first processor does this) */
548   if (win->drw) {
549     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
550   }
551   /* reset the clipping */
552   xl = draw->port_xl; yl = draw->port_yl;
553   xr = draw->port_xr; yr = draw->port_yr;
554   box.x     = (int)(xl*win->w);     box.y      = (int)((1.0-yr)*win->h);
555   box.width = (int)((xr-xl)*win->w);box.height = (int)((yr-yl)*win->h);
556   XSetClipRectangles(win->disp,win->gc.set,0,0,&box,1,Unsorted);
557 
558   /* try to make sure it is actually done before passing info to all */
559   XSync(win->disp,False);
560   ierr = MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);CHKERRQ(ierr);
561   PetscFunctionReturn(0);
562 }
563 
564 static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw,PetscDraw*);
565 static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw,PetscDraw*);
566 
567 #undef __FUNCT__
568 #define __FUNCT__ "PetscDrawDestroy_X"
569 PetscErrorCode PetscDrawDestroy_X(PetscDraw draw)
570 {
571   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
572   PetscErrorCode ierr;
573 #if defined(PETSC_HAVE_POPEN)
574   char           command[PETSC_MAX_PATH_LEN];
575   PetscMPIInt    rank;
576   FILE           *fd;
577 #endif
578 
579   PetscFunctionBegin;
580   ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr);
581 
582 #if defined(PETSC_HAVE_POPEN)
583   ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr);
584   if (draw->savefilename && !rank && draw->savefilemovie) {
585     ierr = PetscSNPrintf(command,PETSC_MAX_PATH_LEN,"ffmpeg  -i %s_%%d.Gif %s.m4v",draw->savefilename,draw->savefilename);CHKERRQ(ierr);
586     ierr = PetscPOpen(((PetscObject)draw)->comm,PETSC_NULL,command,"r",&fd);CHKERRQ(ierr);
587     ierr = PetscPClose(((PetscObject)draw)->comm,fd,PETSC_NULL);CHKERRQ(ierr);
588   }
589 #endif
590 
591   XFreeGC(win->disp,win->gc.set);
592   XCloseDisplay(win->disp);
593   ierr = PetscDrawDestroy(&draw->popup);CHKERRQ(ierr);
594   ierr = PetscFree(win->font);CHKERRQ(ierr);
595   ierr = PetscFree(win);CHKERRQ(ierr);
596   PetscFunctionReturn(0);
597 }
598 
599 PetscErrorCode PetscDrawSave_X(PetscDraw);
600 PetscErrorCode PetscDrawSetSave_X(PetscDraw,const char*);
601 
602 static struct _PetscDrawOps DvOps = { PetscDrawSetDoubleBuffer_X,
603                                       PetscDrawFlush_X,
604                                       PetscDrawLine_X,
605                                       0,
606                                       0,
607                                       PetscDrawPoint_X,
608                                       0,
609                                       PetscDrawString_X,
610                                       PetscDrawStringVertical_X,
611                                       PetscDrawStringSetSize_X,
612                                       PetscDrawStringGetSize_X,
613                                       PetscDrawSetViewport_X,
614                                       PetscDrawClear_X,
615                                       PetscDrawSynchronizedFlush_X,
616                                       PetscDrawRectangle_X,
617                                       PetscDrawTriangle_X,
618                                       PetscDrawEllipse_X,
619                                       PetscDrawGetMouseButton_X,
620                                       PetscDrawPause_X,
621                                       PetscDrawSynchronizedClear_X,
622                                       0,
623                                       0,
624                                       PetscDrawGetPopup_X,
625                                       PetscDrawSetTitle_X,
626                                       PetscDrawCheckResizedWindow_X,
627                                       PetscDrawResizeWindow_X,
628                                       PetscDrawDestroy_X,
629                                       0,
630                                       PetscDrawGetSingleton_X,
631                                       PetscDrawRestoreSingleton_X,
632 #if defined(PETSC_HAVE_AFTERIMAGE)
633                                       PetscDrawSave_X,
634 #else
635                                       0,
636 #endif
637                                       PetscDrawSetSave_X,
638                                       0,
639                                       PetscDrawArrow_X,
640                                       PetscDrawCoordinateToPixel_X,
641                                       PetscDrawPixelToCoordinate_X,
642                                       PetscDrawPointPixel_X};
643 
644 
645 extern PetscErrorCode PetscDrawXiQuickWindow(PetscDraw_X*,char*,char*,int,int,int,int);
646 extern PetscErrorCode PetscDrawXiQuickWindowFromWindow(PetscDraw_X*,char*,Window);
647 
648 #undef __FUNCT__
649 #define __FUNCT__ "PetscDrawGetSingleton_X"
650 static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw draw,PetscDraw *sdraw)
651 {
652   PetscErrorCode ierr;
653   PetscDraw_X *Xwin = (PetscDraw_X*)draw->data,*sXwin;
654 
655   PetscFunctionBegin;
656   ierr = PetscDrawCreate(PETSC_COMM_SELF,draw->display,draw->title,draw->x,draw->y,draw->w,draw->h,sdraw);CHKERRQ(ierr);
657   ierr = PetscObjectChangeTypeName((PetscObject)*sdraw,PETSC_DRAW_X);CHKERRQ(ierr);
658   ierr = PetscMemcpy((*sdraw)->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr);
659   (*sdraw)->ops->destroy = 0;
660 
661   (*sdraw)->pause   = draw->pause;
662   (*sdraw)->coor_xl = draw->coor_xl;
663   (*sdraw)->coor_xr = draw->coor_xr;
664   (*sdraw)->coor_yl = draw->coor_yl;
665   (*sdraw)->coor_yr = draw->coor_yr;
666   (*sdraw)->port_xl = draw->port_xl;
667   (*sdraw)->port_xr = draw->port_xr;
668   (*sdraw)->port_yl = draw->port_yl;
669   (*sdraw)->port_yr = draw->port_yr;
670   (*sdraw)->popup   = draw->popup;
671 
672   /* actually create and open the window */
673   ierr = PetscNew(PetscDraw_X,&sXwin);CHKERRQ(ierr);
674   ierr = PetscDrawXiQuickWindowFromWindow(sXwin,draw->display,Xwin->win);CHKERRQ(ierr);
675 
676   sXwin->x       = Xwin->x;
677   sXwin->y       = Xwin->y;
678   sXwin->w       = Xwin->w;
679   sXwin->h       = Xwin->h;
680   (*sdraw)->data = (void*)sXwin;
681  PetscFunctionReturn(0);
682 }
683 
684 #undef __FUNCT__
685 #define __FUNCT__ "PetscDrawRestoreSingleton_X"
686 static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw draw,PetscDraw *sdraw)
687 {
688   PetscErrorCode ierr;
689   PetscDraw_X    *sXwin = (PetscDraw_X*)(*sdraw)->data;
690 
691   PetscFunctionBegin;
692   XFreeGC(sXwin->disp,sXwin->gc.set);
693   XCloseDisplay(sXwin->disp);
694   ierr = PetscDrawDestroy(&(*sdraw)->popup);CHKERRQ(ierr);
695   ierr = PetscFree((*sdraw)->title);CHKERRQ(ierr);
696   ierr = PetscFree((*sdraw)->display);CHKERRQ(ierr);
697   ierr = PetscFree(sXwin->font);CHKERRQ(ierr);
698   ierr = PetscFree(sXwin);CHKERRQ(ierr);
699   ierr = PetscHeaderDestroy(sdraw);CHKERRQ(ierr);
700   PetscFunctionReturn(0);
701 }
702 
703 #undef __FUNCT__
704 #define __FUNCT__ "PetscDrawXGetDisplaySize_Private"
705 PetscErrorCode PetscDrawXGetDisplaySize_Private(const char name[],int *width,int *height)
706 {
707   Display *display;
708 
709   PetscFunctionBegin;
710   display = XOpenDisplay(name);
711   if (!display) {
712     *width  = 0;
713     *height = 0;
714     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to open display on %s\n.  Make sure your COMPUTE NODES are authorized to connect \n\
715     to this X server and either your DISPLAY variable\n\
716     is set or you use the -display name option\n",name);
717   }
718   *width  = DisplayWidth(display,0);
719   *height = DisplayHeight(display,0);
720   XCloseDisplay(display);
721   PetscFunctionReturn(0);
722 }
723 
724 EXTERN_C_BEGIN
725 #undef __FUNCT__
726 #define __FUNCT__ "PetscDrawCreate_X"
727 PetscErrorCode  PetscDrawCreate_X(PetscDraw draw)
728 {
729   PetscDraw_X    *Xwin;
730   PetscErrorCode ierr;
731   PetscMPIInt    rank;
732   PetscInt       xywh[4],osize = 4;
733   int            x = draw->x,y = draw->y,w = draw->w,h = draw->h;
734   static int     xavailable = 0,yavailable = 0,xmax = 0,ymax = 0,ybottom = 0;
735   PetscBool      flg = PETSC_FALSE;
736 
737   PetscFunctionBegin;
738   if (!draw->display) {
739     ierr = PetscMalloc(256*sizeof(char),&draw->display);CHKERRQ(ierr);
740     ierr = PetscGetDisplay(draw->display,256);CHKERRQ(ierr);
741   }
742 
743   /*
744       Initialize the display size
745   */
746   if (!xmax) {
747     ierr = PetscDrawXGetDisplaySize_Private(draw->display,&xmax,&ymax);
748     /* if some processors fail on this and others succed then this is a problem ! */
749     if (ierr) {
750       (*PetscErrorPrintf)("PETSc unable to use X windows\nproceeding without graphics\n");
751       ierr = PetscDrawSetType(draw,PETSC_DRAW_NULL);CHKERRQ(ierr);
752       PetscFunctionReturn(0);
753     }
754   }
755 
756   if (w == PETSC_DECIDE) w = draw->w = 300;
757   if (h == PETSC_DECIDE) h = draw->h = 300;
758   switch (w) {
759     case PETSC_DRAW_FULL_SIZE: w = draw->w = xmax - 10;
760                         break;
761     case PETSC_DRAW_HALF_SIZE: w = draw->w = (xmax - 20)/2;
762                         break;
763     case PETSC_DRAW_THIRD_SIZE: w = draw->w = (xmax - 30)/3;
764                         break;
765     case PETSC_DRAW_QUARTER_SIZE: w = draw->w = (xmax - 40)/4;
766                         break;
767   }
768   switch (h) {
769     case PETSC_DRAW_FULL_SIZE: h = draw->h = ymax - 10;
770                         break;
771     case PETSC_DRAW_HALF_SIZE: h = draw->h = (ymax - 20)/2;
772                         break;
773     case PETSC_DRAW_THIRD_SIZE: h = draw->h = (ymax - 30)/3;
774                         break;
775     case PETSC_DRAW_QUARTER_SIZE: h = draw->h = (ymax - 40)/4;
776                         break;
777   }
778 
779   /* allow user to set location and size of window */
780   xywh[0] = x; xywh[1] = y; xywh[2] = w; xywh[3] = h;
781   ierr = PetscOptionsGetIntArray(PETSC_NULL,"-geometry",xywh,&osize,PETSC_NULL);CHKERRQ(ierr);
782   x = (int) xywh[0]; y = (int) xywh[1]; w = (int) xywh[2]; h = (int) xywh[3];
783 
784 
785   if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) {
786     /*
787        PETSc tries to place windows starting in the upper left corner and
788        moving across to the right.
789 
790               --------------------------------------------
791               |  Region used so far +xavailable,yavailable |
792               |                     +                      |
793               |                     +                      |
794               |++++++++++++++++++++++ybottom               |
795               |                                            |
796               |                                            |
797               |--------------------------------------------|
798     */
799     /*  First: can we add it to the right? */
800     if (xavailable+w+10 <= xmax) {
801       x       = xavailable;
802       y       = yavailable;
803       ybottom = PetscMax(ybottom,y + h + 30);
804     } else {
805       /* No, so add it below on the left */
806       xavailable = 0;
807       x          = 0;
808       yavailable = ybottom;
809       y          = ybottom;
810       ybottom    = ybottom + h + 30;
811     }
812   }
813   /* update available region */
814   xavailable = PetscMax(xavailable,x + w + 10);
815   if (xavailable >= xmax) {
816     xavailable = 0;
817     yavailable = yavailable + h + 30;
818     ybottom    = yavailable;
819   }
820   if (yavailable >= ymax) {
821     y          = 0;
822     yavailable = 0;
823     ybottom    = 0;
824   }
825 
826   ierr = PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr);
827 
828   /* actually create and open the window */
829   ierr = PetscNew(PetscDraw_X,&Xwin);CHKERRQ(ierr);
830   ierr = PetscLogObjectMemory(draw,sizeof(PetscDraw_X));CHKERRQ(ierr);
831   ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr);
832 
833   if (!rank) {
834     if (x < 0 || y < 0)   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative corner of window");
835     if (w <= 0 || h <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative window width or height");
836     ierr = PetscDrawXiQuickWindow(Xwin,draw->display,draw->title,x,y,w,h);CHKERRQ(ierr);
837     ierr = MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);CHKERRQ(ierr);
838   } else {
839     unsigned long win = 0;
840     ierr = MPI_Bcast(&win,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);CHKERRQ(ierr);
841     ierr = PetscDrawXiQuickWindowFromWindow(Xwin,draw->display,win);CHKERRQ(ierr);
842   }
843 
844   Xwin->x      = x;
845   Xwin->y      = y;
846   Xwin->w      = w;
847   Xwin->h      = h;
848   draw->data   = (void*)Xwin;
849 
850   /*
851     Need barrier here so processor 0 does not destroy the window before other
852     processors have completed PetscDrawXiQuickWindow()
853   */
854   ierr = PetscDrawSynchronizedFlush(draw);CHKERRQ(ierr);
855 
856   flg = PETSC_TRUE;
857   ierr = PetscOptionsGetBool(PETSC_NULL,"-draw_double_buffer",&flg,PETSC_NULL);CHKERRQ(ierr);
858   if (flg) {
859      ierr = PetscDrawSetDoubleBuffer(draw);CHKERRQ(ierr);
860   }
861   PetscFunctionReturn(0);
862 }
863 EXTERN_C_END
864 
865 
866 
867 
868 
869 
870 
871 
872 
873