+ XSetForeground(dpy, dc.gc, col[invert ? ColFG : ColBG]);
+ XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
+ if(!text)
+ return;
+ olen = strlen(text);
+ len = MIN(olen, sizeof buf);
+ memcpy(buf, text, len);
+ h = dc.font.ascent + dc.font.descent;
+ y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
+ x = dc.x + (h / 2);
+ /* shorten text if necessary */
+ for(; len && (i = textnw(buf, len)) > dc.w - h; len--);
+ if(!len)
+ return;
+ if(len < olen)
+ for(i = len; i && i > len - 3; buf[--i] = '.');
+ XSetForeground(dpy, dc.gc, col[invert ? ColBG : ColFG]);
+ if(dc.font.set)
+ XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
+ else
+ XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
+}
+
+void
+enternotify(XEvent *e) {
+ Client *c;
+ XCrossingEvent *ev = &e->xcrossing;
+
+ if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
+ return;
+ if((c = getclient(ev->window)))
+ focus(c);
+ else
+ focus(NULL);
+}
+
+void
+expose(XEvent *e) {
+ XExposeEvent *ev = &e->xexpose;
+
+ if(ev->count == 0 && (ev->window == barwin))
+ drawbar();
+}
+
+void
+focus(Client *c) {
+ if(!c || !ISVISIBLE(c))
+ for(c = stack; c && !ISVISIBLE(c); c = c->snext);
+ if(sel && sel != c) {
+ grabbuttons(sel, False);
+ XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
+ }
+ if(c) {
+ detachstack(c);
+ attachstack(c);
+ grabbuttons(c, True);
+ XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
+ XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
+ }
+ else
+ XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
+ sel = c;
+ drawbar();
+}
+
+void
+focusin(XEvent *e) { /* there are some broken focus acquiring clients */
+ XFocusChangeEvent *ev = &e->xfocus;
+
+ if(sel && ev->window != sel->win)
+ XSetInputFocus(dpy, sel->win, RevertToPointerRoot, CurrentTime);
+}
+
+void
+focusstack(const Arg *arg) {
+ Client *c = NULL, *i;
+
+ if(!sel)
+ return;
+ if (arg->i > 0) {
+ for(c = sel->next; c && !ISVISIBLE(c); c = c->next);
+ if(!c)
+ for(c = clients; c && !ISVISIBLE(c); c = c->next);
+ }
+ else {
+ for(i = clients; i != sel; i = i->next)
+ if(ISVISIBLE(i))
+ c = i;
+ if(!c)
+ for(; i; i = i->next)
+ if(ISVISIBLE(i))
+ c = i;
+ }
+ if(c) {
+ focus(c);
+ restack();
+ }
+}
+
+Client *
+getclient(Window w) {
+ Client *c;
+
+ for(c = clients; c && c->win != w; c = c->next);
+ return c;
+}
+
+unsigned long
+getcolor(const char *colstr) {
+ Colormap cmap = DefaultColormap(dpy, screen);
+ XColor color;
+
+ if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
+ die("error, cannot allocate color '%s'\n", colstr);
+ return color.pixel;
+}
+
+long
+getstate(Window w) {
+ int format, status;
+ long result = -1;
+ unsigned char *p = NULL;
+ unsigned long n, extra;
+ Atom real;
+
+ status = XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
+ &real, &format, &n, &extra, (unsigned char **)&p);
+ if(status != Success)
+ return -1;
+ if(n != 0)
+ result = *p;
+ XFree(p);
+ return result;
+}
+
+Bool
+gettextprop(Window w, Atom atom, char *text, unsigned int size) {
+ char **list = NULL;
+ int n;
+ XTextProperty name;
+
+ if(!text || size == 0)
+ return False;
+ text[0] = '\0';
+ XGetTextProperty(dpy, w, &name, atom);
+ if(!name.nitems)
+ return False;
+ if(name.encoding == XA_STRING)
+ strncpy(text, (char *)name.value, size - 1);
+ else {
+ if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
+ && n > 0 && *list) {
+ strncpy(text, *list, size - 1);
+ XFreeStringList(list);
+ }
+ }
+ text[size - 1] = '\0';
+ XFree(name.value);
+ return True;