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