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