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 MOUSEMASK (BUTTONMASK|PointerMotionMask)
54 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
55 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
56 #define TAGMASK ((1 << LENGTH(tags)) - 1)
57 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
60 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
61 enum { SchemeNorm
, SchemeSel
}; /* color schemes */
62 enum { NetSupported
, NetWMName
, NetWMState
, NetWMCheck
,
63 NetWMFullscreen
, NetActiveWindow
, NetWMWindowType
,
64 NetWMWindowTypeDialog
, NetClientList
, NetLast
}; /* EWMH atoms */
65 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, WMLast
}; /* default atoms */
66 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
67 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
80 void (*func
)(const Arg
*arg
);
84 typedef struct Monitor Monitor
;
85 typedef struct Client Client
;
90 int oldx
, oldy
, oldw
, oldh
;
91 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
, hintsvalid
;
94 int isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
;
104 void (*func
)(const Arg
*);
110 void (*arrange
)(Monitor
*);
118 int by
; /* bar geometry */
119 int mx
, my
, mw
, mh
; /* screen size */
120 int wx
, wy
, ww
, wh
; /* window area */
121 unsigned int seltags
;
123 unsigned int tagset
[2];
136 const char *instance
;
143 /* function declarations */
144 static void applyrules(Client
*c
);
145 static int applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
);
146 static void arrange(Monitor
*m
);
147 static void arrangemon(Monitor
*m
);
148 static void attach(Client
*c
);
149 static void attachstack(Client
*c
);
150 static void buttonpress(XEvent
*e
);
151 static void checkotherwm(void);
152 static void cleanup(void);
153 static void cleanupmon(Monitor
*mon
);
154 static void clientmessage(XEvent
*e
);
155 static void configure(Client
*c
);
156 static void configurenotify(XEvent
*e
);
157 static void configurerequest(XEvent
*e
);
158 static Monitor
*createmon(void);
159 static void destroynotify(XEvent
*e
);
160 static void detach(Client
*c
);
161 static void detachstack(Client
*c
);
162 static Monitor
*dirtomon(int dir
);
163 static void drawbar(Monitor
*m
);
164 static void drawbars(void);
165 static void enternotify(XEvent
*e
);
166 static void expose(XEvent
*e
);
167 static void focus(Client
*c
);
168 static void focusin(XEvent
*e
);
169 static void focusmon(const Arg
*arg
);
170 static void focusstack(const Arg
*arg
);
171 static Atom
getatomprop(Client
*c
, Atom prop
);
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
*c
);
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 spawn(const Arg
*arg
);
208 static void tag(const Arg
*arg
);
209 static void tagmon(const Arg
*arg
);
210 static void tile(Monitor
*m
);
211 static void togglebar(const Arg
*arg
);
212 static void togglefloating(const Arg
*arg
);
213 static void toggletag(const Arg
*arg
);
214 static void toggleview(const Arg
*arg
);
215 static void unfocus(Client
*c
, int setfocus
);
216 static void unmanage(Client
*c
, int destroyed
);
217 static void unmapnotify(XEvent
*e
);
218 static void updatebarpos(Monitor
*m
);
219 static void updatebars(void);
220 static void updateclientlist(void);
221 static int updategeom(void);
222 static void updatenumlockmask(void);
223 static void updatesizehints(Client
*c
);
224 static void updatestatus(void);
225 static void updatetitle(Client
*c
);
226 static void updatewindowtype(Client
*c
);
227 static void updatewmhints(Client
*c
);
228 static void view(const Arg
*arg
);
229 static Client
*wintoclient(Window w
);
230 static Monitor
*wintomon(Window w
);
231 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
232 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
233 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
234 static void zoom(const Arg
*arg
);
237 static const char broken
[] = "broken";
238 static char stext
[256];
240 static int sw
, sh
; /* X display screen geometry width, height */
241 static int bh
; /* bar height */
242 static int lrpad
; /* sum of left and right padding for text */
243 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
244 static unsigned int numlockmask
= 0;
245 static void (*handler
[LASTEvent
]) (XEvent
*) = {
246 [ButtonPress
] = buttonpress
,
247 [ClientMessage
] = clientmessage
,
248 [ConfigureRequest
] = configurerequest
,
249 [ConfigureNotify
] = configurenotify
,
250 [DestroyNotify
] = destroynotify
,
251 [EnterNotify
] = enternotify
,
254 [KeyPress
] = keypress
,
255 [MappingNotify
] = mappingnotify
,
256 [MapRequest
] = maprequest
,
257 [MotionNotify
] = motionnotify
,
258 [PropertyNotify
] = propertynotify
,
259 [UnmapNotify
] = unmapnotify
261 static Atom wmatom
[WMLast
], netatom
[NetLast
];
262 static int running
= 1;
263 static Cur
*cursor
[CurLast
];
267 static Monitor
*mons
, *selmon
;
268 static Window root
, wmcheckwin
;
270 /* configuration, allows nested code to access above variables */
273 /* compile-time check if all tags fit into an unsigned int bit array. */
274 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
276 /* function implementations */
278 applyrules(Client
*c
)
280 const char *class, *instance
;
284 XClassHint ch
= { NULL
, NULL
};
289 XGetClassHint(dpy
, c
->win
, &ch
);
290 class = ch
.res_class
? ch
.res_class
: broken
;
291 instance
= ch
.res_name
? ch
.res_name
: broken
;
293 for (i
= 0; i
< LENGTH(rules
); i
++) {
295 if ((!r
->title
|| strstr(c
->name
, r
->title
))
296 && (!r
->class || strstr(class, r
->class))
297 && (!r
->instance
|| strstr(instance
, r
->instance
)))
299 c
->isfloating
= r
->isfloating
;
301 for (m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
310 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
314 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
)
319 /* set minimum possible */
327 if (*x
+ *w
+ 2 * c
->bw
< 0)
329 if (*y
+ *h
+ 2 * c
->bw
< 0)
332 if (*x
>= m
->wx
+ m
->ww
)
333 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
334 if (*y
>= m
->wy
+ m
->wh
)
335 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
336 if (*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
338 if (*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
345 if (resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
348 /* see last two sentences in ICCCM 4.1.2.3 */
349 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
350 if (!baseismin
) { /* temporarily remove base dimensions */
354 /* adjust for aspect limits */
355 if (c
->mina
> 0 && c
->maxa
> 0) {
356 if (c
->maxa
< (float)*w
/ *h
)
357 *w
= *h
* c
->maxa
+ 0.5;
358 else if (c
->mina
< (float)*h
/ *w
)
359 *h
= *w
* c
->mina
+ 0.5;
361 if (baseismin
) { /* increment calculation requires this */
365 /* adjust for increment value */
370 /* restore base dimensions */
371 *w
= MAX(*w
+ c
->basew
, c
->minw
);
372 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
374 *w
= MIN(*w
, c
->maxw
);
376 *h
= MIN(*h
, c
->maxh
);
378 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
386 else for (m
= mons
; m
; m
= m
->next
)
391 } else for (m
= mons
; m
; m
= m
->next
)
396 arrangemon(Monitor
*m
)
398 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
399 if (m
->lt
[m
->sellt
]->arrange
)
400 m
->lt
[m
->sellt
]->arrange(m
);
406 c
->next
= c
->mon
->clients
;
411 attachstack(Client
*c
)
413 c
->snext
= c
->mon
->stack
;
418 buttonpress(XEvent
*e
)
420 unsigned int i
, x
, click
;
424 XButtonPressedEvent
*ev
= &e
->xbutton
;
427 /* focus monitor if necessary */
428 if ((m
= wintomon(ev
->window
)) && m
!= selmon
) {
429 unfocus(selmon
->sel
, 1);
433 if (ev
->window
== selmon
->barwin
) {
437 while (ev
->x
>= x
&& ++i
< LENGTH(tags
));
438 if (i
< LENGTH(tags
)) {
441 } else if (ev
->x
< x
+ TEXTW(selmon
->ltsymbol
))
443 else if (ev
->x
> selmon
->ww
- (int)TEXTW(stext
))
444 click
= ClkStatusText
;
447 } else if ((c
= wintoclient(ev
->window
))) {
450 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
451 click
= ClkClientWin
;
453 for (i
= 0; i
< LENGTH(buttons
); i
++)
454 if (click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
455 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
456 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
462 xerrorxlib
= XSetErrorHandler(xerrorstart
);
463 /* this causes an error if some other window manager is running */
464 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
466 XSetErrorHandler(xerror
);
474 Layout foo
= { "", NULL
};
479 selmon
->lt
[selmon
->sellt
] = &foo
;
480 for (m
= mons
; m
; m
= m
->next
)
482 unmanage(m
->stack
, 0);
483 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
486 for (i
= 0; i
< CurLast
; i
++)
487 drw_cur_free(drw
, cursor
[i
]);
488 for (i
= 0; i
< LENGTH(colors
); i
++)
491 XDestroyWindow(dpy
, wmcheckwin
);
494 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
495 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
499 cleanupmon(Monitor
*mon
)
506 for (m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
509 XUnmapWindow(dpy
, mon
->barwin
);
510 XDestroyWindow(dpy
, mon
->barwin
);
515 clientmessage(XEvent
*e
)
517 XClientMessageEvent
*cme
= &e
->xclient
;
518 Client
*c
= wintoclient(cme
->window
);
522 if (cme
->message_type
== netatom
[NetWMState
]) {
523 if (cme
->data
.l
[1] == netatom
[NetWMFullscreen
]
524 || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
525 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
526 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
527 } else if (cme
->message_type
== netatom
[NetActiveWindow
]) {
528 if (c
!= selmon
->sel
&& !c
->isurgent
)
538 ce
.type
= ConfigureNotify
;
546 ce
.border_width
= c
->bw
;
548 ce
.override_redirect
= False
;
549 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
553 configurenotify(XEvent
*e
)
557 XConfigureEvent
*ev
= &e
->xconfigure
;
560 /* TODO: updategeom handling sucks, needs to be simplified */
561 if (ev
->window
== root
) {
562 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
565 if (updategeom() || dirty
) {
566 drw_resize(drw
, sw
, bh
);
568 for (m
= mons
; m
; m
= m
->next
) {
569 for (c
= m
->clients
; c
; c
= c
->next
)
571 resizeclient(c
, m
->mx
, m
->my
, m
->mw
, m
->mh
);
572 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
581 configurerequest(XEvent
*e
)
585 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
588 if ((c
= wintoclient(ev
->window
))) {
589 if (ev
->value_mask
& CWBorderWidth
)
590 c
->bw
= ev
->border_width
;
591 else if (c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
593 if (ev
->value_mask
& CWX
) {
595 c
->x
= m
->mx
+ ev
->x
;
597 if (ev
->value_mask
& CWY
) {
599 c
->y
= m
->my
+ ev
->y
;
601 if (ev
->value_mask
& CWWidth
) {
605 if (ev
->value_mask
& CWHeight
) {
609 if ((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
610 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
611 if ((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
612 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
613 if ((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
616 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
622 wc
.width
= ev
->width
;
623 wc
.height
= ev
->height
;
624 wc
.border_width
= ev
->border_width
;
625 wc
.sibling
= ev
->above
;
626 wc
.stack_mode
= ev
->detail
;
627 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
637 m
= ecalloc(1, sizeof(Monitor
));
638 m
->tagset
[0] = m
->tagset
[1] = 1;
640 m
->nmaster
= nmaster
;
641 m
->showbar
= showbar
;
643 m
->lt
[0] = &layouts
[0];
644 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
645 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
650 destroynotify(XEvent
*e
)
653 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
655 if ((c
= wintoclient(ev
->window
)))
664 for (tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
669 detachstack(Client
*c
)
673 for (tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
676 if (c
== c
->mon
->sel
) {
677 for (t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
688 if (!(m
= selmon
->next
))
690 } else if (selmon
== mons
)
691 for (m
= mons
; m
->next
; m
= m
->next
);
693 for (m
= mons
; m
->next
!= selmon
; m
= m
->next
);
701 int boxs
= drw
->fonts
->h
/ 9;
702 int boxw
= drw
->fonts
->h
/ 6 + 2;
703 unsigned int i
, occ
= 0, urg
= 0;
709 /* draw status first so it can be overdrawn by tags later */
710 if (m
== selmon
) { /* status is only drawn on selected monitor */
711 drw_setscheme(drw
, scheme
[SchemeNorm
]);
712 tw
= TEXTW(stext
) - lrpad
+ 2; /* 2px right padding */
713 drw_text(drw
, m
->ww
- tw
, 0, tw
, bh
, 0, stext
, 0);
716 for (c
= m
->clients
; c
; c
= c
->next
) {
722 for (i
= 0; i
< LENGTH(tags
); i
++) {
724 drw_setscheme(drw
, scheme
[m
->tagset
[m
->seltags
] & 1 << i
? SchemeSel
: SchemeNorm
]);
725 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, tags
[i
], urg
& 1 << i
);
727 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
,
728 m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
732 w
= TEXTW(m
->ltsymbol
);
733 drw_setscheme(drw
, scheme
[SchemeNorm
]);
734 x
= drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->ltsymbol
, 0);
736 if ((w
= m
->ww
- tw
- x
) > bh
) {
738 drw_setscheme(drw
, scheme
[m
== selmon
? SchemeSel
: SchemeNorm
]);
739 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->sel
->name
, 0);
740 if (m
->sel
->isfloating
)
741 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
, m
->sel
->isfixed
, 0);
743 drw_setscheme(drw
, scheme
[SchemeNorm
]);
744 drw_rect(drw
, x
, 0, w
, bh
, 1, 1);
747 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
);
755 for (m
= mons
; m
; m
= m
->next
)
760 enternotify(XEvent
*e
)
764 XCrossingEvent
*ev
= &e
->xcrossing
;
766 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
768 c
= wintoclient(ev
->window
);
769 m
= c
? c
->mon
: wintomon(ev
->window
);
771 unfocus(selmon
->sel
, 1);
773 } else if (!c
|| c
== selmon
->sel
)
782 XExposeEvent
*ev
= &e
->xexpose
;
784 if (ev
->count
== 0 && (m
= wintomon(ev
->window
)))
791 if (!c
|| !ISVISIBLE(c
))
792 for (c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
793 if (selmon
->sel
&& selmon
->sel
!= c
)
794 unfocus(selmon
->sel
, 0);
796 if (c
->mon
!= selmon
)
803 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
][ColBorder
].pixel
);
806 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
807 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
813 /* there are some broken focus acquiring clients needing extra handling */
817 XFocusChangeEvent
*ev
= &e
->xfocus
;
819 if (selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
820 setfocus(selmon
->sel
);
824 focusmon(const Arg
*arg
)
830 if ((m
= dirtomon(arg
->i
)) == selmon
)
832 unfocus(selmon
->sel
, 0);
838 focusstack(const Arg
*arg
)
840 Client
*c
= NULL
, *i
;
842 if (!selmon
->sel
|| (selmon
->sel
->isfullscreen
&& lockfullscreen
))
845 for (c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
847 for (c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
849 for (i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
853 for (; i
; i
= i
->next
)
864 getatomprop(Client
*c
, Atom prop
)
868 unsigned char *p
= NULL
;
869 Atom da
, atom
= None
;
871 if (XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
872 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
880 getrootptr(int *x
, int *y
)
886 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
894 unsigned char *p
= NULL
;
895 unsigned long n
, extra
;
898 if (XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
899 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
908 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
914 if (!text
|| size
== 0)
917 if (!XGetTextProperty(dpy
, w
, &name
, atom
) || !name
.nitems
)
919 if (name
.encoding
== XA_STRING
) {
920 strncpy(text
, (char *)name
.value
, size
- 1);
921 } else if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
922 strncpy(text
, *list
, size
- 1);
923 XFreeStringList(list
);
925 text
[size
- 1] = '\0';
931 grabbuttons(Client
*c
, int focused
)
936 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
937 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
939 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
940 BUTTONMASK
, GrabModeSync
, GrabModeSync
, None
, None
);
941 for (i
= 0; i
< LENGTH(buttons
); i
++)
942 if (buttons
[i
].click
== ClkClientWin
)
943 for (j
= 0; j
< LENGTH(modifiers
); j
++)
944 XGrabButton(dpy
, buttons
[i
].button
,
945 buttons
[i
].mask
| modifiers
[j
],
946 c
->win
, False
, BUTTONMASK
,
947 GrabModeAsync
, GrabModeSync
, None
, None
);
956 unsigned int i
, j
, k
;
957 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
958 int start
, end
, skip
;
961 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
962 XDisplayKeycodes(dpy
, &start
, &end
);
963 syms
= XGetKeyboardMapping(dpy
, start
, end
- start
+ 1, &skip
);
966 for (k
= start
; k
<= end
; k
++)
967 for (i
= 0; i
< LENGTH(keys
); i
++)
968 /* skip modifier codes, we do that ourselves */
969 if (keys
[i
].keysym
== syms
[(k
- start
) * skip
])
970 for (j
= 0; j
< LENGTH(modifiers
); j
++)
972 keys
[i
].mod
| modifiers
[j
],
974 GrabModeAsync
, GrabModeAsync
);
980 incnmaster(const Arg
*arg
)
982 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
988 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
)
991 if (unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
992 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
996 #endif /* XINERAMA */
1006 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1007 for (i
= 0; i
< LENGTH(keys
); i
++)
1008 if (keysym
== keys
[i
].keysym
1009 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1011 keys
[i
].func(&(keys
[i
].arg
));
1015 killclient(const Arg
*arg
)
1019 if (!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1021 XSetErrorHandler(xerrordummy
);
1022 XSetCloseDownMode(dpy
, DestroyAll
);
1023 XKillClient(dpy
, selmon
->sel
->win
);
1025 XSetErrorHandler(xerror
);
1031 manage(Window w
, XWindowAttributes
*wa
)
1033 Client
*c
, *t
= NULL
;
1034 Window trans
= None
;
1037 c
= ecalloc(1, sizeof(Client
));
1040 c
->x
= c
->oldx
= wa
->x
;
1041 c
->y
= c
->oldy
= wa
->y
;
1042 c
->w
= c
->oldw
= wa
->width
;
1043 c
->h
= c
->oldh
= wa
->height
;
1044 c
->oldbw
= wa
->border_width
;
1047 if (XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1055 if (c
->x
+ WIDTH(c
) > c
->mon
->wx
+ c
->mon
->ww
)
1056 c
->x
= c
->mon
->wx
+ c
->mon
->ww
- WIDTH(c
);
1057 if (c
->y
+ HEIGHT(c
) > c
->mon
->wy
+ c
->mon
->wh
)
1058 c
->y
= c
->mon
->wy
+ c
->mon
->wh
- HEIGHT(c
);
1059 c
->x
= MAX(c
->x
, c
->mon
->wx
);
1060 c
->y
= MAX(c
->y
, c
->mon
->wy
);
1063 wc
.border_width
= c
->bw
;
1064 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1065 XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1066 configure(c
); /* propagates border_width, if size doesn't change */
1067 updatewindowtype(c
);
1070 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1073 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1075 XRaiseWindow(dpy
, c
->win
);
1078 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1079 (unsigned char *) &(c
->win
), 1);
1080 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1081 setclientstate(c
, NormalState
);
1082 if (c
->mon
== selmon
)
1083 unfocus(selmon
->sel
, 0);
1086 XMapWindow(dpy
, c
->win
);
1091 mappingnotify(XEvent
*e
)
1093 XMappingEvent
*ev
= &e
->xmapping
;
1095 XRefreshKeyboardMapping(ev
);
1096 if (ev
->request
== MappingKeyboard
)
1101 maprequest(XEvent
*e
)
1103 static XWindowAttributes wa
;
1104 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1106 if (!XGetWindowAttributes(dpy
, ev
->window
, &wa
) || wa
.override_redirect
)
1108 if (!wintoclient(ev
->window
))
1109 manage(ev
->window
, &wa
);
1118 for (c
= m
->clients
; c
; c
= c
->next
)
1121 if (n
> 0) /* override layout symbol */
1122 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1123 for (c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1124 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, 0);
1128 motionnotify(XEvent
*e
)
1130 static Monitor
*mon
= NULL
;
1132 XMotionEvent
*ev
= &e
->xmotion
;
1134 if (ev
->window
!= root
)
1136 if ((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1137 unfocus(selmon
->sel
, 1);
1145 movemouse(const Arg
*arg
)
1147 int x
, y
, ocx
, ocy
, nx
, ny
;
1153 if (!(c
= selmon
->sel
))
1155 if (c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1160 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1161 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1163 if (!getrootptr(&x
, &y
))
1166 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1168 case ConfigureRequest
:
1171 handler
[ev
.type
](&ev
);
1174 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1176 lasttime
= ev
.xmotion
.time
;
1178 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1179 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1180 if (abs(selmon
->wx
- nx
) < snap
)
1182 else if (abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1183 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1184 if (abs(selmon
->wy
- ny
) < snap
)
1186 else if (abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1187 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1188 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1189 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1190 togglefloating(NULL
);
1191 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1192 resize(c
, nx
, ny
, c
->w
, c
->h
, 1);
1195 } while (ev
.type
!= ButtonRelease
);
1196 XUngrabPointer(dpy
, CurrentTime
);
1197 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1205 nexttiled(Client
*c
)
1207 for (; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1221 propertynotify(XEvent
*e
)
1225 XPropertyEvent
*ev
= &e
->xproperty
;
1227 if ((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1229 else if (ev
->state
== PropertyDelete
)
1230 return; /* ignore */
1231 else if ((c
= wintoclient(ev
->window
))) {
1234 case XA_WM_TRANSIENT_FOR
:
1235 if (!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1236 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1239 case XA_WM_NORMAL_HINTS
:
1247 if (ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1249 if (c
== c
->mon
->sel
)
1252 if (ev
->atom
== netatom
[NetWMWindowType
])
1253 updatewindowtype(c
);
1258 quit(const Arg
*arg
)
1264 recttomon(int x
, int y
, int w
, int h
)
1266 Monitor
*m
, *r
= selmon
;
1269 for (m
= mons
; m
; m
= m
->next
)
1270 if ((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1278 resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
)
1280 if (applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1281 resizeclient(c
, x
, y
, w
, h
);
1285 resizeclient(Client
*c
, int x
, int y
, int w
, int h
)
1289 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1290 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1291 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1292 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1293 wc
.border_width
= c
->bw
;
1294 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1300 resizemouse(const Arg
*arg
)
1302 int ocx
, ocy
, nw
, nh
;
1308 if (!(c
= selmon
->sel
))
1310 if (c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1315 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1316 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1318 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1320 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1322 case ConfigureRequest
:
1325 handler
[ev
.type
](&ev
);
1328 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1330 lasttime
= ev
.xmotion
.time
;
1332 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1333 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1334 if (c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1335 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1337 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1338 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1339 togglefloating(NULL
);
1341 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1342 resize(c
, c
->x
, c
->y
, nw
, nh
, 1);
1345 } while (ev
.type
!= ButtonRelease
);
1346 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1347 XUngrabPointer(dpy
, CurrentTime
);
1348 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1349 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1366 if (m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1367 XRaiseWindow(dpy
, m
->sel
->win
);
1368 if (m
->lt
[m
->sellt
]->arrange
) {
1369 wc
.stack_mode
= Below
;
1370 wc
.sibling
= m
->barwin
;
1371 for (c
= m
->stack
; c
; c
= c
->snext
)
1372 if (!c
->isfloating
&& ISVISIBLE(c
)) {
1373 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1374 wc
.sibling
= c
->win
;
1378 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1385 /* main event loop */
1387 while (running
&& !XNextEvent(dpy
, &ev
))
1388 if (handler
[ev
.type
])
1389 handler
[ev
.type
](&ev
); /* call handler */
1395 unsigned int i
, num
;
1396 Window d1
, d2
, *wins
= NULL
;
1397 XWindowAttributes wa
;
1399 if (XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1400 for (i
= 0; i
< num
; i
++) {
1401 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1402 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1404 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1405 manage(wins
[i
], &wa
);
1407 for (i
= 0; i
< num
; i
++) { /* now the transients */
1408 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1410 if (XGetTransientForHint(dpy
, wins
[i
], &d1
)
1411 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1412 manage(wins
[i
], &wa
);
1420 sendmon(Client
*c
, Monitor
*m
)
1428 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1436 setclientstate(Client
*c
, long state
)
1438 long data
[] = { state
, None
};
1440 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1441 PropModeReplace
, (unsigned char *)data
, 2);
1445 sendevent(Client
*c
, Atom proto
)
1452 if (XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1453 while (!exists
&& n
--)
1454 exists
= protocols
[n
] == proto
;
1458 ev
.type
= ClientMessage
;
1459 ev
.xclient
.window
= c
->win
;
1460 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1461 ev
.xclient
.format
= 32;
1462 ev
.xclient
.data
.l
[0] = proto
;
1463 ev
.xclient
.data
.l
[1] = CurrentTime
;
1464 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1472 if (!c
->neverfocus
) {
1473 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1474 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1475 XA_WINDOW
, 32, PropModeReplace
,
1476 (unsigned char *) &(c
->win
), 1);
1478 sendevent(c
, wmatom
[WMTakeFocus
]);
1482 setfullscreen(Client
*c
, int fullscreen
)
1484 if (fullscreen
&& !c
->isfullscreen
) {
1485 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1486 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1487 c
->isfullscreen
= 1;
1488 c
->oldstate
= c
->isfloating
;
1492 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1493 XRaiseWindow(dpy
, c
->win
);
1494 } else if (!fullscreen
&& c
->isfullscreen
){
1495 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1496 PropModeReplace
, (unsigned char*)0, 0);
1497 c
->isfullscreen
= 0;
1498 c
->isfloating
= c
->oldstate
;
1504 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1510 setlayout(const Arg
*arg
)
1512 if (!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1515 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1516 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1523 /* arg > 1.0 will set mfact absolutely */
1525 setmfact(const Arg
*arg
)
1529 if (!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1531 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1532 if (f
< 0.05 || f
> 0.95)
1542 XSetWindowAttributes wa
;
1544 struct sigaction sa
;
1546 /* do not transform children into zombies when they terminate */
1547 sigemptyset(&sa
.sa_mask
);
1548 sa
.sa_flags
= SA_NOCLDSTOP
| SA_NOCLDWAIT
| SA_RESTART
;
1549 sa
.sa_handler
= SIG_IGN
;
1550 sigaction(SIGCHLD
, &sa
, NULL
);
1552 /* clean up any zombies (inherited from .xinitrc etc) immediately */
1553 while (waitpid(-1, NULL
, WNOHANG
) > 0);
1556 screen
= DefaultScreen(dpy
);
1557 sw
= DisplayWidth(dpy
, screen
);
1558 sh
= DisplayHeight(dpy
, screen
);
1559 root
= RootWindow(dpy
, screen
);
1560 drw
= drw_create(dpy
, screen
, root
, sw
, sh
);
1561 if (!drw_fontset_create(drw
, fonts
, LENGTH(fonts
)))
1562 die("no fonts could be loaded.");
1563 lrpad
= drw
->fonts
->h
;
1564 bh
= drw
->fonts
->h
+ 2;
1567 utf8string
= XInternAtom(dpy
, "UTF8_STRING", False
);
1568 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1569 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1570 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1571 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1572 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1573 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1574 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1575 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1576 netatom
[NetWMCheck
] = XInternAtom(dpy
, "_NET_SUPPORTING_WM_CHECK", False
);
1577 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1578 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1579 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1580 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1582 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1583 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1584 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1585 /* init appearance */
1586 scheme
= ecalloc(LENGTH(colors
), sizeof(Clr
*));
1587 for (i
= 0; i
< LENGTH(colors
); i
++)
1588 scheme
[i
] = drw_scm_create(drw
, colors
[i
], 3);
1592 /* supporting window for NetWMCheck */
1593 wmcheckwin
= XCreateSimpleWindow(dpy
, root
, 0, 0, 1, 1, 0, 0, 0);
1594 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1595 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1596 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMName
], utf8string
, 8,
1597 PropModeReplace
, (unsigned char *) "dwm", 3);
1598 XChangeProperty(dpy
, root
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1599 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1600 /* EWMH support per view */
1601 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1602 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1603 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1605 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1606 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1607 |ButtonPressMask
|PointerMotionMask
|EnterWindowMask
1608 |LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1609 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1610 XSelectInput(dpy
, root
, wa
.event_mask
);
1616 seturgent(Client
*c
, int urg
)
1621 if (!(wmh
= XGetWMHints(dpy
, c
->win
)))
1623 wmh
->flags
= urg
? (wmh
->flags
| XUrgencyHint
) : (wmh
->flags
& ~XUrgencyHint
);
1624 XSetWMHints(dpy
, c
->win
, wmh
);
1634 /* show clients top down */
1635 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1636 if ((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1637 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, 0);
1640 /* hide clients bottom up */
1642 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1647 spawn(const Arg
*arg
)
1649 struct sigaction sa
;
1651 if (arg
->v
== dmenucmd
)
1652 dmenumon
[0] = '0' + selmon
->num
;
1655 close(ConnectionNumber(dpy
));
1658 sigemptyset(&sa
.sa_mask
);
1660 sa
.sa_handler
= SIG_DFL
;
1661 sigaction(SIGCHLD
, &sa
, NULL
);
1663 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1664 die("dwm: execvp '%s' failed:", ((char **)arg
->v
)[0]);
1671 if (selmon
->sel
&& arg
->ui
& TAGMASK
) {
1672 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1679 tagmon(const Arg
*arg
)
1681 if (!selmon
->sel
|| !mons
->next
)
1683 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1689 unsigned int i
, n
, h
, mw
, my
, ty
;
1692 for (n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1697 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1700 for (i
= my
= ty
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1701 if (i
< m
->nmaster
) {
1702 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
);
1703 resize(c
, m
->wx
, m
->wy
+ my
, mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1704 if (my
+ HEIGHT(c
) < m
->wh
)
1707 h
= (m
->wh
- ty
) / (n
- i
);
1708 resize(c
, m
->wx
+ mw
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1709 if (ty
+ HEIGHT(c
) < m
->wh
)
1715 togglebar(const Arg
*arg
)
1717 selmon
->showbar
= !selmon
->showbar
;
1718 updatebarpos(selmon
);
1719 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1724 togglefloating(const Arg
*arg
)
1728 if (selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1730 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1731 if (selmon
->sel
->isfloating
)
1732 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1733 selmon
->sel
->w
, selmon
->sel
->h
, 0);
1738 toggletag(const Arg
*arg
)
1740 unsigned int newtags
;
1744 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1746 selmon
->sel
->tags
= newtags
;
1753 toggleview(const Arg
*arg
)
1755 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1758 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1765 unfocus(Client
*c
, int setfocus
)
1770 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1772 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1773 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1778 unmanage(Client
*c
, int destroyed
)
1780 Monitor
*m
= c
->mon
;
1786 wc
.border_width
= c
->oldbw
;
1787 XGrabServer(dpy
); /* avoid race conditions */
1788 XSetErrorHandler(xerrordummy
);
1789 XSelectInput(dpy
, c
->win
, NoEventMask
);
1790 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1791 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1792 setclientstate(c
, WithdrawnState
);
1794 XSetErrorHandler(xerror
);
1804 unmapnotify(XEvent
*e
)
1807 XUnmapEvent
*ev
= &e
->xunmap
;
1809 if ((c
= wintoclient(ev
->window
))) {
1811 setclientstate(c
, WithdrawnState
);
1821 XSetWindowAttributes wa
= {
1822 .override_redirect
= True
,
1823 .background_pixmap
= ParentRelative
,
1824 .event_mask
= ButtonPressMask
|ExposureMask
1826 XClassHint ch
= {"dwm", "dwm"};
1827 for (m
= mons
; m
; m
= m
->next
) {
1830 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1831 CopyFromParent
, DefaultVisual(dpy
, screen
),
1832 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1833 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
1834 XMapRaised(dpy
, m
->barwin
);
1835 XSetClassHint(dpy
, m
->barwin
, &ch
);
1840 updatebarpos(Monitor
*m
)
1846 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1847 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1853 updateclientlist(void)
1858 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1859 for (m
= mons
; m
; m
= m
->next
)
1860 for (c
= m
->clients
; c
; c
= c
->next
)
1861 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
1862 XA_WINDOW
, 32, PropModeAppend
,
1863 (unsigned char *) &(c
->win
), 1);
1872 if (XineramaIsActive(dpy
)) {
1876 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1877 XineramaScreenInfo
*unique
= NULL
;
1879 for (n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1880 /* only consider unique geometries as separate screens */
1881 unique
= ecalloc(nn
, sizeof(XineramaScreenInfo
));
1882 for (i
= 0, j
= 0; i
< nn
; i
++)
1883 if (isuniquegeom(unique
, j
, &info
[i
]))
1884 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1888 /* new monitors if nn > n */
1889 for (i
= n
; i
< nn
; i
++) {
1890 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1892 m
->next
= createmon();
1896 for (i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1898 || unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1899 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
)
1903 m
->mx
= m
->wx
= unique
[i
].x_org
;
1904 m
->my
= m
->wy
= unique
[i
].y_org
;
1905 m
->mw
= m
->ww
= unique
[i
].width
;
1906 m
->mh
= m
->wh
= unique
[i
].height
;
1909 /* removed monitors if n > nn */
1910 for (i
= nn
; i
< n
; i
++) {
1911 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1912 while ((c
= m
->clients
)) {
1914 m
->clients
= c
->next
;
1926 #endif /* XINERAMA */
1927 { /* default monitor setup */
1930 if (mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1932 mons
->mw
= mons
->ww
= sw
;
1933 mons
->mh
= mons
->wh
= sh
;
1939 selmon
= wintomon(root
);
1945 updatenumlockmask(void)
1948 XModifierKeymap
*modmap
;
1951 modmap
= XGetModifierMapping(dpy
);
1952 for (i
= 0; i
< 8; i
++)
1953 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1954 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1955 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1956 numlockmask
= (1 << i
);
1957 XFreeModifiermap(modmap
);
1961 updatesizehints(Client
*c
)
1966 if (!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1967 /* size is uninitialized, ensure that size.flags aren't used */
1969 if (size
.flags
& PBaseSize
) {
1970 c
->basew
= size
.base_width
;
1971 c
->baseh
= size
.base_height
;
1972 } else if (size
.flags
& PMinSize
) {
1973 c
->basew
= size
.min_width
;
1974 c
->baseh
= size
.min_height
;
1976 c
->basew
= c
->baseh
= 0;
1977 if (size
.flags
& PResizeInc
) {
1978 c
->incw
= size
.width_inc
;
1979 c
->inch
= size
.height_inc
;
1981 c
->incw
= c
->inch
= 0;
1982 if (size
.flags
& PMaxSize
) {
1983 c
->maxw
= size
.max_width
;
1984 c
->maxh
= size
.max_height
;
1986 c
->maxw
= c
->maxh
= 0;
1987 if (size
.flags
& PMinSize
) {
1988 c
->minw
= size
.min_width
;
1989 c
->minh
= size
.min_height
;
1990 } else if (size
.flags
& PBaseSize
) {
1991 c
->minw
= size
.base_width
;
1992 c
->minh
= size
.base_height
;
1994 c
->minw
= c
->minh
= 0;
1995 if (size
.flags
& PAspect
) {
1996 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1997 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1999 c
->maxa
= c
->mina
= 0.0;
2000 c
->isfixed
= (c
->maxw
&& c
->maxh
&& c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
2007 if (!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
2008 strcpy(stext
, "dwm-"VERSION
);
2013 updatetitle(Client
*c
)
2015 if (!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
2016 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
2017 if (c
->name
[0] == '\0') /* hack to mark broken clients */
2018 strcpy(c
->name
, broken
);
2022 updatewindowtype(Client
*c
)
2024 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
2025 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
2027 if (state
== netatom
[NetWMFullscreen
])
2028 setfullscreen(c
, 1);
2029 if (wtype
== netatom
[NetWMWindowTypeDialog
])
2034 updatewmhints(Client
*c
)
2038 if ((wmh
= XGetWMHints(dpy
, c
->win
))) {
2039 if (c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2040 wmh
->flags
&= ~XUrgencyHint
;
2041 XSetWMHints(dpy
, c
->win
, wmh
);
2043 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? 1 : 0;
2044 if (wmh
->flags
& InputHint
)
2045 c
->neverfocus
= !wmh
->input
;
2053 view(const Arg
*arg
)
2055 if ((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2057 selmon
->seltags
^= 1; /* toggle sel tagset */
2058 if (arg
->ui
& TAGMASK
)
2059 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2065 wintoclient(Window w
)
2070 for (m
= mons
; m
; m
= m
->next
)
2071 for (c
= m
->clients
; c
; c
= c
->next
)
2084 if (w
== root
&& getrootptr(&x
, &y
))
2085 return recttomon(x
, y
, 1, 1);
2086 for (m
= mons
; m
; m
= m
->next
)
2089 if ((c
= wintoclient(w
)))
2094 /* There's no way to check accesses to destroyed windows, thus those cases are
2095 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2096 * default error handler, which may call exit. */
2098 xerror(Display
*dpy
, XErrorEvent
*ee
)
2100 if (ee
->error_code
== BadWindow
2101 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2102 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2103 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2104 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2105 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2106 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2107 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2108 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2110 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2111 ee
->request_code
, ee
->error_code
);
2112 return xerrorxlib(dpy
, ee
); /* may call exit */
2116 xerrordummy(Display
*dpy
, XErrorEvent
*ee
)
2121 /* Startup Error handler to check if another window manager
2122 * is already running. */
2124 xerrorstart(Display
*dpy
, XErrorEvent
*ee
)
2126 die("dwm: another window manager is already running");
2131 zoom(const Arg
*arg
)
2133 Client
*c
= selmon
->sel
;
2135 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| !c
|| c
->isfloating
)
2137 if (c
== nexttiled(selmon
->clients
) && !(c
= nexttiled(c
->next
)))
2143 main(int argc
, char *argv
[])
2145 if (argc
== 2 && !strcmp("-v", argv
[1]))
2148 die("usage: dwm [-v]");
2149 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2150 fputs("warning: no locale support\n", stderr
);
2151 if (!(dpy
= XOpenDisplay(NULL
)))
2152 die("dwm: cannot open display");
2156 if (pledge("stdio rpath proc exec", NULL
) == -1)
2158 #endif /* __OpenBSD__ */
2163 return EXIT_SUCCESS
;