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