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