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>
42 #include <X11/Xft/Xft.h>
48 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
49 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
50 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
51 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
52 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
53 #define LENGTH(X) (sizeof X / sizeof X[0])
54 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
55 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
56 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
57 #define TAGMASK ((1 << LENGTH(tags)) - 1)
58 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
61 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
62 enum { SchemeNorm
, SchemeSel
}; /* color schemes */
63 enum { NetSupported
, NetWMName
, NetWMState
, NetWMCheck
,
64 NetWMFullscreen
, NetActiveWindow
, NetWMWindowType
,
65 NetWMWindowTypeDialog
, NetClientList
, NetLast
}; /* EWMH atoms */
66 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, WMLast
}; /* default atoms */
67 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
68 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
81 void (*func
)(const Arg
*arg
);
85 typedef struct Monitor Monitor
;
86 typedef struct Client Client
;
91 int oldx
, oldy
, oldw
, oldh
;
92 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
, hintsvalid
;
95 int isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
;
105 void (*func
)(const Arg
*);
111 void (*arrange
)(Monitor
*);
119 int by
; /* bar geometry */
120 int mx
, my
, mw
, mh
; /* screen size */
121 int wx
, wy
, ww
, wh
; /* window area */
122 unsigned int seltags
;
124 unsigned int tagset
[2];
137 const char *instance
;
144 /* function declarations */
145 static void applyrules(Client
*c
);
146 static int applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
);
147 static void arrange(Monitor
*m
);
148 static void arrangemon(Monitor
*m
);
149 static void attach(Client
*c
);
150 static void attachstack(Client
*c
);
151 static void buttonpress(XEvent
*e
);
152 static void checkotherwm(void);
153 static void cleanup(void);
154 static void cleanupmon(Monitor
*mon
);
155 static void clientmessage(XEvent
*e
);
156 static void configure(Client
*c
);
157 static void configurenotify(XEvent
*e
);
158 static void configurerequest(XEvent
*e
);
159 static Monitor
*createmon(void);
160 static void destroynotify(XEvent
*e
);
161 static void detach(Client
*c
);
162 static void detachstack(Client
*c
);
163 static Monitor
*dirtomon(int dir
);
164 static void drawbar(Monitor
*m
);
165 static void drawbars(void);
166 static void enternotify(XEvent
*e
);
167 static void expose(XEvent
*e
);
168 static void focus(Client
*c
);
169 static void focusin(XEvent
*e
);
170 static void focusmon(const Arg
*arg
);
171 static void focusstack(const Arg
*arg
);
172 static Atom
getatomprop(Client
*c
, Atom prop
);
173 static int getrootptr(int *x
, int *y
);
174 static long getstate(Window w
);
175 static int gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
176 static void grabbuttons(Client
*c
, int focused
);
177 static void grabkeys(void);
178 static void incnmaster(const Arg
*arg
);
179 static void keypress(XEvent
*e
);
180 static void killclient(const Arg
*arg
);
181 static void manage(Window w
, XWindowAttributes
*wa
);
182 static void mappingnotify(XEvent
*e
);
183 static void maprequest(XEvent
*e
);
184 static void monocle(Monitor
*m
);
185 static void motionnotify(XEvent
*e
);
186 static void movemouse(const Arg
*arg
);
187 static Client
*nexttiled(Client
*c
);
188 static void pop(Client
*c
);
189 static void propertynotify(XEvent
*e
);
190 static void quit(const Arg
*arg
);
191 static Monitor
*recttomon(int x
, int y
, int w
, int h
);
192 static void resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
);
193 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
194 static void resizemouse(const Arg
*arg
);
195 static void restack(Monitor
*m
);
196 static void run(void);
197 static void scan(void);
198 static int sendevent(Client
*c
, Atom proto
);
199 static void sendmon(Client
*c
, Monitor
*m
);
200 static void setclientstate(Client
*c
, long state
);
201 static void setfocus(Client
*c
);
202 static void setfullscreen(Client
*c
, int fullscreen
);
203 static void setlayout(const Arg
*arg
);
204 static void setmfact(const Arg
*arg
);
205 static void setup(void);
206 static void seturgent(Client
*c
, int urg
);
207 static void showhide(Client
*c
);
208 static void sigchld(int unused
);
209 static void spawn(const Arg
*arg
);
210 static void tag(const Arg
*arg
);
211 static void tagmon(const Arg
*arg
);
212 static void tile(Monitor
*m
);
213 static void togglebar(const Arg
*arg
);
214 static void togglefloating(const Arg
*arg
);
215 static void toggletag(const Arg
*arg
);
216 static void toggleview(const Arg
*arg
);
217 static void unfocus(Client
*c
, int setfocus
);
218 static void unmanage(Client
*c
, int destroyed
);
219 static void unmapnotify(XEvent
*e
);
220 static void updatebarpos(Monitor
*m
);
221 static void updatebars(void);
222 static void updateclientlist(void);
223 static int updategeom(void);
224 static void updatenumlockmask(void);
225 static void updatesizehints(Client
*c
);
226 static void updatestatus(void);
227 static void updatetitle(Client
*c
);
228 static void updatewindowtype(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 const char broken
[] = "broken";
240 static char stext
[256];
242 static int sw
, sh
; /* X display screen geometry width, height */
243 static int bh
; /* bar height */
244 static int lrpad
; /* sum of left and right padding for text */
245 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
246 static unsigned int numlockmask
= 0;
247 static void (*handler
[LASTEvent
]) (XEvent
*) = {
248 [ButtonPress
] = buttonpress
,
249 [ClientMessage
] = clientmessage
,
250 [ConfigureRequest
] = configurerequest
,
251 [ConfigureNotify
] = configurenotify
,
252 [DestroyNotify
] = destroynotify
,
253 [EnterNotify
] = enternotify
,
256 [KeyPress
] = keypress
,
257 [MappingNotify
] = mappingnotify
,
258 [MapRequest
] = maprequest
,
259 [MotionNotify
] = motionnotify
,
260 [PropertyNotify
] = propertynotify
,
261 [UnmapNotify
] = unmapnotify
263 static Atom wmatom
[WMLast
], netatom
[NetLast
];
264 static int running
= 1;
265 static Cur
*cursor
[CurLast
];
269 static Monitor
*mons
, *selmon
;
270 static Window root
, wmcheckwin
;
272 /* configuration, allows nested code to access above variables */
275 /* compile-time check if all tags fit into an unsigned int bit array. */
276 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
278 /* function implementations */
280 applyrules(Client
*c
)
282 const char *class, *instance
;
286 XClassHint ch
= { NULL
, NULL
};
291 XGetClassHint(dpy
, c
->win
, &ch
);
292 class = ch
.res_class
? ch
.res_class
: broken
;
293 instance
= ch
.res_name
? ch
.res_name
: broken
;
295 for (i
= 0; i
< LENGTH(rules
); i
++) {
297 if ((!r
->title
|| strstr(c
->name
, r
->title
))
298 && (!r
->class || strstr(class, r
->class))
299 && (!r
->instance
|| strstr(instance
, r
->instance
)))
301 c
->isfloating
= r
->isfloating
;
303 for (m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
312 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
316 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
)
321 /* set minimum possible */
329 if (*x
+ *w
+ 2 * c
->bw
< 0)
331 if (*y
+ *h
+ 2 * c
->bw
< 0)
334 if (*x
>= m
->wx
+ m
->ww
)
335 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
336 if (*y
>= m
->wy
+ m
->wh
)
337 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
338 if (*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
340 if (*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
347 if (resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
350 /* see last two sentences in ICCCM 4.1.2.3 */
351 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
352 if (!baseismin
) { /* temporarily remove base dimensions */
356 /* adjust for aspect limits */
357 if (c
->mina
> 0 && c
->maxa
> 0) {
358 if (c
->maxa
< (float)*w
/ *h
)
359 *w
= *h
* c
->maxa
+ 0.5;
360 else if (c
->mina
< (float)*h
/ *w
)
361 *h
= *w
* c
->mina
+ 0.5;
363 if (baseismin
) { /* increment calculation requires this */
367 /* adjust for increment value */
372 /* restore base dimensions */
373 *w
= MAX(*w
+ c
->basew
, c
->minw
);
374 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
376 *w
= MIN(*w
, c
->maxw
);
378 *h
= MIN(*h
, c
->maxh
);
380 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
388 else for (m
= mons
; m
; m
= m
->next
)
393 } else for (m
= mons
; m
; m
= m
->next
)
398 arrangemon(Monitor
*m
)
400 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
401 if (m
->lt
[m
->sellt
]->arrange
)
402 m
->lt
[m
->sellt
]->arrange(m
);
408 c
->next
= c
->mon
->clients
;
413 attachstack(Client
*c
)
415 c
->snext
= c
->mon
->stack
;
420 buttonpress(XEvent
*e
)
422 unsigned int i
, x
, click
;
426 XButtonPressedEvent
*ev
= &e
->xbutton
;
429 /* focus monitor if necessary */
430 if ((m
= wintomon(ev
->window
)) && m
!= selmon
) {
431 unfocus(selmon
->sel
, 1);
435 if (ev
->window
== selmon
->barwin
) {
439 while (ev
->x
>= x
&& ++i
< LENGTH(tags
));
440 if (i
< LENGTH(tags
)) {
443 } else if (ev
->x
< x
+ TEXTW(selmon
->ltsymbol
))
445 else if (ev
->x
> selmon
->ww
- (int)TEXTW(stext
))
446 click
= ClkStatusText
;
449 } else if ((c
= wintoclient(ev
->window
))) {
452 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
453 click
= ClkClientWin
;
455 for (i
= 0; i
< LENGTH(buttons
); i
++)
456 if (click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
457 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
458 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
464 xerrorxlib
= XSetErrorHandler(xerrorstart
);
465 /* this causes an error if some other window manager is running */
466 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
468 XSetErrorHandler(xerror
);
476 Layout foo
= { "", NULL
};
481 selmon
->lt
[selmon
->sellt
] = &foo
;
482 for (m
= mons
; m
; m
= m
->next
)
484 unmanage(m
->stack
, 0);
485 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
488 for (i
= 0; i
< CurLast
; i
++)
489 drw_cur_free(drw
, cursor
[i
]);
490 for (i
= 0; i
< LENGTH(colors
); i
++)
493 XDestroyWindow(dpy
, wmcheckwin
);
496 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
497 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
501 cleanupmon(Monitor
*mon
)
508 for (m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
511 XUnmapWindow(dpy
, mon
->barwin
);
512 XDestroyWindow(dpy
, mon
->barwin
);
517 clientmessage(XEvent
*e
)
519 XClientMessageEvent
*cme
= &e
->xclient
;
520 Client
*c
= wintoclient(cme
->window
);
524 if (cme
->message_type
== netatom
[NetWMState
]) {
525 if (cme
->data
.l
[1] == netatom
[NetWMFullscreen
]
526 || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
527 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
528 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
529 } else if (cme
->message_type
== netatom
[NetActiveWindow
]) {
530 if (c
!= selmon
->sel
&& !c
->isurgent
)
540 ce
.type
= ConfigureNotify
;
548 ce
.border_width
= c
->bw
;
550 ce
.override_redirect
= False
;
551 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
555 configurenotify(XEvent
*e
)
559 XConfigureEvent
*ev
= &e
->xconfigure
;
562 /* TODO: updategeom handling sucks, needs to be simplified */
563 if (ev
->window
== root
) {
564 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
567 if (updategeom() || dirty
) {
568 drw_resize(drw
, sw
, bh
);
570 for (m
= mons
; m
; m
= m
->next
) {
571 for (c
= m
->clients
; c
; c
= c
->next
)
573 resizeclient(c
, m
->mx
, m
->my
, m
->mw
, m
->mh
);
574 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
583 configurerequest(XEvent
*e
)
587 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
590 if ((c
= wintoclient(ev
->window
))) {
591 if (ev
->value_mask
& CWBorderWidth
)
592 c
->bw
= ev
->border_width
;
593 else if (c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
595 if (ev
->value_mask
& CWX
) {
597 c
->x
= m
->mx
+ ev
->x
;
599 if (ev
->value_mask
& CWY
) {
601 c
->y
= m
->my
+ ev
->y
;
603 if (ev
->value_mask
& CWWidth
) {
607 if (ev
->value_mask
& CWHeight
) {
611 if ((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
612 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
613 if ((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
614 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
615 if ((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
618 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
624 wc
.width
= ev
->width
;
625 wc
.height
= ev
->height
;
626 wc
.border_width
= ev
->border_width
;
627 wc
.sibling
= ev
->above
;
628 wc
.stack_mode
= ev
->detail
;
629 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
639 m
= ecalloc(1, sizeof(Monitor
));
640 m
->tagset
[0] = m
->tagset
[1] = 1;
642 m
->nmaster
= nmaster
;
643 m
->showbar
= showbar
;
645 m
->lt
[0] = &layouts
[0];
646 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
647 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
652 destroynotify(XEvent
*e
)
655 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
657 if ((c
= wintoclient(ev
->window
)))
666 for (tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
671 detachstack(Client
*c
)
675 for (tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
678 if (c
== c
->mon
->sel
) {
679 for (t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
690 if (!(m
= selmon
->next
))
692 } else if (selmon
== mons
)
693 for (m
= mons
; m
->next
; m
= m
->next
);
695 for (m
= mons
; m
->next
!= selmon
; m
= m
->next
);
703 int boxs
= drw
->fonts
->h
/ 9;
704 int boxw
= drw
->fonts
->h
/ 6 + 2;
705 unsigned int i
, occ
= 0, urg
= 0;
711 /* draw status first so it can be overdrawn by tags later */
712 if (m
== selmon
) { /* status is only drawn on selected monitor */
713 drw_setscheme(drw
, scheme
[SchemeNorm
]);
714 tw
= TEXTW(stext
) - lrpad
+ 2; /* 2px right padding */
715 drw_text(drw
, m
->ww
- tw
, 0, tw
, bh
, 0, stext
, 0);
718 for (c
= m
->clients
; c
; c
= c
->next
) {
724 for (i
= 0; i
< LENGTH(tags
); i
++) {
726 drw_setscheme(drw
, scheme
[m
->tagset
[m
->seltags
] & 1 << i
? SchemeSel
: SchemeNorm
]);
727 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, tags
[i
], urg
& 1 << i
);
729 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
,
730 m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
734 w
= TEXTW(m
->ltsymbol
);
735 drw_setscheme(drw
, scheme
[SchemeNorm
]);
736 x
= drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->ltsymbol
, 0);
738 if ((w
= m
->ww
- tw
- x
) > bh
) {
740 drw_setscheme(drw
, scheme
[m
== selmon
? SchemeSel
: SchemeNorm
]);
741 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->sel
->name
, 0);
742 if (m
->sel
->isfloating
)
743 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
, m
->sel
->isfixed
, 0);
745 drw_setscheme(drw
, scheme
[SchemeNorm
]);
746 drw_rect(drw
, x
, 0, w
, bh
, 1, 1);
749 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
);
757 for (m
= mons
; m
; m
= m
->next
)
762 enternotify(XEvent
*e
)
766 XCrossingEvent
*ev
= &e
->xcrossing
;
768 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
770 c
= wintoclient(ev
->window
);
771 m
= c
? c
->mon
: wintomon(ev
->window
);
773 unfocus(selmon
->sel
, 1);
775 } else if (!c
|| c
== selmon
->sel
)
784 XExposeEvent
*ev
= &e
->xexpose
;
786 if (ev
->count
== 0 && (m
= wintomon(ev
->window
)))
793 if (!c
|| !ISVISIBLE(c
))
794 for (c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
795 if (selmon
->sel
&& selmon
->sel
!= c
)
796 unfocus(selmon
->sel
, 0);
798 if (c
->mon
!= selmon
)
805 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
][ColBorder
].pixel
);
808 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
809 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
815 /* there are some broken focus acquiring clients needing extra handling */
819 XFocusChangeEvent
*ev
= &e
->xfocus
;
821 if (selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
822 setfocus(selmon
->sel
);
826 focusmon(const Arg
*arg
)
832 if ((m
= dirtomon(arg
->i
)) == selmon
)
834 unfocus(selmon
->sel
, 0);
840 focusstack(const Arg
*arg
)
842 Client
*c
= NULL
, *i
;
844 if (!selmon
->sel
|| (selmon
->sel
->isfullscreen
&& lockfullscreen
))
847 for (c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
849 for (c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
851 for (i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
855 for (; i
; i
= i
->next
)
866 getatomprop(Client
*c
, Atom prop
)
870 unsigned char *p
= NULL
;
871 Atom da
, atom
= None
;
873 if (XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
874 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
882 getrootptr(int *x
, int *y
)
888 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
896 unsigned char *p
= NULL
;
897 unsigned long n
, extra
;
900 if (XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
901 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
910 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
916 if (!text
|| size
== 0)
919 if (!XGetTextProperty(dpy
, w
, &name
, atom
) || !name
.nitems
)
921 if (name
.encoding
== XA_STRING
) {
922 strncpy(text
, (char *)name
.value
, size
- 1);
923 } else if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
924 strncpy(text
, *list
, size
- 1);
925 XFreeStringList(list
);
927 text
[size
- 1] = '\0';
933 grabbuttons(Client
*c
, int focused
)
938 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
939 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
941 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
942 BUTTONMASK
, GrabModeSync
, GrabModeSync
, None
, None
);
943 for (i
= 0; i
< LENGTH(buttons
); i
++)
944 if (buttons
[i
].click
== ClkClientWin
)
945 for (j
= 0; j
< LENGTH(modifiers
); j
++)
946 XGrabButton(dpy
, buttons
[i
].button
,
947 buttons
[i
].mask
| modifiers
[j
],
948 c
->win
, False
, BUTTONMASK
,
949 GrabModeAsync
, GrabModeSync
, None
, None
);
959 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
962 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
963 for (i
= 0; i
< LENGTH(keys
); i
++)
964 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
965 for (j
= 0; j
< LENGTH(modifiers
); j
++)
966 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
967 True
, GrabModeAsync
, GrabModeAsync
);
972 incnmaster(const Arg
*arg
)
974 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
980 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
)
983 if (unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
984 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
988 #endif /* XINERAMA */
998 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
999 for (i
= 0; i
< LENGTH(keys
); i
++)
1000 if (keysym
== keys
[i
].keysym
1001 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1003 keys
[i
].func(&(keys
[i
].arg
));
1007 killclient(const Arg
*arg
)
1011 if (!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1013 XSetErrorHandler(xerrordummy
);
1014 XSetCloseDownMode(dpy
, DestroyAll
);
1015 XKillClient(dpy
, selmon
->sel
->win
);
1017 XSetErrorHandler(xerror
);
1023 manage(Window w
, XWindowAttributes
*wa
)
1025 Client
*c
, *t
= NULL
;
1026 Window trans
= None
;
1029 c
= ecalloc(1, sizeof(Client
));
1032 c
->x
= c
->oldx
= wa
->x
;
1033 c
->y
= c
->oldy
= wa
->y
;
1034 c
->w
= c
->oldw
= wa
->width
;
1035 c
->h
= c
->oldh
= wa
->height
;
1036 c
->oldbw
= wa
->border_width
;
1039 if (XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1047 if (c
->x
+ WIDTH(c
) > c
->mon
->wx
+ c
->mon
->ww
)
1048 c
->x
= c
->mon
->wx
+ c
->mon
->ww
- WIDTH(c
);
1049 if (c
->y
+ HEIGHT(c
) > c
->mon
->wy
+ c
->mon
->wh
)
1050 c
->y
= c
->mon
->wy
+ c
->mon
->wh
- HEIGHT(c
);
1051 c
->x
= MAX(c
->x
, c
->mon
->wx
);
1052 c
->y
= MAX(c
->y
, c
->mon
->wy
);
1055 wc
.border_width
= c
->bw
;
1056 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1057 XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1058 configure(c
); /* propagates border_width, if size doesn't change */
1059 updatewindowtype(c
);
1062 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1065 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1067 XRaiseWindow(dpy
, c
->win
);
1070 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1071 (unsigned char *) &(c
->win
), 1);
1072 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1073 setclientstate(c
, NormalState
);
1074 if (c
->mon
== selmon
)
1075 unfocus(selmon
->sel
, 0);
1078 XMapWindow(dpy
, c
->win
);
1083 mappingnotify(XEvent
*e
)
1085 XMappingEvent
*ev
= &e
->xmapping
;
1087 XRefreshKeyboardMapping(ev
);
1088 if (ev
->request
== MappingKeyboard
)
1093 maprequest(XEvent
*e
)
1095 static XWindowAttributes wa
;
1096 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1098 if (!XGetWindowAttributes(dpy
, ev
->window
, &wa
) || wa
.override_redirect
)
1100 if (!wintoclient(ev
->window
))
1101 manage(ev
->window
, &wa
);
1110 for (c
= m
->clients
; c
; c
= c
->next
)
1113 if (n
> 0) /* override layout symbol */
1114 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1115 for (c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1116 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, 0);
1120 motionnotify(XEvent
*e
)
1122 static Monitor
*mon
= NULL
;
1124 XMotionEvent
*ev
= &e
->xmotion
;
1126 if (ev
->window
!= root
)
1128 if ((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1129 unfocus(selmon
->sel
, 1);
1137 movemouse(const Arg
*arg
)
1139 int x
, y
, ocx
, ocy
, nx
, ny
;
1145 if (!(c
= selmon
->sel
))
1147 if (c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1152 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1153 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1155 if (!getrootptr(&x
, &y
))
1158 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1160 case ConfigureRequest
:
1163 handler
[ev
.type
](&ev
);
1166 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1168 lasttime
= ev
.xmotion
.time
;
1170 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1171 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1172 if (abs(selmon
->wx
- nx
) < snap
)
1174 else if (abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1175 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1176 if (abs(selmon
->wy
- ny
) < snap
)
1178 else if (abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1179 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1180 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1181 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1182 togglefloating(NULL
);
1183 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1184 resize(c
, nx
, ny
, c
->w
, c
->h
, 1);
1187 } while (ev
.type
!= ButtonRelease
);
1188 XUngrabPointer(dpy
, CurrentTime
);
1189 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1197 nexttiled(Client
*c
)
1199 for (; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1213 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 if (!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1228 (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
== c
->mon
->sel
)
1244 if (ev
->atom
== netatom
[NetWMWindowType
])
1245 updatewindowtype(c
);
1250 quit(const Arg
*arg
)
1256 recttomon(int x
, int y
, int w
, int h
)
1258 Monitor
*m
, *r
= selmon
;
1261 for (m
= mons
; m
; m
= m
->next
)
1262 if ((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1270 resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
)
1272 if (applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1273 resizeclient(c
, x
, y
, w
, h
);
1277 resizeclient(Client
*c
, int x
, int y
, int w
, int h
)
1281 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1282 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1283 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1284 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1285 wc
.border_width
= c
->bw
;
1286 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1292 resizemouse(const Arg
*arg
)
1294 int ocx
, ocy
, nw
, nh
;
1300 if (!(c
= selmon
->sel
))
1302 if (c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1307 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1308 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1310 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1312 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1314 case ConfigureRequest
:
1317 handler
[ev
.type
](&ev
);
1320 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1322 lasttime
= ev
.xmotion
.time
;
1324 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1325 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1326 if (c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1327 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1329 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1330 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1331 togglefloating(NULL
);
1333 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1334 resize(c
, c
->x
, c
->y
, nw
, nh
, 1);
1337 } while (ev
.type
!= ButtonRelease
);
1338 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1339 XUngrabPointer(dpy
, CurrentTime
);
1340 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1341 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1358 if (m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1359 XRaiseWindow(dpy
, m
->sel
->win
);
1360 if (m
->lt
[m
->sellt
]->arrange
) {
1361 wc
.stack_mode
= Below
;
1362 wc
.sibling
= m
->barwin
;
1363 for (c
= m
->stack
; c
; c
= c
->snext
)
1364 if (!c
->isfloating
&& ISVISIBLE(c
)) {
1365 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1366 wc
.sibling
= c
->win
;
1370 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1377 /* main event loop */
1379 while (running
&& !XNextEvent(dpy
, &ev
))
1380 if (handler
[ev
.type
])
1381 handler
[ev
.type
](&ev
); /* call handler */
1387 unsigned int i
, num
;
1388 Window d1
, d2
, *wins
= NULL
;
1389 XWindowAttributes wa
;
1391 if (XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1392 for (i
= 0; i
< num
; i
++) {
1393 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1394 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1396 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1397 manage(wins
[i
], &wa
);
1399 for (i
= 0; i
< num
; i
++) { /* now the transients */
1400 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1402 if (XGetTransientForHint(dpy
, wins
[i
], &d1
)
1403 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1404 manage(wins
[i
], &wa
);
1412 sendmon(Client
*c
, Monitor
*m
)
1420 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1428 setclientstate(Client
*c
, long state
)
1430 long data
[] = { state
, None
};
1432 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1433 PropModeReplace
, (unsigned char *)data
, 2);
1437 sendevent(Client
*c
, Atom proto
)
1444 if (XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1445 while (!exists
&& n
--)
1446 exists
= protocols
[n
] == proto
;
1450 ev
.type
= ClientMessage
;
1451 ev
.xclient
.window
= c
->win
;
1452 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1453 ev
.xclient
.format
= 32;
1454 ev
.xclient
.data
.l
[0] = proto
;
1455 ev
.xclient
.data
.l
[1] = CurrentTime
;
1456 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1464 if (!c
->neverfocus
) {
1465 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1466 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1467 XA_WINDOW
, 32, PropModeReplace
,
1468 (unsigned char *) &(c
->win
), 1);
1470 sendevent(c
, wmatom
[WMTakeFocus
]);
1474 setfullscreen(Client
*c
, int fullscreen
)
1476 if (fullscreen
&& !c
->isfullscreen
) {
1477 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1478 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1479 c
->isfullscreen
= 1;
1480 c
->oldstate
= c
->isfloating
;
1484 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1485 XRaiseWindow(dpy
, c
->win
);
1486 } else if (!fullscreen
&& c
->isfullscreen
){
1487 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1488 PropModeReplace
, (unsigned char*)0, 0);
1489 c
->isfullscreen
= 0;
1490 c
->isfloating
= c
->oldstate
;
1496 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1502 setlayout(const Arg
*arg
)
1504 if (!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1507 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1508 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1515 /* arg > 1.0 will set mfact absolutely */
1517 setmfact(const Arg
*arg
)
1521 if (!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1523 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1524 if (f
< 0.05 || f
> 0.95)
1534 XSetWindowAttributes wa
;
1537 /* clean up any zombies immediately */
1541 screen
= DefaultScreen(dpy
);
1542 sw
= DisplayWidth(dpy
, screen
);
1543 sh
= DisplayHeight(dpy
, screen
);
1544 root
= RootWindow(dpy
, screen
);
1545 drw
= drw_create(dpy
, screen
, root
, sw
, sh
);
1546 if (!drw_fontset_create(drw
, fonts
, LENGTH(fonts
)))
1547 die("no fonts could be loaded.");
1548 lrpad
= drw
->fonts
->h
;
1549 bh
= drw
->fonts
->h
+ 2;
1552 utf8string
= XInternAtom(dpy
, "UTF8_STRING", False
);
1553 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1554 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1555 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1556 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1557 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1558 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1559 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1560 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1561 netatom
[NetWMCheck
] = XInternAtom(dpy
, "_NET_SUPPORTING_WM_CHECK", False
);
1562 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1563 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1564 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1565 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1567 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1568 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1569 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1570 /* init appearance */
1571 scheme
= ecalloc(LENGTH(colors
), sizeof(Clr
*));
1572 for (i
= 0; i
< LENGTH(colors
); i
++)
1573 scheme
[i
] = drw_scm_create(drw
, colors
[i
], 3);
1577 /* supporting window for NetWMCheck */
1578 wmcheckwin
= XCreateSimpleWindow(dpy
, root
, 0, 0, 1, 1, 0, 0, 0);
1579 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1580 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1581 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMName
], utf8string
, 8,
1582 PropModeReplace
, (unsigned char *) "dwm", 3);
1583 XChangeProperty(dpy
, root
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1584 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1585 /* EWMH support per view */
1586 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1587 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1588 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1590 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1591 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1592 |ButtonPressMask
|PointerMotionMask
|EnterWindowMask
1593 |LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1594 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1595 XSelectInput(dpy
, root
, wa
.event_mask
);
1601 seturgent(Client
*c
, int urg
)
1606 if (!(wmh
= XGetWMHints(dpy
, c
->win
)))
1608 wmh
->flags
= urg
? (wmh
->flags
| XUrgencyHint
) : (wmh
->flags
& ~XUrgencyHint
);
1609 XSetWMHints(dpy
, c
->win
, wmh
);
1619 /* show clients top down */
1620 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1621 if ((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1622 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, 0);
1625 /* hide clients bottom up */
1627 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1634 if (signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1635 die("can't install SIGCHLD handler:");
1636 while (0 < waitpid(-1, NULL
, WNOHANG
));
1640 spawn(const Arg
*arg
)
1644 close(ConnectionNumber(dpy
));
1646 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1647 die("dwm: execvp '%s' failed:", ((char **)arg
->v
)[0]);
1654 if (selmon
->sel
&& arg
->ui
& TAGMASK
) {
1655 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1662 tagmon(const Arg
*arg
)
1664 if (!selmon
->sel
|| !mons
->next
)
1666 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1672 unsigned int i
, n
, h
, mw
, my
, ty
;
1675 for (n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1680 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1683 for (i
= my
= ty
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1684 if (i
< m
->nmaster
) {
1685 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
);
1686 resize(c
, m
->wx
, m
->wy
+ my
, mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1687 if (my
+ HEIGHT(c
) < m
->wh
)
1690 h
= (m
->wh
- ty
) / (n
- i
);
1691 resize(c
, m
->wx
+ mw
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1692 if (ty
+ HEIGHT(c
) < m
->wh
)
1698 togglebar(const Arg
*arg
)
1700 selmon
->showbar
= !selmon
->showbar
;
1701 updatebarpos(selmon
);
1702 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1707 togglefloating(const Arg
*arg
)
1711 if (selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1713 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1714 if (selmon
->sel
->isfloating
)
1715 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1716 selmon
->sel
->w
, selmon
->sel
->h
, 0);
1721 toggletag(const Arg
*arg
)
1723 unsigned int newtags
;
1727 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1729 selmon
->sel
->tags
= newtags
;
1736 toggleview(const Arg
*arg
)
1738 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1741 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1748 unfocus(Client
*c
, int setfocus
)
1753 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1755 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1756 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1761 unmanage(Client
*c
, int destroyed
)
1763 Monitor
*m
= c
->mon
;
1769 wc
.border_width
= c
->oldbw
;
1770 XGrabServer(dpy
); /* avoid race conditions */
1771 XSetErrorHandler(xerrordummy
);
1772 XSelectInput(dpy
, c
->win
, NoEventMask
);
1773 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1774 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1775 setclientstate(c
, WithdrawnState
);
1777 XSetErrorHandler(xerror
);
1787 unmapnotify(XEvent
*e
)
1790 XUnmapEvent
*ev
= &e
->xunmap
;
1792 if ((c
= wintoclient(ev
->window
))) {
1794 setclientstate(c
, WithdrawnState
);
1804 XSetWindowAttributes wa
= {
1805 .override_redirect
= True
,
1806 .background_pixmap
= ParentRelative
,
1807 .event_mask
= ButtonPressMask
|ExposureMask
1809 XClassHint ch
= {"dwm", "dwm"};
1810 for (m
= mons
; m
; m
= m
->next
) {
1813 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1814 CopyFromParent
, DefaultVisual(dpy
, screen
),
1815 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1816 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
1817 XMapRaised(dpy
, m
->barwin
);
1818 XSetClassHint(dpy
, m
->barwin
, &ch
);
1823 updatebarpos(Monitor
*m
)
1829 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1830 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1841 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1842 for (m
= mons
; m
; m
= m
->next
)
1843 for (c
= m
->clients
; c
; c
= c
->next
)
1844 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
1845 XA_WINDOW
, 32, PropModeAppend
,
1846 (unsigned char *) &(c
->win
), 1);
1855 if (XineramaIsActive(dpy
)) {
1859 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1860 XineramaScreenInfo
*unique
= NULL
;
1862 for (n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1863 /* only consider unique geometries as separate screens */
1864 unique
= ecalloc(nn
, sizeof(XineramaScreenInfo
));
1865 for (i
= 0, j
= 0; i
< nn
; i
++)
1866 if (isuniquegeom(unique
, j
, &info
[i
]))
1867 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1871 /* new monitors if nn > n */
1872 for (i
= n
; i
< nn
; i
++) {
1873 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1875 m
->next
= createmon();
1879 for (i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1881 || unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1882 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
)
1886 m
->mx
= m
->wx
= unique
[i
].x_org
;
1887 m
->my
= m
->wy
= unique
[i
].y_org
;
1888 m
->mw
= m
->ww
= unique
[i
].width
;
1889 m
->mh
= m
->wh
= unique
[i
].height
;
1892 /* removed monitors if n > nn */
1893 for (i
= nn
; i
< n
; i
++) {
1894 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1895 while ((c
= m
->clients
)) {
1897 m
->clients
= c
->next
;
1909 #endif /* XINERAMA */
1910 { /* default monitor setup */
1913 if (mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1915 mons
->mw
= mons
->ww
= sw
;
1916 mons
->mh
= mons
->wh
= sh
;
1922 selmon
= wintomon(root
);
1928 updatenumlockmask(void)
1931 XModifierKeymap
*modmap
;
1934 modmap
= XGetModifierMapping(dpy
);
1935 for (i
= 0; i
< 8; i
++)
1936 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1937 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1938 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1939 numlockmask
= (1 << i
);
1940 XFreeModifiermap(modmap
);
1944 updatesizehints(Client
*c
)
1949 if (!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1950 /* size is uninitialized, ensure that size.flags aren't used */
1952 if (size
.flags
& PBaseSize
) {
1953 c
->basew
= size
.base_width
;
1954 c
->baseh
= size
.base_height
;
1955 } else if (size
.flags
& PMinSize
) {
1956 c
->basew
= size
.min_width
;
1957 c
->baseh
= size
.min_height
;
1959 c
->basew
= c
->baseh
= 0;
1960 if (size
.flags
& PResizeInc
) {
1961 c
->incw
= size
.width_inc
;
1962 c
->inch
= size
.height_inc
;
1964 c
->incw
= c
->inch
= 0;
1965 if (size
.flags
& PMaxSize
) {
1966 c
->maxw
= size
.max_width
;
1967 c
->maxh
= size
.max_height
;
1969 c
->maxw
= c
->maxh
= 0;
1970 if (size
.flags
& PMinSize
) {
1971 c
->minw
= size
.min_width
;
1972 c
->minh
= size
.min_height
;
1973 } else if (size
.flags
& PBaseSize
) {
1974 c
->minw
= size
.base_width
;
1975 c
->minh
= size
.base_height
;
1977 c
->minw
= c
->minh
= 0;
1978 if (size
.flags
& PAspect
) {
1979 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1980 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1982 c
->maxa
= c
->mina
= 0.0;
1983 c
->isfixed
= (c
->maxw
&& c
->maxh
&& c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1990 if (!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1991 strcpy(stext
, "dwm-"VERSION
);
1996 updatetitle(Client
*c
)
1998 if (!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1999 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
2000 if (c
->name
[0] == '\0') /* hack to mark broken clients */
2001 strcpy(c
->name
, broken
);
2005 updatewindowtype(Client
*c
)
2007 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
2008 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
2010 if (state
== netatom
[NetWMFullscreen
])
2011 setfullscreen(c
, 1);
2012 if (wtype
== netatom
[NetWMWindowTypeDialog
])
2017 updatewmhints(Client
*c
)
2021 if ((wmh
= XGetWMHints(dpy
, c
->win
))) {
2022 if (c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2023 wmh
->flags
&= ~XUrgencyHint
;
2024 XSetWMHints(dpy
, c
->win
, wmh
);
2026 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? 1 : 0;
2027 if (wmh
->flags
& InputHint
)
2028 c
->neverfocus
= !wmh
->input
;
2036 view(const Arg
*arg
)
2038 if ((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2040 selmon
->seltags
^= 1; /* toggle sel tagset */
2041 if (arg
->ui
& TAGMASK
)
2042 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2048 wintoclient(Window w
)
2053 for (m
= mons
; m
; m
= m
->next
)
2054 for (c
= m
->clients
; c
; c
= c
->next
)
2067 if (w
== root
&& getrootptr(&x
, &y
))
2068 return recttomon(x
, y
, 1, 1);
2069 for (m
= mons
; m
; m
= m
->next
)
2072 if ((c
= wintoclient(w
)))
2077 /* There's no way to check accesses to destroyed windows, thus those cases are
2078 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2079 * default error handler, which may call exit. */
2081 xerror(Display
*dpy
, XErrorEvent
*ee
)
2083 if (ee
->error_code
== BadWindow
2084 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2085 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2086 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2087 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2088 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2089 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2090 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2091 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2093 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2094 ee
->request_code
, ee
->error_code
);
2095 return xerrorxlib(dpy
, ee
); /* may call exit */
2099 xerrordummy(Display
*dpy
, XErrorEvent
*ee
)
2104 /* Startup Error handler to check if another window manager
2105 * is already running. */
2107 xerrorstart(Display
*dpy
, XErrorEvent
*ee
)
2109 die("dwm: another window manager is already running");
2114 zoom(const Arg
*arg
)
2116 Client
*c
= selmon
->sel
;
2118 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| !c
|| c
->isfloating
)
2120 if (c
== nexttiled(selmon
->clients
) && !(c
= nexttiled(c
->next
)))
2126 main(int argc
, char *argv
[])
2128 if (argc
== 2 && !strcmp("-v", argv
[1]))
2131 die("usage: dwm [-v]");
2132 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2133 fputs("warning: no locale support\n", stderr
);
2134 if (!(dpy
= XOpenDisplay(NULL
)))
2135 die("dwm: cannot open display");
2139 if (pledge("stdio rpath proc exec", NULL
) == -1)
2141 #endif /* __OpenBSD__ */
2146 return EXIT_SUCCESS
;