PageRenderTime 64ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

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

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