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