PageRenderTime 53ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/sys/draw/impls/x/xops.c

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