aewl

annotate client.c @ 28:3dceb132082d

added max
author Anselm R. Garbe <garbeam@wmii.de>
date Wed, 12 Jul 2006 16:40:37 +0200
parents f96fb3fd8203
children 8ad86d0a6a53
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@27 6 #include <math.h>
garbeam@10 7 #include <stdlib.h>
garbeam@5 8 #include <string.h>
garbeam@5 9 #include <X11/Xatom.h>
garbeam@5 10
garbeam@5 11 #include "util.h"
garbeam@5 12 #include "wm.h"
garbeam@5 13
garbeam@27 14 void
garbeam@28 15 max(void *aux)
garbeam@28 16 {
garbeam@28 17 if(!stack)
garbeam@28 18 return;
garbeam@28 19 stack->x = sx;
garbeam@28 20 stack->y = bh;
garbeam@28 21 stack->w = sw - 2;
garbeam@28 22 stack->h = sh - bh - 2;
garbeam@28 23 resize(stack);
garbeam@28 24 }
garbeam@28 25
garbeam@28 26 void
garbeam@27 27 arrange(void *aux)
garbeam@27 28 {
garbeam@27 29 Client *c;
garbeam@27 30 int n, cols, rows, gw, gh, i, j;
garbeam@27 31 float rt, fd;
garbeam@27 32
garbeam@27 33 if(!clients)
garbeam@27 34 return;
garbeam@27 35 for(n = 0, c = clients; c; c = c->next, n++);
garbeam@27 36 rt = sqrt(n);
garbeam@27 37 if(modff(rt, &fd) < 0.5)
garbeam@27 38 rows = floor(rt);
garbeam@27 39 else
garbeam@27 40 rows = ceil(rt);
garbeam@27 41 if(rows * rows < n)
garbeam@27 42 cols = rows + 1;
garbeam@27 43 else
garbeam@27 44 cols = rows;
garbeam@27 45
garbeam@27 46 gw = (sw - 1) / cols;
garbeam@27 47 gh = (sh - bh - 1) / rows;
garbeam@27 48
garbeam@27 49 for(i = j = 0, c = clients; c; c = c->next) {
garbeam@27 50 c->x = i * gw;
garbeam@27 51 c->y = j * gh + bh;
garbeam@27 52 c->w = gw;
garbeam@27 53 c->h = gh;
garbeam@27 54 resize(c);
garbeam@27 55 if(++i == cols) {
garbeam@27 56 j++;
garbeam@27 57 i = 0;
garbeam@27 58 }
garbeam@27 59 }
garbeam@27 60 }
garbeam@27 61
garbeam@27 62 void
garbeam@27 63 sel(void *aux)
garbeam@27 64 {
garbeam@27 65 const char *arg = aux;
garbeam@27 66 Client *c = NULL;
garbeam@27 67
garbeam@27 68 if(!arg || !stack)
garbeam@27 69 return;
garbeam@27 70 if(!strncmp(arg, "next", 5))
garbeam@27 71 c = stack->snext ? stack->snext : stack;
garbeam@27 72 else if(!strncmp(arg, "prev", 5))
garbeam@27 73 for(c = stack; c && c->snext; c = c->snext);
garbeam@27 74 if(!c)
garbeam@27 75 c = stack;
garbeam@27 76 raise(c);
garbeam@27 77 focus(c);
garbeam@27 78 }
garbeam@27 79
garbeam@27 80 void
garbeam@27 81 kill(void *aux)
garbeam@27 82 {
garbeam@27 83 Client *c = stack;
garbeam@27 84
garbeam@27 85 if(!c)
garbeam@27 86 return;
garbeam@27 87 if(c->proto & WM_PROTOCOL_DELWIN)
garbeam@27 88 send_message(c->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
garbeam@27 89 else
garbeam@27 90 XKillClient(dpy, c->win);
garbeam@27 91 }
garbeam@27 92
garbeam@26 93 static void
garbeam@26 94 resize_title(Client *c)
garbeam@26 95 {
garbeam@26 96 c->tw = textw(&brush.font, c->name) + bh;
garbeam@26 97 if(c->tw > c->w)
garbeam@26 98 c->tw = c->w + 2;
garbeam@26 99 c->tx = c->x + c->w - c->tw + 2;
garbeam@26 100 c->ty = c->y;
garbeam@26 101 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
garbeam@26 102 }
garbeam@18 103
garbeam@13 104 void
garbeam@13 105 update_name(Client *c)
garbeam@5 106 {
garbeam@5 107 XTextProperty name;
garbeam@5 108 int n;
garbeam@7 109 char **list = NULL;
garbeam@5 110
garbeam@5 111 name.nitems = 0;
garbeam@5 112 c->name[0] = 0;
garbeam@5 113 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
garbeam@5 114 if(!name.nitems)
garbeam@5 115 XGetWMName(dpy, c->win, &name);
garbeam@5 116 if(!name.nitems)
garbeam@5 117 return;
garbeam@5 118 if(name.encoding == XA_STRING)
garbeam@5 119 strncpy(c->name, (char *)name.value, sizeof(c->name));
garbeam@5 120 else {
garbeam@5 121 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@5 122 && n > 0 && *list)
garbeam@5 123 {
garbeam@5 124 strncpy(c->name, *list, sizeof(c->name));
garbeam@5 125 XFreeStringList(list);
garbeam@5 126 }
garbeam@5 127 }
garbeam@5 128 XFree(name.value);
garbeam@26 129 resize_title(c);
garbeam@5 130 }
garbeam@5 131
garbeam@10 132 void
garbeam@20 133 update_size(Client *c)
garbeam@20 134 {
garbeam@20 135 XSizeHints size;
garbeam@20 136 long msize;
garbeam@20 137 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
garbeam@20 138 size.flags = PSize;
garbeam@20 139 c->flags = size.flags;
garbeam@21 140 if(c->flags & PBaseSize) {
garbeam@21 141 c->basew = size.base_width;
garbeam@21 142 c->baseh = size.base_height;
garbeam@21 143 }
garbeam@21 144 else
garbeam@21 145 c->basew = c->baseh = 0;
garbeam@21 146 if(c->flags & PResizeInc) {
garbeam@21 147 c->incw = size.width_inc;
garbeam@21 148 c->inch = size.height_inc;
garbeam@21 149 }
garbeam@21 150 else
garbeam@21 151 c->incw = c->inch = 0;
garbeam@21 152 if(c->flags & PMaxSize) {
garbeam@21 153 c->maxw = size.max_width;
garbeam@21 154 c->maxh = size.max_height;
garbeam@21 155 }
garbeam@21 156 else
garbeam@21 157 c->maxw = c->maxh = 0;
garbeam@21 158 if(c->flags & PMinSize) {
garbeam@21 159 c->minw = size.min_width;
garbeam@21 160 c->minh = size.min_height;
garbeam@21 161 }
garbeam@21 162 else
garbeam@21 163 c->minw = c->minh = 0;
garbeam@20 164 }
garbeam@20 165
garbeam@20 166 void
garbeam@26 167 raise(Client *c)
garbeam@26 168 {
garbeam@26 169 XRaiseWindow(dpy, c->win);
garbeam@26 170 XRaiseWindow(dpy, c->title);
garbeam@26 171 }
garbeam@26 172
garbeam@26 173 void
garbeam@26 174 lower(Client *c)
garbeam@26 175 {
garbeam@26 176 XLowerWindow(dpy, c->title);
garbeam@26 177 XLowerWindow(dpy, c->win);
garbeam@26 178 }
garbeam@26 179
garbeam@26 180 void
garbeam@13 181 focus(Client *c)
garbeam@13 182 {
garbeam@21 183 Client **l, *old;
garbeam@21 184
garbeam@21 185 old = stack;
garbeam@26 186 for(l = &stack; *l && *l != c; l = &(*l)->snext);
garbeam@13 187 eassert(*l == c);
garbeam@13 188 *l = c->snext;
garbeam@13 189 c->snext = stack;
garbeam@13 190 stack = c;
garbeam@21 191 if(old && old != c) {
garbeam@21 192 XMapWindow(dpy, old->title);
garbeam@21 193 draw_client(old);
garbeam@21 194 }
garbeam@21 195 XUnmapWindow(dpy, c->title);
garbeam@27 196 draw_client(c);
garbeam@26 197 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
garbeam@13 198 XFlush(dpy);
garbeam@13 199 }
garbeam@13 200
garbeam@13 201 void
garbeam@10 202 manage(Window w, XWindowAttributes *wa)
garbeam@5 203 {
garbeam@10 204 Client *c, **l;
garbeam@5 205 XSetWindowAttributes twa;
garbeam@5 206
garbeam@5 207 c = emallocz(sizeof(Client));
garbeam@5 208 c->win = w;
garbeam@22 209 c->tx = c->x = wa->x;
garbeam@22 210 c->ty = c->y = wa->y;
garbeam@26 211 if(c->y < bh)
garbeam@26 212 c->ty = c->y += bh;
garbeam@22 213 c->tw = c->w = wa->width;
garbeam@20 214 c->h = wa->height;
garbeam@26 215 c->th = bh;
garbeam@20 216 update_size(c);
garbeam@19 217 XSetWindowBorderWidth(dpy, c->win, 1);
garbeam@22 218 XSetWindowBorder(dpy, c->win, brush.border);
garbeam@26 219 XSelectInput(dpy, c->win,
garbeam@26 220 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
garbeam@5 221 XGetTransientForHint(dpy, c->win, &c->trans);
garbeam@5 222 twa.override_redirect = 1;
garbeam@5 223 twa.background_pixmap = ParentRelative;
garbeam@23 224 twa.event_mask = ExposureMask;
garbeam@5 225
garbeam@22 226 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
garbeam@20 227 0, DefaultDepth(dpy, screen), CopyFromParent,
garbeam@5 228 DefaultVisual(dpy, screen),
garbeam@5 229 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
garbeam@21 230 update_name(c);
garbeam@5 231
garbeam@10 232 for(l=&clients; *l; l=&(*l)->next);
garbeam@10 233 c->next = *l; /* *l == nil */
garbeam@10 234 *l = c;
garbeam@13 235 c->snext = stack;
garbeam@13 236 stack = c;
garbeam@26 237 XMapRaised(dpy, c->win);
garbeam@26 238 XMapRaised(dpy, c->title);
garbeam@19 239 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@19 240 GrabModeAsync, GrabModeSync, None, None);
garbeam@19 241 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@19 242 GrabModeAsync, GrabModeSync, None, None);
garbeam@19 243 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@18 244 GrabModeAsync, GrabModeSync, None, None);
garbeam@21 245 resize(c);
garbeam@13 246 focus(c);
garbeam@5 247 }
garbeam@9 248
garbeam@18 249 void
garbeam@18 250 resize(Client *c)
garbeam@18 251 {
garbeam@18 252 XConfigureEvent e;
garbeam@18 253
garbeam@26 254 resize_title(c);
garbeam@20 255 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
garbeam@18 256 e.type = ConfigureNotify;
garbeam@18 257 e.event = c->win;
garbeam@18 258 e.window = c->win;
garbeam@20 259 e.x = c->x;
garbeam@20 260 e.y = c->y;
garbeam@20 261 e.width = c->w;
garbeam@20 262 e.height = c->h;
garbeam@19 263 e.border_width = 0;
garbeam@18 264 e.above = None;
garbeam@18 265 e.override_redirect = False;
garbeam@18 266 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
garbeam@18 267 XFlush(dpy);
garbeam@18 268 }
garbeam@18 269
garbeam@10 270 static int
garbeam@10 271 dummy_error_handler(Display *dpy, XErrorEvent *error)
garbeam@10 272 {
garbeam@10 273 return 0;
garbeam@10 274 }
garbeam@10 275
garbeam@10 276 void
garbeam@10 277 unmanage(Client *c)
garbeam@10 278 {
garbeam@10 279 Client **l;
garbeam@10 280
garbeam@10 281 XGrabServer(dpy);
garbeam@10 282 XSetErrorHandler(dummy_error_handler);
garbeam@10 283
garbeam@18 284 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
garbeam@10 285 XDestroyWindow(dpy, c->title);
garbeam@10 286
garbeam@10 287 for(l=&clients; *l && *l != c; l=&(*l)->next);
garbeam@10 288 eassert(*l == c);
garbeam@10 289 *l = c->next;
garbeam@13 290 for(l=&stack; *l && *l != c; l=&(*l)->snext);
garbeam@13 291 eassert(*l == c);
garbeam@13 292 *l = c->snext;
garbeam@10 293 free(c);
garbeam@10 294
garbeam@10 295 XFlush(dpy);
garbeam@10 296 XSetErrorHandler(error_handler);
garbeam@10 297 XUngrabServer(dpy);
garbeam@14 298 if(stack)
garbeam@14 299 focus(stack);
garbeam@10 300 }
garbeam@10 301
garbeam@23 302 Client *
garbeam@23 303 gettitle(Window w)
garbeam@23 304 {
garbeam@23 305 Client *c;
garbeam@23 306 for(c = clients; c; c = c->next)
garbeam@23 307 if(c->title == w)
garbeam@23 308 return c;
garbeam@23 309 return NULL;
garbeam@23 310 }
garbeam@10 311
garbeam@9 312 Client *
garbeam@9 313 getclient(Window w)
garbeam@9 314 {
garbeam@9 315 Client *c;
garbeam@9 316 for(c = clients; c; c = c->next)
garbeam@9 317 if(c->win == w)
garbeam@9 318 return c;
garbeam@9 319 return NULL;
garbeam@9 320 }
garbeam@13 321
garbeam@14 322 void
garbeam@14 323 draw_client(Client *c)
garbeam@14 324 {
garbeam@26 325 if(c == stack) {
garbeam@21 326 draw_bar();
garbeam@26 327 return;
garbeam@26 328 }
garbeam@14 329
garbeam@26 330 brush.x = brush.y = 0;
garbeam@26 331 brush.w = c->tw;
garbeam@26 332 brush.h = c->th;
garbeam@14 333
garbeam@21 334 draw(dpy, &brush, True, c->name);
garbeam@22 335 XCopyArea(dpy, brush.drawable, c->title, brush.gc,
garbeam@22 336 0, 0, c->tw, c->th, 0, 0);
garbeam@21 337 XFlush(dpy);
garbeam@14 338 }