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