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