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