Xinqi Bao's Git
   1 /* See LICENSE file for copyright and license details. 
   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. 
   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 
  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 
  19  * Keys and tagging rules are organized as arrays and defined in config.h. 
  21  * To understand everything else, start reading main(). 
  32 #include <sys/types.h> 
  34 #include <X11/cursorfont.h> 
  35 #include <X11/keysym.h> 
  36 #include <X11/Xatom.h> 
  38 #include <X11/Xproto.h> 
  39 #include <X11/Xutil.h> 
  41 #include <X11/extensions/Xinerama.h> 
  43 #include <X11/Xft/Xft.h> 
  49 #define BUTTONMASK              (ButtonPressMask|ButtonReleaseMask) 
  50 #define CLEANMASK(mask)         (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask)) 
  51 #define INTERSECT(x,y,w,h,m)    (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \ 
  52                                * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy))) 
  53 #define ISVISIBLE(C)            ((C->tags & C->mon->tagset[C->mon->seltags])) 
  54 #define LENGTH(X)               (sizeof X / sizeof X[0]) 
  55 #define MOUSEMASK               (BUTTONMASK|PointerMotionMask) 
  56 #define WIDTH(X)                ((X)->w + 2 * (X)->bw) 
  57 #define HEIGHT(X)               ((X)->h + 2 * (X)->bw) 
  58 #define TAGMASK                 ((1 << LENGTH(tags)) - 1) 
  59 #define TEXTW(X)                (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h) 
  62 enum { CurNormal
, CurResize
, CurMove
, CurLast 
}; /* cursor */ 
  63 enum { SchemeNorm
, SchemeSel
, SchemeLast 
}; /* color schemes */ 
  64 enum { NetSupported
, NetWMName
, NetWMState
, 
  65        NetWMFullscreen
, NetActiveWindow
, NetWMWindowType
, 
  66        NetWMWindowTypeDialog
, NetClientList
, NetLast 
}; /* EWMH atoms */ 
  67 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, WMLast 
}; /* default atoms */ 
  68 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
, 
  69        ClkClientWin
, ClkRootWin
, ClkLast 
}; /* clicks */ 
  82         void (*func
)(const Arg 
*arg
); 
  86 typedef struct Monitor Monitor
; 
  87 typedef struct Client Client
; 
  92         int oldx
, oldy
, oldw
, oldh
; 
  93         int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
; 
  96         bool isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
; 
 106         void (*func
)(const Arg 
*); 
 112         void (*arrange
)(Monitor 
*); 
 120         int by
;               /* bar geometry */ 
 121         int mx
, my
, mw
, mh
;   /* screen size */ 
 122         int wx
, wy
, ww
, wh
;   /* window area  */ 
 123         unsigned int seltags
; 
 125         unsigned int tagset
[2]; 
 138         const char *instance
; 
 145 /* function declarations */ 
 146 static void applyrules(Client 
*c
); 
 147 static bool applysizehints(Client 
*c
, int *x
, int *y
, int *w
, int *h
, bool interact
); 
 148 static void arrange(Monitor 
*m
); 
 149 static void arrangemon(Monitor 
*m
); 
 150 static void attach(Client 
*c
); 
 151 static void attachstack(Client 
*c
); 
 152 static void buttonpress(XEvent 
*e
); 
 153 static void checkotherwm(void); 
 154 static void cleanup(void); 
 155 static void cleanupmon(Monitor 
*mon
); 
 156 static void clearurgent(Client 
*c
); 
 157 static void clientmessage(XEvent 
*e
); 
 158 static void configure(Client 
*c
); 
 159 static void configurenotify(XEvent 
*e
); 
 160 static void configurerequest(XEvent 
*e
); 
 161 static Monitor 
*createmon(void); 
 162 static void destroynotify(XEvent 
*e
); 
 163 static void detach(Client 
*c
); 
 164 static void detachstack(Client 
*c
); 
 165 static Monitor 
*dirtomon(int dir
); 
 166 static void drawbar(Monitor 
*m
); 
 167 static void drawbars(void); 
 168 static void enternotify(XEvent 
*e
); 
 169 static void expose(XEvent 
*e
); 
 170 static void focus(Client 
*c
); 
 171 static void focusin(XEvent 
*e
); 
 172 static void focusmon(const Arg 
*arg
); 
 173 static void focusstack(const Arg 
*arg
); 
 174 static bool getrootptr(int *x
, int *y
); 
 175 static long getstate(Window w
); 
 176 static bool gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
); 
 177 static void grabbuttons(Client 
*c
, bool focused
); 
 178 static void grabkeys(void); 
 179 static void incnmaster(const Arg 
*arg
); 
 180 static void keypress(XEvent 
*e
); 
 181 static void killclient(const Arg 
*arg
); 
 182 static void manage(Window w
, XWindowAttributes 
*wa
); 
 183 static void mappingnotify(XEvent 
*e
); 
 184 static void maprequest(XEvent 
*e
); 
 185 static void monocle(Monitor 
*m
); 
 186 static void motionnotify(XEvent 
*e
); 
 187 static void movemouse(const Arg 
*arg
); 
 188 static Client 
*nexttiled(Client 
*c
); 
 189 static void pop(Client 
*); 
 190 static void propertynotify(XEvent 
*e
); 
 191 static void quit(const Arg 
*arg
); 
 192 static Monitor 
*recttomon(int x
, int y
, int w
, int h
); 
 193 static void resize(Client 
*c
, int x
, int y
, int w
, int h
, bool interact
); 
 194 static void resizeclient(Client 
*c
, int x
, int y
, int w
, int h
); 
 195 static void resizemouse(const Arg 
*arg
); 
 196 static void restack(Monitor 
*m
); 
 197 static void run(void); 
 198 static void scan(void); 
 199 static bool sendevent(Client 
*c
, Atom proto
); 
 200 static void sendmon(Client 
*c
, Monitor 
*m
); 
 201 static void setclientstate(Client 
*c
, long state
); 
 202 static void setfocus(Client 
*c
); 
 203 static void setfullscreen(Client 
*c
, bool fullscreen
); 
 204 static void setlayout(const Arg 
*arg
); 
 205 static void setmfact(const Arg 
*arg
); 
 206 static void setup(void); 
 207 static void showhide(Client 
*c
); 
 208 static void sigchld(int unused
); 
 209 static void spawn(const Arg 
*arg
); 
 210 static void tag(const Arg 
*arg
); 
 211 static void tagmon(const Arg 
*arg
); 
 212 static void tile(Monitor 
*); 
 213 static void togglebar(const Arg 
*arg
); 
 214 static void togglefloating(const Arg 
*arg
); 
 215 static void toggletag(const Arg 
*arg
); 
 216 static void toggleview(const Arg 
*arg
); 
 217 static void unfocus(Client 
*c
, bool setfocus
); 
 218 static void unmanage(Client 
*c
, bool destroyed
); 
 219 static void unmapnotify(XEvent 
*e
); 
 220 static bool updategeom(void); 
 221 static void updatebarpos(Monitor 
*m
); 
 222 static void updatebars(void); 
 223 static void updateclientlist(void); 
 224 static void updatenumlockmask(void); 
 225 static void updatesizehints(Client 
*c
); 
 226 static void updatestatus(void); 
 227 static void updatewindowtype(Client 
*c
); 
 228 static void updatetitle(Client 
*c
); 
 229 static void updatewmhints(Client 
*c
); 
 230 static void view(const Arg 
*arg
); 
 231 static Client 
*wintoclient(Window w
); 
 232 static Monitor 
*wintomon(Window w
); 
 233 static int xerror(Display 
*dpy
, XErrorEvent 
*ee
); 
 234 static int xerrordummy(Display 
*dpy
, XErrorEvent 
*ee
); 
 235 static int xerrorstart(Display 
*dpy
, XErrorEvent 
*ee
); 
 236 static void zoom(const Arg 
*arg
); 
 239 static const char broken
[] = "broken"; 
 240 static char stext
[256]; 
 242 static int sw
, sh
;           /* X display screen geometry width, height */ 
 243 static int bh
, blw 
= 0;      /* bar geometry */ 
 244 static int (*xerrorxlib
)(Display 
*, XErrorEvent 
*); 
 245 static unsigned int numlockmask 
= 0; 
 246 static void (*handler
[LASTEvent
]) (XEvent 
*) = { 
 247         [ButtonPress
] = buttonpress
, 
 248         [ClientMessage
] = clientmessage
, 
 249         [ConfigureRequest
] = configurerequest
, 
 250         [ConfigureNotify
] = configurenotify
, 
 251         [DestroyNotify
] = destroynotify
, 
 252         [EnterNotify
] = enternotify
, 
 255         [KeyPress
] = keypress
, 
 256         [MappingNotify
] = mappingnotify
, 
 257         [MapRequest
] = maprequest
, 
 258         [MotionNotify
] = motionnotify
, 
 259         [PropertyNotify
] = propertynotify
, 
 260         [UnmapNotify
] = unmapnotify
 
 262 static Atom wmatom
[WMLast
], netatom
[NetLast
]; 
 263 static bool running 
= true; 
 264 static Cur 
*cursor
[CurLast
]; 
 265 static ClrScheme scheme
[SchemeLast
]; 
 268 static Monitor 
*mons
, *selmon
; 
 271 /* configuration, allows nested code to access above variables */ 
 274 /* compile-time check if all tags fit into an unsigned int bit array. */ 
 275 struct NumTags 
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; }; 
 277 /* function implementations */ 
 279 applyrules(Client 
*c
) { 
 280         const char *class, *instance
; 
 284         XClassHint ch 
= { NULL
, NULL 
}; 
 287         c
->isfloating 
= false; 
 289         XGetClassHint(dpy
, c
->win
, &ch
); 
 290         class    = ch
.res_class 
? ch
.res_class 
: broken
; 
 291         instance 
= ch
.res_name  
? ch
.res_name  
: broken
; 
 293         for(i 
= 0; i 
< LENGTH(rules
); i
++) { 
 295                 if((!r
->title 
|| strstr(c
->name
, r
->title
)) 
 296                 && (!r
->class || strstr(class, r
->class)) 
 297                 && (!r
->instance 
|| strstr(instance
, r
->instance
))) 
 299                         c
->isfloating 
= r
->isfloating
; 
 301                         for(m 
= mons
; m 
&& m
->num 
!= r
->monitor
; m 
= m
->next
); 
 310         c
->tags 
= c
->tags 
& TAGMASK 
? c
->tags 
& TAGMASK 
: c
->mon
->tagset
[c
->mon
->seltags
]; 
 314 applysizehints(Client 
*c
, int *x
, int *y
, int *w
, int *h
, bool interact
) { 
 318         /* set minimum possible */ 
 326                 if(*x 
+ *w 
+ 2 * c
->bw 
< 0) 
 328                 if(*y 
+ *h 
+ 2 * c
->bw 
< 0) 
 332                 if(*x 
>= m
->wx 
+ m
->ww
) 
 333                         *x 
= m
->wx 
+ m
->ww 
- WIDTH(c
); 
 334                 if(*y 
>= m
->wy 
+ m
->wh
) 
 335                         *y 
= m
->wy 
+ m
->wh 
- HEIGHT(c
); 
 336                 if(*x 
+ *w 
+ 2 * c
->bw 
<= m
->wx
) 
 338                 if(*y 
+ *h 
+ 2 * c
->bw 
<= m
->wy
) 
 345         if(resizehints 
|| c
->isfloating 
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) { 
 346                 /* see last two sentences in ICCCM 4.1.2.3 */ 
 347                 baseismin 
= c
->basew 
== c
->minw 
&& c
->baseh 
== c
->minh
; 
 348                 if(!baseismin
) { /* temporarily remove base dimensions */ 
 352                 /* adjust for aspect limits */ 
 353                 if(c
->mina 
> 0 && c
->maxa 
> 0) { 
 354                         if(c
->maxa 
< (float)*w 
/ *h
) 
 355                                 *w 
= *h 
* c
->maxa 
+ 0.5; 
 356                         else if(c
->mina 
< (float)*h 
/ *w
) 
 357                                 *h 
= *w 
* c
->mina 
+ 0.5; 
 359                 if(baseismin
) { /* increment calculation requires this */ 
 363                 /* adjust for increment value */ 
 368                 /* restore base dimensions */ 
 369                 *w 
= MAX(*w 
+ c
->basew
, c
->minw
); 
 370                 *h 
= MAX(*h 
+ c
->baseh
, c
->minh
); 
 372                         *w 
= MIN(*w
, c
->maxw
); 
 374                         *h 
= MIN(*h
, c
->maxh
); 
 376         return *x 
!= c
->x 
|| *y 
!= c
->y 
|| *w 
!= c
->w 
|| *h 
!= c
->h
; 
 380 arrange(Monitor 
*m
) { 
 383         else for(m 
= mons
; m
; m 
= m
->next
) 
 388         } else for(m 
= mons
; m
; m 
= m
->next
) 
 393 arrangemon(Monitor 
*m
) { 
 394         strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
); 
 395         if(m
->lt
[m
->sellt
]->arrange
) 
 396                 m
->lt
[m
->sellt
]->arrange(m
); 
 401         c
->next 
= c
->mon
->clients
; 
 406 attachstack(Client 
*c
) { 
 407         c
->snext 
= c
->mon
->stack
; 
 412 buttonpress(XEvent 
*e
) { 
 413         unsigned int i
, x
, click
; 
 417         XButtonPressedEvent 
*ev 
= &e
->xbutton
; 
 420         /* focus monitor if necessary */ 
 421         if((m 
= wintomon(ev
->window
)) && m 
!= selmon
) { 
 422                 unfocus(selmon
->sel
, true); 
 426         if(ev
->window 
== selmon
->barwin
) { 
 430                 while(ev
->x 
>= x 
&& ++i 
< LENGTH(tags
)); 
 431                 if(i 
< LENGTH(tags
)) { 
 435                 else if(ev
->x 
< x 
+ blw
) 
 437                 else if(ev
->x 
> selmon
->ww 
- TEXTW(stext
)) 
 438                         click 
= ClkStatusText
; 
 442         else if((c 
= wintoclient(ev
->window
))) { 
 444                 click 
= ClkClientWin
; 
 446         for(i 
= 0; i 
< LENGTH(buttons
); i
++) 
 447                 if(click 
== buttons
[i
].click 
&& buttons
[i
].func 
&& buttons
[i
].button 
== ev
->button
 
 448                 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
)) 
 449                         buttons
[i
].func(click 
== ClkTagBar 
&& buttons
[i
].arg
.i 
== 0 ? &arg 
: &buttons
[i
].arg
); 
 454         xerrorxlib 
= XSetErrorHandler(xerrorstart
); 
 455         /* this causes an error if some other window manager is running */ 
 456         XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
); 
 458         XSetErrorHandler(xerror
); 
 465         Layout foo 
= { "", NULL 
}; 
 470         selmon
->lt
[selmon
->sellt
] = &foo
; 
 471         for(m 
= mons
; m
; m 
= m
->next
) 
 473                         unmanage(m
->stack
, false); 
 474         XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
); 
 477         for(i 
= 0; i 
< CurLast
; i
++) 
 478                 drw_cur_free(drw
, cursor
[i
]); 
 479         for(i 
= 0; i 
< SchemeLast
; i
++) { 
 480                 drw_clr_free(scheme
[i
].border
); 
 481                 drw_clr_free(scheme
[i
].bg
); 
 482                 drw_clr_free(scheme
[i
].fg
); 
 486         XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
); 
 487         XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]); 
 491 cleanupmon(Monitor 
*mon
) { 
 497                 for(m 
= mons
; m 
&& m
->next 
!= mon
; m 
= m
->next
); 
 500         XUnmapWindow(dpy
, mon
->barwin
); 
 501         XDestroyWindow(dpy
, mon
->barwin
); 
 506 clearurgent(Client 
*c
) { 
 510         if(!(wmh 
= XGetWMHints(dpy
, c
->win
))) 
 512         wmh
->flags 
&= ~XUrgencyHint
; 
 513         XSetWMHints(dpy
, c
->win
, wmh
); 
 518 clientmessage(XEvent 
*e
) { 
 519         XClientMessageEvent 
*cme 
= &e
->xclient
; 
 520         Client 
*c 
= wintoclient(cme
->window
); 
 524         if(cme
->message_type 
== netatom
[NetWMState
]) { 
 525                 if(cme
->data
.l
[1] == netatom
[NetWMFullscreen
] || cme
->data
.l
[2] == netatom
[NetWMFullscreen
]) 
 526                         setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD    */ 
 527                                       || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
))); 
 529         else if(cme
->message_type 
== netatom
[NetActiveWindow
]) { 
 531                         c
->mon
->seltags 
^= 1; 
 532                         c
->mon
->tagset
[c
->mon
->seltags
] = c
->tags
; 
 539 configure(Client 
*c
) { 
 542         ce
.type 
= ConfigureNotify
; 
 550         ce
.border_width 
= c
->bw
; 
 552         ce
.override_redirect 
= False
; 
 553         XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent 
*)&ce
); 
 557 configurenotify(XEvent 
*e
) { 
 559         XConfigureEvent 
*ev 
= &e
->xconfigure
; 
 562         /* TODO: updategeom handling sucks, needs to be simplified */ 
 563         if(ev
->window 
== root
) { 
 564                 dirty 
= (sw 
!= ev
->width 
|| sh 
!= ev
->height
); 
 567                 if(updategeom() || dirty
) { 
 568                         drw_resize(drw
, sw
, bh
); 
 570                         for(m 
= mons
; m
; m 
= m
->next
) 
 571                                 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
); 
 579 configurerequest(XEvent 
*e
) { 
 582         XConfigureRequestEvent 
*ev 
= &e
->xconfigurerequest
; 
 585         if((c 
= wintoclient(ev
->window
))) { 
 586                 if(ev
->value_mask 
& CWBorderWidth
) 
 587                         c
->bw 
= ev
->border_width
; 
 588                 else if(c
->isfloating 
|| !selmon
->lt
[selmon
->sellt
]->arrange
) { 
 590                         if(ev
->value_mask 
& CWX
) { 
 592                                 c
->x 
= m
->mx 
+ ev
->x
; 
 594                         if(ev
->value_mask 
& CWY
) { 
 596                                 c
->y 
= m
->my 
+ ev
->y
; 
 598                         if(ev
->value_mask 
& CWWidth
) { 
 602                         if(ev
->value_mask 
& CWHeight
) { 
 606                         if((c
->x 
+ c
->w
) > m
->mx 
+ m
->mw 
&& c
->isfloating
) 
 607                                 c
->x 
= m
->mx 
+ (m
->mw 
/ 2 - WIDTH(c
) / 2); /* center in x direction */ 
 608                         if((c
->y 
+ c
->h
) > m
->my 
+ m
->mh 
&& c
->isfloating
) 
 609                                 c
->y 
= m
->my 
+ (m
->mh 
/ 2 - HEIGHT(c
) / 2); /* center in y direction */ 
 610                         if((ev
->value_mask 
& (CWX
|CWY
)) && !(ev
->value_mask 
& (CWWidth
|CWHeight
))) 
 613                                 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); 
 621                 wc
.width 
= ev
->width
; 
 622                 wc
.height 
= ev
->height
; 
 623                 wc
.border_width 
= ev
->border_width
; 
 624                 wc
.sibling 
= ev
->above
; 
 625                 wc
.stack_mode 
= ev
->detail
; 
 626                 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
); 
 635         m 
= ecalloc(1, sizeof(Monitor
)); 
 636         m
->tagset
[0] = m
->tagset
[1] = 1; 
 638         m
->nmaster 
= nmaster
; 
 639         m
->showbar 
= showbar
; 
 641         m
->lt
[0] = &layouts
[0]; 
 642         m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)]; 
 643         strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
); 
 648 destroynotify(XEvent 
*e
) { 
 650         XDestroyWindowEvent 
*ev 
= &e
->xdestroywindow
; 
 652         if((c 
= wintoclient(ev
->window
))) 
 660         for(tc 
= &c
->mon
->clients
; *tc 
&& *tc 
!= c
; tc 
= &(*tc
)->next
); 
 665 detachstack(Client 
*c
) { 
 668         for(tc 
= &c
->mon
->stack
; *tc 
&& *tc 
!= c
; tc 
= &(*tc
)->snext
); 
 671         if(c 
== c
->mon
->sel
) { 
 672                 for(t 
= c
->mon
->stack
; t 
&& !ISVISIBLE(t
); t 
= t
->snext
); 
 682                 if(!(m 
= selmon
->next
)) 
 685         else if(selmon 
== mons
) 
 686                 for(m 
= mons
; m
->next
; m 
= m
->next
); 
 688                 for(m 
= mons
; m
->next 
!= selmon
; m 
= m
->next
); 
 693 drawbar(Monitor 
*m
) { 
 695         unsigned int i
, occ 
= 0, urg 
= 0; 
 698         dx 
= (drw
->fonts
[0]->ascent 
+ drw
->fonts
[0]->descent 
+ 2) / 4; 
 700         for(c 
= m
->clients
; c
; c 
= c
->next
) { 
 706         for(i 
= 0; i 
< LENGTH(tags
); i
++) { 
 708                 drw_setscheme(drw
, m
->tagset
[m
->seltags
] & 1 << i 
? &scheme
[SchemeSel
] : &scheme
[SchemeNorm
]); 
 709                 drw_text(drw
, x
, 0, w
, bh
, tags
[i
], urg 
& 1 << i
); 
 710                 drw_rect(drw
, x 
+ 1, 1, dx
, dx
, m 
== selmon 
&& selmon
->sel 
&& selmon
->sel
->tags 
& 1 << i
, 
 711                            occ 
& 1 << i
, urg 
& 1 << i
); 
 714         w 
= blw 
= TEXTW(m
->ltsymbol
); 
 715         drw_setscheme(drw
, &scheme
[SchemeNorm
]); 
 716         drw_text(drw
, x
, 0, w
, bh
, m
->ltsymbol
, 0); 
 719         if(m 
== selmon
) { /* status is only drawn on selected monitor */ 
 726                 drw_text(drw
, x
, 0, w
, bh
, stext
, 0); 
 730         if((w 
= x 
- xx
) > bh
) { 
 733                         drw_setscheme(drw
, m 
== selmon 
? &scheme
[SchemeSel
] : &scheme
[SchemeNorm
]); 
 734                         drw_text(drw
, x
, 0, w
, bh
, m
->sel
->name
, 0); 
 735                         drw_rect(drw
, x 
+ 1, 1, dx
, dx
, m
->sel
->isfixed
, m
->sel
->isfloating
, 0); 
 738                         drw_setscheme(drw
, &scheme
[SchemeNorm
]); 
 739                         drw_rect(drw
, x
, 0, w
, bh
, 1, 0, 1); 
 742         drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
); 
 749         for(m 
= mons
; m
; m 
= m
->next
) 
 754 enternotify(XEvent 
*e
) { 
 757         XCrossingEvent 
*ev 
= &e
->xcrossing
; 
 759         if((ev
->mode 
!= NotifyNormal 
|| ev
->detail 
== NotifyInferior
) && ev
->window 
!= root
) 
 761         c 
= wintoclient(ev
->window
); 
 762         m 
= c 
? c
->mon 
: wintomon(ev
->window
); 
 764                 unfocus(selmon
->sel
, true); 
 767         else if(!c 
|| c 
== selmon
->sel
) 
 775         XExposeEvent 
*ev 
= &e
->xexpose
; 
 777         if(ev
->count 
== 0 && (m 
= wintomon(ev
->window
))) 
 783         if(!c 
|| !ISVISIBLE(c
)) 
 784                 for(c 
= selmon
->stack
; c 
&& !ISVISIBLE(c
); c 
= c
->snext
); 
 785         /* was if(selmon->sel) */ 
 786         if(selmon
->sel 
&& selmon
->sel 
!= c
) 
 787                 unfocus(selmon
->sel
, false); 
 795                 grabbuttons(c
, true); 
 796                 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
].border
->pix
); 
 800                 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
); 
 801                 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]); 
 808 focusin(XEvent 
*e
) { /* there are some broken focus acquiring clients */ 
 809         XFocusChangeEvent 
*ev 
= &e
->xfocus
; 
 811         if(selmon
->sel 
&& ev
->window 
!= selmon
->sel
->win
) 
 812                 setfocus(selmon
->sel
); 
 816 focusmon(const Arg 
*arg
) { 
 821         if((m 
= dirtomon(arg
->i
)) == selmon
) 
 823         unfocus(selmon
->sel
, false); /* s/true/false/ fixes input focus issues 
 824                                         in gedit and anjuta */ 
 830 focusstack(const Arg 
*arg
) { 
 831         Client 
*c 
= NULL
, *i
; 
 836                 for(c 
= selmon
->sel
->next
; c 
&& !ISVISIBLE(c
); c 
= c
->next
); 
 838                         for(c 
= selmon
->clients
; c 
&& !ISVISIBLE(c
); c 
= c
->next
); 
 841                 for(i 
= selmon
->clients
; i 
!= selmon
->sel
; i 
= i
->next
) 
 845                         for(; i
; i 
= i
->next
) 
 856 getatomprop(Client 
*c
, Atom prop
) { 
 859         unsigned char *p 
= NULL
; 
 860         Atom da
, atom 
= None
; 
 862         if(XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
, 
 863                               &da
, &di
, &dl
, &dl
, &p
) == Success 
&& p
) { 
 871 getrootptr(int *x
, int *y
) { 
 876         return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
) == True
; 
 883         unsigned char *p 
= NULL
; 
 884         unsigned long n
, extra
; 
 887         if(XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
], 
 888                               &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
) 
 897 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) { 
 902         if(!text 
|| size 
== 0) 
 905         XGetTextProperty(dpy
, w
, &name
, atom
); 
 908         if(name
.encoding 
== XA_STRING
) 
 909                 strncpy(text
, (char *)name
.value
, size 
- 1); 
 911                 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success 
&& n 
> 0 && *list
) { 
 912                         strncpy(text
, *list
, size 
- 1); 
 913                         XFreeStringList(list
); 
 916         text
[size 
- 1] = '\0'; 
 922 grabbuttons(Client 
*c
, bool focused
) { 
 926                 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask 
}; 
 927                 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
); 
 929                         for(i 
= 0; i 
< LENGTH(buttons
); i
++) 
 930                                 if(buttons
[i
].click 
== ClkClientWin
) 
 931                                         for(j 
= 0; j 
< LENGTH(modifiers
); j
++) 
 932                                                 XGrabButton(dpy
, buttons
[i
].button
, 
 933                                                             buttons
[i
].mask 
| modifiers
[j
], 
 934                                                             c
->win
, False
, BUTTONMASK
, 
 935                                                             GrabModeAsync
, GrabModeSync
, None
, None
); 
 938                         XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, 
 939                                     BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
); 
 948                 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask 
}; 
 951                 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
); 
 952                 for(i 
= 0; i 
< LENGTH(keys
); i
++) 
 953                         if((code 
= XKeysymToKeycode(dpy
, keys
[i
].keysym
))) 
 954                                 for(j 
= 0; j 
< LENGTH(modifiers
); j
++) 
 955                                         XGrabKey(dpy
, code
, keys
[i
].mod 
| modifiers
[j
], root
, 
 956                                                  True
, GrabModeAsync
, GrabModeAsync
); 
 961 incnmaster(const Arg 
*arg
) { 
 962         selmon
->nmaster 
= MAX(selmon
->nmaster 
+ arg
->i
, 0); 
 968 isuniquegeom(XineramaScreenInfo 
*unique
, size_t n
, XineramaScreenInfo 
*info
) { 
 970                 if(unique
[n
].x_org 
== info
->x_org 
&& unique
[n
].y_org 
== info
->y_org
 
 971                 && unique
[n
].width 
== info
->width 
&& unique
[n
].height 
== info
->height
) 
 975 #endif /* XINERAMA */ 
 978 keypress(XEvent 
*e
) { 
 984         keysym 
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0); 
 985         for(i 
= 0; i 
< LENGTH(keys
); i
++) 
 986                 if(keysym 
== keys
[i
].keysym
 
 987                 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
) 
 989                         keys
[i
].func(&(keys
[i
].arg
)); 
 993 killclient(const Arg 
*arg
) { 
 996         if(!sendevent(selmon
->sel
, wmatom
[WMDelete
])) { 
 998                 XSetErrorHandler(xerrordummy
); 
 999                 XSetCloseDownMode(dpy
, DestroyAll
); 
1000                 XKillClient(dpy
, selmon
->sel
->win
); 
1002                 XSetErrorHandler(xerror
); 
1008 manage(Window w
, XWindowAttributes 
*wa
) { 
1009         Client 
*c
, *t 
= NULL
; 
1010         Window trans 
= None
; 
1013         c 
= ecalloc(1, sizeof(Client
)); 
1016         if(XGetTransientForHint(dpy
, w
, &trans
) && (t 
= wintoclient(trans
))) { 
1025         c
->x 
= c
->oldx 
= wa
->x
; 
1026         c
->y 
= c
->oldy 
= wa
->y
; 
1027         c
->w 
= c
->oldw 
= wa
->width
; 
1028         c
->h 
= c
->oldh 
= wa
->height
; 
1029         c
->oldbw 
= wa
->border_width
; 
1031         if(c
->x 
+ WIDTH(c
) > c
->mon
->mx 
+ c
->mon
->mw
) 
1032                 c
->x 
= c
->mon
->mx 
+ c
->mon
->mw 
- WIDTH(c
); 
1033         if(c
->y 
+ HEIGHT(c
) > c
->mon
->my 
+ c
->mon
->mh
) 
1034                 c
->y 
= c
->mon
->my 
+ c
->mon
->mh 
- HEIGHT(c
); 
1035         c
->x 
= MAX(c
->x
, c
->mon
->mx
); 
1036         /* only fix client y-offset, if the client center might cover the bar */ 
1037         c
->y 
= MAX(c
->y
, ((c
->mon
->by 
== c
->mon
->my
) && (c
->x 
+ (c
->w 
/ 2) >= c
->mon
->wx
) 
1038                    && (c
->x 
+ (c
->w 
/ 2) < c
->mon
->wx 
+ c
->mon
->ww
)) ? bh 
: c
->mon
->my
); 
1041         wc
.border_width 
= c
->bw
; 
1042         XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
); 
1043         XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
].border
->pix
); 
1044         configure(c
); /* propagates border_width, if size doesn't change */ 
1045         updatewindowtype(c
); 
1048         XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
); 
1049         grabbuttons(c
, false); 
1051                 c
->isfloating 
= c
->oldstate 
= trans 
!= None 
|| c
->isfixed
; 
1053                 XRaiseWindow(dpy
, c
->win
); 
1056         XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
, 
1057                         (unsigned char *) &(c
->win
), 1); 
1058         XMoveResizeWindow(dpy
, c
->win
, c
->x 
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */ 
1059         setclientstate(c
, NormalState
); 
1060         if (c
->mon 
== selmon
) 
1061                 unfocus(selmon
->sel
, false); 
1064         XMapWindow(dpy
, c
->win
); 
1069 mappingnotify(XEvent 
*e
) { 
1070         XMappingEvent 
*ev 
= &e
->xmapping
; 
1072         XRefreshKeyboardMapping(ev
); 
1073         if(ev
->request 
== MappingKeyboard
) 
1078 maprequest(XEvent 
*e
) { 
1079         static XWindowAttributes wa
; 
1080         XMapRequestEvent 
*ev 
= &e
->xmaprequest
; 
1082         if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
)) 
1084         if(wa
.override_redirect
) 
1086         if(!wintoclient(ev
->window
)) 
1087                 manage(ev
->window
, &wa
); 
1091 monocle(Monitor 
*m
) { 
1095         for(c 
= m
->clients
; c
; c 
= c
->next
) 
1098         if(n 
> 0) /* override layout symbol */ 
1099                 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
); 
1100         for(c 
= nexttiled(m
->clients
); c
; c 
= nexttiled(c
->next
)) 
1101                 resize(c
, m
->wx
, m
->wy
, m
->ww 
- 2 * c
->bw
, m
->wh 
- 2 * c
->bw
, false); 
1105 motionnotify(XEvent 
*e
) { 
1106         static Monitor 
*mon 
= NULL
; 
1108         XMotionEvent 
*ev 
= &e
->xmotion
; 
1110         if(ev
->window 
!= root
) 
1112         if((m 
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon 
&& mon
) { 
1113                 unfocus(selmon
->sel
, true); 
1121 movemouse(const Arg 
*arg
) { 
1122         int x
, y
, ocx
, ocy
, nx
, ny
; 
1128         if(!(c 
= selmon
->sel
)) 
1130         if(c
->isfullscreen
) /* no support moving fullscreen windows by mouse */ 
1135         if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
, 
1136         None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
) 
1138         if(!getrootptr(&x
, &y
)) 
1141                 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
); 
1143                 case ConfigureRequest
: 
1146                         handler
[ev
.type
](&ev
); 
1149                         if ((ev
.xmotion
.time 
- lasttime
) <= (1000 / 60)) 
1151                         lasttime 
= ev
.xmotion
.time
; 
1153                         nx 
= ocx 
+ (ev
.xmotion
.x 
- x
); 
1154                         ny 
= ocy 
+ (ev
.xmotion
.y 
- y
); 
1155                         if(nx 
>= selmon
->wx 
&& nx 
<= selmon
->wx 
+ selmon
->ww
 
1156                         && ny 
>= selmon
->wy 
&& ny 
<= selmon
->wy 
+ selmon
->wh
) { 
1157                                 if(abs(selmon
->wx 
- nx
) < snap
) 
1159                                 else if(abs((selmon
->wx 
+ selmon
->ww
) - (nx 
+ WIDTH(c
))) < snap
) 
1160                                         nx 
= selmon
->wx 
+ selmon
->ww 
- WIDTH(c
); 
1161                                 if(abs(selmon
->wy 
- ny
) < snap
) 
1163                                 else if(abs((selmon
->wy 
+ selmon
->wh
) - (ny 
+ HEIGHT(c
))) < snap
) 
1164                                         ny 
= selmon
->wy 
+ selmon
->wh 
- HEIGHT(c
); 
1165                                 if(!c
->isfloating 
&& selmon
->lt
[selmon
->sellt
]->arrange
 
1166                                 && (abs(nx 
- c
->x
) > snap 
|| abs(ny 
- c
->y
) > snap
)) 
1167                                         togglefloating(NULL
); 
1169                         if(!selmon
->lt
[selmon
->sellt
]->arrange 
|| c
->isfloating
) 
1170                                 resize(c
, nx
, ny
, c
->w
, c
->h
, true); 
1173         } while(ev
.type 
!= ButtonRelease
); 
1174         XUngrabPointer(dpy
, CurrentTime
); 
1175         if((m 
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) { 
1183 nexttiled(Client 
*c
) { 
1184         for(; c 
&& (c
->isfloating 
|| !ISVISIBLE(c
)); c 
= c
->next
); 
1197 propertynotify(XEvent 
*e
) { 
1200         XPropertyEvent 
*ev 
= &e
->xproperty
; 
1202         if((ev
->window 
== root
) && (ev
->atom 
== XA_WM_NAME
)) 
1204         else if(ev
->state 
== PropertyDelete
) 
1205                 return; /* ignore */ 
1206         else if((c 
= wintoclient(ev
->window
))) { 
1209                 case XA_WM_TRANSIENT_FOR
: 
1210                         if(!c
->isfloating 
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) && 
1211                            (c
->isfloating 
= (wintoclient(trans
)) != NULL
)) 
1214                 case XA_WM_NORMAL_HINTS
: 
1222                 if(ev
->atom 
== XA_WM_NAME 
|| ev
->atom 
== netatom
[NetWMName
]) { 
1224                         if(c 
== c
->mon
->sel
) 
1227                 if(ev
->atom 
== netatom
[NetWMWindowType
]) 
1228                         updatewindowtype(c
); 
1233 quit(const Arg 
*arg
) { 
1238 recttomon(int x
, int y
, int w
, int h
) { 
1239         Monitor 
*m
, *r 
= selmon
; 
1242         for(m 
= mons
; m
; m 
= m
->next
) 
1243                 if((a 
= INTERSECT(x
, y
, w
, h
, m
)) > area
) { 
1251 resize(Client 
*c
, int x
, int y
, int w
, int h
, bool interact
) { 
1252         if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
)) 
1253                 resizeclient(c
, x
, y
, w
, h
); 
1257 resizeclient(Client 
*c
, int x
, int y
, int w
, int h
) { 
1260         c
->oldx 
= c
->x
; c
->x 
= wc
.x 
= x
; 
1261         c
->oldy 
= c
->y
; c
->y 
= wc
.y 
= y
; 
1262         c
->oldw 
= c
->w
; c
->w 
= wc
.width 
= w
; 
1263         c
->oldh 
= c
->h
; c
->h 
= wc
.height 
= h
; 
1264         wc
.border_width 
= c
->bw
; 
1265         XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
); 
1271 resizemouse(const Arg 
*arg
) { 
1272         int ocx
, ocy
, nw
, nh
; 
1278         if(!(c 
= selmon
->sel
)) 
1280         if(c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */ 
1285         if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
, 
1286                         None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
) 
1288         XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w 
+ c
->bw 
- 1, c
->h 
+ c
->bw 
- 1); 
1290                 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
); 
1292                 case ConfigureRequest
: 
1295                         handler
[ev
.type
](&ev
); 
1298                         if ((ev
.xmotion
.time 
- lasttime
) <= (1000 / 60)) 
1300                         lasttime 
= ev
.xmotion
.time
; 
1302                         nw 
= MAX(ev
.xmotion
.x 
- ocx 
- 2 * c
->bw 
+ 1, 1); 
1303                         nh 
= MAX(ev
.xmotion
.y 
- ocy 
- 2 * c
->bw 
+ 1, 1); 
1304                         if(c
->mon
->wx 
+ nw 
>= selmon
->wx 
&& c
->mon
->wx 
+ nw 
<= selmon
->wx 
+ selmon
->ww
 
1305                         && c
->mon
->wy 
+ nh 
>= selmon
->wy 
&& c
->mon
->wy 
+ nh 
<= selmon
->wy 
+ selmon
->wh
) 
1307                                 if(!c
->isfloating 
&& selmon
->lt
[selmon
->sellt
]->arrange
 
1308                                 && (abs(nw 
- c
->w
) > snap 
|| abs(nh 
- c
->h
) > snap
)) 
1309                                         togglefloating(NULL
); 
1311                         if(!selmon
->lt
[selmon
->sellt
]->arrange 
|| c
->isfloating
) 
1312                                 resize(c
, c
->x
, c
->y
, nw
, nh
, true); 
1315         } while(ev
.type 
!= ButtonRelease
); 
1316         XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w 
+ c
->bw 
- 1, c
->h 
+ c
->bw 
- 1); 
1317         XUngrabPointer(dpy
, CurrentTime
); 
1318         while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
)); 
1319         if((m 
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) { 
1327 restack(Monitor 
*m
) { 
1335         if(m
->sel
->isfloating 
|| !m
->lt
[m
->sellt
]->arrange
) 
1336                 XRaiseWindow(dpy
, m
->sel
->win
); 
1337         if(m
->lt
[m
->sellt
]->arrange
) { 
1338                 wc
.stack_mode 
= Below
; 
1339                 wc
.sibling 
= m
->barwin
; 
1340                 for(c 
= m
->stack
; c
; c 
= c
->snext
) 
1341                         if(!c
->isfloating 
&& ISVISIBLE(c
)) { 
1342                                 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
); 
1343                                 wc
.sibling 
= c
->win
; 
1347         while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
)); 
1353         /* main event loop */ 
1355         while(running 
&& !XNextEvent(dpy
, &ev
)) 
1356                 if(handler
[ev
.type
]) 
1357                         handler
[ev
.type
](&ev
); /* call handler */ 
1362         unsigned int i
, num
; 
1363         Window d1
, d2
, *wins 
= NULL
; 
1364         XWindowAttributes wa
; 
1366         if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) { 
1367                 for(i 
= 0; i 
< num
; i
++) { 
1368                         if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
) 
1369                         || wa
.override_redirect 
|| XGetTransientForHint(dpy
, wins
[i
], &d1
)) 
1371                         if(wa
.map_state 
== IsViewable 
|| getstate(wins
[i
]) == IconicState
) 
1372                                 manage(wins
[i
], &wa
); 
1374                 for(i 
= 0; i 
< num
; i
++) { /* now the transients */ 
1375                         if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)) 
1377                         if(XGetTransientForHint(dpy
, wins
[i
], &d1
) 
1378                         && (wa
.map_state 
== IsViewable 
|| getstate(wins
[i
]) == IconicState
)) 
1379                                 manage(wins
[i
], &wa
); 
1387 sendmon(Client 
*c
, Monitor 
*m
) { 
1394         c
->tags 
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */ 
1402 setclientstate(Client 
*c
, long state
) { 
1403         long data
[] = { state
, None 
}; 
1405         XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32, 
1406                         PropModeReplace
, (unsigned char *)data
, 2); 
1410 sendevent(Client 
*c
, Atom proto
) { 
1413         bool exists 
= false; 
1416         if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) { 
1417                 while(!exists 
&& n
--) 
1418                         exists 
= protocols
[n
] == proto
; 
1422                 ev
.type 
= ClientMessage
; 
1423                 ev
.xclient
.window 
= c
->win
; 
1424                 ev
.xclient
.message_type 
= wmatom
[WMProtocols
]; 
1425                 ev
.xclient
.format 
= 32; 
1426                 ev
.xclient
.data
.l
[0] = proto
; 
1427                 ev
.xclient
.data
.l
[1] = CurrentTime
; 
1428                 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
); 
1434 setfocus(Client 
*c
) { 
1435         if(!c
->neverfocus
) { 
1436                 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
); 
1437                 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
], 
1438                                 XA_WINDOW
, 32, PropModeReplace
, 
1439                                 (unsigned char *) &(c
->win
), 1); 
1441         sendevent(c
, wmatom
[WMTakeFocus
]); 
1445 setfullscreen(Client 
*c
, bool fullscreen
) { 
1446         if(fullscreen 
&& !c
->isfullscreen
) { 
1447                 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32, 
1448                                 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1); 
1449                 c
->isfullscreen 
= true; 
1450                 c
->oldstate 
= c
->isfloating
; 
1453                 c
->isfloating 
= true; 
1454                 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
); 
1455                 XRaiseWindow(dpy
, c
->win
); 
1457         else if(!fullscreen 
&& c
->isfullscreen
){ 
1458                 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32, 
1459                                 PropModeReplace
, (unsigned char*)0, 0); 
1460                 c
->isfullscreen 
= false; 
1461                 c
->isfloating 
= c
->oldstate
; 
1467                 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
); 
1473 setlayout(const Arg 
*arg
) { 
1474         if(!arg 
|| !arg
->v 
|| arg
->v 
!= selmon
->lt
[selmon
->sellt
]) 
1477                 selmon
->lt
[selmon
->sellt
] = (Layout 
*)arg
->v
; 
1478         strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
); 
1485 /* arg > 1.0 will set mfact absolutly */ 
1487 setmfact(const Arg 
*arg
) { 
1490         if(!arg 
|| !selmon
->lt
[selmon
->sellt
]->arrange
) 
1492         f 
= arg
->f 
< 1.0 ? arg
->f 
+ selmon
->mfact 
: arg
->f 
- 1.0; 
1493         if(f 
< 0.1 || f 
> 0.9) 
1501         XSetWindowAttributes wa
; 
1503         /* clean up any zombies immediately */ 
1507         screen 
= DefaultScreen(dpy
); 
1508         sw 
= DisplayWidth(dpy
, screen
); 
1509         sh 
= DisplayHeight(dpy
, screen
); 
1510         root 
= RootWindow(dpy
, screen
); 
1511         drw 
= drw_create(dpy
, screen
, root
, sw
, sh
); 
1512         drw_load_fonts(drw
, fonts
, LENGTH(fonts
)); 
1513         if (!drw
->fontcount
) 
1514                 die("no fonts could be loaded.\n"); 
1515         bh 
= drw
->fonts
[0]->h 
+ 2; 
1518         wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
); 
1519         wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
); 
1520         wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
); 
1521         wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
); 
1522         netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
); 
1523         netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
); 
1524         netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
); 
1525         netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
); 
1526         netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
); 
1527         netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
); 
1528         netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
); 
1529         netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
); 
1531         cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
); 
1532         cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
); 
1533         cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
); 
1534         /* init appearance */ 
1535         scheme
[SchemeNorm
].border 
= drw_clr_create(drw
, normbordercolor
); 
1536         scheme
[SchemeNorm
].bg 
= drw_clr_create(drw
, normbgcolor
); 
1537         scheme
[SchemeNorm
].fg 
= drw_clr_create(drw
, normfgcolor
); 
1538         scheme
[SchemeSel
].border 
= drw_clr_create(drw
, selbordercolor
); 
1539         scheme
[SchemeSel
].bg 
= drw_clr_create(drw
, selbgcolor
); 
1540         scheme
[SchemeSel
].fg 
= drw_clr_create(drw
, selfgcolor
); 
1544         /* EWMH support per view */ 
1545         XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32, 
1546                         PropModeReplace
, (unsigned char *) netatom
, NetLast
); 
1547         XDeleteProperty(dpy
, root
, netatom
[NetClientList
]); 
1548         /* select for events */ 
1549         wa
.cursor 
= cursor
[CurNormal
]->cursor
; 
1550         wa
.event_mask 
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
|PointerMotionMask
 
1551                         |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
; 
1552         XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
); 
1553         XSelectInput(dpy
, root
, wa
.event_mask
); 
1559 showhide(Client 
*c
) { 
1562         if(ISVISIBLE(c
)) { /* show clients top down */ 
1563                 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
); 
1564                 if((!c
->mon
->lt
[c
->mon
->sellt
]->arrange 
|| c
->isfloating
) && !c
->isfullscreen
) 
1565                         resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, false); 
1568         else { /* hide clients bottom up */ 
1570                 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
); 
1575 sigchld(int unused
) { 
1576         if(signal(SIGCHLD
, sigchld
) == SIG_ERR
) 
1577                 die("can't install SIGCHLD handler:"); 
1578         while(0 < waitpid(-1, NULL
, WNOHANG
)); 
1582 spawn(const Arg 
*arg
) { 
1583         if(arg
->v 
== dmenucmd
) 
1584                 dmenumon
[0] = '0' + selmon
->num
; 
1587                         close(ConnectionNumber(dpy
)); 
1589                 execvp(((char **)arg
->v
)[0], (char **)arg
->v
); 
1590                 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]); 
1597 tag(const Arg 
*arg
) { 
1598         if(selmon
->sel 
&& arg
->ui 
& TAGMASK
) { 
1599                 selmon
->sel
->tags 
= arg
->ui 
& TAGMASK
; 
1606 tagmon(const Arg 
*arg
) { 
1607         if(!selmon
->sel 
|| !mons
->next
) 
1609         sendmon(selmon
->sel
, dirtomon(arg
->i
)); 
1614         unsigned int i
, n
, h
, mw
, my
, ty
; 
1617         for(n 
= 0, c 
= nexttiled(m
->clients
); c
; c 
= nexttiled(c
->next
), n
++); 
1622                 mw 
= m
->nmaster 
? m
->ww 
* m
->mfact 
: 0; 
1625         for(i 
= my 
= ty 
= 0, c 
= nexttiled(m
->clients
); c
; c 
= nexttiled(c
->next
), i
++) 
1626                 if(i 
< m
->nmaster
) { 
1627                         h 
= (m
->wh 
- my
) / (MIN(n
, m
->nmaster
) - i
); 
1628                         resize(c
, m
->wx
, m
->wy 
+ my
, mw 
- (2*c
->bw
), h 
- (2*c
->bw
), false); 
1632                         h 
= (m
->wh 
- ty
) / (n 
- i
); 
1633                         resize(c
, m
->wx 
+ mw
, m
->wy 
+ ty
, m
->ww 
- mw 
- (2*c
->bw
), h 
- (2*c
->bw
), false); 
1639 togglebar(const Arg 
*arg
) { 
1640         selmon
->showbar 
= !selmon
->showbar
; 
1641         updatebarpos(selmon
); 
1642         XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
); 
1647 togglefloating(const Arg 
*arg
) { 
1650         if(selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */ 
1652         selmon
->sel
->isfloating 
= !selmon
->sel
->isfloating 
|| selmon
->sel
->isfixed
; 
1653         if(selmon
->sel
->isfloating
) 
1654                 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
, 
1655                        selmon
->sel
->w
, selmon
->sel
->h
, false); 
1660 toggletag(const Arg 
*arg
) { 
1661         unsigned int newtags
; 
1665         newtags 
= selmon
->sel
->tags 
^ (arg
->ui 
& TAGMASK
); 
1667                 selmon
->sel
->tags 
= newtags
; 
1674 toggleview(const Arg 
*arg
) { 
1675         unsigned int newtagset 
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui 
& TAGMASK
); 
1678                 selmon
->tagset
[selmon
->seltags
] = newtagset
; 
1685 unfocus(Client 
*c
, bool setfocus
) { 
1688         grabbuttons(c
, false); 
1689         XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
].border
->pix
); 
1691                 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
); 
1692                 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]); 
1697 unmanage(Client 
*c
, bool destroyed
) { 
1698         Monitor 
*m 
= c
->mon
; 
1701         /* The server grab construct avoids race conditions. */ 
1705                 wc
.border_width 
= c
->oldbw
; 
1707                 XSetErrorHandler(xerrordummy
); 
1708                 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */ 
1709                 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
); 
1710                 setclientstate(c
, WithdrawnState
); 
1712                 XSetErrorHandler(xerror
); 
1722 unmapnotify(XEvent 
*e
) { 
1724         XUnmapEvent 
*ev 
= &e
->xunmap
; 
1726         if((c 
= wintoclient(ev
->window
))) { 
1728                         setclientstate(c
, WithdrawnState
); 
1737         XSetWindowAttributes wa 
= { 
1738                 .override_redirect 
= True
, 
1739                 .background_pixmap 
= ParentRelative
, 
1740                 .event_mask 
= ButtonPressMask
|ExposureMask
 
1742         for(m 
= mons
; m
; m 
= m
->next
) { 
1745                 m
->barwin 
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
), 
1746                                           CopyFromParent
, DefaultVisual(dpy
, screen
), 
1747                                           CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
); 
1748                 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
); 
1749                 XMapRaised(dpy
, m
->barwin
); 
1754 updatebarpos(Monitor 
*m
) { 
1759                 m
->by 
= m
->topbar 
? m
->wy 
: m
->wy 
+ m
->wh
; 
1760                 m
->wy 
= m
->topbar 
? m
->wy 
+ bh 
: m
->wy
; 
1767 updateclientlist() { 
1771         XDeleteProperty(dpy
, root
, netatom
[NetClientList
]); 
1772         for(m 
= mons
; m
; m 
= m
->next
) 
1773                 for(c 
= m
->clients
; c
; c 
= c
->next
) 
1774                         XChangeProperty(dpy
, root
, netatom
[NetClientList
], 
1775                                         XA_WINDOW
, 32, PropModeAppend
, 
1776                                         (unsigned char *) &(c
->win
), 1); 
1784         if(XineramaIsActive(dpy
)) { 
1788                 XineramaScreenInfo 
*info 
= XineramaQueryScreens(dpy
, &nn
); 
1789                 XineramaScreenInfo 
*unique 
= NULL
; 
1791                 for(n 
= 0, m 
= mons
; m
; m 
= m
->next
, n
++); 
1792                 /* only consider unique geometries as separate screens */ 
1793                 unique 
= ecalloc(nn
, sizeof(XineramaScreenInfo
)); 
1794                 for(i 
= 0, j 
= 0; i 
< nn
; i
++) 
1795                         if(isuniquegeom(unique
, j
, &info
[i
])) 
1796                                 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
)); 
1800                         for(i 
= 0; i 
< (nn 
- n
); i
++) { /* new monitors available */ 
1801                                 for(m 
= mons
; m 
&& m
->next
; m 
= m
->next
); 
1803                                         m
->next 
= createmon(); 
1807                         for(i 
= 0, m 
= mons
; i 
< nn 
&& m
; m 
= m
->next
, i
++) 
1809                                 || (unique
[i
].x_org 
!= m
->mx 
|| unique
[i
].y_org 
!= m
->my
 
1810                                     || unique
[i
].width 
!= m
->mw 
|| unique
[i
].height 
!= m
->mh
)) 
1814                                         m
->mx 
= m
->wx 
= unique
[i
].x_org
; 
1815                                         m
->my 
= m
->wy 
= unique
[i
].y_org
; 
1816                                         m
->mw 
= m
->ww 
= unique
[i
].width
; 
1817                                         m
->mh 
= m
->wh 
= unique
[i
].height
; 
1821                 else { /* less monitors available nn < n */ 
1822                         for(i 
= nn
; i 
< n
; i
++) { 
1823                                 for(m 
= mons
; m 
&& m
->next
; m 
= m
->next
); 
1827                                         m
->clients 
= c
->next
; 
1841 #endif /* XINERAMA */ 
1842         /* default monitor setup */ 
1846                 if(mons
->mw 
!= sw 
|| mons
->mh 
!= sh
) { 
1848                         mons
->mw 
= mons
->ww 
= sw
; 
1849                         mons
->mh 
= mons
->wh 
= sh
; 
1855                 selmon 
= wintomon(root
); 
1861 updatenumlockmask(void) { 
1863         XModifierKeymap 
*modmap
; 
1866         modmap 
= XGetModifierMapping(dpy
); 
1867         for(i 
= 0; i 
< 8; i
++) 
1868                 for(j 
= 0; j 
< modmap
->max_keypermod
; j
++) 
1869                         if(modmap
->modifiermap
[i 
* modmap
->max_keypermod 
+ j
] 
1870                            == XKeysymToKeycode(dpy
, XK_Num_Lock
)) 
1871                                 numlockmask 
= (1 << i
); 
1872         XFreeModifiermap(modmap
); 
1876 updatesizehints(Client 
*c
) { 
1880         if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
)) 
1881                 /* size is uninitialized, ensure that size.flags aren't used */ 
1883         if(size
.flags 
& PBaseSize
) { 
1884                 c
->basew 
= size
.base_width
; 
1885                 c
->baseh 
= size
.base_height
; 
1887         else if(size
.flags 
& PMinSize
) { 
1888                 c
->basew 
= size
.min_width
; 
1889                 c
->baseh 
= size
.min_height
; 
1892                 c
->basew 
= c
->baseh 
= 0; 
1893         if(size
.flags 
& PResizeInc
) { 
1894                 c
->incw 
= size
.width_inc
; 
1895                 c
->inch 
= size
.height_inc
; 
1898                 c
->incw 
= c
->inch 
= 0; 
1899         if(size
.flags 
& PMaxSize
) { 
1900                 c
->maxw 
= size
.max_width
; 
1901                 c
->maxh 
= size
.max_height
; 
1904                 c
->maxw 
= c
->maxh 
= 0; 
1905         if(size
.flags 
& PMinSize
) { 
1906                 c
->minw 
= size
.min_width
; 
1907                 c
->minh 
= size
.min_height
; 
1909         else if(size
.flags 
& PBaseSize
) { 
1910                 c
->minw 
= size
.base_width
; 
1911                 c
->minh 
= size
.base_height
; 
1914                 c
->minw 
= c
->minh 
= 0; 
1915         if(size
.flags 
& PAspect
) { 
1916                 c
->mina 
= (float)size
.min_aspect
.y 
/ size
.min_aspect
.x
; 
1917                 c
->maxa 
= (float)size
.max_aspect
.x 
/ size
.max_aspect
.y
; 
1920                 c
->maxa 
= c
->mina 
= 0.0; 
1921         c
->isfixed 
= (c
->maxw 
&& c
->minw 
&& c
->maxh 
&& c
->minh
 
1922                      && c
->maxw 
== c
->minw 
&& c
->maxh 
== c
->minh
); 
1926 updatetitle(Client 
*c
) { 
1927         if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
)) 
1928                 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
); 
1929         if(c
->name
[0] == '\0') /* hack to mark broken clients */ 
1930                 strcpy(c
->name
, broken
); 
1934 updatestatus(void) { 
1935         if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
))) 
1936                 strcpy(stext
, "dwm-"VERSION
); 
1941 updatewindowtype(Client 
*c
) { 
1942         Atom state 
= getatomprop(c
, netatom
[NetWMState
]); 
1943         Atom wtype 
= getatomprop(c
, netatom
[NetWMWindowType
]); 
1945         if(state 
== netatom
[NetWMFullscreen
]) 
1946                 setfullscreen(c
, true); 
1947         if(wtype 
== netatom
[NetWMWindowTypeDialog
]) 
1948                 c
->isfloating 
= true; 
1952 updatewmhints(Client 
*c
) { 
1955         if((wmh 
= XGetWMHints(dpy
, c
->win
))) { 
1956                 if(c 
== selmon
->sel 
&& wmh
->flags 
& XUrgencyHint
) { 
1957                         wmh
->flags 
&= ~XUrgencyHint
; 
1958                         XSetWMHints(dpy
, c
->win
, wmh
); 
1961                         c
->isurgent 
= (wmh
->flags 
& XUrgencyHint
) ? true : false; 
1962                 if(wmh
->flags 
& InputHint
) 
1963                         c
->neverfocus 
= !wmh
->input
; 
1965                         c
->neverfocus 
= false; 
1971 view(const Arg 
*arg
) { 
1972         if((arg
->ui 
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
]) 
1974         selmon
->seltags 
^= 1; /* toggle sel tagset */ 
1975         if(arg
->ui 
& TAGMASK
) 
1976                 selmon
->tagset
[selmon
->seltags
] = arg
->ui 
& TAGMASK
; 
1982 wintoclient(Window w
) { 
1986         for(m 
= mons
; m
; m 
= m
->next
) 
1987                 for(c 
= m
->clients
; c
; c 
= c
->next
) 
1994 wintomon(Window w
) { 
1999         if(w 
== root 
&& getrootptr(&x
, &y
)) 
2000                 return recttomon(x
, y
, 1, 1); 
2001         for(m 
= mons
; m
; m 
= m
->next
) 
2004         if((c 
= wintoclient(w
))) 
2009 /* There's no way to check accesses to destroyed windows, thus those cases are 
2010  * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs 
2011  * default error handler, which may call exit.  */ 
2013 xerror(Display 
*dpy
, XErrorEvent 
*ee
) { 
2014         if(ee
->error_code 
== BadWindow
 
2015         || (ee
->request_code 
== X_SetInputFocus 
&& ee
->error_code 
== BadMatch
) 
2016         || (ee
->request_code 
== X_PolyText8 
&& ee
->error_code 
== BadDrawable
) 
2017         || (ee
->request_code 
== X_PolyFillRectangle 
&& ee
->error_code 
== BadDrawable
) 
2018         || (ee
->request_code 
== X_PolySegment 
&& ee
->error_code 
== BadDrawable
) 
2019         || (ee
->request_code 
== X_ConfigureWindow 
&& ee
->error_code 
== BadMatch
) 
2020         || (ee
->request_code 
== X_GrabButton 
&& ee
->error_code 
== BadAccess
) 
2021         || (ee
->request_code 
== X_GrabKey 
&& ee
->error_code 
== BadAccess
) 
2022         || (ee
->request_code 
== X_CopyArea 
&& ee
->error_code 
== BadDrawable
)) 
2024         fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n", 
2025                         ee
->request_code
, ee
->error_code
); 
2026         return xerrorxlib(dpy
, ee
); /* may call exit */ 
2030 xerrordummy(Display 
*dpy
, XErrorEvent 
*ee
) { 
2034 /* Startup Error handler to check if another window manager 
2035  * is already running. */ 
2037 xerrorstart(Display 
*dpy
, XErrorEvent 
*ee
) { 
2038         die("dwm: another window manager is already running\n"); 
2043 zoom(const Arg 
*arg
) { 
2044         Client 
*c 
= selmon
->sel
; 
2046         if(!selmon
->lt
[selmon
->sellt
]->arrange
 
2047         || (selmon
->sel 
&& selmon
->sel
->isfloating
)) 
2049         if(c 
== nexttiled(selmon
->clients
)) 
2050                 if(!c 
|| !(c 
= nexttiled(c
->next
))) 
2056 main(int argc
, char *argv
[]) { 
2057         if(argc 
== 2 && !strcmp("-v", argv
[1])) 
2058                 die("dwm-"VERSION
", © 2006-2015 dwm engineers, see LICENSE for details\n"); 
2060                 die("usage: dwm [-v]\n"); 
2061         if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale()) 
2062                 fputs("warning: no locale support\n", stderr
); 
2063         if(!(dpy 
= XOpenDisplay(NULL
))) 
2064                 die("dwm: cannot open display\n"); 
2071         return EXIT_SUCCESS
;