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