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