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