1 2 /* 3 Defines the operations for the X PetscDraw implementation. 4 */ 5 6 #include <../src/sys/classes/draw/impls/x/ximpl.h> /*I "petscsys.h" I*/ 7 8 /* 9 These macros transform from the users coordinates to the X-window pixel coordinates. 10 */ 11 #define XTRANS(draw,xwin,x) (int)(((xwin)->w)*((draw)->port_xl + (((x - (draw)->coor_xl)*((draw)->port_xr - (draw)->port_xl))/((draw)->coor_xr - (draw)->coor_xl)))) 12 #define YTRANS(draw,xwin,y) (int)(((xwin)->h)*(1.0-(draw)->port_yl - (((y - (draw)->coor_yl)*((draw)->port_yr - (draw)->port_yl))/((draw)->coor_yr - (draw)->coor_yl)))) 13 14 #define ITRANS(draw,xwin,i) (draw)->coor_xl + (i*((draw)->coor_xr - (draw)->coor_xl)/((xwin)->w) - (draw)->port_xl)/((draw)->port_xr - (draw)->port_xl) 15 #define JTRANS(draw,xwin,j) draw->coor_yl + (((double)j)/xwin->h + draw->port_yl - 1.0)*(draw->coor_yr - draw->coor_yl)/(draw->port_yl - draw->port_yr) 16 17 #undef __FUNCT__ 18 #define __FUNCT__ "PetscDrawCoordinateToPixel_X" 19 PetscErrorCode PetscDrawCoordinateToPixel_X(PetscDraw draw,PetscReal x,PetscReal y,PetscInt *i,PetscInt *j) 20 { 21 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 22 23 PetscFunctionBegin; 24 *i = XTRANS(draw,XiWin,x); 25 *j = YTRANS(draw,XiWin,y); 26 PetscFunctionReturn(0); 27 } 28 29 #undef __FUNCT__ 30 #define __FUNCT__ "PetscDrawPixelToCoordinate_X" 31 PetscErrorCode PetscDrawPixelToCoordinate_X(PetscDraw draw,PetscInt i,PetscInt j,PetscReal *x,PetscReal *y) 32 { 33 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 34 35 PetscFunctionBegin; 36 *x = ITRANS(draw,XiWin,i); 37 *y = JTRANS(draw,XiWin,j); 38 PetscFunctionReturn(0); 39 } 40 41 #undef __FUNCT__ 42 #define __FUNCT__ "PetscDrawLine_X" 43 PetscErrorCode PetscDrawLine_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl) 44 { 45 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 46 int x1,y_1,x2,y2; 47 48 PetscFunctionBegin; 49 PetscDrawXiSetColor(XiWin,cl); 50 x1 = XTRANS(draw,XiWin,xl); x2 = XTRANS(draw,XiWin,xr); 51 y_1 = YTRANS(draw,XiWin,yl); y2 = YTRANS(draw,XiWin,yr); 52 if (x1 == x2 && y_1 == y2) PetscFunctionReturn(0); 53 XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x1,y_1,x2,y2); 54 PetscFunctionReturn(0); 55 } 56 57 #undef __FUNCT__ 58 #define __FUNCT__ "PetscDrawArrow_X" 59 PetscErrorCode PetscDrawArrow_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int cl) 60 { 61 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 62 int x1,y_1,x2,y2; 63 64 PetscFunctionBegin; 65 PetscDrawXiSetColor(XiWin,cl); 66 x1 = XTRANS(draw,XiWin,xl); x2 = XTRANS(draw,XiWin,xr); 67 y_1 = YTRANS(draw,XiWin,yl); y2 = YTRANS(draw,XiWin,yr); 68 if (x1 == x2 && y_1 == y2) PetscFunctionReturn(0); 69 XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x1,y_1,x2,y2); 70 if (x1 == x2 && PetscAbs(y_1 - y2) > 7) { 71 if (y2 > y_1) { 72 XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x2,y2,x2-3,y2-3); 73 XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x2,y2,x2+3,y2-3); 74 } else { 75 XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x2,y2,x2-3,y2+3); 76 XDrawLine(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x2,y2,x2+3,y2+3); 77 } 78 } 79 PetscFunctionReturn(0); 80 } 81 82 #undef __FUNCT__ 83 #define __FUNCT__ "PetscDrawPoint_X" 84 static PetscErrorCode PetscDrawPoint_X(PetscDraw draw,PetscReal x,PetscReal y,int c) 85 { 86 int xx,yy,i,j; 87 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 88 89 PetscFunctionBegin; 90 xx = XTRANS(draw,XiWin,x); yy = YTRANS(draw,XiWin,y); 91 PetscDrawXiSetColor(XiWin,c); 92 for (i=-1; i<2; i++) { 93 for (j=-1; j<2; j++) { 94 XDrawPoint(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx+i,yy+j); 95 } 96 } 97 PetscFunctionReturn(0); 98 } 99 100 #undef __FUNCT__ 101 #define __FUNCT__ "PetscDrawPointPixel_X" 102 static PetscErrorCode PetscDrawPointPixel_X(PetscDraw draw,PetscInt x,PetscInt y,int c) 103 { 104 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 105 106 PetscFunctionBegin; 107 PetscDrawXiSetColor(XiWin,c); 108 XDrawPoint(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x,y); 109 PetscFunctionReturn(0); 110 } 111 112 #undef __FUNCT__ 113 #define __FUNCT__ "PetscDrawRectangle_X" 114 static PetscErrorCode PetscDrawRectangle_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c1,int c2,int c3,int c4) 115 { 116 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 117 int x1,y_1,w,h,c = (c1 + c2 + c3 + c4)/4; 118 119 PetscFunctionBegin; 120 PetscDrawXiSetColor(XiWin,c); 121 x1 = XTRANS(draw,XiWin,xl); w = XTRANS(draw,XiWin,xr) - x1; 122 y_1 = YTRANS(draw,XiWin,yr); h = YTRANS(draw,XiWin,yl) - y_1; 123 if (w <= 0) w = 1; 124 if (h <= 0) h = 1; 125 XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x1,y_1,w,h); 126 PetscFunctionReturn(0); 127 } 128 129 #undef __FUNCT__ 130 #define __FUNCT__ "PetscDrawEllipse_X" 131 static PetscErrorCode PetscDrawEllipse_X(PetscDraw Win, PetscReal x, PetscReal y, PetscReal a, PetscReal b, int c) 132 { 133 PetscDraw_X *XiWin = (PetscDraw_X*) Win->data; 134 int xA, yA, w, h; 135 136 PetscFunctionBegin; 137 PetscDrawXiSetColor(XiWin, c); 138 xA = XTRANS(Win, XiWin, x - a/2.0); w = XTRANS(Win, XiWin, x + a/2.0) - xA; 139 yA = YTRANS(Win, XiWin, y + b/2.0); h = PetscAbs(YTRANS(Win, XiWin, y - b/2.0) - yA); 140 XFillArc(XiWin->disp, PetscDrawXiDrawable(XiWin), XiWin->gc.set, xA, yA, w, h, 0, 23040); 141 PetscFunctionReturn(0); 142 } 143 144 extern PetscErrorCode PetscDrawInterpolatedTriangle_X(PetscDraw_X*,int,int,int,int,int,int,int,int,int); 145 146 #undef __FUNCT__ 147 #define __FUNCT__ "PetscDrawTriangle_X" 148 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) 149 { 150 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 151 PetscErrorCode ierr; 152 153 PetscFunctionBegin; 154 if (c1 == c2 && c2 == c3) { 155 XPoint pt[3]; 156 PetscDrawXiSetColor(XiWin,c1); 157 pt[0].x = XTRANS(draw,XiWin,X1); 158 pt[0].y = YTRANS(draw,XiWin,Y_1); 159 pt[1].x = XTRANS(draw,XiWin,X2); 160 pt[1].y = YTRANS(draw,XiWin,Y2); 161 pt[2].x = XTRANS(draw,XiWin,X3); 162 pt[2].y = YTRANS(draw,XiWin,Y3); 163 XFillPolygon(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,pt,3,Convex,CoordModeOrigin); 164 } else { 165 int x1,y_1,x2,y2,x3,y3; 166 x1 = XTRANS(draw,XiWin,X1); 167 y_1 = YTRANS(draw,XiWin,Y_1); 168 x2 = XTRANS(draw,XiWin,X2); 169 y2 = YTRANS(draw,XiWin,Y2); 170 x3 = XTRANS(draw,XiWin,X3); 171 y3 = YTRANS(draw,XiWin,Y3); 172 ierr = PetscDrawInterpolatedTriangle_X(XiWin,x1,y_1,c1,x2,y2,c2,x3,y3,c3);CHKERRQ(ierr); 173 } 174 PetscFunctionReturn(0); 175 } 176 177 #undef __FUNCT__ 178 #define __FUNCT__ "PetscDrawString_X" 179 static PetscErrorCode PetscDrawString_X(PetscDraw draw,PetscReal x,PetscReal y,int c,const char chrs[]) 180 { 181 PetscErrorCode ierr; 182 int xx,yy; 183 size_t len; 184 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 185 char *substr; 186 PetscToken token; 187 188 PetscFunctionBegin; 189 xx = XTRANS(draw,XiWin,x); yy = YTRANS(draw,XiWin,y); 190 PetscDrawXiSetColor(XiWin,c); 191 192 ierr = PetscTokenCreate(chrs,'\n',&token);CHKERRQ(ierr); 193 ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr); 194 ierr = PetscStrlen(substr,&len);CHKERRQ(ierr); 195 XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len); 196 ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr); 197 while (substr) { 198 yy += 4*XiWin->font->font_descent; 199 ierr = PetscStrlen(substr,&len);CHKERRQ(ierr); 200 XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,xx,yy - XiWin->font->font_descent,substr,len); 201 ierr = PetscTokenFind(token,&substr);CHKERRQ(ierr); 202 } 203 ierr = PetscTokenDestroy(&token);CHKERRQ(ierr); 204 PetscFunctionReturn(0); 205 } 206 207 extern PetscErrorCode PetscDrawXiFontFixed(PetscDraw_X*,int,int,PetscDrawXiFont**); 208 209 #undef __FUNCT__ 210 #define __FUNCT__ "PetscDrawStringSetSize_X" 211 static PetscErrorCode PetscDrawStringSetSize_X(PetscDraw draw,PetscReal x,PetscReal y) 212 { 213 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 214 PetscErrorCode ierr; 215 int w,h; 216 217 PetscFunctionBegin; 218 w = (int)((XiWin->w)*x*(draw->port_xr - draw->port_xl)/(draw->coor_xr - draw->coor_xl)); 219 h = (int)((XiWin->h)*y*(draw->port_yr - draw->port_yl)/(draw->coor_yr - draw->coor_yl)); 220 ierr = PetscFree(XiWin->font);CHKERRQ(ierr); 221 ierr = PetscDrawXiFontFixed(XiWin,w,h,&XiWin->font);CHKERRQ(ierr); 222 PetscFunctionReturn(0); 223 } 224 225 #undef __FUNCT__ 226 #define __FUNCT__ "PetscDrawStringGetSize_X" 227 PetscErrorCode PetscDrawStringGetSize_X(PetscDraw draw,PetscReal *x,PetscReal *y) 228 { 229 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 230 PetscReal w,h; 231 232 PetscFunctionBegin; 233 w = XiWin->font->font_w; h = XiWin->font->font_h; 234 if (x) *x = w*(draw->coor_xr - draw->coor_xl)/((XiWin->w)*(draw->port_xr - draw->port_xl)); 235 if (y) *y = h*(draw->coor_yr - draw->coor_yl)/((XiWin->h)*(draw->port_yr - draw->port_yl)); 236 PetscFunctionReturn(0); 237 } 238 239 #undef __FUNCT__ 240 #define __FUNCT__ "PetscDrawStringVertical_X" 241 PetscErrorCode PetscDrawStringVertical_X(PetscDraw draw,PetscReal x,PetscReal y,int c,const char chrs[]) 242 { 243 PetscErrorCode ierr; 244 int xx,yy; 245 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 246 char tmp[2]; 247 PetscReal tw,th; 248 size_t i,n; 249 250 PetscFunctionBegin; 251 ierr = PetscStrlen(chrs,&n);CHKERRQ(ierr); 252 tmp[1] = 0; 253 PetscDrawXiSetColor(XiWin,c); 254 ierr = PetscDrawStringGetSize_X(draw,&tw,&th);CHKERRQ(ierr); 255 xx = XTRANS(draw,XiWin,x); 256 for (i=0; i<n; i++) { 257 tmp[0] = chrs[i]; 258 yy = YTRANS(draw,XiWin,y-th*i); 259 XDrawString(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set, xx,yy - XiWin->font->font_descent,tmp,1); 260 } 261 PetscFunctionReturn(0); 262 } 263 264 #undef __FUNCT__ 265 #define __FUNCT__ "PetscDrawFlush_X" 266 static PetscErrorCode PetscDrawFlush_X(PetscDraw draw) 267 { 268 PetscDraw_X* XiWin = (PetscDraw_X*)draw->data; 269 270 PetscFunctionBegin; 271 if (XiWin->drw && XiWin->win) XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0); 272 XFlush(XiWin->disp); 273 XSync(XiWin->disp,False); 274 PetscFunctionReturn(0); 275 } 276 277 #undef __FUNCT__ 278 #define __FUNCT__ "PetscDrawSynchronizedFlush_X" 279 static PetscErrorCode PetscDrawSynchronizedFlush_X(PetscDraw draw) 280 { 281 PetscErrorCode ierr; 282 PetscMPIInt rank; 283 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 284 285 PetscFunctionBegin; 286 if (XiWin->drw && XiWin->win) { 287 ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr); 288 /* make sure data has actually arrived at server */ 289 XSync(XiWin->disp,False); 290 ierr = MPI_Barrier(((PetscObject)draw)->comm);CHKERRQ(ierr); 291 if (!rank) { 292 XCopyArea(XiWin->disp,XiWin->drw,XiWin->win,XiWin->gc.set,0,0,XiWin->w,XiWin->h,0,0); 293 XFlush(XiWin->disp); 294 XSync(XiWin->disp,False); 295 } 296 ierr = MPI_Barrier(((PetscObject)draw)->comm);CHKERRQ(ierr); 297 } else { 298 ierr = MPI_Barrier(((PetscObject)draw)->comm);CHKERRQ(ierr); 299 XSync(XiWin->disp,False); 300 ierr = MPI_Barrier(((PetscObject)draw)->comm);CHKERRQ(ierr); 301 } 302 PetscFunctionReturn(0); 303 } 304 305 #undef __FUNCT__ 306 #define __FUNCT__ "PetscDrawSetViewport_X" 307 static PetscErrorCode PetscDrawSetViewport_X(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr) 308 { 309 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 310 XRectangle box; 311 312 PetscFunctionBegin; 313 box.x = (int)(xl*XiWin->w); box.y = (int)((1.0-yr)*XiWin->h); 314 box.width = (int)((xr-xl)*XiWin->w);box.height = (int)((yr-yl)*XiWin->h); 315 XSetClipRectangles(XiWin->disp,XiWin->gc.set,0,0,&box,1,Unsorted); 316 PetscFunctionReturn(0); 317 } 318 319 #undef __FUNCT__ 320 #define __FUNCT__ "PetscDrawClear_X" 321 static PetscErrorCode PetscDrawClear_X(PetscDraw draw) 322 { 323 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 324 int x, y, w, h; 325 PetscErrorCode ierr; 326 327 PetscFunctionBegin; 328 ierr = PetscDrawSave(draw);CHKERRQ(ierr); 329 x = (int)(draw->port_xl*XiWin->w); 330 w = (int)((draw->port_xr - draw->port_xl)*XiWin->w); 331 y = (int)((1.0-draw->port_yr)*XiWin->h); 332 h = (int)((draw->port_yr - draw->port_yl)*XiWin->h); 333 PetscDrawXiSetPixVal(XiWin,XiWin->background); 334 XFillRectangle(XiWin->disp,PetscDrawXiDrawable(XiWin),XiWin->gc.set,x,y,w,h); 335 PetscFunctionReturn(0); 336 } 337 338 #undef __FUNCT__ 339 #define __FUNCT__ "PetscDrawSynchronizedClear_X" 340 static PetscErrorCode PetscDrawSynchronizedClear_X(PetscDraw draw) 341 { 342 PetscErrorCode ierr; 343 PetscMPIInt rank; 344 PetscDraw_X *XiWin = (PetscDraw_X*)draw->data; 345 346 PetscFunctionBegin; 347 ierr = MPI_Barrier(((PetscObject)draw)->comm);CHKERRQ(ierr); 348 ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr); 349 if (!rank) { 350 ierr = PetscDrawClear_X(draw);CHKERRQ(ierr); 351 } 352 XFlush(XiWin->disp); 353 XFlush(XiWin->disp); 354 ierr = MPI_Barrier(((PetscObject)draw)->comm);CHKERRQ(ierr); 355 /* XSync(XiWin->disp,False); */ 356 ierr = MPI_Barrier(((PetscObject)draw)->comm);CHKERRQ(ierr); 357 PetscFunctionReturn(0); 358 } 359 360 #undef __FUNCT__ 361 #define __FUNCT__ "PetscDrawSetDoubleBuffer_X" 362 static PetscErrorCode PetscDrawSetDoubleBuffer_X(PetscDraw draw) 363 { 364 PetscDraw_X *win = (PetscDraw_X*)draw->data; 365 PetscErrorCode ierr; 366 PetscMPIInt rank; 367 368 PetscFunctionBegin; 369 if (win->drw) PetscFunctionReturn(0); 370 371 ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr); 372 if (!rank) win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth); 373 374 /* try to make sure it is actually done before passing info to all */ 375 XSync(win->disp,False); 376 ierr = MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);CHKERRQ(ierr); 377 PetscFunctionReturn(0); 378 } 379 380 #include <X11/cursorfont.h> 381 382 #undef __FUNCT__ 383 #define __FUNCT__ "PetscDrawGetMouseButton_X" 384 static PetscErrorCode PetscDrawGetMouseButton_X(PetscDraw draw,PetscDrawButton *button,PetscReal *x_user,PetscReal *y_user,PetscReal *x_phys,PetscReal *y_phys) 385 { 386 XEvent report; 387 PetscDraw_X *win = (PetscDraw_X*)draw->data; 388 Window root,child; 389 int root_x,root_y,px,py; 390 unsigned int keys_button; 391 Cursor cursor = 0; 392 393 PetscFunctionBegin; 394 /* change cursor to indicate input */ 395 if (!cursor) { 396 cursor = XCreateFontCursor(win->disp,XC_hand2); 397 if (!cursor) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to create X cursor"); 398 } 399 XDefineCursor(win->disp,win->win,cursor); 400 XSelectInput(win->disp,win->win,ButtonPressMask | ButtonReleaseMask); 401 402 while (XCheckTypedEvent(win->disp,ButtonPress,&report)); 403 XMaskEvent(win->disp,ButtonReleaseMask,&report); 404 switch (report.xbutton.button) { 405 case Button1: 406 if (report.xbutton.state & ShiftMask) *button = PETSC_BUTTON_LEFT_SHIFT; 407 else *button = PETSC_BUTTON_LEFT; 408 break; 409 case Button2: 410 if (report.xbutton.state & ShiftMask) *button = PETSC_BUTTON_CENTER_SHIFT; 411 else *button = PETSC_BUTTON_CENTER; 412 break; 413 case Button3: 414 if (report.xbutton.state & ShiftMask) *button = PETSC_BUTTON_RIGHT_SHIFT; 415 else *button = PETSC_BUTTON_RIGHT; 416 break; 417 } 418 XQueryPointer(win->disp,report.xmotion.window,&root,&child,&root_x,&root_y,&px,&py,&keys_button); 419 420 if (x_phys) *x_phys = ((double)px)/((double)win->w); 421 if (y_phys) *y_phys = 1.0 - ((double)py)/((double)win->h); 422 423 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); 424 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); 425 426 XUndefineCursor(win->disp,win->win); 427 XFlush(win->disp); 428 XSync(win->disp,False); 429 PetscFunctionReturn(0); 430 } 431 432 #undef __FUNCT__ 433 #define __FUNCT__ "PetscDrawPause_X" 434 static PetscErrorCode PetscDrawPause_X(PetscDraw draw) 435 { 436 PetscErrorCode ierr; 437 PetscDraw_X *win = (PetscDraw_X*)draw->data; 438 439 PetscFunctionBegin; 440 if (!win->win) PetscFunctionReturn(0); 441 if (draw->pause > 0) PetscSleep(draw->pause); 442 else if (draw->pause == -1) { 443 PetscDrawButton button; 444 PetscMPIInt rank; 445 ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr); 446 if (!rank) { 447 ierr = PetscDrawGetMouseButton(draw,&button,0,0,0,0);CHKERRQ(ierr); 448 if (button == PETSC_BUTTON_CENTER) draw->pause = 0; 449 } 450 ierr = MPI_Bcast(&draw->pause,1,MPIU_REAL,0,((PetscObject)draw)->comm);CHKERRQ(ierr); 451 } 452 PetscFunctionReturn(0); 453 } 454 455 #undef __FUNCT__ 456 #define __FUNCT__ "PetscDrawGetPopup_X" 457 static PetscErrorCode PetscDrawGetPopup_X(PetscDraw draw,PetscDraw *popup) 458 { 459 PetscErrorCode ierr; 460 PetscDraw_X *win = (PetscDraw_X*)draw->data; 461 PetscBool flg = PETSC_TRUE; 462 463 PetscFunctionBegin; 464 ierr = PetscOptionsGetBool(((PetscObject)draw)->prefix,"-draw_popup",&flg,NULL);CHKERRQ(ierr); 465 if (flg) { 466 ierr = PetscDrawOpenX(((PetscObject)draw)->comm,NULL,NULL,win->x,win->y+win->h+36,220,220,popup);CHKERRQ(ierr); 467 draw->popup = *popup; 468 } else { 469 *popup = NULL; 470 } 471 PetscFunctionReturn(0); 472 } 473 474 #undef __FUNCT__ 475 #define __FUNCT__ "PetscDrawSetTitle_X" 476 static PetscErrorCode PetscDrawSetTitle_X(PetscDraw draw,const char title[]) 477 { 478 PetscDraw_X *win = (PetscDraw_X*)draw->data; 479 XTextProperty prop; 480 PetscErrorCode ierr; 481 size_t len; 482 483 PetscFunctionBegin; 484 if (win->win) { 485 XGetWMName(win->disp,win->win,&prop); 486 XFree((void*)prop.value); 487 prop.value = (unsigned char*)title; 488 ierr = PetscStrlen(title,&len);CHKERRQ(ierr); 489 prop.nitems = (long) len; 490 XSetWMName(win->disp,win->win,&prop); 491 } 492 PetscFunctionReturn(0); 493 } 494 495 #undef __FUNCT__ 496 #define __FUNCT__ "PetscDrawResizeWindow_X" 497 static PetscErrorCode PetscDrawResizeWindow_X(PetscDraw draw,int w,int h) 498 { 499 PetscDraw_X *win = (PetscDraw_X*)draw->data; 500 unsigned int ww,hh,border,depth; 501 int x,y; 502 PetscErrorCode ierr; 503 Window root; 504 505 PetscFunctionBegin; 506 if (win->win) { 507 XResizeWindow(win->disp,win->win,w,h); 508 XGetGeometry(win->disp,win->win,&root,&x,&y,&ww,&hh,&border,&depth); 509 ierr = PetscDrawCheckResizedWindow(draw);CHKERRQ(ierr); 510 } 511 PetscFunctionReturn(0); 512 } 513 514 #undef __FUNCT__ 515 #define __FUNCT__ "PetscDrawCheckResizedWindow_X" 516 static PetscErrorCode PetscDrawCheckResizedWindow_X(PetscDraw draw) 517 { 518 PetscDraw_X *win = (PetscDraw_X*)draw->data; 519 PetscErrorCode ierr; 520 int x,y; 521 PetscMPIInt rank; 522 Window root; 523 unsigned int w,h,border,depth,geo[2]; 524 PetscReal xl,xr,yl,yr; 525 XRectangle box; 526 527 PetscFunctionBegin; 528 if (!win->win) PetscFunctionReturn(0); 529 ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr); 530 if (!rank) { 531 XFlush(win->disp); 532 XSync(win->disp,False); 533 XSync(win->disp,False); 534 XGetGeometry(win->disp,win->win,&root,&x,&y,geo,geo+1,&border,&depth); 535 } 536 ierr = MPI_Bcast(geo,2,MPI_UNSIGNED,0,((PetscObject)draw)->comm);CHKERRQ(ierr); 537 w = geo[0]; 538 h = geo[1]; 539 if (w == (unsigned int) win->w && h == (unsigned int) win->h) PetscFunctionReturn(0); 540 541 /* record new window sizes */ 542 543 win->h = h; win->w = w; 544 545 /* Free buffer space and create new version (only first processor does this) */ 546 if (win->drw) win->drw = XCreatePixmap(win->disp,win->win,win->w,win->h,win->depth); 547 548 /* reset the clipping */ 549 xl = draw->port_xl; yl = draw->port_yl; 550 xr = draw->port_xr; yr = draw->port_yr; 551 552 box.x = (int)(xl*win->w); box.y = (int)((1.0-yr)*win->h); 553 box.width = (int)((xr-xl)*win->w);box.height = (int)((yr-yl)*win->h); 554 XSetClipRectangles(win->disp,win->gc.set,0,0,&box,1,Unsorted); 555 556 /* try to make sure it is actually done before passing info to all */ 557 XSync(win->disp,False); 558 ierr = MPI_Bcast(&win->drw,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);CHKERRQ(ierr); 559 PetscFunctionReturn(0); 560 } 561 562 static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw,PetscDraw*); 563 static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw,PetscDraw*); 564 565 #undef __FUNCT__ 566 #define __FUNCT__ "PetscDrawDestroy_X" 567 PetscErrorCode PetscDrawDestroy_X(PetscDraw draw) 568 { 569 PetscDraw_X *win = (PetscDraw_X*)draw->data; 570 PetscErrorCode ierr; 571 #if defined(PETSC_HAVE_POPEN) 572 char command[PETSC_MAX_PATH_LEN]; 573 PetscMPIInt rank; 574 FILE *fd; 575 #endif 576 577 PetscFunctionBegin; 578 ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr); 579 580 #if defined(PETSC_HAVE_POPEN) 581 ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr); 582 if (draw->savefilename && !rank && draw->savefilemovie) { 583 ierr = PetscSNPrintf(command,PETSC_MAX_PATH_LEN,"ffmpeg -i %s/%s_%%d.Gif %s.m4v",draw->savefilename,draw->savefilename,draw->savefilename);CHKERRQ(ierr); 584 ierr = PetscPOpen(((PetscObject)draw)->comm,NULL,command,"r",&fd);CHKERRQ(ierr); 585 ierr = PetscPClose(((PetscObject)draw)->comm,fd,NULL);CHKERRQ(ierr); 586 } 587 #endif 588 589 XFreeGC(win->disp,win->gc.set); 590 XCloseDisplay(win->disp); 591 ierr = PetscDrawDestroy(&draw->popup);CHKERRQ(ierr); 592 ierr = PetscFree(win->font);CHKERRQ(ierr); 593 ierr = PetscFree(win);CHKERRQ(ierr); 594 PetscFunctionReturn(0); 595 } 596 597 PetscErrorCode PetscDrawSave_X(PetscDraw); 598 PetscErrorCode PetscDrawSetSave_X(PetscDraw,const char*); 599 600 static struct _PetscDrawOps DvOps = { PetscDrawSetDoubleBuffer_X, 601 PetscDrawFlush_X, 602 PetscDrawLine_X, 603 0, 604 0, 605 PetscDrawPoint_X, 606 0, 607 PetscDrawString_X, 608 PetscDrawStringVertical_X, 609 PetscDrawStringSetSize_X, 610 PetscDrawStringGetSize_X, 611 PetscDrawSetViewport_X, 612 PetscDrawClear_X, 613 PetscDrawSynchronizedFlush_X, 614 PetscDrawRectangle_X, 615 PetscDrawTriangle_X, 616 PetscDrawEllipse_X, 617 PetscDrawGetMouseButton_X, 618 PetscDrawPause_X, 619 PetscDrawSynchronizedClear_X, 620 0, 621 0, 622 PetscDrawGetPopup_X, 623 PetscDrawSetTitle_X, 624 PetscDrawCheckResizedWindow_X, 625 PetscDrawResizeWindow_X, 626 PetscDrawDestroy_X, 627 0, 628 PetscDrawGetSingleton_X, 629 PetscDrawRestoreSingleton_X, 630 #if defined(PETSC_HAVE_AFTERIMAGE) 631 PetscDrawSave_X, 632 #else 633 0, 634 #endif 635 PetscDrawSetSave_X, 636 0, 637 PetscDrawArrow_X, 638 PetscDrawCoordinateToPixel_X, 639 PetscDrawPixelToCoordinate_X, 640 PetscDrawPointPixel_X}; 641 642 643 extern PetscErrorCode PetscDrawXiQuickWindow(PetscDraw_X*,char*,char*,int,int,int,int); 644 extern PetscErrorCode PetscDrawXiQuickWindowFromWindow(PetscDraw_X*,char*,Window); 645 646 #undef __FUNCT__ 647 #define __FUNCT__ "PetscDrawGetSingleton_X" 648 static PetscErrorCode PetscDrawGetSingleton_X(PetscDraw draw,PetscDraw *sdraw) 649 { 650 PetscErrorCode ierr; 651 PetscDraw_X *Xwin = (PetscDraw_X*)draw->data,*sXwin; 652 653 PetscFunctionBegin; 654 ierr = PetscDrawCreate(PETSC_COMM_SELF,draw->display,draw->title,draw->x,draw->y,draw->w,draw->h,sdraw);CHKERRQ(ierr); 655 ierr = PetscObjectChangeTypeName((PetscObject)*sdraw,PETSC_DRAW_X);CHKERRQ(ierr); 656 ierr = PetscMemcpy((*sdraw)->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr); 657 658 (*sdraw)->ops->destroy = 0; 659 660 (*sdraw)->pause = draw->pause; 661 (*sdraw)->coor_xl = draw->coor_xl; 662 (*sdraw)->coor_xr = draw->coor_xr; 663 (*sdraw)->coor_yl = draw->coor_yl; 664 (*sdraw)->coor_yr = draw->coor_yr; 665 (*sdraw)->port_xl = draw->port_xl; 666 (*sdraw)->port_xr = draw->port_xr; 667 (*sdraw)->port_yl = draw->port_yl; 668 (*sdraw)->port_yr = draw->port_yr; 669 (*sdraw)->popup = draw->popup; 670 671 /* actually create and open the window */ 672 ierr = PetscNew(PetscDraw_X,&sXwin);CHKERRQ(ierr); 673 ierr = PetscDrawXiQuickWindowFromWindow(sXwin,draw->display,Xwin->win);CHKERRQ(ierr); 674 675 sXwin->x = Xwin->x; 676 sXwin->y = Xwin->y; 677 sXwin->w = Xwin->w; 678 sXwin->h = Xwin->h; 679 (*sdraw)->data = (void*)sXwin; 680 PetscFunctionReturn(0); 681 } 682 683 #undef __FUNCT__ 684 #define __FUNCT__ "PetscDrawRestoreSingleton_X" 685 static PetscErrorCode PetscDrawRestoreSingleton_X(PetscDraw draw,PetscDraw *sdraw) 686 { 687 PetscErrorCode ierr; 688 PetscDraw_X *sXwin = (PetscDraw_X*)(*sdraw)->data; 689 690 PetscFunctionBegin; 691 XFreeGC(sXwin->disp,sXwin->gc.set); 692 XCloseDisplay(sXwin->disp); 693 ierr = PetscDrawDestroy(&(*sdraw)->popup);CHKERRQ(ierr); 694 ierr = PetscFree((*sdraw)->title);CHKERRQ(ierr); 695 ierr = PetscFree((*sdraw)->display);CHKERRQ(ierr); 696 ierr = PetscFree(sXwin->font);CHKERRQ(ierr); 697 ierr = PetscFree(sXwin);CHKERRQ(ierr); 698 ierr = PetscHeaderDestroy(sdraw);CHKERRQ(ierr); 699 PetscFunctionReturn(0); 700 } 701 702 #undef __FUNCT__ 703 #define __FUNCT__ "PetscDrawXGetDisplaySize_Private" 704 PetscErrorCode PetscDrawXGetDisplaySize_Private(const char name[],int *width,int *height) 705 { 706 Display *display; 707 708 PetscFunctionBegin; 709 display = XOpenDisplay(name); 710 if (!display) { 711 *width = 0; 712 *height = 0; 713 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to open display on %s\n. Make sure your COMPUTE NODES are authorized to connect \n\ 714 to this X server and either your DISPLAY variable\n\ 715 is set or you use the -display name option\n",name); 716 } 717 *width = DisplayWidth(display,0); 718 *height = DisplayHeight(display,0); 719 XCloseDisplay(display); 720 PetscFunctionReturn(0); 721 } 722 723 EXTERN_C_BEGIN 724 #undef __FUNCT__ 725 #define __FUNCT__ "PetscDrawCreate_X" 726 PetscErrorCode PetscDrawCreate_X(PetscDraw draw) 727 { 728 PetscDraw_X *Xwin; 729 PetscErrorCode ierr; 730 PetscMPIInt rank; 731 PetscInt xywh[4],osize = 4; 732 int x = draw->x,y = draw->y,w = draw->w,h = draw->h; 733 static int xavailable = 0,yavailable = 0,xmax = 0,ymax = 0,ybottom = 0; 734 PetscBool flg = PETSC_FALSE; 735 736 PetscFunctionBegin; 737 if (!draw->display) { 738 ierr = PetscMalloc(256*sizeof(char),&draw->display);CHKERRQ(ierr); 739 ierr = PetscGetDisplay(draw->display,256);CHKERRQ(ierr); 740 } 741 742 /* 743 Initialize the display size 744 */ 745 if (!xmax) { 746 ierr = PetscDrawXGetDisplaySize_Private(draw->display,&xmax,&ymax); 747 /* if some processors fail on this and others succed then this is a problem ! */ 748 if (ierr) { 749 (*PetscErrorPrintf)("PETSc unable to use X windows\nproceeding without graphics\n"); 750 ierr = PetscDrawSetType(draw,PETSC_DRAW_NULL);CHKERRQ(ierr); 751 PetscFunctionReturn(0); 752 } 753 } 754 755 if (w == PETSC_DECIDE) w = draw->w = 300; 756 if (h == PETSC_DECIDE) h = draw->h = 300; 757 switch (w) { 758 case PETSC_DRAW_FULL_SIZE: w = draw->w = xmax - 10; break; 759 case PETSC_DRAW_HALF_SIZE: w = draw->w = (xmax - 20)/2; break; 760 case PETSC_DRAW_THIRD_SIZE: w = draw->w = (xmax - 30)/3; break; 761 case PETSC_DRAW_QUARTER_SIZE: w = draw->w = (xmax - 40)/4; break; 762 } 763 switch (h) { 764 case PETSC_DRAW_FULL_SIZE: h = draw->h = ymax - 10; break; 765 case PETSC_DRAW_HALF_SIZE: h = draw->h = (ymax - 20)/2; break; 766 case PETSC_DRAW_THIRD_SIZE: h = draw->h = (ymax - 30)/3; break; 767 case PETSC_DRAW_QUARTER_SIZE: h = draw->h = (ymax - 40)/4; break; 768 } 769 770 /* allow user to set location and size of window */ 771 xywh[0] = x; xywh[1] = y; xywh[2] = w; xywh[3] = h; 772 ierr = PetscOptionsGetIntArray(NULL,"-geometry",xywh,&osize,NULL);CHKERRQ(ierr); 773 774 x = (int) xywh[0]; y = (int) xywh[1]; w = (int) xywh[2]; h = (int) xywh[3]; 775 776 777 if (draw->x == PETSC_DECIDE || draw->y == PETSC_DECIDE) { 778 /* 779 PETSc tries to place windows starting in the upper left corner and 780 moving across to the right. 781 782 -------------------------------------------- 783 | Region used so far +xavailable,yavailable | 784 | + | 785 | + | 786 |++++++++++++++++++++++ybottom | 787 | | 788 | | 789 |--------------------------------------------| 790 */ 791 /* First: can we add it to the right? */ 792 if (xavailable+w+10 <= xmax) { 793 x = xavailable; 794 y = yavailable; 795 ybottom = PetscMax(ybottom,y + h + 30); 796 } else { 797 /* No, so add it below on the left */ 798 xavailable = 0; 799 x = 0; 800 yavailable = ybottom; 801 y = ybottom; 802 ybottom = ybottom + h + 30; 803 } 804 } 805 /* update available region */ 806 xavailable = PetscMax(xavailable,x + w + 10); 807 if (xavailable >= xmax) { 808 xavailable = 0; 809 yavailable = yavailable + h + 30; 810 ybottom = yavailable; 811 } 812 if (yavailable >= ymax) { 813 y = 0; 814 yavailable = 0; 815 ybottom = 0; 816 } 817 818 ierr = PetscMemcpy(draw->ops,&DvOps,sizeof(DvOps));CHKERRQ(ierr); 819 820 /* actually create and open the window */ 821 ierr = PetscNew(PetscDraw_X,&Xwin);CHKERRQ(ierr); 822 ierr = PetscLogObjectMemory(draw,sizeof(PetscDraw_X));CHKERRQ(ierr); 823 ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr); 824 825 if (!rank) { 826 if (x < 0 || y < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative corner of window"); 827 if (w <= 0 || h <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative window width or height"); 828 ierr = PetscDrawXiQuickWindow(Xwin,draw->display,draw->title,x,y,w,h);CHKERRQ(ierr); 829 ierr = MPI_Bcast(&Xwin->win,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);CHKERRQ(ierr); 830 } else { 831 unsigned long win = 0; 832 ierr = MPI_Bcast(&win,1,MPI_UNSIGNED_LONG,0,((PetscObject)draw)->comm);CHKERRQ(ierr); 833 ierr = PetscDrawXiQuickWindowFromWindow(Xwin,draw->display,win);CHKERRQ(ierr); 834 } 835 836 Xwin->x = x; 837 Xwin->y = y; 838 Xwin->w = w; 839 Xwin->h = h; 840 draw->data = (void*)Xwin; 841 842 /* 843 Need barrier here so processor 0 does not destroy the window before other 844 processors have completed PetscDrawXiQuickWindow() 845 */ 846 ierr = PetscDrawSynchronizedFlush(draw);CHKERRQ(ierr); 847 848 flg = PETSC_TRUE; 849 ierr = PetscOptionsGetBool(NULL,"-draw_double_buffer",&flg,NULL);CHKERRQ(ierr); 850 if (flg) { 851 ierr = PetscDrawSetDoubleBuffer(draw);CHKERRQ(ierr); 852 } 853 PetscFunctionReturn(0); 854 } 855 EXTERN_C_END 856 857 858 859 860 861 862 863 864 865