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>
43 #include <X11/Xlib-xcb.h>
46 #include <sys/sysctl.h>
48 #endif /* __OpenBSD */
54 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
55 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
56 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
57 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
58 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
59 #define LENGTH(X) (sizeof X / sizeof X[0])
60 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
61 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
62 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
63 #define TAGMASK ((1 << LENGTH(tags)) - 1)
64 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
69 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
70 enum { SchemeNorm
, SchemeSel
}; /* color schemes */
71 enum { NetSupported
, NetWMName
, NetWMState
, NetWMCheck
,
72 NetWMFullscreen
, NetActiveWindow
, NetWMWindowType
,
73 NetWMWindowTypeDialog
, NetClientList
, NetLast
}; /* EWMH atoms */
74 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, WMLast
}; /* default atoms */
75 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
76 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
89 void (*func
)(const Arg
*arg
);
93 typedef struct Monitor Monitor
;
94 typedef struct Client Client
;
99 int oldx
, oldy
, oldw
, oldh
;
100 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
103 int isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
, isterminal
, noswallow
;
115 void (*func
)(const Arg
*);
121 void (*arrange
)(Monitor
*);
129 int by
; /* bar geometry */
130 int mx
, my
, mw
, mh
; /* screen size */
131 int wx
, wy
, ww
, wh
; /* window area */
132 int gappx
; /* gaps between windows */
133 unsigned int seltags
;
135 unsigned int tagset
[2];
148 const char *instance
;
157 /* function declarations */
158 static void applyrules(Client
*c
);
159 static int applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
);
160 static void arrange(Monitor
*m
);
161 static void arrangemon(Monitor
*m
);
162 static void attach(Client
*c
);
163 static void attachstack(Client
*c
);
164 static void buttonpress(XEvent
*e
);
165 static void checkotherwm(void);
166 static void cleanup(void);
167 static void cleanupmon(Monitor
*mon
);
168 static void clientmessage(XEvent
*e
);
169 static void configure(Client
*c
);
170 static void configurenotify(XEvent
*e
);
171 static void configurerequest(XEvent
*e
);
172 static Monitor
*createmon(void);
173 static void destroynotify(XEvent
*e
);
174 static void detach(Client
*c
);
175 static void detachstack(Client
*c
);
176 static Monitor
*dirtomon(int dir
);
177 static void drawbar(Monitor
*m
);
178 static void drawbars(void);
179 static void enternotify(XEvent
*e
);
180 static void expose(XEvent
*e
);
181 static void focus(Client
*c
);
182 static void focusin(XEvent
*e
);
183 static void focusmon(const Arg
*arg
);
184 static void focusstack(const Arg
*arg
);
185 static Atom
getatomprop(Client
*c
, Atom prop
);
186 static int getrootptr(int *x
, int *y
);
187 static long getstate(Window w
);
188 static int gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
189 static void grabbuttons(Client
*c
, int focused
);
190 static void grabkeys(void);
191 static void incnmaster(const Arg
*arg
);
192 static void keypress(XEvent
*e
);
193 static void killclient(const Arg
*arg
);
194 static void manage(Window w
, XWindowAttributes
*wa
);
195 static void mappingnotify(XEvent
*e
);
196 static void maprequest(XEvent
*e
);
197 static void monocle(Monitor
*m
);
198 static void motionnotify(XEvent
*e
);
199 static void movemouse(const Arg
*arg
);
200 static Client
*nexttiled(Client
*c
);
201 static void pop(Client
*);
202 static void propertynotify(XEvent
*e
);
203 static void quit(const Arg
*arg
);
204 static Monitor
*recttomon(int x
, int y
, int w
, int h
);
205 static void resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
);
206 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
207 static void resizemouse(const Arg
*arg
);
208 static void restack(Monitor
*m
);
209 static void run(void);
210 static void scan(void);
211 static int sendevent(Client
*c
, Atom proto
);
212 static void sendmon(Client
*c
, Monitor
*m
);
213 static void setclientstate(Client
*c
, long state
);
214 static void setfocus(Client
*c
);
215 static void setfullscreen(Client
*c
, int fullscreen
);
216 static void setgaps(const Arg
*arg
);
217 static void setlayout(const Arg
*arg
);
218 static void setmfact(const Arg
*arg
);
219 static void setup(void);
220 static void seturgent(Client
*c
, int urg
);
221 static void showhide(Client
*c
);
222 static void sigchld(int unused
);
223 static void sighup(int unused
);
224 static void sigterm(int unused
);
225 static void spawn(const Arg
*arg
);
226 static void tag(const Arg
*arg
);
227 static void tagmon(const Arg
*arg
);
228 static void tile(Monitor
*);
229 static void togglebar(const Arg
*arg
);
230 static void togglefloating(const Arg
*arg
);
231 static void toggletag(const Arg
*arg
);
232 static void toggleview(const Arg
*arg
);
233 static void unfocus(Client
*c
, int setfocus
);
234 static void unmanage(Client
*c
, int destroyed
);
235 static void unmapnotify(XEvent
*e
);
236 static void updatebarpos(Monitor
*m
);
237 static void updatebars(void);
238 static void updateclientlist(void);
239 static int updategeom(void);
240 static void updatenumlockmask(void);
241 static void updatesizehints(Client
*c
);
242 static void updatestatus(void);
243 static void updatetitle(Client
*c
);
244 static void updatewindowtype(Client
*c
);
245 static void updatewmhints(Client
*c
);
246 static void view(const Arg
*arg
);
247 static Client
*wintoclient(Window w
);
248 static Monitor
*wintomon(Window w
);
249 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
250 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
251 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
252 static void xinitvisual();
253 static void zoom(const Arg
*arg
);
255 static pid_t
getparentprocess(pid_t p
);
256 static int isdescprocess(pid_t p
, pid_t c
);
257 static Client
*swallowingclient(Window w
);
258 static Client
*termforwin(const Client
*c
);
259 static pid_t
winpid(Window w
);
262 static const char broken
[] = "broken";
263 static char stext
[256];
265 static int sw
, sh
; /* X display screen geometry width, height */
266 static int bh
, blw
= 0; /* bar geometry */
267 static int lrpad
; /* sum of left and right padding for text */
268 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
269 static unsigned int numlockmask
= 0;
270 static void (*handler
[LASTEvent
]) (XEvent
*) = {
271 [ButtonPress
] = buttonpress
,
272 [ClientMessage
] = clientmessage
,
273 [ConfigureRequest
] = configurerequest
,
274 [ConfigureNotify
] = configurenotify
,
275 [DestroyNotify
] = destroynotify
,
276 [EnterNotify
] = enternotify
,
279 [KeyPress
] = keypress
,
280 [MappingNotify
] = mappingnotify
,
281 [MapRequest
] = maprequest
,
282 [MotionNotify
] = motionnotify
,
283 [PropertyNotify
] = propertynotify
,
284 [UnmapNotify
] = unmapnotify
286 static Atom wmatom
[WMLast
], netatom
[NetLast
];
287 static int restart
= 0;
288 static int running
= 1;
289 static Cur
*cursor
[CurLast
];
293 static Monitor
*mons
, *selmon
;
294 static Window root
, wmcheckwin
;
296 static xcb_connection_t
*xcon
;
298 static int useargb
= 0;
299 static Visual
*visual
;
301 static Colormap cmap
;
303 /* configuration, allows nested code to access above variables */
306 /* compile-time check if all tags fit into an unsigned int bit array. */
307 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
309 /* function implementations */
311 applyrules(Client
*c
)
313 const char *class, *instance
;
317 XClassHint ch
= { NULL
, NULL
};
322 XGetClassHint(dpy
, c
->win
, &ch
);
323 class = ch
.res_class
? ch
.res_class
: broken
;
324 instance
= ch
.res_name
? ch
.res_name
: broken
;
326 for (i
= 0; i
< LENGTH(rules
); i
++) {
328 if ((!r
->title
|| strstr(c
->name
, r
->title
))
329 && (!r
->class || strstr(class, r
->class))
330 && (!r
->instance
|| strstr(instance
, r
->instance
)))
332 c
->isterminal
= r
->isterminal
;
333 c
->noswallow
= r
->noswallow
;
334 c
->isfloating
= r
->isfloating
;
336 for (m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
345 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
349 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
)
354 /* set minimum possible */
362 if (*x
+ *w
+ 2 * c
->bw
< 0)
364 if (*y
+ *h
+ 2 * c
->bw
< 0)
367 if (*x
>= m
->wx
+ m
->ww
)
368 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
369 if (*y
>= m
->wy
+ m
->wh
)
370 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
371 if (*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
373 if (*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
380 if (resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
381 /* see last two sentences in ICCCM 4.1.2.3 */
382 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
383 if (!baseismin
) { /* temporarily remove base dimensions */
387 /* adjust for aspect limits */
388 if (c
->mina
> 0 && c
->maxa
> 0) {
389 if (c
->maxa
< (float)*w
/ *h
)
390 *w
= *h
* c
->maxa
+ 0.5;
391 else if (c
->mina
< (float)*h
/ *w
)
392 *h
= *w
* c
->mina
+ 0.5;
394 if (baseismin
) { /* increment calculation requires this */
398 /* adjust for increment value */
403 /* restore base dimensions */
404 *w
= MAX(*w
+ c
->basew
, c
->minw
);
405 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
407 *w
= MIN(*w
, c
->maxw
);
409 *h
= MIN(*h
, c
->maxh
);
411 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
419 else for (m
= mons
; m
; m
= m
->next
)
424 } else for (m
= mons
; m
; m
= m
->next
)
429 arrangemon(Monitor
*m
)
431 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
432 if (m
->lt
[m
->sellt
]->arrange
)
433 m
->lt
[m
->sellt
]->arrange(m
);
439 c
->next
= c
->mon
->clients
;
444 attachstack(Client
*c
)
446 c
->snext
= c
->mon
->stack
;
451 swallow(Client
*p
, Client
*c
)
454 if (c
->noswallow
|| c
->isterminal
)
456 if (c
->noswallow
&& !swallowfloating
&& c
->isfloating
)
462 setclientstate(c
, WithdrawnState
);
463 XUnmapWindow(dpy
, p
->win
);
472 XMoveResizeWindow(dpy
, p
->win
, p
->x
, p
->y
, p
->w
, p
->h
);
481 c
->win
= c
->swallowing
->win
;
484 c
->swallowing
= NULL
;
486 /* unfullscreen the client */
490 XMapWindow(dpy
, c
->win
);
491 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
492 setclientstate(c
, NormalState
);
498 buttonpress(XEvent
*e
)
500 unsigned int i
, x
, click
;
504 XButtonPressedEvent
*ev
= &e
->xbutton
;
507 /* focus monitor if necessary */
508 if ((m
= wintomon(ev
->window
)) && m
!= selmon
) {
509 unfocus(selmon
->sel
, 1);
513 if (ev
->window
== selmon
->barwin
) {
517 while (ev
->x
>= x
&& ++i
< LENGTH(tags
));
518 if (i
< LENGTH(tags
)) {
521 } else if (ev
->x
< x
+ blw
)
523 else if (ev
->x
> selmon
->ww
- (int)TEXTW(stext
))
524 click
= ClkStatusText
;
527 } else if ((c
= wintoclient(ev
->window
))) {
530 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
531 click
= ClkClientWin
;
533 for (i
= 0; i
< LENGTH(buttons
); i
++)
534 if (click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
535 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
536 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
542 xerrorxlib
= XSetErrorHandler(xerrorstart
);
543 /* this causes an error if some other window manager is running */
544 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
546 XSetErrorHandler(xerror
);
554 Layout foo
= { "", NULL
};
559 selmon
->lt
[selmon
->sellt
] = &foo
;
560 for (m
= mons
; m
; m
= m
->next
)
562 unmanage(m
->stack
, 0);
563 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
566 for (i
= 0; i
< CurLast
; i
++)
567 drw_cur_free(drw
, cursor
[i
]);
568 for (i
= 0; i
< LENGTH(colors
); i
++)
571 XDestroyWindow(dpy
, wmcheckwin
);
574 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
575 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
579 cleanupmon(Monitor
*mon
)
586 for (m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
589 XUnmapWindow(dpy
, mon
->barwin
);
590 XDestroyWindow(dpy
, mon
->barwin
);
595 clientmessage(XEvent
*e
)
597 XClientMessageEvent
*cme
= &e
->xclient
;
598 Client
*c
= wintoclient(cme
->window
);
602 if (cme
->message_type
== netatom
[NetWMState
]) {
603 if (cme
->data
.l
[1] == netatom
[NetWMFullscreen
]
604 || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
605 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
606 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
607 } else if (cme
->message_type
== netatom
[NetActiveWindow
]) {
608 if (c
!= selmon
->sel
&& !c
->isurgent
)
618 ce
.type
= ConfigureNotify
;
626 ce
.border_width
= c
->bw
;
628 ce
.override_redirect
= False
;
629 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
633 configurenotify(XEvent
*e
)
637 XConfigureEvent
*ev
= &e
->xconfigure
;
640 /* TODO: updategeom handling sucks, needs to be simplified */
641 if (ev
->window
== root
) {
642 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
645 if (updategeom() || dirty
) {
646 drw_resize(drw
, sw
, bh
);
648 for (m
= mons
; m
; m
= m
->next
) {
649 for (c
= m
->clients
; c
; c
= c
->next
)
651 resizeclient(c
, m
->mx
, m
->my
, m
->mw
, m
->mh
);
652 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
661 configurerequest(XEvent
*e
)
665 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
668 if ((c
= wintoclient(ev
->window
))) {
669 if (ev
->value_mask
& CWBorderWidth
)
670 c
->bw
= ev
->border_width
;
671 else if (c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
673 if (ev
->value_mask
& CWX
) {
675 c
->x
= m
->mx
+ ev
->x
;
677 if (ev
->value_mask
& CWY
) {
679 c
->y
= m
->my
+ ev
->y
;
681 if (ev
->value_mask
& CWWidth
) {
685 if (ev
->value_mask
& CWHeight
) {
689 if ((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
690 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
691 if ((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
692 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
693 if ((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
696 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
702 wc
.width
= ev
->width
;
703 wc
.height
= ev
->height
;
704 wc
.border_width
= ev
->border_width
;
705 wc
.sibling
= ev
->above
;
706 wc
.stack_mode
= ev
->detail
;
707 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
717 m
= ecalloc(1, sizeof(Monitor
));
718 m
->tagset
[0] = m
->tagset
[1] = 1;
720 m
->nmaster
= nmaster
;
721 m
->showbar
= showbar
;
724 m
->lt
[0] = &layouts
[0];
725 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
726 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
731 destroynotify(XEvent
*e
)
734 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
736 if ((c
= wintoclient(ev
->window
)))
739 else if ((c
= swallowingclient(ev
->window
)))
740 unmanage(c
->swallowing
, 1);
748 for (tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
753 detachstack(Client
*c
)
757 for (tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
760 if (c
== c
->mon
->sel
) {
761 for (t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
772 if (!(m
= selmon
->next
))
774 } else if (selmon
== mons
)
775 for (m
= mons
; m
->next
; m
= m
->next
);
777 for (m
= mons
; m
->next
!= selmon
; m
= m
->next
);
785 int boxs
= drw
->fonts
->h
/ 9;
786 int boxw
= drw
->fonts
->h
/ 6 + 2;
787 unsigned int i
, occ
= 0, urg
= 0;
793 /* draw status first so it can be overdrawn by tags later */
794 if (m
== selmon
) { /* status is only drawn on selected monitor */
795 drw_setscheme(drw
, scheme
[SchemeNorm
]);
796 tw
= TEXTW(stext
) - lrpad
+ 2; /* 2px right padding */
797 drw_text(drw
, m
->ww
- tw
, 0, tw
, bh
, 0, stext
, 0);
800 for (c
= m
->clients
; c
; c
= c
->next
) {
806 for (i
= 0; i
< LENGTH(tags
); i
++) {
808 drw_setscheme(drw
, scheme
[m
->tagset
[m
->seltags
] & 1 << i
? SchemeSel
: SchemeNorm
]);
809 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, tags
[i
], urg
& 1 << i
);
811 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
,
812 m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
816 w
= blw
= TEXTW(m
->ltsymbol
);
817 drw_setscheme(drw
, scheme
[SchemeNorm
]);
818 x
= drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->ltsymbol
, 0);
820 if ((w
= m
->ww
- tw
- x
) > bh
) {
822 drw_setscheme(drw
, scheme
[m
== selmon
? SchemeSel
: SchemeNorm
]);
823 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->sel
->name
, 0);
824 if (m
->sel
->isfloating
)
825 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
, m
->sel
->isfixed
, 0);
827 drw_setscheme(drw
, scheme
[SchemeNorm
]);
828 drw_rect(drw
, x
, 0, w
, bh
, 1, 1);
831 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
);
839 for (m
= mons
; m
; m
= m
->next
)
844 enternotify(XEvent
*e
)
848 XCrossingEvent
*ev
= &e
->xcrossing
;
850 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
852 c
= wintoclient(ev
->window
);
853 m
= c
? c
->mon
: wintomon(ev
->window
);
855 unfocus(selmon
->sel
, 1);
857 } else if (!c
|| c
== selmon
->sel
)
866 XExposeEvent
*ev
= &e
->xexpose
;
868 if (ev
->count
== 0 && (m
= wintomon(ev
->window
)))
875 if (!c
|| !ISVISIBLE(c
))
876 for (c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
877 if (selmon
->sel
&& selmon
->sel
!= c
)
878 unfocus(selmon
->sel
, 0);
880 if (c
->mon
!= selmon
)
887 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
][ColBorder
].pixel
);
890 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
891 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
897 /* there are some broken focus acquiring clients needing extra handling */
901 XFocusChangeEvent
*ev
= &e
->xfocus
;
903 if (selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
904 setfocus(selmon
->sel
);
908 focusmon(const Arg
*arg
)
914 if ((m
= dirtomon(arg
->i
)) == selmon
)
916 unfocus(selmon
->sel
, 0);
922 focusstack(const Arg
*arg
)
924 Client
*c
= NULL
, *i
;
926 if (!selmon
->sel
|| (selmon
->sel
->isfullscreen
&& lockfullscreen
))
929 for (c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
931 for (c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
933 for (i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
937 for (; i
; i
= i
->next
)
948 getatomprop(Client
*c
, Atom prop
)
952 unsigned char *p
= NULL
;
953 Atom da
, atom
= None
;
955 if (XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
956 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
964 getrootptr(int *x
, int *y
)
970 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
978 unsigned char *p
= NULL
;
979 unsigned long n
, extra
;
982 if (XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
983 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
992 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
998 if (!text
|| size
== 0)
1001 if (!XGetTextProperty(dpy
, w
, &name
, atom
) || !name
.nitems
)
1003 if (name
.encoding
== XA_STRING
)
1004 strncpy(text
, (char *)name
.value
, size
- 1);
1006 if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
1007 strncpy(text
, *list
, size
- 1);
1008 XFreeStringList(list
);
1011 text
[size
- 1] = '\0';
1017 grabbuttons(Client
*c
, int focused
)
1019 updatenumlockmask();
1022 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1023 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1025 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
1026 BUTTONMASK
, GrabModeSync
, GrabModeSync
, None
, None
);
1027 for (i
= 0; i
< LENGTH(buttons
); i
++)
1028 if (buttons
[i
].click
== ClkClientWin
)
1029 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1030 XGrabButton(dpy
, buttons
[i
].button
,
1031 buttons
[i
].mask
| modifiers
[j
],
1032 c
->win
, False
, BUTTONMASK
,
1033 GrabModeAsync
, GrabModeSync
, None
, None
);
1040 updatenumlockmask();
1043 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1046 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
1047 for (i
= 0; i
< LENGTH(keys
); i
++)
1048 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
1049 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1050 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
1051 True
, GrabModeAsync
, GrabModeAsync
);
1056 incnmaster(const Arg
*arg
)
1058 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
1064 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
)
1067 if (unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
1068 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
1072 #endif /* XINERAMA */
1082 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1083 for (i
= 0; i
< LENGTH(keys
); i
++)
1084 if (keysym
== keys
[i
].keysym
1085 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1087 keys
[i
].func(&(keys
[i
].arg
));
1091 killclient(const Arg
*arg
)
1095 if (!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1097 XSetErrorHandler(xerrordummy
);
1098 XSetCloseDownMode(dpy
, DestroyAll
);
1099 XKillClient(dpy
, selmon
->sel
->win
);
1101 XSetErrorHandler(xerror
);
1107 manage(Window w
, XWindowAttributes
*wa
)
1109 Client
*c
, *t
= NULL
, *term
= NULL
;
1110 Window trans
= None
;
1113 c
= ecalloc(1, sizeof(Client
));
1117 c
->x
= c
->oldx
= wa
->x
;
1118 c
->y
= c
->oldy
= wa
->y
;
1119 c
->w
= c
->oldw
= wa
->width
;
1120 c
->h
= c
->oldh
= wa
->height
;
1121 c
->oldbw
= wa
->border_width
;
1124 if (XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1130 term
= termforwin(c
);
1133 if (c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1134 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1135 if (c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1136 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1137 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1138 /* only fix client y-offset, if the client center might cover the bar */
1139 c
->y
= MAX(c
->y
, ((c
->mon
->by
== c
->mon
->my
) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1140 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1143 wc
.border_width
= c
->bw
;
1144 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1145 XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1146 configure(c
); /* propagates border_width, if size doesn't change */
1147 updatewindowtype(c
);
1150 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1153 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1155 XRaiseWindow(dpy
, c
->win
);
1158 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1159 (unsigned char *) &(c
->win
), 1);
1160 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1161 setclientstate(c
, NormalState
);
1162 if (c
->mon
== selmon
)
1163 unfocus(selmon
->sel
, 0);
1166 XMapWindow(dpy
, c
->win
);
1173 mappingnotify(XEvent
*e
)
1175 XMappingEvent
*ev
= &e
->xmapping
;
1177 XRefreshKeyboardMapping(ev
);
1178 if (ev
->request
== MappingKeyboard
)
1183 maprequest(XEvent
*e
)
1185 static XWindowAttributes wa
;
1186 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1188 if (!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1190 if (wa
.override_redirect
)
1192 if (!wintoclient(ev
->window
))
1193 manage(ev
->window
, &wa
);
1202 for (c
= m
->clients
; c
; c
= c
->next
)
1205 if (n
> 0) /* override layout symbol */
1206 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1207 for (c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1208 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, 0);
1212 motionnotify(XEvent
*e
)
1214 static Monitor
*mon
= NULL
;
1216 XMotionEvent
*ev
= &e
->xmotion
;
1218 if (ev
->window
!= root
)
1220 if ((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1221 unfocus(selmon
->sel
, 1);
1229 movemouse(const Arg
*arg
)
1231 int x
, y
, ocx
, ocy
, nx
, ny
;
1237 if (!(c
= selmon
->sel
))
1239 if (c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1244 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1245 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1247 if (!getrootptr(&x
, &y
))
1250 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1252 case ConfigureRequest
:
1255 handler
[ev
.type
](&ev
);
1258 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1260 lasttime
= ev
.xmotion
.time
;
1262 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1263 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1264 if (abs(selmon
->wx
- nx
) < snap
)
1266 else if (abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1267 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1268 if (abs(selmon
->wy
- ny
) < snap
)
1270 else if (abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1271 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1272 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1273 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1274 togglefloating(NULL
);
1275 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1276 resize(c
, nx
, ny
, c
->w
, c
->h
, 1);
1279 } while (ev
.type
!= ButtonRelease
);
1280 XUngrabPointer(dpy
, CurrentTime
);
1281 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1289 nexttiled(Client
*c
)
1291 for (; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1305 propertynotify(XEvent
*e
)
1309 XPropertyEvent
*ev
= &e
->xproperty
;
1311 if ((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1313 else if (ev
->state
== PropertyDelete
)
1314 return; /* ignore */
1315 else if ((c
= wintoclient(ev
->window
))) {
1318 case XA_WM_TRANSIENT_FOR
:
1319 if (!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1320 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1323 case XA_WM_NORMAL_HINTS
:
1331 if (ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1333 if (c
== c
->mon
->sel
)
1336 if (ev
->atom
== netatom
[NetWMWindowType
])
1337 updatewindowtype(c
);
1342 quit(const Arg
*arg
)
1344 if(arg
->i
) restart
= 1;
1349 recttomon(int x
, int y
, int w
, int h
)
1351 Monitor
*m
, *r
= selmon
;
1354 for (m
= mons
; m
; m
= m
->next
)
1355 if ((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1363 resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
)
1365 if (applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1366 resizeclient(c
, x
, y
, w
, h
);
1370 resizeclient(Client
*c
, int x
, int y
, int w
, int h
)
1374 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1375 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1376 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1377 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1378 wc
.border_width
= c
->bw
;
1379 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1385 resizemouse(const Arg
*arg
)
1387 int ocx
, ocy
, nw
, nh
;
1393 if (!(c
= selmon
->sel
))
1395 if (c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1400 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1401 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1403 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1405 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1407 case ConfigureRequest
:
1410 handler
[ev
.type
](&ev
);
1413 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1415 lasttime
= ev
.xmotion
.time
;
1417 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1418 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1419 if (c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1420 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1422 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1423 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1424 togglefloating(NULL
);
1426 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1427 resize(c
, c
->x
, c
->y
, nw
, nh
, 1);
1430 } while (ev
.type
!= ButtonRelease
);
1431 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1432 XUngrabPointer(dpy
, CurrentTime
);
1433 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1434 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1451 if (m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1452 XRaiseWindow(dpy
, m
->sel
->win
);
1453 if (m
->lt
[m
->sellt
]->arrange
) {
1454 wc
.stack_mode
= Below
;
1455 wc
.sibling
= m
->barwin
;
1456 for (c
= m
->stack
; c
; c
= c
->snext
)
1457 if (!c
->isfloating
&& ISVISIBLE(c
)) {
1458 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1459 wc
.sibling
= c
->win
;
1463 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1470 /* main event loop */
1472 while (running
&& !XNextEvent(dpy
, &ev
))
1473 if (handler
[ev
.type
])
1474 handler
[ev
.type
](&ev
); /* call handler */
1480 unsigned int i
, num
;
1481 Window d1
, d2
, *wins
= NULL
;
1482 XWindowAttributes wa
;
1484 if (XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1485 for (i
= 0; i
< num
; i
++) {
1486 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1487 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1489 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1490 manage(wins
[i
], &wa
);
1492 for (i
= 0; i
< num
; i
++) { /* now the transients */
1493 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1495 if (XGetTransientForHint(dpy
, wins
[i
], &d1
)
1496 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1497 manage(wins
[i
], &wa
);
1505 sendmon(Client
*c
, Monitor
*m
)
1513 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1521 setclientstate(Client
*c
, long state
)
1523 long data
[] = { state
, None
};
1525 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1526 PropModeReplace
, (unsigned char *)data
, 2);
1530 sendevent(Client
*c
, Atom proto
)
1537 if (XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1538 while (!exists
&& n
--)
1539 exists
= protocols
[n
] == proto
;
1543 ev
.type
= ClientMessage
;
1544 ev
.xclient
.window
= c
->win
;
1545 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1546 ev
.xclient
.format
= 32;
1547 ev
.xclient
.data
.l
[0] = proto
;
1548 ev
.xclient
.data
.l
[1] = CurrentTime
;
1549 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1557 if (!c
->neverfocus
) {
1558 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1559 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1560 XA_WINDOW
, 32, PropModeReplace
,
1561 (unsigned char *) &(c
->win
), 1);
1563 sendevent(c
, wmatom
[WMTakeFocus
]);
1567 setfullscreen(Client
*c
, int fullscreen
)
1569 if (fullscreen
&& !c
->isfullscreen
) {
1570 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1571 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1572 c
->isfullscreen
= 1;
1573 c
->oldstate
= c
->isfloating
;
1577 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1578 XRaiseWindow(dpy
, c
->win
);
1579 } else if (!fullscreen
&& c
->isfullscreen
){
1580 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1581 PropModeReplace
, (unsigned char*)0, 0);
1582 c
->isfullscreen
= 0;
1583 c
->isfloating
= c
->oldstate
;
1589 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1595 setgaps(const Arg
*arg
)
1597 if ((arg
->i
== 0) || (selmon
->gappx
+ arg
->i
< 0))
1600 selmon
->gappx
+= arg
->i
;
1605 setlayout(const Arg
*arg
)
1607 if (!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1610 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1611 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1618 /* arg > 1.0 will set mfact absolutely */
1620 setmfact(const Arg
*arg
)
1624 if (!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1626 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1627 if (f
< 0.05 || f
> 0.95)
1637 XSetWindowAttributes wa
;
1640 /* clean up any zombies immediately */
1643 signal(SIGHUP
, sighup
);
1644 signal(SIGTERM
, sigterm
);
1647 screen
= DefaultScreen(dpy
);
1648 sw
= DisplayWidth(dpy
, screen
);
1649 sh
= DisplayHeight(dpy
, screen
);
1650 root
= RootWindow(dpy
, screen
);
1652 drw
= drw_create(dpy
, screen
, root
, sw
, sh
, visual
, depth
, cmap
);
1653 if (!drw_fontset_create(drw
, fonts
, LENGTH(fonts
)))
1654 die("no fonts could be loaded.");
1655 lrpad
= drw
->fonts
->h
;
1656 bh
= drw
->fonts
->h
+ 2;
1659 utf8string
= XInternAtom(dpy
, "UTF8_STRING", False
);
1660 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1661 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1662 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1663 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1664 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1665 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1666 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1667 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1668 netatom
[NetWMCheck
] = XInternAtom(dpy
, "_NET_SUPPORTING_WM_CHECK", False
);
1669 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1670 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1671 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1672 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1674 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1675 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1676 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1677 /* init appearance */
1678 scheme
= ecalloc(LENGTH(colors
), sizeof(Clr
*));
1679 for (i
= 0; i
< LENGTH(colors
); i
++)
1680 scheme
[i
] = drw_scm_create(drw
, colors
[i
], alphas
[i
], 3);
1684 /* supporting window for NetWMCheck */
1685 wmcheckwin
= XCreateSimpleWindow(dpy
, root
, 0, 0, 1, 1, 0, 0, 0);
1686 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1687 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1688 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMName
], utf8string
, 8,
1689 PropModeReplace
, (unsigned char *) "dwm", 3);
1690 XChangeProperty(dpy
, root
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1691 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1692 /* EWMH support per view */
1693 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1694 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1695 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1697 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1698 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1699 |ButtonPressMask
|PointerMotionMask
|EnterWindowMask
1700 |LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1701 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1702 XSelectInput(dpy
, root
, wa
.event_mask
);
1709 seturgent(Client
*c
, int urg
)
1714 if (!(wmh
= XGetWMHints(dpy
, c
->win
)))
1716 wmh
->flags
= urg
? (wmh
->flags
| XUrgencyHint
) : (wmh
->flags
& ~XUrgencyHint
);
1717 XSetWMHints(dpy
, c
->win
, wmh
);
1727 /* show clients top down */
1728 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1729 if ((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1730 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, 0);
1733 /* hide clients bottom up */
1735 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1742 if (signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1743 die("can't install SIGCHLD handler:");
1744 while (0 < waitpid(-1, NULL
, WNOHANG
));
1762 spawn(const Arg
*arg
)
1764 if (arg
->v
== dmenucmd
)
1765 dmenumon
[0] = '0' + selmon
->num
;
1768 close(ConnectionNumber(dpy
));
1770 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1771 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1780 if (selmon
->sel
&& arg
->ui
& TAGMASK
) {
1781 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1788 tagmon(const Arg
*arg
)
1790 if (!selmon
->sel
|| !mons
->next
)
1792 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1798 unsigned int i
, n
, h
, mw
, my
, ty
;
1801 for (n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1806 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1808 mw
= m
->ww
- m
->gappx
;
1809 for (i
= 0, my
= ty
= m
->gappx
, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1810 if (i
< m
->nmaster
) {
1811 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
) - m
->gappx
;
1812 resize(c
, m
->wx
+ m
->gappx
, m
->wy
+ my
, mw
- (2*c
->bw
) - m
->gappx
, h
- (2*c
->bw
), 0);
1813 if (my
+ HEIGHT(c
) + m
->gappx
< m
->wh
)
1814 my
+= HEIGHT(c
) + m
->gappx
;
1816 h
= (m
->wh
- ty
) / (n
- i
) - m
->gappx
;
1817 resize(c
, m
->wx
+ mw
+ m
->gappx
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
) - 2*m
->gappx
, h
- (2*c
->bw
), 0);
1818 if (ty
+ HEIGHT(c
) + m
->gappx
< m
->wh
)
1819 ty
+= HEIGHT(c
) + m
->gappx
;
1824 togglebar(const Arg
*arg
)
1826 selmon
->showbar
= !selmon
->showbar
;
1827 updatebarpos(selmon
);
1828 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1833 togglefloating(const Arg
*arg
)
1837 if (selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1839 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1840 if (selmon
->sel
->isfloating
)
1841 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1842 selmon
->sel
->w
, selmon
->sel
->h
, 0);
1847 toggletag(const Arg
*arg
)
1849 unsigned int newtags
;
1853 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1855 selmon
->sel
->tags
= newtags
;
1862 toggleview(const Arg
*arg
)
1864 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1867 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1874 unfocus(Client
*c
, int setfocus
)
1879 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1881 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1882 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1887 unmanage(Client
*c
, int destroyed
)
1889 Monitor
*m
= c
->mon
;
1892 if (c
->swallowing
) {
1897 Client
*s
= swallowingclient(c
->win
);
1899 free(s
->swallowing
);
1900 s
->swallowing
= NULL
;
1909 wc
.border_width
= c
->oldbw
;
1910 XGrabServer(dpy
); /* avoid race conditions */
1911 XSetErrorHandler(xerrordummy
);
1912 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1913 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1914 setclientstate(c
, WithdrawnState
);
1916 XSetErrorHandler(xerror
);
1929 unmapnotify(XEvent
*e
)
1932 XUnmapEvent
*ev
= &e
->xunmap
;
1934 if ((c
= wintoclient(ev
->window
))) {
1936 setclientstate(c
, WithdrawnState
);
1946 XSetWindowAttributes wa
= {
1947 .override_redirect
= True
,
1948 .background_pixel
= 0,
1951 .event_mask
= ButtonPressMask
|ExposureMask
1953 XClassHint ch
= {"dwm", "dwm"};
1954 for (m
= mons
; m
; m
= m
->next
) {
1957 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, depth
,
1958 InputOutput
, visual
,
1959 CWOverrideRedirect
|CWBackPixel
|CWBorderPixel
|CWColormap
|CWEventMask
, &wa
);
1960 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
1961 XMapRaised(dpy
, m
->barwin
);
1962 XSetClassHint(dpy
, m
->barwin
, &ch
);
1967 updatebarpos(Monitor
*m
)
1973 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1974 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1985 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1986 for (m
= mons
; m
; m
= m
->next
)
1987 for (c
= m
->clients
; c
; c
= c
->next
)
1988 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
1989 XA_WINDOW
, 32, PropModeAppend
,
1990 (unsigned char *) &(c
->win
), 1);
1999 if (XineramaIsActive(dpy
)) {
2003 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
2004 XineramaScreenInfo
*unique
= NULL
;
2006 for (n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
2007 /* only consider unique geometries as separate screens */
2008 unique
= ecalloc(nn
, sizeof(XineramaScreenInfo
));
2009 for (i
= 0, j
= 0; i
< nn
; i
++)
2010 if (isuniquegeom(unique
, j
, &info
[i
]))
2011 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
2014 if (n
<= nn
) { /* new monitors available */
2015 for (i
= 0; i
< (nn
- n
); i
++) {
2016 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
2018 m
->next
= createmon();
2022 for (i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
2024 || unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
2025 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
)
2029 m
->mx
= m
->wx
= unique
[i
].x_org
;
2030 m
->my
= m
->wy
= unique
[i
].y_org
;
2031 m
->mw
= m
->ww
= unique
[i
].width
;
2032 m
->mh
= m
->wh
= unique
[i
].height
;
2035 } else { /* less monitors available nn < n */
2036 for (i
= nn
; i
< n
; i
++) {
2037 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
2038 while ((c
= m
->clients
)) {
2040 m
->clients
= c
->next
;
2053 #endif /* XINERAMA */
2054 { /* default monitor setup */
2057 if (mons
->mw
!= sw
|| mons
->mh
!= sh
) {
2059 mons
->mw
= mons
->ww
= sw
;
2060 mons
->mh
= mons
->wh
= sh
;
2066 selmon
= wintomon(root
);
2072 updatenumlockmask(void)
2075 XModifierKeymap
*modmap
;
2078 modmap
= XGetModifierMapping(dpy
);
2079 for (i
= 0; i
< 8; i
++)
2080 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
2081 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
2082 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
2083 numlockmask
= (1 << i
);
2084 XFreeModifiermap(modmap
);
2088 updatesizehints(Client
*c
)
2093 if (!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
2094 /* size is uninitialized, ensure that size.flags aren't used */
2096 if (size
.flags
& PBaseSize
) {
2097 c
->basew
= size
.base_width
;
2098 c
->baseh
= size
.base_height
;
2099 } else if (size
.flags
& PMinSize
) {
2100 c
->basew
= size
.min_width
;
2101 c
->baseh
= size
.min_height
;
2103 c
->basew
= c
->baseh
= 0;
2104 if (size
.flags
& PResizeInc
) {
2105 c
->incw
= size
.width_inc
;
2106 c
->inch
= size
.height_inc
;
2108 c
->incw
= c
->inch
= 0;
2109 if (size
.flags
& PMaxSize
) {
2110 c
->maxw
= size
.max_width
;
2111 c
->maxh
= size
.max_height
;
2113 c
->maxw
= c
->maxh
= 0;
2114 if (size
.flags
& PMinSize
) {
2115 c
->minw
= size
.min_width
;
2116 c
->minh
= size
.min_height
;
2117 } else if (size
.flags
& PBaseSize
) {
2118 c
->minw
= size
.base_width
;
2119 c
->minh
= size
.base_height
;
2121 c
->minw
= c
->minh
= 0;
2122 if (size
.flags
& PAspect
) {
2123 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
2124 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
2126 c
->maxa
= c
->mina
= 0.0;
2127 c
->isfixed
= (c
->maxw
&& c
->maxh
&& c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
2133 if (!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
2134 strcpy(stext
, "dwm-"VERSION
);
2139 updatetitle(Client
*c
)
2141 if (!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
2142 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
2143 if (c
->name
[0] == '\0') /* hack to mark broken clients */
2144 strcpy(c
->name
, broken
);
2148 updatewindowtype(Client
*c
)
2150 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
2151 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
2153 if (state
== netatom
[NetWMFullscreen
])
2154 setfullscreen(c
, 1);
2155 if (wtype
== netatom
[NetWMWindowTypeDialog
])
2160 updatewmhints(Client
*c
)
2164 if ((wmh
= XGetWMHints(dpy
, c
->win
))) {
2165 if (c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2166 wmh
->flags
&= ~XUrgencyHint
;
2167 XSetWMHints(dpy
, c
->win
, wmh
);
2169 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? 1 : 0;
2170 if (wmh
->flags
& InputHint
)
2171 c
->neverfocus
= !wmh
->input
;
2179 view(const Arg
*arg
)
2181 if ((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2183 selmon
->seltags
^= 1; /* toggle sel tagset */
2184 if (arg
->ui
& TAGMASK
)
2185 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2197 xcb_res_client_id_spec_t spec
= {0};
2199 spec
.mask
= XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID
;
2201 xcb_generic_error_t
*e
= NULL
;
2202 xcb_res_query_client_ids_cookie_t c
= xcb_res_query_client_ids(xcon
, 1, &spec
);
2203 xcb_res_query_client_ids_reply_t
*r
= xcb_res_query_client_ids_reply(xcon
, c
, &e
);
2208 xcb_res_client_id_value_iterator_t i
= xcb_res_query_client_ids_ids_iterator(r
);
2209 for (; i
.rem
; xcb_res_client_id_value_next(&i
)) {
2210 spec
= i
.data
->spec
;
2211 if (spec
.mask
& XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID
) {
2212 uint32_t *t
= xcb_res_client_id_value_value(i
.data
);
2220 if (result
== (pid_t
)-1)
2223 #endif /* __linux__ */
2228 unsigned long len
, bytes
;
2229 unsigned char *prop
;
2232 if (XGetWindowProperty(dpy
, w
, XInternAtom(dpy
, "_NET_WM_PID", 0), 0, 1, False
, AnyPropertyType
, &type
, &format
, &len
, &bytes
, &prop
) != Success
|| !prop
)
2235 ret
= *(pid_t
*)prop
;
2239 #endif /* __OpenBSD__ */
2244 getparentprocess(pid_t p
)
2251 snprintf(buf
, sizeof(buf
) - 1, "/proc/%u/stat", (unsigned)p
);
2253 if (!(f
= fopen(buf
, "r")))
2256 fscanf(f
, "%*u %*s %*c %u", &v
);
2258 #endif /* __linux__*/
2263 struct kinfo_proc
*kp
;
2265 kd
= kvm_openfiles(NULL
, NULL
, NULL
, KVM_NO_FILES
, NULL
);
2269 kp
= kvm_getprocs(kd
, KERN_PROC_PID
, p
, sizeof(*kp
), &n
);
2271 #endif /* __OpenBSD__ */
2277 isdescprocess(pid_t p
, pid_t c
)
2279 while (p
!= c
&& c
!= 0)
2280 c
= getparentprocess(c
);
2286 termforwin(const Client
*w
)
2291 if (!w
->pid
|| w
->isterminal
)
2294 for (m
= mons
; m
; m
= m
->next
) {
2295 for (c
= m
->clients
; c
; c
= c
->next
) {
2296 if (c
->isterminal
&& !c
->swallowing
&& c
->pid
&& isdescprocess(c
->pid
, w
->pid
))
2305 swallowingclient(Window w
)
2310 for (m
= mons
; m
; m
= m
->next
) {
2311 for (c
= m
->clients
; c
; c
= c
->next
) {
2312 if (c
->swallowing
&& c
->swallowing
->win
== w
)
2321 wintoclient(Window w
)
2326 for (m
= mons
; m
; m
= m
->next
)
2327 for (c
= m
->clients
; c
; c
= c
->next
)
2340 if (w
== root
&& getrootptr(&x
, &y
))
2341 return recttomon(x
, y
, 1, 1);
2342 for (m
= mons
; m
; m
= m
->next
)
2345 if ((c
= wintoclient(w
)))
2350 /* There's no way to check accesses to destroyed windows, thus those cases are
2351 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2352 * default error handler, which may call exit. */
2354 xerror(Display
*dpy
, XErrorEvent
*ee
)
2356 if (ee
->error_code
== BadWindow
2357 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2358 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2359 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2360 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2361 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2362 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2363 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2364 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2366 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2367 ee
->request_code
, ee
->error_code
);
2368 return xerrorxlib(dpy
, ee
); /* may call exit */
2372 xerrordummy(Display
*dpy
, XErrorEvent
*ee
)
2377 /* Startup Error handler to check if another window manager
2378 * is already running. */
2380 xerrorstart(Display
*dpy
, XErrorEvent
*ee
)
2382 die("dwm: another window manager is already running");
2390 XRenderPictFormat
*fmt
;
2399 long masks
= VisualScreenMask
| VisualDepthMask
| VisualClassMask
;
2401 infos
= XGetVisualInfo(dpy
, masks
, &tpl
, &nitems
);
2403 for(i
= 0; i
< nitems
; i
++) {
2404 fmt
= XRenderFindVisualFormat(dpy
, infos
[i
].visual
);
2405 if (fmt
->type
== PictTypeDirect
&& fmt
->direct
.alphaMask
) {
2406 visual
= infos
[i
].visual
;
2407 depth
= infos
[i
].depth
;
2408 cmap
= XCreateColormap(dpy
, root
, visual
, AllocNone
);
2417 visual
= DefaultVisual(dpy
, screen
);
2418 depth
= DefaultDepth(dpy
, screen
);
2419 cmap
= DefaultColormap(dpy
, screen
);
2424 zoom(const Arg
*arg
)
2426 Client
*c
= selmon
->sel
;
2428 if (!selmon
->lt
[selmon
->sellt
]->arrange
2429 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2431 if (c
== nexttiled(selmon
->clients
))
2432 if (!c
|| !(c
= nexttiled(c
->next
)))
2438 main(int argc
, char *argv
[])
2440 if (argc
== 2 && !strcmp("-v", argv
[1]))
2443 die("usage: dwm [-v]");
2444 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2445 fputs("warning: no locale support\n", stderr
);
2446 if (!(dpy
= XOpenDisplay(NULL
)))
2447 die("dwm: cannot open display");
2448 if (!(xcon
= XGetXCBConnection(dpy
)))
2449 die("dwm: cannot get xcb connection\n");
2453 if (pledge("stdio rpath proc exec ps", NULL
) == -1)
2455 #endif /* __OpenBSD__ */
2458 if(restart
) execvp(argv
[0], argv
);
2461 return EXIT_SUCCESS
;