Actual source code: xops.c

petsc-3.3-p7 2013-05-11
  2: /*
  3:     Defines the operations for the X PetscDraw implementation.
  4: */

  6: #include <../src/sys/draw/impls/x/ximpl.h>         /*I  "petscsys.h" I*/

  8: /*
  9:      These macros transform from the users coordinates to the 
 10:    X-window pixel coordinates.
 11: */
 12: #define XTRANS(draw,xwin,x) \
 13:    (int)(((xwin)->w)*((draw)->port_xl + (((x - (draw)->coor_xl)*((draw)->port_xr - (draw)->port_xl))/((draw)->coor_xr - (draw)->coor_xl))))
 14: #define YTRANS(draw,xwin,y) \
 15:    (int)(((xwin)->h)*(1.0-(draw)->port_yl - (((y - (draw)->coor_yl)*((draw)->port_yr - (draw)->port_yl))/((draw)->coor_yr - (draw)->coor_yl))))

 19: PetscErrorCode PetscDrawLine_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
 20: {
 21:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
 22:   int          x1,y_1,x2,y2;

 25:   PetscDrawXiSetColor(XiWin,cl);
 26:   x1 = XTRANS(draw,XiWin,xl);   x2  = XTRANS(draw,XiWin,xr);
 27:   y_1 = YTRANS(draw,XiWin,yl);   y2  = YTRANS(draw,XiWin,yr);
 28:   if (x1 == x2 && y_1 == y2) return(0);
 29:   XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x1,y_1,x2,y2);
 30:   return(0);
 31: }

 35: PetscErrorCode PetscDrawArrow_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
 36: {
 37:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
 38:   int          x1,y_1,x2,y2;

 41:   PetscDrawXiSetColor(XiWin,cl);
 42:   x1 = XTRANS(draw,XiWin,xl);   x2  = XTRANS(draw,XiWin,xr);
 43:   y_1 = YTRANS(draw,XiWin,yl);   y2  = YTRANS(draw,XiWin,yr);
 44:   if (x1 == x2 && y_1 == y2) return(0);
 45:   XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x1,y_1,x2,y2);
 46:   if (x1 == x2 && PetscAbs(y_1 - y2) > 7) {
 47:     if (y2 > y_1) {
 48:        XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x2,y2,x2-3,y2-3);
 49:        XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x2,y2,x2+3,y2-3);
 50:     } else {
 51:        XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x2,y2,x2-3,y2+3);
 52:        XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x2,y2,x2+3,y2+3);
 53:     }
 54:   }
 55:   return(0);
 56: }

 60: static PetscErrorCode PetscDrawPoint_X(PetscDraw draw,PetscReal x,PetscReal  y,int c)
 61: {
 62:   int          xx,yy;
 63:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

 66:   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
 67:   PetscDrawXiSetColor(XiWin,c);
 68:   XDrawPoint(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy);
 69:   return(0);
 70: }

 74: static PetscErrorCode PetscDrawRectangle_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c1,int c2,int c3,int c4)
 75: {
 76:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
 77:   int          x1,y_1,w,h,c = (c1 + c2 + c3 + c4)/4;

 80:   PetscDrawXiSetColor(XiWin,c);
 81:   x1 = XTRANS(draw,XiWin,xl);   w  = XTRANS(draw,XiWin,xr) - x1;
 82:   y_1 = YTRANS(draw,XiWin,yr);   h  = YTRANS(draw,XiWin,yl) - y_1;
 83:   if (w <= 0) w = 1; if (h <= 0) h = 1;
 84:   XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x1,y_1,w,h);
 85:   return(0);
 86: }

 90: static PetscErrorCode PetscDrawEllipse_X(PetscDraw Win, PetscReal x, PetscReal y, PetscReal a, PetscReal b, int c)
 91: {
 92:   PetscDraw_X* XiWin = (PetscDraw_X*) Win->data;
 93:   int          xA, yA, w, h;

 96:   PetscDrawXiSetColor(XiWin, c);
 97:   xA = XTRANS(Win, XiWin, x - a/2.0); w = XTRANS(Win, XiWin, x + a/2.0) - xA;
 98:   yA = YTRANS(Win, XiWin, y + b/2.0); h = YTRANS(Win, XiWin, y - b/2.0) - yA;
 99:   XFillArc(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, xA, yA, w, h, 0, 23040);
100:   return(0);
101: }

103: extern PetscErrorCode PetscDrawInterpolatedTriangle_X(PetscDraw_X*,int,int,int,int,int,int,int,int,int);

107: 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)
108: {
109:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;

113:   if (c1 == c2 && c2 == c3) {
114:     XPoint pt[3];
115:     PetscDrawXiSetColor(XiWin,c1);
116:     pt[0].x = XTRANS(draw,XiWin,X1);
117:     pt[0].y = YTRANS(draw,XiWin,Y_1);
118:     pt[1].x = XTRANS(draw,XiWin,X2);
119:     pt[1].y = YTRANS(draw,XiWin,Y2);
120:     pt[2].x = XTRANS(draw,XiWin,X3);
121:     pt[2].y = YTRANS(draw,XiWin,Y3);
122:     XFillPolygon(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,pt,3,Convex,CoordModeOrigin);
123:   } else {
124:     int x1,y_1,x2,y2,x3,y3;
125:     x1   = XTRANS(draw,XiWin,X1);
126:     y_1  = YTRANS(draw,XiWin,Y_1);
127:     x2   = XTRANS(draw,XiWin,X2);
128:     y2   = YTRANS(draw,XiWin,Y2);
129:     x3   = XTRANS(draw,XiWin,X3);
130:     y3   = YTRANS(draw,XiWin,Y3);
131:     PetscDrawInterpolatedTriangle_X(XiWin,x1,y_1,c1,x2,y2,c2,x3,y3,c3);
132:   }
133:   return(0);
134: }

138: static PetscErrorCode PetscDrawString_X(PetscDraw draw,PetscReal x,PetscReal  y,int c,const char chrs[])
139: {
141:   int            xx,yy;
142:   size_t         len;
143:   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
144:   char           *substr;
145:   PetscToken     token;

148:   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
149:   PetscDrawXiSetColor(XiWin,c);
150: 
151:   PetscTokenCreate(chrs,'\n',&token);
152:   PetscTokenFind(token,&substr);
153:   PetscStrlen(substr,&len);
154:   XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
155:   PetscTokenFind(token,&substr);
156:   while (substr) {
157:     yy  += 4*XiWin->font->font_descent;
158:     PetscStrlen(substr,&len);
159:     XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
160:     PetscTokenFind(token,&substr);
161:   }
162:   PetscTokenDestroy(&token);

164:   return(0);
165: }

167: extern PetscErrorCode PetscDrawXiFontFixed(PetscDraw_X*,int,int,PetscDrawXiFont **);

171: static PetscErrorCode PetscDrawStringSetSize_X(PetscDraw draw,PetscReal x,PetscReal  y)
172: {
173:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;
175:   int            w,h;

178:   w = (int)((XiWin->w)*x*(draw->port_xr - draw->port_xl)/(draw->coor_xr - draw->coor_xl));
179:   h = (int)((XiWin->h)*y*(draw->port_yr - draw->port_yl)/(draw->coor_yr - draw->coor_yl));
180:   PetscFree(XiWin->font);
181:   PetscDrawXiFontFixed(XiWin,w,h,&XiWin->font);
182:   return(0);
183: }

187: PetscErrorCode PetscDrawStringGetSize_X(PetscDraw draw,PetscReal *x,PetscReal  *y)
188: {
189:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
190:   PetscReal    w,h;

193:   w = XiWin->font->font_w; h = XiWin->font->font_h;
194:   *x = w*(draw->coor_xr - draw->coor_xl)/((XiWin->w)*(draw->port_xr - draw->port_xl));
195:   *y = h*(draw->coor_yr - draw->coor_yl)/((XiWin->h)*(draw->port_yr - draw->port_yl));
196:   return(0);
197: }

201: PetscErrorCode PetscDrawStringVertical_X(PetscDraw draw,PetscReal x,PetscReal  y,int c,const char chrs[])
202: {
204:   int            xx,yy;
205:   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
206:   char           tmp[2];
207:   PetscReal      tw,th;
208:   size_t         i,n;
209: 
211:   PetscStrlen(chrs,&n);
212:   tmp[1] = 0;
213:   PetscDrawXiSetColor(XiWin,c);
214:   PetscDrawStringGetSize_X(draw,&tw,&th);
215:   xx = XTRANS(draw,XiWin,x);
216:   for (i=0; i<n; i++) {
217:     tmp[0] = chrs[i];
218:     yy = YTRANS(draw,XiWin,y-th*i);
219:     XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set, xx,yy - XiWin->font->font_descent,tmp,1);
220:   }
221:   return(0);
222: }

226: static PetscErrorCode PetscDrawFlush_X(PetscDraw draw)
227: {
228:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

231:   if (XiWin->drw) {
232:     XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
233:   }
234:   XFlush(XiWin->disp); XSync(XiWin->disp,False);
235:   return(0);
236: }

240: static PetscErrorCode PetscDrawSynchronizedFlush_X(PetscDraw draw)
241: {
243:   PetscMPIInt    rank;
244:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;

247:   XFlush(XiWin->disp);
248:   if (XiWin->drw) {
249:     MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
250:     /* make sure data has actually arrived at server */
251:     XSync(XiWin->disp,False);
252:     MPI_Barrier(((PetscObject)draw)->comm);
253:     if (!rank) {
254:       XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
255:       XFlush(XiWin->disp);
256:     }
257:     XSync(XiWin->disp,False);
258:     MPI_Barrier(((PetscObject)draw)->comm);
259:   } else {
260:     MPI_Barrier(((PetscObject)draw)->comm);
261:     XSync(XiWin->disp,False);
262:     MPI_Barrier(((PetscObject)draw)->comm);
263:   }
264:   return(0);
265: }

269: static PetscErrorCode PetscDrawSetViewport_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
270: {
271:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
272:   XRectangle   box;

275:   box.x = (int)(xl*XiWin->w);   box.y = (int)((1.0-yr)*XiWin->h);
276:   box.width = (int)((xr-xl)*XiWin->w);box.height = (int)((yr-yl)*XiWin->h);
277:   XSetClipRectangles(XiWin->disp,XiWin->gc.set,0,0,&box,1,Unsorted);
278:   return(0);
279: }

283: static PetscErrorCode PetscDrawClear_X(PetscDraw draw)
284: {
285:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;
286:   int            x, y, w, h;

290:   PetscDrawSave(draw);
291:   x = (int)(draw->port_xl*XiWin->w);
292:   w = (int)((draw->port_xr - draw->port_xl)*XiWin->w);
293:   y = (int)((1.0-draw->port_yr)*XiWin->h);
294:   h = (int)((draw->port_yr - draw->port_yl)*XiWin->h);
295:   PetscDrawXiSetPixVal(XiWin,XiWin->background);
296:   XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x,y,w,h);
297:   return(0);
298: }

302: static PetscErrorCode PetscDrawSynchronizedClear_X(PetscDraw draw)
303: {
305:   PetscMPIInt    rank;
306:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;

309:   MPI_Barrier(((PetscObject)draw)->comm);
310:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
311:   if (!rank) {
312:     PetscDrawClear_X(draw);
313:   }
314:   XFlush(XiWin->disp);
315:   MPI_Barrier(((PetscObject)draw)->comm);
316:   XSync(XiWin->disp,False);
317:   MPI_Barrier(((PetscObject)draw)->comm);
318:   return(0);
319: }

323: static PetscErrorCode PetscDrawSetDoubleBuffer_X(PetscDraw draw)
324: {
325:   PetscDraw_X*   win = (PetscDraw_X*)draw->data;
327:   PetscMPIInt   rank;

330:   if (win->drw) return(0);

332:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
333:   if (!rank) {
334:     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
335:   }
336:   /* try to make sure it is actually done before passing info to all */
337:   XSync(win->disp,False);
338:   MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);
339:   return(0);
340: }

342: #include <X11/cursorfont.h>

346: static PetscErrorCode PetscDrawGetMouseButton_X(PetscDraw draw,PetscDrawButton *button,PetscReal* x_user,PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys)
347: {
348:   XEvent       report;
349:   PetscDraw_X* win = (PetscDraw_X*)draw->data;
350:   Window       root,child;
351:   int          root_x,root_y,px,py;
352:   unsigned int keys_button;
353:   Cursor       cursor = 0;

356:   /* change cursor to indicate input */
357:   if (!cursor) {
358:     cursor = XCreateFontCursor(win->disp,XC_hand2);
359:     if (!cursor) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to create X cursor");
360:   }
361:   XDefineCursor(win->disp,win->win,cursor);
362:   XSelectInput(win->disp,win->win,ButtonPressMask | ButtonReleaseMask);

364:   while (XCheckTypedEvent(win->disp,ButtonPress,&report));
365:   XMaskEvent(win->disp,ButtonReleaseMask,&report);
366:   switch (report.xbutton.button) {
367:     case Button1:
368:       if (report.xbutton.state & ShiftMask)
369:         *button = PETSC_BUTTON_LEFT_SHIFT;
370:       else
371:         *button = PETSC_BUTTON_LEFT;
372:       break;
373:     case Button2:
374:       if (report.xbutton.state & ShiftMask)
375:         *button = PETSC_BUTTON_CENTER_SHIFT;
376:       else
377:         *button = PETSC_BUTTON_CENTER;
378:       break;
379:     case Button3:
380:       if (report.xbutton.state & ShiftMask)
381:         *button = PETSC_BUTTON_RIGHT_SHIFT;
382:       else
383:         *button = PETSC_BUTTON_RIGHT;
384:       break;
385:   }
386:   XQueryPointer(win->disp,report.xmotion.window,&root,&child,&root_x,&root_y,&px,&py,&keys_button);

388:   if (x_phys) *x_phys = ((double)px)/((double)win->w);
389:   if (y_phys) *y_phys = 1.0 - ((double)py)/((double)win->h);

391:   if (x_user) *x_user = draw->coor_xl + ((((double)px)/((double)win->w)-draw->port_xl))*(draw->coor_xr - draw->coor_xl)/(draw->port_xr - draw->port_xl);
392:   if (y_user) *y_user = draw->coor_yl + ((1.0 - ((double)py)/((double)win->h)-draw->port_yl))*(draw->coor_yr - draw->coor_yl)/(draw->port_yr - draw->port_yl);

394:   XUndefineCursor(win->disp,win->win);
395:   XFlush(win->disp); XSync(win->disp,False);
396:   return(0);
397: }

401: static PetscErrorCode PetscDrawPause_X(PetscDraw draw)
402: {

406:   if (draw->pause > 0) PetscSleep(draw->pause);
407:   else if (draw->pause < 0) {
408:     PetscDrawButton button;
409:     PetscMPIInt     rank;
410:     MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
411:     if (!rank) {
412:       PetscDrawGetMouseButton(draw,&button,0,0,0,0);
413:       if (button == PETSC_BUTTON_CENTER) draw->pause = 0;
414:     }
415:     MPI_Bcast(&draw->pause,1,MPI_INT,0,((PetscObject)draw)->comm);
416:   }
417:   return(0);
418: }

422: static PetscErrorCode PetscDrawGetPopup_X(PetscDraw draw,PetscDraw *popup)
423: {
425:   PetscDraw_X*   win = (PetscDraw_X*)draw->data;

428:   PetscDrawOpenX(((PetscObject)draw)->comm,PETSC_NULL,PETSC_NULL,win->x,win->y+win->h+36,220,220,popup);
429:   draw->popup = *popup;
430:   return(0);
431: }

435: static PetscErrorCode PetscDrawSetTitle_X(PetscDraw draw,const char title[])
436: {
437:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
438:   XTextProperty  prop;
440:   size_t         len;

443:   XGetWMName(win->disp,win->win,&prop);
444:   XFree((void*)prop.value);
445:   prop.value  = (unsigned char *)title;
446:   PetscStrlen(title,&len);
447:   prop.nitems = (long) len;
448:   XSetWMName(win->disp,win->win,&prop);
449:   return(0);
450: }

454: static PetscErrorCode PetscDrawResizeWindow_X(PetscDraw draw,int w,int h)
455: {
456:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
457:   unsigned int   ww,hh,border,depth;
458:   int            x,y;
460:   Window         root;

463:   XResizeWindow(win->disp,win->win,w,h);
464:   XGetGeometry(win->disp,win->win,&root,&x,&y,&ww,&hh,&border,&depth);
465:   PetscDrawCheckResizedWindow(draw);
466:   return(0);
467: }

471: static PetscErrorCode PetscDrawCheckResizedWindow_X(PetscDraw draw)
472: {
473:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
475:   int            x,y;
476:   PetscMPIInt    rank;
477:   Window         root;
478:   unsigned int   w,h,border,depth,geo[2];
479:   PetscReal      xl,xr,yl,yr;
480:   XRectangle     box;

483:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
484:   if (!rank) {
485:     XSync(win->disp,False);
486:     XGetGeometry(win->disp,win->win,&root,&x,&y,geo,geo+1,&border,&depth);
487:   }
488:   MPI_Bcast(geo,2,MPI_INT,0,((PetscObject)draw)->comm);
489:   w = geo[0];
490:   h = geo[1];
491:   if (w == (unsigned int) win->w && h == (unsigned int) win->h) return(0);

493:   /* record new window sizes */

495:   win->h = h; win->w = w;

497:   /* Free buffer space and create new version (only first processor does this) */
498:   if (win->drw) {
499:     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
500:   }
501:   /* reset the clipping */
502:   xl = draw->port_xl; yl = draw->port_yl;
503:   xr = draw->port_xr; yr = draw->port_yr;
504:   box.x     = (int)(xl*win->w);     box.y      = (int)((1.0-yr)*win->h);
505:   box.width = (int)((xr-xl)*win->w);box.height = (int)((yr-yl)*win->h);
506:   XSetClipRectangles(win->disp,win->gc.set,0,0,&box,1,Unsorted);

508:   /* try to make sure it is actually done before passing info to all */
509:   XSync(win->disp,False);
510:   MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);
511:   return(0);
512: }

514: static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw,PetscDraw*);
515: static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw,PetscDraw*);

519: PetscErrorCode PetscDrawDestroy_X(PetscDraw draw)
520: {
521:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
523: #if defined(PETSC_HAVE_POPEN)
524:   char           command[PETSC_MAX_PATH_LEN];
525:   PetscMPIInt    rank;
526:   FILE           *fd;
527: #endif

530:   PetscDrawSynchronizedClear(draw);

532: #if defined(PETSC_HAVE_POPEN)
533:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
534:   if (draw->savefilename && !rank && draw->savefilemovie) {
535:     PetscSNPrintf(command,PETSC_MAX_PATH_LEN,"ffmpeg  -i %s_%%d.Gif %s.m4v",draw->savefilename,draw->savefilename);
536:     PetscPOpen(((PetscObject)draw)->comm,PETSC_NULL,command,"r",&fd);
537:     PetscPClose(((PetscObject)draw)->comm,fd);
538:   }
539: #endif

541:   XFreeGC(win->disp,win->gc.set);
542:   XCloseDisplay(win->disp);
543:   PetscDrawDestroy(&draw->popup);
544:   PetscFree(win->font);
545:   PetscFree(win);
546:   return(0);
547: }

549: PetscErrorCode PetscDrawSave_X(PetscDraw);
550: PetscErrorCode PetscDrawSetSave_X(PetscDraw,const char*);

552: static struct _PetscDrawOps DvOps = { PetscDrawSetDoubleBuffer_X,
553:                                  PetscDrawFlush_X,PetscDrawLine_X,
554:                                  0,
555:                                  0,
556:                                  PetscDrawPoint_X,
557:                                  0,
558:                                  PetscDrawString_X,
559:                                  PetscDrawStringVertical_X,
560:                                  PetscDrawStringSetSize_X,
561:                                  PetscDrawStringGetSize_X,
562:                                  PetscDrawSetViewport_X,
563:                                  PetscDrawClear_X,
564:                                  PetscDrawSynchronizedFlush_X,
565:                                  PetscDrawRectangle_X,
566:                                  PetscDrawTriangle_X,
567:                                  PetscDrawEllipse_X,
568:                                  PetscDrawGetMouseButton_X,
569:                                  PetscDrawPause_X,
570:                                  PetscDrawSynchronizedClear_X,
571:                                  0,
572:                                  0,
573:                                  PetscDrawGetPopup_X,
574:                                  PetscDrawSetTitle_X,
575:                                  PetscDrawCheckResizedWindow_X,
576:                                  PetscDrawResizeWindow_X,
577:                                  PetscDrawDestroy_X,
578:                                  0,
579:                                  PetscDrawGetSingleton_X,
580:                                  PetscDrawRestoreSingleton_X,
581: #if defined(PETSC_HAVE_AFTERIMAGE)
582:                                  PetscDrawSave_X,
583: #else
584:                                  0,
585: #endif
586:                                  PetscDrawSetSave_X,
587:                                  0,
588:                                  PetscDrawArrow_X};


591: extern PetscErrorCode PetscDrawXiQuickWindow(PetscDraw_X*,char*,char*,int,int,int,int);
592: extern PetscErrorCode PetscDrawXiQuickWindowFromWindow(PetscDraw_X*,char*,Window);

596: static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw draw,PetscDraw *sdraw)
597: {
599:   PetscDraw_X *Xwin = (PetscDraw_X*)draw->data,*sXwin;


603:   PetscDrawCreate(PETSC_COMM_SELF,draw->display,draw->title,draw->x,draw->y,draw->w,draw->h,sdraw);
604:   PetscObjectChangeTypeName((PetscObject)*sdraw,PETSC_DRAW_X);
605:   PetscMemcpy((*sdraw)->ops,&DvOps,sizeof(DvOps));
606:   (*sdraw)->ops->destroy = 0;

608:   (*sdraw)->pause   = draw->pause;
609:   (*sdraw)->coor_xl = draw->coor_xl;
610:   (*sdraw)->coor_xr = draw->coor_xr;
611:   (*sdraw)->coor_yl = draw->coor_yl;
612:   (*sdraw)->coor_yr = draw->coor_yr;
613:   (*sdraw)->port_xl = draw->port_xl;
614:   (*sdraw)->port_xr = draw->port_xr;
615:   (*sdraw)->port_yl = draw->port_yl;
616:   (*sdraw)->port_yr = draw->port_yr;
617:   (*sdraw)->popup   = draw->popup;

619:   /* actually create and open the window */
620:   PetscNew(PetscDraw_X,&sXwin);
621:   PetscDrawXiQuickWindowFromWindow(sXwin,draw->display,Xwin->win);

623:   sXwin->x       = Xwin->x;
624:   sXwin->y       = Xwin->y;
625:   sXwin->w       = Xwin->w;
626:   sXwin->h       = Xwin->h;
627:   (*sdraw)->data = (void*)sXwin;
628:  return(0);
629: }

633: static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw draw,PetscDraw *sdraw)
634: {
636:   PetscDraw_X    *sXwin = (PetscDraw_X*)(*sdraw)->data;

639:   XFreeGC(sXwin->disp,sXwin->gc.set);
640:   XCloseDisplay(sXwin->disp);
641:   PetscDrawDestroy(&(*sdraw)->popup);
642:   PetscFree((*sdraw)->title);
643:   PetscFree((*sdraw)->display);
644:   PetscFree(sXwin->font);
645:   PetscFree(sXwin);
646:   PetscHeaderDestroy(sdraw);
647:   return(0);
648: }

652: PetscErrorCode PetscDrawXGetDisplaySize_Private(const char name[],int *width,int *height)
653: {
654:   Display *display;

657:   display = XOpenDisplay(name);
658:   if (!display) {
659:     *width  = 0;
660:     *height = 0;
661:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to open display on %s\n.  Make sure your COMPUTE NODES are authorized to connect \n\
662:     to this X server and either your DISPLAY variable\n\
663:     is set or you use the -display name option\n",name);
664:   }
665:   *width  = DisplayWidth(display,0);
666:   *height = DisplayHeight(display,0);
667:   XCloseDisplay(display);
668:   return(0);
669: }

671: EXTERN_C_BEGIN
674: PetscErrorCode  PetscDrawCreate_X(PetscDraw draw)
675: {
676:   PetscDraw_X    *Xwin;
678:   PetscMPIInt    rank;
679:   PetscInt       xywh[4],osize = 4;
680:   int            x = draw->x,y = draw->y,w = draw->w,h = draw->h;
681:   static int     xavailable = 0,yavailable = 0,xmax = 0,ymax = 0,ybottom = 0;
682:   PetscBool      flg = PETSC_FALSE;

685:   if (!draw->display) {
686:     PetscMalloc(256*sizeof(char),&draw->display);
687:     PetscGetDisplay(draw->display,256);
688:   }

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

703:   if (w == PETSC_DECIDE) w = draw->w = 300;
704:   if (h == PETSC_DECIDE) h = draw->h = 300;
705:   switch (w) {
706:     case PETSC_DRAW_FULL_SIZE: w = draw->w = xmax - 10;
707:                          break;
708:     case PETSC_DRAW_HALF_SIZE: w = draw->w = (xmax - 20)/2;
709:                          break;
710:     case PETSC_DRAW_THIRD_SIZE: w = draw->w = (xmax - 30)/3;
711:                          break;
712:     case PETSC_DRAW_QUARTER_SIZE: w = draw->w = (xmax - 40)/4;
713:                          break;
714:   }
715:   switch (h) {
716:     case PETSC_DRAW_FULL_SIZE: h = draw->h = ymax - 10;
717:                          break;
718:     case PETSC_DRAW_HALF_SIZE: h = draw->h = (ymax - 20)/2;
719:                          break;
720:     case PETSC_DRAW_THIRD_SIZE: h = draw->h = (ymax - 30)/3;
721:                          break;
722:     case PETSC_DRAW_QUARTER_SIZE: h = draw->h = (ymax - 40)/4;
723:                          break;
724:   }

726:   /* allow user to set location and size of window */
727:   xywh[0] = x; xywh[1] = y; xywh[2] = w; xywh[3] = h;
728:   PetscOptionsGetIntArray(PETSC_NULL,"-geometry",xywh,&osize,PETSC_NULL);
729:   x = (int) xywh[0]; y = (int) xywh[1]; w = (int) xywh[2]; h = (int) xywh[3];


732:   if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) {
733:     /*
734:        PETSc tries to place windows starting in the upper left corner and 
735:        moving across to the right. 
736:     
737:               --------------------------------------------
738:               |  Region used so far +xavailable,yavailable |
739:               |                     +                      |
740:               |                     +                      |
741:               |++++++++++++++++++++++ybottom               |
742:               |                                            |
743:               |                                            |
744:               |--------------------------------------------|
745:     */
746:     /*  First: can we add it to the right? */
747:     if (xavailable+w+10 <= xmax) {
748:       x       = xavailable;
749:       y       = yavailable;
750:       ybottom = PetscMax(ybottom,y + h + 30);
751:     } else {
752:       /* No, so add it below on the left */
753:       xavailable = 0;
754:       x          = 0;
755:       yavailable = ybottom;
756:       y          = ybottom;
757:       ybottom    = ybottom + h + 30;
758:     }
759:   }
760:   /* update available region */
761:   xavailable = PetscMax(xavailable,x + w + 10);
762:   if (xavailable >= xmax) {
763:     xavailable = 0;
764:     yavailable = yavailable + h + 30;
765:     ybottom    = yavailable;
766:   }
767:   if (yavailable >= ymax) {
768:     y          = 0;
769:     yavailable = 0;
770:     ybottom    = 0;
771:   }

773:   PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));

775:   /* actually create and open the window */
776:   PetscNew(PetscDraw_X,&Xwin);
777:   PetscLogObjectMemory(draw,sizeof(PetscDraw_X));
778:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);

780:   if (!rank) {
781:     if (x < 0 || y < 0)   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative corner of window");
782:     if (w <= 0 || h <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative window width or height");
783:     PetscDrawXiQuickWindow(Xwin,draw->display,draw->title,x,y,w,h);
784:     MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);
785:   } else {
786:     unsigned long win = 0;
787:     MPI_Bcast(&win,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);
788:     PetscDrawXiQuickWindowFromWindow(Xwin,draw->display,win);
789:   }

791:   Xwin->x      = x;
792:   Xwin->y      = y;
793:   Xwin->w      = w;
794:   Xwin->h      = h;
795:   draw->data    = (void*)Xwin;

797:   /*
798:     Need barrier here so processor 0 does not destroy the window before other 
799:     processors have completed PetscDrawXiQuickWindow()
800:   */
801:   PetscDrawClear(draw);
802:   PetscDrawSynchronizedFlush(draw);

804:   PetscOptionsGetBool(PETSC_NULL,"-draw_double_buffer",&flg,PETSC_NULL);
805:   if (flg) {
806:      PetscDrawSetDoubleBuffer(draw);
807:   }

809:   return(0);
810: }
811: EXTERN_C_END