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 togglefullscr(const Arg
*arg
);
232 static void toggletag(const Arg
*arg
);
233 static void toggleview(const Arg
*arg
);
234 static void unfocus(Client
*c
, int setfocus
);
235 static void unmanage(Client
*c
, int destroyed
);
236 static void unmapnotify(XEvent
*e
);
237 static void updatebarpos(Monitor
*m
);
238 static void updatebars(void);
239 static void updateclientlist(void);
240 static int updategeom(void);
241 static void updatenumlockmask(void);
242 static void updatesizehints(Client
*c
);
243 static void updatestatus(void);
244 static void updatetitle(Client
*c
);
245 static void updatewindowtype(Client
*c
);
246 static void updatewmhints(Client
*c
);
247 static void view(const Arg
*arg
);
248 static Client
*wintoclient(Window w
);
249 static Monitor
*wintomon(Window w
);
250 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
251 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
252 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
253 static void xinitvisual();
254 static void zoom(const Arg
*arg
);
256 static pid_t
getparentprocess(pid_t p
);
257 static int isdescprocess(pid_t p
, pid_t c
);
258 static Client
*swallowingclient(Window w
);
259 static Client
*termforwin(const Client
*c
);
260 static pid_t
winpid(Window w
);
263 static const char broken
[] = "broken";
264 static char stext
[256];
266 static int sw
, sh
; /* X display screen geometry width, height */
267 static int bh
, blw
= 0; /* bar geometry */
268 static int lrpad
; /* sum of left and right padding for text */
269 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
270 static unsigned int numlockmask
= 0;
271 static void (*handler
[LASTEvent
]) (XEvent
*) = {
272 [ButtonPress
] = buttonpress
,
273 [ClientMessage
] = clientmessage
,
274 [ConfigureRequest
] = configurerequest
,
275 [ConfigureNotify
] = configurenotify
,
276 [DestroyNotify
] = destroynotify
,
277 [EnterNotify
] = enternotify
,
280 [KeyPress
] = keypress
,
281 [MappingNotify
] = mappingnotify
,
282 [MapRequest
] = maprequest
,
283 [MotionNotify
] = motionnotify
,
284 [PropertyNotify
] = propertynotify
,
285 [UnmapNotify
] = unmapnotify
287 static Atom wmatom
[WMLast
], netatom
[NetLast
];
288 static int restart
= 0;
289 static int running
= 1;
290 static Cur
*cursor
[CurLast
];
294 static Monitor
*mons
, *selmon
;
295 static Window root
, wmcheckwin
;
297 static xcb_connection_t
*xcon
;
299 static int useargb
= 0;
300 static Visual
*visual
;
302 static Colormap cmap
;
304 /* configuration, allows nested code to access above variables */
307 /* compile-time check if all tags fit into an unsigned int bit array. */
308 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
310 /* function implementations */
312 applyrules(Client
*c
)
314 const char *class, *instance
;
318 XClassHint ch
= { NULL
, NULL
};
323 XGetClassHint(dpy
, c
->win
, &ch
);
324 class = ch
.res_class
? ch
.res_class
: broken
;
325 instance
= ch
.res_name
? ch
.res_name
: broken
;
327 for (i
= 0; i
< LENGTH(rules
); i
++) {
329 if ((!r
->title
|| strstr(c
->name
, r
->title
))
330 && (!r
->class || strstr(class, r
->class))
331 && (!r
->instance
|| strstr(instance
, r
->instance
)))
333 c
->isterminal
= r
->isterminal
;
334 c
->noswallow
= r
->noswallow
;
335 c
->isfloating
= r
->isfloating
;
337 for (m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
346 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
350 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
)
355 /* set minimum possible */
363 if (*x
+ *w
+ 2 * c
->bw
< 0)
365 if (*y
+ *h
+ 2 * c
->bw
< 0)
368 if (*x
>= m
->wx
+ m
->ww
)
369 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
370 if (*y
>= m
->wy
+ m
->wh
)
371 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
372 if (*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
374 if (*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
381 if (resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
382 /* see last two sentences in ICCCM 4.1.2.3 */
383 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
384 if (!baseismin
) { /* temporarily remove base dimensions */
388 /* adjust for aspect limits */
389 if (c
->mina
> 0 && c
->maxa
> 0) {
390 if (c
->maxa
< (float)*w
/ *h
)
391 *w
= *h
* c
->maxa
+ 0.5;
392 else if (c
->mina
< (float)*h
/ *w
)
393 *h
= *w
* c
->mina
+ 0.5;
395 if (baseismin
) { /* increment calculation requires this */
399 /* adjust for increment value */
404 /* restore base dimensions */
405 *w
= MAX(*w
+ c
->basew
, c
->minw
);
406 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
408 *w
= MIN(*w
, c
->maxw
);
410 *h
= MIN(*h
, c
->maxh
);
412 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
420 else for (m
= mons
; m
; m
= m
->next
)
425 } else for (m
= mons
; m
; m
= m
->next
)
430 arrangemon(Monitor
*m
)
432 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
433 if (m
->lt
[m
->sellt
]->arrange
)
434 m
->lt
[m
->sellt
]->arrange(m
);
440 c
->next
= c
->mon
->clients
;
445 attachstack(Client
*c
)
447 c
->snext
= c
->mon
->stack
;
452 swallow(Client
*p
, Client
*c
)
455 if (c
->noswallow
|| c
->isterminal
)
457 if (c
->noswallow
&& !swallowfloating
&& c
->isfloating
)
463 setclientstate(c
, WithdrawnState
);
464 XUnmapWindow(dpy
, p
->win
);
473 XMoveResizeWindow(dpy
, p
->win
, p
->x
, p
->y
, p
->w
, p
->h
);
482 c
->win
= c
->swallowing
->win
;
485 c
->swallowing
= NULL
;
487 /* unfullscreen the client */
491 XMapWindow(dpy
, c
->win
);
492 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
493 setclientstate(c
, NormalState
);
499 buttonpress(XEvent
*e
)
501 unsigned int i
, x
, click
;
505 XButtonPressedEvent
*ev
= &e
->xbutton
;
508 /* focus monitor if necessary */
509 if ((m
= wintomon(ev
->window
)) && m
!= selmon
) {
510 unfocus(selmon
->sel
, 1);
514 if (ev
->window
== selmon
->barwin
) {
518 while (ev
->x
>= x
&& ++i
< LENGTH(tags
));
519 if (i
< LENGTH(tags
)) {
522 } else if (ev
->x
< x
+ blw
)
524 else if (ev
->x
> selmon
->ww
- (int)TEXTW(stext
))
525 click
= ClkStatusText
;
528 } else if ((c
= wintoclient(ev
->window
))) {
531 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
532 click
= ClkClientWin
;
534 for (i
= 0; i
< LENGTH(buttons
); i
++)
535 if (click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
536 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
537 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
543 xerrorxlib
= XSetErrorHandler(xerrorstart
);
544 /* this causes an error if some other window manager is running */
545 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
547 XSetErrorHandler(xerror
);
555 Layout foo
= { "", NULL
};
560 selmon
->lt
[selmon
->sellt
] = &foo
;
561 for (m
= mons
; m
; m
= m
->next
)
563 unmanage(m
->stack
, 0);
564 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
567 for (i
= 0; i
< CurLast
; i
++)
568 drw_cur_free(drw
, cursor
[i
]);
569 for (i
= 0; i
< LENGTH(colors
); i
++)
572 XDestroyWindow(dpy
, wmcheckwin
);
575 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
576 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
580 cleanupmon(Monitor
*mon
)
587 for (m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
590 XUnmapWindow(dpy
, mon
->barwin
);
591 XDestroyWindow(dpy
, mon
->barwin
);
596 clientmessage(XEvent
*e
)
598 XClientMessageEvent
*cme
= &e
->xclient
;
599 Client
*c
= wintoclient(cme
->window
);
603 if (cme
->message_type
== netatom
[NetWMState
]) {
604 if (cme
->data
.l
[1] == netatom
[NetWMFullscreen
]
605 || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
606 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
607 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
608 } else if (cme
->message_type
== netatom
[NetActiveWindow
]) {
609 if (c
!= selmon
->sel
&& !c
->isurgent
)
619 ce
.type
= ConfigureNotify
;
627 ce
.border_width
= c
->bw
;
629 ce
.override_redirect
= False
;
630 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
634 configurenotify(XEvent
*e
)
638 XConfigureEvent
*ev
= &e
->xconfigure
;
641 /* TODO: updategeom handling sucks, needs to be simplified */
642 if (ev
->window
== root
) {
643 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
646 if (updategeom() || dirty
) {
647 drw_resize(drw
, sw
, bh
);
649 for (m
= mons
; m
; m
= m
->next
) {
650 for (c
= m
->clients
; c
; c
= c
->next
)
652 resizeclient(c
, m
->mx
, m
->my
, m
->mw
, m
->mh
);
653 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
662 configurerequest(XEvent
*e
)
666 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
669 if ((c
= wintoclient(ev
->window
))) {
670 if (ev
->value_mask
& CWBorderWidth
)
671 c
->bw
= ev
->border_width
;
672 else if (c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
674 if (ev
->value_mask
& CWX
) {
676 c
->x
= m
->mx
+ ev
->x
;
678 if (ev
->value_mask
& CWY
) {
680 c
->y
= m
->my
+ ev
->y
;
682 if (ev
->value_mask
& CWWidth
) {
686 if (ev
->value_mask
& CWHeight
) {
690 if ((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
691 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
692 if ((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
693 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
694 if ((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
697 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
703 wc
.width
= ev
->width
;
704 wc
.height
= ev
->height
;
705 wc
.border_width
= ev
->border_width
;
706 wc
.sibling
= ev
->above
;
707 wc
.stack_mode
= ev
->detail
;
708 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
718 m
= ecalloc(1, sizeof(Monitor
));
719 m
->tagset
[0] = m
->tagset
[1] = 1;
721 m
->nmaster
= nmaster
;
722 m
->showbar
= showbar
;
725 m
->lt
[0] = &layouts
[0];
726 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
727 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
732 destroynotify(XEvent
*e
)
735 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
737 if ((c
= wintoclient(ev
->window
)))
740 else if ((c
= swallowingclient(ev
->window
)))
741 unmanage(c
->swallowing
, 1);
749 for (tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
754 detachstack(Client
*c
)
758 for (tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
761 if (c
== c
->mon
->sel
) {
762 for (t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
773 if (!(m
= selmon
->next
))
775 } else if (selmon
== mons
)
776 for (m
= mons
; m
->next
; m
= m
->next
);
778 for (m
= mons
; m
->next
!= selmon
; m
= m
->next
);
786 int boxs
= drw
->fonts
->h
/ 9;
787 int boxw
= drw
->fonts
->h
/ 6 + 2;
788 unsigned int i
, occ
= 0, urg
= 0;
794 /* draw status first so it can be overdrawn by tags later */
795 if (m
== selmon
) { /* status is only drawn on selected monitor */
796 drw_setscheme(drw
, scheme
[SchemeNorm
]);
797 tw
= TEXTW(stext
) - lrpad
+ 2; /* 2px right padding */
798 drw_text(drw
, m
->ww
- tw
, 0, tw
, bh
, 0, stext
, 0);
801 for (c
= m
->clients
; c
; c
= c
->next
) {
807 for (i
= 0; i
< LENGTH(tags
); i
++) {
809 drw_setscheme(drw
, scheme
[m
->tagset
[m
->seltags
] & 1 << i
? SchemeSel
: SchemeNorm
]);
810 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, tags
[i
], urg
& 1 << i
);
812 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
,
813 m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
817 w
= blw
= TEXTW(m
->ltsymbol
);
818 drw_setscheme(drw
, scheme
[SchemeNorm
]);
819 x
= drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->ltsymbol
, 0);
821 if ((w
= m
->ww
- tw
- x
) > bh
) {
823 drw_setscheme(drw
, scheme
[m
== selmon
? SchemeSel
: SchemeNorm
]);
824 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->sel
->name
, 0);
825 if (m
->sel
->isfloating
)
826 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
, m
->sel
->isfixed
, 0);
828 drw_setscheme(drw
, scheme
[SchemeNorm
]);
829 drw_rect(drw
, x
, 0, w
, bh
, 1, 1);
832 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
);
840 for (m
= mons
; m
; m
= m
->next
)
845 enternotify(XEvent
*e
)
849 XCrossingEvent
*ev
= &e
->xcrossing
;
851 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
853 c
= wintoclient(ev
->window
);
854 m
= c
? c
->mon
: wintomon(ev
->window
);
856 unfocus(selmon
->sel
, 1);
858 } else if (!c
|| c
== selmon
->sel
)
867 XExposeEvent
*ev
= &e
->xexpose
;
869 if (ev
->count
== 0 && (m
= wintomon(ev
->window
)))
876 if (!c
|| !ISVISIBLE(c
))
877 for (c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
878 if (selmon
->sel
&& selmon
->sel
!= c
)
879 unfocus(selmon
->sel
, 0);
881 if (c
->mon
!= selmon
)
888 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
][ColBorder
].pixel
);
891 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
892 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
898 /* there are some broken focus acquiring clients needing extra handling */
902 XFocusChangeEvent
*ev
= &e
->xfocus
;
904 if (selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
905 setfocus(selmon
->sel
);
909 focusmon(const Arg
*arg
)
915 if ((m
= dirtomon(arg
->i
)) == selmon
)
917 unfocus(selmon
->sel
, 0);
923 focusstack(const Arg
*arg
)
925 Client
*c
= NULL
, *i
;
927 if (!selmon
->sel
|| (selmon
->sel
->isfullscreen
&& lockfullscreen
))
930 for (c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
932 for (c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
934 for (i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
938 for (; i
; i
= i
->next
)
949 getatomprop(Client
*c
, Atom prop
)
953 unsigned char *p
= NULL
;
954 Atom da
, atom
= None
;
956 if (XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
957 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
965 getrootptr(int *x
, int *y
)
971 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
979 unsigned char *p
= NULL
;
980 unsigned long n
, extra
;
983 if (XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
984 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
993 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
999 if (!text
|| size
== 0)
1002 if (!XGetTextProperty(dpy
, w
, &name
, atom
) || !name
.nitems
)
1004 if (name
.encoding
== XA_STRING
)
1005 strncpy(text
, (char *)name
.value
, size
- 1);
1007 if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
1008 strncpy(text
, *list
, size
- 1);
1009 XFreeStringList(list
);
1012 text
[size
- 1] = '\0';
1018 grabbuttons(Client
*c
, int focused
)
1020 updatenumlockmask();
1023 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1024 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1026 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
1027 BUTTONMASK
, GrabModeSync
, GrabModeSync
, None
, None
);
1028 for (i
= 0; i
< LENGTH(buttons
); i
++)
1029 if (buttons
[i
].click
== ClkClientWin
)
1030 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1031 XGrabButton(dpy
, buttons
[i
].button
,
1032 buttons
[i
].mask
| modifiers
[j
],
1033 c
->win
, False
, BUTTONMASK
,
1034 GrabModeAsync
, GrabModeSync
, None
, None
);
1041 updatenumlockmask();
1044 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1047 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
1048 for (i
= 0; i
< LENGTH(keys
); i
++)
1049 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
1050 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1051 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
1052 True
, GrabModeAsync
, GrabModeAsync
);
1057 incnmaster(const Arg
*arg
)
1059 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
1065 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
)
1068 if (unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
1069 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
1073 #endif /* XINERAMA */
1083 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1084 for (i
= 0; i
< LENGTH(keys
); i
++)
1085 if (keysym
== keys
[i
].keysym
1086 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1088 keys
[i
].func(&(keys
[i
].arg
));
1092 killclient(const Arg
*arg
)
1096 if (!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1098 XSetErrorHandler(xerrordummy
);
1099 XSetCloseDownMode(dpy
, DestroyAll
);
1100 XKillClient(dpy
, selmon
->sel
->win
);
1102 XSetErrorHandler(xerror
);
1108 manage(Window w
, XWindowAttributes
*wa
)
1110 Client
*c
, *t
= NULL
, *term
= NULL
;
1111 Window trans
= None
;
1114 c
= ecalloc(1, sizeof(Client
));
1118 c
->x
= c
->oldx
= wa
->x
;
1119 c
->y
= c
->oldy
= wa
->y
;
1120 c
->w
= c
->oldw
= wa
->width
;
1121 c
->h
= c
->oldh
= wa
->height
;
1122 c
->oldbw
= wa
->border_width
;
1125 if (XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1131 term
= termforwin(c
);
1134 if (c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1135 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1136 if (c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1137 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1138 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1139 /* only fix client y-offset, if the client center might cover the bar */
1140 c
->y
= MAX(c
->y
, ((c
->mon
->by
== c
->mon
->my
) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1141 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1144 wc
.border_width
= c
->bw
;
1145 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1146 XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1147 configure(c
); /* propagates border_width, if size doesn't change */
1148 updatewindowtype(c
);
1151 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1154 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1156 XRaiseWindow(dpy
, c
->win
);
1159 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1160 (unsigned char *) &(c
->win
), 1);
1161 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1162 setclientstate(c
, NormalState
);
1163 if (c
->mon
== selmon
)
1164 unfocus(selmon
->sel
, 0);
1167 XMapWindow(dpy
, c
->win
);
1174 mappingnotify(XEvent
*e
)
1176 XMappingEvent
*ev
= &e
->xmapping
;
1178 XRefreshKeyboardMapping(ev
);
1179 if (ev
->request
== MappingKeyboard
)
1184 maprequest(XEvent
*e
)
1186 static XWindowAttributes wa
;
1187 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1189 if (!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1191 if (wa
.override_redirect
)
1193 if (!wintoclient(ev
->window
))
1194 manage(ev
->window
, &wa
);
1203 for (c
= m
->clients
; c
; c
= c
->next
)
1206 if (n
> 0) /* override layout symbol */
1207 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1208 for (c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1209 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, 0);
1213 motionnotify(XEvent
*e
)
1215 static Monitor
*mon
= NULL
;
1217 XMotionEvent
*ev
= &e
->xmotion
;
1219 if (ev
->window
!= root
)
1221 if ((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1222 unfocus(selmon
->sel
, 1);
1230 movemouse(const Arg
*arg
)
1232 int x
, y
, ocx
, ocy
, nx
, ny
;
1238 if (!(c
= selmon
->sel
))
1240 if (c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1245 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1246 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1248 if (!getrootptr(&x
, &y
))
1251 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1253 case ConfigureRequest
:
1256 handler
[ev
.type
](&ev
);
1259 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1261 lasttime
= ev
.xmotion
.time
;
1263 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1264 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1265 if (abs(selmon
->wx
- nx
) < snap
)
1267 else if (abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1268 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1269 if (abs(selmon
->wy
- ny
) < snap
)
1271 else if (abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1272 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1273 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1274 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1275 togglefloating(NULL
);
1276 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1277 resize(c
, nx
, ny
, c
->w
, c
->h
, 1);
1280 } while (ev
.type
!= ButtonRelease
);
1281 XUngrabPointer(dpy
, CurrentTime
);
1282 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1290 nexttiled(Client
*c
)
1292 for (; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1306 propertynotify(XEvent
*e
)
1310 XPropertyEvent
*ev
= &e
->xproperty
;
1312 if ((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1314 else if (ev
->state
== PropertyDelete
)
1315 return; /* ignore */
1316 else if ((c
= wintoclient(ev
->window
))) {
1319 case XA_WM_TRANSIENT_FOR
:
1320 if (!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1321 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1324 case XA_WM_NORMAL_HINTS
:
1332 if (ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1334 if (c
== c
->mon
->sel
)
1337 if (ev
->atom
== netatom
[NetWMWindowType
])
1338 updatewindowtype(c
);
1343 quit(const Arg
*arg
)
1345 if(arg
->i
) restart
= 1;
1350 recttomon(int x
, int y
, int w
, int h
)
1352 Monitor
*m
, *r
= selmon
;
1355 for (m
= mons
; m
; m
= m
->next
)
1356 if ((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1364 resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
)
1366 if (applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1367 resizeclient(c
, x
, y
, w
, h
);
1371 resizeclient(Client
*c
, int x
, int y
, int w
, int h
)
1375 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1376 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1377 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1378 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1379 wc
.border_width
= c
->bw
;
1380 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1386 resizemouse(const Arg
*arg
)
1388 int ocx
, ocy
, nw
, nh
;
1394 if (!(c
= selmon
->sel
))
1396 if (c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1401 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1402 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1404 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1406 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1408 case ConfigureRequest
:
1411 handler
[ev
.type
](&ev
);
1414 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1416 lasttime
= ev
.xmotion
.time
;
1418 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1419 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1420 if (c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1421 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1423 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1424 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1425 togglefloating(NULL
);
1427 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1428 resize(c
, c
->x
, c
->y
, nw
, nh
, 1);
1431 } while (ev
.type
!= ButtonRelease
);
1432 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1433 XUngrabPointer(dpy
, CurrentTime
);
1434 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1435 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1452 if (m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1453 XRaiseWindow(dpy
, m
->sel
->win
);
1454 if (m
->lt
[m
->sellt
]->arrange
) {
1455 wc
.stack_mode
= Below
;
1456 wc
.sibling
= m
->barwin
;
1457 for (c
= m
->stack
; c
; c
= c
->snext
)
1458 if (!c
->isfloating
&& ISVISIBLE(c
)) {
1459 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1460 wc
.sibling
= c
->win
;
1464 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1471 /* main event loop */
1473 while (running
&& !XNextEvent(dpy
, &ev
))
1474 if (handler
[ev
.type
])
1475 handler
[ev
.type
](&ev
); /* call handler */
1481 unsigned int i
, num
;
1482 Window d1
, d2
, *wins
= NULL
;
1483 XWindowAttributes wa
;
1485 if (XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1486 for (i
= 0; i
< num
; i
++) {
1487 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1488 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1490 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1491 manage(wins
[i
], &wa
);
1493 for (i
= 0; i
< num
; i
++) { /* now the transients */
1494 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1496 if (XGetTransientForHint(dpy
, wins
[i
], &d1
)
1497 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1498 manage(wins
[i
], &wa
);
1506 sendmon(Client
*c
, Monitor
*m
)
1514 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1522 setclientstate(Client
*c
, long state
)
1524 long data
[] = { state
, None
};
1526 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1527 PropModeReplace
, (unsigned char *)data
, 2);
1531 sendevent(Client
*c
, Atom proto
)
1538 if (XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1539 while (!exists
&& n
--)
1540 exists
= protocols
[n
] == proto
;
1544 ev
.type
= ClientMessage
;
1545 ev
.xclient
.window
= c
->win
;
1546 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1547 ev
.xclient
.format
= 32;
1548 ev
.xclient
.data
.l
[0] = proto
;
1549 ev
.xclient
.data
.l
[1] = CurrentTime
;
1550 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1558 if (!c
->neverfocus
) {
1559 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1560 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1561 XA_WINDOW
, 32, PropModeReplace
,
1562 (unsigned char *) &(c
->win
), 1);
1564 sendevent(c
, wmatom
[WMTakeFocus
]);
1568 setfullscreen(Client
*c
, int fullscreen
)
1570 if (fullscreen
&& !c
->isfullscreen
) {
1571 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1572 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1573 c
->isfullscreen
= 1;
1574 c
->oldstate
= c
->isfloating
;
1578 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1579 XRaiseWindow(dpy
, c
->win
);
1580 } else if (!fullscreen
&& c
->isfullscreen
){
1581 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1582 PropModeReplace
, (unsigned char*)0, 0);
1583 c
->isfullscreen
= 0;
1584 c
->isfloating
= c
->oldstate
;
1590 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1596 setgaps(const Arg
*arg
)
1598 if ((arg
->i
== 0) || (selmon
->gappx
+ arg
->i
< 0))
1601 selmon
->gappx
+= arg
->i
;
1606 setlayout(const Arg
*arg
)
1608 if (!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1611 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1612 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1619 /* arg > 1.0 will set mfact absolutely */
1621 setmfact(const Arg
*arg
)
1625 if (!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1627 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1628 if (f
< 0.05 || f
> 0.95)
1638 XSetWindowAttributes wa
;
1641 /* clean up any zombies immediately */
1644 signal(SIGHUP
, sighup
);
1645 signal(SIGTERM
, sigterm
);
1648 screen
= DefaultScreen(dpy
);
1649 sw
= DisplayWidth(dpy
, screen
);
1650 sh
= DisplayHeight(dpy
, screen
);
1651 root
= RootWindow(dpy
, screen
);
1653 drw
= drw_create(dpy
, screen
, root
, sw
, sh
, visual
, depth
, cmap
);
1654 if (!drw_fontset_create(drw
, fonts
, LENGTH(fonts
)))
1655 die("no fonts could be loaded.");
1656 lrpad
= drw
->fonts
->h
;
1657 bh
= drw
->fonts
->h
+ 2;
1660 utf8string
= XInternAtom(dpy
, "UTF8_STRING", False
);
1661 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1662 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1663 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1664 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1665 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1666 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1667 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1668 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1669 netatom
[NetWMCheck
] = XInternAtom(dpy
, "_NET_SUPPORTING_WM_CHECK", False
);
1670 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1671 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1672 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1673 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1675 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1676 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1677 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1678 /* init appearance */
1679 scheme
= ecalloc(LENGTH(colors
), sizeof(Clr
*));
1680 for (i
= 0; i
< LENGTH(colors
); i
++)
1681 scheme
[i
] = drw_scm_create(drw
, colors
[i
], alphas
[i
], 3);
1685 /* supporting window for NetWMCheck */
1686 wmcheckwin
= XCreateSimpleWindow(dpy
, root
, 0, 0, 1, 1, 0, 0, 0);
1687 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1688 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1689 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMName
], utf8string
, 8,
1690 PropModeReplace
, (unsigned char *) "dwm", 3);
1691 XChangeProperty(dpy
, root
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1692 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1693 /* EWMH support per view */
1694 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1695 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1696 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1698 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1699 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1700 |ButtonPressMask
|PointerMotionMask
|EnterWindowMask
1701 |LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1702 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1703 XSelectInput(dpy
, root
, wa
.event_mask
);
1710 seturgent(Client
*c
, int urg
)
1715 if (!(wmh
= XGetWMHints(dpy
, c
->win
)))
1717 wmh
->flags
= urg
? (wmh
->flags
| XUrgencyHint
) : (wmh
->flags
& ~XUrgencyHint
);
1718 XSetWMHints(dpy
, c
->win
, wmh
);
1728 /* show clients top down */
1729 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1730 if ((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1731 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, 0);
1734 /* hide clients bottom up */
1736 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1743 if (signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1744 die("can't install SIGCHLD handler:");
1745 while (0 < waitpid(-1, NULL
, WNOHANG
));
1763 spawn(const Arg
*arg
)
1765 if (arg
->v
== dmenucmd
)
1766 dmenumon
[0] = '0' + selmon
->num
;
1769 close(ConnectionNumber(dpy
));
1771 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1772 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1781 if (selmon
->sel
&& arg
->ui
& TAGMASK
) {
1782 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1789 tagmon(const Arg
*arg
)
1791 if (!selmon
->sel
|| !mons
->next
)
1793 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1799 unsigned int i
, n
, h
, mw
, my
, ty
;
1802 for (n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1807 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1809 mw
= m
->ww
- m
->gappx
;
1810 for (i
= 0, my
= ty
= m
->gappx
, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1811 if (i
< m
->nmaster
) {
1812 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
) - m
->gappx
;
1813 resize(c
, m
->wx
+ m
->gappx
, m
->wy
+ my
, mw
- (2*c
->bw
) - m
->gappx
, h
- (2*c
->bw
), 0);
1814 if (my
+ HEIGHT(c
) + m
->gappx
< m
->wh
)
1815 my
+= HEIGHT(c
) + m
->gappx
;
1817 h
= (m
->wh
- ty
) / (n
- i
) - m
->gappx
;
1818 resize(c
, m
->wx
+ mw
+ m
->gappx
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
) - 2*m
->gappx
, h
- (2*c
->bw
), 0);
1819 if (ty
+ HEIGHT(c
) + m
->gappx
< m
->wh
)
1820 ty
+= HEIGHT(c
) + m
->gappx
;
1825 togglebar(const Arg
*arg
)
1827 selmon
->showbar
= !selmon
->showbar
;
1828 updatebarpos(selmon
);
1829 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1834 togglefloating(const Arg
*arg
)
1838 if (selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1840 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1841 if (selmon
->sel
->isfloating
)
1842 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1843 selmon
->sel
->w
, selmon
->sel
->h
, 0);
1848 togglefullscr(const Arg
*arg
)
1851 setfullscreen(selmon
->sel
, !selmon
->sel
->isfullscreen
);
1855 toggletag(const Arg
*arg
)
1857 unsigned int newtags
;
1861 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1863 selmon
->sel
->tags
= newtags
;
1870 toggleview(const Arg
*arg
)
1872 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1875 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1882 unfocus(Client
*c
, int setfocus
)
1887 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1889 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1890 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1895 unmanage(Client
*c
, int destroyed
)
1897 Monitor
*m
= c
->mon
;
1900 if (c
->swallowing
) {
1905 Client
*s
= swallowingclient(c
->win
);
1907 free(s
->swallowing
);
1908 s
->swallowing
= NULL
;
1917 wc
.border_width
= c
->oldbw
;
1918 XGrabServer(dpy
); /* avoid race conditions */
1919 XSetErrorHandler(xerrordummy
);
1920 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1921 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1922 setclientstate(c
, WithdrawnState
);
1924 XSetErrorHandler(xerror
);
1937 unmapnotify(XEvent
*e
)
1940 XUnmapEvent
*ev
= &e
->xunmap
;
1942 if ((c
= wintoclient(ev
->window
))) {
1944 setclientstate(c
, WithdrawnState
);
1954 XSetWindowAttributes wa
= {
1955 .override_redirect
= True
,
1956 .background_pixel
= 0,
1959 .event_mask
= ButtonPressMask
|ExposureMask
1961 XClassHint ch
= {"dwm", "dwm"};
1962 for (m
= mons
; m
; m
= m
->next
) {
1965 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, depth
,
1966 InputOutput
, visual
,
1967 CWOverrideRedirect
|CWBackPixel
|CWBorderPixel
|CWColormap
|CWEventMask
, &wa
);
1968 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
1969 XMapRaised(dpy
, m
->barwin
);
1970 XSetClassHint(dpy
, m
->barwin
, &ch
);
1975 updatebarpos(Monitor
*m
)
1981 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1982 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1993 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1994 for (m
= mons
; m
; m
= m
->next
)
1995 for (c
= m
->clients
; c
; c
= c
->next
)
1996 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
1997 XA_WINDOW
, 32, PropModeAppend
,
1998 (unsigned char *) &(c
->win
), 1);
2007 if (XineramaIsActive(dpy
)) {
2011 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
2012 XineramaScreenInfo
*unique
= NULL
;
2014 for (n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
2015 /* only consider unique geometries as separate screens */
2016 unique
= ecalloc(nn
, sizeof(XineramaScreenInfo
));
2017 for (i
= 0, j
= 0; i
< nn
; i
++)
2018 if (isuniquegeom(unique
, j
, &info
[i
]))
2019 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
2022 if (n
<= nn
) { /* new monitors available */
2023 for (i
= 0; i
< (nn
- n
); i
++) {
2024 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
2026 m
->next
= createmon();
2030 for (i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
2032 || unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
2033 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
)
2037 m
->mx
= m
->wx
= unique
[i
].x_org
;
2038 m
->my
= m
->wy
= unique
[i
].y_org
;
2039 m
->mw
= m
->ww
= unique
[i
].width
;
2040 m
->mh
= m
->wh
= unique
[i
].height
;
2043 } else { /* less monitors available nn < n */
2044 for (i
= nn
; i
< n
; i
++) {
2045 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
2046 while ((c
= m
->clients
)) {
2048 m
->clients
= c
->next
;
2061 #endif /* XINERAMA */
2062 { /* default monitor setup */
2065 if (mons
->mw
!= sw
|| mons
->mh
!= sh
) {
2067 mons
->mw
= mons
->ww
= sw
;
2068 mons
->mh
= mons
->wh
= sh
;
2074 selmon
= wintomon(root
);
2080 updatenumlockmask(void)
2083 XModifierKeymap
*modmap
;
2086 modmap
= XGetModifierMapping(dpy
);
2087 for (i
= 0; i
< 8; i
++)
2088 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
2089 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
2090 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
2091 numlockmask
= (1 << i
);
2092 XFreeModifiermap(modmap
);
2096 updatesizehints(Client
*c
)
2101 if (!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
2102 /* size is uninitialized, ensure that size.flags aren't used */
2104 if (size
.flags
& PBaseSize
) {
2105 c
->basew
= size
.base_width
;
2106 c
->baseh
= size
.base_height
;
2107 } else if (size
.flags
& PMinSize
) {
2108 c
->basew
= size
.min_width
;
2109 c
->baseh
= size
.min_height
;
2111 c
->basew
= c
->baseh
= 0;
2112 if (size
.flags
& PResizeInc
) {
2113 c
->incw
= size
.width_inc
;
2114 c
->inch
= size
.height_inc
;
2116 c
->incw
= c
->inch
= 0;
2117 if (size
.flags
& PMaxSize
) {
2118 c
->maxw
= size
.max_width
;
2119 c
->maxh
= size
.max_height
;
2121 c
->maxw
= c
->maxh
= 0;
2122 if (size
.flags
& PMinSize
) {
2123 c
->minw
= size
.min_width
;
2124 c
->minh
= size
.min_height
;
2125 } else if (size
.flags
& PBaseSize
) {
2126 c
->minw
= size
.base_width
;
2127 c
->minh
= size
.base_height
;
2129 c
->minw
= c
->minh
= 0;
2130 if (size
.flags
& PAspect
) {
2131 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
2132 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
2134 c
->maxa
= c
->mina
= 0.0;
2135 c
->isfixed
= (c
->maxw
&& c
->maxh
&& c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
2141 if (!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
2142 strcpy(stext
, "dwm-"VERSION
);
2147 updatetitle(Client
*c
)
2149 if (!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
2150 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
2151 if (c
->name
[0] == '\0') /* hack to mark broken clients */
2152 strcpy(c
->name
, broken
);
2156 updatewindowtype(Client
*c
)
2158 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
2159 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
2161 if (state
== netatom
[NetWMFullscreen
])
2162 setfullscreen(c
, 1);
2163 if (wtype
== netatom
[NetWMWindowTypeDialog
])
2168 updatewmhints(Client
*c
)
2172 if ((wmh
= XGetWMHints(dpy
, c
->win
))) {
2173 if (c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2174 wmh
->flags
&= ~XUrgencyHint
;
2175 XSetWMHints(dpy
, c
->win
, wmh
);
2177 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? 1 : 0;
2178 if (wmh
->flags
& InputHint
)
2179 c
->neverfocus
= !wmh
->input
;
2187 view(const Arg
*arg
)
2189 if ((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2191 selmon
->seltags
^= 1; /* toggle sel tagset */
2192 if (arg
->ui
& TAGMASK
)
2193 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2205 xcb_res_client_id_spec_t spec
= {0};
2207 spec
.mask
= XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID
;
2209 xcb_generic_error_t
*e
= NULL
;
2210 xcb_res_query_client_ids_cookie_t c
= xcb_res_query_client_ids(xcon
, 1, &spec
);
2211 xcb_res_query_client_ids_reply_t
*r
= xcb_res_query_client_ids_reply(xcon
, c
, &e
);
2216 xcb_res_client_id_value_iterator_t i
= xcb_res_query_client_ids_ids_iterator(r
);
2217 for (; i
.rem
; xcb_res_client_id_value_next(&i
)) {
2218 spec
= i
.data
->spec
;
2219 if (spec
.mask
& XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID
) {
2220 uint32_t *t
= xcb_res_client_id_value_value(i
.data
);
2228 if (result
== (pid_t
)-1)
2231 #endif /* __linux__ */
2236 unsigned long len
, bytes
;
2237 unsigned char *prop
;
2240 if (XGetWindowProperty(dpy
, w
, XInternAtom(dpy
, "_NET_WM_PID", 0), 0, 1, False
, AnyPropertyType
, &type
, &format
, &len
, &bytes
, &prop
) != Success
|| !prop
)
2243 ret
= *(pid_t
*)prop
;
2247 #endif /* __OpenBSD__ */
2252 getparentprocess(pid_t p
)
2259 snprintf(buf
, sizeof(buf
) - 1, "/proc/%u/stat", (unsigned)p
);
2261 if (!(f
= fopen(buf
, "r")))
2264 fscanf(f
, "%*u %*s %*c %u", &v
);
2266 #endif /* __linux__*/
2271 struct kinfo_proc
*kp
;
2273 kd
= kvm_openfiles(NULL
, NULL
, NULL
, KVM_NO_FILES
, NULL
);
2277 kp
= kvm_getprocs(kd
, KERN_PROC_PID
, p
, sizeof(*kp
), &n
);
2279 #endif /* __OpenBSD__ */
2285 isdescprocess(pid_t p
, pid_t c
)
2287 while (p
!= c
&& c
!= 0)
2288 c
= getparentprocess(c
);
2294 termforwin(const Client
*w
)
2299 if (!w
->pid
|| w
->isterminal
)
2302 for (m
= mons
; m
; m
= m
->next
) {
2303 for (c
= m
->clients
; c
; c
= c
->next
) {
2304 if (c
->isterminal
&& !c
->swallowing
&& c
->pid
&& isdescprocess(c
->pid
, w
->pid
))
2313 swallowingclient(Window w
)
2318 for (m
= mons
; m
; m
= m
->next
) {
2319 for (c
= m
->clients
; c
; c
= c
->next
) {
2320 if (c
->swallowing
&& c
->swallowing
->win
== w
)
2329 wintoclient(Window w
)
2334 for (m
= mons
; m
; m
= m
->next
)
2335 for (c
= m
->clients
; c
; c
= c
->next
)
2348 if (w
== root
&& getrootptr(&x
, &y
))
2349 return recttomon(x
, y
, 1, 1);
2350 for (m
= mons
; m
; m
= m
->next
)
2353 if ((c
= wintoclient(w
)))
2358 /* There's no way to check accesses to destroyed windows, thus those cases are
2359 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2360 * default error handler, which may call exit. */
2362 xerror(Display
*dpy
, XErrorEvent
*ee
)
2364 if (ee
->error_code
== BadWindow
2365 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2366 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2367 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2368 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2369 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2370 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2371 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2372 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2374 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2375 ee
->request_code
, ee
->error_code
);
2376 return xerrorxlib(dpy
, ee
); /* may call exit */
2380 xerrordummy(Display
*dpy
, XErrorEvent
*ee
)
2385 /* Startup Error handler to check if another window manager
2386 * is already running. */
2388 xerrorstart(Display
*dpy
, XErrorEvent
*ee
)
2390 die("dwm: another window manager is already running");
2398 XRenderPictFormat
*fmt
;
2407 long masks
= VisualScreenMask
| VisualDepthMask
| VisualClassMask
;
2409 infos
= XGetVisualInfo(dpy
, masks
, &tpl
, &nitems
);
2411 for(i
= 0; i
< nitems
; i
++) {
2412 fmt
= XRenderFindVisualFormat(dpy
, infos
[i
].visual
);
2413 if (fmt
->type
== PictTypeDirect
&& fmt
->direct
.alphaMask
) {
2414 visual
= infos
[i
].visual
;
2415 depth
= infos
[i
].depth
;
2416 cmap
= XCreateColormap(dpy
, root
, visual
, AllocNone
);
2425 visual
= DefaultVisual(dpy
, screen
);
2426 depth
= DefaultDepth(dpy
, screen
);
2427 cmap
= DefaultColormap(dpy
, screen
);
2432 zoom(const Arg
*arg
)
2434 Client
*c
= selmon
->sel
;
2436 if (!selmon
->lt
[selmon
->sellt
]->arrange
2437 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2439 if (c
== nexttiled(selmon
->clients
))
2440 if (!c
|| !(c
= nexttiled(c
->next
)))
2446 main(int argc
, char *argv
[])
2448 if (argc
== 2 && !strcmp("-v", argv
[1]))
2451 die("usage: dwm [-v]");
2452 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2453 fputs("warning: no locale support\n", stderr
);
2454 if (!(dpy
= XOpenDisplay(NULL
)))
2455 die("dwm: cannot open display");
2456 if (!(xcon
= XGetXCBConnection(dpy
)))
2457 die("dwm: cannot get xcb connection\n");
2461 if (pledge("stdio rpath proc exec ps", NULL
) == -1)
2463 #endif /* __OpenBSD__ */
2466 if(restart
) execvp(argv
[0], argv
);
2469 return EXIT_SUCCESS
;