Xinqi Bao's Git
438b9e716e99db22ae2219c8bcb92c9310be6b7d
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
) {
795 XCrossingEvent
*ev
= &e
->xcrossing
;
797 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
799 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
800 unfocus(selmon
->sel
, True
);
803 if((c
= wintoclient(ev
->window
)))
812 XExposeEvent
*ev
= &e
->xexpose
;
814 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
820 if(!c
|| !ISVISIBLE(c
))
821 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
822 /* was if(selmon->sel) */
823 if(selmon
->sel
&& selmon
->sel
!= c
)
824 unfocus(selmon
->sel
, False
);
832 grabbuttons(c
, True
);
833 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
834 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
837 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
843 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
844 XFocusChangeEvent
*ev
= &e
->xfocus
;
846 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
847 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
851 focusmon(const Arg
*arg
) {
856 if((m
= dirtomon(arg
->i
)) == selmon
)
858 unfocus(selmon
->sel
, True
);
864 focusstack(const Arg
*arg
) {
865 Client
*c
= NULL
, *i
;
870 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
872 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
875 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
879 for(; i
; i
= i
->next
)
890 getcolor(const char *colstr
) {
891 Colormap cmap
= DefaultColormap(dpy
, screen
);
894 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
895 die("error, cannot allocate color '%s'\n", colstr
);
900 getrootptr(int *x
, int *y
) {
905 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
912 unsigned char *p
= NULL
;
913 unsigned long n
, extra
;
916 if(XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
917 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
926 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
931 if(!text
|| size
== 0)
934 XGetTextProperty(dpy
, w
, &name
, atom
);
937 if(name
.encoding
== XA_STRING
)
938 strncpy(text
, (char *)name
.value
, size
- 1);
940 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
941 strncpy(text
, *list
, size
- 1);
942 XFreeStringList(list
);
945 text
[size
- 1] = '\0';
951 grabbuttons(Client
*c
, Bool focused
) {
955 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
956 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
958 for(i
= 0; i
< LENGTH(buttons
); i
++)
959 if(buttons
[i
].click
== ClkClientWin
)
960 for(j
= 0; j
< LENGTH(modifiers
); j
++)
961 XGrabButton(dpy
, buttons
[i
].button
,
962 buttons
[i
].mask
| modifiers
[j
],
963 c
->win
, False
, BUTTONMASK
,
964 GrabModeAsync
, GrabModeSync
, None
, None
);
967 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
968 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
977 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
980 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
981 for(i
= 0; i
< LENGTH(keys
); i
++) {
982 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
983 for(j
= 0; j
< LENGTH(modifiers
); j
++)
984 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
985 True
, GrabModeAsync
, GrabModeAsync
);
991 initfont(const char *fontstr
) {
992 char *def
, **missing
;
996 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
999 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
1000 XFreeStringList(missing
);
1003 XFontSetExtents
*font_extents
;
1004 XFontStruct
**xfonts
;
1007 dc
.font
.ascent
= dc
.font
.descent
= 0;
1008 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
1009 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
1010 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
1011 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
1012 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1017 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1018 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1019 die("error, cannot load font: '%s'\n", fontstr
);
1020 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1021 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1023 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1027 isprotodel(Client
*c
) {
1032 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1033 for(i
= 0; !ret
&& i
< n
; i
++)
1034 if(protocols
[i
] == wmatom
[WMDelete
])
1043 isuniquegeom(XineramaScreenInfo
*unique
, size_t len
, XineramaScreenInfo
*info
) {
1046 for(i
= 0; i
< len
; i
++)
1047 if(unique
[i
].x_org
== info
->x_org
&& unique
[i
].y_org
== info
->y_org
1048 && unique
[i
].width
== info
->width
&& unique
[i
].height
== info
->height
)
1052 #endif /* XINERAMA */
1055 keypress(XEvent
*e
) {
1061 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1062 for(i
= 0; i
< LENGTH(keys
); i
++)
1063 if(keysym
== keys
[i
].keysym
1064 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1066 keys
[i
].func(&(keys
[i
].arg
));
1070 killclient(const Arg
*arg
) {
1075 if(isprotodel(selmon
->sel
)) {
1076 ev
.type
= ClientMessage
;
1077 ev
.xclient
.window
= selmon
->sel
->win
;
1078 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1079 ev
.xclient
.format
= 32;
1080 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1081 ev
.xclient
.data
.l
[1] = CurrentTime
;
1082 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1086 XSetErrorHandler(xerrordummy
);
1087 XSetCloseDownMode(dpy
, DestroyAll
);
1088 XKillClient(dpy
, selmon
->sel
->win
);
1090 XSetErrorHandler(xerror
);
1096 manage(Window w
, XWindowAttributes
*wa
) {
1098 Client
*c
, *t
= NULL
;
1099 Window trans
= None
;
1102 if(!(c
= malloc(sizeof(Client
))))
1103 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1107 if(XGetTransientForHint(dpy
, w
, &trans
))
1108 t
= wintoclient(trans
);
1118 c
->x
= c
->oldx
= wa
->x
+ c
->mon
->wx
;
1119 c
->y
= c
->oldy
= wa
->y
+ c
->mon
->wy
;
1120 c
->w
= c
->oldw
= wa
->width
;
1121 c
->h
= c
->oldh
= wa
->height
;
1122 c
->oldbw
= wa
->border_width
;
1123 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1130 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1131 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1132 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1133 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1134 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1135 /* only fix client y-offset, if the client center might cover the bar */
1136 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1137 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1140 wc
.border_width
= c
->bw
;
1141 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1142 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1143 configure(c
); /* propagates border_width, if size doesn't change */
1145 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1146 grabbuttons(c
, False
);
1148 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1150 XRaiseWindow(dpy
, c
->win
);
1153 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1154 XMapWindow(dpy
, c
->win
);
1155 setclientstate(c
, NormalState
);
1160 mappingnotify(XEvent
*e
) {
1161 XMappingEvent
*ev
= &e
->xmapping
;
1163 XRefreshKeyboardMapping(ev
);
1164 if(ev
->request
== MappingKeyboard
)
1169 maprequest(XEvent
*e
) {
1170 static XWindowAttributes wa
;
1171 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1173 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1175 if(wa
.override_redirect
)
1177 if(!wintoclient(ev
->window
))
1178 manage(ev
->window
, &wa
);
1182 monocle(Monitor
*m
) {
1186 for(c
= m
->clients
; c
; c
= c
->next
)
1189 if(n
> 0) /* override layout symbol */
1190 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1191 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1192 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1196 movemouse(const Arg
*arg
) {
1197 int x
, y
, ocx
, ocy
, nx
, ny
;
1202 if(!(c
= selmon
->sel
))
1207 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1208 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1210 if(!getrootptr(&x
, &y
))
1213 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1215 case ConfigureRequest
:
1218 handler
[ev
.type
](&ev
);
1221 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1222 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1223 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1224 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1225 if(abs(selmon
->wx
- nx
) < snap
)
1227 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1228 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1229 if(abs(selmon
->wy
- ny
) < snap
)
1231 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1232 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1233 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1234 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1235 togglefloating(NULL
);
1237 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1238 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1241 } while(ev
.type
!= ButtonRelease
);
1242 XUngrabPointer(dpy
, CurrentTime
);
1243 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1251 nexttiled(Client
*c
) {
1252 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1257 ptrtomon(int x
, int y
) {
1260 for(m
= mons
; m
; m
= m
->next
)
1261 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1267 propertynotify(XEvent
*e
) {
1270 XPropertyEvent
*ev
= &e
->xproperty
;
1272 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1274 else if(ev
->state
== PropertyDelete
)
1275 return; /* ignore */
1276 else if((c
= wintoclient(ev
->window
))) {
1279 case XA_WM_TRANSIENT_FOR
:
1280 XGetTransientForHint(dpy
, c
->win
, &trans
);
1281 if(!c
->isfloating
&& (c
->isfloating
= (wintoclient(trans
) != NULL
)))
1284 case XA_WM_NORMAL_HINTS
:
1292 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1294 if(c
== c
->mon
->sel
)
1301 clientmessage(XEvent
*e
) {
1302 XClientMessageEvent
*cme
= &e
->xclient
;
1305 if((c
= wintoclient(cme
->window
))
1306 && (cme
->message_type
== netatom
[NetWMState
] && cme
->data
.l
[1] == netatom
[NetWMFullscreen
]))
1308 if(cme
->data
.l
[0]) {
1309 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
1310 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1311 c
->oldstate
= c
->isfloating
;
1315 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1316 XRaiseWindow(dpy
, c
->win
);
1319 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
1320 PropModeReplace
, (unsigned char*)0, 0);
1321 c
->isfloating
= c
->oldstate
;
1327 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1334 quit(const Arg
*arg
) {
1339 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1340 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1341 resizeclient(c
, x
, y
, w
, h
);
1345 resizeclient(Client
*c
, int x
, int y
, int w
, int h
) {
1348 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1349 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1350 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1351 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1352 wc
.border_width
= c
->bw
;
1353 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1359 resizemouse(const Arg
*arg
) {
1366 if(!(c
= selmon
->sel
))
1371 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1372 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1374 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1376 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1378 case ConfigureRequest
:
1381 handler
[ev
.type
](&ev
);
1384 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1385 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1386 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1387 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
)
1389 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1390 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1391 togglefloating(NULL
);
1393 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1394 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1397 } while(ev
.type
!= ButtonRelease
);
1398 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1399 XUngrabPointer(dpy
, CurrentTime
);
1400 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1401 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1409 restack(Monitor
*m
) {
1417 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1418 XRaiseWindow(dpy
, m
->sel
->win
);
1419 if(m
->lt
[m
->sellt
]->arrange
) {
1420 wc
.stack_mode
= Below
;
1421 wc
.sibling
= m
->barwin
;
1422 for(c
= m
->stack
; c
; c
= c
->snext
)
1423 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1424 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1425 wc
.sibling
= c
->win
;
1429 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1435 /* main event loop */
1437 while(running
&& !XNextEvent(dpy
, &ev
)) {
1438 if(handler
[ev
.type
])
1439 handler
[ev
.type
](&ev
); /* call handler */
1445 unsigned int i
, num
;
1446 Window d1
, d2
, *wins
= NULL
;
1447 XWindowAttributes wa
;
1449 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1450 for(i
= 0; i
< num
; i
++) {
1451 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1452 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1454 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1455 manage(wins
[i
], &wa
);
1457 for(i
= 0; i
< num
; i
++) { /* now the transients */
1458 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1460 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1461 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1462 manage(wins
[i
], &wa
);
1470 sendmon(Client
*c
, Monitor
*m
) {
1477 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1485 setclientstate(Client
*c
, long state
) {
1486 long data
[] = { state
, None
};
1488 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1489 PropModeReplace
, (unsigned char *)data
, 2);
1493 setlayout(const Arg
*arg
) {
1494 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1497 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1498 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1505 /* arg > 1.0 will set mfact absolutly */
1507 setmfact(const Arg
*arg
) {
1510 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1512 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1513 if(f
< 0.1 || f
> 0.9)
1521 XSetWindowAttributes wa
;
1523 /* clean up any zombies immediately */
1527 screen
= DefaultScreen(dpy
);
1528 root
= RootWindow(dpy
, screen
);
1530 sw
= DisplayWidth(dpy
, screen
);
1531 sh
= DisplayHeight(dpy
, screen
);
1532 bh
= dc
.h
= dc
.font
.height
+ 2;
1535 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1536 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1537 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1538 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1539 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1540 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1541 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1543 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1544 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1545 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1546 /* init appearance */
1547 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1548 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1549 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1550 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1551 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1552 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1553 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1554 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1555 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1557 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1561 /* EWMH support per view */
1562 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1563 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1564 /* select for events */
1565 wa
.cursor
= cursor
[CurNormal
];
1566 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1567 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1568 |PropertyChangeMask
;
1569 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1570 XSelectInput(dpy
, root
, wa
.event_mask
);
1575 showhide(Client
*c
) {
1578 if(ISVISIBLE(c
)) { /* show clients top down */
1579 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1580 if(!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1581 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1584 else { /* hide clients bottom up */
1586 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1592 sigchld(int unused
) {
1593 if(signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1594 die("Can't install SIGCHLD handler");
1595 while(0 < waitpid(-1, NULL
, WNOHANG
));
1599 spawn(const Arg
*arg
) {
1602 close(ConnectionNumber(dpy
));
1604 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1605 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1612 tag(const Arg
*arg
) {
1613 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1614 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1620 tagmon(const Arg
*arg
) {
1621 if(!selmon
->sel
|| !mons
->next
)
1623 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1627 textnw(const char *text
, unsigned int len
) {
1631 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1634 return XTextWidth(dc
.font
.xfont
, text
, len
);
1643 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1647 c
= nexttiled(m
->clients
);
1648 mw
= m
->mfact
* m
->ww
;
1649 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1653 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1655 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1659 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1660 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1661 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1663 y
= c
->y
+ HEIGHT(c
);
1668 togglebar(const Arg
*arg
) {
1669 selmon
->showbar
= !selmon
->showbar
;
1670 updatebarpos(selmon
);
1671 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1676 togglefloating(const Arg
*arg
) {
1679 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1680 if(selmon
->sel
->isfloating
)
1681 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1682 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1687 toggletag(const Arg
*arg
) {
1688 unsigned int newtags
;
1692 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1694 selmon
->sel
->tags
= newtags
;
1700 toggleview(const Arg
*arg
) {
1701 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1704 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1710 unfocus(Client
*c
, Bool setfocus
) {
1713 grabbuttons(c
, False
);
1714 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1716 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1720 unmanage(Client
*c
, Bool destroyed
) {
1721 Monitor
*m
= c
->mon
;
1724 /* The server grab construct avoids race conditions. */
1728 wc
.border_width
= c
->oldbw
;
1730 XSetErrorHandler(xerrordummy
);
1731 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1732 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1733 setclientstate(c
, WithdrawnState
);
1735 XSetErrorHandler(xerror
);
1744 unmapnotify(XEvent
*e
) {
1746 XUnmapEvent
*ev
= &e
->xunmap
;
1748 if((c
= wintoclient(ev
->window
)))
1755 XSetWindowAttributes wa
;
1757 wa
.override_redirect
= True
;
1758 wa
.background_pixmap
= ParentRelative
;
1759 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1760 for(m
= mons
; m
; m
= m
->next
) {
1761 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1762 CopyFromParent
, DefaultVisual(dpy
, screen
),
1763 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1764 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1765 XMapRaised(dpy
, m
->barwin
);
1770 updatebarpos(Monitor
*m
) {
1775 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1776 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1787 if(XineramaIsActive(dpy
)) {
1791 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1792 XineramaScreenInfo
*unique
= NULL
;
1794 for(n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1795 /* only consider unique geometries as separate screens */
1796 if(!(unique
= (XineramaScreenInfo
*)malloc(sizeof(XineramaScreenInfo
) * nn
)))
1797 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo
) * nn
);
1798 for(i
= 0, j
= 0; i
< nn
; i
++)
1799 if(isuniquegeom(unique
, j
, &info
[i
]))
1800 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1804 for(i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1805 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1807 m
->next
= createmon();
1811 for(i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1813 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1814 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1818 m
->mx
= m
->wx
= unique
[i
].x_org
;
1819 m
->my
= m
->wy
= unique
[i
].y_org
;
1820 m
->mw
= m
->ww
= unique
[i
].width
;
1821 m
->mh
= m
->wh
= unique
[i
].height
;
1825 else { /* less monitors available nn < n */
1826 for(i
= nn
; i
< n
; i
++) {
1827 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1831 m
->clients
= c
->next
;
1845 #endif /* XINERAMA */
1846 /* default monitor setup */
1850 if(mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1852 mons
->mw
= mons
->ww
= sw
;
1853 mons
->mh
= mons
->wh
= sh
;
1859 selmon
= wintomon(root
);
1865 updatenumlockmask(void) {
1867 XModifierKeymap
*modmap
;
1870 modmap
= XGetModifierMapping(dpy
);
1871 for(i
= 0; i
< 8; i
++)
1872 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1873 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1874 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1875 numlockmask
= (1 << i
);
1876 XFreeModifiermap(modmap
);
1880 updatesizehints(Client
*c
) {
1884 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1885 /* size is uninitialized, ensure that size.flags aren't used */
1887 if(size
.flags
& PBaseSize
) {
1888 c
->basew
= size
.base_width
;
1889 c
->baseh
= size
.base_height
;
1891 else if(size
.flags
& PMinSize
) {
1892 c
->basew
= size
.min_width
;
1893 c
->baseh
= size
.min_height
;
1896 c
->basew
= c
->baseh
= 0;
1897 if(size
.flags
& PResizeInc
) {
1898 c
->incw
= size
.width_inc
;
1899 c
->inch
= size
.height_inc
;
1902 c
->incw
= c
->inch
= 0;
1903 if(size
.flags
& PMaxSize
) {
1904 c
->maxw
= size
.max_width
;
1905 c
->maxh
= size
.max_height
;
1908 c
->maxw
= c
->maxh
= 0;
1909 if(size
.flags
& PMinSize
) {
1910 c
->minw
= size
.min_width
;
1911 c
->minh
= size
.min_height
;
1913 else if(size
.flags
& PBaseSize
) {
1914 c
->minw
= size
.base_width
;
1915 c
->minh
= size
.base_height
;
1918 c
->minw
= c
->minh
= 0;
1919 if(size
.flags
& PAspect
) {
1920 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1921 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1924 c
->maxa
= c
->mina
= 0.0;
1925 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1926 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1930 updatetitle(Client
*c
) {
1931 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1932 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1933 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1934 strcpy(c
->name
, broken
);
1938 updatestatus(void) {
1939 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1940 strcpy(stext
, "dwm-"VERSION
);
1945 updatewmhints(Client
*c
) {
1948 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1949 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1950 wmh
->flags
&= ~XUrgencyHint
;
1951 XSetWMHints(dpy
, c
->win
, wmh
);
1954 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1960 view(const Arg
*arg
) {
1961 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1963 selmon
->seltags
^= 1; /* toggle sel tagset */
1964 if(arg
->ui
& TAGMASK
)
1965 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1970 wintoclient(Window w
) {
1974 for(m
= mons
; m
; m
= m
->next
)
1975 for(c
= m
->clients
; c
; c
= c
->next
)
1982 wintomon(Window w
) {
1987 if(w
== root
&& getrootptr(&x
, &y
))
1988 return ptrtomon(x
, y
);
1989 for(m
= mons
; m
; m
= m
->next
)
1992 if((c
= wintoclient(w
)))
1997 /* There's no way to check accesses to destroyed windows, thus those cases are
1998 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1999 * default error handler, which may call exit. */
2001 xerror(Display
*dpy
, XErrorEvent
*ee
) {
2002 if(ee
->error_code
== BadWindow
2003 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2004 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2005 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2006 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2007 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2008 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2009 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2010 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2012 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2013 ee
->request_code
, ee
->error_code
);
2014 return xerrorxlib(dpy
, ee
); /* may call exit */
2018 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
2022 /* Startup Error handler to check if another window manager
2023 * is already running. */
2025 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
2031 zoom(const Arg
*arg
) {
2032 Client
*c
= selmon
->sel
;
2034 if(!selmon
->lt
[selmon
->sellt
]->arrange
2035 || selmon
->lt
[selmon
->sellt
]->arrange
== monocle
2036 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2038 if(c
== nexttiled(selmon
->clients
))
2039 if(!c
|| !(c
= nexttiled(c
->next
)))
2048 main(int argc
, char *argv
[]) {
2049 if(argc
== 2 && !strcmp("-v", argv
[1]))
2050 die("dwm-"VERSION
", © 2006-2010 dwm engineers, see LICENSE for details\n");
2052 die("usage: dwm [-v]\n");
2053 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2054 fputs("warning: no locale support\n", stderr
);
2055 if(!(dpy
= XOpenDisplay(NULL
)))
2056 die("dwm: cannot open display\n");