dwm-meillo

annotate client.c @ 26:e8f627998d6f

simplified several portions of code through replacing rect structs with x,y,h,w counterparts (much more readable)
author Anselm R. Garbe <garbeam@wmii.de>
date Wed, 12 Jul 2006 15:17:22 +0200
parents 95ffdfd0a819
children f96fb3fd8203
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@26 13 static void
garbeam@26 14 resize_title(Client *c)
garbeam@26 15 {
garbeam@26 16 c->tw = textw(&brush.font, c->name) + bh;
garbeam@26 17 if(c->tw > c->w)
garbeam@26 18 c->tw = c->w + 2;
garbeam@26 19 c->tx = c->x + c->w - c->tw + 2;
garbeam@26 20 c->ty = c->y;
garbeam@26 21 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
garbeam@26 22 }
garbeam@18 23
garbeam@13 24 void
garbeam@13 25 update_name(Client *c)
garbeam@5 26 {
garbeam@5 27 XTextProperty name;
garbeam@5 28 int n;
garbeam@7 29 char **list = NULL;
garbeam@5 30
garbeam@5 31 name.nitems = 0;
garbeam@5 32 c->name[0] = 0;
garbeam@5 33 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
garbeam@5 34 if(!name.nitems)
garbeam@5 35 XGetWMName(dpy, c->win, &name);
garbeam@5 36 if(!name.nitems)
garbeam@5 37 return;
garbeam@5 38 if(name.encoding == XA_STRING)
garbeam@5 39 strncpy(c->name, (char *)name.value, sizeof(c->name));
garbeam@5 40 else {
garbeam@5 41 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@5 42 && n > 0 && *list)
garbeam@5 43 {
garbeam@5 44 strncpy(c->name, *list, sizeof(c->name));
garbeam@5 45 XFreeStringList(list);
garbeam@5 46 }
garbeam@5 47 }
garbeam@5 48 XFree(name.value);
garbeam@26 49 resize_title(c);
garbeam@5 50 }
garbeam@5 51
garbeam@10 52 void
garbeam@20 53 update_size(Client *c)
garbeam@20 54 {
garbeam@20 55 XSizeHints size;
garbeam@20 56 long msize;
garbeam@20 57 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
garbeam@20 58 size.flags = PSize;
garbeam@20 59 c->flags = size.flags;
garbeam@21 60 if(c->flags & PBaseSize) {
garbeam@21 61 c->basew = size.base_width;
garbeam@21 62 c->baseh = size.base_height;
garbeam@21 63 }
garbeam@21 64 else
garbeam@21 65 c->basew = c->baseh = 0;
garbeam@21 66 if(c->flags & PResizeInc) {
garbeam@21 67 c->incw = size.width_inc;
garbeam@21 68 c->inch = size.height_inc;
garbeam@21 69 }
garbeam@21 70 else
garbeam@21 71 c->incw = c->inch = 0;
garbeam@21 72 if(c->flags & PMaxSize) {
garbeam@21 73 c->maxw = size.max_width;
garbeam@21 74 c->maxh = size.max_height;
garbeam@21 75 }
garbeam@21 76 else
garbeam@21 77 c->maxw = c->maxh = 0;
garbeam@21 78 if(c->flags & PMinSize) {
garbeam@21 79 c->minw = size.min_width;
garbeam@21 80 c->minh = size.min_height;
garbeam@21 81 }
garbeam@21 82 else
garbeam@21 83 c->minw = c->minh = 0;
garbeam@20 84 }
garbeam@20 85
garbeam@20 86 void
garbeam@26 87 raise(Client *c)
garbeam@26 88 {
garbeam@26 89 XRaiseWindow(dpy, c->win);
garbeam@26 90 XRaiseWindow(dpy, c->title);
garbeam@26 91 }
garbeam@26 92
garbeam@26 93 void
garbeam@26 94 lower(Client *c)
garbeam@26 95 {
garbeam@26 96 XLowerWindow(dpy, c->title);
garbeam@26 97 XLowerWindow(dpy, c->win);
garbeam@26 98 }
garbeam@26 99
garbeam@26 100 void
garbeam@13 101 focus(Client *c)
garbeam@13 102 {
garbeam@21 103 Client **l, *old;
garbeam@21 104
garbeam@21 105 old = stack;
garbeam@26 106 for(l = &stack; *l && *l != c; l = &(*l)->snext);
garbeam@13 107 eassert(*l == c);
garbeam@13 108 *l = c->snext;
garbeam@13 109 c->snext = stack;
garbeam@13 110 stack = c;
garbeam@21 111 if(old && old != c) {
garbeam@21 112 XMapWindow(dpy, old->title);
garbeam@21 113 draw_client(old);
garbeam@21 114 }
garbeam@21 115 XUnmapWindow(dpy, c->title);
garbeam@26 116 draw_client(old);
garbeam@26 117 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
garbeam@13 118 XFlush(dpy);
garbeam@13 119 }
garbeam@13 120
garbeam@13 121 void
garbeam@10 122 manage(Window w, XWindowAttributes *wa)
garbeam@5 123 {
garbeam@10 124 Client *c, **l;
garbeam@5 125 XSetWindowAttributes twa;
garbeam@5 126
garbeam@5 127 c = emallocz(sizeof(Client));
garbeam@5 128 c->win = w;
garbeam@22 129 c->tx = c->x = wa->x;
garbeam@22 130 c->ty = c->y = wa->y;
garbeam@26 131 if(c->y < bh)
garbeam@26 132 c->ty = c->y += bh;
garbeam@22 133 c->tw = c->w = wa->width;
garbeam@20 134 c->h = wa->height;
garbeam@26 135 c->th = bh;
garbeam@20 136 update_size(c);
garbeam@19 137 XSetWindowBorderWidth(dpy, c->win, 1);
garbeam@22 138 XSetWindowBorder(dpy, c->win, brush.border);
garbeam@26 139 XSelectInput(dpy, c->win,
garbeam@26 140 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
garbeam@5 141 XGetTransientForHint(dpy, c->win, &c->trans);
garbeam@5 142 twa.override_redirect = 1;
garbeam@5 143 twa.background_pixmap = ParentRelative;
garbeam@23 144 twa.event_mask = ExposureMask;
garbeam@5 145
garbeam@22 146 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
garbeam@20 147 0, DefaultDepth(dpy, screen), CopyFromParent,
garbeam@5 148 DefaultVisual(dpy, screen),
garbeam@5 149 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
garbeam@21 150 update_name(c);
garbeam@5 151
garbeam@10 152 for(l=&clients; *l; l=&(*l)->next);
garbeam@10 153 c->next = *l; /* *l == nil */
garbeam@10 154 *l = c;
garbeam@13 155 c->snext = stack;
garbeam@13 156 stack = c;
garbeam@26 157 XMapRaised(dpy, c->win);
garbeam@26 158 XMapRaised(dpy, c->title);
garbeam@19 159 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@19 160 GrabModeAsync, GrabModeSync, None, None);
garbeam@19 161 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@19 162 GrabModeAsync, GrabModeSync, None, None);
garbeam@19 163 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@18 164 GrabModeAsync, GrabModeSync, None, None);
garbeam@21 165 resize(c);
garbeam@13 166 focus(c);
garbeam@5 167 }
garbeam@9 168
garbeam@18 169 void
garbeam@18 170 resize(Client *c)
garbeam@18 171 {
garbeam@18 172 XConfigureEvent e;
garbeam@18 173
garbeam@26 174 resize_title(c);
garbeam@20 175 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
garbeam@18 176 e.type = ConfigureNotify;
garbeam@18 177 e.event = c->win;
garbeam@18 178 e.window = c->win;
garbeam@20 179 e.x = c->x;
garbeam@20 180 e.y = c->y;
garbeam@20 181 e.width = c->w;
garbeam@20 182 e.height = c->h;
garbeam@19 183 e.border_width = 0;
garbeam@18 184 e.above = None;
garbeam@18 185 e.override_redirect = False;
garbeam@18 186 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
garbeam@18 187 XFlush(dpy);
garbeam@18 188 }
garbeam@18 189
garbeam@10 190 static int
garbeam@10 191 dummy_error_handler(Display *dpy, XErrorEvent *error)
garbeam@10 192 {
garbeam@10 193 return 0;
garbeam@10 194 }
garbeam@10 195
garbeam@10 196 void
garbeam@10 197 unmanage(Client *c)
garbeam@10 198 {
garbeam@10 199 Client **l;
garbeam@10 200
garbeam@10 201 XGrabServer(dpy);
garbeam@10 202 XSetErrorHandler(dummy_error_handler);
garbeam@10 203
garbeam@18 204 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
garbeam@10 205 XDestroyWindow(dpy, c->title);
garbeam@10 206
garbeam@10 207 for(l=&clients; *l && *l != c; l=&(*l)->next);
garbeam@10 208 eassert(*l == c);
garbeam@10 209 *l = c->next;
garbeam@13 210 for(l=&stack; *l && *l != c; l=&(*l)->snext);
garbeam@13 211 eassert(*l == c);
garbeam@13 212 *l = c->snext;
garbeam@10 213 free(c);
garbeam@10 214
garbeam@10 215 XFlush(dpy);
garbeam@10 216 XSetErrorHandler(error_handler);
garbeam@10 217 XUngrabServer(dpy);
garbeam@14 218 if(stack)
garbeam@14 219 focus(stack);
garbeam@10 220 }
garbeam@10 221
garbeam@23 222 Client *
garbeam@23 223 gettitle(Window w)
garbeam@23 224 {
garbeam@23 225 Client *c;
garbeam@23 226 for(c = clients; c; c = c->next)
garbeam@23 227 if(c->title == w)
garbeam@23 228 return c;
garbeam@23 229 return NULL;
garbeam@23 230 }
garbeam@10 231
garbeam@9 232 Client *
garbeam@9 233 getclient(Window w)
garbeam@9 234 {
garbeam@9 235 Client *c;
garbeam@9 236 for(c = clients; c; c = c->next)
garbeam@9 237 if(c->win == w)
garbeam@9 238 return c;
garbeam@9 239 return NULL;
garbeam@9 240 }
garbeam@13 241
garbeam@14 242 void
garbeam@14 243 draw_client(Client *c)
garbeam@14 244 {
garbeam@26 245 if(c == stack) {
garbeam@21 246 draw_bar();
garbeam@26 247 return;
garbeam@26 248 }
garbeam@14 249
garbeam@26 250 brush.x = brush.y = 0;
garbeam@26 251 brush.w = c->tw;
garbeam@26 252 brush.h = c->th;
garbeam@14 253
garbeam@21 254 draw(dpy, &brush, True, c->name);
garbeam@22 255 XCopyArea(dpy, brush.drawable, c->title, brush.gc,
garbeam@22 256 0, 0, c->tw, c->th, 0, 0);
garbeam@21 257 XFlush(dpy);
garbeam@14 258 }