Actual source code: xops.c

  1: /*
  2:     Defines the operations for the X PetscDraw implementation.
  3: */

  5: #include <../src/sys/classes/draw/impls/x/ximpl.h>

  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)))))

 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))

 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;

 22:   xa = (int)(xl * xmax);
 23:   ya = ymax - (int)(yr * ymax);
 24:   xb = (int)(xr * xmax);
 25:   yb = ymax - (int)(yl * ymax);
 26:   PetscDrawCollectiveBegin(draw);
 27:   box.x      = (short)xa;
 28:   box.width  = (unsigned short)(xb + 1 - xa);
 29:   box.y      = (short)ya;
 30:   box.height = (unsigned short)(yb + 1 - ya);
 31:   XSetClipRectangles(XiWin->disp, XiWin->gc.set, 0, 0, &box, 1, Unsorted);
 32:   PetscDrawCollectiveEnd(draw);
 33:   return 0;
 34: }

 36: static PetscErrorCode PetscDrawCoordinateToPixel_X(PetscDraw draw, PetscReal x, PetscReal y, int *i, int *j)
 37: {
 38:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;

 40:   *i = XTRANS(draw, XiWin, x);
 41:   *j = YTRANS(draw, XiWin, y);
 42:   return 0;
 43: }

 45: static PetscErrorCode PetscDrawPixelToCoordinate_X(PetscDraw draw, int i, int j, PetscReal *x, PetscReal *y)
 46: {
 47:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;

 49:   *x = ITRANS(draw, XiWin, i);
 50:   *y = JTRANS(draw, XiWin, j);
 51:   return 0;
 52: }

 54: static PetscErrorCode PetscDrawPoint_X(PetscDraw draw, PetscReal x, PetscReal y, int c)
 55: {
 56:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;
 57:   int          xx, yy, i, j;

 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++) XDrawPoint(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, xx + i, yy + j);
 64:   }
 65:   return 0;
 66: }

 68: static PetscErrorCode PetscDrawPointPixel_X(PetscDraw draw, int x, int y, int c)
 69: {
 70:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;

 72:   PetscDrawXiSetColor(XiWin, c);
 73:   XDrawPoint(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, x, y);
 74:   return 0;
 75: }

 77: static PetscErrorCode PetscDrawLine_X(PetscDraw draw, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr, int cl)
 78: {
 79:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;
 80:   int          x_1, y_1, x_2, y_2;

 82:   PetscDrawXiSetColor(XiWin, cl);
 83:   x_1 = XTRANS(draw, XiWin, xl);
 84:   x_2 = XTRANS(draw, XiWin, xr);
 85:   y_1 = YTRANS(draw, XiWin, yl);
 86:   y_2 = YTRANS(draw, XiWin, yr);
 87:   XDrawLine(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, x_1, y_1, x_2, y_2);
 88:   return 0;
 89: }

 91: static PetscErrorCode PetscDrawArrow_X(PetscDraw draw, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr, int cl)
 92: {
 93:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;
 94:   int          x_1, y_1, x_2, y_2;

 96:   PetscDrawXiSetColor(XiWin, cl);
 97:   x_1 = XTRANS(draw, XiWin, xl);
 98:   x_2 = XTRANS(draw, XiWin, xr);
 99:   y_1 = YTRANS(draw, XiWin, yl);
100:   y_2 = YTRANS(draw, XiWin, yr);
101:   XDrawLine(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, x_1, y_1, x_2, y_2);
102:   if (x_1 == x_2 && y_1 == y_2) return 0;
103:   if (x_1 == x_2 && PetscAbs(y_1 - y_2) > 7) {
104:     if (y_2 > y_1) {
105:       XDrawLine(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, x_2, y_2, x_2 - 3, y_2 - 3);
106:       XDrawLine(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, x_2, y_2, x_2 + 3, y_2 - 3);
107:     } else {
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:     }
111:   }
112:   if (y_1 == y_2 && PetscAbs(x_1 - x_2) > 7) {
113:     if (x_2 > x_1) {
114:       XDrawLine(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, x_2 - 3, y_2 - 3, x_2, y_2);
115:       XDrawLine(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, x_2 - 3, y_2 + 3, x_2, y_2);
116:     } else {
117:       XDrawLine(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, x_2, y_2, x_2 + 3, y_2 - 3);
118:       XDrawLine(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, x_2, y_2, x_2 + 3, y_2 + 3);
119:     }
120:   }
121:   return 0;
122: }

124: static PetscErrorCode PetscDrawRectangle_X(PetscDraw draw, PetscReal xl, PetscReal yl, PetscReal xr, PetscReal yr, int c1, int c2, int c3, int c4)
125: {
126:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;
127:   int          x, y, w, h, c = (c1 + c2 + c3 + c4) / 4;

129:   PetscDrawXiSetColor(XiWin, c);
130:   x = XTRANS(draw, XiWin, xl);
131:   w = XTRANS(draw, XiWin, xr) + 1 - x;
132:   if (w <= 0) w = 1;
133:   y = YTRANS(draw, XiWin, yr);
134:   h = YTRANS(draw, XiWin, yl) + 1 - y;
135:   if (h <= 0) h = 1;
136:   XFillRectangle(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, x, y, w, h);
137:   return 0;
138: }

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;

145:   PetscDrawXiSetColor(XiWin, c);
146:   xA = XTRANS(draw, XiWin, x - a / 2);
147:   w  = XTRANS(draw, XiWin, x + a / 2) + 1 - xA;
148:   w  = PetscAbs(w);
149:   yA = YTRANS(draw, XiWin, y + b / 2);
150:   h  = YTRANS(draw, XiWin, y - b / 2) + 1 - yA;
151:   h  = PetscAbs(h);
152:   XFillArc(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, xA, yA, w, h, 0, 360 * 64);
153:   return 0;
154: }

156: PETSC_INTERN PetscErrorCode PetscDrawInterpolatedTriangle_X(PetscDraw_X *, int, int, int, int, int, int, int, int, int);

158: 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)
159: {
160:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;

162:   if (c1 == c2 && c2 == c3) {
163:     XPoint pt[3];
164:     PetscDrawXiSetColor(XiWin, c1);
165:     pt[0].x = XTRANS(draw, XiWin, X1);
166:     pt[0].y = YTRANS(draw, XiWin, Y_1);
167:     pt[1].x = XTRANS(draw, XiWin, X2);
168:     pt[1].y = YTRANS(draw, XiWin, Y2);
169:     pt[2].x = XTRANS(draw, XiWin, X3);
170:     pt[2].y = YTRANS(draw, XiWin, Y3);
171:     XFillPolygon(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, pt, 3, Convex, CoordModeOrigin);
172:   } else {
173:     int x1, y_1, x2, y2, x3, y3;
174:     x1  = XTRANS(draw, XiWin, X1);
175:     y_1 = YTRANS(draw, XiWin, Y_1);
176:     x2  = XTRANS(draw, XiWin, X2);
177:     y2  = YTRANS(draw, XiWin, Y2);
178:     x3  = XTRANS(draw, XiWin, X3);
179:     y3  = YTRANS(draw, XiWin, Y3);
180:     PetscDrawInterpolatedTriangle_X(XiWin, x1, y_1, c1, x2, y2, c2, x3, y3, c3);
181:   }
182:   return 0;
183: }

185: static PetscErrorCode PetscDrawStringSetSize_X(PetscDraw draw, PetscReal x, PetscReal y)
186: {
187:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;
188:   int          w, h;

190:   w = (int)((XiWin->w) * x * (draw->port_xr - draw->port_xl) / (draw->coor_xr - draw->coor_xl));
191:   h = (int)((XiWin->h) * y * (draw->port_yr - draw->port_yl) / (draw->coor_yr - draw->coor_yl));
192:   PetscFree(XiWin->font);
193:   PetscDrawXiFontFixed(XiWin, w, h, &XiWin->font);
194:   return 0;
195: }

197: static PetscErrorCode PetscDrawStringGetSize_X(PetscDraw draw, PetscReal *x, PetscReal *y)
198: {
199:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;
200:   PetscReal    w, h;

202:   w = XiWin->font->font_w;
203:   h = XiWin->font->font_h;
204:   if (x) *x = w * (draw->coor_xr - draw->coor_xl) / ((XiWin->w) * (draw->port_xr - draw->port_xl));
205:   if (y) *y = h * (draw->coor_yr - draw->coor_yl) / ((XiWin->h) * (draw->port_yr - draw->port_yl));
206:   return 0;
207: }

209: static PetscErrorCode PetscDrawString_X(PetscDraw draw, PetscReal x, PetscReal y, int c, const char chrs[])
210: {
211:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;
212:   int          xx, yy, descent = XiWin->font->font_descent;
213:   size_t       len;
214:   char        *substr;
215:   PetscToken   token;

217:   xx = XTRANS(draw, XiWin, x);
218:   yy = YTRANS(draw, XiWin, y);
219:   PetscDrawXiSetColor(XiWin, c);

221:   PetscTokenCreate(chrs, '\n', &token);
222:   PetscTokenFind(token, &substr);
223:   while (substr) {
224:     PetscStrlen(substr, &len);
225:     XDrawString(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, xx, yy - descent, substr, len);
226:     yy += XiWin->font->font_h;
227:     PetscTokenFind(token, &substr);
228:   }
229:   PetscTokenDestroy(&token);
230:   return 0;
231: }

233: static PetscErrorCode PetscDrawStringVertical_X(PetscDraw draw, PetscReal x, PetscReal y, int c, const char text[])
234: {
235:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;
236:   int          xx, yy, offset = XiWin->font->font_h - XiWin->font->font_descent;
237:   char         chr[2] = {0, 0};

239:   xx = XTRANS(draw, XiWin, x);
240:   yy = YTRANS(draw, XiWin, y);
241:   PetscDrawXiSetColor(XiWin, c);
242:   while ((chr[0] = *text++)) {
243:     XDrawString(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, xx, yy + offset, chr, 1);
244:     yy += XiWin->font->font_h;
245:   }
246:   return 0;
247: }

249: static PetscErrorCode PetscDrawFlush_X(PetscDraw draw)
250: {
251:   PetscDraw_X *XiWin = (PetscDraw_X *)draw->data;
252:   PetscMPIInt  rank;

254:   /* make sure the X server processed requests from all processes */
255:   PetscDrawCollectiveBegin(draw);
256:   XSync(XiWin->disp, False);
257:   PetscDrawCollectiveEnd(draw);
258:   MPI_Barrier(PetscObjectComm((PetscObject)draw));

260:   /* transfer pixmap contents to window (only the first process does this) */
261:   if (XiWin->drw && XiWin->win) {
262:     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:     MPI_Barrier(PetscObjectComm((PetscObject)draw));
268:   }
269:   return 0;
270: }

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;

280:   /* make sure the X server processed requests from all processes */
281:   PetscDrawCollectiveBegin(draw);
282:   XSync(XiWin->disp, False);
283:   PetscDrawCollectiveEnd(draw);
284:   MPI_Barrier(PetscObjectComm((PetscObject)draw));

286:   /* only the first process handles the clearing business */
287:   PetscDrawCollectiveBegin(draw);
288:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw), &rank);
289:   if (rank == 0) {
290:     int          xa = (int)(xl * xmax), ya = ymax - (int)(yr * ymax);
291:     int          xb = (int)(xr * xmax), yb = ymax - (int)(yl * ymax);
292:     unsigned int w = (unsigned int)(xb + 1 - xa);
293:     unsigned int h = (unsigned int)(yb + 1 - ya);
294:     PetscDrawXiSetPixVal(XiWin, XiWin->background);
295:     XFillRectangle(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, xa, ya, w, h);
296:     XSync(XiWin->disp, False);
297:   }
298:   PetscDrawCollectiveEnd(draw);
299:   MPI_Barrier(PetscObjectComm((PetscObject)draw));
300:   return 0;
301: }

303: static PetscErrorCode PetscDrawSetDoubleBuffer_X(PetscDraw draw)
304: {
305:   PetscDraw_X *win = (PetscDraw_X *)draw->data;
306:   PetscMPIInt  rank;

308:   if (win->drw) return 0;
309:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw), &rank);

311:   PetscDrawCollectiveBegin(draw);
312:   if (rank == 0) PetscDrawXiQuickPixmap(win);
313:   PetscDrawCollectiveEnd(draw);
314:   MPI_Bcast(&win->drw, 1, MPI_UNSIGNED_LONG, 0, PetscObjectComm((PetscObject)draw));
315:   return 0;
316: }

318: static PetscErrorCode PetscDrawGetPopup_X(PetscDraw draw, PetscDraw *popup)
319: {
320:   PetscDraw_X *win = (PetscDraw_X *)draw->data;
321:   PetscBool    flg = PETSC_TRUE;

323:   PetscOptionsGetBool(((PetscObject)draw)->options, ((PetscObject)draw)->prefix, "-draw_popup", &flg, NULL);
324:   if (!flg || !win->win) {
325:     *popup = NULL;
326:     return 0;
327:   }

329:   PetscDrawCreate(PetscObjectComm((PetscObject)draw), draw->display, NULL, win->x, win->y + win->h + 10, 220, 220, popup);
330:   PetscObjectSetOptionsPrefix((PetscObject)*popup, "popup_");
331:   PetscObjectAppendOptionsPrefix((PetscObject)*popup, ((PetscObject)draw)->prefix);
332:   PetscDrawSetType(*popup, PETSC_DRAW_X);
333:   draw->popup = *popup;
334:   return 0;
335: }

337: static PetscErrorCode PetscDrawSetTitle_X(PetscDraw draw, const char title[])
338: {
339:   PetscDraw_X *win = (PetscDraw_X *)draw->data;
340:   PetscMPIInt  rank;

342:   if (!win->win) return 0;
343:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw), &rank);
344:   PetscDrawCollectiveBegin(draw);
345:   if (rank == 0) {
346:     size_t        len;
347:     XTextProperty prop;
348:     PetscStrlen(title, &len);
349:     XGetWMName(win->disp, win->win, &prop);
350:     XFree((void *)prop.value);
351:     prop.value  = (unsigned char *)title;
352:     prop.nitems = (long)len;
353:     XSetWMName(win->disp, win->win, &prop);
354:   }
355:   PetscDrawCollectiveEnd(draw);
356:   return 0;
357: }

359: static PetscErrorCode PetscDrawCheckResizedWindow_X(PetscDraw draw)
360: {
361:   PetscDraw_X *win = (PetscDraw_X *)draw->data;
362:   int          xywh[4];
363:   PetscMPIInt  rank;

365:   if (!win->win) return 0;
366:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw), &rank);

368:   PetscDrawCollectiveBegin(draw);
369:   if (rank == 0) PetscDrawXiGetGeometry(win, xywh, xywh + 1, xywh + 2, xywh + 3);
370:   PetscDrawCollectiveEnd(draw);
371:   MPI_Bcast(xywh, 4, MPI_INT, 0, PetscObjectComm((PetscObject)draw));

373:   /* record new window position */
374:   draw->x = win->x = xywh[0];
375:   draw->y = win->y = xywh[1];
376:   if (xywh[2] == win->w && xywh[3] == win->h) return 0;
377:   /* record new window sizes */
378:   draw->w = win->w = xywh[2];
379:   draw->h = win->h = xywh[3];

381:   /* recreate pixmap (only first processor does this) */
382:   PetscDrawCollectiveBegin(draw);
383:   if (rank == 0 && win->drw) PetscDrawXiQuickPixmap(win);
384:   PetscDrawCollectiveEnd(draw);
385:   MPI_Bcast(&win->drw, 1, MPI_UNSIGNED_LONG, 0, PetscObjectComm((PetscObject)draw));
386:   /* reset the clipping */
387:   PetscDrawSetViewport_X(draw, draw->port_xl, draw->port_yl, draw->port_xr, draw->port_yr);
388:   return 0;
389: }

391: static PetscErrorCode PetscDrawResizeWindow_X(PetscDraw draw, int w, int h)
392: {
393:   PetscDraw_X *win = (PetscDraw_X *)draw->data;
394:   PetscMPIInt  rank;

396:   if (w == win->w && h == win->h) return 0;
397:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw), &rank);

399:   if (win->win) {
400:     PetscDrawCollectiveBegin(draw);
401:     if (rank == 0) PetscDrawXiResizeWindow(win, w, h);
402:     PetscDrawCollectiveEnd(draw);
403:     PetscDrawCheckResizedWindow_X(draw);
404:   } else if (win->drw) {
405:     draw->w = win->w = w;
406:     draw->h = win->h = h;
407:     /* recreate pixmap (only first processor does this) */
408:     PetscDrawCollectiveBegin(draw);
409:     if (rank == 0) PetscDrawXiQuickPixmap(win);
410:     MPI_Bcast(&win->drw, 1, MPI_UNSIGNED_LONG, 0, PetscObjectComm((PetscObject)draw));
411:     /* reset the clipping */
412:     PetscDrawCollectiveEnd(draw);
413:     PetscDrawSetViewport_X(draw, draw->port_xl, draw->port_yl, draw->port_xr, draw->port_yr);
414:   }
415:   return 0;
416: }

418: #include <X11/cursorfont.h>

420: static PetscErrorCode PetscDrawGetMouseButton_X(PetscDraw draw, PetscDrawButton *button, PetscReal *x_user, PetscReal *y_user, PetscReal *x_phys, PetscReal *y_phys)
421: {
422:   PetscDraw_X *win = (PetscDraw_X *)draw->data;
423:   Cursor       cursor;
424:   XEvent       report;
425:   Window       root, child;
426:   int          root_x, root_y, px = 0, py = 0;
427:   unsigned int w, h, border, depth;
428:   unsigned int keys_button;
429:   PetscMPIInt  rank;
430:   PetscReal    xx, yy;

432:   *button = PETSC_BUTTON_NONE;
433:   if (!win->win) return 0;
434:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw), &rank);

436:   PetscDrawCollectiveBegin(draw);
437:   if (rank) goto finally;

439:   /* change cursor to indicate input */
440:   cursor = XCreateFontCursor(win->disp, XC_hand2);
442:   XDefineCursor(win->disp, win->win, cursor);
443:   /* wait for mouse button events */
444:   XSelectInput(win->disp, win->win, ButtonPressMask | ButtonReleaseMask);
445:   while (XCheckTypedEvent(win->disp, ButtonPress, &report))
446:     ;
447:   XMaskEvent(win->disp, ButtonReleaseMask, &report);
448:   /* get mouse pointer coordinates */
449:   XQueryPointer(win->disp, report.xmotion.window, &root, &child, &root_x, &root_y, &px, &py, &keys_button);
450:   /* the user may resize the window before pressing the mouse button */
451:   XGetGeometry(win->disp, win->win, &root, &root_x, &root_y, &w, &h, &border, &depth);
452:   /* cleanup input event handler and cursor  */
453:   XSelectInput(win->disp, win->win, NoEventMask);
454:   XUndefineCursor(win->disp, win->win);
455:   XFreeCursor(win->disp, cursor);
456:   XSync(win->disp, False);

458:   switch (report.xbutton.button) {
459:   case Button1:
460:     *button = PETSC_BUTTON_LEFT;
461:     break;
462:   case Button2:
463:     *button = PETSC_BUTTON_CENTER;
464:     break;
465:   case Button3:
466:     *button = PETSC_BUTTON_RIGHT;
467:     break;
468:   case Button4:
469:     *button = PETSC_BUTTON_WHEEL_UP;
470:     break;
471:   case Button5:
472:     *button = PETSC_BUTTON_WHEEL_DOWN;
473:     break;
474:   }
475:   if (report.xbutton.state & ShiftMask) {
476:     switch (report.xbutton.button) {
477:     case Button1:
478:       *button = PETSC_BUTTON_LEFT_SHIFT;
479:       break;
480:     case Button2:
481:       *button = PETSC_BUTTON_CENTER_SHIFT;
482:       break;
483:     case Button3:
484:       *button = PETSC_BUTTON_RIGHT_SHIFT;
485:       break;
486:     }
487:   }
488:   xx = ((PetscReal)px) / w;
489:   yy = 1 - ((PetscReal)py) / h;
490:   if (x_user) *x_user = draw->coor_xl + (xx - draw->port_xl) * (draw->coor_xr - draw->coor_xl) / (draw->port_xr - draw->port_xl);
491:   if (y_user) *y_user = draw->coor_yl + (yy - draw->port_yl) * (draw->coor_yr - draw->coor_yl) / (draw->port_yr - draw->port_yl);
492:   if (x_phys) *x_phys = xx;
493:   if (y_phys) *y_phys = yy;

495: finally:
496:   PetscDrawCollectiveEnd(draw);
497:   PetscDrawCheckResizedWindow_X(draw);
498:   return 0;
499: }

501: static PetscErrorCode PetscDrawPause_X(PetscDraw draw)
502: {
503:   PetscDraw_X *win = (PetscDraw_X *)draw->data;

505:   if (!win->win) return 0;
506:   if (draw->pause > 0) PetscSleep(draw->pause);
507:   else if (draw->pause == -1) {
508:     PetscDrawButton button = PETSC_BUTTON_NONE;
509:     PetscDrawGetMouseButton(draw, &button, NULL, NULL, NULL, NULL);
510:     if (button == PETSC_BUTTON_CENTER) draw->pause = 0;
511:   }
512:   return 0;
513: }

515: static PetscErrorCode PetscDrawDestroy_X(PetscDraw draw)
516: {
517:   PetscDraw_X *win = (PetscDraw_X *)draw->data;

519:   PetscDrawDestroy(&draw->popup);
520:   PetscDrawXiClose(win);
521:   PetscFree(draw->data);
522:   return 0;
523: }

525: static PetscErrorCode       PetscDrawGetSingleton_X(PetscDraw, PetscDraw *);
526: static PetscErrorCode       PetscDrawRestoreSingleton_X(PetscDraw, PetscDraw *);
527: PETSC_INTERN PetscErrorCode PetscDrawGetImage_X(PetscDraw, unsigned char[][3], unsigned int *, unsigned int *, unsigned char *[]);

529: 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};

531: static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw draw, PetscDraw *sdraw)
532: {
533:   PetscDraw_X *Xwin = (PetscDraw_X *)draw->data, *sXwin;

535:   PetscDrawCreate(PETSC_COMM_SELF, draw->display, draw->title, draw->x, draw->y, draw->w, draw->h, sdraw);
536:   PetscObjectChangeTypeName((PetscObject)*sdraw, PETSC_DRAW_X);
537:   PetscMemcpy((*sdraw)->ops, &DvOps, sizeof(DvOps));

539:   if (draw->popup) PetscDrawGetSingleton(draw->popup, &(*sdraw)->popup);
540:   (*sdraw)->pause   = draw->pause;
541:   (*sdraw)->coor_xl = draw->coor_xl;
542:   (*sdraw)->coor_xr = draw->coor_xr;
543:   (*sdraw)->coor_yl = draw->coor_yl;
544:   (*sdraw)->coor_yr = draw->coor_yr;
545:   (*sdraw)->port_xl = draw->port_xl;
546:   (*sdraw)->port_xr = draw->port_xr;
547:   (*sdraw)->port_yl = draw->port_yl;
548:   (*sdraw)->port_yr = draw->port_yr;

550:   /* share drawables (windows and/or pixmap) from the parent draw */
551:   PetscNew(&sXwin);
552:   (*sdraw)->data = (void *)sXwin;
553:   PetscDrawXiInit(sXwin, draw->display);
554:   if (Xwin->win) {
555:     PetscDrawXiQuickWindowFromWindow(sXwin, Xwin->win);
556:     sXwin->drw = Xwin->drw; /* XXX If the window is ever resized, this is wrong! */
557:   } else if (Xwin->drw) {
558:     PetscDrawXiColormap(sXwin);
559:     sXwin->drw = Xwin->drw;
560:   }
561:   PetscDrawXiGetGeometry(sXwin, &sXwin->x, &sXwin->y, &sXwin->w, &sXwin->h);
562:   (*sdraw)->x = sXwin->x;
563:   (*sdraw)->y = sXwin->y;
564:   (*sdraw)->w = sXwin->w;
565:   (*sdraw)->h = sXwin->h;
566:   return 0;
567: }

569: static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw draw, PetscDraw *sdraw)
570: {
571:   if (draw->popup && (*sdraw)->popup) {
572:     PetscBool    isdrawx;
573:     PetscDraw_X *pXwin = (PetscDraw_X *)draw->popup->data;
574:     PetscDraw_X *sXwin = (PetscDraw_X *)(*sdraw)->popup->data;
575:     PetscObjectTypeCompare((PetscObject)draw->popup, PETSC_DRAW_X, &isdrawx);
576:     if (!isdrawx) goto finally;
577:     PetscObjectTypeCompare((PetscObject)(*sdraw)->popup, PETSC_DRAW_X, &isdrawx);
578:     if (!isdrawx) goto finally;
579:     if (sXwin->win == pXwin->win) PetscDrawRestoreSingleton(draw->popup, &(*sdraw)->popup);
580:   }
581: finally:
582:   PetscDrawDestroy(sdraw);
583:   return 0;
584: }

586: static PetscErrorCode PetscDrawXGetDisplaySize_Private(const char name[], int *width, int *height, PetscBool *has_display)
587: {
588:   Display *display;

590:   display = XOpenDisplay(name);
591:   if (!display) {
592:     *width = *height = 0;
593:     (*PetscErrorPrintf)("Unable to open display on %s\n\
594:     Make sure your COMPUTE NODES are authorized to connect\n\
595:     to this X server and either your DISPLAY variable\n\
596:     is set or you use the -display name option\n",
597:                         name);
598:     *has_display = PETSC_FALSE;
599:     return 0;
600:   }
601:   *has_display = PETSC_TRUE;
602:   *width       = (int)DisplayWidth(display, DefaultScreen(display));
603:   *height      = (int)DisplayHeight(display, DefaultScreen(display));
604:   XCloseDisplay(display);
605:   return 0;
606: }

608: /*MC
609:      PETSC_DRAW_X  - PETSc graphics device that uses either X windows or its virtual version Xvfb

611:    Options Database Keys:
612: +  -display <display> - sets the display to use
613: .  -x_virtual - forces use of a X virtual display Xvfb that will not display anything but -draw_save will still work.
614:                 Xvfb is automatically started up in PetscSetDisplay() with this option
615: .  -draw_size w,h - percentage of screen (either 1, .5, .3, .25), or size in pixels
616: .  -geometry x,y,w,h - set location and size in pixels
617: .  -draw_virtual - do not open a window (draw on a pixmap), -draw_save will still work
618: -  -draw_double_buffer - avoid window flickering (draw on pixmap and flush to window)

620:    Level: beginner

622: .seealso: `PetscDraw`, `PetscDrawOpenX()`, `PetscDrawSetDisplay()`, `PetscDrawSetFromOptions()`
623: M*/

625: PETSC_EXTERN PetscErrorCode PetscDrawCreate_X(PetscDraw draw)
626: {
627:   PetscDraw_X  *Xwin;
628:   PetscMPIInt   rank;
629:   int           x = draw->x, y = draw->y, w = draw->w, h = draw->h;
630:   static int    xavailable = 0, yavailable = 0, ybottom = 0, xmax = 0, ymax = 0;
631:   PetscBool     set, dvirtual = PETSC_FALSE, doublebuffer = PETSC_TRUE, has_display;
632:   PetscInt      xywh[4], osize = 4, nsizes = 2;
633:   PetscReal     sizes[2]       = {.3, .3};
634:   static size_t DISPLAY_LENGTH = 265;

636:   /* get the display variable */
637:   if (!draw->display) {
638:     PetscMalloc1(DISPLAY_LENGTH, &draw->display);
639:     PetscGetDisplay(draw->display, DISPLAY_LENGTH);
640:   }

642:   /* initialize the display size */
643:   if (!xmax) {
644:     PetscDrawXGetDisplaySize_Private(draw->display, &xmax, &ymax, &has_display);
645:     /* if some processors fail on this and others succeed then this is a problem ! */
646:     if (!has_display) {
647:       (*PetscErrorPrintf)("PETSc unable to use X windows\nproceeding without graphics\n");
648:       PetscDrawSetType(draw, PETSC_DRAW_NULL);
649:       return 0;
650:     }
651:   }

653:   /* allow user to set size of drawable */
654:   PetscOptionsGetRealArray(((PetscObject)draw)->options, ((PetscObject)draw)->prefix, "-draw_size", sizes, &nsizes, &set);
655:   if (set && nsizes == 1 && sizes[0] > 1.0) sizes[1] = sizes[0];
656:   if (set) {
657:     if (sizes[0] > 1.0) w = (int)sizes[0];
658:     else if (sizes[0] == 1.0) w = PETSC_DRAW_FULL_SIZE;
659:     else if (sizes[0] == .5) w = PETSC_DRAW_HALF_SIZE;
660:     else if (sizes[0] == .3) w = PETSC_DRAW_THIRD_SIZE;
661:     else if (sizes[0] == .25) w = PETSC_DRAW_QUARTER_SIZE;
662:     if (sizes[1] > 1.0) h = (int)sizes[1];
663:     else if (sizes[1] == 1.0) h = PETSC_DRAW_FULL_SIZE;
664:     else if (sizes[1] == .5) h = PETSC_DRAW_HALF_SIZE;
665:     else if (sizes[1] == .3) h = PETSC_DRAW_THIRD_SIZE;
666:     else if (sizes[1] == .25) h = PETSC_DRAW_QUARTER_SIZE;
667:   }
668:   if (w == PETSC_DECIDE || w == PETSC_DEFAULT) w = draw->w = 300;
669:   if (h == PETSC_DECIDE || h == PETSC_DEFAULT) h = draw->h = 300;
670:   switch (w) {
671:   case PETSC_DRAW_FULL_SIZE:
672:     w = draw->w = (xmax - 10);
673:     break;
674:   case PETSC_DRAW_HALF_SIZE:
675:     w = draw->w = (xmax - 20) / 2;
676:     break;
677:   case PETSC_DRAW_THIRD_SIZE:
678:     w = draw->w = (xmax - 30) / 3;
679:     break;
680:   case PETSC_DRAW_QUARTER_SIZE:
681:     w = draw->w = (xmax - 40) / 4;
682:     break;
683:   }
684:   switch (h) {
685:   case PETSC_DRAW_FULL_SIZE:
686:     h = draw->h = (ymax - 10);
687:     break;
688:   case PETSC_DRAW_HALF_SIZE:
689:     h = draw->h = (ymax - 20) / 2;
690:     break;
691:   case PETSC_DRAW_THIRD_SIZE:
692:     h = draw->h = (ymax - 30) / 3;
693:     break;
694:   case PETSC_DRAW_QUARTER_SIZE:
695:     h = draw->h = (ymax - 40) / 4;
696:     break;
697:   }

699:   PetscOptionsGetBool(((PetscObject)draw)->options, ((PetscObject)draw)->prefix, "-draw_virtual", &dvirtual, NULL);

701:   if (!dvirtual) {
702:     /* allow user to set location and size of window */
703:     xywh[0] = x;
704:     xywh[1] = y;
705:     xywh[2] = w;
706:     xywh[3] = h;
707:     PetscOptionsGetIntArray(((PetscObject)draw)->options, ((PetscObject)draw)->prefix, "-geometry", xywh, &osize, NULL);
708:     x = (int)xywh[0];
709:     y = (int)xywh[1];
710:     w = (int)xywh[2];
711:     h = (int)xywh[3];
712:     if (w == PETSC_DECIDE || w == PETSC_DEFAULT) w = 300;
713:     if (h == PETSC_DECIDE || h == PETSC_DEFAULT) h = 300;
714:     draw->x = x;
715:     draw->y = y;
716:     draw->w = w;
717:     draw->h = h;

719:     if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) {
720:       /*
721:        PETSc tries to place windows starting in the upper left corner
722:         and moving across to the right.

724:        +0,0-------------------------------------------+
725:        |  Region used so far  +xavailable,yavailable  |
726:        |                      |                       |
727:        |                      |                       |
728:        +--------------------- +ybottom                |
729:        |                                              |
730:        |                                              |
731:        +----------------------------------------------+xmax,ymax

733:       */
734:       /*  First: can we add it to the right? */
735:       if (xavailable + w + 10 <= xmax) {
736:         x       = xavailable;
737:         y       = yavailable;
738:         ybottom = PetscMax(ybottom, y + h + 30);
739:       } else {
740:         /* No, so add it below on the left */
741:         xavailable = x = 0;
742:         yavailable = y = ybottom;
743:         ybottom        = ybottom + h + 30;
744:       }
745:     }
746:     /* update available region */
747:     xavailable = PetscMax(xavailable, x + w + 10);
748:     if (xavailable >= xmax) {
749:       xavailable = 0;
750:       yavailable = yavailable + h + 30;
751:       ybottom    = yavailable;
752:     }
753:     if (yavailable >= ymax) {
754:       y          = 0;
755:       yavailable = 0;
756:       ybottom    = 0;
757:     }

759:   } /* endif (!dvirtual) */

761:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw), &rank);

764:   PetscNew(&Xwin);
765:   PetscMemcpy(draw->ops, &DvOps, sizeof(DvOps));
766:   draw->data = (void *)Xwin;

768:   PetscDrawXiInit(Xwin, draw->display);
769:   if (!dvirtual) {
770:     Xwin->x = x;
771:     Xwin->y = y;
772:     Xwin->w = w;
773:     Xwin->h = h;
774:     if (rank == 0) PetscDrawXiQuickWindow(Xwin, draw->title, x, y, w, h);
775:     MPI_Bcast(&Xwin->win, 1, MPI_UNSIGNED_LONG, 0, PetscObjectComm((PetscObject)draw));
776:     if (rank) PetscDrawXiQuickWindowFromWindow(Xwin, Xwin->win);
777:   } else {
778:     Xwin->x = 0;
779:     Xwin->y = 0;
780:     Xwin->w = w;
781:     Xwin->h = h;
782:     PetscDrawXiColormap(Xwin);
783:     if (rank == 0) PetscDrawXiQuickPixmap(Xwin);
784:     MPI_Bcast(&Xwin->drw, 1, MPI_UNSIGNED_LONG, 0, PetscObjectComm((PetscObject)draw));
785:   }
786:   PetscDrawXiGetGeometry(Xwin, &Xwin->x, &Xwin->y, &Xwin->w, &Xwin->h);
787:   draw->x = Xwin->x;
788:   draw->y = Xwin->y;
789:   draw->w = Xwin->w;
790:   draw->h = Xwin->h;

792:   PetscOptionsGetBool(((PetscObject)draw)->options, ((PetscObject)draw)->prefix, "-draw_double_buffer", &doublebuffer, NULL);
793:   if (doublebuffer) PetscDrawSetDoubleBuffer(draw);
794:   return 0;
795: }