Actual source code: xinit.c

petsc-3.3-p7 2013-05-11
  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 PetscDrawXiCreateWindow .  Similarly for the Display.
 12: */

 14: #include <../src/sys/draw/impls/x/ximpl.h>

 16: extern PetscErrorCode PetscDrawXiUniformHues(PetscDraw_X *,int);
 17: extern PetscErrorCode PetscDrawXi_wait_map(PetscDraw_X*);
 18: extern PetscErrorCode PetscDrawXiFontFixed(PetscDraw_X*,int,int,PetscDrawXiFont**);
 19: extern PetscErrorCode PetscDrawXiInitCmap(PetscDraw_X*);
 20: extern PetscErrorCode PetscDrawSetColormap_X(PetscDraw_X*,char *,Colormap);

 22: /*
 23:   PetscDrawXiOpenDisplay - Open a display
 24: */
 27: PetscErrorCode PetscDrawXiOpenDisplay(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:    PetscDrawXiSetGC - set the GC structure in the base window
 43: */
 46: PetscErrorCode PetscDrawXiSetGC(PetscDraw_X* XiWin,PetscDrawXiPixVal 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),GCFunction | GCForeground,&gcvalues);
 58:   return(0);
 59: }

 61: /*
 62:     Actually display a window at [x,y] with sizes (w,h)
 63:     If w and/or h are 0, use the sizes in the fields of XiWin
 64:     (which may have been set by, for example, PetscDrawXiSetWindowSize)
 65: */
 68: PetscErrorCode PetscDrawXiDisplayWindow(PetscDraw_X* XiWin,char *label,int x,int y,int w,int h,PetscDrawXiPixVal backgnd_pixel)
 69: {
 70:   unsigned int            wavail,havail;
 71:   XSizeHints              size_hints;
 72:   XWindowAttributes       in_window_attributes;
 73:   XSetWindowAttributes    window_attributes;
 74:   int                     depth,border_width;
 75:   unsigned long           wmask;

 78:   /* get the available widths */
 79:   wavail              = DisplayWidth(XiWin->disp,XiWin->screen);
 80:   havail              = DisplayHeight(XiWin->disp,XiWin->screen);
 81:   if (w <= 0 || h <= 0) PetscFunctionReturn(2);
 82:   if ((unsigned int) w > wavail) w = wavail;
 83:   if ((unsigned int) h > havail) h = havail;

 85:   /* changed the next line from xtools version */
 86:   border_width   = 0;
 87:   if (x < 0) x   = 0;
 88:   if (y < 0) y   = 0;
 89:   x   = ((unsigned int) x + w > wavail) ? wavail - w : x;
 90:   y   = ((unsigned int) y + h > havail) ? havail - h : y;

 92:   /* We need XCreateWindow since we may need an visual other than
 93:    the default one */
 94:   XGetWindowAttributes(XiWin->disp,RootWindow(XiWin->disp,XiWin->screen),&in_window_attributes);
 95:   window_attributes.background_pixmap = None;
 96:   window_attributes.background_pixel  = backgnd_pixel;
 97:   /* No border for now */
 98:   window_attributes.border_pixmap     = None;
 99:   /* 
100:   window_attributes.border_pixel      = border_pixel; 
101:   */
102:   window_attributes.bit_gravity       = in_window_attributes.bit_gravity;
103:   window_attributes.win_gravity       = in_window_attributes.win_gravity;
104:         /* Backing store is too slow in color systems */
105:   window_attributes.backing_store     = 0;
106:   window_attributes.backing_pixel     = backgnd_pixel;
107:   window_attributes.save_under        = 1;
108:   window_attributes.event_mask        = 0;
109:   window_attributes.do_not_propagate_mask = 0;
110:   window_attributes.override_redirect = 0;
111:   window_attributes.colormap          = XiWin->cmap;
112:   /* None for cursor does NOT mean none, it means cursor of Parent */
113:   window_attributes.cursor            = None;
114:   wmask   = CWBackPixmap | CWBackPixel | CWBorderPixmap | CWBitGravity |
115:             CWWinGravity | CWBackingStore |CWBackingPixel|CWOverrideRedirect |
116:             CWSaveUnder  | CWEventMask    | CWDontPropagate |
117:             CWCursor     | CWColormap ;
118:   depth       = XiWin->depth;
119:   /* DefaultDepth(XiWin->disp,XiWin->screen); */
120:   XiWin->win  = XCreateWindow(XiWin->disp,RootWindow(XiWin->disp,XiWin->screen),x,y,w,h,border_width,depth,InputOutput,XiWin->vis,wmask,&window_attributes);

122:   if (!XiWin->win)  PetscFunctionReturn(2);

124:   /* set window manager hints */
125:   {
126:     XWMHints      wm_hints;
127:     XClassHint    class_hints;
128:     XTextProperty windowname,iconname;
129: 
130:     if (label) { XStringListToTextProperty(&label,1,&windowname);}
131:     else       { XStringListToTextProperty(&label,0,&windowname);}
132:     if (label) { XStringListToTextProperty(&label,1,&iconname);}
133:     else       { XStringListToTextProperty(&label,0,&iconname);}
134: 
135:     wm_hints.initial_state  = NormalState;
136:     wm_hints.input          = True;
137:     wm_hints.flags          = StateHint|InputHint;

139:     /* These properties can be used by window managers to decide how to display a window */
140:     class_hints.res_name    = (char*)"petsc";
141:     class_hints.res_class   = (char*)"PETSc";

143:     size_hints.x            = x;
144:     size_hints.y            = y;
145:     size_hints.min_width    = 4*border_width;
146:     size_hints.min_height   = 4*border_width;
147:     size_hints.width        = w;
148:     size_hints.height       = h;
149:     size_hints.flags        = USPosition | USSize | PMinSize;
150: 
151:     XSetWMProperties(XiWin->disp,XiWin->win,&windowname,&iconname,0,0,&size_hints,&wm_hints,&class_hints);
152:     XFree((void*)windowname.value);
153:     XFree((void*)iconname.value);
154:   }
155:   /* make the window visible */
156:   XSelectInput(XiWin->disp,XiWin->win,ExposureMask | StructureNotifyMask);
157:   XMapWindow(XiWin->disp,XiWin->win);

159:   /* some window systems are cruel and interfere with the placement of
160:      windows.  We wait here for the window to be created or to die */
161:   if (PetscDrawXi_wait_map(XiWin)){
162:     XiWin->win    = (Window)0;
163:     PetscFunctionReturn(1);
164:   }
165:   /* Initial values for the upper left corner */
166:   XiWin->x = 0;
167:   XiWin->y = 0;
168:   return(0);
169: }

173: PetscErrorCode PetscDrawXiQuickWindow(PetscDraw_X* w,char* host,char* name,int x,int y,int nx,int ny)
174: {

178:   PetscDrawXiOpenDisplay(w,host);

180:   w->vis    = DefaultVisual(w->disp,w->screen);
181:   w->depth  = DefaultDepth(w->disp,w->screen);

183:   PetscDrawSetColormap_X(w,host,(Colormap)0);

185:   PetscDrawXiDisplayWindow(w,name,x,y,nx,ny,(PetscDrawXiPixVal)0);
186:   PetscDrawXiSetGC(w,w->cmapping[1]);
187:   PetscDrawXiSetPixVal(w,w->background);
188:   XSetWindowBackground(w->disp,w->win,w->cmapping[0]);


191:   PetscDrawXiFontFixed(w,6,10,&w->font);
192:   XFillRectangle(w->disp,w->win,w->gc.set,0,0,nx,ny);
193:   return(0);
194: }

196: /* 
197:    A version from an already defined window 
198: */
201: PetscErrorCode PetscDrawXiQuickWindowFromWindow(PetscDraw_X* w,char *host,Window win)
202: {
203:   Window            root;
204:   PetscErrorCode    ierr;
205:   int               d;
206:   unsigned int      ud;
207:   XWindowAttributes attributes;

210:   PetscDrawXiOpenDisplay(w,host);
211:   w->win = win;
212:   XGetWindowAttributes(w->disp,w->win,&attributes);

214:   w->vis    = DefaultVisual(w->disp,w->screen);
215:   w->depth  = DefaultDepth(w->disp,w->screen);
216:   PetscDrawSetColormap_X(w,host,attributes.colormap);

218:   XGetGeometry(w->disp,w->win,&root,&d,&d,(unsigned int *)&w->w,(unsigned int *)&w->h,&ud,&ud);
219:   w->x = w->y = 0;

221:   PetscDrawXiSetGC(w,w->cmapping[1]);
222:   PetscDrawXiSetPixVal(w,w->background);
223:   XSetWindowBackground(w->disp,w->win,w->cmapping[0]);
224:   PetscDrawXiFontFixed(w,6,10,&w->font);
225:   return(0);
226: }

228: /*
229:       PetscDrawXiSetWindowLabel - Sets new label in open window.
230: */
233: PetscErrorCode PetscDrawXiSetWindowLabel(PetscDraw_X* Xiwin,char *label)
234: {
235:   XTextProperty prop;
236:   size_t        len;

240:   XGetWMName(Xiwin->disp,Xiwin->win,&prop);
241:   prop.value  = (unsigned char *)label;
242:   PetscStrlen(label,&len);
243:   prop.nitems = (long) len;
244:   XSetWMName(Xiwin->disp,Xiwin->win,&prop);
245:   return(0);
246: }

250: PetscErrorCode PetscDrawXiSetToBackground(PetscDraw_X* XiWin)
251: {
253:   if (XiWin->gc.cur_pix != XiWin->background) {
254:     XSetForeground(XiWin->disp,XiWin->gc.set,XiWin->background);
255:     XiWin->gc.cur_pix   = XiWin->background;
256:   }
257:   return(0);

259: }

263: PetscErrorCode  PetscDrawSetSave_X(PetscDraw draw,const char *filename)
264: {
266: #if defined(PETSC_HAVE_POPEN)
267:   PetscMPIInt    rank;
268:   char           command[PETSC_MAX_PATH_LEN];
269:   FILE           *fd;
270: #endif

274: #if defined(PETSC_HAVE_POPEN)
275:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
276:   if (!rank) {
277:     PetscSNPrintf(command,PETSC_MAX_PATH_LEN,"rm -f %s_[0-9]*.Gif %s.Mpeg",draw->savefilename,draw->savefilename);
278:     PetscPOpen(((PetscObject)draw)->comm,PETSC_NULL,command,"r",&fd);
279:     PetscPClose(((PetscObject)draw)->comm,fd);
280:   }
281: #endif
282:   return(0);
283: }

285: #if defined(PETSC_HAVE_AFTERIMAGE)
286: #include <afterimage.h>
289: PetscErrorCode PetscDrawSave_X(PetscDraw draw)
290: {
291:   PetscDraw_X              *drawx = (PetscDraw_X*)draw->data;
292:   XImage                   *image;
293:   ASImage                  *asimage;
294:   static struct  ASVisual  *asv = 0;
295:   char                     filename[PETSC_MAX_PATH_LEN];
296:   PetscErrorCode           ierr;
297:   PetscMPIInt              rank;

300:   MPI_Comm_rank(((PetscObject)draw)->comm,&rank);
301:   if (rank) return(0);
302:   if (!draw->savefilename) return(0);
303:   if (!asv) {
304:       asv = create_asvisual(drawx->disp, 0, 0, 0);if (!asv) SETERRQ(((PetscObject)draw)->comm,PETSC_ERR_PLIB,"Cannot create AfterImage ASVisual");
305:   }
306:   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()");
307:   asimage = picture_ximage2asimage (asv,image,0,0);if (!asimage) SETERRQ(((PetscObject)draw)->comm,PETSC_ERR_PLIB,"Cannot create AfterImage ASImage");
308:   PetscSNPrintf(filename,PETSC_MAX_PATH_LEN,"%s_%d.Gif",draw->savefilename,draw->savefilecount++);
309:   ASImage2file( asimage, 0, filename,ASIT_Gif,0);

311:   XDestroyImage(image);
312:   return(0);
313: }
314: /*
315:    There are routines wanted by AfterImage for PNG files
316:  */
317: void crc32(void) {;}
318: void inflateReset(void) {;}
319: void deflateReset(void) {;}
320: void deflateInit2(void) {;}
321: void deflateInit2_(void) {;}
322: void deflate(void) {;}
323: void deflateEnd(void) {;}

325: #endif