Actual source code: xinit.c
1: /*
2: This file contains routines to open an X window display and window
3: This consists of a number of routines that set the various
4: fields in the Window structure, which is passed to
5: all of these routines.
7: Note that if you use the default visual and colormap, then you
8: can use these routines with any X toolkit that will give you the
9: Window id of the window that it is managing. Use that instead of the
10: call to XiCreateWindow . Similarly for the Display.
11: */
13: #include src/sys/src/draw/impls/x/ximpl.h
15: EXTERN PetscErrorCode XiUniformHues(PetscDraw_X *,int);
16: EXTERN PetscErrorCode Xi_wait_map(PetscDraw_X*);
17: EXTERN PetscErrorCode XiInitColors(PetscDraw_X*,Colormap);
18: EXTERN PetscErrorCode XiFontFixed(PetscDraw_X*,int,int,XiFont**);
19: EXTERN PetscErrorCode XiInitCmap(PetscDraw_X*);
20: EXTERN PetscErrorCode PetscDrawSetColormap_X(PetscDraw_X*,char *,Colormap);
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_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: }
157: /* make the window visible */
158: XSelectInput(XiWin->disp,XiWin->win,ExposureMask | StructureNotifyMask);
159: XMapWindow(XiWin->disp,XiWin->win);
161: /* some window systems are cruel and interfere with the placement of
162: windows. We wait here for the window to be created or to die */
163: if (Xi_wait_map(XiWin)){
164: XiWin->win = (Window)0;
165: PetscFunctionReturn(1);
166: }
167: /* Initial values for the upper left corner */
168: XiWin->x = 0;
169: XiWin->y = 0;
170: return(0);
171: }
175: PetscErrorCode XiQuickWindow(PetscDraw_X* w,char* host,char* name,int x,int y,int nx,int ny)
176: {
180: XiOpenDisplay(w,host);
182: w->vis = DefaultVisual(w->disp,w->screen);
183: w->depth = DefaultDepth(w->disp,w->screen);
185: PetscDrawSetColormap_X(w,host,(Colormap)0);
187: XiDisplayWindow(w,name,x,y,nx,ny,(PixVal)0);
188: XiSetGC(w,w->cmapping[1]);
189: XiSetPixVal(w,w->background);
190: XSetWindowBackground(w->disp,w->win,w->cmapping[0]);
193: XiFontFixed(w,6,10,&w->font);
194: XFillRectangle(w->disp,w->win,w->gc.set,0,0,nx,ny);
195: return(0);
196: }
198: /*
199: A version from an already defined window
200: */
203: PetscErrorCode XiQuickWindowFromWindow(PetscDraw_X* w,char *host,Window win)
204: {
205: Window root;
207: int d;
208: unsigned int ud;
209: XWindowAttributes attributes;
212: XiOpenDisplay(w,host);
213: w->win = win;
214: XGetWindowAttributes(w->disp,w->win,&attributes);
216: w->vis = DefaultVisual(w->disp,w->screen);
217: w->depth = DefaultDepth(w->disp,w->screen);
218: PetscDrawSetColormap_X(w,host,attributes.colormap);
220: XGetGeometry(w->disp,w->win,&root,&d,&d,
221: (unsigned int *)&w->w,(unsigned int *)&w->h,&ud,&ud);
222: w->x = w->y = 0;
224: XiSetGC(w,w->cmapping[1]);
225: XiSetPixVal(w,w->background);
226: XSetWindowBackground(w->disp,w->win,w->cmapping[0]);
227: XiFontFixed(w,6,10,&w->font);
228: return(0);
229: }
231: /*
232: XiSetWindowLabel - Sets new label in open window.
233: */
236: PetscErrorCode XiSetWindowLabel(PetscDraw_X* Xiwin,char *label)
237: {
238: XTextProperty prop;
239: size_t len;
243: XGetWMName(Xiwin->disp,Xiwin->win,&prop);
244: prop.value = (unsigned char *)label;
245: PetscStrlen(label,&len);
246: prop.nitems = (long) len;
247: XSetWMName(Xiwin->disp,Xiwin->win,&prop);
248: return(0);
249: }
253: PetscErrorCode XiSetToBackground(PetscDraw_X* XiWin)
254: {
256: if (XiWin->gc.cur_pix != XiWin->background) {
257: XSetForeground(XiWin->disp,XiWin->gc.set,XiWin->background);
258: XiWin->gc.cur_pix = XiWin->background;
259: }
260: return(0);
261: }