Actual source code: xinit.c
2: /*
3: This file contains routines to open an X window display and window
4: This consists of a number of routines that set the various
5: fields in the Window structure, which is passed to
6: all of these routines.
8: Note that if you use the default visual and colormap, then you
9: can use these routines with any X toolkit that will give you the
10: Window id of the window that it is managing. Use that instead of the
11: call to XiCreateWindow . Similarly for the Display.
12: */
14: #include <../src/sys/draw/impls/x/ximpl.h>
22: /*
23: XiOpenDisplay - Open a display
24: */
27: PetscErrorCode XiOpenDisplay(PetscDraw_X* XiWin,char *display_name)
28: {
30: XiWin->disp = XOpenDisplay(display_name);
31: if (!XiWin->disp) {
32: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Unable to open display on %s\n. Make sure your COMPUTE NODES are authorized to connect \n\
33: to this X server and either your DISPLAY variable\n\
34: is set or you use the -display name option\n",display_name);
35: }
36: XiWin->screen = DefaultScreen(XiWin->disp);
37: return(0);
38: }
41: /*
42: XiSetGC - set the GC structure in the base window
43: */
46: PetscErrorCode XiSetGC(PetscDraw_X* XiWin,PixVal fg)
47: {
48: XGCValues gcvalues; /* window graphics context values */
51: /* Set the graphics contexts */
52: /* create a gc for the ROP_SET operation (writing the fg value to a pixel) */
53: /* (do this with function GXcopy; GXset will automatically write 1) */
54: gcvalues.function = GXcopy;
55: gcvalues.foreground = fg;
56: XiWin->gc.cur_pix = fg;
57: XiWin->gc.set = XCreateGC(XiWin->disp,RootWindow(XiWin->disp,XiWin->screen),
58: GCFunction | GCForeground,&gcvalues);
59: return(0);
60: }
62: /*
63: Actually display a window at [x,y] with sizes (w,h)
64: If w and/or h are 0, use the sizes in the fields of XiWin
65: (which may have been set by, for example, XiSetWindowSize)
66: */
69: PetscErrorCode XiDisplayWindow(PetscDraw_X* XiWin,char *label,int x,int y,int w,int h,PixVal backgnd_pixel)
70: {
71: unsigned int wavail,havail;
72: XSizeHints size_hints;
73: XWindowAttributes in_window_attributes;
74: XSetWindowAttributes window_attributes;
75: int depth,border_width;
76: unsigned long wmask;
79: /* get the available widths */
80: wavail = DisplayWidth(XiWin->disp,XiWin->screen);
81: havail = DisplayHeight(XiWin->disp,XiWin->screen);
82: if (w <= 0 || h <= 0) PetscFunctionReturn(2);
83: if ((unsigned int) w > wavail) w = wavail;
84: if ((unsigned int) h > havail) h = havail;
86: /* changed the next line from xtools version */
87: border_width = 0;
88: if (x < 0) x = 0;
89: if (y < 0) y = 0;
90: x = ((unsigned int) x + w > wavail) ? wavail - w : x;
91: y = ((unsigned int) y + h > havail) ? havail - h : y;
93: /* We need XCreateWindow since we may need an visual other than
94: the default one */
95: XGetWindowAttributes(XiWin->disp,RootWindow(XiWin->disp,XiWin->screen),&in_window_attributes);
96: window_attributes.background_pixmap = None;
97: window_attributes.background_pixel = backgnd_pixel;
98: /* No border for now */
99: window_attributes.border_pixmap = None;
100: /*
101: window_attributes.border_pixel = border_pixel;
102: */
103: window_attributes.bit_gravity = in_window_attributes.bit_gravity;
104: window_attributes.win_gravity = in_window_attributes.win_gravity;
105: /* Backing store is too slow in color systems */
106: window_attributes.backing_store = 0;
107: window_attributes.backing_pixel = backgnd_pixel;
108: window_attributes.save_under = 1;
109: window_attributes.event_mask = 0;
110: window_attributes.do_not_propagate_mask = 0;
111: window_attributes.override_redirect = 0;
112: window_attributes.colormap = XiWin->cmap;
113: /* None for cursor does NOT mean none, it means cursor of Parent */
114: window_attributes.cursor = None;
115: wmask = CWBackPixmap | CWBackPixel | CWBorderPixmap | CWBitGravity |
116: CWWinGravity | CWBackingStore |CWBackingPixel|CWOverrideRedirect |
117: CWSaveUnder | CWEventMask | CWDontPropagate |
118: CWCursor | CWColormap ;
119: depth = XiWin->depth;
120: /* DefaultDepth(XiWin->disp,XiWin->screen); */
121: XiWin->win = XCreateWindow(XiWin->disp,
122: RootWindow(XiWin->disp,XiWin->screen),
123: x,y,w,h,border_width,
124: depth,InputOutput,XiWin->vis,
125: wmask,&window_attributes);
127: if (!XiWin->win) PetscFunctionReturn(2);
129: /* set window manager hints */
130: {
131: XWMHints wm_hints;
132: XClassHint class_hints;
133: XTextProperty windowname,iconname;
134:
135: if (label) { XStringListToTextProperty(&label,1,&windowname);}
136: else { XStringListToTextProperty(&label,0,&windowname);}
137: if (label) { XStringListToTextProperty(&label,1,&iconname);}
138: else { XStringListToTextProperty(&label,0,&iconname);}
139:
140: wm_hints.initial_state = NormalState;
141: wm_hints.input = True;
142: wm_hints.flags = StateHint|InputHint;
143:
144: class_hints.res_name = 0;
145: class_hints.res_class = (char*)"BaseClass"; /* this is nonsense */
147: size_hints.x = x;
148: size_hints.y = y;
149: size_hints.min_width = 4*border_width;
150: size_hints.min_height = 4*border_width;
151: size_hints.width = w;
152: size_hints.height = h;
153: size_hints.flags = USPosition | USSize | PMinSize;
154:
155: XSetWMProperties(XiWin->disp,XiWin->win,&windowname,&iconname,0,0,&size_hints,&wm_hints,&class_hints);
156: XFree((void*)windowname.value);
157: XFree((void*)iconname.value);
158: }
159: /* make the window visible */
160: XSelectInput(XiWin->disp,XiWin->win,ExposureMask | StructureNotifyMask);
161: XMapWindow(XiWin->disp,XiWin->win);
163: /* some window systems are cruel and interfere with the placement of
164: windows. We wait here for the window to be created or to die */
165: if (Xi_wait_map(XiWin)){
166: XiWin->win = (Window)0;
167: PetscFunctionReturn(1);
168: }
169: /* Initial values for the upper left corner */
170: XiWin->x = 0;
171: XiWin->y = 0;
172: return(0);
173: }
177: PetscErrorCode XiQuickWindow(PetscDraw_X* w,char* host,char* name,int x,int y,int nx,int ny)
178: {
182: XiOpenDisplay(w,host);
184: w->vis = DefaultVisual(w->disp,w->screen);
185: w->depth = DefaultDepth(w->disp,w->screen);
187: PetscDrawSetColormap_X(w,host,(Colormap)0);
189: XiDisplayWindow(w,name,x,y,nx,ny,(PixVal)0);
190: XiSetGC(w,w->cmapping[1]);
191: XiSetPixVal(w,w->background);
192: XSetWindowBackground(w->disp,w->win,w->cmapping[0]);
195: XiFontFixed(w,6,10,&w->font);
196: XFillRectangle(w->disp,w->win,w->gc.set,0,0,nx,ny);
197: return(0);
198: }
200: /*
201: A version from an already defined window
202: */
205: PetscErrorCode XiQuickWindowFromWindow(PetscDraw_X* w,char *host,Window win)
206: {
207: Window root;
209: int d;
210: unsigned int ud;
211: XWindowAttributes attributes;
214: XiOpenDisplay(w,host);
215: w->win = win;
216: XGetWindowAttributes(w->disp,w->win,&attributes);
218: w->vis = DefaultVisual(w->disp,w->screen);
219: w->depth = DefaultDepth(w->disp,w->screen);
220: PetscDrawSetColormap_X(w,host,attributes.colormap);
222: XGetGeometry(w->disp,w->win,&root,&d,&d,
223: (unsigned int *)&w->w,(unsigned int *)&w->h,&ud,&ud);
224: w->x = w->y = 0;
226: XiSetGC(w,w->cmapping[1]);
227: XiSetPixVal(w,w->background);
228: XSetWindowBackground(w->disp,w->win,w->cmapping[0]);
229: XiFontFixed(w,6,10,&w->font);
230: return(0);
231: }
233: /*
234: XiSetWindowLabel - Sets new label in open window.
235: */
238: PetscErrorCode XiSetWindowLabel(PetscDraw_X* Xiwin,char *label)
239: {
240: XTextProperty prop;
241: size_t len;
245: XGetWMName(Xiwin->disp,Xiwin->win,&prop);
246: prop.value = (unsigned char *)label;
247: PetscStrlen(label,&len);
248: prop.nitems = (long) len;
249: XSetWMName(Xiwin->disp,Xiwin->win,&prop);
250: return(0);
251: }
255: PetscErrorCode XiSetToBackground(PetscDraw_X* XiWin)
256: {
258: if (XiWin->gc.cur_pix != XiWin->background) {
259: XSetForeground(XiWin->disp,XiWin->gc.set,XiWin->background);
260: XiWin->gc.cur_pix = XiWin->background;
261: }
262: return(0);
264: }
268: PetscErrorCode PetscDrawSetSave_X(PetscDraw draw,const char *filename)
269: {
271: #if defined(PETSC_HAVE_POPEN)
272: PetscMPIInt rank;
273: char command[PETSC_MAX_PATH_LEN];
274: FILE *fd;
275: #endif
279: #if defined(PETSC_HAVE_POPEN)
280: MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
281: if (!rank) {
282: PetscSNPrintf(command,PETSC_MAX_PATH_LEN,"rm -f %s_[0-9]*.Gif %s.Mpeg",draw->savefilename,draw->savefilename);
283: PetscPOpen(((PetscObject)draw)->comm,PETSC_NULL,command,"r",&fd);
284: PetscPClose(((PetscObject)draw)->comm,fd);
285: }
286: #endif
287: return(0);
288: }
290: #if defined(PETSC_HAVE_AFTERIMAGE)
291: #include <afterimage.h>
294: PetscErrorCode PetscDrawSave_X(PetscDraw draw,PetscViewer viewer)
295: {
296: PetscDraw_X *drawx = (PetscDraw_X*)draw->data;
297: XImage *image;
298: ASImage *asimage;
299: static struct ASVisual *asv = 0;
300: char filename[PETSC_MAX_PATH_LEN];
301: PetscErrorCode ierr;
302: PetscMPIInt rank;
305: MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
306: if (rank) return(0);
307: if (!draw->savefilename) return(0);
308: if (!asv) {
309: asv = create_asvisual(drawx->disp, 0, 0, 0);if (!asv) SETERRQ(((PetscObject)draw)->comm,PETSC_ERR_PLIB,"Cannot create AfterImage ASVisual");
310: }
311: image = XGetImage(drawx->disp, drawx->win, 0, 0, drawx->w, drawx->h, AllPlanes, ZPixmap);if (!image) SETERRQ(((PetscObject)draw)->comm,PETSC_ERR_PLIB,"Cannot XGetImage()");
312: asimage = picture_ximage2asimage (asv,image,0,0);if (!asimage) SETERRQ(((PetscObject)draw)->comm,PETSC_ERR_PLIB,"Cannot create AfterImage ASImage");
313: PetscSNPrintf(filename,PETSC_MAX_PATH_LEN,"%s_%d.Gif",draw->savefilename,draw->savefilecount++);
314: ASImage2file( asimage, 0, filename,ASIT_Gif,0);
316: XDestroyImage(image);
317: return(0);
318: }
319: /*
320: There are routines wanted by AfterImage for PNG files
321: */
322: void crc32(void) {;}
323: void inflateReset(void) {;}
324: void deflateReset(void) {;}
325: void deflateInit2(void) {;}
326: void deflateInit2_(void) {;}
327: void deflate(void) {;}
328: void deflateEnd(void) {;}
330: #elif defined(PETSC_HAVE_IMAGEMAGICK)
331: #include <MagicCore/MagickCore.h>
334: PetscErrorCode PetscDrawSave_X(PetscDraw draw,PetscViewer viewer)
335: {
336: PetscDraw_X *drawx = (PetscDraw_X*)draw->data;
337: Image *image;
340: if (!IsMagickInstantiated()) {
341: MagickCoreGenesis(0,0);
342: }
343: image = XGetWindowImage(drawx->disp, drawx->win, 0, 0);if (!image) SETERRQ(((PetscObject)draw)->comm,PETSC_ERR_PLIB,"Cannot create ImageMagick");
344: return(0);
345: }
346: #endif