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 ((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
; /* 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
;
151 /* function declarations */
152 static void applyrules(Client
*c
);
153 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
154 static void arrange(void);
155 static void attach(Client
*c
);
156 static void attachstack(Client
*c
);
157 static void buttonpress(XEvent
*e
);
158 static void checkotherwm(void);
159 static void cleanup(void);
160 static void cleanupmons(void);
161 static void clearurgent(Client
*c
);
162 static void configure(Client
*c
);
163 static void configurenotify(XEvent
*e
);
164 static void configurerequest(XEvent
*e
);
165 static void destroynotify(XEvent
*e
);
166 static void detach(Client
*c
);
167 static void detachstack(Client
*c
);
168 static void die(const char *errstr
, ...);
169 static Monitor
*dirtomon(int dir
);
170 static void drawbar(Monitor
*m
);
171 static void drawbars(void);
172 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
173 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
174 static void enternotify(XEvent
*e
);
175 static void expose(XEvent
*e
);
176 static void focus(Client
*c
);
177 static void focusin(XEvent
*e
);
178 static void focusmon(const Arg
*arg
);
179 static void focusstack(const Arg
*arg
);
180 static unsigned long getcolor(const char *colstr
);
181 static Bool
getrootptr(int *x
, int *y
);
182 static long getstate(Window w
);
183 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
184 static void grabbuttons(Client
*c
, Bool focused
);
185 static void grabkeys(void);
186 static void initfont(const char *fontstr
);
187 static Bool
isprotodel(Client
*c
);
188 static void keypress(XEvent
*e
);
189 static void killclient(const Arg
*arg
);
190 static void manage(Window w
, XWindowAttributes
*wa
);
191 static void mappingnotify(XEvent
*e
);
192 static void maprequest(XEvent
*e
);
193 static void monocle(Monitor
*m
);
194 static void movemouse(const Arg
*arg
);
195 static Client
*nexttiled(Client
*c
);
196 static Monitor
*ptrtomon(int x
, int y
);
197 static void propertynotify(XEvent
*e
);
198 static void quit(const Arg
*arg
);
199 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
200 static void resizemouse(const Arg
*arg
);
201 static void restack(Monitor
*m
);
202 static void run(void);
203 static void scan(void);
204 static void sendmon(Client
*c
, Monitor
*m
);
205 static void setclientstate(Client
*c
, long state
);
206 static void setlayout(const Arg
*arg
);
207 static void setmfact(const Arg
*arg
);
208 static void setup(void);
209 static void showhide(Client
*c
);
210 static void sigchld(int signal
);
211 static void spawn(const Arg
*arg
);
212 static void tag(const Arg
*arg
);
213 static void tagmon(const Arg
*arg
);
214 static int textnw(const char *text
, unsigned int len
);
215 static void tile(Monitor
*);
216 static void togglebar(const Arg
*arg
);
217 static void togglefloating(const Arg
*arg
);
218 static void toggletag(const Arg
*arg
);
219 static void toggleview(const Arg
*arg
);
220 static void unfocus(Client
*c
);
221 static void unmanage(Client
*c
);
222 static void unmapnotify(XEvent
*e
);
223 static void updategeom(void);
224 static void updatebarpos(Monitor
*m
);
225 static void updatebars(void);
226 static void updatenumlockmask(void);
227 static void updatesizehints(Client
*c
);
228 static void updatestatus(void);
229 static void updatetitle(Client
*c
);
230 static void updatewmhints(Client
*c
);
231 static void view(const Arg
*arg
);
232 static Client
*wintoclient(Window w
);
233 static Monitor
*wintomon(Window w
);
234 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
235 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
236 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
237 static void zoom(const Arg
*arg
);
240 static const char broken
[] = "broken";
241 static char stext
[256], ntext
[8];
243 static int sw
, sh
; /* X display screen geometry width, height */
244 static int bh
, blw
= 0; /* bar geometry */
245 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
246 static unsigned int numlockmask
= 0;
247 static void (*handler
[LASTEvent
]) (XEvent
*) = {
248 [ButtonPress
] = buttonpress
,
249 [ConfigureRequest
] = configurerequest
,
250 [ConfigureNotify
] = configurenotify
,
251 [DestroyNotify
] = destroynotify
,
252 [EnterNotify
] = enternotify
,
255 [KeyPress
] = keypress
,
256 [MappingNotify
] = mappingnotify
,
257 [MapRequest
] = maprequest
,
258 [PropertyNotify
] = propertynotify
,
259 [UnmapNotify
] = unmapnotify
261 static Atom wmatom
[WMLast
], netatom
[NetLast
];
263 static Bool running
= True
;
264 static Cursor cursor
[CurLast
];
267 static Monitor
*mons
= NULL
, *selmon
= NULL
;
270 /* configuration, allows nested code to access above variables */
273 /* compile-time check if all tags fit into an unsigned int bit array. */
274 struct NumTags
{ char limitexceeded
[sizeof(unsigned int) * 8 < LENGTH(tags
) ? -1 : 1]; };
276 /* function implementations */
278 applyrules(Client
*c
) {
279 const char *class, *instance
;
283 XClassHint ch
= { 0 };
286 c
->isfloating
= c
->tags
= 0;
287 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
288 class = ch
.res_class
? ch
.res_class
: broken
;
289 instance
= ch
.res_name
? ch
.res_name
: broken
;
290 for(i
= 0; i
< LENGTH(rules
); i
++) {
292 if((!r
->title
|| strstr(c
->name
, r
->title
))
293 && (!r
->class || strstr(class, r
->class))
294 && (!r
->instance
|| strstr(instance
, r
->instance
)))
296 c
->isfloating
= r
->isfloating
;
298 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
308 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
312 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
316 /* set minimum possible */
324 if(*x
+ *w
+ 2 * c
->bw
< 0)
326 if(*y
+ *h
+ 2 * c
->bw
< 0)
330 if(*x
> m
->mx
+ m
->mw
)
331 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
332 if(*y
> m
->my
+ m
->mh
)
333 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
334 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
336 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
343 if(resizehints
|| c
->isfloating
) {
344 /* see last two sentences in ICCCM 4.1.2.3 */
345 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
346 if(!baseismin
) { /* temporarily remove base dimensions */
350 /* adjust for aspect limits */
351 if(c
->mina
> 0 && c
->maxa
> 0) {
352 if(c
->maxa
< (float)*w
/ *h
)
354 else if(c
->mina
< (float)*h
/ *w
)
357 if(baseismin
) { /* increment calculation requires this */
361 /* adjust for increment value */
366 /* restore base dimensions */
369 *w
= MAX(*w
, c
->minw
);
370 *h
= MAX(*h
, c
->minh
);
372 *w
= MIN(*w
, c
->maxw
);
374 *h
= MIN(*h
, c
->maxh
);
376 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
383 /* optimise two loops into one, check focus(NULL) */
384 for(m
= mons
; m
; m
= m
->next
)
387 for(m
= mons
; m
; m
= m
->next
) {
388 if(m
->lt
[m
->sellt
]->arrange
)
389 m
->lt
[m
->sellt
]->arrange(m
);
396 c
->next
= c
->mon
->clients
;
401 attachstack(Client
*c
) {
402 c
->snext
= c
->mon
->stack
;
407 buttonpress(XEvent
*e
) {
408 unsigned int i
, x
, click
;
412 XButtonPressedEvent
*ev
= &e
->xbutton
;
415 /* focus monitor if necessary */
416 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
417 unfocus(selmon
->sel
);
421 if(ev
->window
== selmon
->barwin
) {
425 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
426 if(i
< LENGTH(tags
)) {
430 else if(ev
->x
< x
+ blw
)
432 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
433 click
= ClkStatusText
;
437 else if((c
= wintoclient(ev
->window
))) {
439 click
= ClkClientWin
;
441 for(i
= 0; i
< LENGTH(buttons
); i
++)
442 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
443 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
444 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
450 xerrorxlib
= XSetErrorHandler(xerrorstart
);
451 /* this causes an error if some other window manager is running */
452 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
455 die("dwm: another window manager is already running\n");
456 XSetErrorHandler(xerror
);
463 Layout foo
= { "", NULL
};
467 selmon
->lt
[selmon
->sellt
] = &foo
;
468 for(m
= mons
; m
; m
= m
->next
)
472 XFreeFontSet(dpy
, dc
.font
.set
);
474 XFreeFont(dpy
, dc
.font
.xfont
);
475 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
476 XFreePixmap(dpy
, dc
.drawable
);
478 XFreeCursor(dpy
, cursor
[CurNormal
]);
479 XFreeCursor(dpy
, cursor
[CurResize
]);
480 XFreeCursor(dpy
, cursor
[CurMove
]);
483 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
492 XUnmapWindow(dpy
, mons
->barwin
);
493 XDestroyWindow(dpy
, mons
->barwin
);
500 clearurgent(Client
*c
) {
504 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
506 wmh
->flags
&= ~XUrgencyHint
;
507 XSetWMHints(dpy
, c
->win
, wmh
);
512 configure(Client
*c
) {
515 ce
.type
= ConfigureNotify
;
523 ce
.border_width
= c
->bw
;
525 ce
.override_redirect
= False
;
526 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
530 configurenotify(XEvent
*e
) {
532 XConfigureEvent
*ev
= &e
->xconfigure
;
534 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
539 XFreePixmap(dpy
, dc
.drawable
);
540 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
542 for(m
= mons
; m
; m
= m
->next
)
543 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
549 configurerequest(XEvent
*e
) {
552 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
555 if((c
= wintoclient(ev
->window
))) {
556 if(ev
->value_mask
& CWBorderWidth
)
557 c
->bw
= ev
->border_width
;
558 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
560 if(ev
->value_mask
& CWX
)
561 c
->x
= m
->mx
+ ev
->x
;
562 if(ev
->value_mask
& CWY
)
563 c
->y
= m
->my
+ ev
->y
;
564 if(ev
->value_mask
& CWWidth
)
566 if(ev
->value_mask
& CWHeight
)
568 if((c
->x
- m
->mx
+ c
->w
) > m
->mw
&& c
->isfloating
)
569 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
570 if((c
->y
- m
->my
+ c
->h
) > m
->mh
&& c
->isfloating
)
571 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
572 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
575 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
583 wc
.width
= ev
->width
;
584 wc
.height
= ev
->height
;
585 wc
.border_width
= ev
->border_width
;
586 wc
.sibling
= ev
->above
;
587 wc
.stack_mode
= ev
->detail
;
588 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
594 destroynotify(XEvent
*e
) {
596 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
598 if((c
= wintoclient(ev
->window
)))
606 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
611 detachstack(Client
*c
) {
614 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
617 if(c
== c
->mon
->sel
) {
618 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
624 die(const char *errstr
, ...) {
627 va_start(ap
, errstr
);
628 vfprintf(stderr
, errstr
, ap
);
638 if(!(m
= selmon
->next
))
643 for(m
= mons
; m
->next
; m
= m
->next
);
645 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
651 drawbar(Monitor
*m
) {
653 unsigned int i
, n
= 0, occ
= 0, urg
= 0;
657 for(c
= m
->clients
; c
; c
= c
->next
) {
665 for(i
= 0; i
< LENGTH(tags
); i
++) {
666 dc
.w
= TEXTW(tags
[i
]);
667 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
668 drawtext(tags
[i
], col
, urg
& 1 << i
);
669 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
670 occ
& 1 << i
, urg
& 1 << i
, col
);
675 drawtext(m
->lt
[m
->sellt
]->symbol
, dc
.norm
, False
);
678 snprintf(ntext
, sizeof ntext
, "%u", n
);
680 drawtext(ntext
, dc
.norm
, False
);
682 if(m
== selmon
) { /* status is only drawn on selected monitor */
689 drawtext(stext
, dc
.norm
, False
);
693 if((dc
.w
= dc
.x
- x
) > bh
) {
696 col
= m
== selmon
? dc
.sel
: dc
.norm
;
697 drawtext(m
->sel
->name
, col
, False
);
698 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
701 drawtext(NULL
, dc
.norm
, False
);
703 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
711 for(m
= mons
; m
; m
= m
->next
)
716 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
719 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
721 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
722 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
723 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
727 r
.width
= r
.height
= x
+ 1;
728 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
731 r
.width
= r
.height
= x
;
732 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
737 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
739 int i
, x
, y
, h
, len
, olen
;
740 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
742 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
743 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
747 h
= dc
.font
.ascent
+ dc
.font
.descent
;
748 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
750 /* shorten text if necessary */
751 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
754 memcpy(buf
, text
, len
);
756 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
757 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
759 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
761 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
765 enternotify(XEvent
*e
) {
768 XCrossingEvent
*ev
= &e
->xcrossing
;
770 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
772 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
773 unfocus(selmon
->sel
);
776 if((c
= wintoclient(ev
->window
)))
785 XExposeEvent
*ev
= &e
->xexpose
;
787 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
793 if(!c
|| !ISVISIBLE(c
))
794 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
796 unfocus(selmon
->sel
);
804 grabbuttons(c
, True
);
805 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
806 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
809 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
815 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
816 XFocusChangeEvent
*ev
= &e
->xfocus
;
818 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
819 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
823 focusmon(const Arg
*arg
) {
828 m
= dirtomon(arg
->i
);
829 unfocus(selmon
->sel
);
835 focusstack(const Arg
*arg
) {
836 Client
*c
= NULL
, *i
;
841 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
843 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
846 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
850 for(; i
; i
= i
->next
)
861 getcolor(const char *colstr
) {
862 Colormap cmap
= DefaultColormap(dpy
, screen
);
865 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
866 die("error, cannot allocate color '%s'\n", colstr
);
871 getrootptr(int *x
, int *y
) {
876 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
883 unsigned char *p
= NULL
;
884 unsigned long n
, extra
;
887 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
888 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
889 if(status
!= Success
)
898 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
903 if(!text
|| size
== 0)
906 XGetTextProperty(dpy
, w
, &name
, atom
);
909 if(name
.encoding
== XA_STRING
)
910 strncpy(text
, (char *)name
.value
, size
- 1);
912 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
915 strncpy(text
, *list
, size
- 1);
916 XFreeStringList(list
);
919 text
[size
- 1] = '\0';
925 grabbuttons(Client
*c
, Bool focused
) {
929 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
930 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
932 for(i
= 0; i
< LENGTH(buttons
); i
++)
933 if(buttons
[i
].click
== ClkClientWin
)
934 for(j
= 0; j
< LENGTH(modifiers
); j
++)
935 XGrabButton(dpy
, buttons
[i
].button
,
936 buttons
[i
].mask
| modifiers
[j
],
937 c
->win
, False
, BUTTONMASK
,
938 GrabModeAsync
, GrabModeSync
, None
, None
);
941 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
942 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
951 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
954 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
955 for(i
= 0; i
< LENGTH(keys
); i
++) {
956 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
957 for(j
= 0; j
< LENGTH(modifiers
); j
++)
958 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
959 True
, GrabModeAsync
, GrabModeAsync
);
965 initfont(const char *fontstr
) {
966 char *def
, **missing
;
970 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
973 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
974 XFreeStringList(missing
);
977 XFontSetExtents
*font_extents
;
978 XFontStruct
**xfonts
;
980 dc
.font
.ascent
= dc
.font
.descent
= 0;
981 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
982 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
983 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
984 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
985 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
990 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
991 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
992 die("error, cannot load font: '%s'\n", fontstr
);
993 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
994 dc
.font
.descent
= dc
.font
.xfont
->descent
;
996 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1000 isprotodel(Client
*c
) {
1005 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1006 for(i
= 0; !ret
&& i
< n
; i
++)
1007 if(protocols
[i
] == wmatom
[WMDelete
])
1015 keypress(XEvent
*e
) {
1021 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1022 for(i
= 0; i
< LENGTH(keys
); i
++)
1023 if(keysym
== keys
[i
].keysym
1024 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1026 keys
[i
].func(&(keys
[i
].arg
));
1030 killclient(const Arg
*arg
) {
1035 if(isprotodel(selmon
->sel
)) {
1036 ev
.type
= ClientMessage
;
1037 ev
.xclient
.window
= selmon
->sel
->win
;
1038 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1039 ev
.xclient
.format
= 32;
1040 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1041 ev
.xclient
.data
.l
[1] = CurrentTime
;
1042 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1045 XKillClient(dpy
, selmon
->sel
->win
);
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
) {
1137 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1138 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1142 movemouse(const Arg
*arg
) {
1143 int x
, y
, ocx
, ocy
, nx
, ny
;
1148 if(!(c
= selmon
->sel
))
1153 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1154 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1156 if(!getrootptr(&x
, &y
))
1159 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1161 case ConfigureRequest
:
1164 handler
[ev
.type
](&ev
);
1167 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1168 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1169 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1170 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1171 if(abs(selmon
->wx
- nx
) < snap
)
1173 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1174 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1175 if(abs(selmon
->wy
- ny
) < snap
)
1177 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1178 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1179 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1180 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1181 togglefloating(NULL
);
1183 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1184 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1188 while(ev
.type
!= ButtonRelease
);
1189 XUngrabPointer(dpy
, CurrentTime
);
1190 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1198 nexttiled(Client
*c
) {
1199 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1204 ptrtomon(int x
, int y
) {
1207 for(m
= mons
; m
; m
= m
->next
)
1208 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1214 propertynotify(XEvent
*e
) {
1217 XPropertyEvent
*ev
= &e
->xproperty
;
1219 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1221 else if(ev
->state
== PropertyDelete
)
1222 return; /* ignore */
1223 else if((c
= wintoclient(ev
->window
))) {
1226 case XA_WM_TRANSIENT_FOR
:
1227 XGetTransientForHint(dpy
, c
->win
, &trans
);
1228 if(!c
->isfloating
&& (c
->isfloating
= (wintoclient(trans
) != NULL
)))
1231 case XA_WM_NORMAL_HINTS
:
1239 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1241 if(c
== selmon
->sel
)
1248 quit(const Arg
*arg
) {
1253 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1256 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
)) {
1259 c
->w
= wc
.width
= w
;
1260 c
->h
= wc
.height
= h
;
1261 wc
.border_width
= c
->bw
;
1262 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1269 resizemouse(const Arg
*arg
) {
1276 if(!(c
= selmon
->sel
))
1281 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1282 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1284 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1286 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1288 case ConfigureRequest
:
1291 handler
[ev
.type
](&ev
);
1294 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1295 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1296 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1297 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
) {
1298 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1299 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1300 togglefloating(NULL
);
1302 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1303 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1307 while(ev
.type
!= ButtonRelease
);
1308 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1309 XUngrabPointer(dpy
, CurrentTime
);
1310 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1311 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1319 restack(Monitor
*m
) {
1327 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1328 XRaiseWindow(dpy
, m
->sel
->win
);
1329 if(m
->lt
[m
->sellt
]->arrange
) {
1330 wc
.stack_mode
= Below
;
1331 wc
.sibling
= m
->barwin
;
1332 for(c
= m
->stack
; c
; c
= c
->snext
)
1333 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1334 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1335 wc
.sibling
= c
->win
;
1339 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1346 /* main event loop */
1348 while(running
&& !XNextEvent(dpy
, &ev
))
1349 if(handler
[ev
.type
])
1350 (handler
[ev
.type
])(&ev
); /* call handler */
1355 unsigned int i
, num
;
1356 Window d1
, d2
, *wins
= NULL
;
1357 XWindowAttributes wa
;
1359 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1360 for(i
= 0; i
< num
; i
++) {
1361 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1362 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1364 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1365 manage(wins
[i
], &wa
);
1367 for(i
= 0; i
< num
; i
++) { /* now the transients */
1368 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1370 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1371 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1372 manage(wins
[i
], &wa
);
1380 sendmon(Client
*c
, Monitor
*m
) {
1387 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1395 setclientstate(Client
*c
, long state
) {
1396 long data
[] = { state
, None
};
1398 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1399 PropModeReplace
, (unsigned char *)data
, 2);
1403 setlayout(const Arg
*arg
) {
1404 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1407 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1414 /* arg > 1.0 will set mfact absolutly */
1416 setmfact(const Arg
*arg
) {
1419 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1421 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1422 if(f
< 0.1 || f
> 0.9)
1432 XSetWindowAttributes wa
;
1435 screen
= DefaultScreen(dpy
);
1436 root
= RootWindow(dpy
, screen
);
1438 sw
= DisplayWidth(dpy
, screen
);
1439 sh
= DisplayHeight(dpy
, screen
);
1440 bh
= dc
.h
= dc
.font
.height
+ 2;
1443 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1444 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1445 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1446 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1447 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1449 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1450 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1451 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1452 /* init appearance */
1453 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1454 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1455 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1456 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1457 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1458 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1459 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1460 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1461 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1463 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1465 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1466 w
= TEXTW(layouts
[i
].symbol
);
1471 /* EWMH support per view */
1472 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1473 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1474 /* select for events */
1475 wa
.cursor
= cursor
[CurNormal
];
1476 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1477 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1478 |PropertyChangeMask
;
1479 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1480 XSelectInput(dpy
, root
, wa
.event_mask
);
1485 showhide(Client
*c
) {
1488 if(ISVISIBLE(c
)) { /* show clients top down */
1489 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1490 if(!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1491 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1494 else { /* hide clients bottom up */
1496 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1502 sigchld(int signal
) {
1503 while(0 < waitpid(-1, NULL
, WNOHANG
));
1507 spawn(const Arg
*arg
) {
1508 signal(SIGCHLD
, sigchld
);
1511 close(ConnectionNumber(dpy
));
1513 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1514 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1521 tag(const Arg
*arg
) {
1522 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1523 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1529 tagmon(const Arg
*arg
) {
1530 if(!selmon
->sel
|| !mons
->next
)
1532 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1536 textnw(const char *text
, unsigned int len
) {
1540 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1543 return XTextWidth(dc
.font
.xfont
, text
, len
);
1552 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1556 c
= nexttiled(m
->clients
);
1557 mw
= m
->mfact
* m
->ww
;
1558 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1562 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1564 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1568 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1569 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1570 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1572 y
= c
->y
+ HEIGHT(c
);
1577 togglebar(const Arg
*arg
) {
1578 selmon
->showbar
= !selmon
->showbar
;
1579 updatebarpos(selmon
);
1580 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1585 togglefloating(const Arg
*arg
) {
1588 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1589 if(selmon
->sel
->isfloating
)
1590 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1591 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1596 toggletag(const Arg
*arg
) {
1601 mask
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1603 selmon
->sel
->tags
= mask
;
1609 toggleview(const Arg
*arg
) {
1610 unsigned int mask
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1613 selmon
->tagset
[selmon
->seltags
] = mask
;
1619 unfocus(Client
*c
) {
1622 grabbuttons(c
, False
);
1623 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1624 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1628 unmanage(Client
*c
) {
1631 wc
.border_width
= c
->oldbw
;
1632 /* The server grab construct avoids race conditions. */
1634 XSetErrorHandler(xerrordummy
);
1635 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1638 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1639 setclientstate(c
, WithdrawnState
);
1642 XSetErrorHandler(xerror
);
1649 unmapnotify(XEvent
*e
) {
1651 XUnmapEvent
*ev
= &e
->xunmap
;
1653 if((c
= wintoclient(ev
->window
)))
1660 XSetWindowAttributes wa
;
1662 wa
.override_redirect
= True
;
1663 wa
.background_pixmap
= ParentRelative
;
1664 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1665 for(m
= mons
; m
; m
= m
->next
) {
1666 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1667 CopyFromParent
, DefaultVisual(dpy
, screen
),
1668 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1669 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1670 XMapRaised(dpy
, m
->barwin
);
1675 updatebarpos(Monitor
*m
) {
1680 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1681 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1691 Monitor
*newmons
= NULL
, *m
= NULL
, *tm
;
1694 XineramaScreenInfo
*info
= NULL
;
1696 if(XineramaIsActive(dpy
))
1697 info
= XineramaQueryScreens(dpy
, &n
);
1698 #endif /* XINERAMA */
1699 /* allocate monitor(s) for the new geometry setup */
1700 for(i
= 0; i
< n
; i
++) {
1701 if(!(m
= (Monitor
*)malloc(sizeof(Monitor
))))
1702 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
1706 /* initialise monitor(s) */
1708 if(XineramaIsActive(dpy
)) {
1709 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1710 m
->num
= info
[i
].screen_number
;
1711 m
->mx
= m
->wx
= info
[i
].x_org
;
1712 m
->my
= m
->wy
= info
[i
].y_org
;
1713 m
->mw
= m
->ww
= info
[i
].width
;
1714 m
->mh
= m
->wh
= info
[i
].height
;
1719 #endif /* XINERAMA */
1720 /* default monitor setup */
1728 /* bar geometry setup */
1729 for(m
= newmons
; m
; m
= m
->next
) {
1730 m
->sel
= m
->stack
= m
->clients
= NULL
;
1733 m
->tagset
[0] = m
->tagset
[1] = 1;
1735 m
->showbar
= showbar
;
1737 m
->lt
[0] = &layouts
[0];
1738 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1741 /* reassign left over clients of disappeared monitors */
1742 for(tm
= mons
; tm
; tm
= tm
->next
)
1743 while(tm
->clients
) {
1745 tm
->clients
= c
->next
;
1751 /* select focused monitor */
1753 selmon
= mons
= newmons
;
1754 selmon
= wintomon(root
);
1758 updatenumlockmask(void) {
1760 XModifierKeymap
*modmap
;
1763 modmap
= XGetModifierMapping(dpy
);
1764 for(i
= 0; i
< 8; i
++)
1765 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1766 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1767 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1768 numlockmask
= (1 << i
);
1769 XFreeModifiermap(modmap
);
1773 updatesizehints(Client
*c
) {
1777 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1778 /* size is uninitialized, ensure that size.flags aren't used */
1780 if(size
.flags
& PBaseSize
) {
1781 c
->basew
= size
.base_width
;
1782 c
->baseh
= size
.base_height
;
1784 else if(size
.flags
& PMinSize
) {
1785 c
->basew
= size
.min_width
;
1786 c
->baseh
= size
.min_height
;
1789 c
->basew
= c
->baseh
= 0;
1790 if(size
.flags
& PResizeInc
) {
1791 c
->incw
= size
.width_inc
;
1792 c
->inch
= size
.height_inc
;
1795 c
->incw
= c
->inch
= 0;
1796 if(size
.flags
& PMaxSize
) {
1797 c
->maxw
= size
.max_width
;
1798 c
->maxh
= size
.max_height
;
1801 c
->maxw
= c
->maxh
= 0;
1802 if(size
.flags
& PMinSize
) {
1803 c
->minw
= size
.min_width
;
1804 c
->minh
= size
.min_height
;
1806 else if(size
.flags
& PBaseSize
) {
1807 c
->minw
= size
.base_width
;
1808 c
->minh
= size
.base_height
;
1811 c
->minw
= c
->minh
= 0;
1812 if(size
.flags
& PAspect
) {
1813 c
->mina
= (float)size
.min_aspect
.y
/ (float)size
.min_aspect
.x
;
1814 c
->maxa
= (float)size
.max_aspect
.x
/ (float)size
.max_aspect
.y
;
1817 c
->maxa
= c
->mina
= 0.0;
1818 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1819 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1823 updatetitle(Client
*c
) {
1824 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1825 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1826 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1827 strcpy(c
->name
, broken
);
1831 updatestatus(void) {
1832 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1833 strcpy(stext
, "dwm-"VERSION
);
1838 updatewmhints(Client
*c
) {
1841 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1842 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1843 wmh
->flags
&= ~XUrgencyHint
;
1844 XSetWMHints(dpy
, c
->win
, wmh
);
1847 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1853 view(const Arg
*arg
) {
1854 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1856 selmon
->seltags
^= 1; /* toggle sel tagset */
1857 if(arg
->ui
& TAGMASK
)
1858 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1863 wintoclient(Window w
) {
1867 for(m
= mons
; m
; m
= m
->next
)
1868 for(c
= m
->clients
; c
; c
= c
->next
)
1875 wintomon(Window w
) {
1880 if(w
== root
&& getrootptr(&x
, &y
))
1881 return ptrtomon(x
, y
);
1882 for(m
= mons
; m
; m
= m
->next
)
1885 if((c
= wintoclient(w
)))
1890 /* There's no way to check accesses to destroyed windows, thus those cases are
1891 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1892 * default error handler, which may call exit. */
1894 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1895 if(ee
->error_code
== BadWindow
1896 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1897 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1898 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1899 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1900 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1901 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1902 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1903 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1905 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1906 ee
->request_code
, ee
->error_code
);
1907 return xerrorxlib(dpy
, ee
); /* may call exit */
1911 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1915 /* Startup Error handler to check if another window manager
1916 * is already running. */
1918 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1924 zoom(const Arg
*arg
) {
1925 Client
*c
= selmon
->sel
;
1927 if(!selmon
->lt
[selmon
->sellt
]->arrange
1928 || selmon
->lt
[selmon
->sellt
]->arrange
== monocle
1929 || (selmon
->sel
&& selmon
->sel
->isfloating
))
1931 if(c
== nexttiled(selmon
->clients
))
1932 if(!c
|| !(c
= nexttiled(c
->next
)))
1941 main(int argc
, char *argv
[]) {
1942 if(argc
== 2 && !strcmp("-v", argv
[1]))
1943 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1945 die("usage: dwm [-v]\n");
1946 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1947 fputs("warning: no locale support\n", stderr
);
1948 if(!(dpy
= XOpenDisplay(NULL
)))
1949 die("dwm: cannot open display\n");