dwm-meillo

annotate client.c @ 335:aca8281c7415

applied zero_to_five patch
author Anselm R. Garbe <arg@10kloc.org>
date Wed, 23 Aug 2006 12:03:25 +0200
parents 30b447bad2e5
children a1901753deef
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@76 5 #include "dwm.h"
garbeam@10 6 #include <stdlib.h>
garbeam@5 7 #include <string.h>
garbeam@5 8 #include <X11/Xatom.h>
garbeam@32 9 #include <X11/Xutil.h>
garbeam@5 10
garbeam@76 11 /* static functions */
garbeam@50 12
garbeam@26 13 static void
arg@318 14 grabbutton(Client *c, unsigned int button, unsigned int modifier)
arg@318 15 {
arg@318 16 XGrabButton(dpy, button, modifier, c->win, False, BUTTONMASK,
arg@318 17 GrabModeAsync, GrabModeSync, None, None);
arg@318 18 XGrabButton(dpy, button, modifier | LockMask, c->win, False, BUTTONMASK,
arg@318 19 GrabModeAsync, GrabModeSync, None, None);
arg@318 20 XGrabButton(dpy, button, modifier | numlockmask, c->win, False, BUTTONMASK,
arg@318 21 GrabModeAsync, GrabModeSync, None, None);
arg@318 22 XGrabButton(dpy, button, modifier | numlockmask | LockMask, c->win, False, BUTTONMASK,
arg@318 23 GrabModeAsync, GrabModeSync, None, None);
arg@318 24 }
arg@318 25
arg@318 26 static void
garbeam@75 27 resizetitle(Client *c)
garbeam@26 28 {
garbeam@31 29 int i;
garbeam@31 30
arg@115 31 c->tw = 0;
arg@178 32 for(i = 0; i < ntags; i++)
garbeam@31 33 if(c->tags[i])
arg@173 34 c->tw += textw(tags[i]);
arg@115 35 c->tw += textw(c->name);
arg@115 36 if(c->tw > c->w)
arg@115 37 c->tw = c->w + 2;
arg@115 38 c->tx = c->x + c->w - c->tw + 2;
arg@115 39 c->ty = c->y;
arg@261 40 if(isvisible(c))
arg@115 41 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
arg@106 42 else
arg@115 43 XMoveResizeWindow(dpy, c->title, c->tx + 2 * sw, c->ty, c->tw, c->th);
arg@106 44
garbeam@26 45 }
garbeam@18 46
arg@318 47 static void
arg@318 48 ungrabbutton(Client *c, unsigned int button, unsigned int modifier)
arg@318 49 {
arg@318 50 XUngrabButton(dpy, button, modifier, c->win);
arg@318 51 XUngrabButton(dpy, button, modifier | LockMask, c->win);
arg@318 52 XUngrabButton(dpy, button, modifier | numlockmask, c->win);
arg@318 53 XUngrabButton(dpy, button, modifier | numlockmask | LockMask, c->win);
arg@318 54 }
arg@318 55
garbeam@76 56 static int
garbeam@76 57 xerrordummy(Display *dsply, XErrorEvent *ee)
garbeam@5 58 {
garbeam@76 59 return 0;
garbeam@5 60 }
garbeam@5 61
garbeam@76 62 /* extern functions */
garbeam@20 63
garbeam@20 64 void
garbeam@76 65 ban(Client *c)
garbeam@26 66 {
arg@115 67 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
arg@115 68 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
garbeam@26 69 }
garbeam@26 70
garbeam@26 71 void
garbeam@13 72 focus(Client *c)
garbeam@13 73 {
arg@286 74 Client *old = sel;
arg@286 75
arg@319 76 if(!issel)
arg@239 77 return;
arg@319 78 if(!sel)
arg@319 79 sel = c;
arg@319 80 else if(sel != c) {
arg@319 81 if(sel->ismax)
arg@319 82 togglemax(NULL);
arg@319 83 sel = c;
arg@318 84 grabbutton(old, AnyButton, 0);
garbeam@74 85 drawtitle(old);
arg@318 86 }
arg@318 87 ungrabbutton(c, AnyButton, 0);
garbeam@74 88 drawtitle(c);
garbeam@26 89 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
garbeam@13 90 }
garbeam@13 91
garbeam@76 92 Client *
garbeam@76 93 getclient(Window w)
garbeam@76 94 {
garbeam@76 95 Client *c;
arg@123 96
garbeam@76 97 for(c = clients; c; c = c->next)
garbeam@76 98 if(c->win == w)
garbeam@76 99 return c;
garbeam@76 100 return NULL;
garbeam@76 101 }
garbeam@76 102
garbeam@76 103 Client *
garbeam@76 104 getctitle(Window w)
garbeam@76 105 {
garbeam@76 106 Client *c;
arg@123 107
garbeam@76 108 for(c = clients; c; c = c->next)
garbeam@76 109 if(c->title == w)
garbeam@76 110 return c;
garbeam@76 111 return NULL;
garbeam@76 112 }
garbeam@76 113
garbeam@76 114 void
garbeam@76 115 gravitate(Client *c, Bool invert)
garbeam@76 116 {
garbeam@76 117 int dx = 0, dy = 0;
garbeam@76 118
garbeam@76 119 switch(c->grav) {
arg@127 120 default:
arg@127 121 break;
garbeam@76 122 case StaticGravity:
garbeam@76 123 case NorthWestGravity:
garbeam@76 124 case NorthGravity:
garbeam@76 125 case NorthEastGravity:
garbeam@76 126 dy = c->border;
garbeam@76 127 break;
garbeam@76 128 case EastGravity:
garbeam@76 129 case CenterGravity:
garbeam@76 130 case WestGravity:
arg@115 131 dy = -(c->h / 2) + c->border;
garbeam@76 132 break;
garbeam@76 133 case SouthEastGravity:
garbeam@76 134 case SouthGravity:
garbeam@76 135 case SouthWestGravity:
arg@115 136 dy = -(c->h);
garbeam@76 137 break;
garbeam@76 138 }
garbeam@76 139
garbeam@76 140 switch (c->grav) {
arg@127 141 default:
arg@127 142 break;
garbeam@76 143 case StaticGravity:
garbeam@76 144 case NorthWestGravity:
garbeam@76 145 case WestGravity:
garbeam@76 146 case SouthWestGravity:
garbeam@76 147 dx = c->border;
garbeam@76 148 break;
garbeam@76 149 case NorthGravity:
garbeam@76 150 case CenterGravity:
garbeam@76 151 case SouthGravity:
arg@115 152 dx = -(c->w / 2) + c->border;
garbeam@76 153 break;
garbeam@76 154 case NorthEastGravity:
garbeam@76 155 case EastGravity:
garbeam@76 156 case SouthEastGravity:
arg@115 157 dx = -(c->w + c->border);
garbeam@76 158 break;
garbeam@76 159 }
garbeam@76 160
garbeam@76 161 if(invert) {
garbeam@76 162 dx = -dx;
garbeam@76 163 dy = -dy;
garbeam@76 164 }
arg@115 165 c->x += dx;
arg@115 166 c->y += dy;
garbeam@76 167 }
garbeam@76 168
garbeam@76 169 void
garbeam@76 170 killclient(Arg *arg)
garbeam@76 171 {
garbeam@76 172 if(!sel)
garbeam@76 173 return;
arg@157 174 if(sel->proto & PROTODELWIN)
garbeam@77 175 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
garbeam@76 176 else
garbeam@76 177 XKillClient(dpy, sel->win);
garbeam@76 178 }
garbeam@76 179
garbeam@76 180 void
garbeam@10 181 manage(Window w, XWindowAttributes *wa)
garbeam@5 182 {
arg@320 183 unsigned int i;
arg@306 184 Client *c, *tc;
arg@123 185 Window trans;
garbeam@5 186 XSetWindowAttributes twa;
garbeam@5 187
garbeam@5 188 c = emallocz(sizeof(Client));
arg@178 189 c->tags = emallocz(ntags * sizeof(Bool));
garbeam@5 190 c->win = w;
arg@115 191 c->x = c->tx = wa->x;
arg@115 192 c->y = c->ty = wa->y;
arg@115 193 c->w = c->tw = wa->width;
arg@115 194 c->h = wa->height;
arg@115 195 c->th = bh;
arg@100 196
arg@164 197 c->border = 0;
arg@163 198 setsize(c);
arg@163 199
arg@315 200 if(c->x + c->w + 2 > sw)
arg@314 201 c->x = sw - c->w - 2;
arg@314 202 if(c->x < 0)
arg@314 203 c->x = 0;
arg@315 204 if(c->y + c->h + 2 > sh)
arg@314 205 c->y = sh - c->h - 2;
arg@163 206 if(c->h != sh && c->y < bh)
arg@314 207 c->y = bh;
arg@104 208
garbeam@75 209 c->proto = getproto(c->win);
garbeam@26 210 XSelectInput(dpy, c->win,
arg@127 211 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
garbeam@53 212 XGetTransientForHint(dpy, c->win, &trans);
garbeam@5 213 twa.override_redirect = 1;
garbeam@5 214 twa.background_pixmap = ParentRelative;
arg@161 215 twa.event_mask = ExposureMask | EnterWindowMask;
garbeam@5 216
arg@115 217 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
garbeam@20 218 0, DefaultDepth(dpy, screen), CopyFromParent,
garbeam@5 219 DefaultVisual(dpy, screen),
garbeam@5 220 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
garbeam@31 221
arg@127 222 if(clients)
arg@127 223 clients->prev = c;
garbeam@80 224 c->next = clients;
garbeam@80 225 clients = c;
garbeam@49 226
arg@318 227 grabbutton(c, Button1, MODKEY);
arg@318 228 grabbutton(c, Button2, MODKEY);
arg@318 229 grabbutton(c, Button3, MODKEY);
garbeam@53 230
arg@320 231 if((tc = getclient(trans))) /* inherit tags */
arg@320 232 for(i = 0; i < ntags; i++)
arg@320 233 c->tags[i] = tc->tags[i];
arg@320 234 else
arg@320 235 settags(c);
garbeam@80 236 if(!c->isfloat)
arg@163 237 c->isfloat = trans
arg@163 238 || (c->maxw && c->minw &&
arg@164 239 c->maxw == c->minw && c->maxh == c->minh);
garbeam@95 240 settitle(c);
arg@283 241 if(isvisible(c))
arg@283 242 sel = c;
arg@283 243 arrange(NULL);
arg@270 244 XMapWindow(dpy, c->win);
arg@270 245 XMapWindow(dpy, c->title);
arg@261 246 if(isvisible(c))
garbeam@51 247 focus(c);
garbeam@94 248 }
garbeam@94 249
garbeam@94 250 void
arg@129 251 resize(Client *c, Bool sizehints, Corner sticky)
garbeam@18 252 {
arg@123 253 int bottom = c->y + c->h;
arg@123 254 int right = c->x + c->w;
arg@163 255 XWindowChanges wc;
garbeam@18 256
arg@129 257 if(sizehints) {
garbeam@52 258 if(c->incw)
arg@115 259 c->w -= (c->w - c->basew) % c->incw;
garbeam@52 260 if(c->inch)
arg@115 261 c->h -= (c->h - c->baseh) % c->inch;
arg@129 262 if(c->minw && c->w < c->minw)
arg@129 263 c->w = c->minw;
arg@129 264 if(c->minh && c->h < c->minh)
arg@129 265 c->h = c->minh;
arg@129 266 if(c->maxw && c->w > c->maxw)
arg@129 267 c->w = c->maxw;
arg@129 268 if(c->maxh && c->h > c->maxh)
arg@129 269 c->h = c->maxh;
garbeam@52 270 }
arg@105 271 if(sticky == TopRight || sticky == BotRight)
arg@115 272 c->x = right - c->w;
arg@105 273 if(sticky == BotLeft || sticky == BotRight)
arg@115 274 c->y = bottom - c->h;
arg@106 275
garbeam@75 276 resizetitle(c);
arg@163 277 wc.x = c->x;
arg@163 278 wc.y = c->y;
arg@163 279 wc.width = c->w;
arg@163 280 wc.height = c->h;
arg@164 281 if(c->w == sw && c->h == sh)
arg@164 282 wc.border_width = 0;
arg@164 283 else
arg@164 284 wc.border_width = 1;
arg@164 285 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
garbeam@79 286 XSync(dpy, False);
garbeam@18 287 }
garbeam@18 288
garbeam@76 289 void
garbeam@76 290 setsize(Client *c)
garbeam@10 291 {
arg@123 292 long msize;
garbeam@76 293 XSizeHints size;
arg@123 294
garbeam@76 295 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
garbeam@76 296 size.flags = PSize;
garbeam@76 297 c->flags = size.flags;
garbeam@76 298 if(c->flags & PBaseSize) {
garbeam@76 299 c->basew = size.base_width;
garbeam@76 300 c->baseh = size.base_height;
garbeam@76 301 }
garbeam@76 302 else
garbeam@76 303 c->basew = c->baseh = 0;
garbeam@76 304 if(c->flags & PResizeInc) {
garbeam@76 305 c->incw = size.width_inc;
garbeam@76 306 c->inch = size.height_inc;
garbeam@76 307 }
garbeam@76 308 else
garbeam@76 309 c->incw = c->inch = 0;
garbeam@76 310 if(c->flags & PMaxSize) {
garbeam@76 311 c->maxw = size.max_width;
garbeam@76 312 c->maxh = size.max_height;
garbeam@76 313 }
garbeam@76 314 else
garbeam@76 315 c->maxw = c->maxh = 0;
garbeam@76 316 if(c->flags & PMinSize) {
garbeam@76 317 c->minw = size.min_width;
garbeam@76 318 c->minh = size.min_height;
garbeam@76 319 }
garbeam@76 320 else
garbeam@76 321 c->minw = c->minh = 0;
garbeam@76 322 if(c->flags & PWinGravity)
garbeam@76 323 c->grav = size.win_gravity;
garbeam@76 324 else
garbeam@76 325 c->grav = NorthWestGravity;
garbeam@76 326 }
garbeam@76 327
garbeam@76 328 void
garbeam@76 329 settitle(Client *c)
garbeam@76 330 {
arg@123 331 char **list = NULL;
arg@123 332 int n;
garbeam@76 333 XTextProperty name;
garbeam@76 334
garbeam@76 335 name.nitems = 0;
garbeam@76 336 c->name[0] = 0;
garbeam@77 337 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
garbeam@76 338 if(!name.nitems)
garbeam@76 339 XGetWMName(dpy, c->win, &name);
garbeam@76 340 if(!name.nitems)
garbeam@76 341 return;
garbeam@76 342 if(name.encoding == XA_STRING)
garbeam@76 343 strncpy(c->name, (char *)name.value, sizeof(c->name));
garbeam@76 344 else {
garbeam@76 345 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@76 346 && n > 0 && *list)
garbeam@76 347 {
garbeam@76 348 strncpy(c->name, *list, sizeof(c->name));
garbeam@76 349 XFreeStringList(list);
garbeam@76 350 }
garbeam@76 351 }
garbeam@76 352 XFree(name.value);
garbeam@76 353 resizetitle(c);
garbeam@10 354 }
garbeam@10 355
garbeam@10 356 void
arg@124 357 togglemax(Arg *arg)
arg@124 358 {
arg@124 359 int ox, oy, ow, oh;
arg@124 360 XEvent ev;
arg@124 361
arg@124 362 if(!sel)
arg@124 363 return;
arg@124 364
arg@124 365 if((sel->ismax = !sel->ismax)) {
arg@124 366 ox = sel->x;
arg@124 367 oy = sel->y;
arg@124 368 ow = sel->w;
arg@124 369 oh = sel->h;
arg@124 370 sel->x = sx;
arg@124 371 sel->y = sy + bh;
arg@164 372 sel->w = sw - 2;
arg@164 373 sel->h = sh - 2 - bh;
arg@124 374
arg@270 375 restack();
arg@247 376 resize(sel, arrange == dofloat, TopLeft);
arg@124 377
arg@124 378 sel->x = ox;
arg@124 379 sel->y = oy;
arg@124 380 sel->w = ow;
arg@124 381 sel->h = oh;
arg@124 382 }
arg@124 383 else
arg@124 384 resize(sel, False, TopLeft);
arg@124 385 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
arg@124 386 }
arg@124 387
arg@124 388 void
garbeam@10 389 unmanage(Client *c)
garbeam@10 390 {
garbeam@10 391 XGrabServer(dpy);
garbeam@75 392 XSetErrorHandler(xerrordummy);
garbeam@10 393
garbeam@18 394 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
garbeam@10 395 XDestroyWindow(dpy, c->title);
garbeam@10 396
arg@127 397 if(c->prev)
arg@127 398 c->prev->next = c->next;
arg@127 399 if(c->next)
arg@127 400 c->next->prev = c->prev;
arg@151 401 if(c == clients)
arg@151 402 clients = c->next;
arg@251 403 if(sel == c)
arg@251 404 sel = getnext(clients);
arg@178 405 free(c->tags);
garbeam@10 406 free(c);
garbeam@10 407
garbeam@79 408 XSync(dpy, False);
garbeam@74 409 XSetErrorHandler(xerror);
garbeam@10 410 XUngrabServer(dpy);
garbeam@50 411 if(sel)
garbeam@50 412 focus(sel);
arg@270 413 arrange(NULL);
garbeam@10 414 }