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
, NetLast
}; /* EWMH atoms */
61 enum { WMProtocols
, WMDelete
, WMState
, WMLast
}; /* default atoms */
62 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
63 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
76 void (*func
)(const Arg
*arg
);
80 typedef struct Monitor Monitor
;
81 typedef struct Client Client
;
86 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
89 Bool isfixed
, isfloating
, isurgent
;
98 unsigned long norm
[ColLast
];
99 unsigned long sel
[ColLast
];
109 } DC
; /* draw context */
114 void (*func
)(const Arg
*);
120 void (*arrange
)(Monitor
*);
124 const char *ltsymbol
;
127 int by
; /* bar geometry */
128 int mx
, my
, mw
, mh
; /* screen size */
129 int wx
, wy
, ww
, wh
; /* window area */
130 unsigned int seltags
;
132 unsigned int tagset
[2];
145 const char *instance
;
152 /* function declarations */
153 static void applyrules(Client
*c
);
154 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
155 static void arrange(void);
156 static void attach(Client
*c
);
157 static void attachstack(Client
*c
);
158 static void buttonpress(XEvent
*e
);
159 static void checkotherwm(void);
160 static void cleanup(void);
161 static void cleanupmons(void);
162 static void clearurgent(Client
*c
);
163 static void configure(Client
*c
);
164 static void configurenotify(XEvent
*e
);
165 static void configurerequest(XEvent
*e
);
166 static void destroynotify(XEvent
*e
);
167 static void detach(Client
*c
);
168 static void detachstack(Client
*c
);
169 static void die(const char *errstr
, ...);
170 static Monitor
*dirtomon(int dir
);
171 static void drawbar(Monitor
*m
);
172 static void drawbars(void);
173 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
174 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
175 static void enternotify(XEvent
*e
);
176 static void expose(XEvent
*e
);
177 static void focus(Client
*c
);
178 static void focusin(XEvent
*e
);
179 static void focusmon(const Arg
*arg
);
180 static void focusstack(const Arg
*arg
);
181 static unsigned long getcolor(const char *colstr
);
182 static Bool
getrootptr(int *x
, int *y
);
183 static long getstate(Window w
);
184 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
185 static void grabbuttons(Client
*c
, Bool focused
);
186 static void grabkeys(void);
187 static void initfont(const char *fontstr
);
188 static Bool
isprotodel(Client
*c
);
189 static void keypress(XEvent
*e
);
190 static void killclient(const Arg
*arg
);
191 static void manage(Window w
, XWindowAttributes
*wa
);
192 static void mappingnotify(XEvent
*e
);
193 static void maprequest(XEvent
*e
);
194 static void monocle(Monitor
*m
);
195 static void movemouse(const Arg
*arg
);
196 static Client
*nexttiled(Client
*c
);
197 static Monitor
*ptrtomon(int x
, int y
);
198 static void propertynotify(XEvent
*e
);
199 static void quit(const Arg
*arg
);
200 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
201 static void resizemouse(const Arg
*arg
);
202 static void restack(Monitor
*m
);
203 static void run(void);
204 static void scan(void);
205 static void sendmon(Client
*c
, Monitor
*m
);
206 static void setclientstate(Client
*c
, long state
);
207 static void setlayout(const Arg
*arg
);
208 static void setmfact(const Arg
*arg
);
209 static void setup(void);
210 static void showhide(Client
*c
);
211 static void sigchld(int unused
);
212 static void spawn(const Arg
*arg
);
213 static void tag(const Arg
*arg
);
214 static void tagmon(const Arg
*arg
);
215 static int textnw(const char *text
, unsigned int len
);
216 static void tile(Monitor
*);
217 static void togglebar(const Arg
*arg
);
218 static void togglefloating(const Arg
*arg
);
219 static void toggletag(const Arg
*arg
);
220 static void toggleview(const Arg
*arg
);
221 static void unfocus(Client
*c
);
222 static void unmanage(Client
*c
, Bool destroyed
);
223 static void unmapnotify(XEvent
*e
);
224 static void updategeom(void);
225 static void updatebarpos(Monitor
*m
);
226 static void updatebars(void);
227 static void updatenumlockmask(void);
228 static void updatesizehints(Client
*c
);
229 static void updatestatus(void);
230 static void updatetitle(Client
*c
);
231 static void updatewmhints(Client
*c
);
232 static void view(const Arg
*arg
);
233 static Client
*wintoclient(Window w
);
234 static Monitor
*wintomon(Window w
);
235 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
236 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
237 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
238 static void zoom(const Arg
*arg
);
241 static const char broken
[] = "broken";
242 static char stext
[256];
244 static int sw
, sh
; /* X display screen geometry width, height */
245 static int bh
, blw
= 0; /* bar geometry */
246 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
247 static unsigned int numlockmask
= 0;
248 static void (*handler
[LASTEvent
]) (XEvent
*) = {
249 [ButtonPress
] = buttonpress
,
250 [ConfigureRequest
] = configurerequest
,
251 [ConfigureNotify
] = configurenotify
,
252 [DestroyNotify
] = destroynotify
,
253 [EnterNotify
] = enternotify
,
256 [KeyPress
] = keypress
,
257 [MappingNotify
] = mappingnotify
,
258 [MapRequest
] = maprequest
,
259 [PropertyNotify
] = propertynotify
,
260 [UnmapNotify
] = unmapnotify
262 static Atom wmatom
[WMLast
], netatom
[NetLast
];
264 static Bool running
= True
;
265 static Cursor cursor
[CurLast
];
268 static Monitor
*mons
= NULL
, *selmon
= NULL
;
271 /* configuration, allows nested code to access above variables */
274 /* compile-time check if all tags fit into an unsigned int bit array. */
275 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
277 /* function implementations */
279 applyrules(Client
*c
) {
280 const char *class, *instance
;
284 XClassHint ch
= { 0 };
287 c
->isfloating
= c
->tags
= 0;
288 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
289 class = ch
.res_class
? ch
.res_class
: broken
;
290 instance
= ch
.res_name
? ch
.res_name
: broken
;
291 for(i
= 0; i
< LENGTH(rules
); i
++) {
293 if((!r
->title
|| strstr(c
->name
, r
->title
))
294 && (!r
->class || strstr(class, r
->class))
295 && (!r
->instance
|| strstr(instance
, r
->instance
)))
297 c
->isfloating
= r
->isfloating
;
299 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
309 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
313 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
317 /* set minimum possible */
325 if(*x
+ *w
+ 2 * c
->bw
< 0)
327 if(*y
+ *h
+ 2 * c
->bw
< 0)
331 if(*x
> m
->mx
+ m
->mw
)
332 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
333 if(*y
> m
->my
+ m
->mh
)
334 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
335 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
337 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
344 if(resizehints
|| c
->isfloating
) {
345 /* see last two sentences in ICCCM 4.1.2.3 */
346 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
347 if(!baseismin
) { /* temporarily remove base dimensions */
351 /* adjust for aspect limits */
352 if(c
->mina
> 0 && c
->maxa
> 0) {
353 if(c
->maxa
< (float)*w
/ *h
)
354 *w
= *h
* c
->maxa
+ 0.5;
355 else if(c
->mina
< (float)*h
/ *w
)
356 *h
= *w
* c
->mina
+ 0.5;
358 if(baseismin
) { /* increment calculation requires this */
362 /* adjust for increment value */
367 /* restore base dimensions */
370 *w
= MAX(*w
, c
->minw
);
371 *h
= MAX(*h
, c
->minh
);
373 *w
= MIN(*w
, c
->maxw
);
375 *h
= MIN(*h
, c
->maxh
);
377 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
384 for(m
= mons
; m
; m
= m
->next
)
387 for(m
= mons
; m
; m
= m
->next
) {
388 m
->ltsymbol
= m
->lt
[m
->sellt
]->symbol
;
389 if(m
->lt
[m
->sellt
]->arrange
)
390 m
->lt
[m
->sellt
]->arrange(m
);
397 c
->next
= c
->mon
->clients
;
402 attachstack(Client
*c
) {
403 c
->snext
= c
->mon
->stack
;
408 buttonpress(XEvent
*e
) {
409 unsigned int i
, x
, click
;
413 XButtonPressedEvent
*ev
= &e
->xbutton
;
416 /* focus monitor if necessary */
417 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
418 unfocus(selmon
->sel
);
422 if(ev
->window
== selmon
->barwin
) {
426 } while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
427 if(i
< LENGTH(tags
)) {
431 else if(ev
->x
< x
+ blw
)
433 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
434 click
= ClkStatusText
;
438 else if((c
= wintoclient(ev
->window
))) {
440 click
= ClkClientWin
;
442 for(i
= 0; i
< LENGTH(buttons
); i
++)
443 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
444 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
445 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
451 xerrorxlib
= XSetErrorHandler(xerrorstart
);
452 /* this causes an error if some other window manager is running */
453 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
456 die("dwm: another window manager is already running\n");
457 XSetErrorHandler(xerror
);
464 Layout foo
= { "", NULL
};
468 selmon
->lt
[selmon
->sellt
] = &foo
;
469 for(m
= mons
; m
; m
= m
->next
)
471 unmanage(m
->stack
, False
);
473 XFreeFontSet(dpy
, dc
.font
.set
);
475 XFreeFont(dpy
, dc
.font
.xfont
);
476 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
477 XFreePixmap(dpy
, dc
.drawable
);
479 XFreeCursor(dpy
, cursor
[CurNormal
]);
480 XFreeCursor(dpy
, cursor
[CurResize
]);
481 XFreeCursor(dpy
, cursor
[CurMove
]);
484 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
493 XUnmapWindow(dpy
, mons
->barwin
);
494 XDestroyWindow(dpy
, mons
->barwin
);
501 clearurgent(Client
*c
) {
505 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
507 wmh
->flags
&= ~XUrgencyHint
;
508 XSetWMHints(dpy
, c
->win
, wmh
);
513 configure(Client
*c
) {
516 ce
.type
= ConfigureNotify
;
524 ce
.border_width
= c
->bw
;
526 ce
.override_redirect
= False
;
527 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
531 configurenotify(XEvent
*e
) {
533 XConfigureEvent
*ev
= &e
->xconfigure
;
535 if(ev
->window
== root
) {
540 XFreePixmap(dpy
, dc
.drawable
);
541 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
543 for(m
= mons
; m
; m
= m
->next
)
544 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
550 configurerequest(XEvent
*e
) {
553 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
556 if((c
= wintoclient(ev
->window
))) {
557 if(ev
->value_mask
& CWBorderWidth
)
558 c
->bw
= ev
->border_width
;
559 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
561 if(ev
->value_mask
& CWX
)
562 c
->x
= m
->mx
+ ev
->x
;
563 if(ev
->value_mask
& CWY
)
564 c
->y
= m
->my
+ ev
->y
;
565 if(ev
->value_mask
& CWWidth
)
567 if(ev
->value_mask
& CWHeight
)
569 if((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
570 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
571 if((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
572 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
573 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
576 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
584 wc
.width
= ev
->width
;
585 wc
.height
= ev
->height
;
586 wc
.border_width
= ev
->border_width
;
587 wc
.sibling
= ev
->above
;
588 wc
.stack_mode
= ev
->detail
;
589 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
595 destroynotify(XEvent
*e
) {
597 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
599 if((c
= wintoclient(ev
->window
)))
607 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
612 detachstack(Client
*c
) {
615 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
618 if(c
== c
->mon
->sel
) {
619 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
625 die(const char *errstr
, ...) {
628 va_start(ap
, errstr
);
629 vfprintf(stderr
, errstr
, ap
);
639 if(!(m
= selmon
->next
))
644 for(m
= mons
; m
->next
; m
= m
->next
);
646 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
652 drawbar(Monitor
*m
) {
654 unsigned int i
, occ
= 0, urg
= 0;
658 for(c
= m
->clients
; c
; c
= c
->next
) {
664 for(i
= 0; i
< LENGTH(tags
); i
++) {
665 dc
.w
= TEXTW(tags
[i
]);
666 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
667 drawtext(tags
[i
], col
, urg
& 1 << i
);
668 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
669 occ
& 1 << i
, urg
& 1 << i
, col
);
672 dc
.w
= blw
= TEXTW(m
->ltsymbol
);
673 drawtext(m
->ltsymbol
, dc
.norm
, False
);
676 if(m
== selmon
) { /* status is only drawn on selected monitor */
683 drawtext(stext
, dc
.norm
, False
);
687 if((dc
.w
= dc
.x
- x
) > bh
) {
690 col
= m
== selmon
? dc
.sel
: dc
.norm
;
691 drawtext(m
->sel
->name
, col
, False
);
692 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
695 drawtext(NULL
, dc
.norm
, False
);
697 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
705 for(m
= mons
; m
; m
= m
->next
)
710 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
713 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
715 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
716 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
717 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
721 r
.width
= r
.height
= x
+ 1;
722 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
725 r
.width
= r
.height
= x
;
726 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
731 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
733 int i
, x
, y
, h
, len
, olen
;
734 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
736 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
737 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
741 h
= dc
.font
.ascent
+ dc
.font
.descent
;
742 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
744 /* shorten text if necessary */
745 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
748 memcpy(buf
, text
, len
);
750 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
751 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
753 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
755 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
759 enternotify(XEvent
*e
) {
762 XCrossingEvent
*ev
= &e
->xcrossing
;
764 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
766 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
767 unfocus(selmon
->sel
);
770 if((c
= wintoclient(ev
->window
)))
779 XExposeEvent
*ev
= &e
->xexpose
;
781 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
787 if(!c
|| !ISVISIBLE(c
))
788 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
790 unfocus(selmon
->sel
);
798 grabbuttons(c
, True
);
799 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
800 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
803 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
809 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
810 XFocusChangeEvent
*ev
= &e
->xfocus
;
812 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
813 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
817 focusmon(const Arg
*arg
) {
822 m
= dirtomon(arg
->i
);
823 unfocus(selmon
->sel
);
829 focusstack(const Arg
*arg
) {
830 Client
*c
= NULL
, *i
;
835 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
837 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
840 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
844 for(; i
; i
= i
->next
)
855 getcolor(const char *colstr
) {
856 Colormap cmap
= DefaultColormap(dpy
, screen
);
859 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
860 die("error, cannot allocate color '%s'\n", colstr
);
865 getrootptr(int *x
, int *y
) {
870 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
877 unsigned char *p
= NULL
;
878 unsigned long n
, extra
;
881 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
882 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
883 if(status
!= Success
)
892 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
897 if(!text
|| size
== 0)
900 XGetTextProperty(dpy
, w
, &name
, atom
);
903 if(name
.encoding
== XA_STRING
)
904 strncpy(text
, (char *)name
.value
, size
- 1);
906 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
907 strncpy(text
, *list
, size
- 1);
908 XFreeStringList(list
);
911 text
[size
- 1] = '\0';
917 grabbuttons(Client
*c
, Bool focused
) {
921 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
922 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
924 for(i
= 0; i
< LENGTH(buttons
); i
++)
925 if(buttons
[i
].click
== ClkClientWin
)
926 for(j
= 0; j
< LENGTH(modifiers
); j
++)
927 XGrabButton(dpy
, buttons
[i
].button
,
928 buttons
[i
].mask
| modifiers
[j
],
929 c
->win
, False
, BUTTONMASK
,
930 GrabModeAsync
, GrabModeSync
, None
, None
);
933 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
934 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
943 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
946 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
947 for(i
= 0; i
< LENGTH(keys
); i
++) {
948 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
949 for(j
= 0; j
< LENGTH(modifiers
); j
++)
950 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
951 True
, GrabModeAsync
, GrabModeAsync
);
957 initfont(const char *fontstr
) {
958 char *def
, **missing
;
962 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
965 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
966 XFreeStringList(missing
);
969 XFontSetExtents
*font_extents
;
970 XFontStruct
**xfonts
;
973 dc
.font
.ascent
= dc
.font
.descent
= 0;
974 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
975 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
976 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
977 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
978 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
983 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
984 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
985 die("error, cannot load font: '%s'\n", fontstr
);
986 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
987 dc
.font
.descent
= dc
.font
.xfont
->descent
;
989 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
993 isprotodel(Client
*c
) {
998 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
999 for(i
= 0; !ret
&& i
< n
; i
++)
1000 if(protocols
[i
] == wmatom
[WMDelete
])
1008 keypress(XEvent
*e
) {
1014 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1015 for(i
= 0; i
< LENGTH(keys
); i
++)
1016 if(keysym
== keys
[i
].keysym
1017 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1019 keys
[i
].func(&(keys
[i
].arg
));
1023 killclient(const Arg
*arg
) {
1028 if(isprotodel(selmon
->sel
)) {
1029 ev
.type
= ClientMessage
;
1030 ev
.xclient
.window
= selmon
->sel
->win
;
1031 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1032 ev
.xclient
.format
= 32;
1033 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1034 ev
.xclient
.data
.l
[1] = CurrentTime
;
1035 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1039 XSetErrorHandler(xerrordummy
);
1040 XSetCloseDownMode(dpy
, DestroyAll
);
1041 XKillClient(dpy
, selmon
->sel
->win
);
1043 XSetErrorHandler(xerror
);
1049 manage(Window w
, XWindowAttributes
*wa
) {
1051 Client
*c
, *t
= NULL
;
1052 Window trans
= None
;
1055 if(!(c
= malloc(sizeof(Client
))))
1056 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1060 if(XGetTransientForHint(dpy
, w
, &trans
))
1061 t
= wintoclient(trans
);
1071 c
->x
= wa
->x
+ c
->mon
->wx
;
1072 c
->y
= wa
->y
+ c
->mon
->wy
;
1075 c
->oldbw
= wa
->border_width
;
1076 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1082 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1083 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1084 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1085 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1086 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1087 /* only fix client y-offset, if the client center might cover the bar */
1088 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1089 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1092 wc
.border_width
= c
->bw
;
1093 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1094 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1095 configure(c
); /* propagates border_width, if size doesn't change */
1097 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1098 grabbuttons(c
, False
);
1100 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1102 XRaiseWindow(dpy
, c
->win
);
1105 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1106 XMapWindow(dpy
, c
->win
);
1107 setclientstate(c
, NormalState
);
1112 mappingnotify(XEvent
*e
) {
1113 XMappingEvent
*ev
= &e
->xmapping
;
1115 XRefreshKeyboardMapping(ev
);
1116 if(ev
->request
== MappingKeyboard
)
1121 maprequest(XEvent
*e
) {
1122 static XWindowAttributes wa
;
1123 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1125 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1127 if(wa
.override_redirect
)
1129 if(!wintoclient(ev
->window
))
1130 manage(ev
->window
, &wa
);
1134 monocle(Monitor
*m
) {
1135 static char ntext
[8];
1139 for(c
= m
->clients
; c
; c
= c
->next
)
1142 if(n
> 0) { /* override layout symbol */
1143 snprintf(ntext
, sizeof ntext
, "[%d]", n
);
1144 m
->ltsymbol
= ntext
;
1146 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1147 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1151 movemouse(const Arg
*arg
) {
1152 int x
, y
, ocx
, ocy
, nx
, ny
;
1157 if(!(c
= selmon
->sel
))
1162 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1163 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1165 if(!getrootptr(&x
, &y
))
1168 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1170 case ConfigureRequest
:
1173 handler
[ev
.type
](&ev
);
1176 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1177 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1178 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1179 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1180 if(abs(selmon
->wx
- nx
) < snap
)
1182 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1183 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1184 if(abs(selmon
->wy
- ny
) < snap
)
1186 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1187 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1188 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1189 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1190 togglefloating(NULL
);
1192 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1193 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1196 } while(ev
.type
!= ButtonRelease
);
1197 XUngrabPointer(dpy
, CurrentTime
);
1198 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1206 nexttiled(Client
*c
) {
1207 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1212 ptrtomon(int x
, int y
) {
1215 for(m
= mons
; m
; m
= m
->next
)
1216 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1222 propertynotify(XEvent
*e
) {
1225 XPropertyEvent
*ev
= &e
->xproperty
;
1227 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1229 else if(ev
->state
== PropertyDelete
)
1230 return; /* ignore */
1231 else if((c
= wintoclient(ev
->window
))) {
1234 case XA_WM_TRANSIENT_FOR
:
1235 XGetTransientForHint(dpy
, c
->win
, &trans
);
1236 if(!c
->isfloating
&& (c
->isfloating
= (wintoclient(trans
) != NULL
)))
1239 case XA_WM_NORMAL_HINTS
:
1247 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1249 if(c
== c
->mon
->sel
)
1256 quit(const Arg
*arg
) {
1261 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1264 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
)) {
1267 c
->w
= wc
.width
= w
;
1268 c
->h
= wc
.height
= h
;
1269 wc
.border_width
= c
->bw
;
1270 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1277 resizemouse(const Arg
*arg
) {
1284 if(!(c
= selmon
->sel
))
1289 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1290 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1292 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1294 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1296 case ConfigureRequest
:
1299 handler
[ev
.type
](&ev
);
1302 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1303 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1304 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1305 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
)
1307 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1308 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1309 togglefloating(NULL
);
1311 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1312 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1315 } while(ev
.type
!= ButtonRelease
);
1316 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1317 XUngrabPointer(dpy
, CurrentTime
);
1318 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1319 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1327 restack(Monitor
*m
) {
1335 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1336 XRaiseWindow(dpy
, m
->sel
->win
);
1337 if(m
->lt
[m
->sellt
]->arrange
) {
1338 wc
.stack_mode
= Below
;
1339 wc
.sibling
= m
->barwin
;
1340 for(c
= m
->stack
; c
; c
= c
->snext
)
1341 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1342 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1343 wc
.sibling
= c
->win
;
1347 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1354 /* main event loop */
1356 while(running
&& !XNextEvent(dpy
, &ev
))
1357 if(handler
[ev
.type
])
1358 handler
[ev
.type
](&ev
); /* call handler */
1363 unsigned int i
, num
;
1364 Window d1
, d2
, *wins
= NULL
;
1365 XWindowAttributes wa
;
1367 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1368 for(i
= 0; i
< num
; i
++) {
1369 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1370 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1372 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1373 manage(wins
[i
], &wa
);
1375 for(i
= 0; i
< num
; i
++) { /* now the transients */
1376 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1378 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1379 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1380 manage(wins
[i
], &wa
);
1388 sendmon(Client
*c
, Monitor
*m
) {
1395 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1403 setclientstate(Client
*c
, long state
) {
1404 long data
[] = { state
, None
};
1406 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1407 PropModeReplace
, (unsigned char *)data
, 2);
1411 setlayout(const Arg
*arg
) {
1412 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1415 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1422 /* arg > 1.0 will set mfact absolutly */
1424 setmfact(const Arg
*arg
) {
1427 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1429 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1430 if(f
< 0.1 || f
> 0.9)
1438 XSetWindowAttributes wa
;
1440 /* clean up any zombies immediately */
1444 screen
= DefaultScreen(dpy
);
1445 root
= RootWindow(dpy
, screen
);
1447 sw
= DisplayWidth(dpy
, screen
);
1448 sh
= DisplayHeight(dpy
, screen
);
1449 bh
= dc
.h
= dc
.font
.height
+ 2;
1452 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1453 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1454 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1455 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1456 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1458 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1459 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1460 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1461 /* init appearance */
1462 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1463 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1464 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1465 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1466 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1467 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1468 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1469 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1470 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1472 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1476 /* EWMH support per view */
1477 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1478 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1479 /* select for events */
1480 wa
.cursor
= cursor
[CurNormal
];
1481 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1482 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1483 |PropertyChangeMask
;
1484 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1485 XSelectInput(dpy
, root
, wa
.event_mask
);
1490 showhide(Client
*c
) {
1493 if(ISVISIBLE(c
)) { /* show clients top down */
1494 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1495 if(!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1496 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1499 else { /* hide clients bottom up */
1501 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1507 sigchld(int unused
) {
1508 if(signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1509 die("Can't install SIGCHLD handler");
1510 while(0 < waitpid(-1, NULL
, WNOHANG
));
1514 spawn(const Arg
*arg
) {
1517 close(ConnectionNumber(dpy
));
1519 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1520 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1527 tag(const Arg
*arg
) {
1528 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1529 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1535 tagmon(const Arg
*arg
) {
1536 if(!selmon
->sel
|| !mons
->next
)
1538 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1542 textnw(const char *text
, unsigned int len
) {
1546 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1549 return XTextWidth(dc
.font
.xfont
, text
, len
);
1558 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1562 c
= nexttiled(m
->clients
);
1563 mw
= m
->mfact
* m
->ww
;
1564 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1568 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1570 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1574 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1575 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1576 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1578 y
= c
->y
+ HEIGHT(c
);
1583 togglebar(const Arg
*arg
) {
1584 selmon
->showbar
= !selmon
->showbar
;
1585 updatebarpos(selmon
);
1586 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1591 togglefloating(const Arg
*arg
) {
1594 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1595 if(selmon
->sel
->isfloating
)
1596 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1597 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1602 toggletag(const Arg
*arg
) {
1603 unsigned int newtags
;
1607 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1609 selmon
->sel
->tags
= newtags
;
1615 toggleview(const Arg
*arg
) {
1616 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1619 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1625 unfocus(Client
*c
) {
1628 grabbuttons(c
, False
);
1629 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1630 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1634 unmanage(Client
*c
, Bool destroyed
) {
1637 /* The server grab construct avoids race conditions. */
1641 wc
.border_width
= c
->oldbw
;
1643 XSetErrorHandler(xerrordummy
);
1644 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1645 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1646 setclientstate(c
, WithdrawnState
);
1648 XSetErrorHandler(xerror
);
1657 unmapnotify(XEvent
*e
) {
1659 XUnmapEvent
*ev
= &e
->xunmap
;
1661 if((c
= wintoclient(ev
->window
)))
1668 XSetWindowAttributes wa
;
1670 wa
.override_redirect
= True
;
1671 wa
.background_pixmap
= ParentRelative
;
1672 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1673 for(m
= mons
; m
; m
= m
->next
) {
1674 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1675 CopyFromParent
, DefaultVisual(dpy
, screen
),
1676 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1677 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1678 XMapRaised(dpy
, m
->barwin
);
1683 updatebarpos(Monitor
*m
) {
1688 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1689 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1699 Monitor
*newmons
= NULL
, *m
= NULL
, *tm
;
1703 XineramaScreenInfo
*info
= NULL
;
1705 if(XineramaIsActive(dpy
))
1706 info
= XineramaQueryScreens(dpy
, &n
);
1707 for(i
= 1, nn
= n
; i
< n
; i
++)
1708 if(info
[i
- 1].x_org
== info
[i
].x_org
&& info
[i
- 1].y_org
== info
[i
].y_org
1709 && info
[i
- 1].width
== info
[i
].width
&& info
[i
- 1].height
== info
[i
].height
)
1711 n
= nn
; /* we only consider unique geometries as separate screens */
1712 #endif /* XINERAMA */
1713 /* allocate monitor(s) for the new geometry setup */
1714 for(i
= 0; i
< n
; i
++) {
1715 if(!(m
= (Monitor
*)malloc(sizeof(Monitor
))))
1716 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
1720 /* initialise monitor(s) */
1722 if(XineramaIsActive(dpy
)) {
1723 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1724 m
->num
= info
[i
].screen_number
;
1725 m
->mx
= m
->wx
= info
[i
].x_org
;
1726 m
->my
= m
->wy
= info
[i
].y_org
;
1727 m
->mw
= m
->ww
= info
[i
].width
;
1728 m
->mh
= m
->wh
= info
[i
].height
;
1733 #endif /* XINERAMA */
1734 /* default monitor setup */
1742 /* bar geometry setup */
1743 for(m
= newmons
; m
; m
= m
->next
) {
1744 m
->sel
= m
->stack
= m
->clients
= NULL
;
1747 m
->tagset
[0] = m
->tagset
[1] = 1;
1749 m
->showbar
= showbar
;
1751 m
->lt
[0] = &layouts
[0];
1752 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1753 m
->ltsymbol
= layouts
[0].symbol
;
1756 /* reassign left over clients of disappeared monitors */
1757 for(tm
= mons
; tm
; tm
= tm
->next
)
1758 while(tm
->clients
) {
1760 tm
->clients
= c
->next
;
1766 /* select focused monitor */
1768 selmon
= mons
= newmons
;
1769 selmon
= wintomon(root
);
1773 updatenumlockmask(void) {
1775 XModifierKeymap
*modmap
;
1778 modmap
= XGetModifierMapping(dpy
);
1779 for(i
= 0; i
< 8; i
++)
1780 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1781 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1782 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1783 numlockmask
= (1 << i
);
1784 XFreeModifiermap(modmap
);
1788 updatesizehints(Client
*c
) {
1792 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1793 /* size is uninitialized, ensure that size.flags aren't used */
1795 if(size
.flags
& PBaseSize
) {
1796 c
->basew
= size
.base_width
;
1797 c
->baseh
= size
.base_height
;
1799 else if(size
.flags
& PMinSize
) {
1800 c
->basew
= size
.min_width
;
1801 c
->baseh
= size
.min_height
;
1804 c
->basew
= c
->baseh
= 0;
1805 if(size
.flags
& PResizeInc
) {
1806 c
->incw
= size
.width_inc
;
1807 c
->inch
= size
.height_inc
;
1810 c
->incw
= c
->inch
= 0;
1811 if(size
.flags
& PMaxSize
) {
1812 c
->maxw
= size
.max_width
;
1813 c
->maxh
= size
.max_height
;
1816 c
->maxw
= c
->maxh
= 0;
1817 if(size
.flags
& PMinSize
) {
1818 c
->minw
= size
.min_width
;
1819 c
->minh
= size
.min_height
;
1821 else if(size
.flags
& PBaseSize
) {
1822 c
->minw
= size
.base_width
;
1823 c
->minh
= size
.base_height
;
1826 c
->minw
= c
->minh
= 0;
1827 if(size
.flags
& PAspect
) {
1828 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1829 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1832 c
->maxa
= c
->mina
= 0.0;
1833 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1834 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1838 updatetitle(Client
*c
) {
1839 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1840 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1841 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1842 strcpy(c
->name
, broken
);
1846 updatestatus(void) {
1847 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1848 strcpy(stext
, "dwm-"VERSION
);
1853 updatewmhints(Client
*c
) {
1856 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1857 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1858 wmh
->flags
&= ~XUrgencyHint
;
1859 XSetWMHints(dpy
, c
->win
, wmh
);
1862 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1868 view(const Arg
*arg
) {
1869 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1871 selmon
->seltags
^= 1; /* toggle sel tagset */
1872 if(arg
->ui
& TAGMASK
)
1873 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1878 wintoclient(Window w
) {
1882 for(m
= mons
; m
; m
= m
->next
)
1883 for(c
= m
->clients
; c
; c
= c
->next
)
1890 wintomon(Window w
) {
1895 if(w
== root
&& getrootptr(&x
, &y
))
1896 return ptrtomon(x
, y
);
1897 for(m
= mons
; m
; m
= m
->next
)
1900 if((c
= wintoclient(w
)))
1905 /* There's no way to check accesses to destroyed windows, thus those cases are
1906 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1907 * default error handler, which may call exit. */
1909 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1910 if(ee
->error_code
== BadWindow
1911 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1912 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1913 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1914 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1915 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1916 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1917 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1918 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1920 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1921 ee
->request_code
, ee
->error_code
);
1922 return xerrorxlib(dpy
, ee
); /* may call exit */
1926 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1930 /* Startup Error handler to check if another window manager
1931 * is already running. */
1933 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1939 zoom(const Arg
*arg
) {
1940 Client
*c
= selmon
->sel
;
1942 if(!selmon
->lt
[selmon
->sellt
]->arrange
1943 || selmon
->lt
[selmon
->sellt
]->arrange
== monocle
1944 || (selmon
->sel
&& selmon
->sel
->isfloating
))
1946 if(c
== nexttiled(selmon
->clients
))
1947 if(!c
|| !(c
= nexttiled(c
->next
)))
1956 main(int argc
, char *argv
[]) {
1957 if(argc
== 2 && !strcmp("-v", argv
[1]))
1958 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1960 die("usage: dwm [-v]\n");
1961 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1962 fputs("warning: no locale support\n", stderr
);
1963 if(!(dpy
= XOpenDisplay(NULL
)))
1964 die("dwm: cannot open display\n");