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