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