dwm.c (70046B)
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 #include <X11/Xresource.h> 40 #ifdef XINERAMA 41 #include <X11/extensions/Xinerama.h> 42 #endif /* XINERAMA */ 43 #include <X11/Xft/Xft.h> 44 #include <X11/Xlib-xcb.h> 45 #include <xcb/res.h> 46 #ifdef __OpenBSD__ 47 #include <sys/sysctl.h> 48 #include <kvm.h> 49 #endif /* __OpenBSD */ 50 51 #include "drw.h" 52 #include "util.h" 53 54 /* macros */ 55 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) 56 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask)) 57 #define GETINC(X) ((X) - 2000) 58 #define INC(X) ((X) + 2000) 59 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \ 60 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy))) 61 #define ISINC(X) ((X) > 1000 && (X) < 3000) 62 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]) || C->issticky) 63 #define PREVSEL 3000 64 #define LENGTH(X) (sizeof X / sizeof X[0]) 65 #define MOD(N,M) ((N)%(M) < 0 ? (N)%(M) + (M) : (N)%(M)) 66 #define MOUSEMASK (BUTTONMASK|PointerMotionMask) 67 #define WIDTH(X) ((X)->w + 2 * (X)->bw) 68 #define HEIGHT(X) ((X)->h + 2 * (X)->bw) 69 #define TAGMASK ((1 << LENGTH(tags)) - 1) 70 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 71 #define TRUNC(X,A,B) (MAX((A), MIN((X), (B)))) 72 73 /* enums */ 74 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ 75 enum { SchemeNorm, SchemeSel }; /* color schemes */ 76 enum { NetSupported, NetWMName, NetWMState, NetWMCheck, 77 NetWMFullscreen, NetActiveWindow, NetWMWindowType, 78 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */ 79 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */ 80 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, 81 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */ 82 83 typedef union { 84 int i; 85 unsigned int ui; 86 float f; 87 const void *v; 88 } Arg; 89 90 typedef struct { 91 unsigned int click; 92 unsigned int mask; 93 unsigned int button; 94 void (*func)(const Arg *arg); 95 const Arg arg; 96 } Button; 97 98 typedef struct Monitor Monitor; 99 typedef struct Client Client; 100 struct Client { 101 char name[256]; 102 float mina, maxa; 103 int x, y, w, h; 104 int oldx, oldy, oldw, oldh; 105 int basew, baseh, incw, inch, maxw, maxh, minw, minh; 106 int bw, oldbw; 107 unsigned int tags; 108 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen, isterminal, noswallow, issticky; 109 int fakefullscreen; 110 pid_t pid; 111 Client *next; 112 Client *snext; 113 Client *swallowing; 114 Monitor *mon; 115 Window win; 116 }; 117 118 typedef struct { 119 unsigned int mod; 120 KeySym keysym; 121 void (*func)(const Arg *); 122 const Arg arg; 123 } Key; 124 125 typedef struct { 126 const char *symbol; 127 void (*arrange)(Monitor *); 128 } Layout; 129 130 typedef struct Pertag Pertag; 131 struct Monitor { 132 char ltsymbol[16]; 133 float mfact; 134 int nmaster; 135 int num; 136 int by; /* bar geometry */ 137 int mx, my, mw, mh; /* screen size */ 138 int wx, wy, ww, wh; /* window area */ 139 int gappih; /* horizontal gap between windows */ 140 int gappiv; /* vertical gap between windows */ 141 int gappoh; /* horizontal outer gaps */ 142 int gappov; /* vertical outer gaps */ 143 unsigned int seltags; 144 unsigned int sellt; 145 unsigned int tagset[2]; 146 int showbar; 147 int topbar; 148 Client *clients; 149 Client *sel; 150 Client *stack; 151 Monitor *next; 152 Window barwin; 153 const Layout *lt[2]; 154 Pertag *pertag; 155 }; 156 157 typedef struct { 158 const char *class; 159 const char *instance; 160 const char *title; 161 unsigned int tags; 162 int isfloating; 163 int isterminal; 164 int noswallow; 165 int monitor; 166 } Rule; 167 168 /* Xresources preferences */ 169 enum resource_type { 170 STRING = 0, 171 INTEGER = 1, 172 FLOAT = 2 173 }; 174 175 typedef struct { 176 char *name; 177 enum resource_type type; 178 void *dst; 179 } ResourcePref; 180 181 /* function declarations */ 182 static void applyrules(Client *c); 183 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact); 184 static void arrange(Monitor *m); 185 static void arrangemon(Monitor *m); 186 static void attach(Client *c); 187 static void attachstack(Client *c); 188 static void buttonpress(XEvent *e); 189 static void checkotherwm(void); 190 static void cleanup(void); 191 static void cleanupmon(Monitor *mon); 192 static void clientmessage(XEvent *e); 193 static void configure(Client *c); 194 static void configurenotify(XEvent *e); 195 static void configurerequest(XEvent *e); 196 static Monitor *createmon(void); 197 static void destroynotify(XEvent *e); 198 static void detach(Client *c); 199 static void detachstack(Client *c); 200 static Monitor *dirtomon(int dir); 201 static void drawbar(Monitor *m); 202 static void drawbars(void); 203 static void enternotify(XEvent *e); 204 static void expose(XEvent *e); 205 static void focus(Client *c); 206 static void focusin(XEvent *e); 207 static void focusmon(const Arg *arg); 208 static void focusstack(const Arg *arg); 209 static Atom getatomprop(Client *c, Atom prop); 210 static int getrootptr(int *x, int *y); 211 static long getstate(Window w); 212 static pid_t getstatusbarpid(); 213 static int gettextprop(Window w, Atom atom, char *text, unsigned int size); 214 static void grabbuttons(Client *c, int focused); 215 static void grabkeys(void); 216 static void incnmaster(const Arg *arg); 217 static void keypress(XEvent *e); 218 static void killclient(const Arg *arg); 219 static void manage(Window w, XWindowAttributes *wa); 220 static void mappingnotify(XEvent *e); 221 static void maprequest(XEvent *e); 222 static void monocle(Monitor *m); 223 static void motionnotify(XEvent *e); 224 static void movemouse(const Arg *arg); 225 static Client *nexttiled(Client *c); 226 static void pop(Client *); 227 static void propertynotify(XEvent *e); 228 static void pushstack(const Arg *arg); 229 static void quit(const Arg *arg); 230 static Monitor *recttomon(int x, int y, int w, int h); 231 static void resize(Client *c, int x, int y, int w, int h, int interact); 232 static void resizeclient(Client *c, int x, int y, int w, int h); 233 static void resizemouse(const Arg *arg); 234 static void restack(Monitor *m); 235 static void run(void); 236 static void scan(void); 237 static int sendevent(Client *c, Atom proto); 238 static void sendmon(Client *c, Monitor *m); 239 static void setclientstate(Client *c, long state); 240 static void setfocus(Client *c); 241 static void setfullscreen(Client *c, int fullscreen); 242 static void setlayout(const Arg *arg); 243 static void setmfact(const Arg *arg); 244 static void setup(void); 245 static void seturgent(Client *c, int urg); 246 static void showhide(Client *c); 247 static void sigchld(int unused); 248 static void sigstatusbar(const Arg *arg); 249 static void spawn(const Arg *arg); 250 static int stackpos(const Arg *arg); 251 static void tag(const Arg *arg); 252 static void tagmon(const Arg *arg); 253 static void togglebar(const Arg *arg); 254 static void togglefakefullscreen(const Arg *arg); 255 static void togglefloating(const Arg *arg); 256 static void togglesticky(const Arg *arg); 257 static void togglescratch(const Arg *arg); 258 static void togglefullscr(const Arg *arg); 259 static void toggletag(const Arg *arg); 260 static void toggletagscratch(const Arg *arg); 261 static void toggleview(const Arg *arg); 262 static void unfocus(Client *c, int setfocus); 263 static void unmanage(Client *c, int destroyed); 264 static void unmapnotify(XEvent *e); 265 static void updatebarpos(Monitor *m); 266 static void updatebars(void); 267 static void updateclientlist(void); 268 static int updategeom(void); 269 static void updatenumlockmask(void); 270 static void updatesizehints(Client *c); 271 static void updatestatus(void); 272 static void updatetitle(Client *c); 273 static void updatewindowtype(Client *c); 274 static void updatewmhints(Client *c); 275 static void view(const Arg *arg); 276 static Client *wintoclient(Window w); 277 static Monitor *wintomon(Window w); 278 static int xerror(Display *dpy, XErrorEvent *ee); 279 static int xerrordummy(Display *dpy, XErrorEvent *ee); 280 static int xerrorstart(Display *dpy, XErrorEvent *ee); 281 static void zoom(const Arg *arg); 282 static void load_xresources(void); 283 static void livereload_xresources(const Arg *arg); 284 static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst); 285 static void jumptotag(const Arg *arg); 286 287 static pid_t getparentprocess(pid_t p); 288 static int isdescprocess(pid_t p, pid_t c); 289 static Client *swallowingclient(Window w); 290 static Client *termforwin(const Client *c); 291 static pid_t winpid(Window w); 292 293 /* variables */ 294 static const char broken[] = "broken"; 295 static char stext[256]; 296 static int statusw; 297 static int statussig; 298 static pid_t statuspid = -1; 299 static int screen; 300 static int sw, sh; /* X display screen geometry width, height */ 301 static int bh, blw = 0; /* bar geometry */ 302 static int lrpad; /* sum of left and right padding for text */ 303 static int (*xerrorxlib)(Display *, XErrorEvent *); 304 static unsigned int numlockmask = 0; 305 static void (*handler[LASTEvent]) (XEvent *) = { 306 [ButtonPress] = buttonpress, 307 [ClientMessage] = clientmessage, 308 [ConfigureRequest] = configurerequest, 309 [ConfigureNotify] = configurenotify, 310 [DestroyNotify] = destroynotify, 311 [EnterNotify] = enternotify, 312 [Expose] = expose, 313 [FocusIn] = focusin, 314 [KeyPress] = keypress, 315 [MappingNotify] = mappingnotify, 316 [MapRequest] = maprequest, 317 [MotionNotify] = motionnotify, 318 [PropertyNotify] = propertynotify, 319 [UnmapNotify] = unmapnotify 320 }; 321 static Atom wmatom[WMLast], netatom[NetLast]; 322 static int running = 1; 323 static Cur *cursor[CurLast]; 324 static Clr **scheme; 325 static Display *dpy; 326 static Drw *drw; 327 static Monitor *mons, *selmon; 328 static Window root, wmcheckwin; 329 330 static xcb_connection_t *xcon; 331 332 /* configuration, allows nested code to access above variables */ 333 #include "config.h" 334 335 struct Pertag { 336 unsigned int curtag, prevtag; /* current and previous tag */ 337 int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */ 338 float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */ 339 unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */ 340 const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes */ 341 int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */ 342 }; 343 344 static unsigned int scratchtag = 1 << LENGTH(tags); 345 346 /* compile-time check if all tags fit into an unsigned int bit array. */ 347 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; }; 348 349 static unsigned int swallow_next = 1; 350 351 /* function implementations */ 352 void 353 applyrules(Client *c) 354 { 355 const char *class, *instance; 356 unsigned int i; 357 const Rule *r; 358 Monitor *m; 359 XClassHint ch = { NULL, NULL }; 360 361 /* rule matching */ 362 c->isfloating = 0; 363 c->tags = 0; 364 XGetClassHint(dpy, c->win, &ch); 365 class = ch.res_class ? ch.res_class : broken; 366 instance = ch.res_name ? ch.res_name : broken; 367 368 int longest_rule_match = 0; 369 370 for (i = 0; i < LENGTH(rules); i++) { 371 r = &rules[i]; 372 373 int rule_title_len = 0; 374 if (r->title) 375 rule_title_len = strlen(r->title); 376 377 if ((!r->title || (strstr(c->name, r->title) && (rule_title_len > longest_rule_match))) 378 && (!r->class || strstr(class, r->class)) 379 && (!r->instance || strstr(instance, r->instance))) 380 { 381 if (r->title) 382 longest_rule_match = rule_title_len; 383 c->isterminal = r->isterminal; 384 c->noswallow = r->noswallow; 385 c->isfloating = r->isfloating; 386 c->tags = r->tags; // I don't want the rules to be additive 387 for (m = mons; m && m->num != r->monitor; m = m->next); 388 if (m) 389 c->mon = m; 390 } 391 } 392 if (ch.res_class) 393 XFree(ch.res_class); 394 if (ch.res_name) 395 XFree(ch.res_name); 396 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags]; 397 } 398 399 int 400 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) 401 { 402 int baseismin; 403 Monitor *m = c->mon; 404 405 /* set minimum possible */ 406 *w = MAX(1, *w); 407 *h = MAX(1, *h); 408 if (interact) { 409 if (*x > sw) 410 *x = sw - WIDTH(c); 411 if (*y > sh) 412 *y = sh - HEIGHT(c); 413 if (*x + *w + 2 * c->bw < 0) 414 *x = 0; 415 if (*y + *h + 2 * c->bw < 0) 416 *y = 0; 417 } else { 418 if (*x >= m->wx + m->ww) 419 *x = m->wx + m->ww - WIDTH(c); 420 if (*y >= m->wy + m->wh) 421 *y = m->wy + m->wh - HEIGHT(c); 422 if (*x + *w + 2 * c->bw <= m->wx) 423 *x = m->wx; 424 if (*y + *h + 2 * c->bw <= m->wy) 425 *y = m->wy; 426 } 427 if (*h < bh) 428 *h = bh; 429 if (*w < bh) 430 *w = bh; 431 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) { 432 /* see last two sentences in ICCCM 4.1.2.3 */ 433 baseismin = c->basew == c->minw && c->baseh == c->minh; 434 if (!baseismin) { /* temporarily remove base dimensions */ 435 *w -= c->basew; 436 *h -= c->baseh; 437 } 438 /* adjust for aspect limits */ 439 if (c->mina > 0 && c->maxa > 0) { 440 if (c->maxa < (float)*w / *h) 441 *w = *h * c->maxa + 0.5; 442 else if (c->mina < (float)*h / *w) 443 *h = *w * c->mina + 0.5; 444 } 445 if (baseismin) { /* increment calculation requires this */ 446 *w -= c->basew; 447 *h -= c->baseh; 448 } 449 /* adjust for increment value */ 450 if (c->incw) 451 *w -= *w % c->incw; 452 if (c->inch) 453 *h -= *h % c->inch; 454 /* restore base dimensions */ 455 *w = MAX(*w + c->basew, c->minw); 456 *h = MAX(*h + c->baseh, c->minh); 457 if (c->maxw) 458 *w = MIN(*w, c->maxw); 459 if (c->maxh) 460 *h = MIN(*h, c->maxh); 461 } 462 return *x != c->x || *y != c->y || *w != c->w || *h != c->h; 463 } 464 465 void 466 arrange(Monitor *m) 467 { 468 if (m) 469 showhide(m->stack); 470 else for (m = mons; m; m = m->next) 471 showhide(m->stack); 472 if (m) { 473 arrangemon(m); 474 restack(m); 475 } else for (m = mons; m; m = m->next) 476 arrangemon(m); 477 } 478 479 void 480 arrangemon(Monitor *m) 481 { 482 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol); 483 if (m->lt[m->sellt]->arrange) 484 m->lt[m->sellt]->arrange(m); 485 } 486 487 void 488 attach(Client *c) 489 { 490 c->next = c->mon->clients; 491 c->mon->clients = c; 492 } 493 494 void 495 attachstack(Client *c) 496 { 497 c->snext = c->mon->stack; 498 c->mon->stack = c; 499 } 500 501 void 502 swallow(Client *p, Client *c) 503 { 504 if (!swallow_next) { 505 swallow_next = 1; 506 return; 507 } 508 509 if (c->noswallow || c->isterminal) 510 return; 511 if (c->noswallow && !swallowfloating && c->isfloating) 512 return; 513 514 detach(c); 515 detachstack(c); 516 517 setclientstate(c, WithdrawnState); 518 XUnmapWindow(dpy, p->win); 519 520 p->swallowing = c; 521 c->mon = p->mon; 522 523 Window w = p->win; 524 p->win = c->win; 525 c->win = w; 526 updatetitle(p); 527 XMoveResizeWindow(dpy, p->win, p->x, p->y, p->w, p->h); 528 arrange(p->mon); 529 configure(p); 530 updateclientlist(); 531 } 532 533 void 534 unswallow(Client *c) 535 { 536 c->win = c->swallowing->win; 537 538 free(c->swallowing); 539 c->swallowing = NULL; 540 541 /* unfullscreen the client */ 542 setfullscreen(c, 0); 543 updatetitle(c); 544 arrange(c->mon); 545 XMapWindow(dpy, c->win); 546 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); 547 setclientstate(c, NormalState); 548 focus(NULL); 549 arrange(c->mon); 550 } 551 552 void 553 buttonpress(XEvent *e) 554 { 555 unsigned int i, x, click, occ = 0; 556 Arg arg = {0}; 557 Client *c; 558 Monitor *m; 559 XButtonPressedEvent *ev = &e->xbutton; 560 char *text, *s, ch; 561 562 click = ClkRootWin; 563 /* focus monitor if necessary */ 564 if ((m = wintomon(ev->window)) && m != selmon) { 565 unfocus(selmon->sel, 1); 566 selmon = m; 567 focus(NULL); 568 } 569 if (ev->window == selmon->barwin) { 570 i = x = 0; 571 for (c = m->clients; c; c = c->next) 572 occ |= c->tags == 255 ? 0 : c->tags; 573 do { 574 /* do not reserve space for vacant tags */ 575 if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i)) 576 continue; 577 x += TEXTW(tags[i]); 578 } while (ev->x >= x && ++i < LENGTH(tags)); 579 if (i < LENGTH(tags)) { 580 click = ClkTagBar; 581 arg.ui = 1 << i; 582 } else if (ev->x < x + blw) 583 click = ClkLtSymbol; 584 else if (ev->x > selmon->ww - statusw) { 585 x = selmon->ww - statusw; 586 click = ClkStatusText; 587 statussig = 0; 588 for (text = s = stext; *s && x <= ev->x; s++) { 589 if ((unsigned char)(*s) < ' ') { 590 ch = *s; 591 *s = '\0'; 592 x += TEXTW(text) - lrpad; 593 *s = ch; 594 text = s + 1; 595 if (x >= ev->x) 596 break; 597 statussig = ch; 598 } 599 } 600 } else 601 click = ClkWinTitle; 602 } else if ((c = wintoclient(ev->window))) { 603 focus(c); 604 restack(selmon); 605 XAllowEvents(dpy, ReplayPointer, CurrentTime); 606 click = ClkClientWin; 607 } 608 for (i = 0; i < LENGTH(buttons); i++) 609 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button 610 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) 611 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg); 612 } 613 614 void 615 checkotherwm(void) 616 { 617 xerrorxlib = XSetErrorHandler(xerrorstart); 618 /* this causes an error if some other window manager is running */ 619 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask); 620 XSync(dpy, False); 621 XSetErrorHandler(xerror); 622 XSync(dpy, False); 623 } 624 625 void 626 cleanup(void) 627 { 628 Arg a = {.ui = ~0}; 629 Layout foo = { "", NULL }; 630 Monitor *m; 631 size_t i; 632 633 view(&a); 634 selmon->lt[selmon->sellt] = &foo; 635 for (m = mons; m; m = m->next) 636 while (m->stack) 637 unmanage(m->stack, 0); 638 XUngrabKey(dpy, AnyKey, AnyModifier, root); 639 while (mons) 640 cleanupmon(mons); 641 for (i = 0; i < CurLast; i++) 642 drw_cur_free(drw, cursor[i]); 643 for (i = 0; i < LENGTH(colors); i++) 644 free(scheme[i]); 645 XDestroyWindow(dpy, wmcheckwin); 646 drw_free(drw); 647 XSync(dpy, False); 648 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); 649 XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 650 } 651 652 void 653 cleanupmon(Monitor *mon) 654 { 655 Monitor *m; 656 657 if (mon == mons) 658 mons = mons->next; 659 else { 660 for (m = mons; m && m->next != mon; m = m->next); 661 m->next = mon->next; 662 } 663 XUnmapWindow(dpy, mon->barwin); 664 XDestroyWindow(dpy, mon->barwin); 665 free(mon); 666 } 667 668 void 669 clientmessage(XEvent *e) 670 { 671 XClientMessageEvent *cme = &e->xclient; 672 Client *c = wintoclient(cme->window); 673 unsigned int i; 674 675 if (!c) 676 return; 677 if (cme->message_type == netatom[NetWMState]) { 678 if (cme->data.l[1] == netatom[NetWMFullscreen] 679 || cme->data.l[2] == netatom[NetWMFullscreen]) 680 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */ 681 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen))); 682 } else if (cme->message_type == netatom[NetActiveWindow]) { 683 for (i = 0; i < LENGTH(tags) && !((1 << i) & c->tags); i++); 684 if (i < LENGTH(tags)) { 685 const Arg a = {.ui = 1 << i}; 686 selmon = c->mon; 687 view(&a); 688 focus(c); 689 restack(selmon); 690 } 691 } 692 } 693 694 void 695 configure(Client *c) 696 { 697 XConfigureEvent ce; 698 699 ce.type = ConfigureNotify; 700 ce.display = dpy; 701 ce.event = c->win; 702 ce.window = c->win; 703 ce.x = c->x; 704 ce.y = c->y; 705 ce.width = c->w; 706 ce.height = c->h; 707 ce.border_width = c->bw; 708 ce.above = None; 709 ce.override_redirect = False; 710 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce); 711 } 712 713 void 714 configurenotify(XEvent *e) 715 { 716 Monitor *m; 717 Client *c; 718 XConfigureEvent *ev = &e->xconfigure; 719 int dirty; 720 721 /* TODO: updategeom handling sucks, needs to be simplified */ 722 if (ev->window == root) { 723 dirty = (sw != ev->width || sh != ev->height); 724 sw = ev->width; 725 sh = ev->height; 726 if (updategeom() || dirty) { 727 drw_resize(drw, sw, bh); 728 updatebars(); 729 for (m = mons; m; m = m->next) { 730 for (c = m->clients; c; c = c->next) 731 if (c->isfullscreen && c->fakefullscreen != 1) 732 resizeclient(c, m->mx, m->my, m->mw, m->mh); 733 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh); 734 } 735 focus(NULL); 736 arrange(NULL); 737 } 738 } 739 } 740 741 void 742 configurerequest(XEvent *e) 743 { 744 Client *c; 745 Monitor *m; 746 XConfigureRequestEvent *ev = &e->xconfigurerequest; 747 XWindowChanges wc; 748 749 if ((c = wintoclient(ev->window))) { 750 if (ev->value_mask & CWBorderWidth) 751 c->bw = ev->border_width; 752 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) { 753 m = c->mon; 754 if (ev->value_mask & CWX) { 755 c->oldx = c->x; 756 c->x = m->mx + ev->x; 757 } 758 if (ev->value_mask & CWY) { 759 c->oldy = c->y; 760 c->y = m->my + ev->y; 761 } 762 if (ev->value_mask & CWWidth) { 763 c->oldw = c->w; 764 c->w = ev->width; 765 } 766 if (ev->value_mask & CWHeight) { 767 c->oldh = c->h; 768 c->h = ev->height; 769 } 770 if ((c->x + c->w) > m->mx + m->mw && c->isfloating) 771 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */ 772 if ((c->y + c->h) > m->my + m->mh && c->isfloating) 773 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */ 774 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight))) 775 configure(c); 776 if (ISVISIBLE(c)) 777 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); 778 } else 779 configure(c); 780 } else { 781 wc.x = ev->x; 782 wc.y = ev->y; 783 wc.width = ev->width; 784 wc.height = ev->height; 785 wc.border_width = ev->border_width; 786 wc.sibling = ev->above; 787 wc.stack_mode = ev->detail; 788 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc); 789 } 790 XSync(dpy, False); 791 } 792 793 Monitor * 794 createmon(void) 795 { 796 Monitor *m; 797 unsigned int i; 798 799 m = ecalloc(1, sizeof(Monitor)); 800 m->tagset[0] = m->tagset[1] = 1; 801 m->mfact = mfact; 802 m->nmaster = nmaster; 803 m->showbar = showbar; 804 m->topbar = topbar; 805 m->gappih = gappih; 806 m->gappiv = gappiv; 807 m->gappoh = gappoh; 808 m->gappov = gappov; 809 m->lt[0] = &layouts[0]; 810 m->lt[1] = &layouts[1 % LENGTH(layouts)]; 811 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol); 812 m->pertag = ecalloc(1, sizeof(Pertag)); 813 m->pertag->curtag = m->pertag->prevtag = 1; 814 815 for (i = 0; i <= LENGTH(tags); i++) { 816 m->pertag->nmasters[i] = m->nmaster; 817 m->pertag->mfacts[i] = m->mfact; 818 819 m->pertag->ltidxs[i][0] = m->lt[0]; 820 m->pertag->ltidxs[i][1] = m->lt[1]; 821 m->pertag->sellts[i] = m->sellt; 822 823 m->pertag->showbars[i] = m->showbar; 824 } 825 826 return m; 827 } 828 829 void 830 destroynotify(XEvent *e) 831 { 832 Client *c; 833 XDestroyWindowEvent *ev = &e->xdestroywindow; 834 835 if ((c = wintoclient(ev->window))) 836 unmanage(c, 1); 837 838 else if ((c = swallowingclient(ev->window))) 839 unmanage(c->swallowing, 1); 840 } 841 842 void 843 detach(Client *c) 844 { 845 Client **tc; 846 847 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next); 848 *tc = c->next; 849 } 850 851 void 852 detachstack(Client *c) 853 { 854 Client **tc, *t; 855 856 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext); 857 *tc = c->snext; 858 859 if (c == c->mon->sel) { 860 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext); 861 c->mon->sel = t; 862 } 863 } 864 865 Monitor * 866 dirtomon(int dir) 867 { 868 Monitor *m = NULL; 869 870 if (dir > 0) { 871 if (!(m = selmon->next)) 872 m = mons; 873 } else if (selmon == mons) 874 for (m = mons; m->next; m = m->next); 875 else 876 for (m = mons; m->next != selmon; m = m->next); 877 return m; 878 } 879 880 void 881 drawbar(Monitor *m) 882 { 883 int x, w, tw = 0; 884 int boxs = drw->fonts->h / 9; 885 int boxw = drw->fonts->h / 6 + 2; 886 unsigned int i, occ = 0, urg = 0; 887 Client *c; 888 889 /* draw status first so it can be overdrawn by tags later */ 890 if (m == selmon) { /* status is only drawn on selected monitor */ 891 char *text, *s, ch; 892 drw_setscheme(drw, scheme[SchemeNorm]); 893 894 x = 0; 895 for (text = s = stext; *s; s++) { 896 if ((unsigned char)(*s) < ' ') { 897 ch = *s; 898 *s = '\0'; 899 tw = TEXTW(text) - lrpad; 900 drw_text(drw, m->ww - statusw + x, 0, tw, bh, 0, text, 0); 901 x += tw; 902 *s = ch; 903 text = s + 1; 904 } 905 } 906 tw = TEXTW(text) - lrpad + 2; 907 drw_text(drw, m->ww - statusw + x, 0, tw, bh, 0, text, 0); 908 tw = statusw; 909 } 910 911 for (c = m->clients; c; c = c->next) { 912 occ |= c->tags == 255 ? 0 : c->tags; 913 if (c->isurgent) 914 urg |= c->tags; 915 } 916 x = 0; 917 for (i = 0; i < LENGTH(tags); i++) { 918 /* do not draw vacant tags */ 919 if (!(occ & 1 << i || m->tagset[m->seltags] & 1 << i)) 920 continue; 921 922 w = TEXTW(tags[i]); 923 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]); 924 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i); 925 x += w; 926 } 927 w = blw = TEXTW(m->ltsymbol); 928 drw_setscheme(drw, scheme[SchemeNorm]); 929 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0); 930 931 if ((w = m->ww - tw - x) > bh) { 932 if (m->sel) { 933 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]); 934 if (selmon->pertag->curtag == 0) { 935 char windowlabel[20] = {0}; 936 windowlabel[0] = '{'; 937 char *cursor = windowlabel+1; 938 for (int i = 0; i < LENGTH(tags); i++) { 939 if (selmon->sel->tags & 1 << i) { 940 int written = sprintf(cursor, "%d,", i+1); 941 cursor += written; 942 } 943 } 944 *(--cursor) = '}'; 945 x = drw_text(drw, x, 0, TEXTW(&windowlabel[0]), bh, lrpad / 2, &windowlabel[0], 0); 946 } 947 drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0); 948 if (m->sel->isfloating) 949 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0); 950 if (m->sel->issticky) 951 drw_polygon(drw, x + boxs, m->sel->isfloating ? boxs * 2 + boxw : boxs, stickyiconbb.x, stickyiconbb.y, boxw, boxw * stickyiconbb.y / stickyiconbb.x, stickyicon, LENGTH(stickyicon), Nonconvex, m->sel->tags & m->tagset[m->seltags]); 952 } else { 953 drw_setscheme(drw, scheme[SchemeNorm]); 954 drw_rect(drw, x, 0, w, bh, 1, 1); 955 } 956 } 957 drw_map(drw, m->barwin, 0, 0, m->ww, bh); 958 } 959 960 void 961 drawbars(void) 962 { 963 Monitor *m; 964 965 for (m = mons; m; m = m->next) 966 drawbar(m); 967 } 968 969 void 970 enternotify(XEvent *e) 971 { 972 Client *c; 973 Monitor *m; 974 XCrossingEvent *ev = &e->xcrossing; 975 976 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root) 977 return; 978 c = wintoclient(ev->window); 979 m = c ? c->mon : wintomon(ev->window); 980 if (m != selmon) { 981 unfocus(selmon->sel, 1); 982 selmon = m; 983 } else if (!c || c == selmon->sel) 984 return; 985 focus(c); 986 } 987 988 void 989 expose(XEvent *e) 990 { 991 Monitor *m; 992 XExposeEvent *ev = &e->xexpose; 993 994 if (ev->count == 0 && (m = wintomon(ev->window))) 995 drawbar(m); 996 } 997 998 // Gives input focus to a visible client. 999 // If the client is NULL, focus will be given to next visible client in 1000 // stacking order. So the window that last had focus will receive input focus. 1001 void 1002 focus(Client *c) 1003 { 1004 // If client is NULL or not visible, search first non-sticky visible client. 1005 if (!c || !ISVISIBLE(c)) 1006 for (c = selmon->stack; c && (!ISVISIBLE(c) || c->issticky); c = c->snext); 1007 1008 // If there's a selected client on the current monitor, and is different 1009 // from client receiving focus, call unfocus on that client. 1010 if (selmon->sel && selmon->sel != c) 1011 unfocus(selmon->sel, 0); 1012 1013 if (c) { 1014 if (c->mon != selmon) 1015 selmon = c->mon; 1016 if (c->isurgent) 1017 seturgent(c, 0); 1018 detachstack(c); 1019 attachstack(c); 1020 grabbuttons(c, 1); 1021 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel); 1022 setfocus(c); 1023 } else { 1024 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); 1025 XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 1026 } 1027 selmon->sel = c; 1028 drawbars(); 1029 } 1030 1031 /* there are some broken focus acquiring clients needing extra handling */ 1032 void 1033 focusin(XEvent *e) 1034 { 1035 XFocusChangeEvent *ev = &e->xfocus; 1036 1037 if (selmon->sel && ev->window != selmon->sel->win) 1038 setfocus(selmon->sel); 1039 } 1040 1041 void 1042 focusmon(const Arg *arg) 1043 { 1044 Monitor *m; 1045 1046 if (!mons->next) 1047 return; 1048 if ((m = dirtomon(arg->i)) == selmon) 1049 return; 1050 unfocus(selmon->sel, 0); 1051 selmon = m; 1052 focus(NULL); 1053 } 1054 1055 void 1056 focusstack(const Arg *arg) 1057 { 1058 int i = stackpos(arg); 1059 Client *c, *p; 1060 1061 if(i < 0 || (selmon->sel->isfullscreen && lockfullscreen)) 1062 return; 1063 1064 for(p = NULL, c = selmon->clients; c && (i || !ISVISIBLE(c)); 1065 i -= ISVISIBLE(c) ? 1 : 0, p = c, c = c->next); 1066 focus(c ? c : p); 1067 restack(selmon); 1068 } 1069 1070 Atom 1071 getatomprop(Client *c, Atom prop) 1072 { 1073 int di; 1074 unsigned long dl; 1075 unsigned char *p = NULL; 1076 Atom da, atom = None; 1077 1078 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM, 1079 &da, &di, &dl, &dl, &p) == Success && p) { 1080 atom = *(Atom *)p; 1081 XFree(p); 1082 } 1083 return atom; 1084 } 1085 1086 pid_t 1087 getstatusbarpid() 1088 { 1089 char buf[32], *str = buf, *c; 1090 FILE *fp; 1091 1092 if (statuspid > 0) { 1093 snprintf(buf, sizeof(buf), "/proc/%u/cmdline", statuspid); 1094 if ((fp = fopen(buf, "r"))) { 1095 fgets(buf, sizeof(buf), fp); 1096 while ((c = strchr(str, '/'))) 1097 str = c + 1; 1098 fclose(fp); 1099 if (!strcmp(str, STATUSBAR)) 1100 return statuspid; 1101 } 1102 } 1103 if (!(fp = popen("pidof -s "STATUSBAR, "r"))) 1104 return -1; 1105 fgets(buf, sizeof(buf), fp); 1106 pclose(fp); 1107 return strtol(buf, NULL, 10); 1108 } 1109 1110 int 1111 getrootptr(int *x, int *y) 1112 { 1113 int di; 1114 unsigned int dui; 1115 Window dummy; 1116 1117 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui); 1118 } 1119 1120 long 1121 getstate(Window w) 1122 { 1123 int format; 1124 long result = -1; 1125 unsigned char *p = NULL; 1126 unsigned long n, extra; 1127 Atom real; 1128 1129 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState], 1130 &real, &format, &n, &extra, (unsigned char **)&p) != Success) 1131 return -1; 1132 if (n != 0) 1133 result = *p; 1134 XFree(p); 1135 return result; 1136 } 1137 1138 int 1139 gettextprop(Window w, Atom atom, char *text, unsigned int size) 1140 { 1141 char **list = NULL; 1142 int n; 1143 XTextProperty name; 1144 1145 if (!text || size == 0) 1146 return 0; 1147 text[0] = '\0'; 1148 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems) 1149 return 0; 1150 if (name.encoding == XA_STRING) 1151 strncpy(text, (char *)name.value, size - 1); 1152 else { 1153 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) { 1154 strncpy(text, *list, size - 1); 1155 XFreeStringList(list); 1156 } 1157 } 1158 text[size - 1] = '\0'; 1159 XFree(name.value); 1160 return 1; 1161 } 1162 1163 void 1164 grabbuttons(Client *c, int focused) 1165 { 1166 updatenumlockmask(); 1167 { 1168 unsigned int i, j; 1169 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask }; 1170 XUngrabButton(dpy, AnyButton, AnyModifier, c->win); 1171 if (!focused) 1172 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, 1173 BUTTONMASK, GrabModeSync, GrabModeSync, None, None); 1174 for (i = 0; i < LENGTH(buttons); i++) 1175 if (buttons[i].click == ClkClientWin) 1176 for (j = 0; j < LENGTH(modifiers); j++) 1177 XGrabButton(dpy, buttons[i].button, 1178 buttons[i].mask | modifiers[j], 1179 c->win, False, BUTTONMASK, 1180 GrabModeAsync, GrabModeSync, None, None); 1181 } 1182 } 1183 1184 void 1185 grabkeys(void) 1186 { 1187 updatenumlockmask(); 1188 { 1189 unsigned int i, j; 1190 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask }; 1191 KeyCode code; 1192 1193 XUngrabKey(dpy, AnyKey, AnyModifier, root); 1194 for (i = 0; i < LENGTH(keys); i++) 1195 if ((code = XKeysymToKeycode(dpy, keys[i].keysym))) 1196 for (j = 0; j < LENGTH(modifiers); j++) 1197 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root, 1198 True, GrabModeAsync, GrabModeAsync); 1199 } 1200 } 1201 1202 void 1203 incnmaster(const Arg *arg) 1204 { 1205 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0); 1206 arrange(selmon); 1207 } 1208 1209 #ifdef XINERAMA 1210 static int 1211 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) 1212 { 1213 while (n--) 1214 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org 1215 && unique[n].width == info->width && unique[n].height == info->height) 1216 return 0; 1217 return 1; 1218 } 1219 #endif /* XINERAMA */ 1220 1221 void 1222 keypress(XEvent *e) 1223 { 1224 unsigned int i; 1225 KeySym keysym; 1226 XKeyEvent *ev; 1227 1228 ev = &e->xkey; 1229 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0); 1230 for (i = 0; i < LENGTH(keys); i++) 1231 if (keysym == keys[i].keysym 1232 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state) 1233 && keys[i].func) 1234 keys[i].func(&(keys[i].arg)); 1235 } 1236 1237 void 1238 killclient(const Arg *arg) 1239 { 1240 if (!selmon->sel) 1241 return; 1242 if (!sendevent(selmon->sel, wmatom[WMDelete])) { 1243 XGrabServer(dpy); 1244 XSetErrorHandler(xerrordummy); 1245 XSetCloseDownMode(dpy, DestroyAll); 1246 XKillClient(dpy, selmon->sel->win); 1247 XSync(dpy, False); 1248 XSetErrorHandler(xerror); 1249 XUngrabServer(dpy); 1250 } 1251 } 1252 1253 void 1254 manage(Window w, XWindowAttributes *wa) 1255 { 1256 Client *c, *t = NULL, *term = NULL; 1257 Window trans = None; 1258 XWindowChanges wc; 1259 1260 c = ecalloc(1, sizeof(Client)); 1261 c->win = w; 1262 c->pid = winpid(w); 1263 /* geometry */ 1264 c->x = c->oldx = wa->x; 1265 c->y = c->oldy = wa->y; 1266 c->w = c->oldw = wa->width; 1267 c->h = c->oldh = wa->height; 1268 c->oldbw = wa->border_width; 1269 1270 updatetitle(c); 1271 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) { 1272 c->mon = t->mon; 1273 c->tags = t->tags; 1274 } else { 1275 c->mon = selmon; 1276 applyrules(c); 1277 term = termforwin(c); 1278 } 1279 1280 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw) 1281 c->x = c->mon->mx + c->mon->mw - WIDTH(c); 1282 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh) 1283 c->y = c->mon->my + c->mon->mh - HEIGHT(c); 1284 c->x = MAX(c->x, c->mon->mx); 1285 /* only fix client y-offset, if the client center might cover the bar */ 1286 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx) 1287 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my); 1288 c->bw = borderpx; 1289 1290 selmon->tagset[selmon->seltags] &= ~scratchtag; 1291 if (!strcmp(c->name, scratchpadname)) { 1292 c->mon->tagset[c->mon->seltags] |= c->tags = scratchtag; 1293 c->isfloating = True; 1294 c->x = c->mon->wx + (c->mon->ww / 2 - WIDTH(c) / 2); 1295 c->y = c->mon->wy + (c->mon->wh / 2 - HEIGHT(c) / 2); 1296 } 1297 1298 wc.border_width = c->bw; 1299 XConfigureWindow(dpy, w, CWBorderWidth, &wc); 1300 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel); 1301 configure(c); /* propagates border_width, if size doesn't change */ 1302 updatewindowtype(c); 1303 updatesizehints(c); 1304 updatewmhints(c); 1305 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask); 1306 grabbuttons(c, 0); 1307 if (!c->isfloating) 1308 c->isfloating = c->oldstate = trans != None || c->isfixed; 1309 if (c->isfloating) 1310 XRaiseWindow(dpy, c->win); 1311 attach(c); 1312 attachstack(c); 1313 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend, 1314 (unsigned char *) &(c->win), 1); 1315 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */ 1316 setclientstate(c, NormalState); 1317 if (c->mon == selmon) 1318 unfocus(selmon->sel, 0); 1319 c->mon->sel = c; 1320 arrange(c->mon); 1321 XMapWindow(dpy, c->win); 1322 if (term) 1323 swallow(term, c); 1324 focus(NULL); 1325 } 1326 1327 void 1328 mappingnotify(XEvent *e) 1329 { 1330 XMappingEvent *ev = &e->xmapping; 1331 1332 XRefreshKeyboardMapping(ev); 1333 if (ev->request == MappingKeyboard) 1334 grabkeys(); 1335 } 1336 1337 void 1338 maprequest(XEvent *e) 1339 { 1340 static XWindowAttributes wa; 1341 XMapRequestEvent *ev = &e->xmaprequest; 1342 1343 if (!XGetWindowAttributes(dpy, ev->window, &wa)) 1344 return; 1345 if (wa.override_redirect) 1346 return; 1347 if (!wintoclient(ev->window)) 1348 manage(ev->window, &wa); 1349 } 1350 1351 void 1352 monocle(Monitor *m) 1353 { 1354 unsigned int n = 0; 1355 Client *c; 1356 1357 for (c = m->clients; c; c = c->next) 1358 if (ISVISIBLE(c)) 1359 n++; 1360 if (n > 0) /* override layout symbol */ 1361 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n); 1362 for (c = nexttiled(m->clients); c; c = nexttiled(c->next)) 1363 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0); 1364 } 1365 1366 void 1367 motionnotify(XEvent *e) 1368 { 1369 static Monitor *mon = NULL; 1370 Monitor *m; 1371 XMotionEvent *ev = &e->xmotion; 1372 1373 if (ev->window != root) 1374 return; 1375 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) { 1376 unfocus(selmon->sel, 1); 1377 selmon = m; 1378 focus(NULL); 1379 } 1380 mon = m; 1381 } 1382 1383 void 1384 movemouse(const Arg *arg) 1385 { 1386 int x, y, ocx, ocy, nx, ny; 1387 Client *c; 1388 Monitor *m; 1389 XEvent ev; 1390 Time lasttime = 0; 1391 1392 if (!(c = selmon->sel)) 1393 return; 1394 if (c->isfullscreen && c->fakefullscreen != 1) /* no support moving fullscreen windows by mouse */ 1395 return; 1396 restack(selmon); 1397 ocx = c->x; 1398 ocy = c->y; 1399 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, 1400 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess) 1401 return; 1402 if (!getrootptr(&x, &y)) 1403 return; 1404 do { 1405 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev); 1406 switch(ev.type) { 1407 case ConfigureRequest: 1408 case Expose: 1409 case MapRequest: 1410 handler[ev.type](&ev); 1411 break; 1412 case MotionNotify: 1413 if ((ev.xmotion.time - lasttime) <= (1000 / 60)) 1414 continue; 1415 lasttime = ev.xmotion.time; 1416 1417 nx = ocx + (ev.xmotion.x - x); 1418 ny = ocy + (ev.xmotion.y - y); 1419 if (abs(selmon->wx - nx) < snap) 1420 nx = selmon->wx; 1421 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap) 1422 nx = selmon->wx + selmon->ww - WIDTH(c); 1423 if (abs(selmon->wy - ny) < snap) 1424 ny = selmon->wy; 1425 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap) 1426 ny = selmon->wy + selmon->wh - HEIGHT(c); 1427 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange 1428 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap)) 1429 togglefloating(NULL); 1430 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating) 1431 resize(c, nx, ny, c->w, c->h, 1); 1432 break; 1433 } 1434 } while (ev.type != ButtonRelease); 1435 XUngrabPointer(dpy, CurrentTime); 1436 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) { 1437 sendmon(c, m); 1438 selmon = m; 1439 focus(NULL); 1440 } 1441 } 1442 1443 Client * 1444 nexttiled(Client *c) 1445 { 1446 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next); 1447 return c; 1448 } 1449 1450 void 1451 pop(Client *c) 1452 { 1453 detach(c); 1454 attach(c); 1455 focus(c); 1456 arrange(c->mon); 1457 } 1458 1459 void 1460 propertynotify(XEvent *e) 1461 { 1462 Client *c; 1463 Window trans; 1464 XPropertyEvent *ev = &e->xproperty; 1465 1466 if ((ev->window == root) && (ev->atom == XA_WM_NAME)) 1467 updatestatus(); 1468 else if (ev->state == PropertyDelete) 1469 return; /* ignore */ 1470 else if ((c = wintoclient(ev->window))) { 1471 switch(ev->atom) { 1472 default: break; 1473 case XA_WM_TRANSIENT_FOR: 1474 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) && 1475 (c->isfloating = (wintoclient(trans)) != NULL)) 1476 arrange(c->mon); 1477 break; 1478 case XA_WM_NORMAL_HINTS: 1479 updatesizehints(c); 1480 break; 1481 case XA_WM_HINTS: 1482 updatewmhints(c); 1483 drawbars(); 1484 break; 1485 } 1486 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) { 1487 updatetitle(c); 1488 if (c == c->mon->sel) 1489 drawbar(c->mon); 1490 } 1491 if (ev->atom == netatom[NetWMWindowType]) 1492 updatewindowtype(c); 1493 } 1494 } 1495 1496 void 1497 pushstack(const Arg *arg) { 1498 int i = stackpos(arg); 1499 Client *sel = selmon->sel, *c, *p; 1500 1501 if(i < 0) 1502 return; 1503 else if(i == 0) { 1504 detach(sel); 1505 attach(sel); 1506 } 1507 else { 1508 for(p = NULL, c = selmon->clients; c; p = c, c = c->next) 1509 if(!(i -= (ISVISIBLE(c) && c != sel))) 1510 break; 1511 c = c ? c : p; 1512 detach(sel); 1513 sel->next = c->next; 1514 c->next = sel; 1515 } 1516 arrange(selmon); 1517 } 1518 1519 void 1520 quit(const Arg *arg) 1521 { 1522 running = 0; 1523 } 1524 1525 Monitor * 1526 recttomon(int x, int y, int w, int h) 1527 { 1528 Monitor *m, *r = selmon; 1529 int a, area = 0; 1530 1531 for (m = mons; m; m = m->next) 1532 if ((a = INTERSECT(x, y, w, h, m)) > area) { 1533 area = a; 1534 r = m; 1535 } 1536 return r; 1537 } 1538 1539 void 1540 resize(Client *c, int x, int y, int w, int h, int interact) 1541 { 1542 if (applysizehints(c, &x, &y, &w, &h, interact)) 1543 resizeclient(c, x, y, w, h); 1544 } 1545 1546 void 1547 resizeclient(Client *c, int x, int y, int w, int h) 1548 { 1549 XWindowChanges wc; 1550 1551 c->oldx = c->x; c->x = wc.x = x; 1552 c->oldy = c->y; c->y = wc.y = y; 1553 c->oldw = c->w; c->w = wc.width = w; 1554 c->oldh = c->h; c->h = wc.height = h; 1555 wc.border_width = c->bw; 1556 if (((nexttiled(c->mon->clients) == c && !nexttiled(c->next)) 1557 || &monocle == c->mon->lt[c->mon->sellt]->arrange) 1558 && !c->isfullscreen && !c->isfloating 1559 && NULL != c->mon->lt[c->mon->sellt]->arrange) { 1560 c->w = wc.width += c->bw * 2; 1561 c->h = wc.height += c->bw * 2; 1562 wc.border_width = 0; 1563 } 1564 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc); 1565 configure(c); 1566 if (c->fakefullscreen == 1) 1567 XSync(dpy, True); 1568 else 1569 XSync(dpy, False); 1570 } 1571 1572 void 1573 resizemouse(const Arg *arg) 1574 { 1575 int ocx, ocy, nw, nh; 1576 Client *c; 1577 Monitor *m; 1578 XEvent ev; 1579 Time lasttime = 0; 1580 1581 if (!(c = selmon->sel)) 1582 return; 1583 if (c->isfullscreen && c->fakefullscreen != 1) /* no support resizing fullscreen windows by mouse */ 1584 return; 1585 restack(selmon); 1586 ocx = c->x; 1587 ocy = c->y; 1588 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, 1589 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess) 1590 return; 1591 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1); 1592 do { 1593 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev); 1594 switch(ev.type) { 1595 case ConfigureRequest: 1596 case Expose: 1597 case MapRequest: 1598 handler[ev.type](&ev); 1599 break; 1600 case MotionNotify: 1601 if ((ev.xmotion.time - lasttime) <= (1000 / 60)) 1602 continue; 1603 lasttime = ev.xmotion.time; 1604 1605 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1); 1606 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1); 1607 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww 1608 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh) 1609 { 1610 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange 1611 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap)) 1612 togglefloating(NULL); 1613 } 1614 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating) 1615 resize(c, c->x, c->y, nw, nh, 1); 1616 break; 1617 } 1618 } while (ev.type != ButtonRelease); 1619 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1); 1620 XUngrabPointer(dpy, CurrentTime); 1621 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev)); 1622 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) { 1623 sendmon(c, m); 1624 selmon = m; 1625 focus(NULL); 1626 } 1627 } 1628 1629 void 1630 restack(Monitor *m) 1631 { 1632 Client *c; 1633 XEvent ev; 1634 XWindowChanges wc; 1635 1636 drawbar(m); 1637 if (!m->sel) 1638 return; 1639 if (m->sel->isfloating || !m->lt[m->sellt]->arrange) 1640 XRaiseWindow(dpy, m->sel->win); 1641 if (m->lt[m->sellt]->arrange) { 1642 wc.stack_mode = Below; 1643 wc.sibling = m->barwin; 1644 for (c = m->stack; c; c = c->snext) 1645 if (!c->isfloating && ISVISIBLE(c)) { 1646 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc); 1647 wc.sibling = c->win; 1648 } 1649 } 1650 XSync(dpy, False); 1651 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev)); 1652 } 1653 1654 void 1655 run(void) 1656 { 1657 XEvent ev; 1658 /* main event loop */ 1659 XSync(dpy, False); 1660 while (running && !XNextEvent(dpy, &ev)) 1661 if (handler[ev.type]) 1662 handler[ev.type](&ev); /* call handler */ 1663 } 1664 1665 void 1666 scan(void) 1667 { 1668 unsigned int i, num; 1669 Window d1, d2, *wins = NULL; 1670 XWindowAttributes wa; 1671 1672 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) { 1673 for (i = 0; i < num; i++) { 1674 if (!XGetWindowAttributes(dpy, wins[i], &wa) 1675 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1)) 1676 continue; 1677 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState) 1678 manage(wins[i], &wa); 1679 } 1680 for (i = 0; i < num; i++) { /* now the transients */ 1681 if (!XGetWindowAttributes(dpy, wins[i], &wa)) 1682 continue; 1683 if (XGetTransientForHint(dpy, wins[i], &d1) 1684 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)) 1685 manage(wins[i], &wa); 1686 } 1687 if (wins) 1688 XFree(wins); 1689 } 1690 } 1691 1692 void 1693 sendmon(Client *c, Monitor *m) 1694 { 1695 if (c->mon == m) 1696 return; 1697 unfocus(c, 1); 1698 detach(c); 1699 detachstack(c); 1700 c->mon = m; 1701 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */ 1702 attach(c); 1703 attachstack(c); 1704 focus(NULL); 1705 arrange(NULL); 1706 } 1707 1708 void 1709 setclientstate(Client *c, long state) 1710 { 1711 long data[] = { state, None }; 1712 1713 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32, 1714 PropModeReplace, (unsigned char *)data, 2); 1715 } 1716 1717 int 1718 sendevent(Client *c, Atom proto) 1719 { 1720 int n; 1721 Atom *protocols; 1722 int exists = 0; 1723 XEvent ev; 1724 1725 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) { 1726 while (!exists && n--) 1727 exists = protocols[n] == proto; 1728 XFree(protocols); 1729 } 1730 if (exists) { 1731 ev.type = ClientMessage; 1732 ev.xclient.window = c->win; 1733 ev.xclient.message_type = wmatom[WMProtocols]; 1734 ev.xclient.format = 32; 1735 ev.xclient.data.l[0] = proto; 1736 ev.xclient.data.l[1] = CurrentTime; 1737 XSendEvent(dpy, c->win, False, NoEventMask, &ev); 1738 } 1739 return exists; 1740 } 1741 1742 void 1743 setfocus(Client *c) 1744 { 1745 if (!c->neverfocus) { 1746 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); 1747 XChangeProperty(dpy, root, netatom[NetActiveWindow], 1748 XA_WINDOW, 32, PropModeReplace, 1749 (unsigned char *) &(c->win), 1); 1750 } 1751 sendevent(c, wmatom[WMTakeFocus]); 1752 } 1753 1754 void 1755 setfullscreen(Client *c, int fullscreen) 1756 { 1757 if (fullscreen && !c->isfullscreen) { 1758 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32, 1759 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1); 1760 c->isfullscreen = 1; 1761 c->oldbw = c->bw; 1762 if (c->fakefullscreen == 1) 1763 return; 1764 c->oldstate = c->isfloating; 1765 c->bw = 0; 1766 c->isfloating = 1; 1767 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh); 1768 XRaiseWindow(dpy, c->win); 1769 } else if (!fullscreen && c->isfullscreen){ 1770 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32, 1771 PropModeReplace, (unsigned char*)0, 0); 1772 c->isfullscreen = 0; 1773 c->bw = c->oldbw; 1774 if (c->fakefullscreen == 1) 1775 return; 1776 if (c->fakefullscreen == 2) 1777 c->fakefullscreen = 1; 1778 c->isfloating = c->oldstate; 1779 c->x = c->oldx; 1780 c->y = c->oldy; 1781 c->w = c->oldw; 1782 c->h = c->oldh; 1783 resizeclient(c, c->x, c->y, c->w, c->h); 1784 arrange(c->mon); 1785 } 1786 } 1787 1788 void 1789 setlayout(const Arg *arg) 1790 { 1791 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt]) 1792 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1; 1793 if (arg && arg->v) 1794 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v; 1795 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol); 1796 if (selmon->sel) 1797 arrange(selmon); 1798 else 1799 drawbar(selmon); 1800 } 1801 1802 /* arg > 1.0 will set mfact absolutely */ 1803 void 1804 setmfact(const Arg *arg) 1805 { 1806 float f; 1807 1808 if (!arg || !selmon->lt[selmon->sellt]->arrange) 1809 return; 1810 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0; 1811 if (f < 0.05 || f > 0.95) 1812 return; 1813 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f; 1814 arrange(selmon); 1815 } 1816 1817 void no_swallow_next(int signum, siginfo_t *si, void *ucontext) 1818 { 1819 swallow_next = 0; 1820 } 1821 1822 void 1823 setup(void) 1824 { 1825 int i; 1826 XSetWindowAttributes wa; 1827 Atom utf8string; 1828 1829 /* clean up any zombies immediately */ 1830 sigchld(0); 1831 1832 /* init screen */ 1833 screen = DefaultScreen(dpy); 1834 sw = DisplayWidth(dpy, screen); 1835 sh = DisplayHeight(dpy, screen); 1836 root = RootWindow(dpy, screen); 1837 drw = drw_create(dpy, screen, root, sw, sh); 1838 if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) 1839 die("no fonts could be loaded."); 1840 lrpad = drw->fonts->h; 1841 bh = drw->fonts->h + 2; 1842 updategeom(); 1843 /* init atoms */ 1844 utf8string = XInternAtom(dpy, "UTF8_STRING", False); 1845 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); 1846 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); 1847 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False); 1848 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False); 1849 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False); 1850 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); 1851 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); 1852 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False); 1853 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False); 1854 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False); 1855 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False); 1856 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False); 1857 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False); 1858 /* init cursors */ 1859 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr); 1860 cursor[CurResize] = drw_cur_create(drw, XC_sizing); 1861 cursor[CurMove] = drw_cur_create(drw, XC_fleur); 1862 /* init appearance */ 1863 scheme = ecalloc(LENGTH(colors), sizeof(Clr *)); 1864 for (i = 0; i < LENGTH(colors); i++) 1865 scheme[i] = drw_scm_create(drw, colors[i], 3); 1866 /* init bars */ 1867 updatebars(); 1868 updatestatus(); 1869 /* supporting window for NetWMCheck */ 1870 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0); 1871 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32, 1872 PropModeReplace, (unsigned char *) &wmcheckwin, 1); 1873 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8, 1874 PropModeReplace, (unsigned char *) "dwm", 3); 1875 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32, 1876 PropModeReplace, (unsigned char *) &wmcheckwin, 1); 1877 /* EWMH support per view */ 1878 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, 1879 PropModeReplace, (unsigned char *) netatom, NetLast); 1880 XDeleteProperty(dpy, root, netatom[NetClientList]); 1881 /* select events */ 1882 wa.cursor = cursor[CurNormal]->cursor; 1883 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask 1884 |ButtonPressMask|PointerMotionMask|EnterWindowMask 1885 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask; 1886 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa); 1887 XSelectInput(dpy, root, wa.event_mask); 1888 grabkeys(); 1889 focus(NULL); 1890 1891 struct sigaction sa = { 1892 .sa_sigaction = no_swallow_next, 1893 .sa_flags = SA_SIGINFO, 1894 }; 1895 sigaction(SIGRTMIN+69, &sa, NULL); 1896 } 1897 1898 1899 void 1900 seturgent(Client *c, int urg) 1901 { 1902 XWMHints *wmh; 1903 1904 c->isurgent = urg; 1905 if (!(wmh = XGetWMHints(dpy, c->win))) 1906 return; 1907 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint); 1908 XSetWMHints(dpy, c->win, wmh); 1909 XFree(wmh); 1910 } 1911 1912 void 1913 showhide(Client *c) 1914 { 1915 if (!c) 1916 return; 1917 if (ISVISIBLE(c)) { 1918 /* show clients top down */ 1919 XMoveWindow(dpy, c->win, c->x, c->y); 1920 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen) 1921 resize(c, c->x, c->y, c->w, c->h, 0); 1922 showhide(c->snext); 1923 } else { 1924 /* hide clients bottom up */ 1925 showhide(c->snext); 1926 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y); 1927 } 1928 } 1929 1930 void 1931 sigchld(int unused) 1932 { 1933 if (signal(SIGCHLD, sigchld) == SIG_ERR) 1934 die("can't install SIGCHLD handler:"); 1935 while (0 < waitpid(-1, NULL, WNOHANG)); 1936 } 1937 1938 void 1939 sigstatusbar(const Arg *arg) 1940 { 1941 union sigval sv; 1942 1943 if (!statussig) 1944 return; 1945 sv.sival_int = arg->i; 1946 if ((statuspid = getstatusbarpid()) <= 0) 1947 return; 1948 1949 sigqueue(statuspid, SIGRTMIN+statussig, sv); 1950 } 1951 1952 void 1953 spawn(const Arg *arg) 1954 { 1955 if (arg->v == dmenucmd) 1956 dmenumon[0] = '0' + selmon->num; 1957 selmon->tagset[selmon->seltags] &= ~scratchtag; 1958 if (fork() == 0) { 1959 if (dpy) 1960 close(ConnectionNumber(dpy)); 1961 setsid(); 1962 execvp(((char **)arg->v)[0], (char **)arg->v); 1963 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]); 1964 perror(" failed"); 1965 exit(EXIT_SUCCESS); 1966 } 1967 } 1968 1969 int 1970 stackpos(const Arg *arg) { 1971 int n, i; 1972 Client *c, *l; 1973 1974 if(!selmon->clients) 1975 return -1; 1976 1977 if(arg->i == PREVSEL) { 1978 for(l = selmon->stack; l && (!ISVISIBLE(l) || l == selmon->sel); l = l->snext); 1979 if(!l) 1980 return -1; 1981 for(i = 0, c = selmon->clients; c != l; i += ISVISIBLE(c) ? 1 : 0, c = c->next); 1982 return i; 1983 } 1984 else if(ISINC(arg->i)) { 1985 if(!selmon->sel) 1986 return -1; 1987 for(i = 0, c = selmon->clients; c != selmon->sel; i += ISVISIBLE(c) ? 1 : 0, c = c->next); 1988 for(n = i; c; n += ISVISIBLE(c) ? 1 : 0, c = c->next); 1989 return MOD(i + GETINC(arg->i), n); 1990 } 1991 else if(arg->i < 0) { 1992 for(i = 0, c = selmon->clients; c; i += ISVISIBLE(c) ? 1 : 0, c = c->next); 1993 return MAX(i + arg->i, 0); 1994 } 1995 else 1996 return arg->i; 1997 } 1998 1999 void 2000 tag(const Arg *arg) 2001 { 2002 if (selmon->sel && arg->ui & TAGMASK) { 2003 selmon->sel->tags = arg->ui & TAGMASK; 2004 focus(NULL); 2005 arrange(selmon); 2006 } 2007 } 2008 2009 void 2010 tagmon(const Arg *arg) 2011 { 2012 if (!selmon->sel || !mons->next) 2013 return; 2014 sendmon(selmon->sel, dirtomon(arg->i)); 2015 } 2016 2017 void 2018 togglebar(const Arg *arg) 2019 { 2020 selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar; 2021 updatebarpos(selmon); 2022 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh); 2023 arrange(selmon); 2024 } 2025 2026 void 2027 togglefakefullscreen(const Arg *arg) 2028 { 2029 Client *c = selmon->sel; 2030 if (!c) 2031 return; 2032 2033 if (c->fakefullscreen) { 2034 if (c->isfullscreen) { 2035 if (c->isfloating && c->fakefullscreen == 1) { 2036 c->oldstate = c->isfloating; 2037 c->oldx = c->x; 2038 c->oldy = c->y; 2039 c->oldw = c->w; 2040 c->oldh = c->h; 2041 } 2042 c->fakefullscreen = 0; 2043 } 2044 else 2045 c->isfullscreen = 0; 2046 } else { 2047 c->fakefullscreen = 1; 2048 if (c->isfullscreen) { 2049 c->isfloating = c->oldstate; 2050 c->bw = c->oldbw; 2051 c->x = c->oldx; 2052 c->y = c->oldy; 2053 c->w = c->oldw; 2054 c->h = c->oldh; 2055 resizeclient(c, c->x, c->y, c->w, c->h); 2056 } 2057 c->isfullscreen = 0; 2058 } 2059 setfullscreen(c, !c->isfullscreen); 2060 } 2061 2062 void 2063 togglefloating(const Arg *arg) 2064 { 2065 if (!selmon->sel) 2066 return; 2067 if (selmon->sel->isfullscreen && selmon->sel->fakefullscreen != 1) /* no support for fullscreen windows */ 2068 return; 2069 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed; 2070 if (selmon->sel->isfloating) 2071 resize(selmon->sel, selmon->sel->x, selmon->sel->y, 2072 selmon->sel->w, selmon->sel->h, 0); 2073 arrange(selmon); 2074 } 2075 2076 void 2077 togglefullscr(const Arg *arg) 2078 { 2079 if(selmon->sel) 2080 setfullscreen(selmon->sel, !selmon->sel->isfullscreen); 2081 } 2082 2083 void 2084 togglescratch(const Arg *arg) 2085 { 2086 Client *c; 2087 unsigned int found = 0; 2088 2089 for (c = selmon->clients; c && !(found = c->tags & scratchtag); c = c->next); 2090 if (found) { 2091 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ scratchtag; 2092 if (newtagset) { 2093 selmon->tagset[selmon->seltags] = newtagset; 2094 focus(NULL); 2095 arrange(selmon); 2096 } 2097 if (ISVISIBLE(c)) { 2098 focus(c); 2099 restack(selmon); 2100 } 2101 } else 2102 spawn(arg); 2103 } 2104 2105 void 2106 togglesticky(const Arg *arg) 2107 { 2108 if (!selmon->sel) 2109 return; 2110 selmon->sel->issticky = !selmon->sel->issticky; 2111 arrange(selmon); 2112 } 2113 2114 void 2115 toggletag(const Arg *arg) 2116 { 2117 unsigned int newtags; 2118 2119 if (!selmon->sel) 2120 return; 2121 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK); 2122 if (newtags) { 2123 selmon->sel->tags = newtags; 2124 focus(NULL); 2125 arrange(selmon); 2126 } 2127 } 2128 2129 void 2130 toggletagscratch(const Arg *arg) 2131 { 2132 if (!selmon->sel) 2133 return; 2134 if (selmon->sel->tags == scratchtag) 2135 selmon->sel->tags = selmon->pertag->curtag; 2136 else 2137 selmon->sel->tags = scratchtag; 2138 2139 focus(NULL); 2140 arrange(selmon); 2141 } 2142 2143 void 2144 toggleview(const Arg *arg) 2145 { 2146 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK); 2147 int i; 2148 2149 if (newtagset) { 2150 selmon->tagset[selmon->seltags] = newtagset; 2151 2152 if (newtagset == ~0) { 2153 selmon->pertag->prevtag = selmon->pertag->curtag; 2154 selmon->pertag->curtag = 0; 2155 } 2156 2157 /* test if the user did not select the same tag */ 2158 if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) { 2159 selmon->pertag->prevtag = selmon->pertag->curtag; 2160 for (i = 0; !(newtagset & 1 << i); i++) ; 2161 selmon->pertag->curtag = i + 1; 2162 } 2163 2164 /* apply settings for this view */ 2165 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag]; 2166 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag]; 2167 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag]; 2168 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; 2169 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1]; 2170 2171 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag]) 2172 togglebar(NULL); 2173 2174 focus(NULL); 2175 arrange(selmon); 2176 } 2177 } 2178 2179 void 2180 unfocus(Client *c, int setfocus) 2181 { 2182 if (!c) 2183 return; 2184 grabbuttons(c, 0); 2185 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel); 2186 if (setfocus) { 2187 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); 2188 XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 2189 } 2190 } 2191 2192 void 2193 unmanage(Client *c, int destroyed) 2194 { 2195 Monitor *m = c->mon; 2196 XWindowChanges wc; 2197 2198 if (c->swallowing) { 2199 unswallow(c); 2200 return; 2201 } 2202 2203 Client *s = swallowingclient(c->win); 2204 if (s) { 2205 free(s->swallowing); 2206 s->swallowing = NULL; 2207 arrange(m); 2208 focus(NULL); 2209 return; 2210 } 2211 2212 detach(c); 2213 detachstack(c); 2214 if (!destroyed) { 2215 wc.border_width = c->oldbw; 2216 XGrabServer(dpy); /* avoid race conditions */ 2217 XSetErrorHandler(xerrordummy); 2218 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */ 2219 XUngrabButton(dpy, AnyButton, AnyModifier, c->win); 2220 setclientstate(c, WithdrawnState); 2221 XSync(dpy, False); 2222 XSetErrorHandler(xerror); 2223 XUngrabServer(dpy); 2224 } 2225 free(c); 2226 2227 if (!s) { 2228 arrange(m); 2229 focus(NULL); 2230 updateclientlist(); 2231 } 2232 } 2233 2234 void 2235 unmapnotify(XEvent *e) 2236 { 2237 Client *c; 2238 XUnmapEvent *ev = &e->xunmap; 2239 2240 if ((c = wintoclient(ev->window))) { 2241 if (ev->send_event) 2242 setclientstate(c, WithdrawnState); 2243 else 2244 unmanage(c, 0); 2245 } 2246 } 2247 2248 void 2249 updatebars(void) 2250 { 2251 Monitor *m; 2252 XSetWindowAttributes wa = { 2253 .override_redirect = True, 2254 .background_pixmap = ParentRelative, 2255 .event_mask = ButtonPressMask|ExposureMask 2256 }; 2257 XClassHint ch = {"dwm", "dwm"}; 2258 for (m = mons; m; m = m->next) { 2259 if (m->barwin) 2260 continue; 2261 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen), 2262 CopyFromParent, DefaultVisual(dpy, screen), 2263 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa); 2264 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor); 2265 XMapRaised(dpy, m->barwin); 2266 XSetClassHint(dpy, m->barwin, &ch); 2267 } 2268 } 2269 2270 void 2271 updatebarpos(Monitor *m) 2272 { 2273 m->wy = m->my; 2274 m->wh = m->mh; 2275 if (m->showbar) { 2276 m->wh -= bh; 2277 m->by = m->topbar ? m->wy : m->wy + m->wh; 2278 m->wy = m->topbar ? m->wy + bh : m->wy; 2279 } else 2280 m->by = -bh; 2281 } 2282 2283 void 2284 updateclientlist() 2285 { 2286 Client *c; 2287 Monitor *m; 2288 2289 XDeleteProperty(dpy, root, netatom[NetClientList]); 2290 for (m = mons; m; m = m->next) 2291 for (c = m->clients; c; c = c->next) 2292 XChangeProperty(dpy, root, netatom[NetClientList], 2293 XA_WINDOW, 32, PropModeAppend, 2294 (unsigned char *) &(c->win), 1); 2295 } 2296 2297 int 2298 updategeom(void) 2299 { 2300 int dirty = 0; 2301 2302 #ifdef XINERAMA 2303 if (XineramaIsActive(dpy)) { 2304 int i, j, n, nn; 2305 Client *c; 2306 Monitor *m; 2307 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn); 2308 XineramaScreenInfo *unique = NULL; 2309 2310 for (n = 0, m = mons; m; m = m->next, n++); 2311 /* only consider unique geometries as separate screens */ 2312 unique = ecalloc(nn, sizeof(XineramaScreenInfo)); 2313 for (i = 0, j = 0; i < nn; i++) 2314 if (isuniquegeom(unique, j, &info[i])) 2315 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo)); 2316 XFree(info); 2317 nn = j; 2318 if (n <= nn) { /* new monitors available */ 2319 for (i = 0; i < (nn - n); i++) { 2320 for (m = mons; m && m->next; m = m->next); 2321 if (m) 2322 m->next = createmon(); 2323 else 2324 mons = createmon(); 2325 } 2326 for (i = 0, m = mons; i < nn && m; m = m->next, i++) 2327 if (i >= n 2328 || unique[i].x_org != m->mx || unique[i].y_org != m->my 2329 || unique[i].width != m->mw || unique[i].height != m->mh) 2330 { 2331 dirty = 1; 2332 m->num = i; 2333 m->mx = m->wx = unique[i].x_org; 2334 m->my = m->wy = unique[i].y_org; 2335 m->mw = m->ww = unique[i].width; 2336 m->mh = m->wh = unique[i].height; 2337 updatebarpos(m); 2338 } 2339 } else { /* less monitors available nn < n */ 2340 for (i = nn; i < n; i++) { 2341 for (m = mons; m && m->next; m = m->next); 2342 while ((c = m->clients)) { 2343 dirty = 1; 2344 m->clients = c->next; 2345 detachstack(c); 2346 c->mon = mons; 2347 attach(c); 2348 attachstack(c); 2349 } 2350 if (m == selmon) 2351 selmon = mons; 2352 cleanupmon(m); 2353 } 2354 } 2355 free(unique); 2356 } else 2357 #endif /* XINERAMA */ 2358 { /* default monitor setup */ 2359 if (!mons) 2360 mons = createmon(); 2361 if (mons->mw != sw || mons->mh != sh) { 2362 dirty = 1; 2363 mons->mw = mons->ww = sw; 2364 mons->mh = mons->wh = sh; 2365 updatebarpos(mons); 2366 } 2367 } 2368 if (dirty) { 2369 selmon = mons; 2370 selmon = wintomon(root); 2371 } 2372 return dirty; 2373 } 2374 2375 void 2376 updatenumlockmask(void) 2377 { 2378 unsigned int i, j; 2379 XModifierKeymap *modmap; 2380 2381 numlockmask = 0; 2382 modmap = XGetModifierMapping(dpy); 2383 for (i = 0; i < 8; i++) 2384 for (j = 0; j < modmap->max_keypermod; j++) 2385 if (modmap->modifiermap[i * modmap->max_keypermod + j] 2386 == XKeysymToKeycode(dpy, XK_Num_Lock)) 2387 numlockmask = (1 << i); 2388 XFreeModifiermap(modmap); 2389 } 2390 2391 void 2392 updatesizehints(Client *c) 2393 { 2394 long msize; 2395 XSizeHints size; 2396 2397 if (!XGetWMNormalHints(dpy, c->win, &size, &msize)) 2398 /* size is uninitialized, ensure that size.flags aren't used */ 2399 size.flags = PSize; 2400 if (size.flags & PBaseSize) { 2401 c->basew = size.base_width; 2402 c->baseh = size.base_height; 2403 } else if (size.flags & PMinSize) { 2404 c->basew = size.min_width; 2405 c->baseh = size.min_height; 2406 } else 2407 c->basew = c->baseh = 0; 2408 if (size.flags & PResizeInc) { 2409 c->incw = size.width_inc; 2410 c->inch = size.height_inc; 2411 } else 2412 c->incw = c->inch = 0; 2413 if (size.flags & PMaxSize) { 2414 c->maxw = size.max_width; 2415 c->maxh = size.max_height; 2416 } else 2417 c->maxw = c->maxh = 0; 2418 if (size.flags & PMinSize) { 2419 c->minw = size.min_width; 2420 c->minh = size.min_height; 2421 } else if (size.flags & PBaseSize) { 2422 c->minw = size.base_width; 2423 c->minh = size.base_height; 2424 } else 2425 c->minw = c->minh = 0; 2426 if (size.flags & PAspect) { 2427 c->mina = (float)size.min_aspect.y / size.min_aspect.x; 2428 c->maxa = (float)size.max_aspect.x / size.max_aspect.y; 2429 } else 2430 c->maxa = c->mina = 0.0; 2431 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh); 2432 } 2433 2434 void 2435 updatestatus(void) 2436 { 2437 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext))) { 2438 strcpy(stext, "dwm-"VERSION); 2439 statusw = TEXTW(stext) - lrpad + 2; 2440 } else { 2441 char *text, *s, ch; 2442 2443 statusw = 0; 2444 for (text = s = stext; *s; s++) { 2445 if ((unsigned char)(*s) < ' ') { 2446 ch = *s; 2447 *s = '\0'; 2448 statusw += TEXTW(text) - lrpad; 2449 *s = ch; 2450 text = s + 1; 2451 } 2452 } 2453 statusw += TEXTW(text) - lrpad + 2; 2454 } 2455 drawbar(selmon); 2456 } 2457 2458 void 2459 updatetitle(Client *c) 2460 { 2461 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name)) 2462 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name); 2463 if (c->name[0] == '\0') /* hack to mark broken clients */ 2464 strcpy(c->name, broken); 2465 } 2466 2467 void 2468 updatewindowtype(Client *c) 2469 { 2470 Atom state = getatomprop(c, netatom[NetWMState]); 2471 Atom wtype = getatomprop(c, netatom[NetWMWindowType]); 2472 2473 if (state == netatom[NetWMFullscreen]) 2474 setfullscreen(c, 1); 2475 if (wtype == netatom[NetWMWindowTypeDialog]) 2476 c->isfloating = 1; 2477 } 2478 2479 void 2480 updatewmhints(Client *c) 2481 { 2482 XWMHints *wmh; 2483 2484 if ((wmh = XGetWMHints(dpy, c->win))) { 2485 if (c == selmon->sel && wmh->flags & XUrgencyHint) { 2486 wmh->flags &= ~XUrgencyHint; 2487 XSetWMHints(dpy, c->win, wmh); 2488 } else 2489 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0; 2490 if (wmh->flags & InputHint) 2491 c->neverfocus = !wmh->input; 2492 else 2493 c->neverfocus = 0; 2494 XFree(wmh); 2495 } 2496 } 2497 2498 void 2499 view(const Arg *arg) 2500 { 2501 int i; 2502 unsigned int tmptag; 2503 2504 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags]) 2505 return; 2506 selmon->seltags ^= 1; /* toggle sel tagset */ 2507 if (arg->ui & TAGMASK) { 2508 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK; 2509 selmon->pertag->prevtag = selmon->pertag->curtag; 2510 2511 if (arg->ui == ~0) 2512 selmon->pertag->curtag = 0; 2513 else { 2514 for (i = 0; !(arg->ui & 1 << i); i++) ; 2515 selmon->pertag->curtag = i + 1; 2516 } 2517 } else { 2518 tmptag = selmon->pertag->prevtag; 2519 selmon->pertag->prevtag = selmon->pertag->curtag; 2520 selmon->pertag->curtag = tmptag; 2521 } 2522 2523 selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag]; 2524 selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag]; 2525 selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag]; 2526 selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt]; 2527 selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1]; 2528 2529 if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag]) 2530 togglebar(NULL); 2531 2532 focus(NULL); 2533 arrange(selmon); 2534 } 2535 2536 pid_t 2537 winpid(Window w) 2538 { 2539 2540 pid_t result = 0; 2541 2542 #ifdef __linux__ 2543 xcb_res_client_id_spec_t spec = {0}; 2544 spec.client = w; 2545 spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID; 2546 2547 xcb_generic_error_t *e = NULL; 2548 xcb_res_query_client_ids_cookie_t c = xcb_res_query_client_ids(xcon, 1, &spec); 2549 xcb_res_query_client_ids_reply_t *r = xcb_res_query_client_ids_reply(xcon, c, &e); 2550 2551 if (!r) 2552 return (pid_t)0; 2553 2554 xcb_res_client_id_value_iterator_t i = xcb_res_query_client_ids_ids_iterator(r); 2555 for (; i.rem; xcb_res_client_id_value_next(&i)) { 2556 spec = i.data->spec; 2557 if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) { 2558 uint32_t *t = xcb_res_client_id_value_value(i.data); 2559 result = *t; 2560 break; 2561 } 2562 } 2563 2564 free(r); 2565 2566 if (result == (pid_t)-1) 2567 result = 0; 2568 2569 #endif /* __linux__ */ 2570 2571 #ifdef __OpenBSD__ 2572 Atom type; 2573 int format; 2574 unsigned long len, bytes; 2575 unsigned char *prop; 2576 pid_t ret; 2577 2578 if (XGetWindowProperty(dpy, w, XInternAtom(dpy, "_NET_WM_PID", 0), 0, 1, False, AnyPropertyType, &type, &format, &len, &bytes, &prop) != Success || !prop) 2579 return 0; 2580 2581 ret = *(pid_t*)prop; 2582 XFree(prop); 2583 result = ret; 2584 2585 #endif /* __OpenBSD__ */ 2586 return result; 2587 } 2588 2589 pid_t 2590 getparentprocess(pid_t p) 2591 { 2592 unsigned int v = 0; 2593 2594 #ifdef __linux__ 2595 FILE *f; 2596 char buf[256]; 2597 snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p); 2598 2599 if (!(f = fopen(buf, "r"))) 2600 return 0; 2601 2602 fscanf(f, "%*u %*s %*c %u", &v); 2603 fclose(f); 2604 #endif /* __linux__*/ 2605 2606 #ifdef __OpenBSD__ 2607 int n; 2608 kvm_t *kd; 2609 struct kinfo_proc *kp; 2610 2611 kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL); 2612 if (!kd) 2613 return 0; 2614 2615 kp = kvm_getprocs(kd, KERN_PROC_PID, p, sizeof(*kp), &n); 2616 v = kp->p_ppid; 2617 #endif /* __OpenBSD__ */ 2618 2619 return (pid_t)v; 2620 } 2621 2622 int 2623 isdescprocess(pid_t p, pid_t c) 2624 { 2625 while (p != c && c != 0) 2626 c = getparentprocess(c); 2627 2628 return (int)c; 2629 } 2630 2631 Client * 2632 termforwin(const Client *w) 2633 { 2634 Client *c; 2635 Monitor *m; 2636 2637 if (!w->pid || w->isterminal) 2638 return NULL; 2639 2640 for (m = mons; m; m = m->next) { 2641 for (c = m->clients; c; c = c->next) { 2642 if (c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid)) 2643 return c; 2644 } 2645 } 2646 2647 return NULL; 2648 } 2649 2650 Client * 2651 swallowingclient(Window w) 2652 { 2653 Client *c; 2654 Monitor *m; 2655 2656 for (m = mons; m; m = m->next) { 2657 for (c = m->clients; c; c = c->next) { 2658 if (c->swallowing && c->swallowing->win == w) 2659 return c; 2660 } 2661 } 2662 2663 return NULL; 2664 } 2665 2666 Client * 2667 wintoclient(Window w) 2668 { 2669 Client *c; 2670 Monitor *m; 2671 2672 for (m = mons; m; m = m->next) 2673 for (c = m->clients; c; c = c->next) 2674 if (c->win == w) 2675 return c; 2676 return NULL; 2677 } 2678 2679 Monitor * 2680 wintomon(Window w) 2681 { 2682 int x, y; 2683 Client *c; 2684 Monitor *m; 2685 2686 if (w == root && getrootptr(&x, &y)) 2687 return recttomon(x, y, 1, 1); 2688 for (m = mons; m; m = m->next) 2689 if (w == m->barwin) 2690 return m; 2691 if ((c = wintoclient(w))) 2692 return c->mon; 2693 return selmon; 2694 } 2695 2696 /* There's no way to check accesses to destroyed windows, thus those cases are 2697 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs 2698 * default error handler, which may call exit. */ 2699 int 2700 xerror(Display *dpy, XErrorEvent *ee) 2701 { 2702 if (ee->error_code == BadWindow 2703 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch) 2704 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable) 2705 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable) 2706 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable) 2707 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch) 2708 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess) 2709 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess) 2710 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable)) 2711 return 0; 2712 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n", 2713 ee->request_code, ee->error_code); 2714 return xerrorxlib(dpy, ee); /* may call exit */ 2715 } 2716 2717 int 2718 xerrordummy(Display *dpy, XErrorEvent *ee) 2719 { 2720 return 0; 2721 } 2722 2723 /* Startup Error handler to check if another window manager 2724 * is already running. */ 2725 int 2726 xerrorstart(Display *dpy, XErrorEvent *ee) 2727 { 2728 die("dwm: another window manager is already running"); 2729 return -1; 2730 } 2731 2732 void 2733 zoom(const Arg *arg) 2734 { 2735 Client *c = selmon->sel; 2736 2737 if (!selmon->lt[selmon->sellt]->arrange 2738 || (selmon->sel && selmon->sel->isfloating)) 2739 return; 2740 if (c == nexttiled(selmon->clients)) 2741 if (!c || !(c = nexttiled(c->next))) 2742 return; 2743 pop(c); 2744 } 2745 2746 void 2747 resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst) 2748 { 2749 char *sdst = NULL; 2750 int *idst = NULL; 2751 float *fdst = NULL; 2752 2753 sdst = dst; 2754 idst = dst; 2755 fdst = dst; 2756 2757 char fullname[256]; 2758 char *type; 2759 XrmValue ret; 2760 2761 snprintf(fullname, sizeof(fullname), "%s.%s", "dwm", name); 2762 fullname[sizeof(fullname) - 1] = '\0'; 2763 2764 XrmGetResource(db, fullname, "*", &type, &ret); 2765 if (!(ret.addr == NULL || strncmp("String", type, 64))) 2766 { 2767 switch (rtype) { 2768 case STRING: 2769 strcpy(sdst, ret.addr); 2770 break; 2771 case INTEGER: 2772 *idst = strtoul(ret.addr, NULL, 10); 2773 break; 2774 case FLOAT: 2775 *fdst = strtof(ret.addr, NULL); 2776 break; 2777 } 2778 } 2779 } 2780 2781 void 2782 load_xresources(void) 2783 { 2784 Display *display; 2785 char *resm; 2786 XrmDatabase db; 2787 ResourcePref *p; 2788 2789 display = XOpenDisplay(NULL); 2790 resm = XResourceManagerString(display); 2791 if (!resm) 2792 return; 2793 2794 db = XrmGetStringDatabase(resm); 2795 for (p = resources; p < resources + LENGTH(resources); p++) 2796 resource_load(db, p->name, p->type, p->dst); 2797 XCloseDisplay(display); 2798 } 2799 2800 void 2801 livereload_xresources(const Arg *arg) 2802 { 2803 load_xresources(); 2804 int i; 2805 for (i = 0; i < LENGTH(colors); i++) 2806 scheme[i] = drw_scm_create(drw, colors[i], 3); 2807 focus(NULL); 2808 arrange(NULL); 2809 } 2810 2811 void 2812 jumptotag(const Arg *arg) { 2813 if (selmon->pertag->curtag != 0) 2814 return; 2815 2816 for (int i = 0; i < LENGTH(tags); i++) { 2817 if (selmon->sel->tags & 1 << i) { 2818 Arg a = {.ui = 1 << i}; 2819 view(&a); 2820 return; 2821 } 2822 } 2823 } 2824 2825 2826 2827 int 2828 main(int argc, char *argv[]) 2829 { 2830 pid_t pid = getpid(); 2831 char pid_s[sizeof(int)] = {0}; 2832 snprintf(pid_s, sizeof(int), "%d", pid); 2833 setenv("DWM_PID", pid_s, 1); 2834 2835 if (argc == 2 && !strcmp("-v", argv[1])) 2836 die("dwm-"VERSION); 2837 else if (argc != 1) 2838 die("usage: dwm [-v]"); 2839 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale()) 2840 fputs("warning: no locale support\n", stderr); 2841 if (!(dpy = XOpenDisplay(NULL))) 2842 die("dwm: cannot open display"); 2843 if (!(xcon = XGetXCBConnection(dpy))) 2844 die("dwm: cannot get xcb connection\n"); 2845 checkotherwm(); 2846 XrmInitialize(); 2847 load_xresources(); 2848 setup(); 2849 #ifdef __OpenBSD__ 2850 if (pledge("stdio rpath proc exec ps", NULL) == -1) 2851 die("pledge"); 2852 #endif /* __OpenBSD__ */ 2853 scan(); 2854 run(); 2855 cleanup(); 2856 XCloseDisplay(dpy); 2857 return EXIT_SUCCESS; 2858 }