1 /* Copyright (c) 1994-1996 David Hogan, see README for licence details */
9 #include <X11/extensions/shape.h>
16 manage(Client *c, int mapped)
18 int fixsize, dohide, doreshape, state;
22 XSetWindowAttributes attrs;
24 trace("manage", c, 0);
25 XSelectInput(dpy, c->window, ColormapChangeMask | EnterWindowMask | PropertyChangeMask | FocusChangeMask);
27 /* Get loads of hints */
29 if (XGetClassHint(dpy, c->window, &class) != 0) { /* ``Success'' */
30 c->instance = class.res_name;
31 c->class = class.res_class;
34 c->is9term = strstr(c->class, "term") || strstr(c->class, "Term");
43 c->iconname = getprop(c->window, XA_WM_ICON_NAME);
44 c->name = getprop(c->window, XA_WM_NAME);
47 hints = XGetWMHints(dpy, c->window);
48 if (XGetWMNormalHints(dpy, c->window, &c->size, &msize) == 0 || c->size.flags == 0)
49 c->size.flags = PSize; /* not specified - punt */
55 c->hold = getiprop(c->window, _rio_hold_mode);
57 /* Figure out what to do with the window from hints */
59 if (!getstate(c->window, &state))
60 state = hints ? hints->initial_state : NormalState;
61 dohide = (state == IconicState);
64 if ((c->size.flags & (USSize|PSize)))
66 if ((c->size.flags & (PMinSize|PMaxSize)) == (PMinSize|PMaxSize) && c->size.min_width == c->size.max_width && c->size.min_height == c->size.max_height)
70 if (c->size.flags & USPosition)
72 if (dohide && (c->size.flags & PPosition))
79 if (c->size.flags & PBaseSize) {
80 c->min_dx = c->size.base_width;
81 c->min_dy = c->size.base_height;
83 else if (c->size.flags & PMinSize) {
84 c->min_dx = c->size.min_width;
85 c->min_dy = c->size.min_height;
87 else if (c->is9term) {
92 c->min_dx = c->min_dy = 0;
100 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",
101 c->is9term, fixsize, c->x, c->y, c->min_dx, c->min_dy, c->dx, c->dy);
102 if (current && current->screen == c->screen)
103 cmapnofocus(c->screen);
104 if (!c->is9term && c->x==0 && c->y==0) {
107 c->x = 20*nwin+BORDER;
108 c->y = 20*nwin+BORDER;
113 if (c->is9term && !(fixsize ? drag(c, Button3) : sweep(c, Button3))) {
114 XKillClient(dpy, c->window);
116 if (current && current->screen == c->screen)
124 attrs.border_pixel = c->screen->black;
125 attrs.background_pixel = c->screen->white;
126 attrs.colormap = c->screen->def_cmap;
127 c->parent = XCreateWindow(dpy, c->screen->root,
128 c->x - BORDER, c->y - BORDER,
129 c->dx + 2*BORDER, c->dy + 2*BORDER,
134 CWBackPixel | CWBorderPixel | CWColormap,
137 XSelectInput(dpy, c->parent, SubstructureRedirectMask | SubstructureNotifyMask|ButtonPressMask| PointerMotionMask|LeaveWindowMask);
140 if (doreshape && !fixsize)
141 XResizeWindow(dpy, c->window, c->dx, c->dy);
142 XSetWindowBorderWidth(dpy, c->window, 0);
145 * To have something more than only a big white or black border
146 * XXX should replace this by a pattern in the white or black
147 * such that we can see the border also if all our
148 * windows are black and/or white
149 * (black (or white) border around black (or white) window
150 * is not very helpful.
152 if (c->screen->depth <= 8) {
153 XSetWindowBorderWidth(dpy, c->parent, 1);
156 XReparentWindow(dpy, c->window, c->parent, BORDER, BORDER);
159 XShapeSelectInput(dpy, c->window, ShapeNotifyMask);
160 ignore_badwindow = 1; /* magic */
162 ignore_badwindow = 0;
165 XAddToSaveSet(dpy, c->window);
169 XMapWindow(dpy, c->window);
170 XMapWindow(dpy, c->parent);
171 XUnmapWindow(dpy, c->screen->sweepwin);
172 if (nostalgia || doreshape)
174 else if (c->trans != None && current && current->window == c->trans)
178 setstate(c, NormalState);
180 if (current && (current != c))
185 * If we swept the window, let's send a resize event to the
186 * guy who just got resized. It's not clear whether the apps
187 * should notice their new size via other means. Try as I might,
188 * I can't find a way to have them notice during initdraw, so
189 * I solve the problem this way instead. -rsc
197 scanwins(ScreenInfo *s)
199 unsigned int i, nwins;
201 Window dw1, dw2, *wins;
202 XWindowAttributes attr;
204 XQueryTree(dpy, s->root, &dw1, &dw2, &wins, &nwins);
205 for (i = 0; i < nwins; i++) {
206 XGetWindowAttributes(dpy, wins[i], &attr);
207 if (attr.override_redirect || wins[i] == s->menuwin)
209 c = getclient(wins[i], 1);
210 if (c != 0 && c->window == wins[i] && !c->init) {
215 c->border = attr.border_width;
218 if (attr.map_state == IsViewable)
222 XFree((void *) wins); /* cast is to shut stoopid compiler up */
231 if (XGetTransientForHint(dpy, c->window, &trans) != 0)
240 XUnmapWindow(dpy, c->parent);
242 XReparentWindow(dpy, c->window, c->screen->root, c->x, c->y);
244 XRemoveFromSaveSet(dpy, c->window);
245 setstate(c, WithdrawnState);
247 /* flush any errors */
248 ignore_badwindow = 1;
250 ignore_badwindow = 0;
254 gravitate(Client *c, int invert)
256 int gravity, dx, dy, delta;
258 gravity = NorthWestGravity;
259 if (c->size.flags & PWinGravity)
260 gravity = c->size.win_gravity;
262 delta = c->border-BORDER;
264 case NorthWestGravity:
272 case NorthEastGravity:
289 case SouthWestGravity:
297 case SouthEastGravity:
302 fprintf(stderr, "rio: bad window gravity %d for 0x%x\n", gravity, (int)c->window);
316 installcmap(ScreenInfo *s, Colormap cmap)
319 XInstallColormap(dpy, s->def_cmap);
321 XInstallColormap(dpy, cmap);
332 else if (c->ncmapwins != 0) {
334 for (i = c->ncmapwins-1; i >= 0; i--) {
335 installcmap(c->screen, c->wmcmaps[i]);
336 if (c->cmapwins[i] == c->window)
340 installcmap(c->screen, c->cmap);
342 else if (c->trans != None && (cc = getclient(c->trans, 0)) != 0 && cc->ncmapwins != 0)
345 installcmap(c->screen, c->cmap);
349 cmapnofocus(ScreenInfo *s)
351 installcmap(s, None);
359 XWindowAttributes attr;
362 ignore_badwindow = 1;
363 XGetWindowAttributes(dpy, c->window, &attr);
364 c->cmap = attr.colormap;
366 ignore_badwindow = 0;
369 n = _getprop(c->window, wm_colormaps, XA_WINDOW, 100L, (void*)&cw);
370 if (c->ncmapwins != 0) {
371 XFree((char *)c->cmapwins);
372 free((char *)c->wmcmaps);
382 c->wmcmaps = (Colormap*)malloc(n*sizeof(Colormap));
383 for (i = 0; i < n; i++) {
384 if (cw[i] == c->window)
385 c->wmcmaps[i] = c->cmap;
387 /* flush any errors (e.g., caused by mozilla tabs) */
388 ignore_badwindow = 1;
389 XSelectInput(dpy, cw[i], ColormapChangeMask);
390 XGetWindowAttributes(dpy, cw[i], &attr);
391 c->wmcmaps[i] = attr.colormap;
393 ignore_badwindow = 0;
403 if (c->iconname != 0)
405 else if (c->name != 0)
407 else if (c->instance != 0)
409 else if (c->class != 0)
413 if ((p = index(label, ':')) != 0)
425 /* don't try to add a border if the window is non-rectangular */
426 rect = XShapeGetRectangles(dpy, c->window, ShapeBounding, &n, &order);
428 XShapeCombineShape(dpy, c->parent, ShapeBounding, BORDER, BORDER,
429 c->window, ShapeBounding, ShapeSet);
435 _getprop(Window w, Atom a, Atom type, long len, unsigned char **p)
439 unsigned long n, extra;
442 status = XGetWindowProperty(dpy, w, a, 0L, len, False, type, &real_type, &format, &n, &extra, p);
443 if (status != Success || *p == 0)
447 /* could check real_type, format, extra here... */
452 getprop(Window w, Atom a)
456 if (_getprop(w, a, XA_STRING, 100L, &p) <= 0)
462 get1prop(Window w, Atom a, Atom type)
466 if (_getprop(w, a, type, 1L, (void*)&p) <= 0)
474 getwprop(Window w, Atom a)
476 return get1prop(w, a, XA_WINDOW);
480 getiprop(Window w, Atom a)
482 return get1prop(w, a, XA_INTEGER);
486 setstate(Client *c, int state)
490 data[0] = (long) state;
491 data[1] = (long) None;
494 XChangeProperty(dpy, c->window, wm_state, wm_state, 32,
495 PropModeReplace, (unsigned char *)data, 2);
499 getstate(Window w, int *state)
503 if (_getprop(w, wm_state, wm_state, 2L, (void*)&p) <= 0)
521 if ((n = _getprop(w, wm_protocols, XA_ATOM, 20L, (void*)&p)) <= 0)
524 for (i = 0; i < n; i++)
525 if (p[i] == wm_delete)
527 else if (p[i] == wm_take_focus)
528 c->proto |= Ptakefocus;
529 else if (p[i] == wm_lose_focus)
530 c->proto |= Plosefocus;