dwm.c (56591B)
1 /* See LICENSE file for copyright and license details. 2 * 3 * dynamic window manager is designed like any other X client as well. It is 4 * driven through handling X events. In contrast to other X clients, a window 5 * manager selects for SubstructureRedirectMask on the root window, to receive 6 * events about window (dis-)appearance. Only one X connection at a time is 7 * allowed to select for this event mask. 8 * 9 * The event handlers of dwm are organized in an array which is accessed 10 * whenever a new event has been fetched. This allows event dispatching 11 * in O(1) time. 12 * 13 * Each child of the root window is called a client, except windows which have 14 * set the override_redirect flag. Clients are organized in a linked client 15 * list on each monitor, the focus history is remembered through a stack list 16 * on each monitor. Each client contains a bit array to indicate the tags of a 17 * client. 18 * 19 * Keys and tagging rules are organized as arrays and defined in config.h. 20 * 21 * To understand everything else, start reading main(). 22 */ 23 #include <errno.h> 24 #include <locale.h> 25 #include <signal.h> 26 #include <stdarg.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <unistd.h> 31 #include <sys/types.h> 32 #include <sys/wait.h> 33 #include <X11/cursorfont.h> 34 #include <X11/keysym.h> 35 #include <X11/Xatom.h> 36 #include <X11/Xlib.h> 37 #include <X11/Xproto.h> 38 #include <X11/Xutil.h> 39 #ifdef XINERAMA 40 #include <X11/extensions/Xinerama.h> 41 #endif /* XINERAMA */ 42 #include <X11/Xft/Xft.h> 43 44 /* for multimedia keys */ 45 #include <X11/XF86keysym.h> 46 47 #include "drw.h" 48 #include "util.h" 49 50 /* macros */ 51 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) 52 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask)) 53 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \ 54 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy))) 55 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags])) 56 #define LENGTH(X) (sizeof X / sizeof X[0]) 57 #define MOUSEMASK (BUTTONMASK|PointerMotionMask) 58 #define WIDTH(X) ((X)->w + 2 * (X)->bw) 59 #define HEIGHT(X) ((X)->h + 2 * (X)->bw) 60 #define TAGMASK ((1 << LENGTH(tags)) - 1) 61 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 62 63 /* enums */ 64 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ 65 enum { SchemeNorm, SchemeSel }; /* color schemes */ 66 enum { NetSupported, NetWMName, NetWMState, NetWMCheck, 67 NetWMFullscreen, NetActiveWindow, NetWMWindowType, 68 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */ 69 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */ 70 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, 71 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */ 72 73 typedef union { 74 int i; 75 unsigned int ui; 76 float f; 77 const void *v; 78 } Arg; 79 80 typedef struct { 81 unsigned int click; 82 unsigned int mask; 83 unsigned int button; 84 void (*func)(const Arg *arg); 85 const Arg arg; 86 } Button; 87 88 typedef struct Monitor Monitor; 89 typedef struct Client Client; 90 struct Client { 91 char name[256]; 92 float mina, maxa; 93 int x, y, w, h; 94 int oldx, oldy, oldw, oldh; 95 int basew, baseh, incw, inch, maxw, maxh, minw, minh; 96 int bw, oldbw; 97 unsigned int tags; 98 int isfixed, floatpos, isfloating, isurgent, neverfocus, oldstate, isfullscreen; 99 Client *next; 100 Client *snext; 101 Monitor *mon; 102 Window win; 103 }; 104 105 typedef struct { 106 unsigned int mod; 107 KeySym keysym; 108 void (*func)(const Arg *); 109 const Arg arg; 110 } Key; 111 112 typedef struct { 113 const char *symbol; 114 void (*arrange)(Monitor *); 115 } Layout; 116 117 typedef struct Pertag Pertag; 118 struct Monitor { 119 char ltsymbol[16]; 120 float mfact; 121 int nmaster; 122 int num; 123 int by; /* bar geometry */ 124 int mx, my, mw, mh; /* screen size */ 125 int wx, wy, ww, wh; /* window area */ 126 unsigned int seltags; 127 unsigned int sellt; 128 unsigned int tagset[2]; 129 int showbar; 130 int topbar; 131 Client *clients; 132 Client *sel; 133 Client *stack; 134 Monitor *next; 135 Window barwin; 136 const Layout *lt[2]; 137 Pertag *pertag; 138 }; 139 140 typedef struct { 141 const char *class; 142 const char *instance; 143 const char *title; 144 unsigned int tags; 145 int floatpos; 146 int isfloating; 147 int monitor; 148 } Rule; 149 150 /* function declarations */ 151 static void applyrules(Client *c); 152 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact); 153 static void arrange(Monitor *m); 154 static void arrangemon(Monitor *m); 155 static void attach(Client *c); 156 static void attachstack(Client *c); 157 static void buttonpress(XEvent *e); 158 static void checkotherwm(void); 159 static void cleanup(void); 160 static void cleanupmon(Monitor *mon); 161 static void clientmessage(XEvent *e); 162 static void configure(Client *c); 163 static void configurenotify(XEvent *e); 164 static void configurerequest(XEvent *e); 165 static Monitor *createmon(void); 166 static void destroynotify(XEvent *e); 167 static void detach(Client *c); 168 static void detachstack(Client *c); 169 static Monitor *dirtomon(int dir); 170 static void drawbar(Monitor *m); 171 static void drawbars(void); 172 static void enternotify(XEvent *e); 173 static void expose(XEvent *e); 174 static void focus(Client *c); 175 static void focusin(XEvent *e); 176 static void focusmon(const Arg *arg); 177 static void focusstack(const Arg *arg); 178 static int getrootptr(int *x, int *y); 179 static long getstate(Window w); 180 static int gettextprop(Window w, Atom atom, char *text, unsigned int size); 181 static void grabbuttons(Client *c, int focused); 182 static void grabkeys(void); 183 static void incnmaster(const Arg *arg); 184 static void keypress(XEvent *e); 185 static void killclient(const Arg *arg); 186 static void manage(Window w, XWindowAttributes *wa); 187 static void mappingnotify(XEvent *e); 188 static void maprequest(XEvent *e); 189 static void monocle(Monitor *m); 190 static void motionnotify(XEvent *e); 191 static void movemouse(const Arg *arg); 192 static Client *nexttiled(Client *c); 193 static void pop(Client *); 194 static void propertynotify(XEvent *e); 195 static void quit(const Arg *arg); 196 static Monitor *recttomon(int x, int y, int w, int h); 197 static void resize(Client *c, int x, int y, int w, int h, int interact); 198 static void resizeclient(Client *c, int x, int y, int w, int h); 199 static void resizemouse(const Arg *arg); 200 static void restack(Monitor *m); 201 static void run(void); 202 static void scan(void); 203 static int sendevent(Client *c, Atom proto); 204 static void sendmon(Client *c, Monitor *m); 205 static void setclientstate(Client *c, long state); 206 static void setfocus(Client *c); 207 static void setfullscreen(Client *c, int fullscreen); 208 static void setlayout(const Arg *arg); 209 static void setmfact(const Arg *arg); 210 static void setup(void); 211 static void seturgent(Client *c, int urg); 212 static void showhide(Client *c); 213 static void sigchld(int unused); 214 static void spawn(const Arg *arg); 215 static void tag(const Arg *arg); 216 static void tagmon(const Arg *arg); 217 static void tile(Monitor *); 218 static void togglebar(const Arg *arg); 219 static void togglefloating(const Arg *arg); 220 static void toggletag(const Arg *arg); 221 static void toggleview(const Arg *arg); 222 static void unfocus(Client *c, int setfocus); 223 static void unmanage(Client *c, int destroyed); 224 static void unmapnotify(XEvent *e); 225 static void updatebarpos(Monitor *m); 226 static void updatebars(void); 227 static void updateclientlist(void); 228 static int updategeom(void); 229 static void updatenumlockmask(void); 230 static void updatesizehints(Client *c); 231 static void updatestatus(void); 232 static void updatetitle(Client *c); 233 static void updatewindowtype(Client *c); 234 static void updatewmhints(Client *c); 235 static void view(const Arg *arg); 236 static Client *wintoclient(Window w); 237 static Monitor *wintomon(Window w); 238 static int xerror(Display *dpy, XErrorEvent *ee); 239 static int xerrordummy(Display *dpy, XErrorEvent *ee); 240 static int xerrorstart(Display *dpy, XErrorEvent *ee); 241 static void zoom(const Arg *arg); 242 243 /* variables */ 244 static const char broken[] = "broken"; 245 static char stext[256]; 246 static int screen; 247 static int sw, sh; /* X display screen geometry width, height */ 248 static int bh, blw = 0; /* bar geometry */ 249 static int lrpad; /* sum of left and right padding for text */ 250 static int (*xerrorxlib)(Display *, XErrorEvent *); 251 static unsigned int numlockmask = 0; 252 static void (*handler[LASTEvent]) (XEvent *) = { 253 [ButtonPress] = buttonpress, 254 [ClientMessage] = clientmessage, 255 [ConfigureRequest] = configurerequest, 256 [ConfigureNotify] = configurenotify, 257 [DestroyNotify] = destroynotify, 258 [EnterNotify] = enternotify, 259 [Expose] = expose, 260 [FocusIn] = focusin, 261 [KeyPress] = keypress, 262 [MappingNotify] = mappingnotify, 263 [MapRequest] = maprequest, 264 [MotionNotify] = motionnotify, 265 [PropertyNotify] = propertynotify, 266 [UnmapNotify] = unmapnotify 267 }; 268 static Atom wmatom[WMLast], netatom[NetLast]; 269 static int running = 1; 270 static Cur *cursor[CurLast]; 271 static Clr **scheme; 272 static Display *dpy; 273 static Drw *drw; 274 static Monitor *mons, *selmon; 275 static Window root, wmcheckwin; 276 277 /* configuration, allows nested code to access above variables */ 278 #include "config.h" 279 280 struct Pertag { 281 unsigned int curtag, prevtag; /* current and previous tag */ 282 int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */ 283 float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */ 284 unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */ 285 const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes */ 286 int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */ 287 }; 288 289 /* compile-time check if all tags fit into an unsigned int bit array. */ 290 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; }; 291 292 /* function implementations */ 293 void 294 applyrules(Client *c) 295 { 296 const char *class, *instance; 297 unsigned int i; 298 const Rule *r; 299 Monitor *m; 300 XClassHint ch = { NULL, NULL }; 301 302 /* rule matching */ 303 c->floatpos = 0; 304 c->isfloating = 0; 305 c->tags = 0; 306 XGetClassHint(dpy, c->win, &ch); 307 class = ch.res_class ? ch.res_class : broken; 308 instance = ch.res_name ? ch.res_name : broken; 309 310 for (i = 0; i < LENGTH(rules); i++) { 311 r = &rules[i]; 312 if ((!r->title || strstr(c->name, r->title)) 313 && (!r->class || strstr(class, r->class)) 314 && (!r->instance || strstr(instance, r->instance))) 315 { 316 c->isfloating = r->isfloating; 317 c->floatpos = r->floatpos; 318 c->tags |= r->tags; 319 for (m = mons; m && m->num != r->monitor; m = m->next); 320 if (m) 321 c->mon = m; 322 } 323 } 324 if (ch.res_class) 325 XFree(ch.res_class); 326 if (ch.res_name) 327 XFree(ch.res_name); 328 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags]; 329 } 330 331 int 332 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) 333 { 334 int baseismin; 335 Monitor *m = c->mon; 336 337 /* set minimum possible */ 338 *w = MAX(1, *w); 339 *h = MAX(1, *h); 340 if (interact) { 341 if (*x > sw) 342 *x = sw - WIDTH(c); 343 if (*y > sh) 344 *y = sh - HEIGHT(c); 345 if (*x + *w + 2 * c->bw < 0) 346 *x = 0; 347 if (*y + *h + 2 * c->bw < 0) 348 *y = 0; 349 } else { 350 if (*x >= m->wx + m->ww) 351 *x = m->wx + m->ww - WIDTH(c); 352 if (*y >= m->wy + m->wh) 353 *y = m->wy + m->wh - HEIGHT(c); 354 if (*x + *w + 2 * c->bw <= m->wx) 355 *x = m->wx; 356 if (*y + *h + 2 * c->bw <= m->wy) 357 *y = m->wy; 358 } 359 if (*h < bh) 360 *h = bh; 361 if (*w < bh) 362 *w = bh; 363 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) { 364 /* see last two sentences in ICCCM 4.1.2.3 */ 365 baseismin = c->basew == c->minw && c->baseh == c->minh; 366 if (!baseismin) { /* temporarily remove base dimensions */ 367 *w -= c->basew; 368 *h -= c->baseh; 369 } 370 /* adjust for aspect limits */ 371 if (c->mina > 0 && c->maxa > 0) { 372 if (c->maxa < (float)*w / *h) 373 *w = *h * c->maxa + 0.5; 374 else if (c->mina < (float)*h / *w) 375 *h = *w * c->mina + 0.5; 376 } 377 if (baseismin) { /* increment calculation requires this */ 378 *w -= c->basew; 379 *h -= c->baseh; 380 } 381 /* adjust for increment value */ 382 if (c->incw) 383 *w -= *w % c->incw; 384 if (c->inch) 385 *h -= *h % c->inch; 386 /* restore base dimensions */ 387 *w = MAX(*w + c->basew, c->minw); 388 *h = MAX(*h + c->baseh, c->minh); 389 if (c->maxw) 390 *w = MIN(*w, c->maxw); 391 if (c->maxh) 392 *h = MIN(*h, c->maxh); 393 } 394 return *x != c->x || *y != c->y || *w != c->w || *h != c->h; 395 } 396 397 void 398 arrange(Monitor *m) 399 { 400 if (m) 401 showhide(m->stack); 402 else for (m = mons; m; m = m->next) 403 showhide(m->stack); 404 if (m) { 405 arrangemon(m); 406 restack(m); 407 } else for (m = mons; m; m = m->next) 408 arrangemon(m); 409 } 410 411 void 412 arrangemon(Monitor *m) 413 { 414 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol); 415 if (m->lt[m->sellt]->arrange) 416 m->lt[m->sellt]->arrange(m); 417 } 418 419 void 420 attach(Client *c) 421 { 422 c->next = c->mon->clients; 423 c->mon->clients = c; 424 } 425 426 void 427 attachstack(Client *c) 428 { 429 c->snext = c->mon->stack; 430 c->mon->stack = c; 431 } 432 433 void 434 buttonpress(XEvent *e) 435 { 436 unsigned int i, x, click, occ = 0; 437 Arg arg = {0}; 438 Client *c; 439 Monitor *m; 440 XButtonPressedEvent *ev = &e->xbutton; 441 442 click = ClkRootWin; 443 /* focus monitor if necessary */ 444 if ((m = wintomon(ev->window)) && m != selmon) { 445 unfocus(selmon->sel, 1); 446 selmon = m; 447 focus(NULL); 448 } 449 if (ev->window == selmon->barwin) { 450 i = x = 0; 451 for (c = m->clients; c; c = c->next) 452 occ |= c->tags == 255 ? 0 : c->tags; 453 do { 454 /* do not reserve space for vacant tags */ 455 if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i)) 456 continue; 457 x += TEXTW(tags[i]); 458 } while (ev->x >= x && ++i < LENGTH(tags)); 459 if (i < LENGTH(tags)) { 460 click = ClkTagBar; 461 arg.ui = 1 << i; 462 } else if (ev->x < x + blw) 463 click = ClkLtSymbol; 464 else if (ev->x > selmon->ww - TEXTW(stext)) 465 click = ClkStatusText; 466 else 467 click = ClkWinTitle; 468 } else if ((c = wintoclient(ev->window))) { 469 focus(c); 470 restack(selmon); 471 XAllowEvents(dpy, ReplayPointer, CurrentTime); 472 click = ClkClientWin; 473 } 474 for (i = 0; i < LENGTH(buttons); i++) 475 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button 476 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) 477 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg); 478 } 479 480 void 481 checkotherwm(void) 482 { 483 xerrorxlib = XSetErrorHandler(xerrorstart); 484 /* this causes an error if some other window manager is running */ 485 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask); 486 XSync(dpy, False); 487 XSetErrorHandler(xerror); 488 XSync(dpy, False); 489 } 490 491 void 492 cleanup(void) 493 { 494 Arg a = {.ui = ~0}; 495 Layout foo = { "", NULL }; 496 Monitor *m; 497 size_t i; 498 499 view(&a); 500 selmon->lt[selmon->sellt] = &foo; 501 for (m = mons; m; m = m->next) 502 while (m->stack) 503 unmanage(m->stack, 0); 504 XUngrabKey(dpy, AnyKey, AnyModifier, root); 505 while (mons) 506 cleanupmon(mons); 507 for (i = 0; i < CurLast; i++) 508 drw_cur_free(drw, cursor[i]); 509 for (i = 0; i < LENGTH(colors); i++) 510 free(scheme[i]); 511 XDestroyWindow(dpy, wmcheckwin); 512 drw_free(drw); 513 XSync(dpy, False); 514 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); 515 XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 516 } 517 518 void 519 cleanupmon(Monitor *mon) 520 { 521 Monitor *m; 522 523 if (mon == mons) 524 mons = mons->next; 525 else { 526 for (m = mons; m && m->next != mon; m = m->next); 527 m->next = mon->next; 528 } 529 XUnmapWindow(dpy, mon->barwin); 530 XDestroyWindow(dpy, mon->barwin); 531 free(mon); 532 } 533 534 void 535 clientmessage(XEvent *e) 536 { 537 XClientMessageEvent *cme = &e->xclient; 538 Client *c = wintoclient(cme->window); 539 540 if (!c) 541 return; 542 if (cme->message_type == netatom[NetWMState]) { 543 if (cme->data.l[1] == netatom[NetWMFullscreen] 544 || cme->data.l[2] == netatom[NetWMFullscreen]) 545 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */ 546 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen))); 547 } else if (cme->message_type == netatom[NetActiveWindow]) { 548 if (c != selmon->sel && !c->isurgent) 549 seturgent(c, 1); 550 } 551 } 552 553 void 554 configure(Client *c) 555 { 556 XConfigureEvent ce; 557 558 ce.type = ConfigureNotify; 559 ce.display = dpy; 560 ce.event = c->win; 561 ce.window = c->win; 562 ce.x = c->x; 563 ce.y = c->y; 564 ce.width = c->w; 565 ce.height = c->h; 566 ce.border_width = c->bw; 567 ce.above = None; 568 ce.override_redirect = False; 569 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce); 570 } 571 572 void 573 configurenotify(XEvent *e) 574 { 575 Monitor *m; 576 Client *c; 577 XConfigureEvent *ev = &e->xconfigure; 578 int dirty; 579 580 /* TODO: updategeom handling sucks, needs to be simplified */ 581 if (ev->window == root) { 582 dirty = (sw != ev->width || sh != ev->height); 583 sw = ev->width; 584 sh = ev->height; 585 if (updategeom() || dirty) { 586 drw_resize(drw, sw, bh); 587 updatebars(); 588 for (m = mons; m; m = m->next) { 589 for (c = m->clients; c; c = c->next) 590 if (c->isfullscreen) 591 resizeclient(c, m->mx, m->my, m->mw, m->mh); 592 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh); 593 } 594 focus(NULL); 595 arrange(NULL); 596 } 597 } 598 } 599 600 void 601 configurerequest(XEvent *e) 602 { 603 Client *c; 604 Monitor *m; 605 XConfigureRequestEvent *ev = &e->xconfigurerequest; 606 XWindowChanges wc; 607 608 if ((c = wintoclient(ev->window))) { 609 if (ev->value_mask & CWBorderWidth) 610 c->bw = ev->border_width; 611 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) { 612 m = c->mon; 613 if (ev->value_mask & CWX) { 614 c->oldx = c->x; 615 c->x = m->mx + ev->x; 616 } 617 if (ev->value_mask & CWY) { 618 c->oldy = c->y; 619 c->y = m->my + ev->y; 620 } 621 if (ev->value_mask & CWWidth) { 622 c->oldw = c->w; 623 c->w = ev->width; 624 } 625 if (ev->value_mask & CWHeight) { 626 c->oldh = c->h; 627 c->h = ev->height; 628 } 629 if ((c->x + c->w) > m->mx + m->mw && c->isfloating) 630 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */ 631 if ((c->y + c->h) > m->my + m->mh && c->isfloating) 632 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */ 633 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight))) 634 configure(c); 635 if (ISVISIBLE(c)) 636 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); 637 } else 638 configure(c); 639 } else { 640 wc.x = ev->x; 641 wc.y = ev->y; 642 wc.width = ev->width; 643 wc.height = ev->height; 644 wc.border_width = ev->border_width; 645 wc.sibling = ev->above; 646 wc.stack_mode = ev->detail; 647 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc); 648 } 649 XSync(dpy, False); 650 } 651 652 Monitor * 653 createmon(void) 654 { 655 Monitor *m; 656 unsigned int i; 657 658 m = ecalloc(1, sizeof(Monitor)); 659 m->tagset[0] = m->tagset[1] = 1; 660 m->mfact = mfact; 661 m->nmaster = nmaster; 662 m->showbar = showbar; 663 m->topbar = topbar; 664 m->lt[0] = &layouts[0]; 665 m->lt[1] = &layouts[1 % LENGTH(layouts)]; 666 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol); 667 m->pertag = ecalloc(1, sizeof(Pertag)); 668 m->pertag->curtag = m->pertag->prevtag = 1; 669 670 for (i = 0; i <= LENGTH(tags); i++) { 671 m->pertag->nmasters[i] = m->nmaster; 672 m->pertag->mfacts[i] = m->mfact; 673 674 m->pertag->ltidxs[i][0] = m->lt[0]; 675 m->pertag->ltidxs[i][1] = m->lt[1]; 676 m->pertag->sellts[i] = m->sellt; 677 678 m->pertag->showbars[i] = m->showbar; 679 } 680 681 return m; 682 } 683 684 void 685 destroynotify(XEvent *e) 686 { 687 Client *c; 688 XDestroyWindowEvent *ev = &e->xdestroywindow; 689 690 if ((c = wintoclient(ev->window))) 691 unmanage(c, 1); 692 } 693 694 void 695 detach(Client *c) 696 { 697 Client **tc; 698 699 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next); 700 *tc = c->next; 701 } 702 703 void 704 detachstack(Client *c) 705 { 706 Client **tc, *t; 707 708 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext); 709 *tc = c->snext; 710 711 if (c == c->mon->sel) { 712 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext); 713 c->mon->sel = t; 714 } 715 } 716 717 Monitor * 718 dirtomon(int dir) 719 { 720 Monitor *m = NULL; 721 722 if (dir > 0) { 723 if (!(m = selmon->next)) 724 m = mons; 725 } else if (selmon == mons) 726 for (m = mons; m->next; m = m->next); 727 else 728 for (m = mons; m->next != selmon; m = m->next); 729 return m; 730 } 731 732 void 733 drawbar(Monitor *m) 734 { 735 int x, w, sw = 0; 736 int boxs = drw->fonts->h / 9; 737 int boxw = drw->fonts->h / 6 + 2; 738 unsigned int i, occ = 0, urg = 0; 739 Client *c; 740 741 /* draw status first so it can be overdrawn by tags later */ 742 if (m == selmon) { /* status is only drawn on selected monitor */ 743 drw_setscheme(drw, scheme[SchemeNorm]); 744 sw = TEXTW(stext) - lrpad + 2; /* 2px right padding */ 745 drw_text(drw, m->ww - sw, 0, sw, bh, 0, stext, 0); 746 } 747 748 for (c = m->clients; c; c = c->next) { 749 occ |= c->tags == 255 ? 0 : c->tags; 750 if (c->isurgent) 751 urg |= c->tags; 752 } 753 x = 0; 754 for (i = 0; i < LENGTH(tags); i++) { 755 /* do not draw vacant tags */ 756 if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i)) 757 continue; 758 759 w = TEXTW(tags[i]); 760 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]); 761 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i); 762 x += w; 763 } 764 w = blw = TEXTW(m->ltsymbol); 765 drw_setscheme(drw, scheme[SchemeNorm]); 766 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0); 767 768 if ((w = m->ww - sw - x) > bh) { 769 if (m->sel) { 770 /*drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);*/ 771 drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0); 772 if (m->sel->isfloating) 773 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0); 774 } else { 775 drw_setscheme(drw, scheme[SchemeNorm]); 776 drw_rect(drw, x, 0, w, bh, 1, 1); 777 } 778 } 779 drw_map(drw, m->barwin, 0, 0, m->ww, bh); 780 } 781 782 void 783 drawbars(void) 784 { 785 Monitor *m; 786 787 for (m = mons; m; m = m->next) 788 drawbar(m); 789 } 790 791 void 792 enternotify(XEvent *e) 793 { 794 Client *c; 795 Monitor *m; 796 XCrossingEvent *ev = &e->xcrossing; 797 798 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root) 799 return; 800 c = wintoclient(ev->window); 801 m = c ? c->mon : wintomon(ev->window); 802 if (m != selmon) { 803 unfocus(selmon->sel, 1); 804 selmon = m; 805 } else if (!c || c == selmon->sel) 806 return; 807 focus(c); 808 } 809 810 void 811 expose(XEvent *e) 812 { 813 Monitor *m; 814 XExposeEvent *ev = &e->xexpose; 815 816 if (ev->count == 0 && (m = wintomon(ev->window))) 817 drawbar(m); 818 } 819 820 void 821 focus(Client *c) 822 { 823 if (!c || !ISVISIBLE(c)) 824 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext); 825 if (selmon->sel && selmon->sel != c) 826 unfocus(selmon->sel, 0); 827 if (c) { 828 if (c->mon != selmon) 829 selmon = c->mon; 830 if (c->isurgent) 831 seturgent(c, 0); 832 detachstack(c); 833 attachstack(c); 834 grabbuttons(c, 1); 835 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel); 836 setfocus(c); 837 } else { 838 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); 839 XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 840 } 841 selmon->sel = c; 842 drawbars(); 843 } 844 845 /* there are some broken focus acquiring clients needing extra handling */ 846 void 847 focusin(XEvent *e) 848 { 849 XFocusChangeEvent *ev = &e->xfocus; 850 851 if (selmon->sel && ev->window != selmon->sel->win) 852 setfocus(selmon->sel); 853 } 854 855 void 856 focusmon(const Arg *arg) 857 { 858 Monitor *m; 859 860 if (!mons->next) 861 return; 862 if ((m = dirtomon(arg->i)) == selmon) 863 return; 864 unfocus(selmon->sel, 0); 865 selmon = m; 866 focus(NULL); 867 } 868 869 void 870 focusstack(const Arg *arg) 871 { 872 Client *c = NULL, *i; 873 874 if (!selmon->sel) 875 return; 876 if (arg->i > 0) { 877 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next); 878 if (!c) 879 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next); 880 } else { 881 for (i = selmon->clients; i != selmon->sel; i = i->next) 882 if (ISVISIBLE(i)) 883 c = i; 884 if (!c) 885 for (; i; i = i->next) 886 if (ISVISIBLE(i)) 887 c = i; 888 } 889 if (c) { 890 focus(c); 891 restack(selmon); 892 } 893 } 894 895 Atom 896 getatomprop(Client *c, Atom prop) 897 { 898 int di; 899 unsigned long dl; 900 unsigned char *p = NULL; 901 Atom da, atom = None; 902 903 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM, 904 &da, &di, &dl, &dl, &p) == Success && p) { 905 atom = *(Atom *)p; 906 XFree(p); 907 } 908 return atom; 909 } 910 911 int 912 getrootptr(int *x, int *y) 913 { 914 int di; 915 unsigned int dui; 916 Window dummy; 917 918 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui); 919 } 920 921 long 922 getstate(Window w) 923 { 924 int format; 925 long result = -1; 926 unsigned char *p = NULL; 927 unsigned long n, extra; 928 Atom real; 929 930 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState], 931 &real, &format, &n, &extra, (unsigned char **)&p) != Success) 932 return -1; 933 if (n != 0) 934 result = *p; 935 XFree(p); 936 return result; 937 } 938 939 int 940 gettextprop(Window w, Atom atom, char *text, unsigned int size) 941 { 942 char **list = NULL; 943 int n; 944 XTextProperty name; 945 946 if (!text || size == 0) 947 return 0; 948 text[0] = '\0'; 949 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems) 950 return 0; 951 if (name.encoding == XA_STRING) 952 strncpy(text, (char *)name.value, size - 1); 953 else { 954 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) { 955 strncpy(text, *list, size - 1); 956 XFreeStringList(list); 957 } 958 } 959 text[size - 1] = '\0'; 960 XFree(name.value); 961 return 1; 962 } 963 964 void 965 grabbuttons(Client *c, int focused) 966 { 967 updatenumlockmask(); 968 { 969 unsigned int i, j; 970 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask }; 971 XUngrabButton(dpy, AnyButton, AnyModifier, c->win); 972 if (!focused) 973 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, 974 BUTTONMASK, GrabModeSync, GrabModeSync, None, None); 975 for (i = 0; i < LENGTH(buttons); i++) 976 if (buttons[i].click == ClkClientWin) 977 for (j = 0; j < LENGTH(modifiers); j++) 978 XGrabButton(dpy, buttons[i].button, 979 buttons[i].mask | modifiers[j], 980 c->win, False, BUTTONMASK, 981 GrabModeAsync, GrabModeSync, None, None); 982 } 983 } 984 985 void 986 grabkeys(void) 987 { 988 updatenumlockmask(); 989 { 990 unsigned int i, j; 991 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask }; 992 KeyCode code; 993 994 XUngrabKey(dpy, AnyKey, AnyModifier, root); 995 for (i = 0; i < LENGTH(keys); i++) 996 if ((code = XKeysymToKeycode(dpy, keys[i].keysym))) 997 for (j = 0; j < LENGTH(modifiers); j++) 998 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root, 999 True, GrabModeAsync, GrabModeAsync); 1000 } 1001 } 1002 1003 void 1004 incnmaster(const Arg *arg) 1005 { 1006 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0); 1007 arrange(selmon); 1008 } 1009 1010 #ifdef XINERAMA 1011 static int 1012 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) 1013 { 1014 while (n--) 1015 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org 1016 && unique[n].width == info->width && unique[n].height == info->height) 1017 return 0; 1018 return 1; 1019 } 1020 #endif /* XINERAMA */ 1021 1022 void 1023 keypress(XEvent *e) 1024 { 1025 unsigned int i; 1026 KeySym keysym; 1027 XKeyEvent *ev; 1028 1029 ev = &e->xkey; 1030 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0); 1031 for (i = 0; i < LENGTH(keys); i++) 1032 if (keysym == keys[i].keysym 1033 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state) 1034 && keys[i].func) 1035 keys[i].func(&(keys[i].arg)); 1036 } 1037 1038 void 1039 killclient(const Arg *arg) 1040 { 1041 if (!selmon->sel) 1042 return; 1043 if (!sendevent(selmon->sel, wmatom[WMDelete])) { 1044 XGrabServer(dpy); 1045 XSetErrorHandler(xerrordummy); 1046 XSetCloseDownMode(dpy, DestroyAll); 1047 XKillClient(dpy, selmon->sel->win); 1048 XSync(dpy, False); 1049 XSetErrorHandler(xerror); 1050 XUngrabServer(dpy); 1051 } 1052 } 1053 1054 void 1055 manage(Window w, XWindowAttributes *wa) 1056 { 1057 Client *c, *t = NULL; 1058 Window trans = None; 1059 XWindowChanges wc; 1060 1061 c = ecalloc(1, sizeof(Client)); 1062 c->win = w; 1063 /* geometry */ 1064 c->x = c->oldx = wa->x; 1065 c->y = c->oldy = wa->y; 1066 c->w = c->oldw = wa->width; 1067 c->h = c->oldh = wa->height; 1068 c->oldbw = wa->border_width; 1069 1070 updatetitle(c); 1071 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) { 1072 c->mon = t->mon; 1073 c->tags = t->tags; 1074 } else { 1075 c->mon = selmon; 1076 applyrules(c); 1077 } 1078 1079 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw) 1080 c->x = c->mon->mx + c->mon->mw - WIDTH(c); 1081 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh) 1082 c->y = c->mon->my + c->mon->mh - HEIGHT(c); 1083 c->x = MAX(c->x, c->mon->mx); 1084 /* only fix client y-offset, if the client center might cover the bar */ 1085 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx) 1086 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my); 1087 c->bw = borderpx; 1088 1089 switch (c->floatpos) { 1090 case 1: case 4: case 7: 1091 c->x = 0; 1092 break; 1093 case 3: case 6: case 9: 1094 c->x = c->mon->mw - WIDTH(c); 1095 break; 1096 case 2: case 5: case 8: default: 1097 c->x = (c->mon->mw - WIDTH(c)) / 2; 1098 break; 1099 } 1100 1101 switch (c->floatpos) { 1102 case 1: case 2: case 3: 1103 c->y = 0; 1104 break; 1105 case 7: case 8: case 9: 1106 c->y = c->mon->mh - HEIGHT(c); 1107 break; 1108 case 4: case 5: case 6: default: 1109 c->y = (c->mon->mh - HEIGHT(c)) / 2; 1110 break; 1111 } 1112 1113 wc.border_width = c->bw; 1114 XConfigureWindow(dpy, w, CWBorderWidth, &wc); 1115 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel); 1116 configure(c); /* propagates border_width, if size doesn't change */ 1117 updatewindowtype(c); 1118 updatesizehints(c); 1119 updatewmhints(c); 1120 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask); 1121 grabbuttons(c, 0); 1122 if (!c->isfloating) 1123 c->isfloating = c->oldstate = trans != None || c->isfixed; 1124 if (c->isfloating) 1125 XRaiseWindow(dpy, c->win); 1126 attach(c); 1127 attachstack(c); 1128 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend, 1129 (unsigned char *) &(c->win), 1); 1130 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */ 1131 setclientstate(c, NormalState); 1132 if (c->mon == selmon) 1133 unfocus(selmon->sel, 0); 1134 c->mon->sel = c; 1135 arrange(c->mon); 1136 XMapWindow(dpy, c->win); 1137 focus(NULL); 1138 } 1139 1140 void 1141 mappingnotify(XEvent *e) 1142 { 1143 XMappingEvent *ev = &e->xmapping; 1144 1145 XRefreshKeyboardMapping(ev); 1146 if (ev->request == MappingKeyboard) 1147 grabkeys(); 1148 } 1149 1150 void 1151 maprequest(XEvent *e) 1152 { 1153 static XWindowAttributes wa; 1154 XMapRequestEvent *ev = &e->xmaprequest; 1155 1156 if (!XGetWindowAttributes(dpy, ev->window, &wa)) 1157 return; 1158 if (wa.override_redirect) 1159 return; 1160 if (!wintoclient(ev->window)) 1161 manage(ev->window, &wa); 1162 } 1163 1164 void 1165 monocle(Monitor *m) 1166 { 1167 unsigned int n = 0; 1168 Client *c; 1169 1170 for (c = m->clients; c; c = c->next) { 1171 c->bw = 0; 1172 if (ISVISIBLE(c)) 1173 n++; 1174 } 1175 if (n > 0) /* override layout symbol */ 1176 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n); 1177 for (c = nexttiled(m->clients); c; c = nexttiled(c->next)) 1178 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0); 1179 } 1180 1181 void 1182 motionnotify(XEvent *e) 1183 { 1184 static Monitor *mon = NULL; 1185 Monitor *m; 1186 XMotionEvent *ev = &e->xmotion; 1187 1188 if (ev->window != root) 1189 return; 1190 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) { 1191 unfocus(selmon->sel, 1); 1192 selmon = m; 1193 focus(NULL); 1194 } 1195 mon = m; 1196 } 1197 1198 void 1199 movemouse(const Arg *arg) 1200 { 1201 int x, y, ocx, ocy, nx, ny; 1202 Client *c; 1203 Monitor *m; 1204 XEvent ev; 1205 Time lasttime = 0; 1206 1207 if (!(c = selmon->sel)) 1208 return; 1209 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */ 1210 return; 1211 restack(selmon); 1212 ocx = c->x; 1213 ocy = c->y; 1214 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, 1215 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess) 1216 return; 1217 if (!getrootptr(&x, &y)) 1218 return; 1219 do { 1220 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev); 1221 switch(ev.type) { 1222 case ConfigureRequest: 1223 case Expose: 1224 case MapRequest: 1225 handler[ev.type](&ev); 1226 break; 1227 case MotionNotify: 1228 if ((ev.xmotion.time - lasttime) <= (1000 / 60)) 1229 continue; 1230 lasttime = ev.xmotion.time; 1231 1232 nx = ocx + (ev.xmotion.x - x); 1233 ny = ocy + (ev.xmotion.y - y); 1234 if (abs(selmon->wx - nx) < snap) 1235 nx = selmon->wx; 1236 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap) 1237 nx = selmon->wx + selmon->ww - WIDTH(c); 1238 if (abs(selmon->wy - ny) < snap) 1239 ny = selmon->wy; 1240 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap) 1241 ny = selmon->wy + selmon->wh - HEIGHT(c); 1242 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange 1243 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap)) 1244 togglefloating(NULL); 1245 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating) 1246 resize(c, nx, ny, c->w, c->h, 1); 1247 break; 1248 } 1249 } while (ev.type != ButtonRelease); 1250 XUngrabPointer(dpy, CurrentTime); 1251 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) { 1252 sendmon(c, m); 1253 selmon = m; 1254 focus(NULL); 1255 } 1256 } 1257 1258 Client * 1259 nexttiled(Client *c) 1260 { 1261 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next); 1262 return c; 1263 } 1264 1265 void 1266 pop(Client *c) 1267 { 1268 detach(c); 1269 attach(c); 1270 focus(c); 1271 arrange(c->mon); 1272 } 1273 1274 void 1275 propertynotify(XEvent *e) 1276 { 1277 Client *c; 1278 Window trans; 1279 XPropertyEvent *ev = &e->xproperty; 1280 1281 if ((ev->window == root) && (ev->atom == XA_WM_NAME)) 1282 updatestatus(); 1283 else if (ev->state == PropertyDelete) 1284 return; /* ignore */ 1285 else if ((c = wintoclient(ev->window))) { 1286 switch(ev->atom) { 1287 default: break; 1288 case XA_WM_TRANSIENT_FOR: 1289 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) && 1290 (c->isfloating = (wintoclient(trans)) != NULL)) 1291 arrange(c->mon); 1292 break; 1293 case XA_WM_NORMAL_HINTS: 1294 updatesizehints(c); 1295 break; 1296 case XA_WM_HINTS: 1297 updatewmhints(c); 1298 drawbars(); 1299 break; 1300 } 1301 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) { 1302 updatetitle(c); 1303 if (c == c->mon->sel) 1304 drawbar(c->mon); 1305 } 1306 if (ev->atom == netatom[NetWMWindowType]) 1307 updatewindowtype(c); 1308 } 1309 } 1310 1311 void 1312 quit(const Arg *arg) 1313 { 1314 running = 0; 1315 } 1316 1317 Monitor * 1318 recttomon(int x, int y, int w, int h) 1319 { 1320 Monitor *m, *r = selmon; 1321 int a, area = 0; 1322 1323 for (m = mons; m; m = m->next) 1324 if ((a = INTERSECT(x, y, w, h, m)) > area) { 1325 area = a; 1326 r = m; 1327 } 1328 return r; 1329 } 1330 1331 void 1332 resize(Client *c, int x, int y, int w, int h, int interact) 1333 { 1334 if (applysizehints(c, &x, &y, &w, &h, interact)) 1335 resizeclient(c, x, y, w, h); 1336 } 1337 1338 void 1339 resizeclient(Client *c, int x, int y, int w, int h) 1340 { 1341 XWindowChanges wc; 1342 1343 c->oldx = c->x; c->x = wc.x = x; 1344 c->oldy = c->y; c->y = wc.y = y; 1345 c->oldw = c->w; c->w = wc.width = w; 1346 c->oldh = c->h; c->h = wc.height = h; 1347 wc.border_width = c->bw; 1348 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc); 1349 configure(c); 1350 XSync(dpy, False); 1351 } 1352 1353 void 1354 resizemouse(const Arg *arg) 1355 { 1356 int ocx, ocy, nw, nh; 1357 Client *c; 1358 Monitor *m; 1359 XEvent ev; 1360 Time lasttime = 0; 1361 1362 if (!(c = selmon->sel)) 1363 return; 1364 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */ 1365 return; 1366 restack(selmon); 1367 ocx = c->x; 1368 ocy = c->y; 1369 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, 1370 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess) 1371 return; 1372 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1); 1373 do { 1374 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev); 1375 switch(ev.type) { 1376 case ConfigureRequest: 1377 case Expose: 1378 case MapRequest: 1379 handler[ev.type](&ev); 1380 break; 1381 case MotionNotify: 1382 if ((ev.xmotion.time - lasttime) <= (1000 / 60)) 1383 continue; 1384 lasttime = ev.xmotion.time; 1385 1386 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1); 1387 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1); 1388 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww 1389 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh) 1390 { 1391 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange 1392 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap)) 1393 togglefloating(NULL); 1394 } 1395 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating) 1396 resize(c, c->x, c->y, nw, nh, 1); 1397 break; 1398 } 1399 } while (ev.type != ButtonRelease); 1400 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1); 1401 XUngrabPointer(dpy, CurrentTime); 1402 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev)); 1403 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) { 1404 sendmon(c, m); 1405 selmon = m; 1406 focus(NULL); 1407 } 1408 } 1409 1410 void 1411 restack(Monitor *m) 1412 { 1413 Client *c; 1414 XEvent ev; 1415 XWindowChanges wc; 1416 1417 drawbar(m); 1418 if (!m->sel) 1419 return; 1420 if (m->sel->isfloating || !m->lt[m->sellt]->arrange) 1421 XRaiseWindow(dpy, m->sel->win); 1422 if (m->lt[m->sellt]->arrange) { 1423 wc.stack_mode = Below; 1424 wc.sibling = m->barwin; 1425 for (c = m->stack; c; c = c->snext) 1426 if (!c->isfloating && ISVISIBLE(c)) { 1427 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc); 1428 wc.sibling = c->win; 1429 } 1430 } 1431 XSync(dpy, False); 1432 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev)); 1433 } 1434 1435 void 1436 run(void) 1437 { 1438 XEvent ev; 1439 /* main event loop */ 1440 XSync(dpy, False); 1441 while (running && !XNextEvent(dpy, &ev)) 1442 if (handler[ev.type]) 1443 handler[ev.type](&ev); /* call handler */ 1444 } 1445 1446 void 1447 scan(void) 1448 { 1449 unsigned int i, num; 1450 Window d1, d2, *wins = NULL; 1451 XWindowAttributes wa; 1452 1453 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) { 1454 for (i = 0; i < num; i++) { 1455 if (!XGetWindowAttributes(dpy, wins[i], &wa) 1456 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1)) 1457 continue; 1458 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState) 1459 manage(wins[i], &wa); 1460 } 1461 for (i = 0; i < num; i++) { /* now the transients */ 1462 if (!XGetWindowAttributes(dpy, wins[i], &wa)) 1463 continue; 1464 if (XGetTransientForHint(dpy, wins[i], &d1) 1465 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)) 1466 manage(wins[i], &wa); 1467 } 1468 if (wins) 1469 XFree(wins); 1470 } 1471 } 1472 1473 void 1474 sendmon(Client *c, Monitor *m) 1475 { 1476 if (c->mon == m) 1477 return; 1478 unfocus(c, 1); 1479 detach(c); 1480 detachstack(c); 1481 c->mon = m; 1482 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */ 1483 attach(c); 1484 attachstack(c); 1485 focus(NULL); 1486 arrange(NULL); 1487 } 1488 1489 void 1490 setclientstate(Client *c, long state) 1491 { 1492 long data[] = { state, None }; 1493 1494 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32, 1495 PropModeReplace, (unsigned char *)data, 2); 1496 } 1497 1498 int 1499 sendevent(Client *c, Atom proto) 1500 { 1501 int n; 1502 Atom *protocols; 1503 int exists = 0; 1504 XEvent ev; 1505 1506 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) { 1507 while (!exists && n--) 1508 exists = protocols[n] == proto; 1509 XFree(protocols); 1510 } 1511 if (exists) { 1512 ev.type = ClientMessage; 1513 ev.xclient.window = c->win; 1514 ev.xclient.message_type = wmatom[WMProtocols]; 1515 ev.xclient.format = 32; 1516 ev.xclient.data.l[0] = proto; 1517 ev.xclient.data.l[1] = CurrentTime; 1518 XSendEvent(dpy, c->win, False, NoEventMask, &ev); 1519 } 1520 return exists; 1521 } 1522 1523 void 1524 setfocus(Client *c) 1525 { 1526 if (!c->neverfocus) { 1527 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); 1528 XChangeProperty(dpy, root, netatom[NetActiveWindow], 1529 XA_WINDOW, 32, PropModeReplace, 1530 (unsigned char *) &(c->win), 1); 1531 } 1532 sendevent(c, wmatom[WMTakeFocus]); 1533 } 1534 1535 void 1536 setfullscreen(Client *c, int fullscreen) 1537 { 1538 if (fullscreen && !c->isfullscreen) { 1539 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32, 1540 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1); 1541 c->isfullscreen = 1; 1542 c->oldstate = c->isfloating; 1543 c->oldbw = c->bw; 1544 c->bw = 0; 1545 c->isfloating = 1; 1546 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh); 1547 XRaiseWindow(dpy, c->win); 1548 } else if (!fullscreen && c->isfullscreen){ 1549 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32, 1550 PropModeReplace, (unsigned char*)0, 0); 1551 c->isfullscreen = 0; 1552 c->isfloating = c->oldstate; 1553 c->bw = c->oldbw; 1554 c->x = c->oldx; 1555 c->y = c->oldy; 1556 c->w = c->oldw; 1557 c->h = c->oldh; 1558 resizeclient(c, c->x, c->y, c->w, c->h); 1559 arrange(c->mon); 1560 } 1561 } 1562 1563 void 1564 setlayout(const Arg *arg) 1565 { 1566 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt]) 1567 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1; 1568 if (arg && arg->v) 1569 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v; 1570 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol); 1571 if (selmon->sel) 1572 arrange(selmon); 1573 else 1574 drawbar(selmon); 1575 } 1576 1577 /* arg > 1.0 will set mfact absolutely */ 1578 void 1579 setmfact(const Arg *arg) 1580 { 1581 float f; 1582 1583 if (!arg || !selmon->lt[selmon->sellt]->arrange) 1584 return; 1585 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0; 1586 if (f < 0.1 || f > 0.9) 1587 return; 1588 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f; 1589 arrange(selmon); 1590 } 1591 1592 void 1593 setup(void) 1594 { 1595 int i; 1596 XSetWindowAttributes wa; 1597 Atom utf8string; 1598 1599 /* clean up any zombies immediately */ 1600 sigchld(0); 1601 1602 /* init screen */ 1603 screen = DefaultScreen(dpy); 1604 sw = DisplayWidth(dpy, screen); 1605 sh = DisplayHeight(dpy, screen); 1606 root = RootWindow(dpy, screen); 1607 drw = drw_create(dpy, screen, root, sw, sh); 1608 if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) 1609 die("no fonts could be loaded."); 1610 lrpad = drw->fonts->h; 1611 bh = drw->fonts->h + 2; 1612 updategeom(); 1613 /* init atoms */ 1614 utf8string = XInternAtom(dpy, "UTF8_STRING", False); 1615 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); 1616 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); 1617 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False); 1618 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False); 1619 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False); 1620 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); 1621 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); 1622 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False); 1623 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False); 1624 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False); 1625 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False); 1626 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False); 1627 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False); 1628 /* init cursors */ 1629 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr); 1630 cursor[CurResize] = drw_cur_create(drw, XC_sizing); 1631 cursor[CurMove] = drw_cur_create(drw, XC_fleur); 1632 /* init appearance */ 1633 scheme = ecalloc(LENGTH(colors), sizeof(Clr *)); 1634 for (i = 0; i < LENGTH(colors); i++) 1635 scheme[i] = drw_scm_create(drw, colors[i], 3); 1636 /* init bars */ 1637 updatebars(); 1638 updatestatus(); 1639 /* supporting window for NetWMCheck */ 1640 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0); 1641 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32, 1642 PropModeReplace, (unsigned char *) &wmcheckwin, 1); 1643 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8, 1644 PropModeReplace, (unsigned char *) "dwm", 3); 1645 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32, 1646 PropModeReplace, (unsigned char *) &wmcheckwin, 1); 1647 /* EWMH support per view */ 1648 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, 1649 PropModeReplace, (unsigned char *) netatom, NetLast); 1650 XDeleteProperty(dpy, root, netatom[NetClientList]); 1651 /* select events */ 1652 wa.cursor = cursor[CurNormal]->cursor; 1653 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask 1654 |ButtonPressMask|PointerMotionMask|EnterWindowMask 1655 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask; 1656 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa); 1657 XSelectInput(dpy, root, wa.event_mask); 1658 grabkeys(); 1659 focus(NULL); 1660 } 1661 1662 1663 void 1664 seturgent(Client *c, int urg) 1665 { 1666 XWMHints *wmh; 1667 1668 c->isurgent = urg; 1669 if (!(wmh = XGetWMHints(dpy, c->win))) 1670 return; 1671 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint); 1672 XSetWMHints(dpy, c->win, wmh); 1673 XFree(wmh); 1674 } 1675 1676 void 1677 showhide(Client *c) 1678 { 1679 if (!c) 1680 return; 1681 if (ISVISIBLE(c)) { 1682 /* show clients top down */ 1683 XMoveWindow(dpy, c->win, c->x, c->y); 1684 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen) 1685 resize(c, c->x, c->y, c->w, c->h, 0); 1686 showhide(c->snext); 1687 } else { 1688 /* hide clients bottom up */ 1689 showhide(c->snext); 1690 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y); 1691 } 1692 } 1693 1694 void 1695 sigchld(int unused) 1696 { 1697 if (signal(SIGCHLD, sigchld) == SIG_ERR) 1698 die("can't install SIGCHLD handler:"); 1699 while (0 < waitpid(-1, NULL, WNOHANG)); 1700 } 1701 1702 void 1703 spawn(const Arg *arg) 1704 { 1705 if (arg->v == dmenucmd) 1706 dmenumon[0] = '0' + selmon->num; 1707 if (fork() == 0) { 1708 if (dpy) 1709 close(ConnectionNumber(dpy)); 1710 setsid(); 1711 execvp(((char **)arg->v)[0], (char **)arg->v); 1712 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]); 1713 perror(" failed"); 1714 exit(EXIT_SUCCESS); 1715 } 1716 } 1717 1718 void 1719 tag(const Arg *arg) 1720 { 1721 if (selmon->sel && arg->ui & TAGMASK) { 1722 selmon->sel->tags = arg->ui & TAGMASK; 1723 focus(NULL); 1724 arrange(selmon); 1725 } 1726 } 1727 1728 void 1729 tagmon(const Arg *arg) 1730 { 1731 if (!selmon->sel || !mons->next) 1732 return; 1733 sendmon(selmon->sel, dirtomon(arg->i)); 1734 } 1735 1736 void 1737 tile(Monitor *m) 1738 { 1739 unsigned int i, n, h, mw, my, ty; 1740 Client *c; 1741 1742 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++); 1743 if (n == 0) 1744 return; 1745 1746 if (n > m->nmaster) 1747 mw = m->nmaster ? m->ww * m->mfact : 0; 1748 else 1749 mw = m->ww; 1750 for (i = 0, my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) { 1751 if (n == 1) 1752 c->bw = 0; 1753 else 1754 c->bw = borderpx; 1755 1756 if (i < m->nmaster) { 1757 h = (m->wh - my) / (MIN(n, m->nmaster) - i); 1758 resize(c, m->wx, m->wy, mw - (2*c->bw), h - (2*c->bw), 0); 1759 if (my + HEIGHT(c) < m->wh) 1760 my += HEIGHT(c); 1761 } else { 1762 h = (m->wh - ty) / (n - i); 1763 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0); 1764 if (ty + HEIGHT(c) < m->wh) 1765 ty += HEIGHT(c); 1766 } 1767 } 1768 } 1769 1770 void 1771 togglebar(const Arg *arg) 1772 { 1773 selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar; 1774 updatebarpos(selmon); 1775 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh); 1776 arrange(selmon); 1777 } 1778 1779 void 1780 togglefloating(const Arg *arg) 1781 { 1782 if (!selmon->sel) 1783 return; 1784 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */ 1785 return; 1786 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed; 1787 if (selmon->sel->isfloating) 1788 resize(selmon->sel, selmon->sel->x, selmon->sel->y, 1789 selmon->sel->w, selmon->sel->h, 0); 1790 arrange(selmon); 1791 } 1792 1793 void 1794 toggletag(const Arg *arg) 1795 { 1796 unsigned int newtags; 1797 1798 if (!selmon->sel) 1799 return; 1800 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK); 1801 if (newtags) { 1802 selmon->sel->tags = newtags; 1803 focus(NULL); 1804 arrange(selmon); 1805 } 1806 } 1807 1808 void 1809 toggleview(const Arg *arg) 1810 { 1811 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK); 1812 int i; 1813 1814 if (newtagset) { 1815 selmon->tagset[selmon->seltags] = newtagset; 1816 1817 if (newtagset == ~0) { 1818 selmon->pertag->prevtag = selmon->pertag->curtag; 1819 selmon->pertag->curtag = 0; 1820 } 1821 1822 /* test if the user did not select the same tag */ 1823 if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) { 1824 selmon->pertag->prevtag = selmon->pertag->curtag; 1825 for (i = 0; !(newtagset & 1 << i); i++) ; 1826 selmon->pertag->curtag = i + 1; 1827 } 1828 1829 /* apply settings for this view */ 1830 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag]; 1831 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag]; 1832 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag]; 1833 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; 1834 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1]; 1835 1836 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag]) 1837 togglebar(NULL); 1838 1839 focus(NULL); 1840 arrange(selmon); 1841 } 1842 } 1843 1844 void 1845 unfocus(Client *c, int setfocus) 1846 { 1847 if (!c) 1848 return; 1849 grabbuttons(c, 0); 1850 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel); 1851 if (setfocus) { 1852 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); 1853 XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 1854 } 1855 } 1856 1857 void 1858 unmanage(Client *c, int destroyed) 1859 { 1860 Monitor *m = c->mon; 1861 XWindowChanges wc; 1862 1863 detach(c); 1864 detachstack(c); 1865 if (!destroyed) { 1866 wc.border_width = c->oldbw; 1867 XGrabServer(dpy); /* avoid race conditions */ 1868 XSetErrorHandler(xerrordummy); 1869 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */ 1870 XUngrabButton(dpy, AnyButton, AnyModifier, c->win); 1871 setclientstate(c, WithdrawnState); 1872 XSync(dpy, False); 1873 XSetErrorHandler(xerror); 1874 XUngrabServer(dpy); 1875 } 1876 free(c); 1877 focus(NULL); 1878 updateclientlist(); 1879 arrange(m); 1880 } 1881 1882 void 1883 unmapnotify(XEvent *e) 1884 { 1885 Client *c; 1886 XUnmapEvent *ev = &e->xunmap; 1887 1888 if ((c = wintoclient(ev->window))) { 1889 if (ev->send_event) 1890 setclientstate(c, WithdrawnState); 1891 else 1892 unmanage(c, 0); 1893 } 1894 } 1895 1896 void 1897 updatebars(void) 1898 { 1899 Monitor *m; 1900 XSetWindowAttributes wa = { 1901 .override_redirect = True, 1902 .background_pixmap = ParentRelative, 1903 .event_mask = ButtonPressMask|ExposureMask 1904 }; 1905 XClassHint ch = {"dwm", "dwm"}; 1906 for (m = mons; m; m = m->next) { 1907 if (m->barwin) 1908 continue; 1909 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen), 1910 CopyFromParent, DefaultVisual(dpy, screen), 1911 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa); 1912 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor); 1913 XMapRaised(dpy, m->barwin); 1914 XSetClassHint(dpy, m->barwin, &ch); 1915 } 1916 } 1917 1918 void 1919 updatebarpos(Monitor *m) 1920 { 1921 m->wy = m->my; 1922 m->wh = m->mh; 1923 if (m->showbar) { 1924 m->wh -= bh; 1925 m->by = m->topbar ? m->wy : m->wy + m->wh; 1926 m->wy = m->topbar ? m->wy + bh : m->wy; 1927 } else 1928 m->by = -bh; 1929 } 1930 1931 void 1932 updateclientlist() 1933 { 1934 Client *c; 1935 Monitor *m; 1936 1937 XDeleteProperty(dpy, root, netatom[NetClientList]); 1938 for (m = mons; m; m = m->next) 1939 for (c = m->clients; c; c = c->next) 1940 XChangeProperty(dpy, root, netatom[NetClientList], 1941 XA_WINDOW, 32, PropModeAppend, 1942 (unsigned char *) &(c->win), 1); 1943 } 1944 1945 int 1946 updategeom(void) 1947 { 1948 int dirty = 0; 1949 1950 #ifdef XINERAMA 1951 if (XineramaIsActive(dpy)) { 1952 int i, j, n, nn; 1953 Client *c; 1954 Monitor *m; 1955 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn); 1956 XineramaScreenInfo *unique = NULL; 1957 1958 for (n = 0, m = mons; m; m = m->next, n++); 1959 /* only consider unique geometries as separate screens */ 1960 unique = ecalloc(nn, sizeof(XineramaScreenInfo)); 1961 for (i = 0, j = 0; i < nn; i++) 1962 if (isuniquegeom(unique, j, &info[i])) 1963 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo)); 1964 XFree(info); 1965 nn = j; 1966 if (n <= nn) { /* new monitors available */ 1967 for (i = 0; i < (nn - n); i++) { 1968 for (m = mons; m && m->next; m = m->next); 1969 if (m) 1970 m->next = createmon(); 1971 else 1972 mons = createmon(); 1973 } 1974 for (i = 0, m = mons; i < nn && m; m = m->next, i++) 1975 if (i >= n 1976 || unique[i].x_org != m->mx || unique[i].y_org != m->my 1977 || unique[i].width != m->mw || unique[i].height != m->mh) 1978 { 1979 dirty = 1; 1980 m->num = i; 1981 m->mx = m->wx = unique[i].x_org; 1982 m->my = m->wy = unique[i].y_org; 1983 m->mw = m->ww = unique[i].width; 1984 m->mh = m->wh = unique[i].height; 1985 updatebarpos(m); 1986 } 1987 } else { /* less monitors available nn < n */ 1988 for (i = nn; i < n; i++) { 1989 for (m = mons; m && m->next; m = m->next); 1990 while ((c = m->clients)) { 1991 dirty = 1; 1992 m->clients = c->next; 1993 detachstack(c); 1994 c->mon = mons; 1995 attach(c); 1996 attachstack(c); 1997 } 1998 if (m == selmon) 1999 selmon = mons; 2000 cleanupmon(m); 2001 } 2002 } 2003 free(unique); 2004 } else 2005 #endif /* XINERAMA */ 2006 { /* default monitor setup */ 2007 if (!mons) 2008 mons = createmon(); 2009 if (mons->mw != sw || mons->mh != sh) { 2010 dirty = 1; 2011 mons->mw = mons->ww = sw; 2012 mons->mh = mons->wh = sh; 2013 updatebarpos(mons); 2014 } 2015 } 2016 if (dirty) { 2017 selmon = mons; 2018 selmon = wintomon(root); 2019 } 2020 return dirty; 2021 } 2022 2023 void 2024 updatenumlockmask(void) 2025 { 2026 unsigned int i, j; 2027 XModifierKeymap *modmap; 2028 2029 numlockmask = 0; 2030 modmap = XGetModifierMapping(dpy); 2031 for (i = 0; i < 8; i++) 2032 for (j = 0; j < modmap->max_keypermod; j++) 2033 if (modmap->modifiermap[i * modmap->max_keypermod + j] 2034 == XKeysymToKeycode(dpy, XK_Num_Lock)) 2035 numlockmask = (1 << i); 2036 XFreeModifiermap(modmap); 2037 } 2038 2039 void 2040 updatesizehints(Client *c) 2041 { 2042 long msize; 2043 XSizeHints size; 2044 2045 if (!XGetWMNormalHints(dpy, c->win, &size, &msize)) 2046 /* size is uninitialized, ensure that size.flags aren't used */ 2047 size.flags = PSize; 2048 if (size.flags & PBaseSize) { 2049 c->basew = size.base_width; 2050 c->baseh = size.base_height; 2051 } else if (size.flags & PMinSize) { 2052 c->basew = size.min_width; 2053 c->baseh = size.min_height; 2054 } else 2055 c->basew = c->baseh = 0; 2056 if (size.flags & PResizeInc) { 2057 c->incw = size.width_inc; 2058 c->inch = size.height_inc; 2059 } else 2060 c->incw = c->inch = 0; 2061 if (size.flags & PMaxSize) { 2062 c->maxw = size.max_width; 2063 c->maxh = size.max_height; 2064 } else 2065 c->maxw = c->maxh = 0; 2066 if (size.flags & PMinSize) { 2067 c->minw = size.min_width; 2068 c->minh = size.min_height; 2069 } else if (size.flags & PBaseSize) { 2070 c->minw = size.base_width; 2071 c->minh = size.base_height; 2072 } else 2073 c->minw = c->minh = 0; 2074 if (size.flags & PAspect) { 2075 c->mina = (float)size.min_aspect.y / size.min_aspect.x; 2076 c->maxa = (float)size.max_aspect.x / size.max_aspect.y; 2077 } else 2078 c->maxa = c->mina = 0.0; 2079 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh); 2080 } 2081 2082 void 2083 updatestatus(void) 2084 { 2085 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext))) 2086 strcpy(stext, "dwm-"VERSION); 2087 drawbar(selmon); 2088 } 2089 2090 void 2091 updatetitle(Client *c) 2092 { 2093 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name)) 2094 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name); 2095 if (c->name[0] == '\0') /* hack to mark broken clients */ 2096 strcpy(c->name, broken); 2097 } 2098 2099 void 2100 updatewindowtype(Client *c) 2101 { 2102 Atom state = getatomprop(c, netatom[NetWMState]); 2103 Atom wtype = getatomprop(c, netatom[NetWMWindowType]); 2104 2105 if (state == netatom[NetWMFullscreen]) 2106 setfullscreen(c, 1); 2107 if (wtype == netatom[NetWMWindowTypeDialog]) 2108 c->isfloating = 1; 2109 } 2110 2111 void 2112 updatewmhints(Client *c) 2113 { 2114 XWMHints *wmh; 2115 2116 if ((wmh = XGetWMHints(dpy, c->win))) { 2117 if (c == selmon->sel && wmh->flags & XUrgencyHint) { 2118 wmh->flags &= ~XUrgencyHint; 2119 XSetWMHints(dpy, c->win, wmh); 2120 } else 2121 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0; 2122 if (wmh->flags & InputHint) 2123 c->neverfocus = !wmh->input; 2124 else 2125 c->neverfocus = 0; 2126 XFree(wmh); 2127 } 2128 } 2129 2130 void 2131 view(const Arg *arg) 2132 { 2133 int i; 2134 unsigned int tmptag; 2135 2136 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags]) 2137 return; 2138 selmon->seltags ^= 1; /* toggle sel tagset */ 2139 if (arg->ui & TAGMASK) { 2140 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK; 2141 selmon->pertag->prevtag = selmon->pertag->curtag; 2142 2143 if (arg->ui == ~0) 2144 selmon->pertag->curtag = 0; 2145 else { 2146 for (i = 0; !(arg->ui & 1 << i); i++) ; 2147 selmon->pertag->curtag = i + 1; 2148 } 2149 } else { 2150 tmptag = selmon->pertag->prevtag; 2151 selmon->pertag->prevtag = selmon->pertag->curtag; 2152 selmon->pertag->curtag = tmptag; 2153 } 2154 2155 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag]; 2156 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag]; 2157 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag]; 2158 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; 2159 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1]; 2160 2161 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag]) 2162 togglebar(NULL); 2163 2164 focus(NULL); 2165 arrange(selmon); 2166 } 2167 2168 Client * 2169 wintoclient(Window w) 2170 { 2171 Client *c; 2172 Monitor *m; 2173 2174 for (m = mons; m; m = m->next) 2175 for (c = m->clients; c; c = c->next) 2176 if (c->win == w) 2177 return c; 2178 return NULL; 2179 } 2180 2181 Monitor * 2182 wintomon(Window w) 2183 { 2184 int x, y; 2185 Client *c; 2186 Monitor *m; 2187 2188 if (w == root && getrootptr(&x, &y)) 2189 return recttomon(x, y, 1, 1); 2190 for (m = mons; m; m = m->next) 2191 if (w == m->barwin) 2192 return m; 2193 if ((c = wintoclient(w))) 2194 return c->mon; 2195 return selmon; 2196 } 2197 2198 /* There's no way to check accesses to destroyed windows, thus those cases are 2199 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs 2200 * default error handler, which may call exit. */ 2201 int 2202 xerror(Display *dpy, XErrorEvent *ee) 2203 { 2204 if (ee->error_code == BadWindow 2205 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch) 2206 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable) 2207 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable) 2208 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable) 2209 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch) 2210 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess) 2211 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess) 2212 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable)) 2213 return 0; 2214 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n", 2215 ee->request_code, ee->error_code); 2216 return xerrorxlib(dpy, ee); /* may call exit */ 2217 } 2218 2219 int 2220 xerrordummy(Display *dpy, XErrorEvent *ee) 2221 { 2222 return 0; 2223 } 2224 2225 /* Startup Error handler to check if another window manager 2226 * is already running. */ 2227 int 2228 xerrorstart(Display *dpy, XErrorEvent *ee) 2229 { 2230 die("dwm: another window manager is already running"); 2231 return -1; 2232 } 2233 2234 void 2235 zoom(const Arg *arg) 2236 { 2237 Client *c = selmon->sel; 2238 2239 if (!selmon->lt[selmon->sellt]->arrange 2240 || (selmon->sel && selmon->sel->isfloating)) 2241 return; 2242 if (c == nexttiled(selmon->clients)) 2243 if (!c || !(c = nexttiled(c->next))) 2244 return; 2245 pop(c); 2246 } 2247 2248 int 2249 main(int argc, char *argv[]) 2250 { 2251 if (argc == 2 && !strcmp("-v", argv[1])) 2252 die("dwm-"VERSION); 2253 else if (argc != 1) 2254 die("usage: dwm [-v]"); 2255 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) 2256 fputs("warning: no locale support\n", stderr); 2257 if (!(dpy = XOpenDisplay(NULL))) 2258 die("dwm: cannot open display"); 2259 checkotherwm(); 2260 setup(); 2261 #ifdef __OpenBSD__ 2262 if (pledge("stdio rpath proc exec", NULL) == -1) 2263 die("pledge"); 2264 #endif /* __OpenBSD__ */ 2265 scan(); 2266 run(); 2267 cleanup(); 2268 XCloseDisplay(dpy); 2269 return EXIT_SUCCESS; 2270 }