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().
31 #include <sys/types.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
40 #include <X11/extensions/Xinerama.h>
44 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
45 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
46 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
47 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
48 #define LENGTH(X) (sizeof X / sizeof X[0])
49 #define MAX(A, B) ((A) > (B) ? (A) : (B))
50 #define MIN(A, B) ((A) < (B) ? (A) : (B))
51 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
52 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
53 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
54 #define TAGMASK ((1 << LENGTH(tags)) - 1)
55 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
58 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
59 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
60 enum { NetSupported
, NetWMName
, NetWMState
,
61 NetWMFullscreen
, NetLast
}; /* EWMH atoms */
62 enum { WMProtocols
, WMDelete
, WMState
, WMLast
}; /* default atoms */
63 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
64 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
77 void (*func
)(const Arg
*arg
);
81 typedef struct Monitor Monitor
;
82 typedef struct Client Client
;
87 int oldx
, oldy
, oldw
, oldh
;
88 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
91 Bool isfixed
, isfloating
, isurgent
, oldstate
;
100 unsigned long norm
[ColLast
];
101 unsigned long sel
[ColLast
];
111 } DC
; /* draw context */
116 void (*func
)(const Arg
*);
122 void (*arrange
)(Monitor
*);
129 int by
; /* bar geometry */
130 int mx
, my
, mw
, mh
; /* screen size */
131 int wx
, wy
, ww
, wh
; /* window area */
132 unsigned int seltags
;
134 unsigned int tagset
[2];
147 const char *instance
;
154 /* function declarations */
155 static void applyrules(Client
*c
);
156 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
157 static void arrange(Monitor
*m
);
158 static void arrangemon(Monitor
*m
);
159 static void attach(Client
*c
);
160 static void attachstack(Client
*c
);
161 static void buttonpress(XEvent
*e
);
162 static void checkotherwm(void);
163 static void cleanup(void);
164 static void cleanupmon(Monitor
*mon
);
165 static void clearurgent(Client
*c
);
166 static void clientmessage(XEvent
*e
);
167 static void configure(Client
*c
);
168 static void configurenotify(XEvent
*e
);
169 static void configurerequest(XEvent
*e
);
170 static Monitor
*createmon(void);
171 static void destroynotify(XEvent
*e
);
172 static void detach(Client
*c
);
173 static void detachstack(Client
*c
);
174 static void die(const char *errstr
, ...);
175 static Monitor
*dirtomon(int dir
);
176 static void drawbar(Monitor
*m
);
177 static void drawbars(void);
178 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
179 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
180 static void enternotify(XEvent
*e
);
181 static void expose(XEvent
*e
);
182 static void focus(Client
*c
);
183 static void focusin(XEvent
*e
);
184 static void focusmon(const Arg
*arg
);
185 static void focusstack(const Arg
*arg
);
186 static unsigned long getcolor(const char *colstr
);
187 static Bool
getrootptr(int *x
, int *y
);
188 static long getstate(Window w
);
189 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
190 static void grabbuttons(Client
*c
, Bool focused
);
191 static void grabkeys(void);
192 static void initfont(const char *fontstr
);
193 static Bool
isprotodel(Client
*c
);
194 static void keypress(XEvent
*e
);
195 static void killclient(const Arg
*arg
);
196 static void manage(Window w
, XWindowAttributes
*wa
);
197 static void mappingnotify(XEvent
*e
);
198 static void maprequest(XEvent
*e
);
199 static void monocle(Monitor
*m
);
200 static void movemouse(const Arg
*arg
);
201 static Client
*nexttiled(Client
*c
);
202 static Monitor
*ptrtomon(int x
, int y
);
203 static void propertynotify(XEvent
*e
);
204 static void quit(const Arg
*arg
);
205 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
206 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
207 static void resizemouse(const Arg
*arg
);
208 static void restack(Monitor
*m
);
209 static void run(void);
210 static void scan(void);
211 static void sendmon(Client
*c
, Monitor
*m
);
212 static void setclientstate(Client
*c
, long state
);
213 static void setlayout(const Arg
*arg
);
214 static void setmfact(const Arg
*arg
);
215 static void setup(void);
216 static void showhide(Client
*c
);
217 static void sigchld(int unused
);
218 static void spawn(const Arg
*arg
);
219 static void tag(const Arg
*arg
);
220 static void tagmon(const Arg
*arg
);
221 static int textnw(const char *text
, unsigned int len
);
222 static void tile(Monitor
*);
223 static void togglebar(const Arg
*arg
);
224 static void togglefloating(const Arg
*arg
);
225 static void toggletag(const Arg
*arg
);
226 static void toggleview(const Arg
*arg
);
227 static void unfocus(Client
*c
, Bool setfocus
);
228 static void unmanage(Client
*c
, Bool destroyed
);
229 static void unmapnotify(XEvent
*e
);
230 static Bool
updategeom(void);
231 static void updatebarpos(Monitor
*m
);
232 static void updatebars(void);
233 static void updatenumlockmask(void);
234 static void updatesizehints(Client
*c
);
235 static void updatestatus(void);
236 static void updatetitle(Client
*c
);
237 static void updatewmhints(Client
*c
);
238 static void view(const Arg
*arg
);
239 static Client
*wintoclient(Window w
);
240 static Monitor
*wintomon(Window w
);
241 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
242 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
243 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
244 static void zoom(const Arg
*arg
);
247 static const char broken
[] = "broken";
248 static char stext
[256];
250 static int sw
, sh
; /* X display screen geometry width, height */
251 static int bh
, blw
= 0; /* bar geometry */
252 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
253 static unsigned int numlockmask
= 0;
254 static void (*handler
[LASTEvent
]) (XEvent
*) = {
255 [ButtonPress
] = buttonpress
,
256 [ClientMessage
] = clientmessage
,
257 [ConfigureRequest
] = configurerequest
,
258 [ConfigureNotify
] = configurenotify
,
259 [DestroyNotify
] = destroynotify
,
260 [EnterNotify
] = enternotify
,
263 [KeyPress
] = keypress
,
264 [MappingNotify
] = mappingnotify
,
265 [MapRequest
] = maprequest
,
266 [PropertyNotify
] = propertynotify
,
267 [UnmapNotify
] = unmapnotify
269 static Atom wmatom
[WMLast
], netatom
[NetLast
];
270 static Bool running
= True
;
271 static Cursor cursor
[CurLast
];
274 static Monitor
*mons
= NULL
, *selmon
= NULL
;
277 /* configuration, allows nested code to access above variables */
280 /* compile-time check if all tags fit into an unsigned int bit array. */
281 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
283 /* function implementations */
285 applyrules(Client
*c
) {
286 const char *class, *instance
;
290 XClassHint ch
= { 0 };
293 c
->isfloating
= c
->tags
= 0;
294 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
295 class = ch
.res_class
? ch
.res_class
: broken
;
296 instance
= ch
.res_name
? ch
.res_name
: broken
;
297 for(i
= 0; i
< LENGTH(rules
); i
++) {
299 if((!r
->title
|| strstr(c
->name
, r
->title
))
300 && (!r
->class || strstr(class, r
->class))
301 && (!r
->instance
|| strstr(instance
, r
->instance
)))
303 c
->isfloating
= r
->isfloating
;
305 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
315 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
319 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
323 /* set minimum possible */
331 if(*x
+ *w
+ 2 * c
->bw
< 0)
333 if(*y
+ *h
+ 2 * c
->bw
< 0)
337 if(*x
> m
->mx
+ m
->mw
)
338 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
339 if(*y
> m
->my
+ m
->mh
)
340 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
341 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
343 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
350 if(resizehints
|| c
->isfloating
) {
351 /* see last two sentences in ICCCM 4.1.2.3 */
352 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
353 if(!baseismin
) { /* temporarily remove base dimensions */
357 /* adjust for aspect limits */
358 if(c
->mina
> 0 && c
->maxa
> 0) {
359 if(c
->maxa
< (float)*w
/ *h
)
360 *w
= *h
* c
->maxa
+ 0.5;
361 else if(c
->mina
< (float)*h
/ *w
)
362 *h
= *w
* c
->mina
+ 0.5;
364 if(baseismin
) { /* increment calculation requires this */
368 /* adjust for increment value */
373 /* restore base dimensions */
374 *w
= MAX(*w
+ c
->basew
, c
->minw
);
375 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
377 *w
= MIN(*w
, c
->maxw
);
379 *h
= MIN(*h
, c
->maxh
);
381 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
385 arrange(Monitor
*m
) {
388 else for(m
= mons
; m
; m
= m
->next
)
393 else for(m
= mons
; m
; m
= m
->next
)
398 arrangemon(Monitor
*m
) {
399 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
400 if(m
->lt
[m
->sellt
]->arrange
)
401 m
->lt
[m
->sellt
]->arrange(m
);
407 c
->next
= c
->mon
->clients
;
412 attachstack(Client
*c
) {
413 c
->snext
= c
->mon
->stack
;
418 buttonpress(XEvent
*e
) {
419 unsigned int i
, x
, click
;
423 XButtonPressedEvent
*ev
= &e
->xbutton
;
426 /* focus monitor if necessary */
427 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
428 unfocus(selmon
->sel
, True
);
432 if(ev
->window
== selmon
->barwin
) {
436 } while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
437 if(i
< LENGTH(tags
)) {
441 else if(ev
->x
< x
+ blw
)
443 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
444 click
= ClkStatusText
;
448 else if((c
= wintoclient(ev
->window
))) {
450 click
= ClkClientWin
;
452 for(i
= 0; i
< LENGTH(buttons
); i
++)
453 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
454 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
455 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
460 xerrorxlib
= XSetErrorHandler(xerrorstart
);
461 /* this causes an error if some other window manager is running */
462 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
464 XSetErrorHandler(xerror
);
471 Layout foo
= { "", NULL
};
475 selmon
->lt
[selmon
->sellt
] = &foo
;
476 for(m
= mons
; m
; m
= m
->next
)
478 unmanage(m
->stack
, False
);
480 XFreeFontSet(dpy
, dc
.font
.set
);
482 XFreeFont(dpy
, dc
.font
.xfont
);
483 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
484 XFreePixmap(dpy
, dc
.drawable
);
486 XFreeCursor(dpy
, cursor
[CurNormal
]);
487 XFreeCursor(dpy
, cursor
[CurResize
]);
488 XFreeCursor(dpy
, cursor
[CurMove
]);
492 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
496 cleanupmon(Monitor
*mon
) {
502 for(m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
505 XUnmapWindow(dpy
, mon
->barwin
);
506 XDestroyWindow(dpy
, mon
->barwin
);
511 clearurgent(Client
*c
) {
515 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
517 wmh
->flags
&= ~XUrgencyHint
;
518 XSetWMHints(dpy
, c
->win
, wmh
);
523 configure(Client
*c
) {
526 ce
.type
= ConfigureNotify
;
534 ce
.border_width
= c
->bw
;
536 ce
.override_redirect
= False
;
537 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
541 configurenotify(XEvent
*e
) {
543 XConfigureEvent
*ev
= &e
->xconfigure
;
545 if(ev
->window
== root
) {
550 XFreePixmap(dpy
, dc
.drawable
);
551 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
553 for(m
= mons
; m
; m
= m
->next
)
554 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
561 configurerequest(XEvent
*e
) {
564 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
567 if((c
= wintoclient(ev
->window
))) {
568 if(ev
->value_mask
& CWBorderWidth
)
569 c
->bw
= ev
->border_width
;
570 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
572 if(ev
->value_mask
& CWX
)
573 c
->x
= m
->mx
+ ev
->x
;
574 if(ev
->value_mask
& CWY
)
575 c
->y
= m
->my
+ ev
->y
;
576 if(ev
->value_mask
& CWWidth
)
578 if(ev
->value_mask
& CWHeight
)
580 if((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
581 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
582 if((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
583 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
584 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
587 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
595 wc
.width
= ev
->width
;
596 wc
.height
= ev
->height
;
597 wc
.border_width
= ev
->border_width
;
598 wc
.sibling
= ev
->above
;
599 wc
.stack_mode
= ev
->detail
;
600 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
609 if(!(m
= (Monitor
*)calloc(1, sizeof(Monitor
))))
610 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
611 m
->tagset
[0] = m
->tagset
[1] = 1;
613 m
->showbar
= showbar
;
615 m
->lt
[0] = &layouts
[0];
616 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
617 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
622 destroynotify(XEvent
*e
) {
624 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
626 if((c
= wintoclient(ev
->window
)))
634 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
639 detachstack(Client
*c
) {
642 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
645 if(c
== c
->mon
->sel
) {
646 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
652 die(const char *errstr
, ...) {
655 va_start(ap
, errstr
);
656 vfprintf(stderr
, errstr
, ap
);
666 if(!(m
= selmon
->next
))
671 for(m
= mons
; m
->next
; m
= m
->next
);
673 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
679 drawbar(Monitor
*m
) {
681 unsigned int i
, occ
= 0, urg
= 0;
685 for(c
= m
->clients
; c
; c
= c
->next
) {
691 for(i
= 0; i
< LENGTH(tags
); i
++) {
692 dc
.w
= TEXTW(tags
[i
]);
693 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
694 drawtext(tags
[i
], col
, urg
& 1 << i
);
695 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
696 occ
& 1 << i
, urg
& 1 << i
, col
);
699 dc
.w
= blw
= TEXTW(m
->ltsymbol
);
700 drawtext(m
->ltsymbol
, dc
.norm
, False
);
703 if(m
== selmon
) { /* status is only drawn on selected monitor */
710 drawtext(stext
, dc
.norm
, False
);
714 if((dc
.w
= dc
.x
- x
) > bh
) {
717 col
= m
== selmon
? dc
.sel
: dc
.norm
;
718 drawtext(m
->sel
->name
, col
, False
);
719 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
722 drawtext(NULL
, dc
.norm
, False
);
724 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
732 for(m
= mons
; m
; m
= m
->next
)
737 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
740 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
742 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
743 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
744 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
748 r
.width
= r
.height
= x
+ 1;
749 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
752 r
.width
= r
.height
= x
;
753 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
758 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
760 int i
, x
, y
, h
, len
, olen
;
761 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
763 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
764 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
768 h
= dc
.font
.ascent
+ dc
.font
.descent
;
769 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
771 /* shorten text if necessary */
772 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
775 memcpy(buf
, text
, len
);
777 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
778 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
780 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
782 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
786 enternotify(XEvent
*e
) {
788 XCrossingEvent
*ev
= &e
->xcrossing
;
790 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
792 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
793 unfocus(selmon
->sel
, True
);
796 focus((wintoclient(ev
->window
)));
802 XExposeEvent
*ev
= &e
->xexpose
;
804 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
810 if(!c
|| !ISVISIBLE(c
))
811 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
812 /* was if(selmon->sel) */
813 if(selmon
->sel
&& selmon
->sel
!= c
)
814 unfocus(selmon
->sel
, False
);
822 grabbuttons(c
, True
);
823 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
824 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
827 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
833 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
834 XFocusChangeEvent
*ev
= &e
->xfocus
;
836 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
837 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
841 focusmon(const Arg
*arg
) {
846 if((m
= dirtomon(arg
->i
)) == selmon
)
848 unfocus(selmon
->sel
, True
);
854 focusstack(const Arg
*arg
) {
855 Client
*c
= NULL
, *i
;
860 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
862 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
865 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
869 for(; i
; i
= i
->next
)
880 getcolor(const char *colstr
) {
881 Colormap cmap
= DefaultColormap(dpy
, screen
);
884 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
885 die("error, cannot allocate color '%s'\n", colstr
);
890 getrootptr(int *x
, int *y
) {
895 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
902 unsigned char *p
= NULL
;
903 unsigned long n
, extra
;
906 if(XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
907 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
916 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
921 if(!text
|| size
== 0)
924 XGetTextProperty(dpy
, w
, &name
, atom
);
927 if(name
.encoding
== XA_STRING
)
928 strncpy(text
, (char *)name
.value
, size
- 1);
930 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
931 strncpy(text
, *list
, size
- 1);
932 XFreeStringList(list
);
935 text
[size
- 1] = '\0';
941 grabbuttons(Client
*c
, Bool focused
) {
945 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
946 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
948 for(i
= 0; i
< LENGTH(buttons
); i
++)
949 if(buttons
[i
].click
== ClkClientWin
)
950 for(j
= 0; j
< LENGTH(modifiers
); j
++)
951 XGrabButton(dpy
, buttons
[i
].button
,
952 buttons
[i
].mask
| modifiers
[j
],
953 c
->win
, False
, BUTTONMASK
,
954 GrabModeAsync
, GrabModeSync
, None
, None
);
957 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
958 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
967 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
970 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
971 for(i
= 0; i
< LENGTH(keys
); i
++) {
972 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
973 for(j
= 0; j
< LENGTH(modifiers
); j
++)
974 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
975 True
, GrabModeAsync
, GrabModeAsync
);
981 initfont(const char *fontstr
) {
982 char *def
, **missing
;
986 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
989 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
990 XFreeStringList(missing
);
993 XFontStruct
**xfonts
;
996 dc
.font
.ascent
= dc
.font
.descent
= 0;
997 XExtentsOfFontSet(dc
.font
.set
);
998 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
1000 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
1001 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1006 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1007 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1008 die("error, cannot load font: '%s'\n", fontstr
);
1009 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1010 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1012 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1016 isprotodel(Client
*c
) {
1021 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1023 ret
= protocols
[n
] == wmatom
[WMDelete
];
1031 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
) {
1033 if(unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
1034 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
1038 #endif /* XINERAMA */
1041 keypress(XEvent
*e
) {
1047 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1048 for(i
= 0; i
< LENGTH(keys
); i
++)
1049 if(keysym
== keys
[i
].keysym
1050 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1052 keys
[i
].func(&(keys
[i
].arg
));
1056 killclient(const Arg
*arg
) {
1061 if(isprotodel(selmon
->sel
)) {
1062 ev
.type
= ClientMessage
;
1063 ev
.xclient
.window
= selmon
->sel
->win
;
1064 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1065 ev
.xclient
.format
= 32;
1066 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1067 ev
.xclient
.data
.l
[1] = CurrentTime
;
1068 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1072 XSetErrorHandler(xerrordummy
);
1073 XSetCloseDownMode(dpy
, DestroyAll
);
1074 XKillClient(dpy
, selmon
->sel
->win
);
1076 XSetErrorHandler(xerror
);
1082 manage(Window w
, XWindowAttributes
*wa
) {
1083 Client
*c
, *t
= NULL
;
1084 Window trans
= None
;
1087 if(!(c
= calloc(1, sizeof(Client
))))
1088 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1091 if(XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1100 c
->x
= c
->oldx
= wa
->x
+ c
->mon
->wx
;
1101 c
->y
= c
->oldy
= wa
->y
+ c
->mon
->wy
;
1102 c
->w
= c
->oldw
= wa
->width
;
1103 c
->h
= c
->oldh
= wa
->height
;
1104 c
->oldbw
= wa
->border_width
;
1105 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1106 c
->isfloating
= True
;
1112 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1113 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1114 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1115 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1116 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1117 /* only fix client y-offset, if the client center might cover the bar */
1118 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1119 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1122 wc
.border_width
= c
->bw
;
1123 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1124 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1125 configure(c
); /* propagates border_width, if size doesn't change */
1127 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1128 grabbuttons(c
, False
);
1130 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1132 XRaiseWindow(dpy
, c
->win
);
1135 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1136 XMapWindow(dpy
, c
->win
);
1137 setclientstate(c
, NormalState
);
1142 mappingnotify(XEvent
*e
) {
1143 XMappingEvent
*ev
= &e
->xmapping
;
1145 XRefreshKeyboardMapping(ev
);
1146 if(ev
->request
== MappingKeyboard
)
1151 maprequest(XEvent
*e
) {
1152 static XWindowAttributes wa
;
1153 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1155 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1157 if(wa
.override_redirect
)
1159 if(!wintoclient(ev
->window
))
1160 manage(ev
->window
, &wa
);
1164 monocle(Monitor
*m
) {
1168 for(c
= m
->clients
; c
; c
= c
->next
)
1171 if(n
> 0) /* override layout symbol */
1172 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1173 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1174 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1178 movemouse(const Arg
*arg
) {
1179 int x
, y
, ocx
, ocy
, nx
, ny
;
1184 if(!(c
= selmon
->sel
))
1189 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1190 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1192 if(!getrootptr(&x
, &y
))
1195 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1197 case ConfigureRequest
:
1200 handler
[ev
.type
](&ev
);
1203 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1204 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1205 if(nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1206 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1207 if(abs(selmon
->wx
- nx
) < snap
)
1209 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1210 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1211 if(abs(selmon
->wy
- ny
) < snap
)
1213 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1214 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1215 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1216 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1217 togglefloating(NULL
);
1219 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1220 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1223 } while(ev
.type
!= ButtonRelease
);
1224 XUngrabPointer(dpy
, CurrentTime
);
1225 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1233 nexttiled(Client
*c
) {
1234 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1239 ptrtomon(int x
, int y
) {
1242 for(m
= mons
; m
; m
= m
->next
)
1243 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1249 propertynotify(XEvent
*e
) {
1252 XPropertyEvent
*ev
= &e
->xproperty
;
1254 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1256 else if(ev
->state
== PropertyDelete
)
1257 return; /* ignore */
1258 else if((c
= wintoclient(ev
->window
))) {
1261 case XA_WM_TRANSIENT_FOR
:
1262 if(!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1263 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1266 case XA_WM_NORMAL_HINTS
:
1274 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1276 if(c
== c
->mon
->sel
)
1283 clientmessage(XEvent
*e
) {
1284 XClientMessageEvent
*cme
= &e
->xclient
;
1287 if((c
= wintoclient(cme
->window
))
1288 && (cme
->message_type
== netatom
[NetWMState
] && cme
->data
.l
[1] == netatom
[NetWMFullscreen
]))
1290 if(cme
->data
.l
[0]) {
1291 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
1292 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1293 c
->oldstate
= c
->isfloating
;
1296 c
->isfloating
= True
;
1297 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1298 XRaiseWindow(dpy
, c
->win
);
1301 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
1302 PropModeReplace
, (unsigned char*)0, 0);
1303 c
->isfloating
= c
->oldstate
;
1309 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1316 quit(const Arg
*arg
) {
1321 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1322 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1323 resizeclient(c
, x
, y
, w
, h
);
1327 resizeclient(Client
*c
, int x
, int y
, int w
, int h
) {
1330 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1331 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1332 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1333 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1334 wc
.border_width
= c
->bw
;
1335 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1341 resizemouse(const Arg
*arg
) {
1348 if(!(c
= selmon
->sel
))
1353 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1354 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1356 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1358 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1360 case ConfigureRequest
:
1363 handler
[ev
.type
](&ev
);
1366 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1367 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1368 if(snap
&& c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1369 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1371 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1372 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1373 togglefloating(NULL
);
1375 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1376 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1379 } while(ev
.type
!= ButtonRelease
);
1380 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1381 XUngrabPointer(dpy
, CurrentTime
);
1382 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1383 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1391 restack(Monitor
*m
) {
1399 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1400 XRaiseWindow(dpy
, m
->sel
->win
);
1401 if(m
->lt
[m
->sellt
]->arrange
) {
1402 wc
.stack_mode
= Below
;
1403 wc
.sibling
= m
->barwin
;
1404 for(c
= m
->stack
; c
; c
= c
->snext
)
1405 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1406 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1407 wc
.sibling
= c
->win
;
1411 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1417 /* main event loop */
1419 while(running
&& !XNextEvent(dpy
, &ev
)) {
1420 if(handler
[ev
.type
])
1421 handler
[ev
.type
](&ev
); /* call handler */
1427 unsigned int i
, num
;
1428 Window d1
, d2
, *wins
= NULL
;
1429 XWindowAttributes wa
;
1431 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1432 for(i
= 0; i
< num
; i
++) {
1433 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1434 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1436 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1437 manage(wins
[i
], &wa
);
1439 for(i
= 0; i
< num
; i
++) { /* now the transients */
1440 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1442 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1443 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1444 manage(wins
[i
], &wa
);
1452 sendmon(Client
*c
, Monitor
*m
) {
1459 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1467 setclientstate(Client
*c
, long state
) {
1468 long data
[] = { state
, None
};
1470 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1471 PropModeReplace
, (unsigned char *)data
, 2);
1475 setlayout(const Arg
*arg
) {
1476 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1479 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1480 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1487 /* arg > 1.0 will set mfact absolutly */
1489 setmfact(const Arg
*arg
) {
1492 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1494 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1495 if(f
< 0.1 || f
> 0.9)
1503 XSetWindowAttributes wa
;
1505 /* clean up any zombies immediately */
1509 screen
= DefaultScreen(dpy
);
1510 root
= RootWindow(dpy
, screen
);
1512 sw
= DisplayWidth(dpy
, screen
);
1513 sh
= DisplayHeight(dpy
, screen
);
1514 bh
= dc
.h
= dc
.font
.height
+ 2;
1517 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1518 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1519 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1520 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1521 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1522 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1523 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1525 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1526 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1527 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1528 /* init appearance */
1529 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1530 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1531 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1532 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1533 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1534 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1535 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1536 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1537 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1539 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1543 /* EWMH support per view */
1544 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1545 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1546 /* select for events */
1547 wa
.cursor
= cursor
[CurNormal
];
1548 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1549 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1550 |PropertyChangeMask
;
1551 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1552 XSelectInput(dpy
, root
, wa
.event_mask
);
1557 showhide(Client
*c
) {
1560 if(ISVISIBLE(c
)) { /* show clients top down */
1561 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1562 if(!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1563 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1566 else { /* hide clients bottom up */
1568 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1573 sigchld(int unused
) {
1574 if(signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1575 die("Can't install SIGCHLD handler");
1576 while(0 < waitpid(-1, NULL
, WNOHANG
));
1580 spawn(const Arg
*arg
) {
1583 close(ConnectionNumber(dpy
));
1585 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1586 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1593 tag(const Arg
*arg
) {
1594 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1595 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1601 tagmon(const Arg
*arg
) {
1602 if(!selmon
->sel
|| !mons
->next
)
1604 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1608 textnw(const char *text
, unsigned int len
) {
1612 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1615 return XTextWidth(dc
.font
.xfont
, text
, len
);
1624 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1628 c
= nexttiled(m
->clients
);
1629 mw
= m
->mfact
* m
->ww
;
1630 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1634 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1636 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1640 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1641 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1642 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1644 y
= c
->y
+ HEIGHT(c
);
1649 togglebar(const Arg
*arg
) {
1650 selmon
->showbar
= !selmon
->showbar
;
1651 updatebarpos(selmon
);
1652 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1657 togglefloating(const Arg
*arg
) {
1660 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1661 if(selmon
->sel
->isfloating
)
1662 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1663 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1668 toggletag(const Arg
*arg
) {
1669 unsigned int newtags
;
1673 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1675 selmon
->sel
->tags
= newtags
;
1681 toggleview(const Arg
*arg
) {
1682 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1685 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1691 unfocus(Client
*c
, Bool setfocus
) {
1694 grabbuttons(c
, False
);
1695 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1697 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1701 unmanage(Client
*c
, Bool destroyed
) {
1702 Monitor
*m
= c
->mon
;
1705 /* The server grab construct avoids race conditions. */
1709 wc
.border_width
= c
->oldbw
;
1711 XSetErrorHandler(xerrordummy
);
1712 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1713 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1714 setclientstate(c
, WithdrawnState
);
1716 XSetErrorHandler(xerror
);
1725 unmapnotify(XEvent
*e
) {
1727 XUnmapEvent
*ev
= &e
->xunmap
;
1729 if((c
= wintoclient(ev
->window
)))
1736 XSetWindowAttributes wa
;
1738 wa
.override_redirect
= True
;
1739 wa
.background_pixmap
= ParentRelative
;
1740 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1741 for(m
= mons
; m
; m
= m
->next
) {
1742 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1743 CopyFromParent
, DefaultVisual(dpy
, screen
),
1744 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1745 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1746 XMapRaised(dpy
, m
->barwin
);
1751 updatebarpos(Monitor
*m
) {
1756 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1757 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1768 if(XineramaIsActive(dpy
)) {
1772 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1773 XineramaScreenInfo
*unique
= NULL
;
1775 for(n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1776 /* only consider unique geometries as separate screens */
1777 if(!(unique
= (XineramaScreenInfo
*)malloc(sizeof(XineramaScreenInfo
) * nn
)))
1778 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo
) * nn
);
1779 for(i
= 0, j
= 0; i
< nn
; i
++)
1780 if(isuniquegeom(unique
, j
, &info
[i
]))
1781 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1785 for(i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1786 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1788 m
->next
= createmon();
1792 for(i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1794 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1795 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1799 m
->mx
= m
->wx
= unique
[i
].x_org
;
1800 m
->my
= m
->wy
= unique
[i
].y_org
;
1801 m
->mw
= m
->ww
= unique
[i
].width
;
1802 m
->mh
= m
->wh
= unique
[i
].height
;
1806 else { /* less monitors available nn < n */
1807 for(i
= nn
; i
< n
; i
++) {
1808 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1812 m
->clients
= c
->next
;
1826 #endif /* XINERAMA */
1827 /* default monitor setup */
1831 if(mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1833 mons
->mw
= mons
->ww
= sw
;
1834 mons
->mh
= mons
->wh
= sh
;
1840 selmon
= wintomon(root
);
1846 updatenumlockmask(void) {
1848 XModifierKeymap
*modmap
;
1851 modmap
= XGetModifierMapping(dpy
);
1852 for(i
= 0; i
< 8; i
++)
1853 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1854 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1855 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1856 numlockmask
= (1 << i
);
1857 XFreeModifiermap(modmap
);
1861 updatesizehints(Client
*c
) {
1865 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1866 /* size is uninitialized, ensure that size.flags aren't used */
1868 if(size
.flags
& PBaseSize
) {
1869 c
->basew
= size
.base_width
;
1870 c
->baseh
= size
.base_height
;
1872 else if(size
.flags
& PMinSize
) {
1873 c
->basew
= size
.min_width
;
1874 c
->baseh
= size
.min_height
;
1877 c
->basew
= c
->baseh
= 0;
1878 if(size
.flags
& PResizeInc
) {
1879 c
->incw
= size
.width_inc
;
1880 c
->inch
= size
.height_inc
;
1883 c
->incw
= c
->inch
= 0;
1884 if(size
.flags
& PMaxSize
) {
1885 c
->maxw
= size
.max_width
;
1886 c
->maxh
= size
.max_height
;
1889 c
->maxw
= c
->maxh
= 0;
1890 if(size
.flags
& PMinSize
) {
1891 c
->minw
= size
.min_width
;
1892 c
->minh
= size
.min_height
;
1894 else if(size
.flags
& PBaseSize
) {
1895 c
->minw
= size
.base_width
;
1896 c
->minh
= size
.base_height
;
1899 c
->minw
= c
->minh
= 0;
1900 if(size
.flags
& PAspect
) {
1901 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1902 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1905 c
->maxa
= c
->mina
= 0.0;
1906 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1907 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1911 updatetitle(Client
*c
) {
1912 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1913 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1914 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1915 strcpy(c
->name
, broken
);
1919 updatestatus(void) {
1920 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1921 strcpy(stext
, "dwm-"VERSION
);
1926 updatewmhints(Client
*c
) {
1929 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1930 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1931 wmh
->flags
&= ~XUrgencyHint
;
1932 XSetWMHints(dpy
, c
->win
, wmh
);
1935 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1941 view(const Arg
*arg
) {
1942 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1944 selmon
->seltags
^= 1; /* toggle sel tagset */
1945 if(arg
->ui
& TAGMASK
)
1946 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1951 wintoclient(Window w
) {
1955 for(m
= mons
; m
; m
= m
->next
)
1956 for(c
= m
->clients
; c
; c
= c
->next
)
1963 wintomon(Window w
) {
1968 if(w
== root
&& getrootptr(&x
, &y
))
1969 return ptrtomon(x
, y
);
1970 for(m
= mons
; m
; m
= m
->next
)
1973 if((c
= wintoclient(w
)))
1978 /* There's no way to check accesses to destroyed windows, thus those cases are
1979 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1980 * default error handler, which may call exit. */
1982 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1983 if(ee
->error_code
== BadWindow
1984 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1985 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1986 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1987 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1988 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1989 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1990 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1991 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1993 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1994 ee
->request_code
, ee
->error_code
);
1995 return xerrorxlib(dpy
, ee
); /* may call exit */
1999 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
2003 /* Startup Error handler to check if another window manager
2004 * is already running. */
2006 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
2007 die("dwm: another window manager is already running\n");
2012 zoom(const Arg
*arg
) {
2013 Client
*c
= selmon
->sel
;
2015 if(!selmon
->lt
[selmon
->sellt
]->arrange
2016 || selmon
->lt
[selmon
->sellt
]->arrange
== monocle
2017 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2019 if(c
== nexttiled(selmon
->clients
))
2020 if(!c
|| !(c
= nexttiled(c
->next
)))
2029 main(int argc
, char *argv
[]) {
2030 if(argc
== 2 && !strcmp("-v", argv
[1]))
2031 die("dwm-"VERSION
", © 2006-2010 dwm engineers, see LICENSE for details\n");
2033 die("usage: dwm [-v]\n");
2034 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2035 fputs("warning: no locale support\n", stderr
);
2036 if(!(dpy
= XOpenDisplay(NULL
)))
2037 die("dwm: cannot open display\n");