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