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