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