5 /* Copyright (c) 1994-1996 David Hogan, see README for licence details */
12 #include <X11/Xutil.h>
13 #include <X11/Xatom.h>
14 #include <X11/extensions/shape.h>
21 manage(Client *c, int mapped)
23 int fixsize, dohide, doreshape, state;
27 XSetWindowAttributes attrs;
29 trace("manage", c, 0);
30 XSelectInput(dpy, c->window, ColormapChangeMask | EnterWindowMask | PropertyChangeMask | FocusChangeMask | KeyPressMask);
32 /* Get loads of hints */
34 if(XGetClassHint(dpy, c->window, &class) != 0){ /* ``Success'' */
35 c->instance = class.res_name;
36 c->class = class.res_class;
39 c->is9term = strstr(c->class, "term") || strstr(c->class, "Term");
48 c->iconname = getprop(c->window, XA_WM_ICON_NAME);
49 c->name = getprop(c->window, XA_WM_NAME);
52 hints = XGetWMHints(dpy, c->window);
53 if(XGetWMNormalHints(dpy, c->window, &c->size, &msize) == 0 || c->size.flags == 0)
54 c->size.flags = PSize; /* not specified - punt */
60 c->hold = getiprop(c->window, _rio_hold_mode);
62 /* Figure out what to do with the window from hints */
64 if(!getstate(c->window, &state))
65 state = hints ? hints->initial_state : NormalState;
66 dohide = (state == IconicState);
69 if((c->size.flags & (USSize|PSize)))
71 if((c->size.flags & (PMinSize|PMaxSize)) == (PMinSize|PMaxSize) && c->size.min_width == c->size.max_width && c->size.min_height == c->size.max_height)
75 if(c->size.flags & USPosition)
77 if(dohide && (c->size.flags & PPosition))
84 if(c->size.flags & PBaseSize){
85 c->min_dx = c->size.base_width;
86 c->min_dy = c->size.base_height;
88 else if(c->size.flags & PMinSize){
89 c->min_dx = c->size.min_width;
90 c->min_dy = c->size.min_height;
97 c->min_dx = c->min_dy = 0;
105 if(0) fprintf(stderr, "in doreshape is9term=%d fixsize=%d, x=%d, y=%d, min_dx=%d, min_dy=%d, dx=%d, dy=%d\n",
106 c->is9term, fixsize, c->x, c->y, c->min_dx, c->min_dy, c->dx, c->dy);
107 if(current && current->screen == c->screen)
108 cmapnofocus(c->screen);
109 if(!c->is9term && c->x==0 && c->y==0){
112 c->x = 20*nwin+BORDER;
113 c->y = 20*nwin+BORDER;
118 if(c->is9term && !(fixsize ? drag(c, Button3) : sweep(c, Button3))){
119 XKillClient(dpy, c->window);
121 if(current && current->screen == c->screen)
127 attrs.border_pixel = c->screen->black;
128 attrs.background_pixel = c->screen->white;
129 attrs.colormap = c->screen->def_cmap;
130 c->parent = XCreateWindow(dpy, c->screen->root,
131 c->x - BORDER, c->y - BORDER,
132 c->dx + 2*BORDER, c->dy + 2*BORDER,
137 CWBackPixel | CWBorderPixel | CWColormap,
140 XSelectInput(dpy, c->parent, SubstructureRedirectMask | SubstructureNotifyMask|ButtonPressMask| PointerMotionMask|LeaveWindowMask|KeyPressMask);
143 if(doreshape && !fixsize)
144 XResizeWindow(dpy, c->window, c->dx, c->dy);
145 XSetWindowBorderWidth(dpy, c->window, 0);
148 * To have something more than only a big white or black border
149 * XXX should replace this by a pattern in the white or black
150 * such that we can see the border also if all our
151 * windows are black and/or white
152 * (black (or white) border around black (or white) window
153 * is not very helpful.
155 if(c->screen->depth <= 8){
156 XSetWindowBorderWidth(dpy, c->parent, 1);
159 XReparentWindow(dpy, c->window, c->parent, BORDER, BORDER);
162 XShapeSelectInput(dpy, c->window, ShapeNotifyMask);
163 ignore_badwindow = 1; /* magic */
165 ignore_badwindow = 0;
168 XAddToSaveSet(dpy, c->window);
172 XMapWindow(dpy, c->window);
173 XMapWindow(dpy, c->parent);
174 XUnmapWindow(dpy, c->screen->sweepwin);
175 if(nostalgia || doreshape)
177 else if(c->trans != None && current && current->window == c->trans)
181 setstate(c, NormalState);
183 if(current && (current != c))
188 * If we swept the window, let's send a resize event to the
189 * guy who just got resized. It's not clear whether the apps
190 * should notice their new size via other means. Try as I might,
191 * I can't find a way to have them notice during initdraw, so
192 * I solve the problem this way instead. -rsc
200 scanwins(ScreenInfo *s)
202 unsigned int i, nwins;
204 Window dw1, dw2, *wins;
205 XWindowAttributes attr;
207 XQueryTree(dpy, s->root, &dw1, &dw2, &wins, &nwins);
208 for(i = 0; i < nwins; i++){
209 XGetWindowAttributes(dpy, wins[i], &attr);
210 if(attr.override_redirect || wins[i] == s->menuwin)
212 c = getclient(wins[i], 1);
213 if(c != 0 && c->window == wins[i] && !c->init){
218 c->border = attr.border_width;
221 if(attr.map_state == IsViewable)
225 XFree((void *) wins); /* cast is to shut stoopid compiler up */
234 if(XGetTransientForHint(dpy, c->window, &trans) != 0)
243 XUnmapWindow(dpy, c->parent);
244 XReparentWindow(dpy, c->window, c->screen->root, c->x, c->y);
245 XRemoveFromSaveSet(dpy, c->window);
246 setstate(c, WithdrawnState);
248 /* flush any errors */
249 ignore_badwindow = 1;
251 ignore_badwindow = 0;
255 installcmap(ScreenInfo *s, Colormap cmap)
258 XInstallColormap(dpy, s->def_cmap);
260 XInstallColormap(dpy, cmap);
271 else if(c->ncmapwins != 0){
273 for(i = c->ncmapwins-1; i >= 0; i--){
274 installcmap(c->screen, c->wmcmaps[i]);
275 if(c->cmapwins[i] == c->window)
279 installcmap(c->screen, c->cmap);
281 else if(c->trans != None && (cc = getclient(c->trans, 0)) != 0 && cc->ncmapwins != 0)
284 installcmap(c->screen, c->cmap);
288 cmapnofocus(ScreenInfo *s)
290 installcmap(s, None);
298 XWindowAttributes attr;
301 ignore_badwindow = 1;
302 XGetWindowAttributes(dpy, c->window, &attr);
303 c->cmap = attr.colormap;
305 ignore_badwindow = 0;
308 n = _getprop(c->window, wm_colormaps, XA_WINDOW, 100L, (void*)&cw);
309 if(c->ncmapwins != 0){
310 XFree((char *)c->cmapwins);
311 free((char *)c->wmcmaps);
321 c->wmcmaps = (Colormap*)malloc(n*sizeof(Colormap));
322 for(i = 0; i < n; i++){
323 if(cw[i] == c->window)
324 c->wmcmaps[i] = c->cmap;
326 /* flush any errors (e.g., caused by mozilla tabs) */
327 ignore_badwindow = 1;
328 XSelectInput(dpy, cw[i], ColormapChangeMask);
329 XGetWindowAttributes(dpy, cw[i], &attr);
330 c->wmcmaps[i] = attr.colormap;
332 ignore_badwindow = 0;
344 else if(c->name != 0)
346 else if(c->instance != 0)
348 else if(c->class != 0)
352 if((p = index(label, ':')) != 0)
364 /* don't try to add a border if the window is non-rectangular */
365 rect = XShapeGetRectangles(dpy, c->window, ShapeBounding, &n, &order);
367 XShapeCombineShape(dpy, c->parent, ShapeBounding, BORDER, BORDER,
368 c->window, ShapeBounding, ShapeSet);
374 _getprop(Window w, Atom a, Atom type, long len, unsigned char **p)
378 unsigned long n, extra;
381 status = XGetWindowProperty(dpy, w, a, 0L, len, False, type, &real_type, &format, &n, &extra, p);
382 if(status != Success || *p == 0)
386 /* could check real_type, format, extra here... */
391 getprop(Window w, Atom a)
395 if(_getprop(w, a, XA_STRING, 100L, &p) <= 0)
401 get1prop(Window w, Atom a, Atom type)
405 if(_getprop(w, a, type, 1L, (void*)&p) <= 0)
409 return (int)(uintptr_t)x;
413 getwprop(Window w, Atom a)
415 return get1prop(w, a, XA_WINDOW);
419 getiprop(Window w, Atom a)
421 return get1prop(w, a, XA_INTEGER);
425 setstate(Client *c, int state)
429 data[0] = (long) state;
430 data[1] = (long) None;
433 XChangeProperty(dpy, c->window, wm_state, wm_state, 32,
434 PropModeReplace, (unsigned char *)data, 2);
438 getstate(Window w, int *state)
442 if(_getprop(w, wm_state, wm_state, 2L, (void*)&p) <= 0)
460 if((n = _getprop(w, wm_protocols, XA_ATOM, 20L, (void*)&p)) <= 0)
463 for(i = 0; i < n; i++)
464 if(p[i] == wm_delete)
466 else if(p[i] == wm_take_focus)
467 c->proto |= Ptakefocus;
468 else if(p[i] == wm_lose_focus)
469 c->proto |= Plosefocus;