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