dwm.c (71045B)
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 LENGTH(X) (sizeof X / sizeof X[0]) 54 #define MOUSEMASK (BUTTONMASK|PointerMotionMask) 55 #define WIDTH(X) ((X)->w + 2 * (X)->bw) 56 #define HEIGHT(X) ((X)->h + 2 * (X)->bw) 57 #define TAGMASK ((1 << LENGTH(tags)) - 1) 58 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 59 60 #define SYSTEM_TRAY_REQUEST_DOCK 0 61 /* XEMBED messages */ 62 #define XEMBED_EMBEDDED_NOTIFY 0 63 #define XEMBED_WINDOW_ACTIVATE 1 64 #define XEMBED_FOCUS_IN 4 65 #define XEMBED_MODALITY_ON 10 66 #define XEMBED_MAPPED (1 << 0) 67 #define XEMBED_WINDOW_ACTIVATE 1 68 #define XEMBED_WINDOW_DEACTIVATE 2 69 #define VERSION_MAJOR 0 70 #define VERSION_MINOR 0 71 #define XEMBED_EMBEDDED_VERSION (VERSION_MAJOR << 16) | VERSION_MINOR 72 73 /* enums */ 74 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ 75 enum { SchemeNorm, SchemeSel }; /* color schemes */ 76 enum { NetSupported, NetWMName, NetWMState, NetWMCheck, 77 NetSystemTray, NetSystemTrayOP, NetSystemTrayOrientation, NetSystemTrayOrientationHorz, 78 NetWMFullscreen, NetActiveWindow, NetWMWindowType, 79 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */ 80 enum { Manager, Xembed, XembedInfo, XLast }; /* Xembed atoms */ 81 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */ 82 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, 83 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */ 84 85 typedef union { 86 int i; 87 unsigned int ui; 88 float f; 89 const void *v; 90 } Arg; 91 92 typedef struct { 93 unsigned int click; 94 unsigned int mask; 95 unsigned int button; 96 void (*func)(const Arg *arg); 97 const Arg arg; 98 } Button; 99 100 typedef struct Monitor Monitor; 101 typedef struct Client Client; 102 struct Client { 103 char name[256]; 104 float mina, maxa; 105 int x, y, w, h; 106 int oldx, oldy, oldw, oldh; 107 int basew, baseh, incw, inch, maxw, maxh, minw, minh, hintsvalid; 108 int bw, oldbw; 109 unsigned int tags; 110 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen; 111 Client *next; 112 Client *snext; 113 Monitor *mon; 114 Window win; 115 }; 116 117 typedef struct { 118 unsigned int mod; 119 KeySym keysym; 120 void (*func)(const Arg *); 121 const Arg arg; 122 } Key; 123 124 typedef struct { 125 const char *symbol; 126 void (*arrange)(Monitor *); 127 } Layout; 128 129 struct Monitor { 130 char ltsymbol[16]; 131 float mfact; 132 int nmaster; 133 int num; 134 int by; /* bar geometry */ 135 int mx, my, mw, mh; /* screen size */ 136 int wx, wy, ww, wh; /* window area */ 137 unsigned int seltags; 138 unsigned int sellt; 139 unsigned int tagset[2]; 140 int showbar; 141 int topbar; 142 Client *clients; 143 Client *sel; 144 Client *stack; 145 Monitor *next; 146 Window barwin; 147 const Layout *lt[2]; 148 }; 149 150 typedef struct { 151 const char *class; 152 const char *instance; 153 const char *title; 154 unsigned int tags; 155 int isfloating; 156 int monitor; 157 } Rule; 158 159 typedef struct Systray Systray; 160 struct Systray { 161 Window win; 162 Client *icons; 163 }; 164 165 /* function declarations */ 166 static void applyrules(Client *c); 167 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact); 168 static void arrange(Monitor *m); 169 static void arrangemon(Monitor *m); 170 static void attach(Client *c); 171 static void attachstack(Client *c); 172 static void buttonpress(XEvent *e); 173 static void checkotherwm(void); 174 static void cleanup(void); 175 static void cleanupmon(Monitor *mon); 176 static void clientmessage(XEvent *e); 177 static void configure(Client *c); 178 static void configurenotify(XEvent *e); 179 static void configurerequest(XEvent *e); 180 static Monitor *createmon(void); 181 static void destroynotify(XEvent *e); 182 static void detach(Client *c); 183 static void detachstack(Client *c); 184 static Monitor *dirtomon(int dir); 185 static void drawbar(Monitor *m); 186 static void drawbars(void); 187 static void enternotify(XEvent *e); 188 static void expose(XEvent *e); 189 static void focus(Client *c); 190 static void focusin(XEvent *e); 191 static void focusmon(const Arg *arg); 192 static void focusstack(const Arg *arg); 193 static Atom getatomprop(Client *c, Atom prop); 194 static int getrootptr(int *x, int *y); 195 static long getstate(Window w); 196 static unsigned int getsystraywidth(); 197 static int gettextprop(Window w, Atom atom, char *text, unsigned int size); 198 static void grabbuttons(Client *c, int focused); 199 static void grabkeys(void); 200 static void incnmaster(const Arg *arg); 201 static void keypress(XEvent *e); 202 static void killclient(const Arg *arg); 203 static void manage(Window w, XWindowAttributes *wa); 204 static void mappingnotify(XEvent *e); 205 static void maprequest(XEvent *e); 206 static void monocle(Monitor *m); 207 static void motionnotify(XEvent *e); 208 static void movemouse(const Arg *arg); 209 static Client *nexttiled(Client *c); 210 static void pop(Client *c); 211 static void propertynotify(XEvent *e); 212 static void quit(const Arg *arg); 213 static Monitor *recttomon(int x, int y, int w, int h); 214 static void removesystrayicon(Client *i); 215 static void resize(Client *c, int x, int y, int w, int h, int interact); 216 static void resizebarwin(Monitor *m); 217 static void resizeclient(Client *c, int x, int y, int w, int h); 218 static void resizemouse(const Arg *arg); 219 static void resizerequest(XEvent *e); 220 static void restack(Monitor *m); 221 static void run(void); 222 static void scan(void); 223 static int sendevent(Window w, Atom proto, int m, long d0, long d1, long d2, long d3, long d4); 224 static void sendmon(Client *c, Monitor *m); 225 static void setclientstate(Client *c, long state); 226 static void setfocus(Client *c); 227 static void setfullscreen(Client *c, int fullscreen); 228 static void setlayout(const Arg *arg); 229 static void setmfact(const Arg *arg); 230 static void setup(void); 231 static void seturgent(Client *c, int urg); 232 static void showhide(Client *c); 233 static void spawn(const Arg *arg); 234 static Monitor *systraytomon(Monitor *m); 235 static void tag(const Arg *arg); 236 static void tagmon(const Arg *arg); 237 static void tile(Monitor *m); 238 static void togglebar(const Arg *arg); 239 static void togglefloating(const Arg *arg); 240 static void toggletag(const Arg *arg); 241 static void toggleview(const Arg *arg); 242 static void unfocus(Client *c, int setfocus); 243 static void unmanage(Client *c, int destroyed); 244 static void unmapnotify(XEvent *e); 245 static void updatebarpos(Monitor *m); 246 static void updatebars(void); 247 static void updateclientlist(void); 248 static int updategeom(void); 249 static void updatenumlockmask(void); 250 static void updatesizehints(Client *c); 251 static void updatestatus(void); 252 static void updatesystray(void); 253 static void updatesystrayicongeom(Client *i, int w, int h); 254 static void updatesystrayiconstate(Client *i, XPropertyEvent *ev); 255 static void updatetitle(Client *c); 256 static void updatewindowtype(Client *c); 257 static void updatewmhints(Client *c); 258 static void view(const Arg *arg); 259 static Client *wintoclient(Window w); 260 static Monitor *wintomon(Window w); 261 static Client *wintosystrayicon(Window w); 262 static int xerror(Display *dpy, XErrorEvent *ee); 263 static int xerrordummy(Display *dpy, XErrorEvent *ee); 264 static int xerrorstart(Display *dpy, XErrorEvent *ee); 265 static void zoom(const Arg *arg); 266 267 /* variables */ 268 static Systray *systray = NULL; 269 static const char broken[] = "broken"; 270 static char stext[512]; 271 static int screen; 272 static int sw, sh; /* X display screen geometry width, height */ 273 static int bh; /* bar height */ 274 static int lrpad; /* sum of left and right padding for text */ 275 static int (*xerrorxlib)(Display *, XErrorEvent *); 276 static unsigned int numlockmask = 0; 277 static void (*handler[LASTEvent]) (XEvent *) = { 278 [ButtonPress] = buttonpress, 279 [ClientMessage] = clientmessage, 280 [ConfigureRequest] = configurerequest, 281 [ConfigureNotify] = configurenotify, 282 [DestroyNotify] = destroynotify, 283 [EnterNotify] = enternotify, 284 [Expose] = expose, 285 [FocusIn] = focusin, 286 [KeyPress] = keypress, 287 [MappingNotify] = mappingnotify, 288 [MapRequest] = maprequest, 289 [MotionNotify] = motionnotify, 290 [PropertyNotify] = propertynotify, 291 [ResizeRequest] = resizerequest, 292 [UnmapNotify] = unmapnotify 293 }; 294 static Atom wmatom[WMLast], netatom[NetLast], xatom[XLast]; 295 static int running = 1; 296 static Cur *cursor[CurLast]; 297 static Clr **scheme; 298 static Clr barclrs[256]; 299 static Display *dpy; 300 static Drw *drw; 301 static Monitor *mons, *selmon; 302 static Window root, wmcheckwin; 303 304 /* configuration, allows nested code to access above variables */ 305 #include "config.h" 306 307 /* compile-time check if all tags fit into an unsigned int bit array. */ 308 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; }; 309 310 /* function implementations */ 311 void 312 applyrules(Client *c) 313 { 314 const char *class, *instance; 315 unsigned int i; 316 const Rule *r; 317 Monitor *m; 318 XClassHint ch = { NULL, NULL }; 319 320 /* rule matching */ 321 c->isfloating = 0; 322 c->tags = 0; 323 XGetClassHint(dpy, c->win, &ch); 324 class = ch.res_class ? ch.res_class : broken; 325 instance = ch.res_name ? ch.res_name : broken; 326 327 for (i = 0; i < LENGTH(rules); i++) { 328 r = &rules[i]; 329 if ((!r->title || strstr(c->name, r->title)) 330 && (!r->class || strstr(class, r->class)) 331 && (!r->instance || strstr(instance, r->instance))) 332 { 333 c->isfloating = r->isfloating; 334 c->tags |= r->tags; 335 for (m = mons; m && m->num != r->monitor; m = m->next); 336 if (m) 337 c->mon = m; 338 } 339 } 340 if (ch.res_class) 341 XFree(ch.res_class); 342 if (ch.res_name) 343 XFree(ch.res_name); 344 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags]; 345 } 346 347 int 348 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) 349 { 350 int baseismin; 351 Monitor *m = c->mon; 352 353 /* set minimum possible */ 354 *w = MAX(1, *w); 355 *h = MAX(1, *h); 356 if (interact) { 357 if (*x > sw) 358 *x = sw - WIDTH(c); 359 if (*y > sh) 360 *y = sh - HEIGHT(c); 361 if (*x + *w + 2 * c->bw < 0) 362 *x = 0; 363 if (*y + *h + 2 * c->bw < 0) 364 *y = 0; 365 } else { 366 if (*x >= m->wx + m->ww) 367 *x = m->wx + m->ww - WIDTH(c); 368 if (*y >= m->wy + m->wh) 369 *y = m->wy + m->wh - HEIGHT(c); 370 if (*x + *w + 2 * c->bw <= m->wx) 371 *x = m->wx; 372 if (*y + *h + 2 * c->bw <= m->wy) 373 *y = m->wy; 374 } 375 if (*h < bh) 376 *h = bh; 377 if (*w < bh) 378 *w = bh; 379 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) { 380 if (!c->hintsvalid) 381 updatesizehints(c); 382 /* see last two sentences in ICCCM 4.1.2.3 */ 383 baseismin = c->basew == c->minw && c->baseh == c->minh; 384 if (!baseismin) { /* temporarily remove base dimensions */ 385 *w -= c->basew; 386 *h -= c->baseh; 387 } 388 /* adjust for aspect limits */ 389 if (c->mina > 0 && c->maxa > 0) { 390 if (c->maxa < (float)*w / *h) 391 *w = *h * c->maxa + 0.5; 392 else if (c->mina < (float)*h / *w) 393 *h = *w * c->mina + 0.5; 394 } 395 if (baseismin) { /* increment calculation requires this */ 396 *w -= c->basew; 397 *h -= c->baseh; 398 } 399 /* adjust for increment value */ 400 if (c->incw) 401 *w -= *w % c->incw; 402 if (c->inch) 403 *h -= *h % c->inch; 404 /* restore base dimensions */ 405 *w = MAX(*w + c->basew, c->minw); 406 *h = MAX(*h + c->baseh, c->minh); 407 if (c->maxw) 408 *w = MIN(*w, c->maxw); 409 if (c->maxh) 410 *h = MIN(*h, c->maxh); 411 } 412 return *x != c->x || *y != c->y || *w != c->w || *h != c->h; 413 } 414 415 void 416 arrange(Monitor *m) 417 { 418 if (m) 419 showhide(m->stack); 420 else for (m = mons; m; m = m->next) 421 showhide(m->stack); 422 if (m) { 423 arrangemon(m); 424 restack(m); 425 } else for (m = mons; m; m = m->next) 426 arrangemon(m); 427 } 428 429 void 430 arrangemon(Monitor *m) 431 { 432 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol); 433 if (m->lt[m->sellt]->arrange) 434 m->lt[m->sellt]->arrange(m); 435 } 436 437 void 438 attach(Client *c) 439 { 440 c->next = c->mon->clients; 441 c->mon->clients = c; 442 } 443 444 void 445 attachstack(Client *c) 446 { 447 c->snext = c->mon->stack; 448 c->mon->stack = c; 449 } 450 451 void 452 buttonpress(XEvent *e) 453 { 454 unsigned int i, x, click; 455 Arg arg = {0}; 456 Client *c; 457 Monitor *m; 458 XButtonPressedEvent *ev = &e->xbutton; 459 460 click = ClkRootWin; 461 /* focus monitor if necessary */ 462 if ((m = wintomon(ev->window)) && m != selmon) { 463 unfocus(selmon->sel, 1); 464 selmon = m; 465 focus(NULL); 466 } 467 if (ev->window == selmon->barwin) { 468 i = x = 0; 469 do 470 x += TEXTW(tags[i]); 471 while (ev->x >= x && ++i < LENGTH(tags)); 472 if (i < LENGTH(tags)) { 473 click = ClkTagBar; 474 arg.ui = 1 << i; 475 } else if (ev->x < x + TEXTW(selmon->ltsymbol)) 476 click = ClkLtSymbol; 477 else if (ev->x > selmon->ww - (int)TEXTW(stext) - getsystraywidth()) 478 click = ClkStatusText; 479 else 480 click = ClkWinTitle; 481 } else if ((c = wintoclient(ev->window))) { 482 focus(c); 483 restack(selmon); 484 XAllowEvents(dpy, ReplayPointer, CurrentTime); 485 click = ClkClientWin; 486 } 487 for (i = 0; i < LENGTH(buttons); i++) 488 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button 489 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) 490 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg); 491 } 492 493 void 494 checkotherwm(void) 495 { 496 xerrorxlib = XSetErrorHandler(xerrorstart); 497 /* this causes an error if some other window manager is running */ 498 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask); 499 XSync(dpy, False); 500 XSetErrorHandler(xerror); 501 XSync(dpy, False); 502 } 503 504 void 505 cleanup(void) 506 { 507 Arg a = {.ui = ~0}; 508 Layout foo = { "", NULL }; 509 Monitor *m; 510 size_t i; 511 512 view(&a); 513 selmon->lt[selmon->sellt] = &foo; 514 for (m = mons; m; m = m->next) 515 while (m->stack) 516 unmanage(m->stack, 0); 517 XUngrabKey(dpy, AnyKey, AnyModifier, root); 518 while (mons) 519 cleanupmon(mons); 520 521 if (showsystray) { 522 XUnmapWindow(dpy, systray->win); 523 XDestroyWindow(dpy, systray->win); 524 free(systray); 525 } 526 527 for (i = 0; i < CurLast; i++) 528 drw_cur_free(drw, cursor[i]); 529 for (i = 0; i < LENGTH(colors); i++) 530 free(scheme[i]); 531 free(scheme); 532 XDestroyWindow(dpy, wmcheckwin); 533 drw_free(drw); 534 XSync(dpy, False); 535 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); 536 XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 537 } 538 539 void 540 cleanupmon(Monitor *mon) 541 { 542 Monitor *m; 543 544 if (mon == mons) 545 mons = mons->next; 546 else { 547 for (m = mons; m && m->next != mon; m = m->next); 548 m->next = mon->next; 549 } 550 XUnmapWindow(dpy, mon->barwin); 551 XDestroyWindow(dpy, mon->barwin); 552 free(mon); 553 } 554 555 void 556 clientmessage(XEvent *e) 557 { 558 XWindowAttributes wa; 559 XSetWindowAttributes swa; 560 XClientMessageEvent *cme = &e->xclient; 561 Client *c = wintoclient(cme->window); 562 563 if (showsystray && cme->window == systray->win && cme->message_type == netatom[NetSystemTrayOP]) { 564 /* add systray icons */ 565 if (cme->data.l[1] == SYSTEM_TRAY_REQUEST_DOCK) { 566 if (!(c = (Client *)calloc(1, sizeof(Client)))) 567 die("fatal: could not malloc() %u bytes\n", sizeof(Client)); 568 if (!(c->win = cme->data.l[2])) { 569 free(c); 570 return; 571 } 572 c->mon = selmon; 573 c->next = systray->icons; 574 systray->icons = c; 575 if (!XGetWindowAttributes(dpy, c->win, &wa)) { 576 /* use sane defaults */ 577 wa.width = bh; 578 wa.height = bh; 579 wa.border_width = 0; 580 } 581 c->x = c->oldx = c->y = c->oldy = 0; 582 c->w = c->oldw = wa.width; 583 c->h = c->oldh = wa.height; 584 c->oldbw = wa.border_width; 585 c->bw = 0; 586 c->isfloating = True; 587 /* reuse tags field as mapped status */ 588 c->tags = 1; 589 updatesizehints(c); 590 updatesystrayicongeom(c, wa.width, wa.height); 591 XAddToSaveSet(dpy, c->win); 592 XSelectInput(dpy, c->win, StructureNotifyMask | PropertyChangeMask | ResizeRedirectMask); 593 XReparentWindow(dpy, c->win, systray->win, 0, 0); 594 /* use parents background color */ 595 swa.background_pixel = scheme[SchemeNorm][ColBg].pixel; 596 XChangeWindowAttributes(dpy, c->win, CWBackPixel, &swa); 597 sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_EMBEDDED_NOTIFY, 0 , systray->win, XEMBED_EMBEDDED_VERSION); 598 /* FIXME not sure if I have to send these events, too */ 599 sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_FOCUS_IN, 0 , systray->win, XEMBED_EMBEDDED_VERSION); 600 sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_WINDOW_ACTIVATE, 0 , systray->win, XEMBED_EMBEDDED_VERSION); 601 sendevent(c->win, netatom[Xembed], StructureNotifyMask, CurrentTime, XEMBED_MODALITY_ON, 0 , systray->win, XEMBED_EMBEDDED_VERSION); 602 XSync(dpy, False); 603 resizebarwin(selmon); 604 updatesystray(); 605 setclientstate(c, NormalState); 606 } 607 return; 608 } 609 610 if (!c) 611 return; 612 if (cme->message_type == netatom[NetWMState]) { 613 if (cme->data.l[1] == netatom[NetWMFullscreen] 614 || cme->data.l[2] == netatom[NetWMFullscreen]) 615 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */ 616 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen))); 617 } else if (cme->message_type == netatom[NetActiveWindow]) { 618 if (c != selmon->sel && !c->isurgent) 619 seturgent(c, 1); 620 } 621 } 622 623 void 624 configure(Client *c) 625 { 626 XConfigureEvent ce; 627 628 ce.type = ConfigureNotify; 629 ce.display = dpy; 630 ce.event = c->win; 631 ce.window = c->win; 632 ce.x = c->x; 633 ce.y = c->y; 634 ce.width = c->w; 635 ce.height = c->h; 636 ce.border_width = c->bw; 637 ce.above = None; 638 ce.override_redirect = False; 639 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce); 640 } 641 642 void 643 configurenotify(XEvent *e) 644 { 645 Monitor *m; 646 Client *c; 647 XConfigureEvent *ev = &e->xconfigure; 648 int dirty; 649 650 /* TODO: updategeom handling sucks, needs to be simplified */ 651 if (ev->window == root) { 652 dirty = (sw != ev->width || sh != ev->height); 653 sw = ev->width; 654 sh = ev->height; 655 if (updategeom() || dirty) { 656 drw_resize(drw, sw, bh); 657 updatebars(); 658 for (m = mons; m; m = m->next) { 659 for (c = m->clients; c; c = c->next) 660 if (c->isfullscreen) 661 resizeclient(c, m->mx, m->my, m->mw, m->mh); 662 resizebarwin(m); 663 } 664 focus(NULL); 665 arrange(NULL); 666 } 667 } 668 } 669 670 void 671 configurerequest(XEvent *e) 672 { 673 Client *c; 674 Monitor *m; 675 XConfigureRequestEvent *ev = &e->xconfigurerequest; 676 XWindowChanges wc; 677 678 if ((c = wintoclient(ev->window))) { 679 if (ev->value_mask & CWBorderWidth) 680 c->bw = ev->border_width; 681 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) { 682 m = c->mon; 683 if (ev->value_mask & CWX) { 684 c->oldx = c->x; 685 c->x = m->mx + ev->x; 686 } 687 if (ev->value_mask & CWY) { 688 c->oldy = c->y; 689 c->y = m->my + ev->y; 690 } 691 if (ev->value_mask & CWWidth) { 692 c->oldw = c->w; 693 c->w = ev->width; 694 } 695 if (ev->value_mask & CWHeight) { 696 c->oldh = c->h; 697 c->h = ev->height; 698 } 699 if ((c->x + c->w) > m->mx + m->mw && c->isfloating) 700 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */ 701 if ((c->y + c->h) > m->my + m->mh && c->isfloating) 702 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */ 703 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight))) 704 configure(c); 705 if (ISVISIBLE(c)) 706 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); 707 } else 708 configure(c); 709 } else { 710 wc.x = ev->x; 711 wc.y = ev->y; 712 wc.width = ev->width; 713 wc.height = ev->height; 714 wc.border_width = ev->border_width; 715 wc.sibling = ev->above; 716 wc.stack_mode = ev->detail; 717 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc); 718 } 719 XSync(dpy, False); 720 } 721 722 Monitor * 723 createmon(void) 724 { 725 Monitor *m; 726 727 m = ecalloc(1, sizeof(Monitor)); 728 m->tagset[0] = m->tagset[1] = 1; 729 m->mfact = mfact; 730 m->nmaster = nmaster; 731 m->showbar = showbar; 732 m->topbar = topbar; 733 m->lt[0] = &layouts[0]; 734 m->lt[1] = &layouts[1 % LENGTH(layouts)]; 735 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol); 736 return m; 737 } 738 739 void 740 destroynotify(XEvent *e) 741 { 742 Client *c; 743 XDestroyWindowEvent *ev = &e->xdestroywindow; 744 745 if ((c = wintoclient(ev->window))) 746 unmanage(c, 1); 747 else if ((c = wintosystrayicon(ev->window))) { 748 removesystrayicon(c); 749 resizebarwin(selmon); 750 updatesystray(); 751 } 752 } 753 754 void 755 detach(Client *c) 756 { 757 Client **tc; 758 759 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next); 760 *tc = c->next; 761 } 762 763 void 764 detachstack(Client *c) 765 { 766 Client **tc, *t; 767 768 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext); 769 *tc = c->snext; 770 771 if (c == c->mon->sel) { 772 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext); 773 c->mon->sel = t; 774 } 775 } 776 777 Monitor * 778 dirtomon(int dir) 779 { 780 Monitor *m = NULL; 781 782 if (dir > 0) { 783 if (!(m = selmon->next)) 784 m = mons; 785 } else if (selmon == mons) 786 for (m = mons; m->next; m = m->next); 787 else 788 for (m = mons; m->next != selmon; m = m->next); 789 return m; 790 } 791 792 void 793 resetfntlist(Fnt *orighead, Fnt *curhead) 794 { 795 if (orighead != curhead) { 796 Fnt *f; 797 for (f = orighead; f->next; f = f->next); 798 f->next = curhead; 799 for (f = f->next; f->next != orighead; f = f->next); 800 f->next = NULL; 801 } 802 } 803 804 enum SgrFlags { 805 REVERSE = 1 << 0, 806 UNDERLINE = 1 << 1, 807 STRIKETHROUGH = 1 << 2, 808 OVERLINE = 1 << 3 809 }; 810 811 void 812 drawbar(Monitor *m) 813 { 814 int x, w, tw = 0, stw = 0; 815 int boxs = drw->fonts->h / 9; 816 int boxw = drw->fonts->h / 6 + 2; 817 unsigned int i, occ = 0, urg = 0; 818 Client *c; 819 820 if (!m->showbar) 821 return; 822 823 if(showsystray && m == systraytomon(m) && !systrayonleft) 824 stw = getsystraywidth(); 825 826 /* draw status first so it can be overdrawn by tags later */ 827 if (m == selmon) { /* status is only drawn on selected monitor */ 828 char buffer[sizeof(stext)]; 829 Clr scm[3]; 830 int wr, rd; 831 int pw; 832 int fg = 7; 833 int bg = 0; 834 int fmt = 0; 835 int lp = lrpad / 2 - 2; 836 Fnt *fset = drw->fonts; 837 838 memcpy(scm, scheme[SchemeNorm], sizeof(scm)); 839 840 drw_setscheme(drw, scm); 841 842 for (tw = 0, wr = 0, rd = 0; stext[rd]; rd++) { 843 if (stext[rd] == '\033' && stext[rd + 1] == '[') { 844 size_t alen = strspn(stext + rd + 2, 845 "0123456789;"); 846 if (stext[rd + alen + 2] == 'm') { 847 if (wr) { 848 buffer[wr] = '\0'; 849 tw += TEXTW(buffer) - lrpad; 850 wr = 0; 851 } 852 853 char *ep = stext + rd + 1; 854 while (*ep != 'm') { 855 unsigned v = strtoul(ep + 1, &ep, 10); 856 if (v == 0 || (v >= 10 && v <= 19)) { 857 int fi = v % 10; 858 Fnt *f; 859 Fnt *p; 860 resetfntlist(fset, drw->fonts); 861 for (p = NULL, f = fset; f && fi--; p = f, f = f->next); 862 if (f) { 863 if (p) { 864 p->next = NULL; 865 for (p = f; p->next; p = p->next); 866 p->next = fset; 867 } 868 drw_setfontset(drw, f); 869 } else { 870 drw_setfontset(drw, fset); 871 } 872 } else if (v == 48 || v == 38) { 873 break; 874 } 875 } 876 877 rd += alen + 2; 878 continue; 879 } 880 } 881 buffer[wr++] = stext[rd]; 882 } 883 buffer[wr] = '\0'; 884 885 tw += TEXTW(buffer) - lrpad / 2 + 2; 886 x = m->ww - tw - stw; 887 888 resetfntlist(fset, drw->fonts); 889 drw_setfontset(drw, fset); 890 891 for (wr = 0, rd = 0; stext[rd]; rd++) { 892 if (stext[rd] == '' && stext[rd + 1] == '[') { 893 size_t alen = strspn(stext + rd + 2, 894 "0123456789;"); 895 if (stext[rd + alen + 2] == 'm') { 896 if (wr) { 897 buffer[wr] = '\0'; 898 pw = TEXTW(buffer) - lrpad + lp; 899 drw_text(drw, x, 0, pw, bh, lp, buffer, fmt & REVERSE); 900 if (fmt & UNDERLINE) 901 drw_rect(drw, x, (bh + drw->fonts->h) / 2, pw, 1, 1, fmt & REVERSE); 902 if (fmt & STRIKETHROUGH) 903 drw_rect(drw, x, bh / 2, pw, 1, 1, fmt & REVERSE); 904 if (fmt & OVERLINE) 905 drw_rect(drw, x, (bh - drw->fonts->h) / 2, pw, 1, 1, fmt & REVERSE); 906 x += pw; 907 lp = 0; 908 } 909 910 char *ep = stext + rd + 1; 911 int ignore = 0; 912 int bgfg = 0; 913 while (*ep != 'm') { 914 unsigned v = strtoul(ep + 1, &ep, 10); 915 if (ignore) 916 continue; 917 if (bgfg) { 918 if (bgfg < 4 && v == 5) { 919 bgfg <<= 1; 920 continue; 921 } 922 if (bgfg == 4) 923 scm[0] = barclrs[fg = v]; 924 else if (bgfg == 6) 925 scm[1] = barclrs[bg = v]; 926 ignore = 1; 927 928 continue; 929 } 930 if (v == 0) { 931 memcpy(scm, scheme[SchemeNorm], sizeof(scm)); 932 fg = 7; 933 bg = 0; 934 fmt = 0; 935 resetfntlist(fset, drw->fonts); 936 drw_setfontset(drw, fset); 937 } else if (v == 1) { 938 fg |= 8; 939 scm[0] = barclrs[fg]; 940 } else if (v == 4) { 941 fmt |= UNDERLINE; 942 } else if (v == 7) { 943 fmt |= REVERSE; 944 } else if (v == 9) { 945 fmt |= STRIKETHROUGH; 946 } else if (v >= 10 && v <= 19) { 947 int fi = v % 10; 948 Fnt *f; 949 Fnt *p; 950 resetfntlist(fset, drw->fonts); 951 for (p = NULL, f = fset; f && fi--; p = f, f = f->next); 952 if (f) { 953 if (p) { 954 p->next = NULL; 955 for (p = f; p->next; p = p->next); 956 p->next = fset; 957 } 958 drw_setfontset(drw, f); 959 } else { 960 drw_setfontset(drw, fset); 961 } 962 } else if (v == 22) { 963 fg &= ~8; 964 scm[0] = barclrs[fg]; 965 } else if (v == 24) { 966 fmt &= ~UNDERLINE; 967 } else if (v == 27) { 968 fmt &= ~REVERSE; 969 } else if (v == 29) { 970 fmt &= ~STRIKETHROUGH; 971 } else if (v >= 30 && v <= 37) { 972 fg = v % 10 | (fg & 8); 973 scm[0] = barclrs[fg]; 974 } else if (v == 38) { 975 bgfg = 2; 976 } else if (v >= 40 && v <= 47) { 977 bg = v % 10; 978 scm[1] = barclrs[bg]; 979 } else if (v == 48) { 980 bgfg = 3; 981 } else if (v == 53) { 982 fmt |= OVERLINE; 983 } else if (v == 55) { 984 fmt &= ~OVERLINE; 985 } 986 } 987 988 rd += alen + 2; 989 wr = 0; 990 991 drw_setscheme(drw, scm); 992 continue; 993 } 994 } 995 buffer[wr++] = stext[rd]; 996 } 997 998 buffer[wr] = '\0'; 999 pw = TEXTW(buffer) - lrpad + lp + (lrpad & 1); 1000 drw_text(drw, x, 0, pw + 4, bh, lp, buffer, fmt & REVERSE); 1001 if (fmt & UNDERLINE) 1002 drw_rect(drw, x, (bh + drw->fonts->h) / 2, pw, 1, 1, fmt & REVERSE); 1003 if (fmt & STRIKETHROUGH) 1004 drw_rect(drw, x, bh / 2, pw, 1, 1, fmt & REVERSE); 1005 if (fmt & OVERLINE) 1006 drw_rect(drw, x, (bh - drw->fonts->h) / 2, pw, 1, 1, fmt & REVERSE); 1007 1008 resetfntlist(fset, drw->fonts); 1009 drw_setscheme(drw, scheme[SchemeNorm]); 1010 } 1011 1012 resizebarwin(m); 1013 for (c = m->clients; c; c = c->next) { 1014 occ |= c->tags; 1015 if (c->isurgent) 1016 urg |= c->tags; 1017 } 1018 x = 0; 1019 for (i = 0; i < LENGTH(tags); i++) { 1020 w = TEXTW(tags[i]); 1021 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]); 1022 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i); 1023 if (occ & 1 << i) 1024 drw_rect(drw, x + boxs, boxs, boxw, boxw, 1025 m == selmon && selmon->sel && selmon->sel->tags & 1 << i, 1026 urg & 1 << i); 1027 x += w; 1028 } 1029 w = TEXTW(m->ltsymbol); 1030 drw_setscheme(drw, scheme[SchemeNorm]); 1031 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0); 1032 1033 if ((w = m->ww - tw - stw - x) > bh) { 1034 if (m->sel) { 1035 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]); 1036 drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0); 1037 if (m->sel->isfloating) 1038 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0); 1039 } else { 1040 drw_setscheme(drw, scheme[SchemeNorm]); 1041 drw_rect(drw, x, 0, w, bh, 1, 1); 1042 } 1043 } 1044 drw_map(drw, m->barwin, 0, 0, m->ww - stw, bh); 1045 } 1046 1047 void 1048 drawbars(void) 1049 { 1050 Monitor *m; 1051 1052 for (m = mons; m; m = m->next) 1053 drawbar(m); 1054 } 1055 1056 void 1057 enternotify(XEvent *e) 1058 { 1059 Client *c; 1060 Monitor *m; 1061 XCrossingEvent *ev = &e->xcrossing; 1062 1063 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root) 1064 return; 1065 c = wintoclient(ev->window); 1066 m = c ? c->mon : wintomon(ev->window); 1067 if (m != selmon) { 1068 unfocus(selmon->sel, 1); 1069 selmon = m; 1070 } else if (!c || c == selmon->sel) 1071 return; 1072 focus(c); 1073 } 1074 1075 void 1076 expose(XEvent *e) 1077 { 1078 Monitor *m; 1079 XExposeEvent *ev = &e->xexpose; 1080 1081 if (ev->count == 0 && (m = wintomon(ev->window))) { 1082 drawbar(m); 1083 if (m == selmon) 1084 updatesystray(); 1085 } 1086 } 1087 1088 void 1089 focus(Client *c) 1090 { 1091 if (!c || !ISVISIBLE(c)) 1092 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext); 1093 if (selmon->sel && selmon->sel != c) 1094 unfocus(selmon->sel, 0); 1095 if (c) { 1096 if (c->mon != selmon) 1097 selmon = c->mon; 1098 if (c->isurgent) 1099 seturgent(c, 0); 1100 detachstack(c); 1101 attachstack(c); 1102 grabbuttons(c, 1); 1103 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel); 1104 setfocus(c); 1105 } else { 1106 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); 1107 XDeleteProperty(dpy, root, netatom[NetActiveWindow]); 1108 } 1109 selmon->sel = c; 1110 drawbars(); 1111 } 1112 1113 /* there are some broken focus acquiring clients needing extra handling */ 1114 void 1115 focusin(XEvent *e) 1116 { 1117 XFocusChangeEvent *ev = &e->xfocus; 1118 1119 if (selmon->sel && ev->window != selmon->sel->win) 1120 setfocus(selmon->sel); 1121 } 1122 1123 void 1124 focusmon(const Arg *arg) 1125 { 1126 Monitor *m; 1127 1128 if (!mons->next) 1129 return; 1130 if ((m = dirtomon(arg->i)) == selmon) 1131 return; 1132 unfocus(selmon->sel, 0); 1133 selmon = m; 1134 focus(NULL); 1135 } 1136 1137 void 1138 focusstack(const Arg *arg) 1139 { 1140 Client *c = NULL, *i; 1141 1142 if (!selmon->sel || (selmon->sel->isfullscreen && lockfullscreen)) 1143 return; 1144 if (arg->i > 0) { 1145 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next); 1146 if (!c) 1147 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next); 1148 } else { 1149 for (i = selmon->clients; i != selmon->sel; i = i->next) 1150 if (ISVISIBLE(i)) 1151 c = i; 1152 if (!c) 1153 for (; i; i = i->next) 1154 if (ISVISIBLE(i)) 1155 c = i; 1156 } 1157 if (c) { 1158 focus(c); 1159 restack(selmon); 1160 } 1161 } 1162 1163 Atom 1164 getatomprop(Client *c, Atom prop) 1165 { 1166 int di; 1167 unsigned long dl; 1168 unsigned char *p = NULL; 1169 Atom da, atom = None; 1170 1171 /* FIXME getatomprop should return the number of items and a pointer to 1172 * the stored data instead of this workaround */ 1173 Atom req = XA_ATOM; 1174 if (prop == xatom[XembedInfo]) 1175 req = xatom[XembedInfo]; 1176 1177 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, req, 1178 &da, &di, &dl, &dl, &p) == Success && p) { 1179 atom = *(Atom *)p; 1180 if (da == xatom[XembedInfo] && dl == 2) 1181 atom = ((Atom *)p)[1]; 1182 XFree(p); 1183 } 1184 return atom; 1185 } 1186 1187 int 1188 getrootptr(int *x, int *y) 1189 { 1190 int di; 1191 unsigned int dui; 1192 Window dummy; 1193 1194 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui); 1195 } 1196 1197 long 1198 getstate(Window w) 1199 { 1200 int format; 1201 long result = -1; 1202 unsigned char *p = NULL; 1203 unsigned long n, extra; 1204 Atom real; 1205 1206 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState], 1207 &real, &format, &n, &extra, (unsigned char **)&p) != Success) 1208 return -1; 1209 if (n != 0) 1210 result = *p; 1211 XFree(p); 1212 return result; 1213 } 1214 1215 unsigned int 1216 getsystraywidth() 1217 { 1218 unsigned int w = 0; 1219 Client *i; 1220 if(showsystray) 1221 for(i = systray->icons; i; w += i->w + systrayspacing, i = i->next) ; 1222 return w ? w + systrayspacing : 1; 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 / 60)) 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 resizemouse(const Arg *arg) 1659 { 1660 int ocx, ocy, nw, nh; 1661 Client *c; 1662 Monitor *m; 1663 XEvent ev; 1664 Time lasttime = 0; 1665 1666 if (!(c = selmon->sel)) 1667 return; 1668 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */ 1669 return; 1670 restack(selmon); 1671 ocx = c->x; 1672 ocy = c->y; 1673 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync, 1674 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess) 1675 return; 1676 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1); 1677 do { 1678 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev); 1679 switch(ev.type) { 1680 case ConfigureRequest: 1681 case Expose: 1682 case MapRequest: 1683 handler[ev.type](&ev); 1684 break; 1685 case MotionNotify: 1686 if ((ev.xmotion.time - lasttime) <= (1000 / 60)) 1687 continue; 1688 lasttime = ev.xmotion.time; 1689 1690 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1); 1691 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1); 1692 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww 1693 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh) 1694 { 1695 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange 1696 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap)) 1697 togglefloating(NULL); 1698 } 1699 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating) 1700 resize(c, c->x, c->y, nw, nh, 1); 1701 break; 1702 } 1703 } while (ev.type != ButtonRelease); 1704 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1); 1705 XUngrabPointer(dpy, CurrentTime); 1706 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev)); 1707 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) { 1708 sendmon(c, m); 1709 selmon = m; 1710 focus(NULL); 1711 } 1712 } 1713 1714 void 1715 resizerequest(XEvent *e) 1716 { 1717 XResizeRequestEvent *ev = &e->xresizerequest; 1718 Client *i; 1719 1720 if ((i = wintosystrayicon(ev->window))) { 1721 updatesystrayicongeom(i, ev->width, ev->height); 1722 resizebarwin(selmon); 1723 updatesystray(); 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 focus(NULL); 1803 arrange(NULL); 1804 } 1805 1806 void 1807 setclientstate(Client *c, long state) 1808 { 1809 long data[] = { state, None }; 1810 1811 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32, 1812 PropModeReplace, (unsigned char *)data, 2); 1813 } 1814 1815 int 1816 sendevent(Window w, Atom proto, int mask, long d0, long d1, long d2, long d3, long d4) 1817 { 1818 int n; 1819 Atom *protocols, mt; 1820 int exists = 0; 1821 XEvent ev; 1822 1823 if (proto == wmatom[WMTakeFocus] || proto == wmatom[WMDelete]) { 1824 mt = wmatom[WMProtocols]; 1825 if (XGetWMProtocols(dpy, w, &protocols, &n)) { 1826 while (!exists && n--) 1827 exists = protocols[n] == proto; 1828 XFree(protocols); 1829 } 1830 } 1831 else { 1832 exists = True; 1833 mt = proto; 1834 } 1835 1836 if (exists) { 1837 ev.type = ClientMessage; 1838 ev.xclient.window = w; 1839 ev.xclient.message_type = mt; 1840 ev.xclient.format = 32; 1841 ev.xclient.data.l[0] = d0; 1842 ev.xclient.data.l[1] = d1; 1843 ev.xclient.data.l[2] = d2; 1844 ev.xclient.data.l[3] = d3; 1845 ev.xclient.data.l[4] = d4; 1846 XSendEvent(dpy, w, False, mask, &ev); 1847 } 1848 return exists; 1849 } 1850 1851 void 1852 setfocus(Client *c) 1853 { 1854 if (!c->neverfocus) { 1855 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); 1856 XChangeProperty(dpy, root, netatom[NetActiveWindow], 1857 XA_WINDOW, 32, PropModeReplace, 1858 (unsigned char *) &(c->win), 1); 1859 } 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() 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 }