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