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  * Calls to fetch an X event from the event queue are blocking.  Due reading 
  10  * status text from standard input, a select()-driven main loop has been 
  11  * implemented which selects for reads on the X connection and STDIN_FILENO to 
  12  * handle all data smoothly. The event handlers of dwm are organized in an 
  13  * array which is accessed whenever a new event has been fetched. This allows 
  14  * event dispatching in O(1) time. 
  16  * Each child of the root window is called a client, except windows which have 
  17  * set the override_redirect flag.  Clients are organized in a global 
  18  * doubly-linked client list, the focus history is remembered through a global 
  19  * stack list. Each client contains an array of Bools of the same size as the 
  20  * global tags array to indicate the tags of a client. 
  22  * Keys and tagging rules are organized as arrays and defined in config.h. 
  24  * To understand everything else, start reading main(). 
  33 #include <sys/select.h> 
  34 #include <sys/types.h> 
  36 #include <X11/cursorfont.h> 
  37 #include <X11/keysym.h> 
  38 #include <X11/Xatom.h> 
  40 #include <X11/Xproto.h> 
  41 #include <X11/Xutil.h> 
  44 #define BUTTONMASK              (ButtonPressMask|ButtonReleaseMask) 
  45 #define CLEANMASK(mask)         (mask & ~(numlockmask|LockMask)) 
  46 #define LENGTH(x)               (sizeof x / sizeof x[0]) 
  48 #define MOUSEMASK               (BUTTONMASK|PointerMotionMask) 
  49 #define DEFGEOM(GEONAME,BX,BY,BW,WX,WY,WW,WH,MX,MY,MW,MH,TX,TY,TW,TH,MOX,MOY,MOW,MOH) \ 
  50 void GEONAME(void) { \ 
  51         bx = (BX); by = (BY); bw = (BW); \ 
  52         wx = (WX); wy = (WY); ww = (WW); wh = (WH); \ 
  53         mx = (MX); my = (MY); mw = (MW); mh = (MH); \ 
  54         tx = (TX); ty = (TY); tw = (TW); th = (TH); \ 
  55         mox = (MOX); moy = (MOY); mow = (MOW); moh = (MOH); \ 
  59 enum { CurNormal
, CurResize
, CurMove
, CurLast 
};        /* cursor */ 
  60 enum { ColBorder
, ColFG
, ColBG
, ColLast 
};              /* color */ 
  61 enum { NetSupported
, NetWMName
, NetLast 
};              /* EWMH atoms */ 
  62 enum { WMProtocols
, WMDelete
, WMName
, WMState
, WMLast 
};/* default atoms */ 
  65 typedef struct Client Client
; 
  69         int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
; 
  70         int minax
, maxax
, minay
, maxay
; 
  72         unsigned int border
, oldborder
; 
  73         Bool isbanned
, isfixed
, isfloating
, isurgent
; 
  83         unsigned long norm
[ColLast
]; 
  84         unsigned long sel
[ColLast
]; 
  94 } DC
; /* draw context */ 
 104         void (*func
)(const char *arg
); 
 110         void (*arrange
)(void); 
 116         const char *instance
; 
 122 /* function declarations */ 
 123 void applyrules(Client 
*c
); 
 125 void attach(Client 
*c
); 
 126 void attachstack(Client 
*c
); 
 128 void buttonpress(XEvent 
*e
); 
 129 void checkotherwm(void); 
 131 void configure(Client 
*c
); 
 132 void configurenotify(XEvent 
*e
); 
 133 void configurerequest(XEvent 
*e
); 
 134 unsigned int counttiled(void); 
 135 void destroynotify(XEvent 
*e
); 
 136 void detach(Client 
*c
); 
 137 void detachstack(Client 
*c
); 
 139 void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]); 
 140 void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
); 
 141 void *emallocz(unsigned int size
); 
 142 void enternotify(XEvent 
*e
); 
 143 void eprint(const char *errstr
, ...); 
 144 void expose(XEvent 
*e
); 
 145 void floating(void); /* default floating layout */ 
 146 void focus(Client 
*c
); 
 147 void focusin(XEvent 
*e
); 
 148 void focusnext(const char *arg
); 
 149 void focusprev(const char *arg
); 
 150 Client 
*getclient(Window w
); 
 151 unsigned long getcolor(const char *colstr
); 
 152 long getstate(Window w
); 
 153 Bool 
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
); 
 154 void grabbuttons(Client 
*c
, Bool focused
); 
 156 unsigned int idxoftag(const char *t
); 
 157 void initfont(const char *fontstr
); 
 158 Bool 
isoccupied(unsigned int t
); 
 159 Bool 
isprotodel(Client 
*c
); 
 160 Bool 
isurgent(unsigned int t
); 
 161 Bool 
isvisible(Client 
*c
); 
 162 void keypress(XEvent 
*e
); 
 163 void killclient(const char *arg
); 
 164 void manage(Window w
, XWindowAttributes 
*wa
); 
 165 void mappingnotify(XEvent 
*e
); 
 166 void maprequest(XEvent 
*e
); 
 168 void movemouse(Client 
*c
); 
 169 Client 
*nexttiled(Client 
*c
); 
 170 void propertynotify(XEvent 
*e
); 
 171 void quit(const char *arg
); 
 172 void reapply(const char *arg
); 
 173 void resize(Client 
*c
, int x
, int y
, int w
, int h
, Bool sizehints
); 
 174 void resizemouse(Client 
*c
); 
 178 void setclientstate(Client 
*c
, long state
); 
 179 void setgeom(const char *arg
); 
 180 void setlayout(const char *arg
); 
 182 void spawn(const char *arg
); 
 183 void tag(const char *arg
); 
 184 unsigned int textnw(const char *text
, unsigned int len
); 
 185 unsigned int textw(const char *text
); 
 187 void tilehstack(unsigned int n
); 
 188 Client 
*tilemaster(unsigned int n
); 
 189 void tileresize(Client 
*c
, int x
, int y
, int w
, int h
); 
 191 void tilevstack(unsigned int n
); 
 192 void togglefloating(const char *arg
); 
 193 void toggletag(const char *arg
); 
 194 void toggleview(const char *arg
); 
 195 void unban(Client 
*c
); 
 196 void unmanage(Client 
*c
); 
 197 void unmapnotify(XEvent 
*e
); 
 198 void updatebarpos(void); 
 199 void updatesizehints(Client 
*c
); 
 200 void updatetitle(Client 
*c
); 
 201 void updatewmhints(Client 
*c
); 
 202 void view(const char *arg
); 
 203 void viewprevtag(const char *arg
);      /* views previous selected tags */ 
 204 int xerror(Display 
*dpy
, XErrorEvent 
*ee
); 
 205 int xerrordummy(Display 
*dpy
, XErrorEvent 
*ee
); 
 206 int xerrorstart(Display 
*dpy
, XErrorEvent 
*ee
); 
 207 void zoom(const char *arg
); 
 210 char stext
[256], buf
[256]; 
 211 int screen
, sx
, sy
, sw
, sh
; 
 212 int (*xerrorxlib
)(Display 
*, XErrorEvent 
*); 
 213 int bx
, by
, bw
, bh
, blw
, mx
, my
, mw
, mh
, mox
, moy
, mow
, moh
, tx
, ty
, tw
, th
, wx
, wy
, ww
, wh
; 
 214 unsigned int numlockmask 
= 0; 
 215 void (*handler
[LASTEvent
]) (XEvent 
*) = { 
 216         [ButtonPress
] = buttonpress
, 
 217         [ConfigureRequest
] = configurerequest
, 
 218         [ConfigureNotify
] = configurenotify
, 
 219         [DestroyNotify
] = destroynotify
, 
 220         [EnterNotify
] = enternotify
, 
 223         [KeyPress
] = keypress
, 
 224         [MappingNotify
] = mappingnotify
, 
 225         [MapRequest
] = maprequest
, 
 226         [PropertyNotify
] = propertynotify
, 
 227         [UnmapNotify
] = unmapnotify
 
 229 Atom wmatom
[WMLast
], netatom
[NetLast
]; 
 230 Bool otherwm
, readin
; 
 234 Client 
*clients 
= NULL
; 
 236 Client 
*stack 
= NULL
; 
 237 Cursor cursor
[CurLast
]; 
 244 /* configuration, allows nested code to access above variables */ 
 246 #define TAGSZ (LENGTH(tags) * sizeof(Bool)) 
 247 static Bool tmp
[LENGTH(tags
)]; 
 249 /* function implementations */ 
 252 applyrules(Client 
*c
) { 
 254         Bool matched 
= False
; 
 256         XClassHint ch 
= { 0 }; 
 259         XGetClassHint(dpy
, c
->win
, &ch
); 
 260         for(i 
= 0; i 
< LENGTH(rules
); i
++) { 
 262                 if(strstr(c
->name
, r
->title
) 
 263                 || (ch
.res_class 
&& r
->class && strstr(ch
.res_class
, r
->class)) 
 264                 || (ch
.res_name 
&& r
->instance 
&& strstr(ch
.res_name
, r
->instance
))) 
 266                         c
->isfloating 
= r
->isfloating
; 
 268                                 c
->tags
[idxoftag(r
->tag
)] = True
; 
 278                 memcpy(c
->tags
, seltags
, TAGSZ
); 
 285         for(c 
= clients
; c
; c 
= c
->next
) 
 305 attachstack(Client 
*c
) { 
 314         XMoveWindow(dpy
, c
->win
, c
->x 
+ 2 * sw
, c
->y
); 
 319 buttonpress(XEvent 
*e
) { 
 322         XButtonPressedEvent 
*ev 
= &e
->xbutton
; 
 324         if(ev
->window 
== barwin
) { 
 326                 for(i 
= 0; i 
< LENGTH(tags
); i
++) { 
 329                                 if(ev
->button 
== Button1
) { 
 330                                         if(ev
->state 
& MODKEY
) 
 335                                 else if(ev
->button 
== Button3
) { 
 336                                         if(ev
->state 
& MODKEY
) 
 345         else if((c 
= getclient(ev
->window
))) { 
 347                 if(CLEANMASK(ev
->state
) != MODKEY
) 
 349                 if(ev
->button 
== Button1
) { 
 353                 else if(ev
->button 
== Button2
) { 
 354                         if((floating 
!= lt
->arrange
) && c
->isfloating
) 
 355                                 togglefloating(NULL
); 
 359                 else if(ev
->button 
== Button3 
&& !c
->isfixed
) { 
 369         XSetErrorHandler(xerrorstart
); 
 371         /* this causes an error if some other window manager is running */ 
 372         XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
); 
 375                 eprint("dwm: another window manager is already running\n"); 
 377         XSetErrorHandler(NULL
); 
 378         xerrorxlib 
= XSetErrorHandler(xerror
); 
 390                 XFreeFontSet(dpy
, dc
.font
.set
); 
 392                 XFreeFont(dpy
, dc
.font
.xfont
); 
 393         XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
); 
 394         XFreePixmap(dpy
, dc
.drawable
); 
 396         XFreeCursor(dpy
, cursor
[CurNormal
]); 
 397         XFreeCursor(dpy
, cursor
[CurResize
]); 
 398         XFreeCursor(dpy
, cursor
[CurMove
]); 
 399         XDestroyWindow(dpy
, barwin
); 
 401         XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
); 
 405 configure(Client 
*c
) { 
 408         ce
.type 
= ConfigureNotify
; 
 416         ce
.border_width 
= c
->border
; 
 418         ce
.override_redirect 
= False
; 
 419         XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent 
*)&ce
); 
 423 configurenotify(XEvent 
*e
) { 
 424         XConfigureEvent 
*ev 
= &e
->xconfigure
; 
 426         if(ev
->window 
== root 
&& (ev
->width 
!= sw 
|| ev
->height 
!= sh
)) 
 431 configurerequest(XEvent 
*e
) { 
 433         XConfigureRequestEvent 
*ev 
= &e
->xconfigurerequest
; 
 436         if((c 
= getclient(ev
->window
))) { 
 437                 if(ev
->value_mask 
& CWBorderWidth
) 
 438                         c
->border 
= ev
->border_width
; 
 439                 if(c
->isfixed 
|| c
->isfloating 
|| lt
->isfloating
) { 
 440                         if(ev
->value_mask 
& CWX
) 
 442                         if(ev
->value_mask 
& CWY
) 
 444                         if(ev
->value_mask 
& CWWidth
) 
 446                         if(ev
->value_mask 
& CWHeight
) 
 448                         if((c
->x 
- sx 
+ c
->w
) > sw 
&& c
->isfloating
) 
 449                                 c
->x 
= sx 
+ (sw 
/ 2 - c
->w 
/ 2); /* center in x direction */ 
 450                         if((c
->y 
- sy 
+ c
->h
) > sh 
&& c
->isfloating
) 
 451                                 c
->y 
= sy 
+ (sh 
/ 2 - c
->h 
/ 2); /* center in y direction */ 
 452                         if((ev
->value_mask 
& (CWX
|CWY
)) 
 453                         && !(ev
->value_mask 
& (CWWidth
|CWHeight
))) 
 456                                 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); 
 464                 wc
.width 
= ev
->width
; 
 465                 wc
.height 
= ev
->height
; 
 466                 wc
.border_width 
= ev
->border_width
; 
 467                 wc
.sibling 
= ev
->above
; 
 468                 wc
.stack_mode 
= ev
->detail
; 
 469                 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
); 
 479         for(n 
= 0, c 
= nexttiled(clients
); c
; c 
= nexttiled(c
->next
), n
++); 
 484 destroynotify(XEvent 
*e
) { 
 486         XDestroyWindowEvent 
*ev 
= &e
->xdestroywindow
; 
 488         if((c 
= getclient(ev
->window
))) 
 495                 c
->prev
->next 
= c
->next
; 
 497                 c
->next
->prev 
= c
->prev
; 
 500         c
->next 
= c
->prev 
= NULL
; 
 504 detachstack(Client 
*c
) { 
 507         for(tc
=&stack
; *tc 
&& *tc 
!= c
; tc
=&(*tc
)->snext
); 
 517         for(c 
= stack
; c 
&& !isvisible(c
); c 
= c
->snext
); 
 518         for(i 
= 0; i 
< LENGTH(tags
); i
++) { 
 519                 dc
.w 
= textw(tags
[i
]); 
 521                         drawtext(tags
[i
], dc
.sel
, isurgent(i
)); 
 522                         drawsquare(c 
&& c
->tags
[i
], isoccupied(i
), isurgent(i
), dc
.sel
); 
 525                         drawtext(tags
[i
], dc
.norm
, isurgent(i
)); 
 526                         drawsquare(c 
&& c
->tags
[i
], isoccupied(i
), isurgent(i
), dc
.norm
); 
 531         drawtext(lt
->symbol
, dc
.norm
, False
); 
 539         drawtext(stext
, dc
.norm
, False
); 
 540         if((dc
.w 
= dc
.x 
- x
) > bh
) { 
 543                         drawtext(c
->name
, dc
.sel
, False
); 
 544                         drawsquare(False
, c
->isfloating
, False
, dc
.sel
); 
 547                         drawtext(NULL
, dc
.norm
, False
); 
 549         XCopyArea(dpy
, dc
.drawable
, barwin
, dc
.gc
, 0, 0, bw
, bh
, 0, 0); 
 554 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) { 
 557         XRectangle r 
= { dc
.x
, dc
.y
, dc
.w
, dc
.h 
}; 
 559         gcv
.foreground 
= col
[invert 
? ColBG 
: ColFG
]; 
 560         XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
); 
 561         x 
= (dc
.font
.ascent 
+ dc
.font
.descent 
+ 2) / 4; 
 565                 r
.width 
= r
.height 
= x 
+ 1; 
 566                 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1); 
 569                 r
.width 
= r
.height 
= x
; 
 570                 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1); 
 575 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) { 
 577         unsigned int len
, olen
; 
 578         XRectangle r 
= { dc
.x
, dc
.y
, dc
.w
, dc
.h 
}; 
 580         XSetForeground(dpy
, dc
.gc
, col
[invert 
? ColFG 
: ColBG
]); 
 581         XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1); 
 585         olen 
= len 
= strlen(text
); 
 586         if(len 
>= sizeof buf
) 
 587                 len 
= sizeof buf 
- 1; 
 588         memcpy(buf
, text
, len
); 
 590         h 
= dc
.font
.ascent 
+ dc
.font
.descent
; 
 591         y 
= dc
.y 
+ (dc
.h 
/ 2) - (h 
/ 2) + dc
.font
.ascent
; 
 593         /* shorten text if necessary */ 
 594         while(len 
&& (w 
= textnw(buf
, len
)) > dc
.w 
- h
) 
 605                 return; /* too long */ 
 606         XSetForeground(dpy
, dc
.gc
, col
[invert 
? ColBG 
: ColFG
]); 
 608                 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
); 
 610                 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
); 
 614 emallocz(unsigned int size
) { 
 615         void *res 
= calloc(1, size
); 
 618                 eprint("fatal: could not malloc() %u bytes\n", size
); 
 623 enternotify(XEvent 
*e
) { 
 625         XCrossingEvent 
*ev 
= &e
->xcrossing
; 
 627         if((ev
->mode 
!= NotifyNormal 
|| ev
->detail 
== NotifyInferior
) && ev
->window 
!= root
) 
 629         if((c 
= getclient(ev
->window
))) 
 636 eprint(const char *errstr
, ...) { 
 639         va_start(ap
, errstr
); 
 640         vfprintf(stderr
, errstr
, ap
); 
 647         XExposeEvent 
*ev 
= &e
->xexpose
; 
 649         if(ev
->count 
== 0 && (ev
->window 
== barwin
)) 
 654 floating(void) { /* default floating layout */ 
 657         for(c 
= clients
; c
; c 
= c
->next
) 
 659                         resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, True
); 
 664         if(!c 
|| (c 
&& !isvisible(c
))) 
 665                 for(c 
= stack
; c 
&& !isvisible(c
); c 
= c
->snext
); 
 666         if(sel 
&& sel 
!= c
) { 
 667                 grabbuttons(sel
, False
); 
 668                 XSetWindowBorder(dpy
, sel
->win
, dc
.norm
[ColBorder
]); 
 673                 grabbuttons(c
, True
); 
 677                 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]); 
 678                 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
); 
 681                 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
); 
 686 focusin(XEvent 
*e
) { /* there are some broken focus acquiring clients */ 
 687         XFocusChangeEvent 
*ev 
= &e
->xfocus
; 
 689         if(sel 
&& ev
->window 
!= sel
->win
) 
 690                 XSetInputFocus(dpy
, sel
->win
, RevertToPointerRoot
, CurrentTime
); 
 694 focusnext(const char *arg
) { 
 699         for(c 
= sel
->next
; c 
&& !isvisible(c
); c 
= c
->next
); 
 701                 for(c 
= clients
; c 
&& !isvisible(c
); c 
= c
->next
); 
 709 focusprev(const char *arg
) { 
 714         for(c 
= sel
->prev
; c 
&& !isvisible(c
); c 
= c
->prev
); 
 716                 for(c 
= clients
; c 
&& c
->next
; c 
= c
->next
); 
 717                 for(; c 
&& !isvisible(c
); c 
= c
->prev
); 
 726 getclient(Window w
) { 
 729         for(c 
= clients
; c 
&& c
->win 
!= w
; c 
= c
->next
); 
 734 getcolor(const char *colstr
) { 
 735         Colormap cmap 
= DefaultColormap(dpy
, screen
); 
 738         if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
)) 
 739                 eprint("error, cannot allocate color '%s'\n", colstr
); 
 747         unsigned char *p 
= NULL
; 
 748         unsigned long n
, extra
; 
 751         status 
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
], 
 752                         &real
, &format
, &n
, &extra
, (unsigned char **)&p
); 
 753         if(status 
!= Success
) 
 762 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) { 
 767         if(!text 
|| size 
== 0) 
 770         XGetTextProperty(dpy
, w
, &name
, atom
); 
 773         if(name
.encoding 
== XA_STRING
) 
 774                 strncpy(text
, (char *)name
.value
, size 
- 1); 
 776                 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
 
 778                         strncpy(text
, *list
, size 
- 1); 
 779                         XFreeStringList(list
); 
 782         text
[size 
- 1] = '\0'; 
 788 grabbuttons(Client 
*c
, Bool focused
) { 
 789         XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
); 
 792                 XGrabButton(dpy
, Button1
, MODKEY
, c
->win
, False
, BUTTONMASK
, 
 793                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 794                 XGrabButton(dpy
, Button1
, MODKEY
|LockMask
, c
->win
, False
, BUTTONMASK
, 
 795                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 796                 XGrabButton(dpy
, Button1
, MODKEY
|numlockmask
, c
->win
, False
, BUTTONMASK
, 
 797                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 798                 XGrabButton(dpy
, Button1
, MODKEY
|numlockmask
|LockMask
, c
->win
, False
, BUTTONMASK
, 
 799                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 801                 XGrabButton(dpy
, Button2
, MODKEY
, c
->win
, False
, BUTTONMASK
, 
 802                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 803                 XGrabButton(dpy
, Button2
, MODKEY
|LockMask
, c
->win
, False
, BUTTONMASK
, 
 804                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 805                 XGrabButton(dpy
, Button2
, MODKEY
|numlockmask
, c
->win
, False
, BUTTONMASK
, 
 806                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 807                 XGrabButton(dpy
, Button2
, MODKEY
|numlockmask
|LockMask
, c
->win
, False
, BUTTONMASK
, 
 808                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 810                 XGrabButton(dpy
, Button3
, MODKEY
, c
->win
, False
, BUTTONMASK
, 
 811                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 812                 XGrabButton(dpy
, Button3
, MODKEY
|LockMask
, c
->win
, False
, BUTTONMASK
, 
 813                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 814                 XGrabButton(dpy
, Button3
, MODKEY
|numlockmask
, c
->win
, False
, BUTTONMASK
, 
 815                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 816                 XGrabButton(dpy
, Button3
, MODKEY
|numlockmask
|LockMask
, c
->win
, False
, BUTTONMASK
, 
 817                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 820                 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
, BUTTONMASK
, 
 821                                 GrabModeAsync
, GrabModeSync
, None
, None
); 
 828         XModifierKeymap 
*modmap
; 
 830         /* init modifier map */ 
 831         modmap 
= XGetModifierMapping(dpy
); 
 832         for(i 
= 0; i 
< 8; i
++) 
 833                 for(j 
= 0; j 
< modmap
->max_keypermod
; j
++) { 
 834                         if(modmap
->modifiermap
[i 
* modmap
->max_keypermod 
+ j
] == XKeysymToKeycode(dpy
, XK_Num_Lock
)) 
 835                                 numlockmask 
= (1 << i
); 
 837         XFreeModifiermap(modmap
); 
 839         XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
); 
 840         for(i 
= 0; i 
< LENGTH(keys
); i
++) { 
 841                 code 
= XKeysymToKeycode(dpy
, keys
[i
].keysym
); 
 842                 XGrabKey(dpy
, code
, keys
[i
].mod
, root
, True
, 
 843                                 GrabModeAsync
, GrabModeAsync
); 
 844                 XGrabKey(dpy
, code
, keys
[i
].mod
|LockMask
, root
, True
, 
 845                                 GrabModeAsync
, GrabModeAsync
); 
 846                 XGrabKey(dpy
, code
, keys
[i
].mod
|numlockmask
, root
, True
, 
 847                                 GrabModeAsync
, GrabModeAsync
); 
 848                 XGrabKey(dpy
, code
, keys
[i
].mod
|numlockmask
|LockMask
, root
, True
, 
 849                                 GrabModeAsync
, GrabModeAsync
); 
 854 idxoftag(const char *t
) { 
 857         for(i 
= 0; (i 
< LENGTH(tags
)) && (tags
[i
] != t
); i
++); 
 858         return (i 
< LENGTH(tags
)) ? i 
: 0; 
 862 initfont(const char *fontstr
) { 
 863         char *def
, **missing
; 
 868                 XFreeFontSet(dpy
, dc
.font
.set
); 
 869         dc
.font
.set 
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
); 
 872                         fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]); 
 873                 XFreeStringList(missing
); 
 876                 XFontSetExtents 
*font_extents
; 
 877                 XFontStruct 
**xfonts
; 
 879                 dc
.font
.ascent 
= dc
.font
.descent 
= 0; 
 880                 font_extents 
= XExtentsOfFontSet(dc
.font
.set
); 
 881                 n 
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
); 
 882                 for(i 
= 0, dc
.font
.ascent 
= 0, dc
.font
.descent 
= 0; i 
< n
; i
++) { 
 883                         if(dc
.font
.ascent 
< (*xfonts
)->ascent
) 
 884                                 dc
.font
.ascent 
= (*xfonts
)->ascent
; 
 885                         if(dc
.font
.descent 
< (*xfonts
)->descent
) 
 886                                 dc
.font
.descent 
= (*xfonts
)->descent
; 
 892                         XFreeFont(dpy
, dc
.font
.xfont
); 
 893                 dc
.font
.xfont 
= NULL
; 
 894                 if(!(dc
.font
.xfont 
= XLoadQueryFont(dpy
, fontstr
)) 
 895                 && !(dc
.font
.xfont 
= XLoadQueryFont(dpy
, "fixed"))) 
 896                         eprint("error, cannot load font: '%s'\n", fontstr
); 
 897                 dc
.font
.ascent 
= dc
.font
.xfont
->ascent
; 
 898                 dc
.font
.descent 
= dc
.font
.xfont
->descent
; 
 900         dc
.font
.height 
= dc
.font
.ascent 
+ dc
.font
.descent
; 
 904 isoccupied(unsigned int t
) { 
 907         for(c 
= clients
; c
; c 
= c
->next
) 
 914 isprotodel(Client 
*c
) { 
 919         if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) { 
 920                 for(i 
= 0; !ret 
&& i 
< n
; i
++) 
 921                         if(protocols
[i
] == wmatom
[WMDelete
]) 
 929 isurgent(unsigned int t
) { 
 932         for(c 
= clients
; c
; c 
= c
->next
) 
 933                 if(c
->isurgent 
&& c
->tags
[t
]) 
 939 isvisible(Client 
*c
) { 
 942         for(i 
= 0; i 
< LENGTH(tags
); i
++) 
 943                 if(c
->tags
[i
] && seltags
[i
]) 
 949 keypress(XEvent 
*e
) { 
 955         keysym 
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0); 
 956         for(i 
= 0; i 
< LENGTH(keys
); i
++) 
 957                 if(keysym 
== keys
[i
].keysym
 
 958                 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)) 
 961                                 keys
[i
].func(keys
[i
].arg
); 
 966 killclient(const char *arg
) { 
 971         if(isprotodel(sel
)) { 
 972                 ev
.type 
= ClientMessage
; 
 973                 ev
.xclient
.window 
= sel
->win
; 
 974                 ev
.xclient
.message_type 
= wmatom
[WMProtocols
]; 
 975                 ev
.xclient
.format 
= 32; 
 976                 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
]; 
 977                 ev
.xclient
.data
.l
[1] = CurrentTime
; 
 978                 XSendEvent(dpy
, sel
->win
, False
, NoEventMask
, &ev
); 
 981                 XKillClient(dpy
, sel
->win
); 
 985 manage(Window w
, XWindowAttributes 
*wa
) { 
 986         Client 
*c
, *t 
= NULL
; 
 991         c 
= emallocz(sizeof(Client
)); 
 992         c
->tags 
= emallocz(TAGSZ
); 
1000         c
->oldborder 
= wa
->border_width
; 
1001         if(c
->w 
== sw 
&& c
->h 
== sh
) { 
1004                 c
->border 
= wa
->border_width
; 
1007                 if(c
->x 
+ c
->w 
+ 2 * c
->border 
> wx 
+ ww
) 
1008                         c
->x 
= wx 
+ ww 
- c
->w 
- 2 * c
->border
; 
1009                 if(c
->y 
+ c
->h 
+ 2 * c
->border 
> wy 
+ wh
) 
1010                         c
->y 
= wy 
+ wh 
- c
->h 
- 2 * c
->border
; 
1015                 c
->border 
= BORDERPX
; 
1018         wc
.border_width 
= c
->border
; 
1019         XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
); 
1020         XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]); 
1021         configure(c
); /* propagates border_width, if size doesn't change */ 
1023         XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
); 
1024         grabbuttons(c
, False
); 
1026         if((rettrans 
= XGetTransientForHint(dpy
, w
, &trans
) == Success
)) 
1027                 for(t 
= clients
; t 
&& t
->win 
!= trans
; t 
= t
->next
); 
1029                 memcpy(c
->tags
, t
->tags
, TAGSZ
); 
1033                 c
->isfloating 
= (rettrans 
== Success
) || c
->isfixed
; 
1036         XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
); /* some windows require this */ 
1038         XMapWindow(dpy
, c
->win
); 
1039         setclientstate(c
, NormalState
); 
1044 mappingnotify(XEvent 
*e
) { 
1045         XMappingEvent 
*ev 
= &e
->xmapping
; 
1047         XRefreshKeyboardMapping(ev
); 
1048         if(ev
->request 
== MappingKeyboard
) 
1053 maprequest(XEvent 
*e
) { 
1054         static XWindowAttributes wa
; 
1055         XMapRequestEvent 
*ev 
= &e
->xmaprequest
; 
1057         if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
)) 
1059         if(wa
.override_redirect
) 
1061         if(!getclient(ev
->window
)) 
1062                 manage(ev
->window
, &wa
); 
1069         for(c 
= clients
; c
; c 
= c
->next
) 
1071                         resize(c
, mox
, moy
, mow
, moh
, RESIZEHINTS
); 
1075 movemouse(Client 
*c
) { 
1076         int x1
, y1
, ocx
, ocy
, di
, nx
, ny
; 
1083         if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
, 
1084                         None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
) 
1086         XQueryPointer(dpy
, root
, &dummy
, &dummy
, &x1
, &y1
, &di
, &di
, &dui
); 
1088                 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
); 
1091                         XUngrabPointer(dpy
, CurrentTime
); 
1093                 case ConfigureRequest
: 
1096                         handler
[ev
.type
](&ev
); 
1100                         nx 
= ocx 
+ (ev
.xmotion
.x 
- x1
); 
1101                         ny 
= ocy 
+ (ev
.xmotion
.y 
- y1
); 
1102                         if(abs(wx 
- nx
) < SNAP
) 
1104                         else if(abs((wx 
+ ww
) - (nx 
+ c
->w 
+ 2 * c
->border
)) < SNAP
) 
1105                                 nx 
= wx 
+ ww 
- c
->w 
- 2 * c
->border
; 
1106                         if(abs(wy 
- ny
) < SNAP
) 
1108                         else if(abs((wy 
+ wh
) - (ny 
+ c
->h 
+ 2 * c
->border
)) < SNAP
) 
1109                                 ny 
= wy 
+ wh 
- c
->h 
- 2 * c
->border
; 
1110                         if(!c
->isfloating 
&& !lt
->isfloating 
&& (abs(nx 
- c
->x
) > SNAP 
|| abs(ny 
- c
->y
) > SNAP
)) 
1111                                 togglefloating(NULL
); 
1112                         if((lt
->isfloating
) || c
->isfloating
) 
1113                                 resize(c
, nx
, ny
, c
->w
, c
->h
, False
); 
1120 nexttiled(Client 
*c
) { 
1121         for(; c 
&& (c
->isfloating 
|| !isvisible(c
)); c 
= c
->next
); 
1126 propertynotify(XEvent 
*e
) { 
1129         XPropertyEvent 
*ev 
= &e
->xproperty
; 
1131         if(ev
->state 
== PropertyDelete
) 
1132                 return; /* ignore */ 
1133         if((c 
= getclient(ev
->window
))) { 
1136                 case XA_WM_TRANSIENT_FOR
: 
1137                         XGetTransientForHint(dpy
, c
->win
, &trans
); 
1138                         if(!c
->isfloating 
&& (c
->isfloating 
= (getclient(trans
) != NULL
))) 
1141                 case XA_WM_NORMAL_HINTS
: 
1149                 if(ev
->atom 
== XA_WM_NAME 
|| ev
->atom 
== netatom
[NetWMName
]) { 
1158 quit(const char *arg
) { 
1159         readin 
= running 
= False
; 
1163 reapply(const char *arg
) { 
1164         static Bool zerotags
[LENGTH(tags
)] = { 0 }; 
1167         for(c 
= clients
; c
; c 
= c
->next
) { 
1168                 memcpy(c
->tags
, zerotags
, sizeof zerotags
); 
1175 resize(Client 
*c
, int x
, int y
, int w
, int h
, Bool sizehints
) { 
1179                 /* set minimum possible */ 
1185                 /* temporarily remove base dimensions */ 
1189                 /* adjust for aspect limits */ 
1190                 if (c
->minay 
> 0 && c
->maxay 
> 0 && c
->minax 
> 0 && c
->maxax 
> 0) { 
1191                         if (w 
* c
->maxay 
> h 
* c
->maxax
) 
1192                                 w 
= h 
* c
->maxax 
/ c
->maxay
; 
1193                         else if (w 
* c
->minay 
< h 
* c
->minax
) 
1194                                 h 
= w 
* c
->minay 
/ c
->minax
; 
1197                 /* adjust for increment value */ 
1203                 /* restore base dimensions */ 
1207                 if(c
->minw 
> 0 && w 
< c
->minw
) 
1209                 if(c
->minh 
> 0 && h 
< c
->minh
) 
1211                 if(c
->maxw 
> 0 && w 
> c
->maxw
) 
1213                 if(c
->maxh 
> 0 && h 
> c
->maxh
) 
1216         if(w 
<= 0 || h 
<= 0) 
1219                 x 
= sw 
- w 
- 2 * c
->border
; 
1221                 y 
= sh 
- h 
- 2 * c
->border
; 
1222         if(x 
+ w 
+ 2 * c
->border 
< sx
) 
1224         if(y 
+ h 
+ 2 * c
->border 
< sy
) 
1226         if(c
->x 
!= x 
|| c
->y 
!= y 
|| c
->w 
!= w 
|| c
->h 
!= h
) { 
1229                 c
->w 
= wc
.width 
= w
; 
1230                 c
->h 
= wc
.height 
= h
; 
1231                 wc
.border_width 
= c
->border
; 
1232                 XConfigureWindow(dpy
, c
->win
, 
1233                                 CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
); 
1240 resizemouse(Client 
*c
) { 
1247         if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
, 
1248                         None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
) 
1250         XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w 
+ c
->border 
- 1, c
->h 
+ c
->border 
- 1); 
1252                 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask 
, &ev
); 
1255                         XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, 
1256                                         c
->w 
+ c
->border 
- 1, c
->h 
+ c
->border 
- 1); 
1257                         XUngrabPointer(dpy
, CurrentTime
); 
1258                         while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
)); 
1260                 case ConfigureRequest
: 
1263                         handler
[ev
.type
](&ev
); 
1267                         if((nw 
= ev
.xmotion
.x 
- ocx 
- 2 * c
->border 
+ 1) <= 0) 
1269                         if((nh 
= ev
.xmotion
.y 
- ocy 
- 2 * c
->border 
+ 1) <= 0) 
1271                         if(!c
->isfloating 
&& !lt
->isfloating 
&& (abs(nw 
- c
->w
) > SNAP 
|| abs(nh 
- c
->h
) > SNAP
)) 
1272                                 togglefloating(NULL
); 
1273                         if((lt
->isfloating
) || c
->isfloating
) 
1274                                 resize(c
, c
->x
, c
->y
, nw
, nh
, True
); 
1289         if(sel
->isfloating 
|| lt
->isfloating
) 
1290                 XRaiseWindow(dpy
, sel
->win
); 
1291         if(!lt
->isfloating
) { 
1292                 wc
.stack_mode 
= Below
; 
1293                 wc
.sibling 
= barwin
; 
1294                 if(!sel
->isfloating
) { 
1295                         XConfigureWindow(dpy
, sel
->win
, CWSibling
|CWStackMode
, &wc
); 
1296                         wc
.sibling 
= sel
->win
; 
1298                 for(c 
= nexttiled(clients
); c
; c 
= nexttiled(c
->next
)) { 
1301                         XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
); 
1302                         wc
.sibling 
= c
->win
; 
1306         while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
)); 
1312         char sbuf
[sizeof stext
]; 
1315         unsigned int len
, offset
; 
1318         /* main event loop, also reads status text from stdin */ 
1320         xfd 
= ConnectionNumber(dpy
); 
1323         len 
= sizeof stext 
- 1; 
1324         sbuf
[len
] = stext
[len
] = '\0'; /* 0-terminator is never touched */ 
1328                         FD_SET(STDIN_FILENO
, &rd
); 
1330                 if(select(xfd 
+ 1, &rd
, NULL
, NULL
, NULL
) == -1) { 
1333                         eprint("select failed\n"); 
1335                 if(FD_ISSET(STDIN_FILENO
, &rd
)) { 
1336                         switch((r 
= read(STDIN_FILENO
, sbuf 
+ offset
, len 
- offset
))) { 
1338                                 strncpy(stext
, strerror(errno
), len
); 
1342                                 strncpy(stext
, "EOF", 4); 
1346                                 for(p 
= sbuf 
+ offset
; r 
> 0; p
++, r
--, offset
++) 
1347                                         if(*p 
== '\n' || *p 
== '\0') { 
1349                                                 strncpy(stext
, sbuf
, len
); 
1350                                                 p 
+= r 
- 1; /* p is sbuf + offset + r - 1 */ 
1351                                                 for(r 
= 0; *(p 
- r
) && *(p 
- r
) != '\n'; r
++); 
1354                                                         memmove(sbuf
, p 
- r 
+ 1, r
); 
1361                 while(XPending(dpy
)) { 
1362                         XNextEvent(dpy
, &ev
); 
1363                         if(handler
[ev
.type
]) 
1364                                 (handler
[ev
.type
])(&ev
); /* call handler */ 
1371         unsigned int i
, num
; 
1372         Window 
*wins
, d1
, d2
; 
1373         XWindowAttributes wa
; 
1376         if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) { 
1377                 for(i 
= 0; i 
< num
; i
++) { 
1378                         if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
) 
1379                                         || wa
.override_redirect 
|| XGetTransientForHint(dpy
, wins
[i
], &d1
)) 
1381                         if(wa
.map_state 
== IsViewable 
|| getstate(wins
[i
]) == IconicState
) 
1382                                 manage(wins
[i
], &wa
); 
1384                 for(i 
= 0; i 
< num
; i
++) { /* now the transients */ 
1385                         if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)) 
1387                         if(XGetTransientForHint(dpy
, wins
[i
], &d1
) 
1388                                         && (wa
.map_state 
== IsViewable 
|| getstate(wins
[i
]) == IconicState
)) 
1389                                 manage(wins
[i
], &wa
); 
1397 setclientstate(Client 
*c
, long state
) { 
1398         long data
[] = {state
, None
}; 
1400         XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32, 
1401                         PropModeReplace
, (unsigned char *)data
, 2); 
1405 setgeom(const char *arg
) { 
1408         for(i 
= 0; arg 
&& i 
< LENGTH(geoms
); i
++) 
1409                 if(!strcmp(geoms
[i
].symbol
, arg
)) 
1411         if(i 
== LENGTH(geoms
)) 
1420 setlayout(const char *arg
) { 
1421         static Layout 
*revert 
= 0; 
1426         for(i 
= 0; i 
< LENGTH(layouts
); i
++) 
1427                 if(!strcmp(arg
, layouts
[i
].symbol
)) 
1429         if(i 
== LENGTH(layouts
)) 
1431         if(revert 
&& &layouts
[i
] == lt
) 
1446         XSetWindowAttributes wa
; 
1449         screen 
= DefaultScreen(dpy
); 
1450         root 
= RootWindow(dpy
, screen
); 
1453         /* apply default geometry */ 
1456         sw 
= DisplayWidth(dpy
, screen
); 
1457         sh 
= DisplayHeight(dpy
, screen
); 
1458         bh 
= dc
.font
.height 
+ 2; 
1463         wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
); 
1464         wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
); 
1465         wmatom
[WMName
] = XInternAtom(dpy
, "WM_NAME", False
); 
1466         wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
); 
1467         netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
); 
1468         netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
); 
1471         wa
.cursor 
= cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
); 
1472         cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
); 
1473         cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
); 
1475         /* init appearance */ 
1476         dc
.norm
[ColBorder
] = getcolor(NORMBORDERCOLOR
); 
1477         dc
.norm
[ColBG
] = getcolor(NORMBGCOLOR
); 
1478         dc
.norm
[ColFG
] = getcolor(NORMFGCOLOR
); 
1479         dc
.sel
[ColBorder
] = getcolor(SELBORDERCOLOR
); 
1480         dc
.sel
[ColBG
] = getcolor(SELBGCOLOR
); 
1481         dc
.sel
[ColFG
] = getcolor(SELFGCOLOR
); 
1484         dc
.drawable 
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
)); 
1485         dc
.gc 
= XCreateGC(dpy
, root
, 0, 0); 
1486         XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
); 
1488                 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
); 
1491         seltags 
= emallocz(TAGSZ
); 
1492         prevtags 
= emallocz(TAGSZ
); 
1493         seltags
[0] = prevtags
[0] = True
; 
1499         for(blw 
= i 
= 0; i 
< LENGTH(layouts
); i
++) { 
1500                 i 
= textw(layouts
[i
].symbol
); 
1505         wa
.override_redirect 
= 1; 
1506         wa
.background_pixmap 
= ParentRelative
; 
1507         wa
.event_mask 
= ButtonPressMask
|ExposureMask
; 
1509         barwin 
= XCreateWindow(dpy
, root
, bx
, by
, bw
, bh
, 0, DefaultDepth(dpy
, screen
), 
1510                                 CopyFromParent
, DefaultVisual(dpy
, screen
), 
1511                                 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
); 
1512         XDefineCursor(dpy
, barwin
, cursor
[CurNormal
]); 
1513         XMapRaised(dpy
, barwin
); 
1514         strcpy(stext
, "dwm-"VERSION
); 
1517         /* EWMH support per view */ 
1518         XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32, 
1519                         PropModeReplace
, (unsigned char *) netatom
, NetLast
); 
1521         /* select for events */ 
1522         wa
.event_mask 
= SubstructureRedirectMask
|SubstructureNotifyMask
 
1523                         |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
; 
1524         XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
); 
1525         XSelectInput(dpy
, root
, wa
.event_mask
); 
1533 spawn(const char *arg
) { 
1534         static char *shell 
= NULL
; 
1536         if(!shell 
&& !(shell 
= getenv("SHELL"))) 
1540         /* The double-fork construct avoids zombie processes and keeps the code 
1541          * clean from stupid signal handlers. */ 
1545                                 close(ConnectionNumber(dpy
)); 
1547                         execl(shell
, shell
, "-c", arg
, (char *)NULL
); 
1548                         fprintf(stderr
, "dwm: execl '%s -c %s'", shell
, arg
); 
1557 tag(const char *arg
) { 
1562         for(i 
= 0; i 
< LENGTH(tags
); i
++) 
1563                 sel
->tags
[i
] = (NULL 
== arg
); 
1564         sel
->tags
[idxoftag(arg
)] = True
; 
1569 textnw(const char *text
, unsigned int len
) { 
1573                 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
); 
1576         return XTextWidth(dc
.font
.xfont
, text
, len
); 
1580 textw(const char *text
) { 
1581         return textnw(text
, strlen(text
)) + dc
.font
.height
; 
1587         unsigned int i
, n 
= counttiled(); 
1601         for(i 
= 0, c 
= nexttiled(c
->next
); c
; c 
= nexttiled(c
->next
), i
++) { 
1602                 if(i 
+ 1 == n
) /* remainder */ 
1603                         tileresize(c
, x
, ty
, (tx 
+ tw
) - x 
- 2 * c
->border
, th 
- 2 * c
->border
); 
1605                         tileresize(c
, x
, ty
, w 
- 2 * c
->border
, th 
- 2 * c
->border
); 
1607                         x 
= c
->x 
+ c
->w 
+ 2 * c
->border
; 
1612 tilemaster(unsigned int n
) { 
1613         Client 
*c 
= nexttiled(clients
); 
1616                 tileresize(c
, mox
, moy
, mow 
- 2 * c
->border
, moh 
- 2 * c
->border
); 
1618                 tileresize(c
, mx
, my
, mw 
- 2 * c
->border
, mh 
- 2 * c
->border
); 
1623 tileresize(Client 
*c
, int x
, int y
, int w
, int h
) { 
1624         resize(c
, x
, y
, w
, h
, RESIZEHINTS
); 
1625         if((RESIZEHINTS
) && ((c
->h 
< bh
) || (c
->h 
> h
) || (c
->w 
< bh
) || (c
->w 
> w
))) 
1626                 /* client doesn't accept size constraints */ 
1627                 resize(c
, x
, y
, w
, h
, False
); 
1633         unsigned int i
, n 
= counttiled(); 
1647         for(i 
= 0, c 
= nexttiled(c
->next
); c
; c 
= nexttiled(c
->next
), i
++) { 
1648                 if(i 
+ 1 == n
) /* remainder */ 
1649                         tileresize(c
, tx
, y
, tw 
- 2 * c
->border
, (ty 
+ th
) - y 
- 2 * c
->border
); 
1651                         tileresize(c
, tx
, y
, tw 
- 2 * c
->border
, h 
- 2 * c
->border
); 
1653                         y 
= c
->y 
+ c
->h 
+ 2 * c
->border
; 
1658 togglefloating(const char *arg
) { 
1661         sel
->isfloating 
= !sel
->isfloating
; 
1663                 resize(sel
, sel
->x
, sel
->y
, sel
->w
, sel
->h
, True
); 
1668 toggletag(const char *arg
) { 
1674         sel
->tags
[i
] = !sel
->tags
[i
]; 
1675         for(j 
= 0; j 
< LENGTH(tags
) && !sel
->tags
[j
]; j
++); 
1676         if(j 
== LENGTH(tags
)) 
1677                 sel
->tags
[i
] = True
; /* at least one tag must be enabled */ 
1682 toggleview(const char *arg
) { 
1686         seltags
[i
] = !seltags
[i
]; 
1687         for(j 
= 0; j 
< LENGTH(tags
) && !seltags
[j
]; j
++); 
1688         if(j 
== LENGTH(tags
)) 
1689                 seltags
[i
] = True
; /* at least one tag must be viewed */ 
1697         XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
); 
1698         c
->isbanned 
= False
; 
1702 unmanage(Client 
*c
) { 
1705         wc
.border_width 
= c
->oldborder
; 
1706         /* The server grab construct avoids race conditions. */ 
1708         XSetErrorHandler(xerrordummy
); 
1709         XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */ 
1714         XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
); 
1715         setclientstate(c
, WithdrawnState
); 
1719         XSetErrorHandler(xerror
); 
1725 unmapnotify(XEvent 
*e
) { 
1727         XUnmapEvent 
*ev 
= &e
->xunmap
; 
1729         if((c 
= getclient(ev
->window
))) 
1734 updatebarpos(void) { 
1736         if(dc
.drawable 
!= 0) 
1737                 XFreePixmap(dpy
, dc
.drawable
); 
1738         dc
.drawable 
= XCreatePixmap(dpy
, root
, bw
, bh
, DefaultDepth(dpy
, screen
)); 
1739         XMoveResizeWindow(dpy
, barwin
, bx
, by
, bw
, bh
); 
1743 updatesizehints(Client 
*c
) { 
1747         if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
) || !size
.flags
) 
1749         c
->flags 
= size
.flags
; 
1750         if(c
->flags 
& PBaseSize
) { 
1751                 c
->basew 
= size
.base_width
; 
1752                 c
->baseh 
= size
.base_height
; 
1754         else if(c
->flags 
& PMinSize
) { 
1755                 c
->basew 
= size
.min_width
; 
1756                 c
->baseh 
= size
.min_height
; 
1759                 c
->basew 
= c
->baseh 
= 0; 
1760         if(c
->flags 
& PResizeInc
) { 
1761                 c
->incw 
= size
.width_inc
; 
1762                 c
->inch 
= size
.height_inc
; 
1765                 c
->incw 
= c
->inch 
= 0; 
1766         if(c
->flags 
& PMaxSize
) { 
1767                 c
->maxw 
= size
.max_width
; 
1768                 c
->maxh 
= size
.max_height
; 
1771                 c
->maxw 
= c
->maxh 
= 0; 
1772         if(c
->flags 
& PMinSize
) { 
1773                 c
->minw 
= size
.min_width
; 
1774                 c
->minh 
= size
.min_height
; 
1776         else if(c
->flags 
& PBaseSize
) { 
1777                 c
->minw 
= size
.base_width
; 
1778                 c
->minh 
= size
.base_height
; 
1781                 c
->minw 
= c
->minh 
= 0; 
1782         if(c
->flags 
& PAspect
) { 
1783                 c
->minax 
= size
.min_aspect
.x
; 
1784                 c
->maxax 
= size
.max_aspect
.x
; 
1785                 c
->minay 
= size
.min_aspect
.y
; 
1786                 c
->maxay 
= size
.max_aspect
.y
; 
1789                 c
->minax 
= c
->maxax 
= c
->minay 
= c
->maxay 
= 0; 
1790         c
->isfixed 
= (c
->maxw 
&& c
->minw 
&& c
->maxh 
&& c
->minh
 
1791                         && c
->maxw 
== c
->minw 
&& c
->maxh 
== c
->minh
); 
1795 updatetitle(Client 
*c
) { 
1796         if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
)) 
1797                 gettextprop(c
->win
, wmatom
[WMName
], c
->name
, sizeof c
->name
); 
1801 updatewmhints(Client 
*c
) { 
1804         if((wmh 
= XGetWMHints(dpy
, c
->win
))) { 
1806                         sel
->isurgent 
= False
; 
1808                         c
->isurgent 
= (wmh
->flags 
& XUrgencyHint
) ? True 
: False
; 
1815 view(const char *arg
) { 
1818         for(i 
= 0; i 
< LENGTH(tags
); i
++) 
1819                 tmp
[i
] = (NULL 
== arg
); 
1820         tmp
[idxoftag(arg
)] = True
; 
1822         if(memcmp(seltags
, tmp
, TAGSZ
) != 0) { 
1823                 memcpy(prevtags
, seltags
, TAGSZ
); 
1824                 memcpy(seltags
, tmp
, TAGSZ
); 
1830 viewprevtag(const char *arg
) { 
1832         memcpy(tmp
, seltags
, TAGSZ
); 
1833         memcpy(seltags
, prevtags
, TAGSZ
); 
1834         memcpy(prevtags
, tmp
, TAGSZ
); 
1838 /* There's no way to check accesses to destroyed windows, thus those cases are 
1839  * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs 
1840  * default error handler, which may call exit.  */ 
1842 xerror(Display 
*dpy
, XErrorEvent 
*ee
) { 
1843         if(ee
->error_code 
== BadWindow
 
1844         || (ee
->request_code 
== X_SetInputFocus 
&& ee
->error_code 
== BadMatch
) 
1845         || (ee
->request_code 
== X_PolyText8 
&& ee
->error_code 
== BadDrawable
) 
1846         || (ee
->request_code 
== X_PolyFillRectangle 
&& ee
->error_code 
== BadDrawable
) 
1847         || (ee
->request_code 
== X_PolySegment 
&& ee
->error_code 
== BadDrawable
) 
1848         || (ee
->request_code 
== X_ConfigureWindow 
&& ee
->error_code 
== BadMatch
) 
1849         || (ee
->request_code 
== X_GrabKey 
&& ee
->error_code 
== BadAccess
) 
1850         || (ee
->request_code 
== X_CopyArea 
&& ee
->error_code 
== BadDrawable
)) 
1852         fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n", 
1853                 ee
->request_code
, ee
->error_code
); 
1854         return xerrorxlib(dpy
, ee
); /* may call exit */ 
1858 xerrordummy(Display 
*dpy
, XErrorEvent 
*ee
) { 
1862 /* Startup Error handler to check if another window manager 
1863  * is already running. */ 
1865 xerrorstart(Display 
*dpy
, XErrorEvent 
*ee
) { 
1871 zoom(const char *arg
) { 
1874         if(!sel 
|| lt
->isfloating 
|| sel
->isfloating
) 
1876         if(c 
== nexttiled(clients
)) 
1877                 if(!(c 
= nexttiled(c
->next
))) 
1886 main(int argc
, char *argv
[]) { 
1887         if(argc 
== 2 && !strcmp("-v", argv
[1])) 
1888                 eprint("dwm-"VERSION
", © 2006-2008 dwm engineers, see LICENSE for details\n"); 
1890                 eprint("usage: dwm [-v]\n"); 
1892         setlocale(LC_CTYPE
, ""); 
1893         if(!(dpy 
= XOpenDisplay(0))) 
1894                 eprint("dwm: cannot open display\n");