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
;
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 int getrootptr(int *x
, int *y
);
173 static long getstate(Window w
);
174 static int gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
175 static void grabbuttons(Client
*c
, int focused
);
176 static void grabkeys(void);
177 static void incnmaster(const Arg
*arg
);
178 static void keypress(XEvent
*e
);
179 static void killclient(const Arg
*arg
);
180 static void manage(Window w
, XWindowAttributes
*wa
);
181 static void mappingnotify(XEvent
*e
);
182 static void maprequest(XEvent
*e
);
183 static void monocle(Monitor
*m
);
184 static void motionnotify(XEvent
*e
);
185 static void movemouse(const Arg
*arg
);
186 static Client
*nexttiled(Client
*c
);
187 static void pop(Client
*);
188 static void propertynotify(XEvent
*e
);
189 static void quit(const Arg
*arg
);
190 static Monitor
*recttomon(int x
, int y
, int w
, int h
);
191 static void resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
);
192 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
193 static void resizemouse(const Arg
*arg
);
194 static void restack(Monitor
*m
);
195 static void run(void);
196 static void scan(void);
197 static int sendevent(Client
*c
, Atom proto
);
198 static void sendmon(Client
*c
, Monitor
*m
);
199 static void setclientstate(Client
*c
, long state
);
200 static void setfocus(Client
*c
);
201 static void setfullscreen(Client
*c
, int fullscreen
);
202 static void setlayout(const Arg
*arg
);
203 static void setmfact(const Arg
*arg
);
204 static void setup(void);
205 static void seturgent(Client
*c
, int urg
);
206 static void showhide(Client
*c
);
207 static void sigchld(int unused
);
208 static void spawn(const Arg
*arg
);
209 static void tag(const Arg
*arg
);
210 static void tagmon(const Arg
*arg
);
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
, int setfocus
);
217 static void unmanage(Client
*c
, int destroyed
);
218 static void unmapnotify(XEvent
*e
);
219 static void updatebarpos(Monitor
*m
);
220 static void updatebars(void);
221 static void updateclientlist(void);
222 static int updategeom(void);
223 static void updatenumlockmask(void);
224 static void updatesizehints(Client
*c
);
225 static void updatestatus(void);
226 static void updatetitle(Client
*c
);
227 static void updatewindowtype(Client
*c
);
228 static void updatewmhints(Client
*c
);
229 static void view(const Arg
*arg
);
230 static Client
*wintoclient(Window w
);
231 static Monitor
*wintomon(Window w
);
232 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
233 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
234 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
235 static void zoom(const Arg
*arg
);
238 static const char broken
[] = "broken";
239 static char stext
[256];
241 static int sw
, sh
; /* X display screen geometry width, height */
242 static int bh
, blw
= 0; /* bar geometry */
243 static int lrpad
; /* sum of left and right padding for text */
244 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
245 static unsigned int numlockmask
= 0;
246 static void (*handler
[LASTEvent
]) (XEvent
*) = {
247 [ButtonPress
] = buttonpress
,
248 [ClientMessage
] = clientmessage
,
249 [ConfigureRequest
] = configurerequest
,
250 [ConfigureNotify
] = configurenotify
,
251 [DestroyNotify
] = destroynotify
,
252 [EnterNotify
] = enternotify
,
255 [KeyPress
] = keypress
,
256 [MappingNotify
] = mappingnotify
,
257 [MapRequest
] = maprequest
,
258 [MotionNotify
] = motionnotify
,
259 [PropertyNotify
] = propertynotify
,
260 [UnmapNotify
] = unmapnotify
262 static Atom wmatom
[WMLast
], netatom
[NetLast
];
263 static int running
= 1;
264 static Cur
*cursor
[CurLast
];
268 static Monitor
*mons
, *selmon
;
269 static Window root
, wmcheckwin
;
271 /* configuration, allows nested code to access above variables */
274 /* compile-time check if all tags fit into an unsigned int bit array. */
275 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
277 /* function implementations */
279 applyrules(Client
*c
)
281 const char *class, *instance
;
285 XClassHint ch
= { NULL
, NULL
};
290 XGetClassHint(dpy
, c
->win
, &ch
);
291 class = ch
.res_class
? ch
.res_class
: broken
;
292 instance
= ch
.res_name
? ch
.res_name
: broken
;
294 for (i
= 0; i
< LENGTH(rules
); i
++) {
296 if ((!r
->title
|| strstr(c
->name
, r
->title
))
297 && (!r
->class || strstr(class, r
->class))
298 && (!r
->instance
|| strstr(instance
, r
->instance
)))
300 c
->isfloating
= r
->isfloating
;
302 for (m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
311 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
315 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
)
320 /* set minimum possible */
328 if (*x
+ *w
+ 2 * c
->bw
< 0)
330 if (*y
+ *h
+ 2 * c
->bw
< 0)
333 if (*x
>= m
->wx
+ m
->ww
)
334 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
335 if (*y
>= m
->wy
+ m
->wh
)
336 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
337 if (*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
339 if (*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
346 if (resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
347 /* see last two sentences in ICCCM 4.1.2.3 */
348 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
349 if (!baseismin
) { /* temporarily remove base dimensions */
353 /* adjust for aspect limits */
354 if (c
->mina
> 0 && c
->maxa
> 0) {
355 if (c
->maxa
< (float)*w
/ *h
)
356 *w
= *h
* c
->maxa
+ 0.5;
357 else if (c
->mina
< (float)*h
/ *w
)
358 *h
= *w
* c
->mina
+ 0.5;
360 if (baseismin
) { /* increment calculation requires this */
364 /* adjust for increment value */
369 /* restore base dimensions */
370 *w
= MAX(*w
+ c
->basew
, c
->minw
);
371 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
373 *w
= MIN(*w
, c
->maxw
);
375 *h
= MIN(*h
, c
->maxh
);
377 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
385 else for (m
= mons
; m
; m
= m
->next
)
390 } else for (m
= mons
; m
; m
= m
->next
)
395 arrangemon(Monitor
*m
)
397 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
398 if (m
->lt
[m
->sellt
]->arrange
)
399 m
->lt
[m
->sellt
]->arrange(m
);
405 c
->next
= c
->mon
->clients
;
410 attachstack(Client
*c
)
412 c
->snext
= c
->mon
->stack
;
417 buttonpress(XEvent
*e
)
419 unsigned int i
, x
, click
;
423 XButtonPressedEvent
*ev
= &e
->xbutton
;
426 /* focus monitor if necessary */
427 if ((m
= wintomon(ev
->window
)) && m
!= selmon
) {
428 unfocus(selmon
->sel
, 1);
432 if (ev
->window
== selmon
->barwin
) {
436 while (ev
->x
>= x
&& ++i
< LENGTH(tags
));
437 if (i
< LENGTH(tags
)) {
440 } else if (ev
->x
< x
+ blw
)
442 else if (ev
->x
> selmon
->ww
- TEXTW(stext
))
443 click
= ClkStatusText
;
446 } else if ((c
= wintoclient(ev
->window
))) {
449 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
450 click
= ClkClientWin
;
452 for (i
= 0; i
< LENGTH(buttons
); i
++)
453 if (click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
454 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
455 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
461 xerrorxlib
= XSetErrorHandler(xerrorstart
);
462 /* this causes an error if some other window manager is running */
463 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
465 XSetErrorHandler(xerror
);
473 Layout foo
= { "", NULL
};
478 selmon
->lt
[selmon
->sellt
] = &foo
;
479 for (m
= mons
; m
; m
= m
->next
)
481 unmanage(m
->stack
, 0);
482 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
485 for (i
= 0; i
< CurLast
; i
++)
486 drw_cur_free(drw
, cursor
[i
]);
487 for (i
= 0; i
< LENGTH(colors
); i
++)
489 XDestroyWindow(dpy
, wmcheckwin
);
492 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
493 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
497 cleanupmon(Monitor
*mon
)
504 for (m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
507 XUnmapWindow(dpy
, mon
->barwin
);
508 XDestroyWindow(dpy
, mon
->barwin
);
513 clientmessage(XEvent
*e
)
515 XClientMessageEvent
*cme
= &e
->xclient
;
516 Client
*c
= wintoclient(cme
->window
);
520 if (cme
->message_type
== netatom
[NetWMState
]) {
521 if (cme
->data
.l
[1] == netatom
[NetWMFullscreen
]
522 || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
523 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
524 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
525 } else if (cme
->message_type
== netatom
[NetActiveWindow
]) {
526 if (c
!= selmon
->sel
&& !c
->isurgent
)
536 ce
.type
= ConfigureNotify
;
544 ce
.border_width
= c
->bw
;
546 ce
.override_redirect
= False
;
547 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
551 configurenotify(XEvent
*e
)
555 XConfigureEvent
*ev
= &e
->xconfigure
;
558 /* TODO: updategeom handling sucks, needs to be simplified */
559 if (ev
->window
== root
) {
560 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
563 if (updategeom() || dirty
) {
564 drw_resize(drw
, sw
, bh
);
566 for (m
= mons
; m
; m
= m
->next
) {
567 for (c
= m
->clients
; c
; c
= c
->next
)
569 resizeclient(c
, m
->mx
, m
->my
, m
->mw
, m
->mh
);
570 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
579 configurerequest(XEvent
*e
)
583 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
586 if ((c
= wintoclient(ev
->window
))) {
587 if (ev
->value_mask
& CWBorderWidth
)
588 c
->bw
= ev
->border_width
;
589 else if (c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
591 if (ev
->value_mask
& CWX
) {
593 c
->x
= m
->mx
+ ev
->x
;
595 if (ev
->value_mask
& CWY
) {
597 c
->y
= m
->my
+ ev
->y
;
599 if (ev
->value_mask
& CWWidth
) {
603 if (ev
->value_mask
& CWHeight
) {
607 if ((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
608 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
609 if ((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
610 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
611 if ((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
614 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
620 wc
.width
= ev
->width
;
621 wc
.height
= ev
->height
;
622 wc
.border_width
= ev
->border_width
;
623 wc
.sibling
= ev
->above
;
624 wc
.stack_mode
= ev
->detail
;
625 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
635 m
= ecalloc(1, sizeof(Monitor
));
636 m
->tagset
[0] = m
->tagset
[1] = 1;
638 m
->nmaster
= nmaster
;
639 m
->showbar
= showbar
;
641 m
->lt
[0] = &layouts
[0];
642 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
643 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
648 destroynotify(XEvent
*e
)
651 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
653 if ((c
= wintoclient(ev
->window
)))
662 for (tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
667 detachstack(Client
*c
)
671 for (tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
674 if (c
== c
->mon
->sel
) {
675 for (t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
686 if (!(m
= selmon
->next
))
688 } else if (selmon
== mons
)
689 for (m
= mons
; m
->next
; m
= m
->next
);
691 for (m
= mons
; m
->next
!= selmon
; m
= m
->next
);
699 int boxs
= drw
->fonts
->h
/ 9;
700 int boxw
= drw
->fonts
->h
/ 6 + 2;
701 unsigned int i
, occ
= 0, urg
= 0;
704 /* draw status first so it can be overdrawn by tags later */
705 if (m
== selmon
) { /* status is only drawn on selected monitor */
706 drw_setscheme(drw
, scheme
[SchemeNorm
]);
707 sw
= TEXTW(stext
) - lrpad
+ 2; /* 2px right padding */
708 drw_text(drw
, m
->ww
- sw
, 0, sw
, bh
, 0, stext
, 0);
711 for (c
= m
->clients
; c
; c
= c
->next
) {
717 for (i
= 0; i
< LENGTH(tags
); i
++) {
719 drw_setscheme(drw
, scheme
[m
->tagset
[m
->seltags
] & 1 << i
? SchemeSel
: SchemeNorm
]);
720 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, tags
[i
], urg
& 1 << i
);
722 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
,
723 m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
727 w
= blw
= TEXTW(m
->ltsymbol
);
728 drw_setscheme(drw
, scheme
[SchemeNorm
]);
729 x
= drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->ltsymbol
, 0);
731 if ((w
= m
->ww
- sw
- x
) > bh
) {
733 drw_setscheme(drw
, scheme
[m
== selmon
? SchemeSel
: SchemeNorm
]);
734 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->sel
->name
, 0);
735 if (m
->sel
->isfloating
)
736 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
, m
->sel
->isfixed
, 0);
738 drw_setscheme(drw
, scheme
[SchemeNorm
]);
739 drw_rect(drw
, x
, 0, w
, bh
, 1, 1);
742 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
);
750 for (m
= mons
; m
; m
= m
->next
)
755 enternotify(XEvent
*e
)
759 XCrossingEvent
*ev
= &e
->xcrossing
;
761 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
763 c
= wintoclient(ev
->window
);
764 m
= c
? c
->mon
: wintomon(ev
->window
);
766 unfocus(selmon
->sel
, 1);
768 } else if (!c
|| c
== selmon
->sel
)
777 XExposeEvent
*ev
= &e
->xexpose
;
779 if (ev
->count
== 0 && (m
= wintomon(ev
->window
)))
786 if (!c
|| !ISVISIBLE(c
))
787 for (c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
788 if (selmon
->sel
&& selmon
->sel
!= c
)
789 unfocus(selmon
->sel
, 0);
791 if (c
->mon
!= selmon
)
798 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
][ColBorder
].pixel
);
801 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
802 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
808 /* there are some broken focus acquiring clients needing extra handling */
812 XFocusChangeEvent
*ev
= &e
->xfocus
;
814 if (selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
815 setfocus(selmon
->sel
);
819 focusmon(const Arg
*arg
)
825 if ((m
= dirtomon(arg
->i
)) == selmon
)
827 unfocus(selmon
->sel
, 0);
833 focusstack(const Arg
*arg
)
835 Client
*c
= NULL
, *i
;
840 for (c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
842 for (c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
844 for (i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
848 for (; i
; i
= i
->next
)
859 getatomprop(Client
*c
, Atom prop
)
863 unsigned char *p
= NULL
;
864 Atom da
, atom
= None
;
866 if (XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
867 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
875 getrootptr(int *x
, int *y
)
881 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
889 unsigned char *p
= NULL
;
890 unsigned long n
, extra
;
893 if (XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
894 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
903 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
909 if (!text
|| size
== 0)
912 if (!XGetTextProperty(dpy
, w
, &name
, atom
) || !name
.nitems
)
914 if (name
.encoding
== XA_STRING
)
915 strncpy(text
, (char *)name
.value
, size
- 1);
917 if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
918 strncpy(text
, *list
, size
- 1);
919 XFreeStringList(list
);
922 text
[size
- 1] = '\0';
928 grabbuttons(Client
*c
, int focused
)
933 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
934 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
936 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
937 BUTTONMASK
, GrabModeSync
, GrabModeSync
, None
, None
);
938 for (i
= 0; i
< LENGTH(buttons
); i
++)
939 if (buttons
[i
].click
== ClkClientWin
)
940 for (j
= 0; j
< LENGTH(modifiers
); j
++)
941 XGrabButton(dpy
, buttons
[i
].button
,
942 buttons
[i
].mask
| modifiers
[j
],
943 c
->win
, False
, BUTTONMASK
,
944 GrabModeAsync
, GrabModeSync
, None
, None
);
954 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
957 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
958 for (i
= 0; i
< LENGTH(keys
); i
++)
959 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
960 for (j
= 0; j
< LENGTH(modifiers
); j
++)
961 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
962 True
, GrabModeAsync
, GrabModeAsync
);
967 incnmaster(const Arg
*arg
)
969 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
975 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
)
978 if (unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
979 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
983 #endif /* XINERAMA */
993 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
994 for (i
= 0; i
< LENGTH(keys
); i
++)
995 if (keysym
== keys
[i
].keysym
996 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
998 keys
[i
].func(&(keys
[i
].arg
));
1002 killclient(const Arg
*arg
)
1006 if (!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1008 XSetErrorHandler(xerrordummy
);
1009 XSetCloseDownMode(dpy
, DestroyAll
);
1010 XKillClient(dpy
, selmon
->sel
->win
);
1012 XSetErrorHandler(xerror
);
1018 manage(Window w
, XWindowAttributes
*wa
)
1020 Client
*c
, *t
= NULL
;
1021 Window trans
= None
;
1024 c
= ecalloc(1, sizeof(Client
));
1027 c
->x
= c
->oldx
= wa
->x
;
1028 c
->y
= c
->oldy
= wa
->y
;
1029 c
->w
= c
->oldw
= wa
->width
;
1030 c
->h
= c
->oldh
= wa
->height
;
1031 c
->oldbw
= wa
->border_width
;
1034 if (XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1042 if (c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1043 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1044 if (c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1045 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1046 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1047 /* only fix client y-offset, if the client center might cover the bar */
1048 c
->y
= MAX(c
->y
, ((c
->mon
->by
== c
->mon
->my
) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1049 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1052 wc
.border_width
= c
->bw
;
1053 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1054 XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1055 configure(c
); /* propagates border_width, if size doesn't change */
1056 updatewindowtype(c
);
1059 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1062 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1064 XRaiseWindow(dpy
, c
->win
);
1067 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1068 (unsigned char *) &(c
->win
), 1);
1069 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1070 setclientstate(c
, NormalState
);
1071 if (c
->mon
== selmon
)
1072 unfocus(selmon
->sel
, 0);
1075 XMapWindow(dpy
, c
->win
);
1080 mappingnotify(XEvent
*e
)
1082 XMappingEvent
*ev
= &e
->xmapping
;
1084 XRefreshKeyboardMapping(ev
);
1085 if (ev
->request
== MappingKeyboard
)
1090 maprequest(XEvent
*e
)
1092 static XWindowAttributes wa
;
1093 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1095 if (!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1097 if (wa
.override_redirect
)
1099 if (!wintoclient(ev
->window
))
1100 manage(ev
->window
, &wa
);
1109 for (c
= m
->clients
; c
; c
= c
->next
)
1112 if (n
> 0) /* override layout symbol */
1113 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1114 for (c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1115 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, 0);
1119 motionnotify(XEvent
*e
)
1121 static Monitor
*mon
= NULL
;
1123 XMotionEvent
*ev
= &e
->xmotion
;
1125 if (ev
->window
!= root
)
1127 if ((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1128 unfocus(selmon
->sel
, 1);
1136 movemouse(const Arg
*arg
)
1138 int x
, y
, ocx
, ocy
, nx
, ny
;
1144 if (!(c
= selmon
->sel
))
1146 if (c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1151 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1152 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1154 if (!getrootptr(&x
, &y
))
1157 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1159 case ConfigureRequest
:
1162 handler
[ev
.type
](&ev
);
1165 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1167 lasttime
= ev
.xmotion
.time
;
1169 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1170 ny
= ocy
+ (ev
.xmotion
.y
- y
);
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
);
1182 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1183 resize(c
, nx
, ny
, c
->w
, c
->h
, 1);
1186 } while (ev
.type
!= ButtonRelease
);
1187 XUngrabPointer(dpy
, CurrentTime
);
1188 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1196 nexttiled(Client
*c
)
1198 for (; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1212 propertynotify(XEvent
*e
)
1216 XPropertyEvent
*ev
= &e
->xproperty
;
1218 if ((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1220 else if (ev
->state
== PropertyDelete
)
1221 return; /* ignore */
1222 else if ((c
= wintoclient(ev
->window
))) {
1225 case XA_WM_TRANSIENT_FOR
:
1226 if (!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1227 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1230 case XA_WM_NORMAL_HINTS
:
1238 if (ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1240 if (c
== c
->mon
->sel
)
1243 if (ev
->atom
== netatom
[NetWMWindowType
])
1244 updatewindowtype(c
);
1249 quit(const Arg
*arg
)
1255 recttomon(int x
, int y
, int w
, int h
)
1257 Monitor
*m
, *r
= selmon
;
1260 for (m
= mons
; m
; m
= m
->next
)
1261 if ((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1269 resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
)
1271 if (applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1272 resizeclient(c
, x
, y
, w
, h
);
1276 resizeclient(Client
*c
, int x
, int y
, int w
, int h
)
1280 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1281 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1282 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1283 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1284 wc
.border_width
= c
->bw
;
1285 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1291 resizemouse(const Arg
*arg
)
1293 int ocx
, ocy
, nw
, nh
;
1299 if (!(c
= selmon
->sel
))
1301 if (c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1306 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1307 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1309 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1311 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1313 case ConfigureRequest
:
1316 handler
[ev
.type
](&ev
);
1319 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1321 lasttime
= ev
.xmotion
.time
;
1323 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1324 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1325 if (c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1326 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1328 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1329 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1330 togglefloating(NULL
);
1332 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1333 resize(c
, c
->x
, c
->y
, nw
, nh
, 1);
1336 } while (ev
.type
!= ButtonRelease
);
1337 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1338 XUngrabPointer(dpy
, CurrentTime
);
1339 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1340 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1357 if (m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1358 XRaiseWindow(dpy
, m
->sel
->win
);
1359 if (m
->lt
[m
->sellt
]->arrange
) {
1360 wc
.stack_mode
= Below
;
1361 wc
.sibling
= m
->barwin
;
1362 for (c
= m
->stack
; c
; c
= c
->snext
)
1363 if (!c
->isfloating
&& ISVISIBLE(c
)) {
1364 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1365 wc
.sibling
= c
->win
;
1369 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1376 /* main event loop */
1378 while (running
&& !XNextEvent(dpy
, &ev
))
1379 if (handler
[ev
.type
])
1380 handler
[ev
.type
](&ev
); /* call handler */
1386 unsigned int i
, num
;
1387 Window d1
, d2
, *wins
= NULL
;
1388 XWindowAttributes wa
;
1390 if (XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1391 for (i
= 0; i
< num
; i
++) {
1392 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1393 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1395 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1396 manage(wins
[i
], &wa
);
1398 for (i
= 0; i
< num
; i
++) { /* now the transients */
1399 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1401 if (XGetTransientForHint(dpy
, wins
[i
], &d1
)
1402 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1403 manage(wins
[i
], &wa
);
1411 sendmon(Client
*c
, Monitor
*m
)
1419 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1427 setclientstate(Client
*c
, long state
)
1429 long data
[] = { state
, None
};
1431 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1432 PropModeReplace
, (unsigned char *)data
, 2);
1436 sendevent(Client
*c
, Atom proto
)
1443 if (XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1444 while (!exists
&& n
--)
1445 exists
= protocols
[n
] == proto
;
1449 ev
.type
= ClientMessage
;
1450 ev
.xclient
.window
= c
->win
;
1451 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1452 ev
.xclient
.format
= 32;
1453 ev
.xclient
.data
.l
[0] = proto
;
1454 ev
.xclient
.data
.l
[1] = CurrentTime
;
1455 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1463 if (!c
->neverfocus
) {
1464 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1465 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1466 XA_WINDOW
, 32, PropModeReplace
,
1467 (unsigned char *) &(c
->win
), 1);
1469 sendevent(c
, wmatom
[WMTakeFocus
]);
1473 setfullscreen(Client
*c
, int fullscreen
)
1475 if (fullscreen
&& !c
->isfullscreen
) {
1476 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1477 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1478 c
->isfullscreen
= 1;
1479 c
->oldstate
= c
->isfloating
;
1483 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1484 XRaiseWindow(dpy
, c
->win
);
1485 } else if (!fullscreen
&& c
->isfullscreen
){
1486 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1487 PropModeReplace
, (unsigned char*)0, 0);
1488 c
->isfullscreen
= 0;
1489 c
->isfloating
= c
->oldstate
;
1495 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1501 setlayout(const Arg
*arg
)
1503 if (!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1506 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1507 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1514 /* arg > 1.0 will set mfact absolutely */
1516 setmfact(const Arg
*arg
)
1520 if (!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1522 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1523 if (f
< 0.1 || f
> 0.9)
1533 XSetWindowAttributes wa
;
1536 /* clean up any zombies immediately */
1540 screen
= DefaultScreen(dpy
);
1541 sw
= DisplayWidth(dpy
, screen
);
1542 sh
= DisplayHeight(dpy
, screen
);
1543 root
= RootWindow(dpy
, screen
);
1544 drw
= drw_create(dpy
, screen
, root
, sw
, sh
);
1545 if (!drw_fontset_create(drw
, fonts
, LENGTH(fonts
)))
1546 die("no fonts could be loaded.");
1547 lrpad
= drw
->fonts
->h
;
1548 bh
= drw
->fonts
->h
+ 2;
1551 utf8string
= XInternAtom(dpy
, "UTF8_STRING", False
);
1552 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1553 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1554 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1555 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1556 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1557 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1558 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1559 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1560 netatom
[NetWMCheck
] = XInternAtom(dpy
, "_NET_SUPPORTING_WM_CHECK", False
);
1561 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1562 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1563 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1564 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1566 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1567 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1568 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1569 /* init appearance */
1570 scheme
= ecalloc(LENGTH(colors
), sizeof(Clr
*));
1571 for (i
= 0; i
< LENGTH(colors
); i
++)
1572 scheme
[i
] = drw_scm_create(drw
, colors
[i
], 3);
1576 /* supporting window for NetWMCheck */
1577 wmcheckwin
= XCreateSimpleWindow(dpy
, root
, 0, 0, 1, 1, 0, 0, 0);
1578 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1579 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1580 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMName
], utf8string
, 8,
1581 PropModeReplace
, (unsigned char *) "dwm", 3);
1582 XChangeProperty(dpy
, root
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1583 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1584 /* EWMH support per view */
1585 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1586 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1587 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1589 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1590 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1591 |ButtonPressMask
|PointerMotionMask
|EnterWindowMask
1592 |LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1593 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1594 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
)
1642 if (arg
->v
== dmenucmd
)
1643 dmenumon
[0] = '0' + selmon
->num
;
1646 close(ConnectionNumber(dpy
));
1648 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1649 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1658 if (selmon
->sel
&& arg
->ui
& TAGMASK
) {
1659 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1666 tagmon(const Arg
*arg
)
1668 if (!selmon
->sel
|| !mons
->next
)
1670 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1676 unsigned int i
, n
, h
, mw
, my
, ty
;
1679 for (n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1684 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1687 for (i
= my
= ty
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1688 if (i
< m
->nmaster
) {
1689 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
);
1690 resize(c
, m
->wx
, m
->wy
+ my
, mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1693 h
= (m
->wh
- ty
) / (n
- i
);
1694 resize(c
, m
->wx
+ mw
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1700 togglebar(const Arg
*arg
)
1702 selmon
->showbar
= !selmon
->showbar
;
1703 updatebarpos(selmon
);
1704 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1709 togglefloating(const Arg
*arg
)
1713 if (selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1715 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1716 if (selmon
->sel
->isfloating
)
1717 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1718 selmon
->sel
->w
, selmon
->sel
->h
, 0);
1723 toggletag(const Arg
*arg
)
1725 unsigned int newtags
;
1729 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1731 selmon
->sel
->tags
= newtags
;
1738 toggleview(const Arg
*arg
)
1740 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1743 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1750 unfocus(Client
*c
, int setfocus
)
1755 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1757 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1758 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1763 unmanage(Client
*c
, int destroyed
)
1765 Monitor
*m
= c
->mon
;
1771 wc
.border_width
= c
->oldbw
;
1772 XGrabServer(dpy
); /* avoid race conditions */
1773 XSetErrorHandler(xerrordummy
);
1774 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1775 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1776 setclientstate(c
, WithdrawnState
);
1778 XSetErrorHandler(xerror
);
1788 unmapnotify(XEvent
*e
)
1791 XUnmapEvent
*ev
= &e
->xunmap
;
1793 if ((c
= wintoclient(ev
->window
))) {
1795 setclientstate(c
, WithdrawnState
);
1805 XSetWindowAttributes wa
= {
1806 .override_redirect
= True
,
1807 .background_pixmap
= ParentRelative
,
1808 .event_mask
= ButtonPressMask
|ExposureMask
1810 XClassHint ch
= {"dwm", "dwm"};
1811 for (m
= mons
; m
; m
= m
->next
) {
1814 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1815 CopyFromParent
, DefaultVisual(dpy
, screen
),
1816 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1817 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
1818 XMapRaised(dpy
, m
->barwin
);
1819 XSetClassHint(dpy
, m
->barwin
, &ch
);
1824 updatebarpos(Monitor
*m
)
1830 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1831 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1842 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1843 for (m
= mons
; m
; m
= m
->next
)
1844 for (c
= m
->clients
; c
; c
= c
->next
)
1845 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
1846 XA_WINDOW
, 32, PropModeAppend
,
1847 (unsigned char *) &(c
->win
), 1);
1856 if (XineramaIsActive(dpy
)) {
1860 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1861 XineramaScreenInfo
*unique
= NULL
;
1863 for (n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1864 /* only consider unique geometries as separate screens */
1865 unique
= ecalloc(nn
, sizeof(XineramaScreenInfo
));
1866 for (i
= 0, j
= 0; i
< nn
; i
++)
1867 if (isuniquegeom(unique
, j
, &info
[i
]))
1868 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1871 if (n
<= nn
) { /* new monitors available */
1872 for (i
= 0; i
< (nn
- n
); 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 } else { /* less monitors available nn < n */
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
;
1910 #endif /* XINERAMA */
1911 { /* default monitor setup */
1914 if (mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1916 mons
->mw
= mons
->ww
= sw
;
1917 mons
->mh
= mons
->wh
= sh
;
1923 selmon
= wintomon(root
);
1929 updatenumlockmask(void)
1932 XModifierKeymap
*modmap
;
1935 modmap
= XGetModifierMapping(dpy
);
1936 for (i
= 0; i
< 8; i
++)
1937 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1938 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1939 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1940 numlockmask
= (1 << i
);
1941 XFreeModifiermap(modmap
);
1945 updatesizehints(Client
*c
)
1950 if (!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1951 /* size is uninitialized, ensure that size.flags aren't used */
1953 if (size
.flags
& PBaseSize
) {
1954 c
->basew
= size
.base_width
;
1955 c
->baseh
= size
.base_height
;
1956 } else if (size
.flags
& PMinSize
) {
1957 c
->basew
= size
.min_width
;
1958 c
->baseh
= size
.min_height
;
1960 c
->basew
= c
->baseh
= 0;
1961 if (size
.flags
& PResizeInc
) {
1962 c
->incw
= size
.width_inc
;
1963 c
->inch
= size
.height_inc
;
1965 c
->incw
= c
->inch
= 0;
1966 if (size
.flags
& PMaxSize
) {
1967 c
->maxw
= size
.max_width
;
1968 c
->maxh
= size
.max_height
;
1970 c
->maxw
= c
->maxh
= 0;
1971 if (size
.flags
& PMinSize
) {
1972 c
->minw
= size
.min_width
;
1973 c
->minh
= size
.min_height
;
1974 } else if (size
.flags
& PBaseSize
) {
1975 c
->minw
= size
.base_width
;
1976 c
->minh
= size
.base_height
;
1978 c
->minw
= c
->minh
= 0;
1979 if (size
.flags
& PAspect
) {
1980 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1981 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1983 c
->maxa
= c
->mina
= 0.0;
1984 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
2119 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2121 if (c
== nexttiled(selmon
->clients
))
2122 if (!c
|| !(c
= nexttiled(c
->next
)))
2128 main(int argc
, char *argv
[])
2130 if (argc
== 2 && !strcmp("-v", argv
[1]))
2133 die("usage: dwm [-v]");
2134 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2135 fputs("warning: no locale support\n", stderr
);
2136 if (!(dpy
= XOpenDisplay(NULL
)))
2137 die("dwm: cannot open display");
2144 return EXIT_SUCCESS
;