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
];
271 static Bool running
= True
;
272 static Cursor cursor
[CurLast
];
275 static Monitor
*mons
= NULL
, *selmon
= NULL
;
278 /* configuration, allows nested code to access above variables */
281 /* compile-time check if all tags fit into an unsigned int bit array. */
282 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
284 /* function implementations */
286 applyrules(Client
*c
) {
287 const char *class, *instance
;
291 XClassHint ch
= { 0 };
294 c
->isfloating
= c
->tags
= 0;
295 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
296 class = ch
.res_class
? ch
.res_class
: broken
;
297 instance
= ch
.res_name
? ch
.res_name
: broken
;
298 for(i
= 0; i
< LENGTH(rules
); i
++) {
300 if((!r
->title
|| strstr(c
->name
, r
->title
))
301 && (!r
->class || strstr(class, r
->class))
302 && (!r
->instance
|| strstr(instance
, r
->instance
)))
304 c
->isfloating
= r
->isfloating
;
306 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
316 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
320 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
324 /* set minimum possible */
332 if(*x
+ *w
+ 2 * c
->bw
< 0)
334 if(*y
+ *h
+ 2 * c
->bw
< 0)
338 if(*x
> m
->mx
+ m
->mw
)
339 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
340 if(*y
> m
->my
+ m
->mh
)
341 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
342 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
344 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
351 if(resizehints
|| c
->isfloating
) {
352 /* see last two sentences in ICCCM 4.1.2.3 */
353 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
354 if(!baseismin
) { /* temporarily remove base dimensions */
358 /* adjust for aspect limits */
359 if(c
->mina
> 0 && c
->maxa
> 0) {
360 if(c
->maxa
< (float)*w
/ *h
)
361 *w
= *h
* c
->maxa
+ 0.5;
362 else if(c
->mina
< (float)*h
/ *w
)
363 *h
= *w
* c
->mina
+ 0.5;
365 if(baseismin
) { /* increment calculation requires this */
369 /* adjust for increment value */
374 /* restore base dimensions */
377 *w
= MAX(*w
, c
->minw
);
378 *h
= MAX(*h
, c
->minh
);
380 *w
= MIN(*w
, c
->maxw
);
382 *h
= MIN(*h
, c
->maxh
);
384 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
388 arrange(Monitor
*m
) {
391 else for(m
= mons
; m
; m
= m
->next
)
396 else for(m
= mons
; m
; m
= m
->next
)
401 arrangemon(Monitor
*m
) {
402 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
403 if(m
->lt
[m
->sellt
]->arrange
)
404 m
->lt
[m
->sellt
]->arrange(m
);
410 c
->next
= c
->mon
->clients
;
415 attachstack(Client
*c
) {
416 c
->snext
= c
->mon
->stack
;
421 buttonpress(XEvent
*e
) {
422 unsigned int i
, x
, click
;
426 XButtonPressedEvent
*ev
= &e
->xbutton
;
429 /* focus monitor if necessary */
430 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
431 unfocus(selmon
->sel
, True
);
435 if(ev
->window
== selmon
->barwin
) {
439 } while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
440 if(i
< LENGTH(tags
)) {
444 else if(ev
->x
< x
+ blw
)
446 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
447 click
= ClkStatusText
;
451 else if((c
= wintoclient(ev
->window
))) {
453 click
= ClkClientWin
;
455 for(i
= 0; i
< LENGTH(buttons
); i
++)
456 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
457 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
458 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
464 xerrorxlib
= XSetErrorHandler(xerrorstart
);
465 /* this causes an error if some other window manager is running */
466 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
469 die("dwm: another window manager is already running\n");
470 XSetErrorHandler(xerror
);
477 Layout foo
= { "", NULL
};
481 selmon
->lt
[selmon
->sellt
] = &foo
;
482 for(m
= mons
; m
; m
= m
->next
)
484 unmanage(m
->stack
, False
);
486 XFreeFontSet(dpy
, dc
.font
.set
);
488 XFreeFont(dpy
, dc
.font
.xfont
);
489 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
490 XFreePixmap(dpy
, dc
.drawable
);
492 XFreeCursor(dpy
, cursor
[CurNormal
]);
493 XFreeCursor(dpy
, cursor
[CurResize
]);
494 XFreeCursor(dpy
, cursor
[CurMove
]);
498 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
502 cleanupmon(Monitor
*mon
) {
508 for(m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
511 XUnmapWindow(dpy
, mon
->barwin
);
512 XDestroyWindow(dpy
, mon
->barwin
);
517 clearurgent(Client
*c
) {
521 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
523 wmh
->flags
&= ~XUrgencyHint
;
524 XSetWMHints(dpy
, c
->win
, wmh
);
529 configure(Client
*c
) {
532 ce
.type
= ConfigureNotify
;
540 ce
.border_width
= c
->bw
;
542 ce
.override_redirect
= False
;
543 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
547 configurenotify(XEvent
*e
) {
549 XConfigureEvent
*ev
= &e
->xconfigure
;
551 if(ev
->window
== root
) {
556 XFreePixmap(dpy
, dc
.drawable
);
557 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
559 for(m
= mons
; m
; m
= m
->next
)
560 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
567 configurerequest(XEvent
*e
) {
570 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
573 if((c
= wintoclient(ev
->window
))) {
574 if(ev
->value_mask
& CWBorderWidth
)
575 c
->bw
= ev
->border_width
;
576 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
578 if(ev
->value_mask
& CWX
)
579 c
->x
= m
->mx
+ ev
->x
;
580 if(ev
->value_mask
& CWY
)
581 c
->y
= m
->my
+ ev
->y
;
582 if(ev
->value_mask
& CWWidth
)
584 if(ev
->value_mask
& CWHeight
)
586 if((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
587 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
588 if((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
589 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
590 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
593 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
601 wc
.width
= ev
->width
;
602 wc
.height
= ev
->height
;
603 wc
.border_width
= ev
->border_width
;
604 wc
.sibling
= ev
->above
;
605 wc
.stack_mode
= ev
->detail
;
606 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
615 if(!(m
= (Monitor
*)calloc(1, sizeof(Monitor
))))
616 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
617 m
->tagset
[0] = m
->tagset
[1] = 1;
619 m
->showbar
= showbar
;
621 m
->lt
[0] = &layouts
[0];
622 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
623 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
628 destroynotify(XEvent
*e
) {
630 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
632 if((c
= wintoclient(ev
->window
)))
640 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
645 detachstack(Client
*c
) {
648 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
651 if(c
== c
->mon
->sel
) {
652 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
658 die(const char *errstr
, ...) {
661 va_start(ap
, errstr
);
662 vfprintf(stderr
, errstr
, ap
);
672 if(!(m
= selmon
->next
))
677 for(m
= mons
; m
->next
; m
= m
->next
);
679 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
685 drawbar(Monitor
*m
) {
687 unsigned int i
, occ
= 0, urg
= 0;
691 for(c
= m
->clients
; c
; c
= c
->next
) {
697 for(i
= 0; i
< LENGTH(tags
); i
++) {
698 dc
.w
= TEXTW(tags
[i
]);
699 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
700 drawtext(tags
[i
], col
, urg
& 1 << i
);
701 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
702 occ
& 1 << i
, urg
& 1 << i
, col
);
705 dc
.w
= blw
= TEXTW(m
->ltsymbol
);
706 drawtext(m
->ltsymbol
, dc
.norm
, False
);
709 if(m
== selmon
) { /* status is only drawn on selected monitor */
716 drawtext(stext
, dc
.norm
, False
);
720 if((dc
.w
= dc
.x
- x
) > bh
) {
723 col
= m
== selmon
? dc
.sel
: dc
.norm
;
724 drawtext(m
->sel
->name
, col
, False
);
725 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
728 drawtext(NULL
, dc
.norm
, False
);
730 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
738 for(m
= mons
; m
; m
= m
->next
)
743 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
746 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
748 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
749 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
750 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
754 r
.width
= r
.height
= x
+ 1;
755 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
758 r
.width
= r
.height
= x
;
759 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
764 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
766 int i
, x
, y
, h
, len
, olen
;
767 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
769 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
770 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
774 h
= dc
.font
.ascent
+ dc
.font
.descent
;
775 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
777 /* shorten text if necessary */
778 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
781 memcpy(buf
, text
, len
);
783 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
784 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
786 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
788 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
792 enternotify(XEvent
*e
) {
794 XCrossingEvent
*ev
= &e
->xcrossing
;
796 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
798 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
799 unfocus(selmon
->sel
, True
);
802 focus(wintoclient(ev
->window
));
808 XExposeEvent
*ev
= &e
->xexpose
;
810 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
816 if(!c
|| !ISVISIBLE(c
))
817 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
818 /* was if(selmon->sel) */
819 if(selmon
->sel
&& selmon
->sel
!= c
)
820 unfocus(selmon
->sel
, False
);
828 grabbuttons(c
, True
);
829 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
830 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
833 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
839 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
840 XFocusChangeEvent
*ev
= &e
->xfocus
;
842 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
843 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
847 focusmon(const Arg
*arg
) {
852 if((m
= dirtomon(arg
->i
)) == selmon
)
854 unfocus(selmon
->sel
, True
);
860 focusstack(const Arg
*arg
) {
861 Client
*c
= NULL
, *i
;
866 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
868 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
871 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
875 for(; i
; i
= i
->next
)
886 getcolor(const char *colstr
) {
887 Colormap cmap
= DefaultColormap(dpy
, screen
);
890 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
891 die("error, cannot allocate color '%s'\n", colstr
);
896 getrootptr(int *x
, int *y
) {
901 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
908 unsigned char *p
= NULL
;
909 unsigned long n
, extra
;
912 if(XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
913 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
922 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
927 if(!text
|| size
== 0)
930 XGetTextProperty(dpy
, w
, &name
, atom
);
933 if(name
.encoding
== XA_STRING
)
934 strncpy(text
, (char *)name
.value
, size
- 1);
936 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
937 strncpy(text
, *list
, size
- 1);
938 XFreeStringList(list
);
941 text
[size
- 1] = '\0';
947 grabbuttons(Client
*c
, Bool focused
) {
951 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
952 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
954 for(i
= 0; i
< LENGTH(buttons
); i
++)
955 if(buttons
[i
].click
== ClkClientWin
)
956 for(j
= 0; j
< LENGTH(modifiers
); j
++)
957 XGrabButton(dpy
, buttons
[i
].button
,
958 buttons
[i
].mask
| modifiers
[j
],
959 c
->win
, False
, BUTTONMASK
,
960 GrabModeAsync
, GrabModeSync
, None
, None
);
963 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
964 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
973 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
976 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
977 for(i
= 0; i
< LENGTH(keys
); i
++) {
978 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
979 for(j
= 0; j
< LENGTH(modifiers
); j
++)
980 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
981 True
, GrabModeAsync
, GrabModeAsync
);
987 initfont(const char *fontstr
) {
988 char *def
, **missing
;
992 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
995 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
996 XFreeStringList(missing
);
999 XFontSetExtents
*font_extents
;
1000 XFontStruct
**xfonts
;
1003 dc
.font
.ascent
= dc
.font
.descent
= 0;
1004 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
1005 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
1006 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
1007 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
1008 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1013 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1014 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1015 die("error, cannot load font: '%s'\n", fontstr
);
1016 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1017 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1019 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1023 isprotodel(Client
*c
) {
1028 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1029 for(i
= 0; !ret
&& i
< n
; i
++)
1030 if(protocols
[i
] == wmatom
[WMDelete
])
1039 isuniquegeom(XineramaScreenInfo
*unique
, size_t len
, XineramaScreenInfo
*info
) {
1042 for(i
= 0; i
< len
; i
++)
1043 if(unique
[i
].x_org
== info
->x_org
&& unique
[i
].y_org
== info
->y_org
1044 && unique
[i
].width
== info
->width
&& unique
[i
].height
== info
->height
)
1048 #endif /* XINERAMA */
1051 keypress(XEvent
*e
) {
1057 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1058 for(i
= 0; i
< LENGTH(keys
); i
++)
1059 if(keysym
== keys
[i
].keysym
1060 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1062 keys
[i
].func(&(keys
[i
].arg
));
1066 killclient(const Arg
*arg
) {
1071 if(isprotodel(selmon
->sel
)) {
1072 ev
.type
= ClientMessage
;
1073 ev
.xclient
.window
= selmon
->sel
->win
;
1074 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1075 ev
.xclient
.format
= 32;
1076 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1077 ev
.xclient
.data
.l
[1] = CurrentTime
;
1078 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1082 XSetErrorHandler(xerrordummy
);
1083 XSetCloseDownMode(dpy
, DestroyAll
);
1084 XKillClient(dpy
, selmon
->sel
->win
);
1086 XSetErrorHandler(xerror
);
1092 manage(Window w
, XWindowAttributes
*wa
) {
1094 Client
*c
, *t
= NULL
;
1095 Window trans
= None
;
1098 if(!(c
= malloc(sizeof(Client
))))
1099 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1103 if(XGetTransientForHint(dpy
, w
, &trans
))
1104 t
= wintoclient(trans
);
1114 c
->x
= c
->oldx
= wa
->x
+ c
->mon
->wx
;
1115 c
->y
= c
->oldy
= wa
->y
+ c
->mon
->wy
;
1116 c
->w
= c
->oldw
= wa
->width
;
1117 c
->h
= c
->oldh
= wa
->height
;
1118 c
->oldbw
= wa
->border_width
;
1119 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1126 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1127 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1128 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1129 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1130 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1131 /* only fix client y-offset, if the client center might cover the bar */
1132 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1133 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1136 wc
.border_width
= c
->bw
;
1137 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1138 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1139 configure(c
); /* propagates border_width, if size doesn't change */
1141 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1142 grabbuttons(c
, False
);
1144 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1146 XRaiseWindow(dpy
, c
->win
);
1149 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1150 XMapWindow(dpy
, c
->win
);
1151 setclientstate(c
, NormalState
);
1156 mappingnotify(XEvent
*e
) {
1157 XMappingEvent
*ev
= &e
->xmapping
;
1159 XRefreshKeyboardMapping(ev
);
1160 if(ev
->request
== MappingKeyboard
)
1165 maprequest(XEvent
*e
) {
1166 static XWindowAttributes wa
;
1167 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1169 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1171 if(wa
.override_redirect
)
1173 if(!wintoclient(ev
->window
))
1174 manage(ev
->window
, &wa
);
1178 monocle(Monitor
*m
) {
1182 for(c
= m
->clients
; c
; c
= c
->next
)
1185 if(n
> 0) /* override layout symbol */
1186 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1187 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1188 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1192 movemouse(const Arg
*arg
) {
1193 int x
, y
, ocx
, ocy
, nx
, ny
;
1198 if(!(c
= selmon
->sel
))
1203 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1204 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1206 if(!getrootptr(&x
, &y
))
1209 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1211 case ConfigureRequest
:
1214 handler
[ev
.type
](&ev
);
1217 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1218 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1219 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1220 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1221 if(abs(selmon
->wx
- nx
) < snap
)
1223 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1224 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1225 if(abs(selmon
->wy
- ny
) < snap
)
1227 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1228 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1229 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1230 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1231 togglefloating(NULL
);
1233 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1234 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1237 } while(ev
.type
!= ButtonRelease
);
1238 XUngrabPointer(dpy
, CurrentTime
);
1239 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1247 nexttiled(Client
*c
) {
1248 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1253 ptrtomon(int x
, int y
) {
1256 for(m
= mons
; m
; m
= m
->next
)
1257 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1263 propertynotify(XEvent
*e
) {
1266 XPropertyEvent
*ev
= &e
->xproperty
;
1268 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1270 else if(ev
->state
== PropertyDelete
)
1271 return; /* ignore */
1272 else if((c
= wintoclient(ev
->window
))) {
1275 case XA_WM_TRANSIENT_FOR
:
1276 XGetTransientForHint(dpy
, c
->win
, &trans
);
1277 if(!c
->isfloating
&& (c
->isfloating
= (wintoclient(trans
) != NULL
)))
1280 case XA_WM_NORMAL_HINTS
:
1288 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1290 if(c
== c
->mon
->sel
)
1297 clientmessage(XEvent
*e
) {
1298 XClientMessageEvent
*cme
= &e
->xclient
;
1301 if((c
= wintoclient(cme
->window
))
1302 && (cme
->message_type
== netatom
[NetWMState
] && cme
->data
.l
[1] == netatom
[NetWMFullscreen
]))
1304 if(cme
->data
.l
[0]) {
1305 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
1306 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1307 c
->oldstate
= c
->isfloating
;
1311 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1312 XRaiseWindow(dpy
, c
->win
);
1315 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
1316 PropModeReplace
, (unsigned char*)0, 0);
1317 c
->isfloating
= c
->oldstate
;
1323 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1330 quit(const Arg
*arg
) {
1335 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1336 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1337 resizeclient(c
, x
, y
, w
, h
);
1341 resizeclient(Client
*c
, int x
, int y
, int w
, int h
) {
1344 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1345 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1346 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1347 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1348 wc
.border_width
= c
->bw
;
1349 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1355 resizemouse(const Arg
*arg
) {
1362 if(!(c
= selmon
->sel
))
1367 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1368 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1370 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1372 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1374 case ConfigureRequest
:
1377 handler
[ev
.type
](&ev
);
1380 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1381 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1382 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1383 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
)
1385 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1386 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1387 togglefloating(NULL
);
1389 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1390 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1393 } while(ev
.type
!= ButtonRelease
);
1394 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1395 XUngrabPointer(dpy
, CurrentTime
);
1396 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1397 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1405 restack(Monitor
*m
) {
1413 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1414 XRaiseWindow(dpy
, m
->sel
->win
);
1415 if(m
->lt
[m
->sellt
]->arrange
) {
1416 wc
.stack_mode
= Below
;
1417 wc
.sibling
= m
->barwin
;
1418 for(c
= m
->stack
; c
; c
= c
->snext
)
1419 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1420 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1421 wc
.sibling
= c
->win
;
1425 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1431 /* main event loop */
1433 while(running
&& !XNextEvent(dpy
, &ev
)) {
1434 if(handler
[ev
.type
])
1435 handler
[ev
.type
](&ev
); /* call handler */
1441 unsigned int i
, num
;
1442 Window d1
, d2
, *wins
= NULL
;
1443 XWindowAttributes wa
;
1445 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1446 for(i
= 0; i
< num
; i
++) {
1447 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1448 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1450 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1451 manage(wins
[i
], &wa
);
1453 for(i
= 0; i
< num
; i
++) { /* now the transients */
1454 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1456 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1457 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1458 manage(wins
[i
], &wa
);
1466 sendmon(Client
*c
, Monitor
*m
) {
1473 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1481 setclientstate(Client
*c
, long state
) {
1482 long data
[] = { state
, None
};
1484 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1485 PropModeReplace
, (unsigned char *)data
, 2);
1489 setlayout(const Arg
*arg
) {
1490 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1493 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1494 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1501 /* arg > 1.0 will set mfact absolutly */
1503 setmfact(const Arg
*arg
) {
1506 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1508 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1509 if(f
< 0.1 || f
> 0.9)
1517 XSetWindowAttributes wa
;
1519 /* clean up any zombies immediately */
1523 screen
= DefaultScreen(dpy
);
1524 root
= RootWindow(dpy
, screen
);
1526 sw
= DisplayWidth(dpy
, screen
);
1527 sh
= DisplayHeight(dpy
, screen
);
1528 bh
= dc
.h
= dc
.font
.height
+ 2;
1531 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1532 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1533 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1534 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1535 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1536 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1537 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1539 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1540 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1541 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1542 /* init appearance */
1543 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1544 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1545 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1546 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1547 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1548 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1549 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1550 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1551 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1553 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1557 /* EWMH support per view */
1558 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1559 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1560 /* select for events */
1561 wa
.cursor
= cursor
[CurNormal
];
1562 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1563 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1564 |PropertyChangeMask
;
1565 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1566 XSelectInput(dpy
, root
, wa
.event_mask
);
1571 showhide(Client
*c
) {
1574 if(ISVISIBLE(c
)) { /* show clients top down */
1575 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1576 if(!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1577 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1580 else { /* hide clients bottom up */
1582 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1588 sigchld(int unused
) {
1589 if(signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1590 die("Can't install SIGCHLD handler");
1591 while(0 < waitpid(-1, NULL
, WNOHANG
));
1595 spawn(const Arg
*arg
) {
1598 close(ConnectionNumber(dpy
));
1600 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1601 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1608 tag(const Arg
*arg
) {
1609 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1610 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1616 tagmon(const Arg
*arg
) {
1617 if(!selmon
->sel
|| !mons
->next
)
1619 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1623 textnw(const char *text
, unsigned int len
) {
1627 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1630 return XTextWidth(dc
.font
.xfont
, text
, len
);
1639 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1643 c
= nexttiled(m
->clients
);
1644 mw
= m
->mfact
* m
->ww
;
1645 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1649 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1651 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1655 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1656 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1657 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1659 y
= c
->y
+ HEIGHT(c
);
1664 togglebar(const Arg
*arg
) {
1665 selmon
->showbar
= !selmon
->showbar
;
1666 updatebarpos(selmon
);
1667 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1672 togglefloating(const Arg
*arg
) {
1675 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1676 if(selmon
->sel
->isfloating
)
1677 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1678 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1683 toggletag(const Arg
*arg
) {
1684 unsigned int newtags
;
1688 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1690 selmon
->sel
->tags
= newtags
;
1696 toggleview(const Arg
*arg
) {
1697 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1700 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1706 unfocus(Client
*c
, Bool setfocus
) {
1709 grabbuttons(c
, False
);
1710 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1712 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1716 unmanage(Client
*c
, Bool destroyed
) {
1717 Monitor
*m
= c
->mon
;
1720 /* The server grab construct avoids race conditions. */
1724 wc
.border_width
= c
->oldbw
;
1726 XSetErrorHandler(xerrordummy
);
1727 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1728 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1729 setclientstate(c
, WithdrawnState
);
1731 XSetErrorHandler(xerror
);
1740 unmapnotify(XEvent
*e
) {
1742 XUnmapEvent
*ev
= &e
->xunmap
;
1744 if((c
= wintoclient(ev
->window
)))
1751 XSetWindowAttributes wa
;
1753 wa
.override_redirect
= True
;
1754 wa
.background_pixmap
= ParentRelative
;
1755 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1756 for(m
= mons
; m
; m
= m
->next
) {
1757 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1758 CopyFromParent
, DefaultVisual(dpy
, screen
),
1759 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1760 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1761 XMapRaised(dpy
, m
->barwin
);
1766 updatebarpos(Monitor
*m
) {
1771 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1772 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1783 if(XineramaIsActive(dpy
)) {
1787 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1788 XineramaScreenInfo
*unique
= NULL
;
1790 for(n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1791 /* only consider unique geometries as separate screens */
1792 if(!(unique
= (XineramaScreenInfo
*)malloc(sizeof(XineramaScreenInfo
) * nn
)))
1793 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo
) * nn
);
1794 for(i
= 0, j
= 0; i
< nn
; i
++)
1795 if(isuniquegeom(unique
, j
, &info
[i
]))
1796 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1800 for(i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1801 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1803 m
->next
= createmon();
1807 for(i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1809 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1810 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1814 m
->mx
= m
->wx
= unique
[i
].x_org
;
1815 m
->my
= m
->wy
= unique
[i
].y_org
;
1816 m
->mw
= m
->ww
= unique
[i
].width
;
1817 m
->mh
= m
->wh
= unique
[i
].height
;
1821 else { /* less monitors available nn < n */
1822 for(i
= nn
; i
< n
; i
++) {
1823 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1827 m
->clients
= c
->next
;
1841 #endif /* XINERAMA */
1842 /* default monitor setup */
1846 if(mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1848 mons
->mw
= mons
->ww
= sw
;
1849 mons
->mh
= mons
->wh
= sh
;
1855 selmon
= wintomon(root
);
1861 updatenumlockmask(void) {
1863 XModifierKeymap
*modmap
;
1866 modmap
= XGetModifierMapping(dpy
);
1867 for(i
= 0; i
< 8; i
++)
1868 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1869 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1870 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1871 numlockmask
= (1 << i
);
1872 XFreeModifiermap(modmap
);
1876 updatesizehints(Client
*c
) {
1880 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1881 /* size is uninitialized, ensure that size.flags aren't used */
1883 if(size
.flags
& PBaseSize
) {
1884 c
->basew
= size
.base_width
;
1885 c
->baseh
= size
.base_height
;
1887 else if(size
.flags
& PMinSize
) {
1888 c
->basew
= size
.min_width
;
1889 c
->baseh
= size
.min_height
;
1892 c
->basew
= c
->baseh
= 0;
1893 if(size
.flags
& PResizeInc
) {
1894 c
->incw
= size
.width_inc
;
1895 c
->inch
= size
.height_inc
;
1898 c
->incw
= c
->inch
= 0;
1899 if(size
.flags
& PMaxSize
) {
1900 c
->maxw
= size
.max_width
;
1901 c
->maxh
= size
.max_height
;
1904 c
->maxw
= c
->maxh
= 0;
1905 if(size
.flags
& PMinSize
) {
1906 c
->minw
= size
.min_width
;
1907 c
->minh
= size
.min_height
;
1909 else if(size
.flags
& PBaseSize
) {
1910 c
->minw
= size
.base_width
;
1911 c
->minh
= size
.base_height
;
1914 c
->minw
= c
->minh
= 0;
1915 if(size
.flags
& PAspect
) {
1916 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1917 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1920 c
->maxa
= c
->mina
= 0.0;
1921 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1922 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1926 updatetitle(Client
*c
) {
1927 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1928 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1929 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1930 strcpy(c
->name
, broken
);
1934 updatestatus(void) {
1935 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1936 strcpy(stext
, "dwm-"VERSION
);
1941 updatewmhints(Client
*c
) {
1944 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1945 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1946 wmh
->flags
&= ~XUrgencyHint
;
1947 XSetWMHints(dpy
, c
->win
, wmh
);
1950 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1956 view(const Arg
*arg
) {
1957 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1959 selmon
->seltags
^= 1; /* toggle sel tagset */
1960 if(arg
->ui
& TAGMASK
)
1961 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1966 wintoclient(Window w
) {
1970 for(m
= mons
; m
; m
= m
->next
)
1971 for(c
= m
->clients
; c
; c
= c
->next
)
1978 wintomon(Window w
) {
1983 if(w
== root
&& getrootptr(&x
, &y
))
1984 return ptrtomon(x
, y
);
1985 for(m
= mons
; m
; m
= m
->next
)
1988 if((c
= wintoclient(w
)))
1993 /* There's no way to check accesses to destroyed windows, thus those cases are
1994 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1995 * default error handler, which may call exit. */
1997 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1998 if(ee
->error_code
== BadWindow
1999 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2000 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2001 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2002 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2003 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2004 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2005 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2006 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2008 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2009 ee
->request_code
, ee
->error_code
);
2010 return xerrorxlib(dpy
, ee
); /* may call exit */
2014 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
2018 /* Startup Error handler to check if another window manager
2019 * is already running. */
2021 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
2027 zoom(const Arg
*arg
) {
2028 Client
*c
= selmon
->sel
;
2030 if(!selmon
->lt
[selmon
->sellt
]->arrange
2031 || selmon
->lt
[selmon
->sellt
]->arrange
== monocle
2032 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2034 if(c
== nexttiled(selmon
->clients
))
2035 if(!c
|| !(c
= nexttiled(c
->next
)))
2044 main(int argc
, char *argv
[]) {
2045 if(argc
== 2 && !strcmp("-v", argv
[1]))
2046 die("dwm-"VERSION
", © 2006-2010 dwm engineers, see LICENSE for details\n");
2048 die("usage: dwm [-v]\n");
2049 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2050 fputs("warning: no locale support\n", stderr
);
2051 if(!(dpy
= XOpenDisplay(NULL
)))
2052 die("dwm: cannot open display\n");