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