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