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 spawn(const Arg
*arg
);
209 static void tag(const Arg
*arg
);
210 static void tagmon(const Arg
*arg
);
211 static void tile(Monitor
*m
);
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
; /* bar height */
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
) {
349 /* see last two sentences in ICCCM 4.1.2.3 */
350 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
351 if (!baseismin
) { /* temporarily remove base dimensions */
355 /* adjust for aspect limits */
356 if (c
->mina
> 0 && c
->maxa
> 0) {
357 if (c
->maxa
< (float)*w
/ *h
)
358 *w
= *h
* c
->maxa
+ 0.5;
359 else if (c
->mina
< (float)*h
/ *w
)
360 *h
= *w
* c
->mina
+ 0.5;
362 if (baseismin
) { /* increment calculation requires this */
366 /* adjust for increment value */
371 /* restore base dimensions */
372 *w
= MAX(*w
+ c
->basew
, c
->minw
);
373 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
375 *w
= MIN(*w
, c
->maxw
);
377 *h
= MIN(*h
, c
->maxh
);
379 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
387 else for (m
= mons
; m
; m
= m
->next
)
392 } else for (m
= mons
; m
; m
= m
->next
)
397 arrangemon(Monitor
*m
)
399 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
400 if (m
->lt
[m
->sellt
]->arrange
)
401 m
->lt
[m
->sellt
]->arrange(m
);
407 c
->next
= c
->mon
->clients
;
412 attachstack(Client
*c
)
414 c
->snext
= c
->mon
->stack
;
419 buttonpress(XEvent
*e
)
421 unsigned int i
, x
, click
;
425 XButtonPressedEvent
*ev
= &e
->xbutton
;
428 /* focus monitor if necessary */
429 if ((m
= wintomon(ev
->window
)) && m
!= selmon
) {
430 unfocus(selmon
->sel
, 1);
434 if (ev
->window
== selmon
->barwin
) {
438 while (ev
->x
>= x
&& ++i
< LENGTH(tags
));
439 if (i
< LENGTH(tags
)) {
442 } else if (ev
->x
< x
+ TEXTW(selmon
->ltsymbol
))
444 else if (ev
->x
> selmon
->ww
- (int)TEXTW(stext
))
445 click
= ClkStatusText
;
448 } else if ((c
= wintoclient(ev
->window
))) {
451 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
452 click
= ClkClientWin
;
454 for (i
= 0; i
< LENGTH(buttons
); i
++)
455 if (click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
456 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
457 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
463 xerrorxlib
= XSetErrorHandler(xerrorstart
);
464 /* this causes an error if some other window manager is running */
465 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
467 XSetErrorHandler(xerror
);
475 Layout foo
= { "", NULL
};
480 selmon
->lt
[selmon
->sellt
] = &foo
;
481 for (m
= mons
; m
; m
= m
->next
)
483 unmanage(m
->stack
, 0);
484 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
487 for (i
= 0; i
< CurLast
; i
++)
488 drw_cur_free(drw
, cursor
[i
]);
489 for (i
= 0; i
< LENGTH(colors
); i
++)
492 XDestroyWindow(dpy
, wmcheckwin
);
495 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
496 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
500 cleanupmon(Monitor
*mon
)
507 for (m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
510 XUnmapWindow(dpy
, mon
->barwin
);
511 XDestroyWindow(dpy
, mon
->barwin
);
516 clientmessage(XEvent
*e
)
518 XClientMessageEvent
*cme
= &e
->xclient
;
519 Client
*c
= wintoclient(cme
->window
);
523 if (cme
->message_type
== netatom
[NetWMState
]) {
524 if (cme
->data
.l
[1] == netatom
[NetWMFullscreen
]
525 || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
526 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
527 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
528 } else if (cme
->message_type
== netatom
[NetActiveWindow
]) {
529 if (c
!= selmon
->sel
&& !c
->isurgent
)
539 ce
.type
= ConfigureNotify
;
547 ce
.border_width
= c
->bw
;
549 ce
.override_redirect
= False
;
550 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
554 configurenotify(XEvent
*e
)
558 XConfigureEvent
*ev
= &e
->xconfigure
;
561 /* TODO: updategeom handling sucks, needs to be simplified */
562 if (ev
->window
== root
) {
563 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
566 if (updategeom() || dirty
) {
567 drw_resize(drw
, sw
, bh
);
569 for (m
= mons
; m
; m
= m
->next
) {
570 for (c
= m
->clients
; c
; c
= c
->next
)
572 resizeclient(c
, m
->mx
, m
->my
, m
->mw
, m
->mh
);
573 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
582 configurerequest(XEvent
*e
)
586 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
589 if ((c
= wintoclient(ev
->window
))) {
590 if (ev
->value_mask
& CWBorderWidth
)
591 c
->bw
= ev
->border_width
;
592 else if (c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
594 if (ev
->value_mask
& CWX
) {
596 c
->x
= m
->mx
+ ev
->x
;
598 if (ev
->value_mask
& CWY
) {
600 c
->y
= m
->my
+ ev
->y
;
602 if (ev
->value_mask
& CWWidth
) {
606 if (ev
->value_mask
& CWHeight
) {
610 if ((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
611 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
612 if ((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
613 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
614 if ((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
617 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
623 wc
.width
= ev
->width
;
624 wc
.height
= ev
->height
;
625 wc
.border_width
= ev
->border_width
;
626 wc
.sibling
= ev
->above
;
627 wc
.stack_mode
= ev
->detail
;
628 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
638 m
= ecalloc(1, sizeof(Monitor
));
639 m
->tagset
[0] = m
->tagset
[1] = 1;
641 m
->nmaster
= nmaster
;
642 m
->showbar
= showbar
;
644 m
->lt
[0] = &layouts
[0];
645 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
646 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
651 destroynotify(XEvent
*e
)
654 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
656 if ((c
= wintoclient(ev
->window
)))
665 for (tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
670 detachstack(Client
*c
)
674 for (tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
677 if (c
== c
->mon
->sel
) {
678 for (t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
689 if (!(m
= selmon
->next
))
691 } else if (selmon
== mons
)
692 for (m
= mons
; m
->next
; m
= m
->next
);
694 for (m
= mons
; m
->next
!= selmon
; m
= m
->next
);
702 int boxs
= drw
->fonts
->h
/ 9;
703 int boxw
= drw
->fonts
->h
/ 6 + 2;
704 unsigned int i
, occ
= 0, urg
= 0;
710 /* draw status first so it can be overdrawn by tags later */
711 if (m
== selmon
) { /* status is only drawn on selected monitor */
712 drw_setscheme(drw
, scheme
[SchemeNorm
]);
713 tw
= TEXTW(stext
) - lrpad
+ 2; /* 2px right padding */
714 drw_text(drw
, m
->ww
- tw
, 0, tw
, bh
, 0, stext
, 0);
717 for (c
= m
->clients
; c
; c
= c
->next
) {
723 for (i
= 0; i
< LENGTH(tags
); i
++) {
725 drw_setscheme(drw
, scheme
[m
->tagset
[m
->seltags
] & 1 << i
? SchemeSel
: SchemeNorm
]);
726 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, tags
[i
], urg
& 1 << i
);
728 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
,
729 m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
733 w
= TEXTW(m
->ltsymbol
);
734 drw_setscheme(drw
, scheme
[SchemeNorm
]);
735 x
= drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->ltsymbol
, 0);
737 if ((w
= m
->ww
- tw
- x
) > bh
) {
739 drw_setscheme(drw
, scheme
[m
== selmon
? SchemeSel
: SchemeNorm
]);
740 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->sel
->name
, 0);
741 if (m
->sel
->isfloating
)
742 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
, m
->sel
->isfixed
, 0);
744 drw_setscheme(drw
, scheme
[SchemeNorm
]);
745 drw_rect(drw
, x
, 0, w
, bh
, 1, 1);
748 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
);
756 for (m
= mons
; m
; m
= m
->next
)
761 enternotify(XEvent
*e
)
765 XCrossingEvent
*ev
= &e
->xcrossing
;
767 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
769 c
= wintoclient(ev
->window
);
770 m
= c
? c
->mon
: wintomon(ev
->window
);
772 unfocus(selmon
->sel
, 1);
774 } else if (!c
|| c
== selmon
->sel
)
783 XExposeEvent
*ev
= &e
->xexpose
;
785 if (ev
->count
== 0 && (m
= wintomon(ev
->window
)))
792 if (!c
|| !ISVISIBLE(c
))
793 for (c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
794 if (selmon
->sel
&& selmon
->sel
!= c
)
795 unfocus(selmon
->sel
, 0);
797 if (c
->mon
!= selmon
)
804 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
][ColBorder
].pixel
);
807 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
808 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
814 /* there are some broken focus acquiring clients needing extra handling */
818 XFocusChangeEvent
*ev
= &e
->xfocus
;
820 if (selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
821 setfocus(selmon
->sel
);
825 focusmon(const Arg
*arg
)
831 if ((m
= dirtomon(arg
->i
)) == selmon
)
833 unfocus(selmon
->sel
, 0);
839 focusstack(const Arg
*arg
)
841 Client
*c
= NULL
, *i
;
843 if (!selmon
->sel
|| (selmon
->sel
->isfullscreen
&& lockfullscreen
))
846 for (c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
848 for (c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
850 for (i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
854 for (; i
; i
= i
->next
)
865 getatomprop(Client
*c
, Atom prop
)
869 unsigned char *p
= NULL
;
870 Atom da
, atom
= None
;
872 if (XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
873 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
881 getrootptr(int *x
, int *y
)
887 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
895 unsigned char *p
= NULL
;
896 unsigned long n
, extra
;
899 if (XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
900 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
909 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
915 if (!text
|| size
== 0)
918 if (!XGetTextProperty(dpy
, w
, &name
, atom
) || !name
.nitems
)
920 if (name
.encoding
== XA_STRING
) {
921 strncpy(text
, (char *)name
.value
, size
- 1);
922 } else if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
923 strncpy(text
, *list
, size
- 1);
924 XFreeStringList(list
);
926 text
[size
- 1] = '\0';
932 grabbuttons(Client
*c
, int focused
)
937 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
938 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
940 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
941 BUTTONMASK
, GrabModeSync
, GrabModeSync
, None
, None
);
942 for (i
= 0; i
< LENGTH(buttons
); i
++)
943 if (buttons
[i
].click
== ClkClientWin
)
944 for (j
= 0; j
< LENGTH(modifiers
); j
++)
945 XGrabButton(dpy
, buttons
[i
].button
,
946 buttons
[i
].mask
| modifiers
[j
],
947 c
->win
, False
, BUTTONMASK
,
948 GrabModeAsync
, GrabModeSync
, None
, None
);
957 unsigned int i
, j
, k
;
958 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
959 int start
, end
, skip
;
962 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
963 XDisplayKeycodes(dpy
, &start
, &end
);
964 syms
= XGetKeyboardMapping(dpy
, start
, end
- start
+ 1, &skip
);
967 for (k
= start
; k
<= end
; k
++)
968 for (i
= 0; i
< LENGTH(keys
); i
++)
969 /* skip modifier codes, we do that ourselves */
970 if (keys
[i
].keysym
== syms
[(k
- start
) * skip
])
971 for (j
= 0; j
< LENGTH(modifiers
); j
++)
973 keys
[i
].mod
| modifiers
[j
],
975 GrabModeAsync
, GrabModeAsync
);
981 incnmaster(const Arg
*arg
)
983 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
989 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
)
992 if (unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
993 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
997 #endif /* XINERAMA */
1007 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1008 for (i
= 0; i
< LENGTH(keys
); i
++)
1009 if (keysym
== keys
[i
].keysym
1010 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1012 keys
[i
].func(&(keys
[i
].arg
));
1016 killclient(const Arg
*arg
)
1020 if (!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1022 XSetErrorHandler(xerrordummy
);
1023 XSetCloseDownMode(dpy
, DestroyAll
);
1024 XKillClient(dpy
, selmon
->sel
->win
);
1026 XSetErrorHandler(xerror
);
1032 manage(Window w
, XWindowAttributes
*wa
)
1034 Client
*c
, *t
= NULL
;
1035 Window trans
= None
;
1038 c
= ecalloc(1, sizeof(Client
));
1041 c
->x
= c
->oldx
= wa
->x
;
1042 c
->y
= c
->oldy
= wa
->y
;
1043 c
->w
= c
->oldw
= wa
->width
;
1044 c
->h
= c
->oldh
= wa
->height
;
1045 c
->oldbw
= wa
->border_width
;
1048 if (XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1056 if (c
->x
+ WIDTH(c
) > c
->mon
->wx
+ c
->mon
->ww
)
1057 c
->x
= c
->mon
->wx
+ c
->mon
->ww
- WIDTH(c
);
1058 if (c
->y
+ HEIGHT(c
) > c
->mon
->wy
+ c
->mon
->wh
)
1059 c
->y
= c
->mon
->wy
+ c
->mon
->wh
- HEIGHT(c
);
1060 c
->x
= MAX(c
->x
, c
->mon
->wx
);
1061 c
->y
= MAX(c
->y
, c
->mon
->wy
);
1064 wc
.border_width
= c
->bw
;
1065 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1066 XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1067 configure(c
); /* propagates border_width, if size doesn't change */
1068 updatewindowtype(c
);
1071 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1074 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1076 XRaiseWindow(dpy
, c
->win
);
1079 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1080 (unsigned char *) &(c
->win
), 1);
1081 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1082 setclientstate(c
, NormalState
);
1083 if (c
->mon
== selmon
)
1084 unfocus(selmon
->sel
, 0);
1087 XMapWindow(dpy
, c
->win
);
1092 mappingnotify(XEvent
*e
)
1094 XMappingEvent
*ev
= &e
->xmapping
;
1096 XRefreshKeyboardMapping(ev
);
1097 if (ev
->request
== MappingKeyboard
)
1102 maprequest(XEvent
*e
)
1104 static XWindowAttributes wa
;
1105 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1107 if (!XGetWindowAttributes(dpy
, ev
->window
, &wa
) || wa
.override_redirect
)
1109 if (!wintoclient(ev
->window
))
1110 manage(ev
->window
, &wa
);
1119 for (c
= m
->clients
; c
; c
= c
->next
)
1122 if (n
> 0) /* override layout symbol */
1123 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1124 for (c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1125 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, 0);
1129 motionnotify(XEvent
*e
)
1131 static Monitor
*mon
= NULL
;
1133 XMotionEvent
*ev
= &e
->xmotion
;
1135 if (ev
->window
!= root
)
1137 if ((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1138 unfocus(selmon
->sel
, 1);
1146 movemouse(const Arg
*arg
)
1148 int x
, y
, ocx
, ocy
, nx
, ny
;
1154 if (!(c
= selmon
->sel
))
1156 if (c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1161 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1162 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1164 if (!getrootptr(&x
, &y
))
1167 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1169 case ConfigureRequest
:
1172 handler
[ev
.type
](&ev
);
1175 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1177 lasttime
= ev
.xmotion
.time
;
1179 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1180 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1181 if (abs(selmon
->wx
- nx
) < snap
)
1183 else if (abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1184 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1185 if (abs(selmon
->wy
- ny
) < snap
)
1187 else if (abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1188 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1189 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1190 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1191 togglefloating(NULL
);
1192 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1193 resize(c
, nx
, ny
, c
->w
, c
->h
, 1);
1196 } while (ev
.type
!= ButtonRelease
);
1197 XUngrabPointer(dpy
, CurrentTime
);
1198 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1206 nexttiled(Client
*c
)
1208 for (; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1222 propertynotify(XEvent
*e
)
1226 XPropertyEvent
*ev
= &e
->xproperty
;
1228 if ((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1230 else if (ev
->state
== PropertyDelete
)
1231 return; /* ignore */
1232 else if ((c
= wintoclient(ev
->window
))) {
1235 case XA_WM_TRANSIENT_FOR
:
1236 if (!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1237 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1240 case XA_WM_NORMAL_HINTS
:
1248 if (ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1250 if (c
== c
->mon
->sel
)
1253 if (ev
->atom
== netatom
[NetWMWindowType
])
1254 updatewindowtype(c
);
1259 quit(const Arg
*arg
)
1265 recttomon(int x
, int y
, int w
, int h
)
1267 Monitor
*m
, *r
= selmon
;
1270 for (m
= mons
; m
; m
= m
->next
)
1271 if ((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1279 resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
)
1281 if (applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1282 resizeclient(c
, x
, y
, w
, h
);
1286 resizeclient(Client
*c
, int x
, int y
, int w
, int h
)
1290 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1291 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1292 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1293 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1294 wc
.border_width
= c
->bw
;
1295 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1301 resizemouse(const Arg
*arg
)
1303 int ocx
, ocy
, nw
, nh
;
1309 if (!(c
= selmon
->sel
))
1311 if (c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1316 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1317 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1319 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1321 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1323 case ConfigureRequest
:
1326 handler
[ev
.type
](&ev
);
1329 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1331 lasttime
= ev
.xmotion
.time
;
1333 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1334 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1335 if (c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1336 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1338 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1339 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1340 togglefloating(NULL
);
1342 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1343 resize(c
, c
->x
, c
->y
, nw
, nh
, 1);
1346 } while (ev
.type
!= ButtonRelease
);
1347 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1348 XUngrabPointer(dpy
, CurrentTime
);
1349 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1350 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1367 if (m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1368 XRaiseWindow(dpy
, m
->sel
->win
);
1369 if (m
->lt
[m
->sellt
]->arrange
) {
1370 wc
.stack_mode
= Below
;
1371 wc
.sibling
= m
->barwin
;
1372 for (c
= m
->stack
; c
; c
= c
->snext
)
1373 if (!c
->isfloating
&& ISVISIBLE(c
)) {
1374 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1375 wc
.sibling
= c
->win
;
1379 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1386 /* main event loop */
1388 while (running
&& !XNextEvent(dpy
, &ev
))
1389 if (handler
[ev
.type
])
1390 handler
[ev
.type
](&ev
); /* call handler */
1396 unsigned int i
, num
;
1397 Window d1
, d2
, *wins
= NULL
;
1398 XWindowAttributes wa
;
1400 if (XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1401 for (i
= 0; i
< num
; i
++) {
1402 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1403 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1405 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1406 manage(wins
[i
], &wa
);
1408 for (i
= 0; i
< num
; i
++) { /* now the transients */
1409 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1411 if (XGetTransientForHint(dpy
, wins
[i
], &d1
)
1412 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1413 manage(wins
[i
], &wa
);
1421 sendmon(Client
*c
, Monitor
*m
)
1429 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1437 setclientstate(Client
*c
, long state
)
1439 long data
[] = { state
, None
};
1441 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1442 PropModeReplace
, (unsigned char *)data
, 2);
1446 sendevent(Client
*c
, Atom proto
)
1453 if (XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1454 while (!exists
&& n
--)
1455 exists
= protocols
[n
] == proto
;
1459 ev
.type
= ClientMessage
;
1460 ev
.xclient
.window
= c
->win
;
1461 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1462 ev
.xclient
.format
= 32;
1463 ev
.xclient
.data
.l
[0] = proto
;
1464 ev
.xclient
.data
.l
[1] = CurrentTime
;
1465 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1473 if (!c
->neverfocus
) {
1474 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1475 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1476 XA_WINDOW
, 32, PropModeReplace
,
1477 (unsigned char *) &(c
->win
), 1);
1479 sendevent(c
, wmatom
[WMTakeFocus
]);
1483 setfullscreen(Client
*c
, int fullscreen
)
1485 if (fullscreen
&& !c
->isfullscreen
) {
1486 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1487 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1488 c
->isfullscreen
= 1;
1489 c
->oldstate
= c
->isfloating
;
1493 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1494 XRaiseWindow(dpy
, c
->win
);
1495 } else if (!fullscreen
&& c
->isfullscreen
){
1496 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1497 PropModeReplace
, (unsigned char*)0, 0);
1498 c
->isfullscreen
= 0;
1499 c
->isfloating
= c
->oldstate
;
1505 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1511 setlayout(const Arg
*arg
)
1513 if (!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1516 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1517 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1524 /* arg > 1.0 will set mfact absolutely */
1526 setmfact(const Arg
*arg
)
1530 if (!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1532 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1533 if (f
< 0.05 || f
> 0.95)
1543 XSetWindowAttributes wa
;
1545 struct sigaction sa
;
1547 /* do not transform children into zombies when they terminate */
1548 sigemptyset(&sa
.sa_mask
);
1549 sa
.sa_flags
= SA_NOCLDSTOP
| SA_NOCLDWAIT
| SA_RESTART
;
1550 sa
.sa_handler
= SIG_IGN
;
1551 sigaction(SIGCHLD
, &sa
, NULL
);
1553 /* clean up any zombies (inherited from .xinitrc etc) immediately */
1554 while (waitpid(-1, NULL
, WNOHANG
) > 0);
1557 screen
= DefaultScreen(dpy
);
1558 sw
= DisplayWidth(dpy
, screen
);
1559 sh
= DisplayHeight(dpy
, screen
);
1560 root
= RootWindow(dpy
, screen
);
1561 drw
= drw_create(dpy
, screen
, root
, sw
, sh
);
1562 if (!drw_fontset_create(drw
, fonts
, LENGTH(fonts
)))
1563 die("no fonts could be loaded.");
1564 lrpad
= drw
->fonts
->h
;
1565 bh
= drw
->fonts
->h
+ 2;
1568 utf8string
= XInternAtom(dpy
, "UTF8_STRING", False
);
1569 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1570 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1571 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1572 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1573 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1574 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1575 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1576 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1577 netatom
[NetWMCheck
] = XInternAtom(dpy
, "_NET_SUPPORTING_WM_CHECK", False
);
1578 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1579 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1580 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1581 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1583 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1584 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1585 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1586 /* init appearance */
1587 scheme
= ecalloc(LENGTH(colors
), sizeof(Clr
*));
1588 for (i
= 0; i
< LENGTH(colors
); i
++)
1589 scheme
[i
] = drw_scm_create(drw
, colors
[i
], 3);
1593 /* supporting window for NetWMCheck */
1594 wmcheckwin
= XCreateSimpleWindow(dpy
, root
, 0, 0, 1, 1, 0, 0, 0);
1595 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1596 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1597 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMName
], utf8string
, 8,
1598 PropModeReplace
, (unsigned char *) "dwm", 3);
1599 XChangeProperty(dpy
, root
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1600 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1601 /* EWMH support per view */
1602 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1603 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1604 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1606 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1607 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1608 |ButtonPressMask
|PointerMotionMask
|EnterWindowMask
1609 |LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1610 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1611 XSelectInput(dpy
, root
, wa
.event_mask
);
1617 seturgent(Client
*c
, int urg
)
1622 if (!(wmh
= XGetWMHints(dpy
, c
->win
)))
1624 wmh
->flags
= urg
? (wmh
->flags
| XUrgencyHint
) : (wmh
->flags
& ~XUrgencyHint
);
1625 XSetWMHints(dpy
, c
->win
, wmh
);
1635 /* show clients top down */
1636 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1637 if ((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1638 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, 0);
1641 /* hide clients bottom up */
1643 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1648 spawn(const Arg
*arg
)
1650 if (arg
->v
== dmenucmd
)
1651 dmenumon
[0] = '0' + selmon
->num
;
1654 close(ConnectionNumber(dpy
));
1656 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1657 die("dwm: execvp '%s' failed:", ((char **)arg
->v
)[0]);
1664 if (selmon
->sel
&& arg
->ui
& TAGMASK
) {
1665 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1672 tagmon(const Arg
*arg
)
1674 if (!selmon
->sel
|| !mons
->next
)
1676 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1682 unsigned int i
, n
, h
, mw
, my
, ty
;
1685 for (n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1690 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1693 for (i
= my
= ty
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1694 if (i
< m
->nmaster
) {
1695 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
);
1696 resize(c
, m
->wx
, m
->wy
+ my
, mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1697 if (my
+ HEIGHT(c
) < m
->wh
)
1700 h
= (m
->wh
- ty
) / (n
- i
);
1701 resize(c
, m
->wx
+ mw
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1702 if (ty
+ HEIGHT(c
) < m
->wh
)
1708 togglebar(const Arg
*arg
)
1710 selmon
->showbar
= !selmon
->showbar
;
1711 updatebarpos(selmon
);
1712 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1717 togglefloating(const Arg
*arg
)
1721 if (selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1723 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1724 if (selmon
->sel
->isfloating
)
1725 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1726 selmon
->sel
->w
, selmon
->sel
->h
, 0);
1731 toggletag(const Arg
*arg
)
1733 unsigned int newtags
;
1737 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1739 selmon
->sel
->tags
= newtags
;
1746 toggleview(const Arg
*arg
)
1748 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1751 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1758 unfocus(Client
*c
, int setfocus
)
1763 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1765 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1766 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1771 unmanage(Client
*c
, int destroyed
)
1773 Monitor
*m
= c
->mon
;
1779 wc
.border_width
= c
->oldbw
;
1780 XGrabServer(dpy
); /* avoid race conditions */
1781 XSetErrorHandler(xerrordummy
);
1782 XSelectInput(dpy
, c
->win
, NoEventMask
);
1783 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1784 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1785 setclientstate(c
, WithdrawnState
);
1787 XSetErrorHandler(xerror
);
1797 unmapnotify(XEvent
*e
)
1800 XUnmapEvent
*ev
= &e
->xunmap
;
1802 if ((c
= wintoclient(ev
->window
))) {
1804 setclientstate(c
, WithdrawnState
);
1814 XSetWindowAttributes wa
= {
1815 .override_redirect
= True
,
1816 .background_pixmap
= ParentRelative
,
1817 .event_mask
= ButtonPressMask
|ExposureMask
1819 XClassHint ch
= {"dwm", "dwm"};
1820 for (m
= mons
; m
; m
= m
->next
) {
1823 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1824 CopyFromParent
, DefaultVisual(dpy
, screen
),
1825 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1826 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
1827 XMapRaised(dpy
, m
->barwin
);
1828 XSetClassHint(dpy
, m
->barwin
, &ch
);
1833 updatebarpos(Monitor
*m
)
1839 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1840 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1851 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1852 for (m
= mons
; m
; m
= m
->next
)
1853 for (c
= m
->clients
; c
; c
= c
->next
)
1854 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
1855 XA_WINDOW
, 32, PropModeAppend
,
1856 (unsigned char *) &(c
->win
), 1);
1865 if (XineramaIsActive(dpy
)) {
1869 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1870 XineramaScreenInfo
*unique
= NULL
;
1872 for (n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1873 /* only consider unique geometries as separate screens */
1874 unique
= ecalloc(nn
, sizeof(XineramaScreenInfo
));
1875 for (i
= 0, j
= 0; i
< nn
; i
++)
1876 if (isuniquegeom(unique
, j
, &info
[i
]))
1877 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1881 /* new monitors if nn > n */
1882 for (i
= n
; i
< nn
; i
++) {
1883 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1885 m
->next
= createmon();
1889 for (i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1891 || unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1892 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
)
1896 m
->mx
= m
->wx
= unique
[i
].x_org
;
1897 m
->my
= m
->wy
= unique
[i
].y_org
;
1898 m
->mw
= m
->ww
= unique
[i
].width
;
1899 m
->mh
= m
->wh
= unique
[i
].height
;
1902 /* removed monitors if n > nn */
1903 for (i
= nn
; i
< n
; i
++) {
1904 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1905 while ((c
= m
->clients
)) {
1907 m
->clients
= c
->next
;
1919 #endif /* XINERAMA */
1920 { /* default monitor setup */
1923 if (mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1925 mons
->mw
= mons
->ww
= sw
;
1926 mons
->mh
= mons
->wh
= sh
;
1932 selmon
= wintomon(root
);
1938 updatenumlockmask(void)
1941 XModifierKeymap
*modmap
;
1944 modmap
= XGetModifierMapping(dpy
);
1945 for (i
= 0; i
< 8; i
++)
1946 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1947 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1948 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1949 numlockmask
= (1 << i
);
1950 XFreeModifiermap(modmap
);
1954 updatesizehints(Client
*c
)
1959 if (!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1960 /* size is uninitialized, ensure that size.flags aren't used */
1962 if (size
.flags
& PBaseSize
) {
1963 c
->basew
= size
.base_width
;
1964 c
->baseh
= size
.base_height
;
1965 } else if (size
.flags
& PMinSize
) {
1966 c
->basew
= size
.min_width
;
1967 c
->baseh
= size
.min_height
;
1969 c
->basew
= c
->baseh
= 0;
1970 if (size
.flags
& PResizeInc
) {
1971 c
->incw
= size
.width_inc
;
1972 c
->inch
= size
.height_inc
;
1974 c
->incw
= c
->inch
= 0;
1975 if (size
.flags
& PMaxSize
) {
1976 c
->maxw
= size
.max_width
;
1977 c
->maxh
= size
.max_height
;
1979 c
->maxw
= c
->maxh
= 0;
1980 if (size
.flags
& PMinSize
) {
1981 c
->minw
= size
.min_width
;
1982 c
->minh
= size
.min_height
;
1983 } else if (size
.flags
& PBaseSize
) {
1984 c
->minw
= size
.base_width
;
1985 c
->minh
= size
.base_height
;
1987 c
->minw
= c
->minh
= 0;
1988 if (size
.flags
& PAspect
) {
1989 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1990 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1992 c
->maxa
= c
->mina
= 0.0;
1993 c
->isfixed
= (c
->maxw
&& c
->maxh
&& c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
2000 if (!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
2001 strcpy(stext
, "dwm-"VERSION
);
2006 updatetitle(Client
*c
)
2008 if (!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
2009 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
2010 if (c
->name
[0] == '\0') /* hack to mark broken clients */
2011 strcpy(c
->name
, broken
);
2015 updatewindowtype(Client
*c
)
2017 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
2018 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
2020 if (state
== netatom
[NetWMFullscreen
])
2021 setfullscreen(c
, 1);
2022 if (wtype
== netatom
[NetWMWindowTypeDialog
])
2027 updatewmhints(Client
*c
)
2031 if ((wmh
= XGetWMHints(dpy
, c
->win
))) {
2032 if (c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2033 wmh
->flags
&= ~XUrgencyHint
;
2034 XSetWMHints(dpy
, c
->win
, wmh
);
2036 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? 1 : 0;
2037 if (wmh
->flags
& InputHint
)
2038 c
->neverfocus
= !wmh
->input
;
2046 view(const Arg
*arg
)
2048 if ((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2050 selmon
->seltags
^= 1; /* toggle sel tagset */
2051 if (arg
->ui
& TAGMASK
)
2052 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2058 wintoclient(Window w
)
2063 for (m
= mons
; m
; m
= m
->next
)
2064 for (c
= m
->clients
; c
; c
= c
->next
)
2077 if (w
== root
&& getrootptr(&x
, &y
))
2078 return recttomon(x
, y
, 1, 1);
2079 for (m
= mons
; m
; m
= m
->next
)
2082 if ((c
= wintoclient(w
)))
2087 /* There's no way to check accesses to destroyed windows, thus those cases are
2088 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2089 * default error handler, which may call exit. */
2091 xerror(Display
*dpy
, XErrorEvent
*ee
)
2093 if (ee
->error_code
== BadWindow
2094 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2095 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2096 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2097 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2098 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2099 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2100 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2101 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2103 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2104 ee
->request_code
, ee
->error_code
);
2105 return xerrorxlib(dpy
, ee
); /* may call exit */
2109 xerrordummy(Display
*dpy
, XErrorEvent
*ee
)
2114 /* Startup Error handler to check if another window manager
2115 * is already running. */
2117 xerrorstart(Display
*dpy
, XErrorEvent
*ee
)
2119 die("dwm: another window manager is already running");
2124 zoom(const Arg
*arg
)
2126 Client
*c
= selmon
->sel
;
2128 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| !c
|| c
->isfloating
)
2130 if (c
== nexttiled(selmon
->clients
) && !(c
= nexttiled(c
->next
)))
2136 main(int argc
, char *argv
[])
2138 if (argc
== 2 && !strcmp("-v", argv
[1]))
2141 die("usage: dwm [-v]");
2142 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2143 fputs("warning: no locale support\n", stderr
);
2144 if (!(dpy
= XOpenDisplay(NULL
)))
2145 die("dwm: cannot open display");
2149 if (pledge("stdio rpath proc exec", NULL
) == -1)
2151 #endif /* __OpenBSD__ */
2156 return EXIT_SUCCESS
;