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