Xinqi Bao's Git
b089fe9e79bd700b141428649f00f5935fb6e588
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 ((int)((1LL << 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
*);
126 int by
, btx
; /* bar geometry */
127 int mx
, my
, mw
, mh
; /* screen size */
128 int wx
, wy
, ww
, wh
; /* window area */
129 unsigned int seltags
;
131 unsigned int tagset
[2];
144 const char *instance
;
150 /* function declarations */
151 static void applyrules(Client
*c
);
152 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
153 static void arrange(void);
154 static void attach(Client
*c
);
155 static void attachstack(Client
*c
);
156 static void buttonpress(XEvent
*e
);
157 static void checkotherwm(void);
158 static void cleanup(void);
159 static void cleanupmons(void);
160 static void clearurgent(Client
*c
);
161 static void configure(Client
*c
);
162 static void configurenotify(XEvent
*e
);
163 static void configurerequest(XEvent
*e
);
164 static void destroynotify(XEvent
*e
);
165 static void detach(Client
*c
);
166 static void detachstack(Client
*c
);
167 static void die(const char *errstr
, ...);
168 static Monitor
*dirtomon(int dir
);
169 static void drawbar(Monitor
*m
);
170 static void drawbars(void);
171 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
172 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
173 static void enternotify(XEvent
*e
);
174 static void expose(XEvent
*e
);
175 static void focus(Client
*c
);
176 static void focusin(XEvent
*e
);
177 static void focusmon(const Arg
*arg
);
178 static void focusstack(const Arg
*arg
);
179 static unsigned long getcolor(const char *colstr
);
180 static Bool
getrootptr(int *x
, int *y
);
181 static long getstate(Window w
);
182 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
183 static void grabbuttons(Client
*c
, Bool focused
);
184 static void grabkeys(void);
185 static void initfont(const char *fontstr
);
186 static Bool
isprotodel(Client
*c
);
187 static void keypress(XEvent
*e
);
188 static void killclient(const Arg
*arg
);
189 static void manage(Window w
, XWindowAttributes
*wa
);
190 static void mappingnotify(XEvent
*e
);
191 static void maprequest(XEvent
*e
);
192 static void monocle(Monitor
*m
);
193 static void movemouse(const Arg
*arg
);
194 static Client
*nexttiled(Client
*c
);
195 static Monitor
*ptrtomon(int x
, int y
);
196 static void propertynotify(XEvent
*e
);
197 static void quit(const Arg
*arg
);
198 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
199 static void resizemouse(const Arg
*arg
);
200 static void restack(Monitor
*m
);
201 static void run(void);
202 static void scan(void);
203 static void sendmon(Client
*c
, Monitor
*m
);
204 static void setclientstate(Client
*c
, long state
);
205 static void setlayout(const Arg
*arg
);
206 static void setmfact(const Arg
*arg
);
207 static void setup(void);
208 static void showhide(Client
*c
);
209 static void sigchld(int signal
);
210 static void spawn(const Arg
*arg
);
211 static void tag(const Arg
*arg
);
212 static void tagmon(const Arg
*arg
);
213 static int textnw(const char *text
, unsigned int len
);
214 static void tile(Monitor
*);
215 static void togglebar(const Arg
*arg
);
216 static void togglefloating(const Arg
*arg
);
217 static void toggletag(const Arg
*arg
);
218 static void toggleview(const Arg
*arg
);
219 static void unfocus(Client
*c
);
220 static void unmanage(Client
*c
);
221 static void unmapnotify(XEvent
*e
);
222 static void updategeom(void);
223 static void updatebarpos(Monitor
*m
);
224 static void updatebars(void);
225 static void updatenumlockmask(void);
226 static void updatesizehints(Client
*c
);
227 static void updatestatus(void);
228 static void updatetitle(Client
*c
);
229 static void updatewmhints(Client
*c
);
230 static void view(const Arg
*arg
);
231 static Client
*wintoclient(Window w
);
232 static Monitor
*wintomon(Window w
);
233 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
234 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
235 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
236 static void zoom(const Arg
*arg
);
239 static char stext
[256];
241 static int sw
, sh
; /* X display screen geometry x, y, width, height */
242 static int bh
, blw
= 0; /* bar geometry */
243 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
244 static unsigned int numlockmask
= 0;
245 static void (*handler
[LASTEvent
]) (XEvent
*) = {
246 [ButtonPress
] = buttonpress
,
247 [ConfigureRequest
] = configurerequest
,
248 [ConfigureNotify
] = configurenotify
,
249 [DestroyNotify
] = destroynotify
,
250 [EnterNotify
] = enternotify
,
253 [KeyPress
] = keypress
,
254 [MappingNotify
] = mappingnotify
,
255 [MapRequest
] = maprequest
,
256 [PropertyNotify
] = propertynotify
,
257 [UnmapNotify
] = unmapnotify
259 static Atom wmatom
[WMLast
], netatom
[NetLast
];
261 static Bool running
= True
;
262 static Cursor cursor
[CurLast
];
265 static Monitor
*mons
= NULL
, *selmon
= NULL
;
268 /* configuration, allows nested code to access above variables */
271 /* compile-time check if all tags fit into an unsigned int bit array. */
272 struct NumTags
{ char limitexceeded
[sizeof(unsigned int) * 8 < LENGTH(tags
) ? -1 : 1]; };
274 /* function implementations */
276 applyrules(Client
*c
) {
279 XClassHint ch
= { 0 };
282 c
->isfloating
= c
->tags
= 0;
283 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
284 for(i
= 0; i
< LENGTH(rules
); i
++) {
286 if((!r
->title
|| strstr(c
->name
, r
->title
))
287 && (!r
->class || (ch
.res_class
&& strstr(ch
.res_class
, r
->class)))
288 && (!r
->instance
|| (ch
.res_name
&& strstr(ch
.res_name
, r
->instance
))))
290 c
->isfloating
= r
->isfloating
;
299 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
303 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
307 /* set minimum possible */
315 if(*x
+ *w
+ 2 * c
->bw
< 0)
317 if(*y
+ *h
+ 2 * c
->bw
< 0)
321 if(*x
> m
->mx
+ m
->mw
)
322 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
323 if(*y
> m
->my
+ m
->mh
)
324 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
325 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
327 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
334 if(resizehints
|| c
->isfloating
) {
335 /* see last two sentences in ICCCM 4.1.2.3 */
336 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
337 if(!baseismin
) { /* temporarily remove base dimensions */
341 /* adjust for aspect limits */
342 if(c
->mina
> 0 && c
->maxa
> 0) {
343 if(c
->maxa
< (float)*w
/ *h
)
345 else if(c
->mina
< (float)*h
/ *w
)
348 if(baseismin
) { /* increment calculation requires this */
352 /* adjust for increment value */
357 /* restore base dimensions */
360 *w
= MAX(*w
, c
->minw
);
361 *h
= MAX(*h
, c
->minh
);
363 *w
= MIN(*w
, c
->maxw
);
365 *h
= MIN(*h
, c
->maxh
);
367 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
374 /* optimise two loops into one, check focus(NULL) */
375 for(m
= mons
; m
; m
= m
->next
)
378 for(m
= mons
; m
; m
= m
->next
) {
379 if(m
->lt
[m
->sellt
]->arrange
)
380 m
->lt
[m
->sellt
]->arrange(m
);
387 c
->next
= c
->mon
->clients
;
392 attachstack(Client
*c
) {
393 c
->snext
= c
->mon
->stack
;
398 buttonpress(XEvent
*e
) {
399 unsigned int i
, x
, click
;
403 XButtonPressedEvent
*ev
= &e
->xbutton
;
406 /* focus monitor if necessary */
407 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
408 unfocus(selmon
->sel
);
412 if(ev
->window
== selmon
->barwin
&& ev
->x
>= selmon
->btx
) {
417 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
418 if(i
< LENGTH(tags
)) {
422 else if(ev
->x
< x
+ blw
)
424 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
425 click
= ClkStatusText
;
429 else if((c
= wintoclient(ev
->window
))) {
431 click
= ClkClientWin
;
433 for(i
= 0; i
< LENGTH(buttons
); i
++)
434 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
435 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
436 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
442 xerrorxlib
= XSetErrorHandler(xerrorstart
);
443 /* this causes an error if some other window manager is running */
444 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
447 die("dwm: another window manager is already running\n");
448 XSetErrorHandler(xerror
);
455 Layout foo
= { "", NULL
};
459 selmon
->lt
[selmon
->sellt
] = &foo
;
460 for(m
= mons
; m
; m
= m
->next
)
464 XFreeFontSet(dpy
, dc
.font
.set
);
466 XFreeFont(dpy
, dc
.font
.xfont
);
467 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
468 XFreePixmap(dpy
, dc
.drawable
);
470 XFreeCursor(dpy
, cursor
[CurNormal
]);
471 XFreeCursor(dpy
, cursor
[CurResize
]);
472 XFreeCursor(dpy
, cursor
[CurMove
]);
475 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
484 XUnmapWindow(dpy
, mons
->barwin
);
485 XDestroyWindow(dpy
, mons
->barwin
);
492 clearurgent(Client
*c
) {
496 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
498 wmh
->flags
&= ~XUrgencyHint
;
499 XSetWMHints(dpy
, c
->win
, wmh
);
504 configure(Client
*c
) {
507 ce
.type
= ConfigureNotify
;
515 ce
.border_width
= c
->bw
;
517 ce
.override_redirect
= False
;
518 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
522 configurenotify(XEvent
*e
) {
524 XConfigureEvent
*ev
= &e
->xconfigure
;
526 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
531 XFreePixmap(dpy
, dc
.drawable
);
532 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
534 for(m
= mons
; m
; m
= m
->next
)
535 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
541 configurerequest(XEvent
*e
) {
544 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
547 if((c
= wintoclient(ev
->window
))) {
548 if(ev
->value_mask
& CWBorderWidth
)
549 c
->bw
= ev
->border_width
;
550 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
552 if(ev
->value_mask
& CWX
)
553 c
->x
= m
->mx
+ ev
->x
;
554 if(ev
->value_mask
& CWY
)
555 c
->y
= m
->my
+ ev
->y
;
556 if(ev
->value_mask
& CWWidth
)
558 if(ev
->value_mask
& CWHeight
)
560 if((c
->x
- m
->mx
+ c
->w
) > m
->mw
&& c
->isfloating
)
561 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
562 if((c
->y
- m
->my
+ c
->h
) > m
->mh
&& c
->isfloating
)
563 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
564 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
567 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
575 wc
.width
= ev
->width
;
576 wc
.height
= ev
->height
;
577 wc
.border_width
= ev
->border_width
;
578 wc
.sibling
= ev
->above
;
579 wc
.stack_mode
= ev
->detail
;
580 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
586 destroynotify(XEvent
*e
) {
588 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
590 if((c
= wintoclient(ev
->window
)))
598 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
603 detachstack(Client
*c
) {
606 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
609 if(c
== c
->mon
->sel
) {
610 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
616 die(const char *errstr
, ...) {
619 va_start(ap
, errstr
);
620 vfprintf(stderr
, errstr
, ap
);
630 if(!(m
= selmon
->next
))
635 for(m
= mons
; m
->next
; m
= m
->next
);
637 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
643 drawbar(Monitor
*m
) {
645 unsigned int i
, occ
= 0, urg
= 0;
649 for(c
= m
->clients
; c
; c
= c
->next
) {
655 if(mons
->next
) { /* more than a single monitor */
656 dc
.w
= TEXTW(monsyms
[m
->screen_number
]);
657 drawtext(monsyms
[m
->screen_number
], selmon
== m
? dc
.sel
: dc
.norm
, False
);
661 for(i
= 0; i
< LENGTH(tags
); i
++) {
662 dc
.w
= TEXTW(tags
[i
]);
663 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
664 drawtext(tags
[i
], col
, urg
& 1 << i
);
665 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
666 occ
& 1 << i
, urg
& 1 << i
, col
);
671 drawtext(m
->lt
[m
->sellt
]->symbol
, 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
909 strncpy(text
, *list
, size
- 1);
910 XFreeStringList(list
);
913 text
[size
- 1] = '\0';
919 grabbuttons(Client
*c
, Bool focused
) {
923 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
924 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
926 for(i
= 0; i
< LENGTH(buttons
); i
++)
927 if(buttons
[i
].click
== ClkClientWin
)
928 for(j
= 0; j
< LENGTH(modifiers
); j
++)
929 XGrabButton(dpy
, buttons
[i
].button
,
930 buttons
[i
].mask
| modifiers
[j
],
931 c
->win
, False
, BUTTONMASK
,
932 GrabModeAsync
, GrabModeSync
, None
, None
);
935 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
936 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
945 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
948 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
949 for(i
= 0; i
< LENGTH(keys
); i
++) {
950 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
951 for(j
= 0; j
< LENGTH(modifiers
); j
++)
952 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
953 True
, GrabModeAsync
, GrabModeAsync
);
959 initfont(const char *fontstr
) {
960 char *def
, **missing
;
964 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
967 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
968 XFreeStringList(missing
);
971 XFontSetExtents
*font_extents
;
972 XFontStruct
**xfonts
;
974 dc
.font
.ascent
= dc
.font
.descent
= 0;
975 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
976 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
977 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
978 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
979 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
984 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
985 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
986 die("error, cannot load font: '%s'\n", fontstr
);
987 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
988 dc
.font
.descent
= dc
.font
.xfont
->descent
;
990 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
994 isprotodel(Client
*c
) {
999 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1000 for(i
= 0; !ret
&& i
< n
; i
++)
1001 if(protocols
[i
] == wmatom
[WMDelete
])
1009 keypress(XEvent
*e
) {
1015 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1016 for(i
= 0; i
< LENGTH(keys
); i
++)
1017 if(keysym
== keys
[i
].keysym
1018 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1020 keys
[i
].func(&(keys
[i
].arg
));
1024 killclient(const Arg
*arg
) {
1029 if(isprotodel(selmon
->sel
)) {
1030 ev
.type
= ClientMessage
;
1031 ev
.xclient
.window
= selmon
->sel
->win
;
1032 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1033 ev
.xclient
.format
= 32;
1034 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1035 ev
.xclient
.data
.l
[1] = CurrentTime
;
1036 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1039 XKillClient(dpy
, selmon
->sel
->win
);
1043 manage(Window w
, XWindowAttributes
*wa
) {
1045 Client
*c
, *t
= NULL
;
1046 Window trans
= None
;
1049 if(!(c
= malloc(sizeof(Client
))))
1050 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1053 if(XGetTransientForHint(dpy
, w
, &trans
))
1054 t
= wintoclient(trans
);
1064 c
->x
= wa
->x
+ c
->mon
->wx
;
1065 c
->y
= wa
->y
+ c
->mon
->wy
;
1068 c
->oldbw
= wa
->border_width
;
1069 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1075 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1076 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1077 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1078 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1079 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1080 /* only fix client y-offset, if the client center might cover the bar */
1081 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1082 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1085 wc
.border_width
= c
->bw
;
1086 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1087 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1088 configure(c
); /* propagates border_width, if size doesn't change */
1090 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1091 grabbuttons(c
, False
);
1094 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1096 XRaiseWindow(dpy
, c
->win
);
1099 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1100 XMapWindow(dpy
, c
->win
);
1101 setclientstate(c
, NormalState
);
1106 mappingnotify(XEvent
*e
) {
1107 XMappingEvent
*ev
= &e
->xmapping
;
1109 XRefreshKeyboardMapping(ev
);
1110 if(ev
->request
== MappingKeyboard
)
1115 maprequest(XEvent
*e
) {
1116 static XWindowAttributes wa
;
1117 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1119 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1121 if(wa
.override_redirect
)
1123 if(!wintoclient(ev
->window
))
1124 manage(ev
->window
, &wa
);
1128 monocle(Monitor
*m
) {
1131 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1132 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1136 movemouse(const Arg
*arg
) {
1137 int x
, y
, ocx
, ocy
, nx
, ny
;
1142 if(!(c
= selmon
->sel
))
1147 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1148 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1150 if(!getrootptr(&x
, &y
))
1153 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1155 case ConfigureRequest
:
1158 handler
[ev
.type
](&ev
);
1161 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1162 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1163 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1164 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1165 if(abs(selmon
->wx
- nx
) < snap
)
1167 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1168 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1169 if(abs(selmon
->wy
- ny
) < snap
)
1171 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1172 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1173 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1174 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1175 togglefloating(NULL
);
1177 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1178 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1182 while(ev
.type
!= ButtonRelease
);
1183 XUngrabPointer(dpy
, CurrentTime
);
1184 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1192 nexttiled(Client
*c
) {
1193 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1198 ptrtomon(int x
, int y
) {
1201 for(m
= mons
; m
; m
= m
->next
)
1202 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1208 propertynotify(XEvent
*e
) {
1211 XPropertyEvent
*ev
= &e
->xproperty
;
1213 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1215 else if(ev
->state
== PropertyDelete
)
1216 return; /* ignore */
1217 else if((c
= wintoclient(ev
->window
))) {
1220 case XA_WM_TRANSIENT_FOR
:
1221 XGetTransientForHint(dpy
, c
->win
, &trans
);
1222 if(!c
->isfloating
&& (c
->isfloating
= (wintoclient(trans
) != NULL
)))
1225 case XA_WM_NORMAL_HINTS
:
1233 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1235 if(c
== selmon
->sel
)
1242 quit(const Arg
*arg
) {
1247 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1250 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
)) {
1253 c
->w
= wc
.width
= w
;
1254 c
->h
= wc
.height
= h
;
1255 wc
.border_width
= c
->bw
;
1256 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1263 resizemouse(const Arg
*arg
) {
1270 if(!(c
= selmon
->sel
))
1275 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1276 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1278 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1280 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1282 case ConfigureRequest
:
1285 handler
[ev
.type
](&ev
);
1288 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1289 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1290 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1291 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
) {
1292 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1293 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1294 togglefloating(NULL
);
1296 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1297 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1301 while(ev
.type
!= ButtonRelease
);
1302 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1303 XUngrabPointer(dpy
, CurrentTime
);
1304 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1305 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1313 restack(Monitor
*m
) {
1321 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1322 XRaiseWindow(dpy
, m
->sel
->win
);
1323 if(m
->lt
[m
->sellt
]->arrange
) {
1324 wc
.stack_mode
= Below
;
1325 wc
.sibling
= m
->barwin
;
1326 for(c
= m
->stack
; c
; c
= c
->snext
)
1327 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1328 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1329 wc
.sibling
= c
->win
;
1332 XLowerWindow(dpy
, m
->barwin
);
1334 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1341 /* main event loop */
1343 while(running
&& !XNextEvent(dpy
, &ev
))
1344 if(handler
[ev
.type
])
1345 (handler
[ev
.type
])(&ev
); /* call handler */
1350 unsigned int i
, num
;
1351 Window d1
, d2
, *wins
= NULL
;
1352 XWindowAttributes wa
;
1354 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1355 for(i
= 0; i
< num
; i
++) {
1356 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1357 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1359 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1360 manage(wins
[i
], &wa
);
1362 for(i
= 0; i
< num
; i
++) { /* now the transients */
1363 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1365 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1366 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1367 manage(wins
[i
], &wa
);
1375 sendmon(Client
*c
, Monitor
*m
) {
1382 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1390 setclientstate(Client
*c
, long state
) {
1391 long data
[] = { state
, None
};
1393 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1394 PropModeReplace
, (unsigned char *)data
, 2);
1398 setlayout(const Arg
*arg
) {
1399 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1402 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1409 /* arg > 1.0 will set mfact absolutly */
1411 setmfact(const Arg
*arg
) {
1414 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1416 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1417 if(f
< 0.1 || f
> 0.9)
1427 XSetWindowAttributes wa
;
1430 screen
= DefaultScreen(dpy
);
1431 root
= RootWindow(dpy
, screen
);
1433 sw
= DisplayWidth(dpy
, screen
);
1434 sh
= DisplayHeight(dpy
, screen
);
1435 bh
= dc
.h
= dc
.font
.height
+ 2;
1438 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1439 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1440 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1441 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1442 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1444 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1445 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1446 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1447 /* init appearance */
1448 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1449 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1450 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1451 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1452 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1453 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1454 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1455 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1456 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1458 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1460 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1461 w
= TEXTW(layouts
[i
].symbol
);
1466 /* EWMH support per view */
1467 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1468 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1469 /* select for events */
1470 wa
.cursor
= cursor
[CurNormal
];
1471 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1472 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1473 |PropertyChangeMask
;
1474 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1475 XSelectInput(dpy
, root
, wa
.event_mask
);
1480 showhide(Client
*c
) {
1483 if(ISVISIBLE(c
)) { /* show clients top down */
1484 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1485 if(!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1486 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1489 else { /* hide clients bottom up */
1491 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1497 sigchld(int signal
) {
1498 while(0 < waitpid(-1, NULL
, WNOHANG
));
1502 spawn(const Arg
*arg
) {
1503 signal(SIGCHLD
, sigchld
);
1506 close(ConnectionNumber(dpy
));
1508 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1509 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1516 tag(const Arg
*arg
) {
1517 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1518 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1524 tagmon(const Arg
*arg
) {
1525 if(!selmon
->sel
|| !mons
->next
)
1527 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1531 textnw(const char *text
, unsigned int len
) {
1535 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1538 return XTextWidth(dc
.font
.xfont
, text
, len
);
1547 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1551 c
= nexttiled(m
->clients
);
1552 mw
= m
->mfact
* m
->ww
;
1553 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1557 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1559 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1563 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1564 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1565 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1567 y
= c
->y
+ HEIGHT(c
);
1572 togglebar(const Arg
*arg
) {
1573 selmon
->showbar
= !selmon
->showbar
;
1574 updatebarpos(selmon
);
1575 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1580 togglefloating(const Arg
*arg
) {
1583 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1584 if(selmon
->sel
->isfloating
)
1585 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1586 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1591 toggletag(const Arg
*arg
) {
1596 mask
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1598 selmon
->sel
->tags
= mask
;
1604 toggleview(const Arg
*arg
) {
1605 unsigned int mask
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1608 selmon
->tagset
[selmon
->seltags
] = mask
;
1614 unfocus(Client
*c
) {
1617 grabbuttons(c
, False
);
1618 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1619 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1623 unmanage(Client
*c
) {
1626 wc
.border_width
= c
->oldbw
;
1627 /* The server grab construct avoids race conditions. */
1629 XSetErrorHandler(xerrordummy
);
1630 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1633 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1634 setclientstate(c
, WithdrawnState
);
1637 XSetErrorHandler(xerror
);
1644 unmapnotify(XEvent
*e
) {
1646 XUnmapEvent
*ev
= &e
->xunmap
;
1648 if((c
= wintoclient(ev
->window
)))
1655 XSetWindowAttributes wa
;
1657 wa
.override_redirect
= True
;
1658 wa
.background_pixmap
= ParentRelative
;
1659 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1660 for(m
= mons
; m
; m
= m
->next
) {
1661 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1662 CopyFromParent
, DefaultVisual(dpy
, screen
),
1663 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1664 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1665 XMapRaised(dpy
, m
->barwin
);
1670 updatebarpos(Monitor
*m
) {
1675 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1676 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1686 Monitor
*newmons
= NULL
, *m
, *tm
;
1689 XineramaScreenInfo
*info
= NULL
;
1691 if(XineramaIsActive(dpy
))
1692 info
= XineramaQueryScreens(dpy
, &n
);
1693 #endif /* XINERAMA */
1694 /* allocate monitor(s) for the new geometry setup */
1695 for(i
= 0; i
< n
; i
++) {
1696 if(!(m
= (Monitor
*)malloc(sizeof(Monitor
))))
1697 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
1701 /* initialise monitor(s) */
1703 if(XineramaIsActive(dpy
)) {
1704 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1705 m
->screen_number
= info
[i
].screen_number
;
1706 m
->mx
= m
->wx
= info
[i
].x_org
;
1707 m
->my
= m
->wy
= info
[i
].y_org
;
1708 m
->mw
= m
->ww
= info
[i
].width
;
1709 m
->mh
= m
->wh
= info
[i
].height
;
1714 #endif /* XINERAMA */
1715 /* default monitor setup */
1717 m
->screen_number
= 0;
1723 /* bar geometry setup */
1724 for(m
= newmons
; m
; m
= m
->next
) {
1725 m
->sel
= m
->stack
= m
->clients
= NULL
;
1728 m
->tagset
[0] = m
->tagset
[1] = 1;
1730 m
->showbar
= SHOWBAR
;
1732 m
->lt
[0] = &layouts
[0];
1733 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1736 /* reassign left over clients of disappeared monitors */
1737 for(tm
= mons
; tm
; tm
= tm
->next
)
1738 while(tm
->clients
) {
1740 tm
->clients
= c
->next
;
1746 /* select focused monitor */
1748 selmon
= mons
= newmons
;
1749 selmon
= wintomon(root
);
1753 updatenumlockmask(void) {
1755 XModifierKeymap
*modmap
;
1758 modmap
= XGetModifierMapping(dpy
);
1759 for(i
= 0; i
< 8; i
++)
1760 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1761 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1762 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1763 numlockmask
= (1 << i
);
1764 XFreeModifiermap(modmap
);
1768 updatesizehints(Client
*c
) {
1772 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1773 /* size is uninitialized, ensure that size.flags aren't used */
1775 if(size
.flags
& PBaseSize
) {
1776 c
->basew
= size
.base_width
;
1777 c
->baseh
= size
.base_height
;
1779 else if(size
.flags
& PMinSize
) {
1780 c
->basew
= size
.min_width
;
1781 c
->baseh
= size
.min_height
;
1784 c
->basew
= c
->baseh
= 0;
1785 if(size
.flags
& PResizeInc
) {
1786 c
->incw
= size
.width_inc
;
1787 c
->inch
= size
.height_inc
;
1790 c
->incw
= c
->inch
= 0;
1791 if(size
.flags
& PMaxSize
) {
1792 c
->maxw
= size
.max_width
;
1793 c
->maxh
= size
.max_height
;
1796 c
->maxw
= c
->maxh
= 0;
1797 if(size
.flags
& PMinSize
) {
1798 c
->minw
= size
.min_width
;
1799 c
->minh
= size
.min_height
;
1801 else if(size
.flags
& PBaseSize
) {
1802 c
->minw
= size
.base_width
;
1803 c
->minh
= size
.base_height
;
1806 c
->minw
= c
->minh
= 0;
1807 if(size
.flags
& PAspect
) {
1808 c
->mina
= (float)size
.min_aspect
.y
/ (float)size
.min_aspect
.x
;
1809 c
->maxa
= (float)size
.max_aspect
.x
/ (float)size
.max_aspect
.y
;
1812 c
->maxa
= c
->mina
= 0.0;
1813 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1814 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1818 updatetitle(Client
*c
) {
1819 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1820 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1824 updatestatus(void) {
1825 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1826 strcpy(stext
, "dwm-"VERSION
);
1831 updatewmhints(Client
*c
) {
1834 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1835 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1836 wmh
->flags
&= ~XUrgencyHint
;
1837 XSetWMHints(dpy
, c
->win
, wmh
);
1840 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1846 view(const Arg
*arg
) {
1847 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1849 selmon
->seltags
^= 1; /* toggle sel tagset */
1850 if(arg
->ui
& TAGMASK
)
1851 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1856 wintoclient(Window w
) {
1860 for(m
= mons
; m
; m
= m
->next
)
1861 for(c
= m
->clients
; c
; c
= c
->next
)
1868 wintomon(Window w
) {
1873 if(w
== root
&& getrootptr(&x
, &y
))
1874 return ptrtomon(x
, y
);
1875 for(m
= mons
; m
; m
= m
->next
)
1878 if((c
= wintoclient(w
)))
1883 /* There's no way to check accesses to destroyed windows, thus those cases are
1884 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1885 * default error handler, which may call exit. */
1887 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1888 if(ee
->error_code
== BadWindow
1889 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1890 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1891 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1892 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1893 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1894 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1895 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1896 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1898 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1899 ee
->request_code
, ee
->error_code
);
1900 return xerrorxlib(dpy
, ee
); /* may call exit */
1904 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1908 /* Startup Error handler to check if another window manager
1909 * is already running. */
1911 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1917 zoom(const Arg
*arg
) {
1918 Client
*c
= selmon
->sel
;
1920 if(!selmon
->lt
[selmon
->sellt
]->arrange
1921 || selmon
->lt
[selmon
->sellt
]->arrange
== monocle
1922 || (selmon
->sel
&& selmon
->sel
->isfloating
))
1924 if(c
== nexttiled(selmon
->clients
))
1925 if(!c
|| !(c
= nexttiled(c
->next
)))
1934 main(int argc
, char *argv
[]) {
1935 if(argc
== 2 && !strcmp("-v", argv
[1]))
1936 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1938 die("usage: dwm [-v]\n");
1939 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1940 fputs("warning: no locale support\n", stderr
);
1941 if(!(dpy
= XOpenDisplay(NULL
)))
1942 die("dwm: cannot open display\n");