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 global
15 * linked client list, the focus history is remembered through a global
16 * stack list. 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];
143 const char *instance
;
149 /* function declarations */
150 static void applyrules(Client
*c
);
151 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
);
152 static void arrange(void);
153 static void attach(Client
*c
);
154 static void attachstack(Client
*c
);
155 static void buttonpress(XEvent
*e
);
156 static void checkotherwm(void);
157 static void cleanup(void);
158 static void cleanupmons(void);
159 static void clearurgent(Client
*c
);
160 static void configure(Client
*c
);
161 static void configurenotify(XEvent
*e
);
162 static void configurerequest(XEvent
*e
);
163 static void destroynotify(XEvent
*e
);
164 static void detach(Client
*c
);
165 static void detachstack(Client
*c
);
166 static void die(const char *errstr
, ...);
167 static void drawbar(Monitor
*m
);
168 static void drawbars(void);
169 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
170 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
171 static void enternotify(XEvent
*e
);
172 static void expose(XEvent
*e
);
173 static void focus(Client
*c
);
174 static void focusin(XEvent
*e
);
175 static void focusstack(const Arg
*arg
);
176 static unsigned long getcolor(const char *colstr
);
177 static Bool
getrootpointer(int *x
, int *y
);
178 static long getstate(Window w
);
179 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
180 static void grabbuttons(Client
*c
, Bool focused
);
181 static void grabkeys(void);
182 static Monitor
*idxtomon(unsigned int n
);
183 static void initfont(const char *fontstr
);
184 static Bool
isprotodel(Client
*c
);
185 static void keypress(XEvent
*e
);
186 static void killclient(const Arg
*arg
);
187 static void manage(Window w
, XWindowAttributes
*wa
);
188 static void mappingnotify(XEvent
*e
);
189 static void maprequest(XEvent
*e
);
190 static void monocle(Monitor
*m
);
191 static void movemouse(const Arg
*arg
);
192 static Client
*nexttiled(Client
*c
);
193 static Monitor
*pointertomon(int x
, int y
);
194 static void propertynotify(XEvent
*e
);
195 static void quit(const Arg
*arg
);
196 static void resize(Client
*c
, int x
, int y
, int w
, int h
);
197 static void resizemouse(const Arg
*arg
);
198 static void restack(Monitor
*m
);
199 static void run(void);
200 static void scan(void);
201 static void sendmon(Client
*c
, Monitor
*m
);
202 static void setclientstate(Client
*c
, long state
);
203 static void setlayout(const Arg
*arg
);
204 static void setmfact(const Arg
*arg
);
205 static void setup(void);
206 static void showhide(Client
*c
);
207 static void sigchld(int signal
);
208 static void spawn(const Arg
*arg
);
209 static void tag(const Arg
*arg
);
210 static int textnw(const char *text
, unsigned int len
);
211 static void tile(Monitor
*);
212 static void togglebar(const Arg
*arg
);
213 static void togglefloating(const Arg
*arg
);
214 static void toggletag(const Arg
*arg
);
215 static void toggleview(const Arg
*arg
);
216 static void unfocus(Client
*c
);
217 static void unmanage(Client
*c
);
218 static void unmapnotify(XEvent
*e
);
219 static void updategeom(void);
220 static void updatebarpos(Monitor
*m
);
221 static void updatebars(void);
222 static void updatenumlockmask(void);
223 static void updatesizehints(Client
*c
);
224 static void updatestatus(void);
225 static void updatetitle(Client
*c
);
226 static void updatewmhints(Client
*c
);
227 static void view(const Arg
*arg
);
228 static Client
*wintoclient(Window w
);
229 static Monitor
*wintomon(Window w
);
230 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
231 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
232 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
233 static void zoom(const Arg
*arg
);
235 static void focusmon(const Arg
*arg
);
236 static void tagmon(const Arg
*arg
);
237 #endif /* XINERAMA */
240 static char stext
[256];
242 static int sw
, sh
; /* X display screen geometry x, y, width, height */
243 static int bh
, blw
= 0; /* bar geometry */
244 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
245 static unsigned int numlockmask
= 0;
246 static void (*handler
[LASTEvent
]) (XEvent
*) = {
247 [ButtonPress
] = buttonpress
,
248 [ConfigureRequest
] = configurerequest
,
249 [ConfigureNotify
] = configurenotify
,
250 [DestroyNotify
] = destroynotify
,
251 [EnterNotify
] = enternotify
,
254 [KeyPress
] = keypress
,
255 [MappingNotify
] = mappingnotify
,
256 [MapRequest
] = maprequest
,
257 [PropertyNotify
] = propertynotify
,
258 [UnmapNotify
] = unmapnotify
260 static Atom wmatom
[WMLast
], netatom
[NetLast
];
262 static Bool running
= True
;
263 static Cursor cursor
[CurLast
];
266 static Layout
*lt
[] = { NULL
, NULL
};
267 static Monitor
*mons
= NULL
, *selmon
= NULL
;
269 /* configuration, allows nested code to access above variables */
272 /* compile-time check if all tags fit into an unsigned int bit array. */
273 struct NumTags
{ char limitexceeded
[sizeof(unsigned int) * 8 < LENGTH(tags
) ? -1 : 1]; };
275 /* function implementations */
277 applyrules(Client
*c
) {
280 XClassHint ch
= { 0 };
283 c
->isfloating
= c
->tags
= 0;
284 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
285 for(i
= 0; i
< LENGTH(rules
); i
++) {
287 if((!r
->title
|| strstr(c
->name
, r
->title
))
288 && (!r
->class || (ch
.res_class
&& strstr(ch
.res_class
, r
->class)))
289 && (!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
) {
307 /* set minimum possible */
311 if(*x
> m
->mx
+ m
->mw
)
312 *x
= m
->mw
- WIDTH(c
);
313 if(*y
> m
->my
+ m
->mh
)
314 *y
= m
->mh
- HEIGHT(c
);
315 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
317 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
324 if(resizehints
|| c
->isfloating
) {
325 /* see last two sentences in ICCCM 4.1.2.3 */
326 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
328 if(!baseismin
) { /* temporarily remove base dimensions */
333 /* adjust for aspect limits */
334 if(c
->mina
> 0 && c
->maxa
> 0) {
335 if(c
->maxa
< (float)*w
/ *h
)
337 else if(c
->mina
< (float)*h
/ *w
)
341 if(baseismin
) { /* increment calculation requires this */
346 /* adjust for increment value */
352 /* restore base dimensions */
356 *w
= MAX(*w
, c
->minw
);
357 *h
= MAX(*h
, c
->minh
);
360 *w
= MIN(*w
, c
->maxw
);
363 *h
= MIN(*h
, c
->maxh
);
365 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
372 /* optimise two loops into one, check focus(NULL) */
373 for(m
= mons
; m
; m
= m
->next
)
376 for(m
= mons
; m
; m
= m
->next
) {
377 if(lt
[m
->sellt
]->arrange
)
378 lt
[m
->sellt
]->arrange(m
);
385 c
->next
= c
->mon
->clients
;
390 attachstack(Client
*c
) {
391 c
->snext
= c
->mon
->stack
;
396 buttonpress(XEvent
*e
) {
397 unsigned int i
, x
, click
;
401 XButtonPressedEvent
*ev
= &e
->xbutton
;
404 /* focus monitor if necessary */
405 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
406 unfocus(selmon
->sel
);
410 if(ev
->window
== selmon
->barwin
&& ev
->x
>= selmon
->btx
) {
415 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
416 if(i
< LENGTH(tags
)) {
420 else if(ev
->x
< x
+ blw
)
422 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
423 click
= ClkStatusText
;
427 else if((c
= wintoclient(ev
->window
))) {
429 click
= ClkClientWin
;
432 for(i
= 0; i
< LENGTH(buttons
); i
++)
433 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
434 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
435 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
441 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 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
|| !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
);
626 drawbar(Monitor
*m
) {
628 unsigned int i
, occ
= 0, urg
= 0;
632 for(c
= m
->clients
; c
; c
= c
->next
) {
640 if(mons
->next
) { /* more than a single monitor */
642 buf
[0] = m
->screen_number
+ '0';
645 drawtext(buf
, selmon
== m
? dc
.sel
: dc
.norm
, True
);
648 #endif /* XINERAMA */
650 for(i
= 0; i
< LENGTH(tags
); i
++) {
651 dc
.w
= TEXTW(tags
[i
]);
652 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
653 drawtext(tags
[i
], col
, urg
& 1 << i
);
654 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
655 occ
& 1 << i
, urg
& 1 << i
, col
);
660 drawtext(lt
[m
->sellt
]->symbol
, dc
.norm
, False
);
665 if(m
== selmon
) { /* status is only drawn on selected monitor */
672 drawtext(stext
, dc
.norm
, False
);
677 if((dc
.w
= dc
.x
- x
) > bh
) {
680 col
= m
== selmon
? dc
.sel
: dc
.norm
;
681 drawtext(m
->sel
->name
, col
, False
);
682 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
685 drawtext(NULL
, dc
.norm
, False
);
687 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
695 for(m
= mons
; m
; m
= m
->next
)
700 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
703 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
705 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
706 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
707 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
711 r
.width
= r
.height
= x
+ 1;
712 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
715 r
.width
= r
.height
= x
;
716 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
721 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
723 int i
, x
, y
, h
, len
, olen
;
724 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
726 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
727 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
731 h
= dc
.font
.ascent
+ dc
.font
.descent
;
732 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
734 /* shorten text if necessary */
735 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
738 memcpy(buf
, text
, len
);
740 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
741 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
743 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
745 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
749 enternotify(XEvent
*e
) {
752 XCrossingEvent
*ev
= &e
->xcrossing
;
754 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
756 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
757 unfocus(selmon
->sel
);
760 if((c
= wintoclient(ev
->window
)))
769 XExposeEvent
*ev
= &e
->xexpose
;
771 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
777 if(!c
|| !ISVISIBLE(c
))
778 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
780 unfocus(selmon
->sel
);
788 grabbuttons(c
, True
);
789 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
790 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
793 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
799 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
800 XFocusChangeEvent
*ev
= &e
->xfocus
;
802 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
803 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
808 focusmon(const Arg
*arg
) {
811 if(!(m
= idxtomon(arg
->ui
)) || m
== selmon
)
813 unfocus(selmon
->sel
);
817 #endif /* XINERAMA */
820 focusstack(const Arg
*arg
) {
821 Client
*c
= NULL
, *i
;
826 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
828 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
831 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
835 for(; i
; i
= i
->next
)
846 getcolor(const char *colstr
) {
847 Colormap cmap
= DefaultColormap(dpy
, screen
);
850 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
851 die("error, cannot allocate color '%s'\n", colstr
);
856 getrootpointer(int *x
, int *y
) {
860 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
867 unsigned char *p
= NULL
;
868 unsigned long n
, extra
;
871 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
872 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
873 if(status
!= Success
)
882 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
887 if(!text
|| size
== 0)
890 XGetTextProperty(dpy
, w
, &name
, atom
);
893 if(name
.encoding
== XA_STRING
)
894 strncpy(text
, (char *)name
.value
, size
- 1);
896 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
898 strncpy(text
, *list
, size
- 1);
899 XFreeStringList(list
);
902 text
[size
- 1] = '\0';
908 grabbuttons(Client
*c
, Bool focused
) {
912 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
913 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
915 for(i
= 0; i
< LENGTH(buttons
); i
++)
916 if(buttons
[i
].click
== ClkClientWin
)
917 for(j
= 0; j
< LENGTH(modifiers
); j
++)
918 XGrabButton(dpy
, buttons
[i
].button
,
919 buttons
[i
].mask
| modifiers
[j
],
920 c
->win
, False
, BUTTONMASK
,
921 GrabModeAsync
, GrabModeSync
, None
, None
);
923 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
924 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
933 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
936 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
937 for(i
= 0; i
< LENGTH(keys
); i
++) {
938 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
939 for(j
= 0; j
< LENGTH(modifiers
); j
++)
940 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
941 True
, GrabModeAsync
, GrabModeAsync
);
947 idxtomon(unsigned int n
) {
951 for(m
= mons
, i
= 0; m
&& i
!= n
; m
= m
->next
, i
++);
956 initfont(const char *fontstr
) {
957 char *def
, **missing
;
961 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
964 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
965 XFreeStringList(missing
);
968 XFontSetExtents
*font_extents
;
969 XFontStruct
**xfonts
;
971 dc
.font
.ascent
= dc
.font
.descent
= 0;
972 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
973 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
974 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
975 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
976 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
981 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
982 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
983 die("error, cannot load font: '%s'\n", fontstr
);
984 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
985 dc
.font
.descent
= dc
.font
.xfont
->descent
;
987 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
991 isprotodel(Client
*c
) {
996 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
997 for(i
= 0; !ret
&& i
< n
; i
++)
998 if(protocols
[i
] == wmatom
[WMDelete
])
1006 keypress(XEvent
*e
) {
1012 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1013 for(i
= 0; i
< LENGTH(keys
); i
++)
1014 if(keysym
== keys
[i
].keysym
1015 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1017 keys
[i
].func(&(keys
[i
].arg
));
1021 killclient(const Arg
*arg
) {
1026 if(isprotodel(selmon
->sel
)) {
1027 ev
.type
= ClientMessage
;
1028 ev
.xclient
.window
= selmon
->sel
->win
;
1029 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1030 ev
.xclient
.format
= 32;
1031 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1032 ev
.xclient
.data
.l
[1] = CurrentTime
;
1033 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1036 XKillClient(dpy
, selmon
->sel
->win
);
1040 manage(Window w
, XWindowAttributes
*wa
) {
1042 Client
*c
, *t
= NULL
;
1043 Window trans
= None
;
1046 if(!(c
= malloc(sizeof(Client
))))
1047 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1051 if(XGetTransientForHint(dpy
, w
, &trans
))
1052 t
= wintoclient(trans
);
1063 c
->x
= wa
->x
+ c
->mon
->wx
;
1064 c
->y
= wa
->y
+ c
->mon
->wy
;
1067 c
->oldbw
= wa
->border_width
;
1068 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1074 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1075 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1076 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1077 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1078 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1079 /* only fix client y-offset, if the client center might cover the bar */
1080 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1081 && (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
);
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(!getrootpointer(&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
&& lt
[selmon
->sellt
]->arrange
1174 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1175 togglefloating(NULL
);
1177 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1178 resize(c
, nx
, ny
, c
->w
, c
->h
);
1182 while(ev
.type
!= ButtonRelease
);
1183 XUngrabPointer(dpy
, CurrentTime
);
1184 if((m
= pointertomon(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 pointertomon(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
) {
1250 if(applysizehints(c
, &x
, &y
, &w
, &h
)) {
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);
1291 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1292 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
) {
1293 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1294 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1295 togglefloating(NULL
);
1297 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1298 resize(c
, c
->x
, c
->y
, nw
, nh
);
1302 while(ev
.type
!= ButtonRelease
);
1303 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1304 XUngrabPointer(dpy
, CurrentTime
);
1305 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1306 if((m
= pointertomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1314 restack(Monitor
*m
) {
1322 if(m
->sel
->isfloating
|| !lt
[m
->sellt
]->arrange
)
1323 XRaiseWindow(dpy
, m
->sel
->win
);
1324 if(lt
[m
->sellt
]->arrange
) {
1325 wc
.stack_mode
= Below
;
1326 wc
.sibling
= m
->barwin
;
1327 for(c
= m
->stack
; c
; c
= c
->snext
)
1328 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1329 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1330 wc
.sibling
= c
->win
;
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 */
1351 unsigned int i
, num
;
1352 Window d1
, d2
, *wins
= NULL
;
1353 XWindowAttributes wa
;
1355 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1356 for(i
= 0; i
< num
; i
++) {
1357 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1358 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1360 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1361 manage(wins
[i
], &wa
);
1363 for(i
= 0; i
< num
; i
++) { /* now the transients */
1364 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1366 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1367 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1368 manage(wins
[i
], &wa
);
1376 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
!= lt
[selmon
->sellt
])
1402 lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1409 /* arg > 1.0 will set mfact absolutly */
1411 setmfact(const Arg
*arg
) {
1414 if(!arg
|| !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;
1436 lt
[0] = &layouts
[0];
1437 lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1441 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1442 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1443 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1444 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1445 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1448 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1449 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1450 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
);
1466 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1467 w
= TEXTW(layouts
[i
].symbol
);
1473 /* EWMH support per view */
1474 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1475 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1477 /* select for events */
1478 wa
.cursor
= cursor
[CurNormal
];
1479 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1480 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1481 |PropertyChangeMask
;
1482 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1483 XSelectInput(dpy
, root
, wa
.event_mask
);
1489 showhide(Client
*c
) {
1492 if(ISVISIBLE(c
)) { /* show clients top down */
1493 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1494 if(!lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1495 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1498 else { /* hide clients bottom up */
1500 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1506 sigchld(int signal
) {
1507 while(0 < waitpid(-1, NULL
, WNOHANG
));
1511 spawn(const Arg
*arg
) {
1512 signal(SIGCHLD
, sigchld
);
1515 close(ConnectionNumber(dpy
));
1517 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1518 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1525 tag(const Arg
*arg
) {
1526 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1527 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1534 tagmon(const Arg
*arg
) {
1537 if(!selmon
->sel
|| !(m
= idxtomon(arg
->ui
)))
1539 sendmon(selmon
->sel
, m
);
1541 #endif /* XINERAMA */
1544 textnw(const char *text
, unsigned int len
) {
1548 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1551 return XTextWidth(dc
.font
.xfont
, text
, len
);
1560 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1565 c
= nexttiled(m
->clients
);
1566 mw
= m
->mfact
* m
->ww
;
1567 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1573 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1575 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1580 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1581 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1582 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
));
1584 y
= c
->y
+ HEIGHT(c
);
1589 togglebar(const Arg
*arg
) {
1590 selmon
->showbar
= !selmon
->showbar
;
1591 updatebarpos(selmon
);
1592 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1597 togglefloating(const Arg
*arg
) {
1600 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1601 if(selmon
->sel
->isfloating
)
1602 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
, selmon
->sel
->w
, selmon
->sel
->h
);
1607 toggletag(const Arg
*arg
) {
1613 mask
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1615 selmon
->sel
->tags
= mask
;
1621 toggleview(const Arg
*arg
) {
1622 unsigned int mask
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1625 selmon
->tagset
[selmon
->seltags
] = mask
;
1631 unfocus(Client
*c
) {
1634 grabbuttons(c
, False
);
1635 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1636 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1640 unmanage(Client
*c
) {
1643 wc
.border_width
= c
->oldbw
;
1644 /* The server grab construct avoids race conditions. */
1646 XSetErrorHandler(xerrordummy
);
1647 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1650 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1651 setclientstate(c
, WithdrawnState
);
1654 XSetErrorHandler(xerror
);
1661 unmapnotify(XEvent
*e
) {
1663 XUnmapEvent
*ev
= &e
->xunmap
;
1665 if((c
= wintoclient(ev
->window
)))
1672 XSetWindowAttributes wa
;
1674 wa
.override_redirect
= True
;
1675 wa
.background_pixmap
= ParentRelative
;
1676 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1678 for(m
= mons
; m
; m
= m
->next
) {
1679 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1681 CopyFromParent
, DefaultVisual(dpy
, screen
),
1682 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1683 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1684 XMapRaised(dpy
, m
->barwin
);
1689 updatebarpos(Monitor
*m
) {
1694 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1695 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1705 Monitor
*newmons
= NULL
, *m
, *tm
;
1708 XineramaScreenInfo
*info
= NULL
;
1710 if(XineramaIsActive(dpy
))
1711 info
= XineramaQueryScreens(dpy
, &n
);
1712 #endif /* XINERAMA */
1713 /* allocate monitor(s) for the new geometry setup */
1714 for(i
= 0; i
< n
; i
++) {
1715 m
= (Monitor
*)malloc(sizeof(Monitor
));
1720 /* initialise monitor(s) */
1722 if(XineramaIsActive(dpy
)) {
1723 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1724 m
->screen_number
= 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 */
1736 m
->screen_number
= 0;
1743 /* bar geometry setup */
1744 for(m
= newmons
; m
; m
= m
->next
) {
1745 m
->sel
= m
->stack
= m
->clients
= NULL
;
1748 m
->tagset
[0] = m
->tagset
[1] = 1;
1750 m
->showbar
= SHOWBAR
;
1755 /* reassign left over clients of disappeared monitors */
1756 for(tm
= mons
; tm
; tm
= tm
->next
)
1757 while(tm
->clients
) {
1759 tm
->clients
= c
->next
;
1766 /* select focused monitor */
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
/ (float)size
.min_aspect
.x
;
1829 c
->maxa
= (float)size
.max_aspect
.x
/ (float)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
);
1844 updatestatus(void) {
1845 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1846 strcpy(stext
, "dwm-"VERSION
);
1851 updatewmhints(Client
*c
) {
1854 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1855 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1856 wmh
->flags
&= ~XUrgencyHint
;
1857 XSetWMHints(dpy
, c
->win
, wmh
);
1860 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1867 view(const Arg
*arg
) {
1868 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1870 selmon
->seltags
^= 1; /* toggle sel tagset */
1871 if(arg
->ui
& TAGMASK
)
1872 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1877 wintoclient(Window w
) {
1881 for(m
= mons
; m
; m
= m
->next
)
1882 for(c
= m
->clients
; c
; c
= c
->next
)
1889 wintomon(Window w
) {
1894 if(w
== root
&& getrootpointer(&x
, &y
))
1895 return pointertomon(x
, y
);
1896 for(m
= mons
; m
; m
= m
->next
)
1899 if((c
= wintoclient(w
)))
1904 /* There's no way to check accesses to destroyed windows, thus those cases are
1905 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1906 * default error handler, which may call exit. */
1908 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1909 if(ee
->error_code
== BadWindow
1910 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1911 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1912 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1913 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1914 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1915 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1916 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1917 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1919 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1920 ee
->request_code
, ee
->error_code
);
1921 return xerrorxlib(dpy
, ee
); /* may call exit */
1925 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1929 /* Startup Error handler to check if another window manager
1930 * is already running. */
1932 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1938 zoom(const Arg
*arg
) {
1939 Client
*c
= selmon
->sel
;
1941 if(!lt
[selmon
->sellt
]->arrange
|| lt
[selmon
->sellt
]->arrange
== monocle
|| (selmon
->sel
&& selmon
->sel
->isfloating
))
1943 if(c
== nexttiled(selmon
->clients
))
1944 if(!c
|| !(c
= nexttiled(c
->next
)))
1953 main(int argc
, char *argv
[]) {
1954 if(argc
== 2 && !strcmp("-v", argv
[1]))
1955 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1957 die("usage: dwm [-v]\n");
1959 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1960 fputs("warning: no locale support\n", stderr
);
1962 if(!(dpy
= XOpenDisplay(NULL
)))
1963 die("dwm: cannot open display\n");