aewl

annotate client.c @ 20:4560e0882c1d

made code more readable
author Anselm R. Garbe <garbeam@wmii.de>
date Tue, 11 Jul 2006 22:49:09 +0200
parents b5510d0c6d43
children 3ef108a5ca0a
rev   line source
garbeam@5 1 /*
garbeam@5 2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@5 3 * See LICENSE file for license details.
garbeam@5 4 */
garbeam@5 5
garbeam@10 6 #include <stdlib.h>
garbeam@5 7 #include <string.h>
garbeam@5 8 #include <X11/Xatom.h>
garbeam@5 9
garbeam@5 10 #include "util.h"
garbeam@5 11 #include "wm.h"
garbeam@5 12
garbeam@18 13 #define CLIENT_MASK (StructureNotifyMask | PropertyChangeMask | EnterWindowMask)
garbeam@18 14
garbeam@13 15 void
garbeam@13 16 update_name(Client *c)
garbeam@5 17 {
garbeam@5 18 XTextProperty name;
garbeam@5 19 int n;
garbeam@7 20 char **list = NULL;
garbeam@5 21
garbeam@5 22 name.nitems = 0;
garbeam@5 23 c->name[0] = 0;
garbeam@5 24 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
garbeam@5 25 if(!name.nitems)
garbeam@5 26 XGetWMName(dpy, c->win, &name);
garbeam@5 27 if(!name.nitems)
garbeam@5 28 return;
garbeam@5 29 if(name.encoding == XA_STRING)
garbeam@5 30 strncpy(c->name, (char *)name.value, sizeof(c->name));
garbeam@5 31 else {
garbeam@5 32 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@5 33 && n > 0 && *list)
garbeam@5 34 {
garbeam@5 35 strncpy(c->name, *list, sizeof(c->name));
garbeam@5 36 XFreeStringList(list);
garbeam@5 37 }
garbeam@5 38 }
garbeam@5 39 XFree(name.value);
garbeam@16 40 if(c == stack)
garbeam@16 41 draw_bar();
garbeam@16 42 else
garbeam@16 43 draw_client(c);
garbeam@5 44 }
garbeam@5 45
garbeam@10 46 void
garbeam@20 47 update_size(Client *c)
garbeam@20 48 {
garbeam@20 49 XSizeHints size;
garbeam@20 50 long msize;
garbeam@20 51 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
garbeam@20 52 size.flags = PSize;
garbeam@20 53 c->flags = size.flags;
garbeam@20 54 c->basew = size.base_width;
garbeam@20 55 c->baseh = size.base_height;
garbeam@20 56 c->incw = size.width_inc;
garbeam@20 57 c->inch = size.height_inc;
garbeam@20 58 c->maxw = size.max_width;
garbeam@20 59 c->maxh = size.max_height;
garbeam@20 60 c->minw = size.min_width;
garbeam@20 61 c->minh = size.min_height;
garbeam@20 62 }
garbeam@20 63
garbeam@20 64 void
garbeam@13 65 focus(Client *c)
garbeam@13 66 {
garbeam@13 67 Client **l;
garbeam@13 68 for(l=&stack; *l && *l != c; l=&(*l)->snext);
garbeam@13 69 eassert(*l == c);
garbeam@13 70 *l = c->snext;
garbeam@13 71 c->snext = stack;
garbeam@13 72 stack = c;
garbeam@13 73 XRaiseWindow(dpy, c->win);
garbeam@13 74 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
garbeam@13 75 XFlush(dpy);
garbeam@13 76 }
garbeam@13 77
garbeam@13 78 void
garbeam@10 79 manage(Window w, XWindowAttributes *wa)
garbeam@5 80 {
garbeam@10 81 Client *c, **l;
garbeam@5 82 XSetWindowAttributes twa;
garbeam@5 83
garbeam@5 84 c = emallocz(sizeof(Client));
garbeam@5 85 c->win = w;
garbeam@20 86 c->x = wa->x;
garbeam@20 87 c->y = wa->y;
garbeam@20 88 c->w = wa->width;
garbeam@20 89 c->h = wa->height;
garbeam@20 90 update_size(c);
garbeam@19 91 XSetWindowBorderWidth(dpy, c->win, 1);
garbeam@18 92 XSelectInput(dpy, c->win, CLIENT_MASK);
garbeam@5 93 XGetTransientForHint(dpy, c->win, &c->trans);
garbeam@13 94 update_name(c);
garbeam@5 95 twa.override_redirect = 1;
garbeam@5 96 twa.background_pixmap = ParentRelative;
garbeam@5 97 twa.event_mask = ExposureMask;
garbeam@5 98
garbeam@20 99 c->title = XCreateWindow(dpy, root, c->x, c->y, c->w, barrect.height,
garbeam@20 100 0, DefaultDepth(dpy, screen), CopyFromParent,
garbeam@5 101 DefaultVisual(dpy, screen),
garbeam@5 102 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
garbeam@5 103
garbeam@10 104 for(l=&clients; *l; l=&(*l)->next);
garbeam@10 105 c->next = *l; /* *l == nil */
garbeam@10 106 *l = c;
garbeam@13 107 c->snext = stack;
garbeam@13 108 stack = c;
garbeam@13 109 XMapWindow(dpy, c->win);
garbeam@19 110 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@19 111 GrabModeAsync, GrabModeSync, None, None);
garbeam@19 112 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@19 113 GrabModeAsync, GrabModeSync, None, None);
garbeam@19 114 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@18 115 GrabModeAsync, GrabModeSync, None, None);
garbeam@13 116 focus(c);
garbeam@5 117 }
garbeam@9 118
garbeam@18 119 void
garbeam@18 120 resize(Client *c)
garbeam@18 121 {
garbeam@18 122 XConfigureEvent e;
garbeam@18 123
garbeam@20 124 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
garbeam@18 125 e.type = ConfigureNotify;
garbeam@18 126 e.event = c->win;
garbeam@18 127 e.window = c->win;
garbeam@20 128 e.x = c->x;
garbeam@20 129 e.y = c->y;
garbeam@20 130 e.width = c->w;
garbeam@20 131 e.height = c->h;
garbeam@19 132 e.border_width = 0;
garbeam@18 133 e.above = None;
garbeam@18 134 e.override_redirect = False;
garbeam@18 135 XSelectInput(dpy, c->win, CLIENT_MASK & ~StructureNotifyMask);
garbeam@18 136 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
garbeam@18 137 XSelectInput(dpy, c->win, CLIENT_MASK);
garbeam@18 138 XFlush(dpy);
garbeam@18 139 }
garbeam@18 140
garbeam@10 141 static int
garbeam@10 142 dummy_error_handler(Display *dpy, XErrorEvent *error)
garbeam@10 143 {
garbeam@10 144 return 0;
garbeam@10 145 }
garbeam@10 146
garbeam@10 147 void
garbeam@10 148 unmanage(Client *c)
garbeam@10 149 {
garbeam@10 150 Client **l;
garbeam@10 151
garbeam@10 152 XGrabServer(dpy);
garbeam@10 153 XSetErrorHandler(dummy_error_handler);
garbeam@10 154
garbeam@18 155 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
garbeam@10 156 XUnmapWindow(dpy, c->win);
garbeam@10 157 XDestroyWindow(dpy, c->title);
garbeam@10 158
garbeam@10 159 for(l=&clients; *l && *l != c; l=&(*l)->next);
garbeam@10 160 eassert(*l == c);
garbeam@10 161 *l = c->next;
garbeam@13 162 for(l=&stack; *l && *l != c; l=&(*l)->snext);
garbeam@13 163 eassert(*l == c);
garbeam@13 164 *l = c->snext;
garbeam@10 165 free(c);
garbeam@10 166
garbeam@10 167 XFlush(dpy);
garbeam@10 168 XSetErrorHandler(error_handler);
garbeam@10 169 XUngrabServer(dpy);
garbeam@18 170 discard_events(EnterWindowMask);
garbeam@14 171 if(stack)
garbeam@14 172 focus(stack);
garbeam@10 173 }
garbeam@10 174
garbeam@10 175
garbeam@9 176 Client *
garbeam@9 177 getclient(Window w)
garbeam@9 178 {
garbeam@9 179 Client *c;
garbeam@9 180 for(c = clients; c; c = c->next)
garbeam@9 181 if(c->win == w)
garbeam@9 182 return c;
garbeam@9 183 return NULL;
garbeam@9 184 }
garbeam@13 185
garbeam@14 186 void
garbeam@14 187 draw_client(Client *c)
garbeam@14 188 {
garbeam@14 189
garbeam@14 190
garbeam@14 191
garbeam@14 192 }