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