Actual source code: xops.c

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

     6:  #include src/sys/src/draw/impls/x/ximpl.h

      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)*\
     14:                                    ((draw)->port_xr - (draw)->port_xl))/\
     15:                                    ((draw)->coor_xr - (draw)->coor_xl))))
     16: #define YTRANS(draw,xwin,y) \
     17:    (int)(((xwin)->h)*(1.0-(draw)->port_yl - (((y - (draw)->coor_yl)*\
     18:                                        ((draw)->port_yr - (draw)->port_yl))/\
     19:                                        ((draw)->coor_yr - (draw)->coor_yl))))

     23: PetscErrorCode PetscDrawLine_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl)
     24: {
     25:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
     26:   int          x1,y_1,x2,y2;

     29:   XiSetColor(XiWin,cl);
     30:   x1 = XTRANS(draw,XiWin,xl);   x2  = XTRANS(draw,XiWin,xr);
     31:   y_1 = YTRANS(draw,XiWin,yl);   y2  = YTRANS(draw,XiWin,yr);
     32:   XDrawLine(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x1,y_1,x2,y2);
     33:   return(0);
     34: }

     38: static PetscErrorCode PetscDrawPoint_X(PetscDraw draw,PetscReal x,PetscReal  y,int c)
     39: {
     40:   int          xx,yy;
     41:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

     44:   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
     45:   XiSetColor(XiWin,c);
     46:   XDrawPoint(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy);
     47:   return(0);
     48: }

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

     58:   XiSetColor(XiWin,c);
     59:   x1 = XTRANS(draw,XiWin,xl);   w  = XTRANS(draw,XiWin,xr) - x1;
     60:   y_1 = YTRANS(draw,XiWin,yr);   h  = YTRANS(draw,XiWin,yl) - y_1;
     61:   if (w <= 0) w = 1; if (h <= 0) h = 1;
     62:   XFillRectangle(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x1,y_1,w,h);
     63:   return(0);
     64: }

     68: static PetscErrorCode PetscDrawEllipse_X(PetscDraw Win, PetscReal x, PetscReal y, PetscReal a, PetscReal b, int c)
     69: {
     70:   PetscDraw_X* XiWin = (PetscDraw_X*) Win->data;
     71:   int          xA, yA, w, h;

     74:   XiSetColor(XiWin, c);
     75:   xA = XTRANS(Win, XiWin, x - a/2.0); w = XTRANS(Win, XiWin, x + a/2.0) - xA;
     76:   yA = YTRANS(Win, XiWin, y + b/2.0); h = YTRANS(Win, XiWin, y - b/2.0) - yA;
     77:   XFillArc(XiWin->disp, XiDrawable(XiWin), XiWin->gc.set, xA, yA, w, h, 0, 23040);
     78:   return(0);
     79: }

     81: EXTERN PetscErrorCode PetscDrawInterpolatedTriangle_X(PetscDraw_X*,int,int,int,int,int,int,int,int,int);

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

     92:   if (c1 == c2 && c2 == c3) {
     93:     XPoint pt[3];
     94:     XiSetColor(XiWin,c1);
     95:     pt[0].x = XTRANS(draw,XiWin,X1);
     96:     pt[0].y = YTRANS(draw,XiWin,Y_1);
     97:     pt[1].x = XTRANS(draw,XiWin,X2);
     98:     pt[1].y = YTRANS(draw,XiWin,Y2);
     99:     pt[2].x = XTRANS(draw,XiWin,X3);
    100:     pt[2].y = YTRANS(draw,XiWin,Y3);
    101:     XFillPolygon(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,pt,3,Convex,CoordModeOrigin);
    102:   } else {
    103:     int x1,y_1,x2,y2,x3,y3;
    104:     x1   = XTRANS(draw,XiWin,X1);
    105:     y_1  = YTRANS(draw,XiWin,Y_1);
    106:     x2   = XTRANS(draw,XiWin,X2);
    107:     y2   = YTRANS(draw,XiWin,Y2);
    108:     x3   = XTRANS(draw,XiWin,X3);
    109:     y3   = YTRANS(draw,XiWin,Y3);
    110:     PetscDrawInterpolatedTriangle_X(XiWin,x1,y_1,c1,x2,y2,c2,x3,y3,c3);
    111:   }
    112:   return(0);
    113: }

    117: static PetscErrorCode PetscDrawString_X(PetscDraw draw,PetscReal x,PetscReal  y,int c,const char chrs[])
    118: {
    120:   int            xx,yy;
    121:   size_t         len;
    122:   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
    123:   char           *substr;
    124:   PetscToken     *token;

    127:   xx = XTRANS(draw,XiWin,x);  yy = YTRANS(draw,XiWin,y);
    128:   XiSetColor(XiWin,c);
    129:
    130:   PetscTokenCreate(chrs,'\n',&token);
    131:   PetscTokenFind(token,&substr);
    132:   PetscStrlen(substr,&len);
    133:   XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
    134:   PetscTokenFind(token,&substr);
    135:   while (substr) {
    136:     yy  += 4*XiWin->font->font_descent;
    137:     PetscStrlen(substr,&len);
    138:     XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len);
    139:     PetscTokenFind(token,&substr);
    140:   }
    141:   PetscTokenDestroy(token);

    143:   return(0);
    144: }

    146: EXTERN PetscErrorCode XiFontFixed(PetscDraw_X*,int,int,XiFont **);

    150: static PetscErrorCode PetscDrawStringSetSize_X(PetscDraw draw,PetscReal x,PetscReal  y)
    151: {
    152:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;
    154:   int            w,h;

    157:   w = (int)((XiWin->w)*x*(draw->port_xr - draw->port_xl)/(draw->coor_xr - draw->coor_xl));
    158:   h = (int)((XiWin->h)*y*(draw->port_yr - draw->port_yl)/(draw->coor_yr - draw->coor_yl));
    159:   PetscFree(XiWin->font);
    160:   XiFontFixed(XiWin,w,h,&XiWin->font);
    161:   return(0);
    162: }

    166: PetscErrorCode PetscDrawStringGetSize_X(PetscDraw draw,PetscReal *x,PetscReal  *y)
    167: {
    168:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
    169:   PetscReal    w,h;

    172:   w = XiWin->font->font_w; h = XiWin->font->font_h;
    173:   *x = w*(draw->coor_xr - draw->coor_xl)/(XiWin->w)*(draw->port_xr - draw->port_xl);
    174:   *y = h*(draw->coor_yr - draw->coor_yl)/(XiWin->h)*(draw->port_yr - draw->port_yl);
    175:   return(0);
    176: }

    180: PetscErrorCode PetscDrawStringVertical_X(PetscDraw draw,PetscReal x,PetscReal  y,int c,const char chrs[])
    181: {
    183:   int            xx,yy;
    184:   PetscDraw_X    *XiWin = (PetscDraw_X*)draw->data;
    185:   char           tmp[2];
    186:   PetscReal      tw,th;
    187:   size_t         i,n;
    188:
    190:   PetscStrlen(chrs,&n);
    191:   tmp[1] = 0;
    192:   XiSetColor(XiWin,c);
    193:   PetscDrawStringGetSize_X(draw,&tw,&th);
    194:   xx = XTRANS(draw,XiWin,x);
    195:   for (i=0; i<n; i++) {
    196:     tmp[0] = chrs[i];
    197:     yy = YTRANS(draw,XiWin,y-th*i);
    198:     XDrawString(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,
    199:                 xx,yy - XiWin->font->font_descent,tmp,1);
    200:   }
    201:   return(0);
    202: }

    206: static PetscErrorCode PetscDrawFlush_X(PetscDraw draw)
    207: {
    208:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;

    211:   if (XiWin->drw) {
    212:     XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
    213:   }
    214:   XFlush(XiWin->disp); XSync(XiWin->disp,False);
    215:   return(0);
    216: }

    220: static PetscErrorCode PetscDrawSynchronizedFlush_X(PetscDraw draw)
    221: {
    223:   PetscMPIInt    rank;
    224:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;

    227:   XFlush(XiWin->disp);
    228:   if (XiWin->drw) {
    229:     MPI_Comm_rank(draw->comm,&rank);
    230:     /* make sure data has actually arrived at server */
    231:     XSync(XiWin->disp,False);
    232:     MPI_Barrier(draw->comm);
    233:     if (!rank) {
    234:       XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0);
    235:       XFlush(XiWin->disp);
    236:     }
    237:     XSync(XiWin->disp,False);
    238:     MPI_Barrier(draw->comm);
    239:   } else {
    240:     MPI_Barrier(draw->comm);
    241:     XSync(XiWin->disp,False);
    242:     MPI_Barrier(draw->comm);
    243:   }
    244:   return(0);
    245: }

    249: static PetscErrorCode PetscDrawSetViewport_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
    250: {
    251:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
    252:   XRectangle   box;

    255:   box.x = (int)(xl*XiWin->w);   box.y = (int)((1.0-yr)*XiWin->h);
    256:   box.width = (int)((xr-xl)*XiWin->w);box.height = (int)((yr-yl)*XiWin->h);
    257:   XSetClipRectangles(XiWin->disp,XiWin->gc.set,0,0,&box,1,Unsorted);
    258:   return(0);
    259: }

    263: static PetscErrorCode PetscDrawClear_X(PetscDraw draw)
    264: {
    265:   PetscDraw_X* XiWin = (PetscDraw_X*)draw->data;
    266:   int          x, y, w, h;

    269:   x = (int)(draw->port_xl*XiWin->w);
    270:   w = (int)((draw->port_xr - draw->port_xl)*XiWin->w);
    271:   y = (int)((1.0-draw->port_yr)*XiWin->h);
    272:   h = (int)((draw->port_yr - draw->port_yl)*XiWin->h);
    273:   XiSetPixVal(XiWin,XiWin->background);
    274:   XFillRectangle(XiWin->disp,XiDrawable(XiWin),XiWin->gc.set,x,y,w,h);
    275:   return(0);
    276: }

    280: static PetscErrorCode PetscDrawSynchronizedClear_X(PetscDraw draw)
    281: {
    283:   PetscMPIInt    rank;
    284:   PetscDraw_X*   XiWin = (PetscDraw_X*)draw->data;

    287:   MPI_Barrier(draw->comm);
    288:   MPI_Comm_rank(draw->comm,&rank);
    289:   if (!rank) {
    290:     PetscDrawClear_X(draw);
    291:   }
    292:   XFlush(XiWin->disp);
    293:   MPI_Barrier(draw->comm);
    294:   XSync(XiWin->disp,False);
    295:   MPI_Barrier(draw->comm);
    296:   return(0);
    297: }

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

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

    310:   MPI_Comm_rank(draw->comm,&rank);
    311:   if (!rank) {
    312:     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
    313:   }
    314:   /* try to make sure it is actually done before passing info to all */
    315:   XSync(win->disp,False);
    316:   MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,draw->comm);
    317:   return(0);
    318: }

    320: #include <X11/cursorfont.h>

    324: static PetscErrorCode PetscDrawGetMouseButton_X(PetscDraw draw,PetscDrawButton *button,PetscReal* x_user,
    325:                                 PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys)
    326: {
    327:   XEvent       report;
    328:   PetscDraw_X* win = (PetscDraw_X*)draw->data;
    329:   Window       root,child;
    330:   int          root_x,root_y,px,py;
    331:   unsigned int keys_button;
    332:   Cursor       cursor = 0;

    335:   /* change cursor to indicate input */
    336:   if (!cursor) {
    337:     cursor = XCreateFontCursor(win->disp,XC_hand2);
    338:     if (!cursor) SETERRQ(PETSC_ERR_LIB,"Unable to create X cursor");
    339:   }
    340:   XDefineCursor(win->disp,win->win,cursor);

    342:   XSelectInput(win->disp,win->win,ButtonPressMask | ButtonReleaseMask);

    344:   while (XCheckTypedEvent(win->disp,ButtonPress,&report));
    345:   XMaskEvent(win->disp,ButtonReleaseMask,&report);
    346:   switch (report.xbutton.button) {
    347:     case Button1:
    348:       if (report.xbutton.state & ShiftMask)
    349:         *button = BUTTON_LEFT_SHIFT;
    350:       else
    351:         *button = BUTTON_LEFT;
    352:       break;
    353:     case Button2:
    354:       if (report.xbutton.state & ShiftMask)
    355:         *button = BUTTON_CENTER_SHIFT;
    356:       else
    357:         *button = BUTTON_CENTER;
    358:       break;
    359:     case Button3:
    360:       if (report.xbutton.state & ShiftMask)
    361:         *button = BUTTON_RIGHT_SHIFT;
    362:       else
    363:         *button = BUTTON_RIGHT;
    364:       break;
    365:   }
    366:   XQueryPointer(win->disp,report.xmotion.window,&root,&child,&root_x,&root_y,&px,&py,&keys_button);

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

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

    377:   XUndefineCursor(win->disp,win->win);
    378:   XFlush(win->disp); XSync(win->disp,False);
    379:   return(0);
    380: }

    384: static PetscErrorCode PetscDrawPause_X(PetscDraw draw)
    385: {

    389:   if (draw->pause > 0) PetscSleep(draw->pause);
    390:   else if (draw->pause < 0) {
    391:     PetscDrawButton button;
    392:     PetscMPIInt     rank;
    393:     MPI_Comm_rank(draw->comm,&rank);
    394:     if (!rank) {
    395:       PetscDrawGetMouseButton(draw,&button,0,0,0,0);
    396:       if (button == BUTTON_CENTER) draw->pause = 0;
    397:     }
    398:     MPI_Bcast(&draw->pause,1,MPI_INT,0,draw->comm);
    399:   }
    400:   return(0);
    401: }

    405: static PetscErrorCode PetscDrawGetPopup_X(PetscDraw draw,PetscDraw *popup)
    406: {
    408:   PetscDraw_X*   win = (PetscDraw_X*)draw->data;

    411:   PetscDrawOpenX(draw->comm,PETSC_NULL,PETSC_NULL,win->x,win->y+win->h+36,150,220,popup);
    412:   draw->popup = *popup;
    413:   return(0);
    414: }

    418: static PetscErrorCode PetscDrawSetTitle_X(PetscDraw draw,const char title[])
    419: {
    420:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
    421:   XTextProperty  prop;
    423:   size_t         len;

    426:   XGetWMName(win->disp,win->win,&prop);
    427:   prop.value  = (unsigned char *)title;
    428:   PetscStrlen(title,&len);
    429:   prop.nitems = (long) len;
    430:   XSetWMName(win->disp,win->win,&prop);
    431:   return(0);
    432: }

    436: static PetscErrorCode PetscDrawResizeWindow_X(PetscDraw draw,int w,int h)
    437: {
    438:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
    439:   unsigned int   ww,hh,border,depth;
    440:   int            x,y;
    442:   Window         root;

    445:   XResizeWindow(win->disp,win->win,w,h);
    446:   XGetGeometry(win->disp,win->win,&root,&x,&y,&ww,&hh,&border,&depth);
    447:   PetscDrawCheckResizedWindow(draw);
    448:   return(0);
    449: }

    453: static PetscErrorCode PetscDrawCheckResizedWindow_X(PetscDraw draw)
    454: {
    455:   PetscDraw_X    *win = (PetscDraw_X*)draw->data;
    457:   int            x,y;
    458:   PetscMPIInt    rank;
    459:   Window         root;
    460:   unsigned int   w,h,border,depth,geo[2];
    461:   PetscReal      xl,xr,yl,yr;
    462:   XRectangle     box;

    465:   MPI_Comm_rank(draw->comm,&rank);
    466:   if (!rank) {
    467:     XSync(win->disp,False);
    468:     XGetGeometry(win->disp,win->win,&root,&x,&y,geo,geo+1,&border,&depth);
    469:   }
    470:   MPI_Bcast(geo,2,MPI_INT,0,draw->comm);
    471:   w = geo[0];
    472:   h = geo[1];
    473:   if (w == (unsigned int) win->w && h == (unsigned int) win->h) return(0);

    475:   /* record new window sizes */

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

    479:   /* Free buffer space and create new version (only first processor does this) */
    480:   if (win->drw) {
    481:     win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth);
    482:   }
    483:   /* reset the clipping */
    484:   xl = draw->port_xl; yl = draw->port_yl;
    485:   xr = draw->port_xr; yr = draw->port_yr;
    486:   box.x     = (int)(xl*win->w);     box.y      = (int)((1.0-yr)*win->h);
    487:   box.width = (int)((xr-xl)*win->w);box.height = (int)((yr-yl)*win->h);
    488:   XSetClipRectangles(win->disp,win->gc.set,0,0,&box,1,Unsorted);

    490:   /* try to make sure it is actually done before passing info to all */
    491:   XSync(win->disp,False);
    492:   MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,draw->comm);
    493:   return(0);
    494: }

    496: static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw,PetscDraw*);
    497: static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw,PetscDraw*);

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

    507:   XFreeGC(win->disp,win->gc.set);
    508:   XCloseDisplay(win->disp);
    509:   if (draw->popup) {PetscDrawDestroy(draw->popup);}
    510:   PetscFree(win->font);
    511:   PetscFree(win);
    512:   return(0);
    513: }

    515: static struct _PetscDrawOps DvOps = { PetscDrawSetDoubleBuffer_X,
    516:                                  PetscDrawFlush_X,PetscDrawLine_X,
    517:                                  0,
    518:                                  0,
    519:                                  PetscDrawPoint_X,
    520:                                  0,
    521:                                  PetscDrawString_X,
    522:                                  PetscDrawStringVertical_X,
    523:                                  PetscDrawStringSetSize_X,
    524:                                  PetscDrawStringGetSize_X,
    525:                                  PetscDrawSetViewport_X,
    526:                                  PetscDrawClear_X,
    527:                                  PetscDrawSynchronizedFlush_X,
    528:                                  PetscDrawRectangle_X,
    529:                                  PetscDrawTriangle_X,
    530:                                  PetscDrawEllipse_X,
    531:                                  PetscDrawGetMouseButton_X,
    532:                                  PetscDrawPause_X,
    533:                                  PetscDrawSynchronizedClear_X,
    534:                                  0,
    535:                                  0,
    536:                                  PetscDrawGetPopup_X,
    537:                                  PetscDrawSetTitle_X,
    538:                                  PetscDrawCheckResizedWindow_X,
    539:                                  PetscDrawResizeWindow_X,
    540:                                  PetscDrawDestroy_X,
    541:                                  0,
    542:                                  PetscDrawGetSingleton_X,
    543:                                  PetscDrawRestoreSingleton_X };

    546: EXTERN PetscErrorCode XiQuickWindow(PetscDraw_X*,char*,char*,int,int,int,int);
    547: EXTERN PetscErrorCode XiQuickWindowFromWindow(PetscDraw_X*,char*,Window);

    551: static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw draw,PetscDraw *sdraw)
    552: {
    554:   PetscDraw_X *Xwin = (PetscDraw_X*)draw->data,*sXwin;

    558:   PetscDrawCreate(PETSC_COMM_SELF,draw->display,draw->title,draw->x,draw->y,draw->w,draw->h,sdraw);
    559:   PetscObjectChangeTypeName((PetscObject)*sdraw,PETSC_DRAW_X);
    560:   PetscMemcpy((*sdraw)->ops,&DvOps,sizeof(DvOps));
    561:   (*sdraw)->ops->destroy = 0;

    563:   (*sdraw)->pause   = draw->pause;
    564:   (*sdraw)->coor_xl = draw->coor_xl;
    565:   (*sdraw)->coor_xr = draw->coor_xr;
    566:   (*sdraw)->coor_yl = draw->coor_yl;
    567:   (*sdraw)->coor_yr = draw->coor_yr;
    568:   (*sdraw)->port_xl = draw->port_xl;
    569:   (*sdraw)->port_xr = draw->port_xr;
    570:   (*sdraw)->port_yl = draw->port_yl;
    571:   (*sdraw)->port_yr = draw->port_yr;
    572:   (*sdraw)->popup   = draw->popup;

    574:   /* actually create and open the window */
    575:   PetscNew(PetscDraw_X,&sXwin);
    576:   XiQuickWindowFromWindow(sXwin,draw->display,Xwin->win);

    578:   sXwin->x       = Xwin->x;
    579:   sXwin->y       = Xwin->y;
    580:   sXwin->w       = Xwin->w;
    581:   sXwin->h       = Xwin->h;
    582:   (*sdraw)->data = (void*)sXwin;
    583:  return(0);
    584: }

    588: static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw draw,PetscDraw *sdraw)
    589: {
    591:   PetscDraw_X    *sXwin = (PetscDraw_X*)(*sdraw)->data;

    594:   XFreeGC(sXwin->disp,sXwin->gc.set);
    595:   XCloseDisplay(sXwin->disp);
    596:   if ((*sdraw)->popup)   {PetscDrawDestroy((*sdraw)->popup);}
    597:   PetscStrfree((*sdraw)->title);
    598:   PetscStrfree((*sdraw)->display);
    599:   PetscFree(sXwin->font);
    600:   PetscFree(sXwin);
    601:   PetscHeaderDestroy(*sdraw);
    602:   return(0);
    603: }

    607: PetscErrorCode PetscDrawXGetDisplaySize_Private(const char name[],int *width,int *height)
    608: {
    609:   Display *display;

    612:   display = XOpenDisplay(name);
    613:   if (!display) {
    614:     *width  = 0;
    615:     *height = 0;
    616:     SETERRQ1(PETSC_ERR_LIB,"Unable to open display on %s\n.  Make sure your COMPUTE NODES are authorized to connect \n\
    617:     to this X server and either your DISPLAY variable\n\
    618:     is set or you use the -display name option\n",name);
    619:   }
    620:   *width  = DisplayWidth(display,0);
    621:   *height = DisplayHeight(display,0);
    622:   XCloseDisplay(display);
    623:   return(0);
    624: }

    629: PetscErrorCode PETSC_DLLEXPORT PetscDrawCreate_X(PetscDraw draw)
    630: {
    631:   PetscDraw_X    *Xwin;
    633:   PetscMPIInt    rank;
    634:   PetscInt       xywh[4],osize = 4;
    635:   int            x = draw->x,y = draw->y,w = draw->w,h = draw->h;
    636:   static int     xavailable = 0,yavailable = 0,xmax = 0,ymax = 0,ybottom = 0;
    637:   PetscTruth     flg;

    640:   if (!draw->display) {
    641:     PetscMalloc(128*sizeof(char),&draw->display);
    642:     PetscGetDisplay(draw->display,128);
    643:   }

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

    658:   if (w == PETSC_DECIDE) w = draw->w = 300;
    659:   if (h == PETSC_DECIDE) h = draw->h = 300;
    660:   switch (w) {
    661:     case PETSC_DRAW_FULL_SIZE: w = draw->w = xmax - 10;
    662:                          break;
    663:     case PETSC_DRAW_HALF_SIZE: w = draw->w = (xmax - 20)/2;
    664:                          break;
    665:     case PETSC_DRAW_THIRD_SIZE: w = draw->w = (xmax - 30)/3;
    666:                          break;
    667:     case PETSC_DRAW_QUARTER_SIZE: w = draw->w = (xmax - 40)/4;
    668:                          break;
    669:   }
    670:   switch (h) {
    671:     case PETSC_DRAW_FULL_SIZE: h = draw->h = ymax - 10;
    672:                          break;
    673:     case PETSC_DRAW_HALF_SIZE: h = draw->h = (ymax - 20)/2;
    674:                          break;
    675:     case PETSC_DRAW_THIRD_SIZE: h = draw->h = (ymax - 30)/3;
    676:                          break;
    677:     case PETSC_DRAW_QUARTER_SIZE: h = draw->h = (ymax - 40)/4;
    678:                          break;
    679:   }

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

    687:   if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) {
    688:     /*
    689:        PETSc tries to place windows starting in the upper left corner and
    690:        moving across to the right.
    691:
    692:               --------------------------------------------
    693:               |  Region used so far +xavailable,yavailable |
    694:               |                     +                      |
    695:               |                     +                      |
    696:               |++++++++++++++++++++++ybottom               |
    697:               |                                            |
    698:               |                                            |
    699:               |--------------------------------------------|
    700:     */
    701:     /*  First: can we add it to the right? */
    702:     if (xavailable+w+10 <= xmax) {
    703:       x       = xavailable;
    704:       y       = yavailable;
    705:       ybottom = PetscMax(ybottom,y + h + 30);
    706:     } else {
    707:       /* No, so add it below on the left */
    708:       xavailable = 0;
    709:       x          = 0;
    710:       yavailable = ybottom;
    711:       y          = ybottom;
    712:       ybottom    = ybottom + h + 30;
    713:     }
    714:   }
    715:   /* update available region */
    716:   xavailable = PetscMax(xavailable,x + w + 10);
    717:   if (xavailable >= xmax) {
    718:     xavailable = 0;
    719:     yavailable = yavailable + h + 30;
    720:     ybottom    = yavailable;
    721:   }
    722:   if (yavailable >= ymax) {
    723:     y          = 0;
    724:     yavailable = 0;
    725:     ybottom    = 0;
    726:   }

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

    730:   /* actually create and open the window */
    731:   PetscNew(PetscDraw_X,&Xwin);
    732:   PetscLogObjectMemory(draw,sizeof(PetscDraw_X)+sizeof(struct _p_PetscDraw));
    733:   MPI_Comm_rank(draw->comm,&rank);

    735:   if (!rank) {
    736:     if (x < 0 || y < 0)   SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative corner of window");
    737:     if (w <= 0 || h <= 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative window width or height");
    738:     XiQuickWindow(Xwin,draw->display,draw->title,x,y,w,h);
    739:     MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,draw->comm);
    740:   } else {
    741:     unsigned long win = 0;
    742:     MPI_Bcast(&win,1,MPI_UNSIGNED_LONG,0,draw->comm);
    743:     XiQuickWindowFromWindow(Xwin,draw->display,win);
    744:   }

    746:   Xwin->x      = x;
    747:   Xwin->y      = y;
    748:   Xwin->w      = w;
    749:   Xwin->h      = h;
    750:   draw->data    = (void*)Xwin;

    752:   /*
    753:     Need barrier here so processor 0 does not destroy the window before other
    754:     processors have completed XiQuickWindow()
    755:   */
    756:   PetscDrawClear(draw);
    757:   PetscDrawSynchronizedFlush(draw);

    759:   PetscOptionsHasName(PETSC_NULL,"-draw_double_buffer",&flg);
    760:   if (flg) {
    761:      PetscDrawSetDoubleBuffer(draw);
    762:   }

    764:   return(0);
    765: }