garbeam@5: /* garbeam@5: * (C)opyright MMVI Anselm R. Garbe garbeam@5: * See LICENSE file for license details. garbeam@5: */ garbeam@5: garbeam@10: #include garbeam@5: #include garbeam@5: #include garbeam@5: garbeam@5: #include "util.h" garbeam@5: #include "wm.h" garbeam@5: garbeam@18: #define CLIENT_MASK (StructureNotifyMask | PropertyChangeMask | EnterWindowMask) garbeam@18: garbeam@13: void garbeam@13: update_name(Client *c) garbeam@5: { garbeam@5: XTextProperty name; garbeam@5: int n; garbeam@7: char **list = NULL; garbeam@5: garbeam@5: name.nitems = 0; garbeam@5: c->name[0] = 0; garbeam@5: XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]); garbeam@5: if(!name.nitems) garbeam@5: XGetWMName(dpy, c->win, &name); garbeam@5: if(!name.nitems) garbeam@5: return; garbeam@5: if(name.encoding == XA_STRING) garbeam@5: strncpy(c->name, (char *)name.value, sizeof(c->name)); garbeam@5: else { garbeam@5: if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success garbeam@5: && n > 0 && *list) garbeam@5: { garbeam@5: strncpy(c->name, *list, sizeof(c->name)); garbeam@5: XFreeStringList(list); garbeam@5: } garbeam@5: } garbeam@5: XFree(name.value); garbeam@5: } garbeam@5: garbeam@10: void garbeam@20: update_size(Client *c) garbeam@20: { garbeam@20: XSizeHints size; garbeam@20: long msize; garbeam@20: if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags) garbeam@20: size.flags = PSize; garbeam@20: c->flags = size.flags; garbeam@21: if(c->flags & PBaseSize) { garbeam@21: c->basew = size.base_width; garbeam@21: c->baseh = size.base_height; garbeam@21: } garbeam@21: else garbeam@21: c->basew = c->baseh = 0; garbeam@21: if(c->flags & PResizeInc) { garbeam@21: c->incw = size.width_inc; garbeam@21: c->inch = size.height_inc; garbeam@21: } garbeam@21: else garbeam@21: c->incw = c->inch = 0; garbeam@21: if(c->flags & PMaxSize) { garbeam@21: c->maxw = size.max_width; garbeam@21: c->maxh = size.max_height; garbeam@21: } garbeam@21: else garbeam@21: c->maxw = c->maxh = 0; garbeam@21: if(c->flags & PMinSize) { garbeam@21: c->minw = size.min_width; garbeam@21: c->minh = size.min_height; garbeam@21: } garbeam@21: else garbeam@21: c->minw = c->minh = 0; garbeam@20: } garbeam@20: garbeam@20: void garbeam@13: focus(Client *c) garbeam@13: { garbeam@21: Client **l, *old; garbeam@21: garbeam@21: old = stack; garbeam@13: for(l=&stack; *l && *l != c; l=&(*l)->snext); garbeam@13: eassert(*l == c); garbeam@13: *l = c->snext; garbeam@13: c->snext = stack; garbeam@13: stack = c; garbeam@13: XRaiseWindow(dpy, c->win); garbeam@21: XRaiseWindow(dpy, c->title); garbeam@13: XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); garbeam@21: if(old && old != c) { garbeam@21: XMapWindow(dpy, old->title); garbeam@21: draw_client(old); garbeam@21: } garbeam@21: XUnmapWindow(dpy, c->title); garbeam@21: draw_bar(); garbeam@13: XFlush(dpy); garbeam@13: } garbeam@13: garbeam@13: void garbeam@10: manage(Window w, XWindowAttributes *wa) garbeam@5: { garbeam@10: Client *c, **l; garbeam@5: XSetWindowAttributes twa; garbeam@5: garbeam@5: c = emallocz(sizeof(Client)); garbeam@5: c->win = w; garbeam@20: c->x = wa->x; garbeam@20: c->y = wa->y; garbeam@20: c->w = wa->width; garbeam@20: c->h = wa->height; garbeam@20: update_size(c); garbeam@19: XSetWindowBorderWidth(dpy, c->win, 1); garbeam@18: XSelectInput(dpy, c->win, CLIENT_MASK); garbeam@5: XGetTransientForHint(dpy, c->win, &c->trans); garbeam@5: twa.override_redirect = 1; garbeam@5: twa.background_pixmap = ParentRelative; garbeam@5: twa.event_mask = ExposureMask; garbeam@5: garbeam@20: c->title = XCreateWindow(dpy, root, c->x, c->y, c->w, barrect.height, garbeam@20: 0, DefaultDepth(dpy, screen), CopyFromParent, garbeam@5: DefaultVisual(dpy, screen), garbeam@5: CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa); garbeam@21: update_name(c); garbeam@5: garbeam@10: for(l=&clients; *l; l=&(*l)->next); garbeam@10: c->next = *l; /* *l == nil */ garbeam@10: *l = c; garbeam@13: c->snext = stack; garbeam@13: stack = c; garbeam@13: XMapWindow(dpy, c->win); garbeam@21: XMapWindow(dpy, c->title); garbeam@19: XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask, garbeam@19: GrabModeAsync, GrabModeSync, None, None); garbeam@19: XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask, garbeam@19: GrabModeAsync, GrabModeSync, None, None); garbeam@19: XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask, garbeam@18: GrabModeAsync, GrabModeSync, None, None); garbeam@21: resize(c); garbeam@13: focus(c); garbeam@5: } garbeam@9: garbeam@18: void garbeam@18: resize(Client *c) garbeam@18: { garbeam@18: XConfigureEvent e; garbeam@18: garbeam@20: XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); garbeam@21: XMoveResizeWindow(dpy, c->title, c->x + c->w / 3, c->y, 2 * c->w / 3, barrect.height); garbeam@18: e.type = ConfigureNotify; garbeam@18: e.event = c->win; garbeam@18: e.window = c->win; garbeam@20: e.x = c->x; garbeam@20: e.y = c->y; garbeam@20: e.width = c->w; garbeam@20: e.height = c->h; garbeam@19: e.border_width = 0; garbeam@18: e.above = None; garbeam@18: e.override_redirect = False; garbeam@18: XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask); garbeam@18: XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e); garbeam@18: XSelectInput(dpy, c->win, CLIENT_MASK); garbeam@18: XFlush(dpy); garbeam@18: } garbeam@18: garbeam@10: static int garbeam@10: dummy_error_handler(Display *dpy, XErrorEvent *error) garbeam@10: { garbeam@10: return 0; garbeam@10: } garbeam@10: garbeam@10: void garbeam@10: unmanage(Client *c) garbeam@10: { garbeam@10: Client **l; garbeam@10: garbeam@10: XGrabServer(dpy); garbeam@10: XSetErrorHandler(dummy_error_handler); garbeam@10: garbeam@18: XUngrabButton(dpy, AnyButton, AnyModifier, c->win); garbeam@10: XUnmapWindow(dpy, c->win); garbeam@10: XDestroyWindow(dpy, c->title); garbeam@10: garbeam@10: for(l=&clients; *l && *l != c; l=&(*l)->next); garbeam@10: eassert(*l == c); garbeam@10: *l = c->next; garbeam@13: for(l=&stack; *l && *l != c; l=&(*l)->snext); garbeam@13: eassert(*l == c); garbeam@13: *l = c->snext; garbeam@10: free(c); garbeam@10: garbeam@10: XFlush(dpy); garbeam@10: XSetErrorHandler(error_handler); garbeam@10: XUngrabServer(dpy); garbeam@18: discard_events(EnterWindowMask); garbeam@14: if(stack) garbeam@14: focus(stack); garbeam@10: } garbeam@10: garbeam@10: garbeam@9: Client * garbeam@9: getclient(Window w) garbeam@9: { garbeam@9: Client *c; garbeam@9: for(c = clients; c; c = c->next) garbeam@9: if(c->win == w) garbeam@9: return c; garbeam@9: return NULL; garbeam@9: } garbeam@13: garbeam@14: void garbeam@14: draw_client(Client *c) garbeam@14: { garbeam@21: if(!c) garbeam@21: return; garbeam@21: if(c == stack) garbeam@21: draw_bar(); garbeam@14: garbeam@21: brush.rect.x = brush.rect.y = 0; garbeam@21: brush.rect.width = 2 * c->w / 3; garbeam@21: brush.rect.height = barrect.height; garbeam@14: garbeam@21: draw(dpy, &brush, True, c->name); garbeam@21: XCopyArea(dpy, brush.drawable, c->title, brush.gc, 0, 0, garbeam@21: brush.rect.width, brush.rect.height, 0, 0); garbeam@21: XFlush(dpy); garbeam@14: }