aewl

annotate client.c @ 751:743061dbe7f2

made Mod-F4 (shutdown) work on notebook
author meillo@marmaro.de
date Sat, 16 Jun 2007 21:44:00 +0200
parents 6283adb1fcf2
children
rev   line source
arg@644 1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
garbeam@5 2 * See LICENSE file for license details.
garbeam@5 3 */
garbeam@76 4 #include "dwm.h"
garbeam@10 5 #include <stdlib.h>
garbeam@5 6 #include <string.h>
garbeam@5 7 #include <X11/Xatom.h>
garbeam@32 8 #include <X11/Xutil.h>
garbeam@5 9
arg@730 10 /* static */
garbeam@50 11
garbeam@26 12 static void
arg@461 13 detachstack(Client *c) {
arg@446 14 Client **tc;
arg@446 15 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
arg@446 16 *tc = c->snext;
arg@446 17 }
arg@446 18
arg@446 19 static void
arg@461 20 grabbuttons(Client *c, Bool focused) {
arg@372 21 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
arg@372 22
arg@452 23 if(focused) {
arg@372 24 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
arg@372 25 GrabModeAsync, GrabModeSync, None, None);
arg@372 26 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
arg@372 27 GrabModeAsync, GrabModeSync, None, None);
arg@372 28 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
arg@372 29 GrabModeAsync, GrabModeSync, None, None);
arg@372 30 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
arg@372 31 GrabModeAsync, GrabModeSync, None, None);
arg@372 32
arg@372 33 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
arg@372 34 GrabModeAsync, GrabModeSync, None, None);
arg@372 35 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
arg@372 36 GrabModeAsync, GrabModeSync, None, None);
arg@372 37 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
arg@372 38 GrabModeAsync, GrabModeSync, None, None);
arg@372 39 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
arg@372 40 GrabModeAsync, GrabModeSync, None, None);
arg@372 41
arg@372 42 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
arg@372 43 GrabModeAsync, GrabModeSync, None, None);
arg@372 44 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
arg@372 45 GrabModeAsync, GrabModeSync, None, None);
arg@372 46 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
arg@372 47 GrabModeAsync, GrabModeSync, None, None);
arg@372 48 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
arg@372 49 GrabModeAsync, GrabModeSync, None, None);
arg@372 50 }
arg@372 51 else
arg@372 52 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
arg@372 53 GrabModeAsync, GrabModeSync, None, None);
arg@318 54 }
arg@318 55
arg@725 56 static void
arg@725 57 setclientstate(Client *c, long state) {
arg@725 58 long data[] = {state, None};
arg@725 59 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
arg@725 60 PropModeReplace, (unsigned char *)data, 2);
arg@725 61 }
arg@725 62
garbeam@76 63 static int
arg@461 64 xerrordummy(Display *dsply, XErrorEvent *ee) {
garbeam@76 65 return 0;
garbeam@5 66 }
garbeam@5 67
arg@730 68 /* extern */
garbeam@20 69
garbeam@20 70 void
arg@491 71 configure(Client *c) {
arg@491 72 XEvent synev;
arg@491 73
arg@491 74 synev.type = ConfigureNotify;
arg@491 75 synev.xconfigure.display = dpy;
arg@491 76 synev.xconfigure.event = c->win;
arg@491 77 synev.xconfigure.window = c->win;
arg@491 78 synev.xconfigure.x = c->x;
arg@491 79 synev.xconfigure.y = c->y;
arg@491 80 synev.xconfigure.width = c->w;
arg@491 81 synev.xconfigure.height = c->h;
arg@491 82 synev.xconfigure.border_width = c->border;
arg@491 83 synev.xconfigure.above = None;
arg@491 84 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@491 85 }
arg@491 86
arg@491 87 void
arg@461 88 focus(Client *c) {
arg@709 89 if(c && !isvisible(c))
arg@239 90 return;
arg@712 91 if(sel && sel != c) {
arg@712 92 grabbuttons(sel, False);
arg@712 93 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
arg@318 94 }
arg@400 95 if(c) {
arg@711 96 detachstack(c);
arg@711 97 c->snext = stack;
arg@711 98 stack = c;
arg@711 99 grabbuttons(c, True);
arg@714 100 }
arg@714 101 sel = c;
arg@714 102 drawstatus();
arg@716 103 if(!selscreen)
arg@714 104 return;
arg@715 105 if(c) {
arg@711 106 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
arg@711 107 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
arg@400 108 }
arg@712 109 else
arg@400 110 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
garbeam@13 111 }
garbeam@13 112
garbeam@76 113 Client *
arg@461 114 getclient(Window w) {
garbeam@76 115 Client *c;
arg@123 116
garbeam@76 117 for(c = clients; c; c = c->next)
garbeam@76 118 if(c->win == w)
garbeam@76 119 return c;
garbeam@76 120 return NULL;
garbeam@76 121 }
garbeam@76 122
arg@734 123 Bool
arg@734 124 isprotodel(Client *c) {
arg@734 125 int i, n;
arg@734 126 Atom *protocols;
arg@734 127 Bool ret = False;
arg@734 128
arg@734 129 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
arg@734 130 for(i = 0; !ret && i < n; i++)
arg@734 131 if(protocols[i] == wmatom[WMDelete])
arg@734 132 ret = True;
arg@734 133 XFree(protocols);
arg@734 134 }
arg@734 135 return ret;
arg@734 136 }
arg@734 137
garbeam@76 138 void
arg@461 139 killclient(Arg *arg) {
garbeam@76 140 if(!sel)
garbeam@76 141 return;
arg@734 142 if(isprotodel(sel))
garbeam@77 143 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
garbeam@76 144 else
garbeam@76 145 XKillClient(dpy, sel->win);
garbeam@76 146 }
garbeam@76 147
garbeam@76 148 void
arg@461 149 manage(Window w, XWindowAttributes *wa) {
arg@431 150 Client *c;
arg@123 151 Window trans;
garbeam@5 152
garbeam@5 153 c = emallocz(sizeof(Client));
arg@178 154 c->tags = emallocz(ntags * sizeof(Bool));
garbeam@5 155 c->win = w;
arg@687 156 c->x = wa->x;
arg@687 157 c->y = wa->y;
arg@687 158 c->w = wa->width;
arg@115 159 c->h = wa->height;
arg@708 160 if(c->w == sw && c->h == sh) {
arg@708 161 c->border = 0;
arg@708 162 c->x = sx;
arg@708 163 c->y = sy;
arg@708 164 }
arg@708 165 else {
arg@708 166 c->border = BORDERPX;
arg@718 167 if(c->x + c->w + 2 * c->border > wax + waw)
arg@718 168 c->x = wax + waw - c->w - 2 * c->border;
arg@718 169 if(c->y + c->h + 2 * c->border > way + wah)
arg@718 170 c->y = way + wah - c->h - 2 * c->border;
arg@708 171 if(c->x < wax)
arg@708 172 c->x = wax;
arg@708 173 if(c->y < way)
arg@708 174 c->y = way;
arg@708 175 }
arg@639 176 updatesizehints(c);
garbeam@26 177 XSelectInput(dpy, c->win,
arg@127 178 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
garbeam@53 179 XGetTransientForHint(dpy, c->win, &trans);
arg@372 180 grabbuttons(c, False);
arg@715 181 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
arg@500 182 updatetitle(c);
arg@431 183 settags(c, getclient(trans));
garbeam@80 184 if(!c->isfloat)
arg@549 185 c->isfloat = trans || c->isfixed;
arg@381 186 if(clients)
arg@381 187 clients->prev = c;
arg@381 188 c->next = clients;
arg@446 189 c->snext = stack;
arg@446 190 stack = clients = c;
arg@687 191 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
arg@270 192 XMapWindow(dpy, c->win);
arg@725 193 setclientstate(c, NormalState);
arg@261 194 if(isvisible(c))
garbeam@51 195 focus(c);
arg@533 196 arrange();
garbeam@94 197 }
garbeam@94 198
garbeam@94 199 void
arg@708 200 resize(Client *c, Bool sizehints) {
arg@733 201 float actual, dx, dy, max, min;
arg@163 202 XWindowChanges wc;
garbeam@18 203
arg@721 204 if(c->w <= 0 || c->h <= 0)
arg@721 205 return;
arg@129 206 if(sizehints) {
arg@129 207 if(c->minw && c->w < c->minw)
arg@129 208 c->w = c->minw;
arg@129 209 if(c->minh && c->h < c->minh)
arg@129 210 c->h = c->minh;
arg@129 211 if(c->maxw && c->w > c->maxw)
arg@129 212 c->w = c->maxw;
arg@129 213 if(c->maxh && c->h > c->maxh)
arg@129 214 c->h = c->maxh;
arg@731 215 /* inspired by algorithm from fluxbox */
arg@731 216 if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
arg@731 217 dx = (float)(c->w - c->basew);
arg@731 218 dy = (float)(c->h - c->baseh);
arg@731 219 min = (float)(c->minax) / (float)(c->minay);
arg@731 220 max = (float)(c->maxax) / (float)(c->maxay);
arg@731 221 actual = dx / dy;
arg@731 222 if(max > 0 && min > 0 && actual > 0) {
arg@731 223 if(actual < min) {
arg@732 224 dy = (dx * min + dy) / (min * min + 1);
arg@732 225 dx = dy * min;
arg@731 226 c->w = (int)dx + c->basew;
arg@731 227 c->h = (int)dy + c->baseh;
arg@731 228 }
arg@731 229 else if(actual > max) {
arg@732 230 dy = (dx * min + dy) / (max * max + 1);
arg@732 231 dx = dy * min;
arg@731 232 c->w = (int)dx + c->basew;
arg@731 233 c->h = (int)dy + c->baseh;
arg@731 234 }
arg@731 235 }
arg@731 236 }
arg@731 237 if(c->incw)
arg@731 238 c->w -= (c->w - c->basew) % c->incw;
arg@731 239 if(c->inch)
arg@731 240 c->h -= (c->h - c->baseh) % c->inch;
garbeam@52 241 }
arg@708 242 if(c->w == sw && c->h == sh)
arg@708 243 c->border = 0;
arg@708 244 else
arg@708 245 c->border = BORDERPX;
arg@465 246 /* offscreen appearance fixes */
arg@718 247 if(c->x > sw)
arg@718 248 c->x = sw - c->w - 2 * c->border;
arg@718 249 if(c->y > sh)
arg@718 250 c->y = sh - c->h - 2 * c->border;
arg@708 251 if(c->x + c->w + 2 * c->border < sx)
arg@517 252 c->x = sx;
arg@708 253 if(c->y + c->h + 2 * c->border < sy)
arg@708 254 c->y = sy;
arg@163 255 wc.x = c->x;
arg@163 256 wc.y = c->y;
arg@163 257 wc.width = c->w;
arg@163 258 wc.height = c->h;
arg@708 259 wc.border_width = c->border;
arg@452 260 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
arg@630 261 configure(c);
garbeam@79 262 XSync(dpy, False);
garbeam@18 263 }
garbeam@18 264
garbeam@76 265 void
arg@639 266 updatesizehints(Client *c) {
arg@123 267 long msize;
garbeam@76 268 XSizeHints size;
arg@123 269
garbeam@76 270 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
garbeam@76 271 size.flags = PSize;
garbeam@76 272 c->flags = size.flags;
garbeam@76 273 if(c->flags & PBaseSize) {
garbeam@76 274 c->basew = size.base_width;
garbeam@76 275 c->baseh = size.base_height;
garbeam@76 276 }
garbeam@76 277 else
garbeam@76 278 c->basew = c->baseh = 0;
garbeam@76 279 if(c->flags & PResizeInc) {
garbeam@76 280 c->incw = size.width_inc;
garbeam@76 281 c->inch = size.height_inc;
garbeam@76 282 }
garbeam@76 283 else
garbeam@76 284 c->incw = c->inch = 0;
garbeam@76 285 if(c->flags & PMaxSize) {
garbeam@76 286 c->maxw = size.max_width;
garbeam@76 287 c->maxh = size.max_height;
garbeam@76 288 }
garbeam@76 289 else
garbeam@76 290 c->maxw = c->maxh = 0;
garbeam@76 291 if(c->flags & PMinSize) {
garbeam@76 292 c->minw = size.min_width;
garbeam@76 293 c->minh = size.min_height;
garbeam@76 294 }
garbeam@76 295 else
garbeam@76 296 c->minw = c->minh = 0;
arg@731 297 if(c->flags & PAspect) {
arg@731 298 c->minax = size.min_aspect.x;
arg@731 299 c->minay = size.min_aspect.y;
arg@731 300 c->maxax = size.max_aspect.x;
arg@731 301 c->maxay = size.max_aspect.y;
arg@731 302 }
arg@731 303 else
arg@731 304 c->minax = c->minay = c->maxax = c->maxay = 0;
arg@550 305 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
arg@550 306 c->maxw == c->minw && c->maxh == c->minh);
garbeam@76 307 }
garbeam@76 308
garbeam@76 309 void
arg@461 310 updatetitle(Client *c) {
arg@123 311 char **list = NULL;
arg@377 312 int n;
garbeam@76 313 XTextProperty name;
garbeam@76 314
garbeam@76 315 name.nitems = 0;
garbeam@76 316 c->name[0] = 0;
garbeam@77 317 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
garbeam@76 318 if(!name.nitems)
garbeam@76 319 XGetWMName(dpy, c->win, &name);
garbeam@76 320 if(!name.nitems)
garbeam@76 321 return;
garbeam@76 322 if(name.encoding == XA_STRING)
arg@581 323 strncpy(c->name, (char *)name.value, sizeof c->name);
garbeam@76 324 else {
garbeam@76 325 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@76 326 && n > 0 && *list)
garbeam@76 327 {
arg@581 328 strncpy(c->name, *list, sizeof c->name);
garbeam@76 329 XFreeStringList(list);
garbeam@76 330 }
garbeam@76 331 }
garbeam@76 332 XFree(name.value);
garbeam@10 333 }
garbeam@10 334
garbeam@10 335 void
arg@461 336 unmanage(Client *c) {
arg@450 337 Client *nc;
arg@450 338
arg@472 339 /* The server grab construct avoids race conditions. */
garbeam@10 340 XGrabServer(dpy);
garbeam@75 341 XSetErrorHandler(xerrordummy);
arg@400 342 detach(c);
arg@448 343 detachstack(c);
arg@400 344 if(sel == c) {
arg@450 345 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
arg@450 346 focus(nc);
arg@400 347 }
garbeam@18 348 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
arg@725 349 setclientstate(c, WithdrawnState);
arg@178 350 free(c->tags);
garbeam@10 351 free(c);
garbeam@79 352 XSync(dpy, False);
garbeam@74 353 XSetErrorHandler(xerror);
garbeam@10 354 XUngrabServer(dpy);
arg@533 355 arrange();
garbeam@10 356 }