Actual source code: xops.c

petsc-3.14.6 2021-03-30
Report Typos and Errors
  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))


 17: static PetscErrorCode PetscDrawSetViewport_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
 18: {
 19:   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
 20:   int            xa,ya,xb,yb,xmax = XiWin->w-1,ymax = XiWin->h-1;
 21:   XRectangle     box;

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

 35: static PetscErrorCode PetscDrawCoordinateToPixel_X(PetscDraw draw,PetscReal x,PetscReal y,int *i,int *j)
 36: {
 37:   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;

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

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

 61:   xx = XTRANS(draw,XiWin,x);
 62:   yy = YTRANS(draw,XiWin,y);
 63:   PetscDrawXiSetColor(XiWin,c);
 64:   for (i=-1; i<2; i++) {
 65:     for (j=-1; j<2; j++) {
 66:       XDrawPoint(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx+i,yy+j);
 67:     }
 68:   }
 69:   return(0);
 70: }

 72: static PetscErrorCode PetscDrawPointPixel_X(PetscDraw draw,int x,int y,int c)
 73: {
 74:   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;

 77:   PetscDrawXiSetColor(XiWin,c);
 78:   XDrawPoint(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x,y);
 79:   return(0);
 80: }

 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;

 88:   PetscDrawXiSetColor(XiWin,cl);
 89:   x_1 = XTRANS(draw,XiWin,xl); x_2  = XTRANS(draw,XiWin,xr);
 90:   y_1 = YTRANS(draw,XiWin,yl); y_2  = YTRANS(draw,XiWin,yr);
 91:   XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_1,y_1,x_2,y_2);
 92:   return(0);
 93: }

 95: static PetscErrorCode PetscDrawArrow_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
 96: {
 97:   PetscDraw_X *XiWin = (PetscDraw_X*)draw->data;
 98:   int         x_1,y_1,x_2,y_2;

101:   PetscDrawXiSetColor(XiWin,cl);
102:   x_1 = XTRANS(draw,XiWin,xl); x_2 = XTRANS(draw,XiWin,xr);
103:   y_1 = YTRANS(draw,XiWin,yl); y_2 = YTRANS(draw,XiWin,yr);
104:   XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_1,y_1,x_2,y_2);
105:   if (x_1 == x_2 && y_1 == y_2) return(0);
106:   if (x_1 == x_2 && PetscAbs(y_1 - y_2) > 7) {
107:     if (y_2 > y_1) {
108:       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2,y_2,x_2-3,y_2-3);
109:       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2,y_2,x_2+3,y_2-3);
110:     } else {
111:       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2,y_2,x_2-3,y_2+3);
112:       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2,y_2,x_2+3,y_2+3);
113:     }
114:   }
115:   if (y_1 == y_2 && PetscAbs(x_1 - x_2) > 7) {
116:     if (x_2 > x_1) {
117:       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2-3,y_2-3,x_2,y_2);
118:       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2-3,y_2+3,x_2,y_2);
119:     } else {
120:       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2,y_2,x_2+3,y_2-3);
121:       XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x_2,y_2,x_2+3,y_2+3);
122:     }
123:   }
124:   return(0);
125: }

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

133:   PetscDrawXiSetColor(XiWin,c);
134:   x = XTRANS(draw,XiWin,xl); w = XTRANS(draw,XiWin,xr) + 1 - x; if (w <= 0) w = 1;
135:   y = YTRANS(draw,XiWin,yr); h = YTRANS(draw,XiWin,yl) + 1 - y; if (h <= 0) h = 1;
136:   XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x,y,w,h);
137:   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;

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

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

155: static PetscErrorCode PetscDrawTriangle_X(PetscDraw draw,PetscReal X1,PetscReal Y_1,PetscReal X2,PetscReal Y2,PetscReal X3,PetscReal Y3,int c1,int c2,int c3)
156: {
157:   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;

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

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

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

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

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

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

220:   xx = XTRANS(draw,XiWin,x);
221:   yy = YTRANS(draw,XiWin,y);
222:   PetscDrawXiSetColor(XiWin,c);

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

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

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

253: static PetscErrorCode PetscDrawFlush_X(PetscDraw draw)
254: {
255:   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
256:   PetscMPIInt    rank;

260:   /* make sure the X server processed requests from all processes */
261:   PetscDrawCollectiveBegin(draw);
262:   XSync(XiWin->disp,False);
263:   PetscDrawCollectiveEnd(draw);
264:   MPI_Barrier(PetscObjectComm((PetscObject)draw));

266:   /* transfer pixmap contents to window (only the first process does this) */
267:   if (XiWin->drw && XiWin->win) {
268:     MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);
269:     PetscDrawCollectiveBegin(draw);
270:     if (!rank) XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
271:     if (!rank) XSync(XiWin->disp,False);
272:     PetscDrawCollectiveEnd(draw);
273:     MPI_Barrier(PetscObjectComm((PetscObject)draw));
274:   }
275:   return(0);
276: }

278: static PetscErrorCode PetscDrawClear_X(PetscDraw draw)
279: {
280:   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
281:   int            xmax = XiWin->w-1,  ymax = XiWin->h-1;
282:   PetscReal      xl = draw->port_xl, yl = draw->port_yl;
283:   PetscReal      xr = draw->port_xr, yr = draw->port_yr;
284:   PetscMPIInt    rank;

288:   /* make sure the X server processed requests from all processes */
289:   PetscDrawCollectiveBegin(draw);
290:   XSync(XiWin->disp,False);
291:   PetscDrawCollectiveEnd(draw);
292:   MPI_Barrier(PetscObjectComm((PetscObject)draw));

294:   /* only the first process handles the clearing business */
295:   PetscDrawCollectiveBegin(draw);
296:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);
297:   if (!rank) {
298:     int xa = (int)(xl*xmax), ya = ymax - (int)(yr*ymax);
299:     int xb = (int)(xr*xmax), yb = ymax - (int)(yl*ymax);
300:     unsigned int w = (unsigned int)(xb + 1 - xa);
301:     unsigned int h = (unsigned int)(yb + 1 - ya);
302:     PetscDrawXiSetPixVal(XiWin,XiWin->background);
303:     XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xa,ya,w,h);
304:     XSync(XiWin->disp,False);
305:   }
306:   PetscDrawCollectiveEnd(draw);
307:   MPI_Barrier(PetscObjectComm((PetscObject)draw));
308:   return(0);
309: }

311: static PetscErrorCode PetscDrawSetDoubleBuffer_X(PetscDraw draw)
312: {
313:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
314:   PetscMPIInt    rank;

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

321:   PetscDrawCollectiveBegin(draw);
322:   if (!rank) {PetscDrawXiQuickPixmap(win);}
323:   PetscDrawCollectiveEnd(draw);
324:   MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));
325:   return(0);
326: }

328: static PetscErrorCode PetscDrawGetPopup_X(PetscDraw draw,PetscDraw *popup)
329: {
330:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
331:   PetscBool      flg  = PETSC_TRUE;

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

338:   PetscDrawCreate(PetscObjectComm((PetscObject)draw),draw->display,NULL,win->x,win->y+win->h+10,220,220,popup);
339:   PetscObjectSetOptionsPrefix((PetscObject)*popup,"popup_");
340:   PetscObjectAppendOptionsPrefix((PetscObject)*popup,((PetscObject)draw)->prefix);
341:   PetscDrawSetType(*popup,PETSC_DRAW_X);
342:   draw->popup = *popup;
343:   return(0);
344: }

346: static PetscErrorCode PetscDrawSetTitle_X(PetscDraw draw,const char title[])
347: {
348:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
349:   PetscMPIInt    rank;

353:   if (!win->win) return(0);
354:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);
355:   PetscDrawCollectiveBegin(draw);
356:   if (!rank) {
357:     size_t        len;
358:     XTextProperty prop;
359:     PetscStrlen(title,&len);
360:     XGetWMName(win->disp,win->win,&prop);
361:     XFree((void*)prop.value);
362:     prop.value  = (unsigned char*)title;
363:     prop.nitems = (long)len;
364:     XSetWMName(win->disp,win->win,&prop);
365:   }
366:   PetscDrawCollectiveEnd(draw);
367:   return(0);
368: }

370: static PetscErrorCode PetscDrawCheckResizedWindow_X(PetscDraw draw)
371: {
372:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
373:   int            xywh[4];
374:   PetscMPIInt    rank;

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

381:   PetscDrawCollectiveBegin(draw);
382:   if (!rank) {PetscDrawXiGetGeometry(win,xywh,xywh+1,xywh+2,xywh+3);}
383:   PetscDrawCollectiveEnd(draw);
384:   MPI_Bcast(xywh,4,MPI_INT,0,PetscObjectComm((PetscObject)draw));

386:   /* record new window position */
387:   draw->x = win->x = xywh[0];
388:   draw->y = win->y = xywh[1];
389:   if (xywh[2] == win->w && xywh[3] == win->h) return(0);
390:   /* record new window sizes */
391:   draw->w = win->w = xywh[2];
392:   draw->h = win->h = xywh[3];

394:   /* recreate pixmap (only first processor does this) */
395:   PetscDrawCollectiveBegin(draw);
396:   if (!rank && win->drw) {PetscDrawXiQuickPixmap(win);}
397:   PetscDrawCollectiveEnd(draw);
398:   MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));
399:   /* reset the clipping */
400:   PetscDrawSetViewport_X(draw,draw->port_xl,draw->port_yl,draw->port_xr,draw->port_yr);
401:   return(0);
402: }

404: static PetscErrorCode PetscDrawResizeWindow_X(PetscDraw draw,int w,int h)
405: {
406:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
407:   PetscMPIInt    rank;

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

414:   if (win->win) {
415:     PetscDrawCollectiveBegin(draw);
416:     if (!rank) {PetscDrawXiResizeWindow(win,w,h);}
417:     PetscDrawCollectiveEnd(draw);
418:     PetscDrawCheckResizedWindow_X(draw);
419:   } else if (win->drw) {
420:     draw->w = win->w = w; draw->h = win->h = h;
421:     /* recreate pixmap (only first processor does this) */
422:     PetscDrawCollectiveBegin(draw);
423:     if (!rank) {PetscDrawXiQuickPixmap(win);}
424:     MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));
425:     /* reset the clipping */
426:     PetscDrawCollectiveEnd(draw);
427:     PetscDrawSetViewport_X(draw,draw->port_xl,draw->port_yl,draw->port_xr,draw->port_yr);
428:   }
429:   return(0);
430: }

432: #include <X11/cursorfont.h>

434: static PetscErrorCode PetscDrawGetMouseButton_X(PetscDraw draw,PetscDrawButton *button,PetscReal *x_user,PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys)
435: {
436:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
437:   Cursor         cursor;
438:   XEvent         report;
439:   Window         root,child;
440:   int            root_x,root_y,px=0,py=0;
441:   unsigned int   w,h,border,depth;
442:   unsigned int   keys_button;
443:   PetscMPIInt    rank;
444:   PetscReal      xx,yy;

448:   *button = PETSC_BUTTON_NONE;
449:   if (!win->win) return(0);
450:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);

452:   PetscDrawCollectiveBegin(draw);
453:   if (rank) goto finally;

455:   /* change cursor to indicate input */
456:   cursor = XCreateFontCursor(win->disp,XC_hand2); if (!cursor) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to create X cursor");
457:   XDefineCursor(win->disp,win->win,cursor);
458:   /* wait for mouse button events */
459:   XSelectInput(win->disp,win->win,ButtonPressMask|ButtonReleaseMask);
460:   while (XCheckTypedEvent(win->disp,ButtonPress,&report));
461:   XMaskEvent(win->disp,ButtonReleaseMask,&report);
462:   /* get mouse pointer coordinates */
463:   XQueryPointer(win->disp,report.xmotion.window,&root,&child,&root_x,&root_y,&px,&py,&keys_button);
464:   /* the user may resize the window before pressing the mouse button */
465:   XGetGeometry(win->disp,win->win,&root,&root_x,&root_y,&w,&h,&border,&depth);
466:   /* cleanup input event handler and cursor  */
467:   XSelectInput(win->disp,win->win,NoEventMask);
468:   XUndefineCursor(win->disp,win->win);
469:   XFreeCursor(win->disp, cursor);
470:   XSync(win->disp,False);

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

493: finally:
494:   PetscDrawCollectiveEnd(draw);
495:   PetscDrawCheckResizedWindow_X(draw);
496:   return(0);
497: }

499: static PetscErrorCode PetscDrawPause_X(PetscDraw draw)
500: {
501:   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;

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

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

531: static struct _PetscDrawOps DvOps = { PetscDrawSetDoubleBuffer_X,
532:                                       PetscDrawFlush_X,
533:                                       PetscDrawLine_X,
534:                                       NULL,
535:                                       NULL,
536:                                       PetscDrawPoint_X,
537:                                       NULL,
538:                                       PetscDrawString_X,
539:                                       PetscDrawStringVertical_X,
540:                                       PetscDrawStringSetSize_X,
541:                                       PetscDrawStringGetSize_X,
542:                                       PetscDrawSetViewport_X,
543:                                       PetscDrawClear_X,
544:                                       PetscDrawRectangle_X,
545:                                       PetscDrawTriangle_X,
546:                                       PetscDrawEllipse_X,
547:                                       PetscDrawGetMouseButton_X,
548:                                       PetscDrawPause_X,
549:                                       NULL,
550:                                       NULL,
551:                                       PetscDrawGetPopup_X,
552:                                       PetscDrawSetTitle_X,
553:                                       PetscDrawCheckResizedWindow_X,
554:                                       PetscDrawResizeWindow_X,
555:                                       PetscDrawDestroy_X,
556:                                       NULL,
557:                                       PetscDrawGetSingleton_X,
558:                                       PetscDrawRestoreSingleton_X,
559:                                       NULL,
560:                                       PetscDrawGetImage_X,
561:                                       NULL,
562:                                       PetscDrawArrow_X,
563:                                       PetscDrawCoordinateToPixel_X,
564:                                       PetscDrawPixelToCoordinate_X,
565:                                       PetscDrawPointPixel_X,
566:                                       NULL};


569: static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw draw,PetscDraw *sdraw)
570: {
571:   PetscDraw_X    *Xwin = (PetscDraw_X*)draw->data,*sXwin;

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

579:   if (draw->popup) {
580:     PetscDrawGetSingleton(draw->popup,&(*sdraw)->popup);
581:   }
582:   (*sdraw)->pause   = draw->pause;
583:   (*sdraw)->coor_xl = draw->coor_xl;
584:   (*sdraw)->coor_xr = draw->coor_xr;
585:   (*sdraw)->coor_yl = draw->coor_yl;
586:   (*sdraw)->coor_yr = draw->coor_yr;
587:   (*sdraw)->port_xl = draw->port_xl;
588:   (*sdraw)->port_xr = draw->port_xr;
589:   (*sdraw)->port_yl = draw->port_yl;
590:   (*sdraw)->port_yr = draw->port_yr;

592:   /* share drawables (windows and/or pixmap) from the parent draw */
593:   PetscNewLog(*sdraw,&sXwin);
594:   (*sdraw)->data = (void*)sXwin;
595:   PetscDrawXiInit(sXwin,draw->display);
596:   if (Xwin->win) {
597:     PetscDrawXiQuickWindowFromWindow(sXwin,Xwin->win);
598:     sXwin->drw = Xwin->drw; /* XXX If the window is ever resized, this is wrong! */
599:   } else if (Xwin->drw) {
600:     PetscDrawXiColormap(sXwin);
601:     sXwin->drw = Xwin->drw;
602:   }
603:   PetscDrawXiGetGeometry(sXwin,&sXwin->x,&sXwin->y,&sXwin->w,&sXwin->h);
604:   (*sdraw)->x = sXwin->x; (*sdraw)->y = sXwin->y;
605:   (*sdraw)->w = sXwin->w; (*sdraw)->h = sXwin->h;
606:   return(0);
607: }

609: static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw draw,PetscDraw *sdraw)
610: {

614:   if (draw->popup && (*sdraw)->popup) {
615:     PetscBool isdrawx;
616:     PetscDraw_X *pXwin = (PetscDraw_X*)draw->popup->data;
617:     PetscDraw_X *sXwin = (PetscDraw_X*)(*sdraw)->popup->data;
618:     PetscObjectTypeCompare((PetscObject)draw->popup,PETSC_DRAW_X,&isdrawx);
619:     if (!isdrawx) goto finally;
620:     PetscObjectTypeCompare((PetscObject)(*sdraw)->popup,PETSC_DRAW_X,&isdrawx);
621:     if (!isdrawx) goto finally;
622:     if (sXwin->win == pXwin->win) {
623:       PetscDrawRestoreSingleton(draw->popup,&(*sdraw)->popup);
624:     }
625:   }
626: finally:
627:   PetscDrawDestroy(sdraw);
628:   return(0);
629: }

631: static PetscErrorCode PetscDrawXGetDisplaySize_Private(const char name[],int *width,int *height,PetscBool *has_display)
632: {
633:   Display *display;

636:   display = XOpenDisplay(name);
637:   if (!display) {
638:     *width  = *height = 0;
639:     (*PetscErrorPrintf)("Unable to open display on %s\n\
640:     Make sure your COMPUTE NODES are authorized to connect\n\
641:     to this X server and either your DISPLAY variable\n\
642:     is set or you use the -display name option\n",name);
643:     *has_display = PETSC_FALSE;
644:     return(0);
645:   }
646:   *has_display = PETSC_TRUE;
647:   *width  = (int)DisplayWidth(display,DefaultScreen(display));
648:   *height = (int)DisplayHeight(display,DefaultScreen(display));
649:   XCloseDisplay(display);
650:   return(0);
651: }

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

656:    Options Database Keys:
657: +  -display <display> - sets the display to use
658: .  -x_virtual - forces use of a X virtual display Xvfb that will not display anything but -draw_save will still work.
659:                 Xvfb is automatically started up in PetscSetDisplay() with this option
660: .  -draw_size w,h - percentage of screeen (either 1, .5, .3, .25), or size in pixels
661: .  -geometry x,y,w,h - set location and size in pixels
662: .  -draw_virtual - do not open a window (draw on a pixmap), -draw_save will still work
663: -  -draw_double_buffer - avoid window flickering (draw on pixmap and flush to window)

665:    Level: beginner

667: .seealso:  PetscDrawOpenX(), PetscDrawSetDisplay(), PetscDrawSetFromOptions()

669: M*/

671: PETSC_EXTERN PetscErrorCode PetscDrawCreate_X(PetscDraw draw)
672: {
673:   PetscDraw_X    *Xwin;
675:   PetscMPIInt    rank;
676:   int            x = draw->x,y = draw->y,w = draw->w,h = draw->h;
677:   static int     xavailable = 0,yavailable = 0,ybottom = 0,xmax = 0,ymax = 0;
678:   PetscBool      set,dvirtual = PETSC_FALSE,doublebuffer = PETSC_TRUE,has_display;
679:   PetscInt       xywh[4],osize = 4,nsizes=2;
680:   PetscReal      sizes[2] = {.3,.3};
681:   static size_t  DISPLAY_LENGTH = 265;

684:   /* get the display variable */
685:   if (!draw->display) {
686:     PetscMalloc1(DISPLAY_LENGTH,&draw->display);
687:     PetscGetDisplay(draw->display,DISPLAY_LENGTH);
688:   }

690:   /* initialize the display size */
691:   if (!xmax) {
692:     PetscDrawXGetDisplaySize_Private(draw->display,&xmax,&ymax,&has_display);
693:     /* if some processors fail on this and others succed then this is a problem ! */
694:     if (!has_display) {
695:       (*PetscErrorPrintf)("PETSc unable to use X windows\nproceeding without graphics\n");
696:       PetscDrawSetType(draw,PETSC_DRAW_NULL);
697:       return(0);
698:     }
699:   }

701:   /* allow user to set size of drawable */
702:   PetscOptionsGetRealArray(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-draw_size",sizes,&nsizes,&set);
703:   if (set && nsizes == 1 && sizes[0] > 1.0) sizes[1] = sizes[0];
704:   if (set) {
705:     if (sizes[0] > 1.0)       w = (int)sizes[0];
706:     else if (sizes[0] == 1.0) w = PETSC_DRAW_FULL_SIZE;
707:     else if (sizes[0] == .5)  w = PETSC_DRAW_HALF_SIZE;
708:     else if (sizes[0] == .3)  w = PETSC_DRAW_THIRD_SIZE;
709:     else if (sizes[0] == .25) w = PETSC_DRAW_QUARTER_SIZE;
710:     if (sizes[1] > 1.0)       h = (int)sizes[1];
711:     else if (sizes[1] == 1.0) h = PETSC_DRAW_FULL_SIZE;
712:     else if (sizes[1] == .5)  h = PETSC_DRAW_HALF_SIZE;
713:     else if (sizes[1] == .3)  h = PETSC_DRAW_THIRD_SIZE;
714:     else if (sizes[1] == .25) h = PETSC_DRAW_QUARTER_SIZE;
715:   }
716:   if (w == PETSC_DECIDE || w == PETSC_DEFAULT) w = draw->w = 300;
717:   if (h == PETSC_DECIDE || h == PETSC_DEFAULT) h = draw->h = 300;
718:   switch (w) {
719:   case PETSC_DRAW_FULL_SIZE:    w = draw->w = (xmax - 10);   break;
720:   case PETSC_DRAW_HALF_SIZE:    w = draw->w = (xmax - 20)/2; break;
721:   case PETSC_DRAW_THIRD_SIZE:   w = draw->w = (xmax - 30)/3; break;
722:   case PETSC_DRAW_QUARTER_SIZE: w = draw->w = (xmax - 40)/4; break;
723:   }
724:   switch (h) {
725:   case PETSC_DRAW_FULL_SIZE:    h = draw->h = (ymax - 10);   break;
726:   case PETSC_DRAW_HALF_SIZE:    h = draw->h = (ymax - 20)/2; break;
727:   case PETSC_DRAW_THIRD_SIZE:   h = draw->h = (ymax - 30)/3; break;
728:   case PETSC_DRAW_QUARTER_SIZE: h = draw->h = (ymax - 40)/4; break;
729:   }

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

733:   if (!dvirtual) {

735:     /* allow user to set location and size of window */
736:     xywh[0] = x; xywh[1] = y; xywh[2] = w; xywh[3] = h;
737:     PetscOptionsGetIntArray(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-geometry",xywh,&osize,NULL);
738:     x = (int)xywh[0]; y = (int)xywh[1]; w = (int)xywh[2]; h = (int)xywh[3];
739:     if (w == PETSC_DECIDE || w == PETSC_DEFAULT) w = 300;
740:     if (h == PETSC_DECIDE || h == PETSC_DEFAULT) h = 300;
741:     draw->x = x; draw->y = y; draw->w = w; draw->h = h;

743:     if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) {
744:       /*
745:        PETSc tries to place windows starting in the upper left corner
746:         and moving across to the right.

748:        +0,0-------------------------------------------+
749:        |  Region used so far  +xavailable,yavailable  |
750:        |                      |                       |
751:        |                      |                       |
752:        +--------------------- +ybottom                |
753:        |                                              |
754:        |                                              |
755:        +----------------------------------------------+xmax,ymax

757:       */
758:       /*  First: can we add it to the right? */
759:       if (xavailable + w + 10 <= xmax) {
760:         x       = xavailable;
761:         y       = yavailable;
762:         ybottom = PetscMax(ybottom,y + h + 30);
763:       } else {
764:         /* No, so add it below on the left */
765:         xavailable = x = 0;
766:         yavailable = y = ybottom;
767:         ybottom    = ybottom + h + 30;
768:       }
769:     }
770:     /* update available region */
771:     xavailable = PetscMax(xavailable,x + w + 10);
772:     if (xavailable >= xmax) {
773:       xavailable = 0;
774:       yavailable = yavailable + h + 30;
775:       ybottom    = yavailable;
776:     }
777:     if (yavailable >= ymax) {
778:       y          = 0;
779:       yavailable = 0;
780:       ybottom    = 0;
781:     }

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

785:   MPI_Comm_rank(PetscObjectComm((PetscObject)draw),&rank);
786:   if (!rank && (w <= 0 || h <= 0)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative window width or height");

788:   PetscNewLog(draw,&Xwin);
789:   PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));
790:   draw->data = (void*)Xwin;

792:   PetscDrawXiInit(Xwin,draw->display);
793:   if (!dvirtual) {
794:     Xwin->x = x; Xwin->y = y;
795:     Xwin->w = w; Xwin->h = h;
796:     if (!rank) {PetscDrawXiQuickWindow(Xwin,draw->title,x,y,w,h);}
797:     MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));
798:     if (rank) {PetscDrawXiQuickWindowFromWindow(Xwin,Xwin->win);}
799:   } else {
800:     Xwin->x = 0; Xwin->y = 0;
801:     Xwin->w = w; Xwin->h = h;
802:     PetscDrawXiColormap(Xwin);
803:     if (!rank) {PetscDrawXiQuickPixmap(Xwin);}
804:     MPI_Bcast(&Xwin->drw,1,MPI_UNSIGNED_LONG,0,PetscObjectComm((PetscObject)draw));
805:   }
806:   PetscDrawXiGetGeometry(Xwin,&Xwin->x,&Xwin->y,&Xwin->w,&Xwin->h);
807:   draw->x = Xwin->x; draw->y = Xwin->y;
808:   draw->w = Xwin->w; draw->h = Xwin->h;

810:   PetscOptionsGetBool(((PetscObject)draw)->options,((PetscObject)draw)->prefix,"-draw_double_buffer",&doublebuffer,NULL);
811:   if (doublebuffer) {PetscDrawSetDoubleBuffer(draw);}
812:   return(0);
813: }