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>
39 #include <X11/Xresource.h>
41 #include <X11/extensions/Xinerama.h>
43 #include <X11/Xft/Xft.h>
44 #include <X11/Xlib-xcb.h>
47 #include <sys/sysctl.h>
49 #endif /* __OpenBSD */
55 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
56 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
57 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
58 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
59 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
60 #define LENGTH(X) (sizeof X / sizeof X[0])
61 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
62 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
63 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
64 #define TAGMASK ((1 << LENGTH(tags)) - 1)
65 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
70 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
71 enum { SchemeNorm
, SchemeSel
}; /* color schemes */
72 enum { NetSupported
, NetWMName
, NetWMState
, NetWMCheck
,
73 NetWMFullscreen
, NetActiveWindow
, NetWMWindowType
,
74 NetWMWindowTypeDialog
, NetClientList
, NetLast
}; /* EWMH atoms */
75 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, WMLast
}; /* default atoms */
76 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
77 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
90 void (*func
)(const Arg
*arg
);
94 typedef struct Monitor Monitor
;
95 typedef struct Client Client
;
100 int oldx
, oldy
, oldw
, oldh
;
101 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
104 int isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
, isterminal
, noswallow
;
116 void (*func
)(const Arg
*);
122 void (*arrange
)(Monitor
*);
130 int by
; /* bar geometry */
131 int mx
, my
, mw
, mh
; /* screen size */
132 int wx
, wy
, ww
, wh
; /* window area */
133 int gappx
; /* gaps between windows */
134 unsigned int seltags
;
136 unsigned int tagset
[2];
149 const char *instance
;
158 /* Xresources preferences */
167 enum resource_type type
;
171 /* function declarations */
172 static void applyrules(Client
*c
);
173 static int applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
);
174 static void arrange(Monitor
*m
);
175 static void arrangemon(Monitor
*m
);
176 static void attach(Client
*c
);
177 static void attachstack(Client
*c
);
178 static void buttonpress(XEvent
*e
);
179 static void checkotherwm(void);
180 static void cleanup(void);
181 static void cleanupmon(Monitor
*mon
);
182 static void clientmessage(XEvent
*e
);
183 static void configure(Client
*c
);
184 static void configurenotify(XEvent
*e
);
185 static void configurerequest(XEvent
*e
);
186 static Monitor
*createmon(void);
187 static void destroynotify(XEvent
*e
);
188 static void detach(Client
*c
);
189 static void detachstack(Client
*c
);
190 static Monitor
*dirtomon(int dir
);
191 static void drawbar(Monitor
*m
);
192 static void drawbars(void);
193 static void enternotify(XEvent
*e
);
194 static void expose(XEvent
*e
);
195 static void focus(Client
*c
);
196 static void focusin(XEvent
*e
);
197 static void focusmon(const Arg
*arg
);
198 static void focusstack(const Arg
*arg
);
199 static Atom
getatomprop(Client
*c
, Atom prop
);
200 static int getrootptr(int *x
, int *y
);
201 static long getstate(Window w
);
202 static int gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
203 static void grabbuttons(Client
*c
, int focused
);
204 static void grabkeys(void);
205 static void incnmaster(const Arg
*arg
);
206 static void keypress(XEvent
*e
);
207 static void killclient(const Arg
*arg
);
208 static void manage(Window w
, XWindowAttributes
*wa
);
209 static void mappingnotify(XEvent
*e
);
210 static void maprequest(XEvent
*e
);
211 static void monocle(Monitor
*m
);
212 static void motionnotify(XEvent
*e
);
213 static void movemouse(const Arg
*arg
);
214 static Client
*nexttiled(Client
*c
);
215 static void pop(Client
*);
216 static void propertynotify(XEvent
*e
);
217 static void quit(const Arg
*arg
);
218 static Monitor
*recttomon(int x
, int y
, int w
, int h
);
219 static void resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
);
220 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
221 static void resizemouse(const Arg
*arg
);
222 static void restack(Monitor
*m
);
223 static void run(void);
224 static void scan(void);
225 static int sendevent(Client
*c
, Atom proto
);
226 static void sendmon(Client
*c
, Monitor
*m
);
227 static void setclientstate(Client
*c
, long state
);
228 static void setfocus(Client
*c
);
229 static void setfullscreen(Client
*c
, int fullscreen
);
230 static void setgaps(const Arg
*arg
);
231 static void setlayout(const Arg
*arg
);
232 static void setmfact(const Arg
*arg
);
233 static void setup(void);
234 static void seturgent(Client
*c
, int urg
);
235 static void showhide(Client
*c
);
236 static void sigchld(int unused
);
237 static void sighup(int unused
);
238 static void sigterm(int unused
);
239 static void spawn(const Arg
*arg
);
240 static void tag(const Arg
*arg
);
241 static void tagmon(const Arg
*arg
);
242 static void tile(Monitor
*);
243 static void togglebar(const Arg
*arg
);
244 static void togglefloating(const Arg
*arg
);
245 static void togglefullscr(const Arg
*arg
);
246 static void toggletag(const Arg
*arg
);
247 static void toggleview(const Arg
*arg
);
248 static void unfocus(Client
*c
, int setfocus
);
249 static void unmanage(Client
*c
, int destroyed
);
250 static void unmapnotify(XEvent
*e
);
251 static void updatebarpos(Monitor
*m
);
252 static void updatebars(void);
253 static void updateclientlist(void);
254 static int updategeom(void);
255 static void updatenumlockmask(void);
256 static void updatesizehints(Client
*c
);
257 static void updatestatus(void);
258 static void updatetitle(Client
*c
);
259 static void updatewindowtype(Client
*c
);
260 static void updatewmhints(Client
*c
);
261 static void view(const Arg
*arg
);
262 static Client
*wintoclient(Window w
);
263 static Monitor
*wintomon(Window w
);
264 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
265 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
266 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
267 static void xinitvisual();
268 static void zoom(const Arg
*arg
);
269 static void load_xresources(void);
270 static void resource_load(XrmDatabase db
, char *name
, enum resource_type rtype
, void *dst
);
272 static pid_t
getparentprocess(pid_t p
);
273 static int isdescprocess(pid_t p
, pid_t c
);
274 static Client
*swallowingclient(Window w
);
275 static Client
*termforwin(const Client
*c
);
276 static pid_t
winpid(Window w
);
279 static const char broken
[] = "broken";
280 static char stext
[256];
282 static int sw
, sh
; /* X display screen geometry width, height */
283 static int bh
, blw
= 0; /* bar geometry */
284 static int lrpad
; /* sum of left and right padding for text */
285 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
286 static unsigned int numlockmask
= 0;
287 static void (*handler
[LASTEvent
]) (XEvent
*) = {
288 [ButtonPress
] = buttonpress
,
289 [ClientMessage
] = clientmessage
,
290 [ConfigureRequest
] = configurerequest
,
291 [ConfigureNotify
] = configurenotify
,
292 [DestroyNotify
] = destroynotify
,
293 [EnterNotify
] = enternotify
,
296 [KeyPress
] = keypress
,
297 [MappingNotify
] = mappingnotify
,
298 [MapRequest
] = maprequest
,
299 [MotionNotify
] = motionnotify
,
300 [PropertyNotify
] = propertynotify
,
301 [UnmapNotify
] = unmapnotify
303 static Atom wmatom
[WMLast
], netatom
[NetLast
];
304 static int restart
= 0;
305 static int running
= 1;
306 static Cur
*cursor
[CurLast
];
310 static Monitor
*mons
, *selmon
;
311 static Window root
, wmcheckwin
;
313 static xcb_connection_t
*xcon
;
315 static int useargb
= 0;
316 static Visual
*visual
;
318 static Colormap cmap
;
320 /* configuration, allows nested code to access above variables */
323 /* compile-time check if all tags fit into an unsigned int bit array. */
324 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
326 /* function implementations */
328 applyrules(Client
*c
)
330 const char *class, *instance
;
334 XClassHint ch
= { NULL
, NULL
};
339 XGetClassHint(dpy
, c
->win
, &ch
);
340 class = ch
.res_class
? ch
.res_class
: broken
;
341 instance
= ch
.res_name
? ch
.res_name
: broken
;
343 for (i
= 0; i
< LENGTH(rules
); i
++) {
345 if ((!r
->title
|| strstr(c
->name
, r
->title
))
346 && (!r
->class || strstr(class, r
->class))
347 && (!r
->instance
|| strstr(instance
, r
->instance
)))
349 c
->isterminal
= r
->isterminal
;
350 c
->noswallow
= r
->noswallow
;
351 c
->isfloating
= r
->isfloating
;
353 for (m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
362 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
366 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
)
371 /* set minimum possible */
379 if (*x
+ *w
+ 2 * c
->bw
< 0)
381 if (*y
+ *h
+ 2 * c
->bw
< 0)
384 if (*x
>= m
->wx
+ m
->ww
)
385 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
386 if (*y
>= m
->wy
+ m
->wh
)
387 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
388 if (*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
390 if (*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
397 if (resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
398 /* see last two sentences in ICCCM 4.1.2.3 */
399 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
400 if (!baseismin
) { /* temporarily remove base dimensions */
404 /* adjust for aspect limits */
405 if (c
->mina
> 0 && c
->maxa
> 0) {
406 if (c
->maxa
< (float)*w
/ *h
)
407 *w
= *h
* c
->maxa
+ 0.5;
408 else if (c
->mina
< (float)*h
/ *w
)
409 *h
= *w
* c
->mina
+ 0.5;
411 if (baseismin
) { /* increment calculation requires this */
415 /* adjust for increment value */
420 /* restore base dimensions */
421 *w
= MAX(*w
+ c
->basew
, c
->minw
);
422 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
424 *w
= MIN(*w
, c
->maxw
);
426 *h
= MIN(*h
, c
->maxh
);
428 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
436 else for (m
= mons
; m
; m
= m
->next
)
441 } else for (m
= mons
; m
; m
= m
->next
)
446 arrangemon(Monitor
*m
)
448 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
449 if (m
->lt
[m
->sellt
]->arrange
)
450 m
->lt
[m
->sellt
]->arrange(m
);
456 c
->next
= c
->mon
->clients
;
461 attachstack(Client
*c
)
463 c
->snext
= c
->mon
->stack
;
468 swallow(Client
*p
, Client
*c
)
471 if (c
->noswallow
|| c
->isterminal
)
473 if (c
->noswallow
&& !swallowfloating
&& c
->isfloating
)
479 setclientstate(c
, WithdrawnState
);
480 XUnmapWindow(dpy
, p
->win
);
489 XMoveResizeWindow(dpy
, p
->win
, p
->x
, p
->y
, p
->w
, p
->h
);
498 c
->win
= c
->swallowing
->win
;
501 c
->swallowing
= NULL
;
503 /* unfullscreen the client */
507 XMapWindow(dpy
, c
->win
);
508 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
509 setclientstate(c
, NormalState
);
515 buttonpress(XEvent
*e
)
517 unsigned int i
, x
, click
;
521 XButtonPressedEvent
*ev
= &e
->xbutton
;
524 /* focus monitor if necessary */
525 if ((m
= wintomon(ev
->window
)) && m
!= selmon
) {
526 unfocus(selmon
->sel
, 1);
530 if (ev
->window
== selmon
->barwin
) {
534 while (ev
->x
>= x
&& ++i
< LENGTH(tags
));
535 if (i
< LENGTH(tags
)) {
538 } else if (ev
->x
< x
+ blw
)
540 else if (ev
->x
> selmon
->ww
- (int)TEXTW(stext
))
541 click
= ClkStatusText
;
544 } else if ((c
= wintoclient(ev
->window
))) {
547 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
548 click
= ClkClientWin
;
550 for (i
= 0; i
< LENGTH(buttons
); i
++)
551 if (click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
552 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
553 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
559 xerrorxlib
= XSetErrorHandler(xerrorstart
);
560 /* this causes an error if some other window manager is running */
561 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
563 XSetErrorHandler(xerror
);
571 Layout foo
= { "", NULL
};
576 selmon
->lt
[selmon
->sellt
] = &foo
;
577 for (m
= mons
; m
; m
= m
->next
)
579 unmanage(m
->stack
, 0);
580 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
583 for (i
= 0; i
< CurLast
; i
++)
584 drw_cur_free(drw
, cursor
[i
]);
585 for (i
= 0; i
< LENGTH(colors
); i
++)
588 XDestroyWindow(dpy
, wmcheckwin
);
591 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
592 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
596 cleanupmon(Monitor
*mon
)
603 for (m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
606 XUnmapWindow(dpy
, mon
->barwin
);
607 XDestroyWindow(dpy
, mon
->barwin
);
612 clientmessage(XEvent
*e
)
614 XClientMessageEvent
*cme
= &e
->xclient
;
615 Client
*c
= wintoclient(cme
->window
);
619 if (cme
->message_type
== netatom
[NetWMState
]) {
620 if (cme
->data
.l
[1] == netatom
[NetWMFullscreen
]
621 || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
622 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
623 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
624 } else if (cme
->message_type
== netatom
[NetActiveWindow
]) {
625 if (c
!= selmon
->sel
&& !c
->isurgent
)
635 ce
.type
= ConfigureNotify
;
643 ce
.border_width
= c
->bw
;
645 ce
.override_redirect
= False
;
646 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
650 configurenotify(XEvent
*e
)
654 XConfigureEvent
*ev
= &e
->xconfigure
;
657 /* TODO: updategeom handling sucks, needs to be simplified */
658 if (ev
->window
== root
) {
659 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
662 if (updategeom() || dirty
) {
663 drw_resize(drw
, sw
, bh
);
665 for (m
= mons
; m
; m
= m
->next
) {
666 for (c
= m
->clients
; c
; c
= c
->next
)
668 resizeclient(c
, m
->mx
, m
->my
, m
->mw
, m
->mh
);
669 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
678 configurerequest(XEvent
*e
)
682 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
685 if ((c
= wintoclient(ev
->window
))) {
686 if (ev
->value_mask
& CWBorderWidth
)
687 c
->bw
= ev
->border_width
;
688 else if (c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
690 if (ev
->value_mask
& CWX
) {
692 c
->x
= m
->mx
+ ev
->x
;
694 if (ev
->value_mask
& CWY
) {
696 c
->y
= m
->my
+ ev
->y
;
698 if (ev
->value_mask
& CWWidth
) {
702 if (ev
->value_mask
& CWHeight
) {
706 if ((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
707 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
708 if ((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
709 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
710 if ((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
713 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
719 wc
.width
= ev
->width
;
720 wc
.height
= ev
->height
;
721 wc
.border_width
= ev
->border_width
;
722 wc
.sibling
= ev
->above
;
723 wc
.stack_mode
= ev
->detail
;
724 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
734 m
= ecalloc(1, sizeof(Monitor
));
735 m
->tagset
[0] = m
->tagset
[1] = 1;
737 m
->nmaster
= nmaster
;
738 m
->showbar
= showbar
;
741 m
->lt
[0] = &layouts
[0];
742 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
743 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
748 destroynotify(XEvent
*e
)
751 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
753 if ((c
= wintoclient(ev
->window
)))
756 else if ((c
= swallowingclient(ev
->window
)))
757 unmanage(c
->swallowing
, 1);
765 for (tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
770 detachstack(Client
*c
)
774 for (tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
777 if (c
== c
->mon
->sel
) {
778 for (t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
789 if (!(m
= selmon
->next
))
791 } else if (selmon
== mons
)
792 for (m
= mons
; m
->next
; m
= m
->next
);
794 for (m
= mons
; m
->next
!= selmon
; m
= m
->next
);
802 int boxs
= drw
->fonts
->h
/ 9;
803 int boxw
= drw
->fonts
->h
/ 6 + 2;
804 unsigned int i
, occ
= 0, urg
= 0;
810 /* draw status first so it can be overdrawn by tags later */
811 if (m
== selmon
) { /* status is only drawn on selected monitor */
812 drw_setscheme(drw
, scheme
[SchemeNorm
]);
813 tw
= TEXTW(stext
) - lrpad
+ 2; /* 2px right padding */
814 drw_text(drw
, m
->ww
- tw
, 0, tw
, bh
, 0, stext
, 0);
817 for (c
= m
->clients
; c
; c
= c
->next
) {
823 for (i
= 0; i
< LENGTH(tags
); i
++) {
825 drw_setscheme(drw
, scheme
[m
->tagset
[m
->seltags
] & 1 << i
? SchemeSel
: SchemeNorm
]);
826 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, tags
[i
], urg
& 1 << i
);
828 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
,
829 m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
833 w
= blw
= TEXTW(m
->ltsymbol
);
834 drw_setscheme(drw
, scheme
[SchemeNorm
]);
835 x
= drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->ltsymbol
, 0);
837 if ((w
= m
->ww
- tw
- x
) > bh
) {
839 drw_setscheme(drw
, scheme
[m
== selmon
? SchemeSel
: SchemeNorm
]);
840 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->sel
->name
, 0);
841 if (m
->sel
->isfloating
)
842 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
, m
->sel
->isfixed
, 0);
844 drw_setscheme(drw
, scheme
[SchemeNorm
]);
845 drw_rect(drw
, x
, 0, w
, bh
, 1, 1);
848 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
);
856 for (m
= mons
; m
; m
= m
->next
)
861 enternotify(XEvent
*e
)
865 XCrossingEvent
*ev
= &e
->xcrossing
;
867 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
869 c
= wintoclient(ev
->window
);
870 m
= c
? c
->mon
: wintomon(ev
->window
);
872 unfocus(selmon
->sel
, 1);
874 } else if (!c
|| c
== selmon
->sel
)
883 XExposeEvent
*ev
= &e
->xexpose
;
885 if (ev
->count
== 0 && (m
= wintomon(ev
->window
)))
892 if (!c
|| !ISVISIBLE(c
))
893 for (c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
894 if (selmon
->sel
&& selmon
->sel
!= c
)
895 unfocus(selmon
->sel
, 0);
897 if (c
->mon
!= selmon
)
904 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
][ColBorder
].pixel
);
907 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
908 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
914 /* there are some broken focus acquiring clients needing extra handling */
918 XFocusChangeEvent
*ev
= &e
->xfocus
;
920 if (selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
921 setfocus(selmon
->sel
);
925 focusmon(const Arg
*arg
)
931 if ((m
= dirtomon(arg
->i
)) == selmon
)
933 unfocus(selmon
->sel
, 0);
939 focusstack(const Arg
*arg
)
941 Client
*c
= NULL
, *i
;
943 if (!selmon
->sel
|| (selmon
->sel
->isfullscreen
&& lockfullscreen
))
946 for (c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
948 for (c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
950 for (i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
954 for (; i
; i
= i
->next
)
965 getatomprop(Client
*c
, Atom prop
)
969 unsigned char *p
= NULL
;
970 Atom da
, atom
= None
;
972 if (XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
973 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
981 getrootptr(int *x
, int *y
)
987 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
995 unsigned char *p
= NULL
;
996 unsigned long n
, extra
;
999 if (XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
1000 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
1009 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
1015 if (!text
|| size
== 0)
1018 if (!XGetTextProperty(dpy
, w
, &name
, atom
) || !name
.nitems
)
1020 if (name
.encoding
== XA_STRING
)
1021 strncpy(text
, (char *)name
.value
, size
- 1);
1023 if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
1024 strncpy(text
, *list
, size
- 1);
1025 XFreeStringList(list
);
1028 text
[size
- 1] = '\0';
1034 grabbuttons(Client
*c
, int focused
)
1036 updatenumlockmask();
1039 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1040 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1042 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
1043 BUTTONMASK
, GrabModeSync
, GrabModeSync
, None
, None
);
1044 for (i
= 0; i
< LENGTH(buttons
); i
++)
1045 if (buttons
[i
].click
== ClkClientWin
)
1046 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1047 XGrabButton(dpy
, buttons
[i
].button
,
1048 buttons
[i
].mask
| modifiers
[j
],
1049 c
->win
, False
, BUTTONMASK
,
1050 GrabModeAsync
, GrabModeSync
, None
, None
);
1057 updatenumlockmask();
1060 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1063 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
1064 for (i
= 0; i
< LENGTH(keys
); i
++)
1065 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
1066 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1067 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
1068 True
, GrabModeAsync
, GrabModeAsync
);
1073 incnmaster(const Arg
*arg
)
1075 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
1081 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
)
1084 if (unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
1085 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
1089 #endif /* XINERAMA */
1099 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1100 for (i
= 0; i
< LENGTH(keys
); i
++)
1101 if (keysym
== keys
[i
].keysym
1102 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1104 keys
[i
].func(&(keys
[i
].arg
));
1108 killclient(const Arg
*arg
)
1112 if (!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1114 XSetErrorHandler(xerrordummy
);
1115 XSetCloseDownMode(dpy
, DestroyAll
);
1116 XKillClient(dpy
, selmon
->sel
->win
);
1118 XSetErrorHandler(xerror
);
1124 manage(Window w
, XWindowAttributes
*wa
)
1126 Client
*c
, *t
= NULL
, *term
= NULL
;
1127 Window trans
= None
;
1130 c
= ecalloc(1, sizeof(Client
));
1134 c
->x
= c
->oldx
= wa
->x
;
1135 c
->y
= c
->oldy
= wa
->y
;
1136 c
->w
= c
->oldw
= wa
->width
;
1137 c
->h
= c
->oldh
= wa
->height
;
1138 c
->oldbw
= wa
->border_width
;
1141 if (XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1147 term
= termforwin(c
);
1150 if (c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1151 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1152 if (c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1153 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1154 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1155 /* only fix client y-offset, if the client center might cover the bar */
1156 c
->y
= MAX(c
->y
, ((c
->mon
->by
== c
->mon
->my
) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1157 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1160 wc
.border_width
= c
->bw
;
1161 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1162 XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1163 configure(c
); /* propagates border_width, if size doesn't change */
1164 updatewindowtype(c
);
1167 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1170 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1172 XRaiseWindow(dpy
, c
->win
);
1175 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1176 (unsigned char *) &(c
->win
), 1);
1177 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1178 setclientstate(c
, NormalState
);
1179 if (c
->mon
== selmon
)
1180 unfocus(selmon
->sel
, 0);
1183 XMapWindow(dpy
, c
->win
);
1190 mappingnotify(XEvent
*e
)
1192 XMappingEvent
*ev
= &e
->xmapping
;
1194 XRefreshKeyboardMapping(ev
);
1195 if (ev
->request
== MappingKeyboard
)
1200 maprequest(XEvent
*e
)
1202 static XWindowAttributes wa
;
1203 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1205 if (!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1207 if (wa
.override_redirect
)
1209 if (!wintoclient(ev
->window
))
1210 manage(ev
->window
, &wa
);
1219 for (c
= m
->clients
; c
; c
= c
->next
)
1222 if (n
> 0) /* override layout symbol */
1223 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1224 for (c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1225 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, 0);
1229 motionnotify(XEvent
*e
)
1231 static Monitor
*mon
= NULL
;
1233 XMotionEvent
*ev
= &e
->xmotion
;
1235 if (ev
->window
!= root
)
1237 if ((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1238 unfocus(selmon
->sel
, 1);
1246 movemouse(const Arg
*arg
)
1248 int x
, y
, ocx
, ocy
, nx
, ny
;
1254 if (!(c
= selmon
->sel
))
1256 if (c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1261 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1262 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1264 if (!getrootptr(&x
, &y
))
1267 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1269 case ConfigureRequest
:
1272 handler
[ev
.type
](&ev
);
1275 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1277 lasttime
= ev
.xmotion
.time
;
1279 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1280 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1281 if (abs(selmon
->wx
- nx
) < snap
)
1283 else if (abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1284 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1285 if (abs(selmon
->wy
- ny
) < snap
)
1287 else if (abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1288 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1289 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1290 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1291 togglefloating(NULL
);
1292 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1293 resize(c
, nx
, ny
, c
->w
, c
->h
, 1);
1296 } while (ev
.type
!= ButtonRelease
);
1297 XUngrabPointer(dpy
, CurrentTime
);
1298 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1306 nexttiled(Client
*c
)
1308 for (; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1322 propertynotify(XEvent
*e
)
1326 XPropertyEvent
*ev
= &e
->xproperty
;
1328 if ((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1330 else if (ev
->state
== PropertyDelete
)
1331 return; /* ignore */
1332 else if ((c
= wintoclient(ev
->window
))) {
1335 case XA_WM_TRANSIENT_FOR
:
1336 if (!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1337 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1340 case XA_WM_NORMAL_HINTS
:
1348 if (ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1350 if (c
== c
->mon
->sel
)
1353 if (ev
->atom
== netatom
[NetWMWindowType
])
1354 updatewindowtype(c
);
1359 quit(const Arg
*arg
)
1361 if(arg
->i
) restart
= 1;
1366 recttomon(int x
, int y
, int w
, int h
)
1368 Monitor
*m
, *r
= selmon
;
1371 for (m
= mons
; m
; m
= m
->next
)
1372 if ((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1380 resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
)
1382 if (applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1383 resizeclient(c
, x
, y
, w
, h
);
1387 resizeclient(Client
*c
, int x
, int y
, int w
, int h
)
1391 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1392 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1393 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1394 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1395 wc
.border_width
= c
->bw
;
1396 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1402 resizemouse(const Arg
*arg
)
1404 int ocx
, ocy
, nw
, nh
;
1410 if (!(c
= selmon
->sel
))
1412 if (c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1417 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1418 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1420 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1422 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1424 case ConfigureRequest
:
1427 handler
[ev
.type
](&ev
);
1430 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1432 lasttime
= ev
.xmotion
.time
;
1434 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1435 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1436 if (c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1437 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1439 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1440 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1441 togglefloating(NULL
);
1443 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1444 resize(c
, c
->x
, c
->y
, nw
, nh
, 1);
1447 } while (ev
.type
!= ButtonRelease
);
1448 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1449 XUngrabPointer(dpy
, CurrentTime
);
1450 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1451 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1468 if (m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1469 XRaiseWindow(dpy
, m
->sel
->win
);
1470 if (m
->lt
[m
->sellt
]->arrange
) {
1471 wc
.stack_mode
= Below
;
1472 wc
.sibling
= m
->barwin
;
1473 for (c
= m
->stack
; c
; c
= c
->snext
)
1474 if (!c
->isfloating
&& ISVISIBLE(c
)) {
1475 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1476 wc
.sibling
= c
->win
;
1480 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1487 /* main event loop */
1489 while (running
&& !XNextEvent(dpy
, &ev
))
1490 if (handler
[ev
.type
])
1491 handler
[ev
.type
](&ev
); /* call handler */
1497 unsigned int i
, num
;
1498 Window d1
, d2
, *wins
= NULL
;
1499 XWindowAttributes wa
;
1501 if (XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1502 for (i
= 0; i
< num
; i
++) {
1503 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1504 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1506 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1507 manage(wins
[i
], &wa
);
1509 for (i
= 0; i
< num
; i
++) { /* now the transients */
1510 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1512 if (XGetTransientForHint(dpy
, wins
[i
], &d1
)
1513 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1514 manage(wins
[i
], &wa
);
1522 sendmon(Client
*c
, Monitor
*m
)
1530 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1538 setclientstate(Client
*c
, long state
)
1540 long data
[] = { state
, None
};
1542 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1543 PropModeReplace
, (unsigned char *)data
, 2);
1547 sendevent(Client
*c
, Atom proto
)
1554 if (XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1555 while (!exists
&& n
--)
1556 exists
= protocols
[n
] == proto
;
1560 ev
.type
= ClientMessage
;
1561 ev
.xclient
.window
= c
->win
;
1562 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1563 ev
.xclient
.format
= 32;
1564 ev
.xclient
.data
.l
[0] = proto
;
1565 ev
.xclient
.data
.l
[1] = CurrentTime
;
1566 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1574 if (!c
->neverfocus
) {
1575 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1576 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1577 XA_WINDOW
, 32, PropModeReplace
,
1578 (unsigned char *) &(c
->win
), 1);
1580 sendevent(c
, wmatom
[WMTakeFocus
]);
1584 setfullscreen(Client
*c
, int fullscreen
)
1586 if (fullscreen
&& !c
->isfullscreen
) {
1587 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1588 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1589 c
->isfullscreen
= 1;
1590 c
->oldstate
= c
->isfloating
;
1594 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1595 XRaiseWindow(dpy
, c
->win
);
1596 } else if (!fullscreen
&& c
->isfullscreen
){
1597 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1598 PropModeReplace
, (unsigned char*)0, 0);
1599 c
->isfullscreen
= 0;
1600 c
->isfloating
= c
->oldstate
;
1606 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1612 setgaps(const Arg
*arg
)
1614 if ((arg
->i
== 0) || (selmon
->gappx
+ arg
->i
< 0))
1617 selmon
->gappx
+= arg
->i
;
1622 setlayout(const Arg
*arg
)
1624 if (!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1627 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1628 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1635 /* arg > 1.0 will set mfact absolutely */
1637 setmfact(const Arg
*arg
)
1641 if (!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1643 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1644 if (f
< 0.05 || f
> 0.95)
1654 XSetWindowAttributes wa
;
1657 /* clean up any zombies immediately */
1660 signal(SIGHUP
, sighup
);
1661 signal(SIGTERM
, sigterm
);
1664 screen
= DefaultScreen(dpy
);
1665 sw
= DisplayWidth(dpy
, screen
);
1666 sh
= DisplayHeight(dpy
, screen
);
1667 root
= RootWindow(dpy
, screen
);
1669 drw
= drw_create(dpy
, screen
, root
, sw
, sh
, visual
, depth
, cmap
);
1670 if (!drw_fontset_create(drw
, fonts
, LENGTH(fonts
)))
1671 die("no fonts could be loaded.");
1672 lrpad
= drw
->fonts
->h
;
1673 bh
= drw
->fonts
->h
+ 2;
1676 utf8string
= XInternAtom(dpy
, "UTF8_STRING", False
);
1677 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1678 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1679 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1680 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1681 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1682 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1683 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1684 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1685 netatom
[NetWMCheck
] = XInternAtom(dpy
, "_NET_SUPPORTING_WM_CHECK", False
);
1686 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1687 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1688 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1689 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1691 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1692 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1693 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1694 /* init appearance */
1695 scheme
= ecalloc(LENGTH(colors
), sizeof(Clr
*));
1696 for (i
= 0; i
< LENGTH(colors
); i
++)
1697 scheme
[i
] = drw_scm_create(drw
, colors
[i
], alphas
[i
], 3);
1701 /* supporting window for NetWMCheck */
1702 wmcheckwin
= XCreateSimpleWindow(dpy
, root
, 0, 0, 1, 1, 0, 0, 0);
1703 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1704 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1705 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMName
], utf8string
, 8,
1706 PropModeReplace
, (unsigned char *) "dwm", 3);
1707 XChangeProperty(dpy
, root
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1708 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1709 /* EWMH support per view */
1710 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1711 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1712 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1714 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1715 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1716 |ButtonPressMask
|PointerMotionMask
|EnterWindowMask
1717 |LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1718 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1719 XSelectInput(dpy
, root
, wa
.event_mask
);
1726 seturgent(Client
*c
, int urg
)
1731 if (!(wmh
= XGetWMHints(dpy
, c
->win
)))
1733 wmh
->flags
= urg
? (wmh
->flags
| XUrgencyHint
) : (wmh
->flags
& ~XUrgencyHint
);
1734 XSetWMHints(dpy
, c
->win
, wmh
);
1744 /* show clients top down */
1745 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1746 if ((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1747 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, 0);
1750 /* hide clients bottom up */
1752 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1759 if (signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1760 die("can't install SIGCHLD handler:");
1761 while (0 < waitpid(-1, NULL
, WNOHANG
));
1779 spawn(const Arg
*arg
)
1781 if (arg
->v
== dmenucmd
)
1782 dmenumon
[0] = '0' + selmon
->num
;
1785 close(ConnectionNumber(dpy
));
1787 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1788 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1797 if (selmon
->sel
&& arg
->ui
& TAGMASK
) {
1798 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1805 tagmon(const Arg
*arg
)
1807 if (!selmon
->sel
|| !mons
->next
)
1809 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1815 unsigned int i
, n
, h
, mw
, my
, ty
;
1818 for (n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1823 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1825 mw
= m
->ww
- m
->gappx
;
1826 for (i
= 0, my
= ty
= m
->gappx
, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1827 if (i
< m
->nmaster
) {
1828 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
) - m
->gappx
;
1829 resize(c
, m
->wx
+ m
->gappx
, m
->wy
+ my
, mw
- (2*c
->bw
) - m
->gappx
, h
- (2*c
->bw
), 0);
1830 if (my
+ HEIGHT(c
) + m
->gappx
< m
->wh
)
1831 my
+= HEIGHT(c
) + m
->gappx
;
1833 h
= (m
->wh
- ty
) / (n
- i
) - m
->gappx
;
1834 resize(c
, m
->wx
+ mw
+ m
->gappx
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
) - 2*m
->gappx
, h
- (2*c
->bw
), 0);
1835 if (ty
+ HEIGHT(c
) + m
->gappx
< m
->wh
)
1836 ty
+= HEIGHT(c
) + m
->gappx
;
1841 togglebar(const Arg
*arg
)
1843 selmon
->showbar
= !selmon
->showbar
;
1844 updatebarpos(selmon
);
1845 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1850 togglefloating(const Arg
*arg
)
1854 if (selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1856 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1857 if (selmon
->sel
->isfloating
)
1858 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1859 selmon
->sel
->w
, selmon
->sel
->h
, 0);
1864 togglefullscr(const Arg
*arg
)
1867 setfullscreen(selmon
->sel
, !selmon
->sel
->isfullscreen
);
1871 toggletag(const Arg
*arg
)
1873 unsigned int newtags
;
1877 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1879 selmon
->sel
->tags
= newtags
;
1886 toggleview(const Arg
*arg
)
1888 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1891 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1898 unfocus(Client
*c
, int setfocus
)
1903 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1905 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1906 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1911 unmanage(Client
*c
, int destroyed
)
1913 Monitor
*m
= c
->mon
;
1916 if (c
->swallowing
) {
1921 Client
*s
= swallowingclient(c
->win
);
1923 free(s
->swallowing
);
1924 s
->swallowing
= NULL
;
1933 wc
.border_width
= c
->oldbw
;
1934 XGrabServer(dpy
); /* avoid race conditions */
1935 XSetErrorHandler(xerrordummy
);
1936 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1937 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1938 setclientstate(c
, WithdrawnState
);
1940 XSetErrorHandler(xerror
);
1953 unmapnotify(XEvent
*e
)
1956 XUnmapEvent
*ev
= &e
->xunmap
;
1958 if ((c
= wintoclient(ev
->window
))) {
1960 setclientstate(c
, WithdrawnState
);
1970 XSetWindowAttributes wa
= {
1971 .override_redirect
= True
,
1972 .background_pixel
= 0,
1975 .event_mask
= ButtonPressMask
|ExposureMask
1977 XClassHint ch
= {"dwm", "dwm"};
1978 for (m
= mons
; m
; m
= m
->next
) {
1981 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, depth
,
1982 InputOutput
, visual
,
1983 CWOverrideRedirect
|CWBackPixel
|CWBorderPixel
|CWColormap
|CWEventMask
, &wa
);
1984 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
1985 XMapRaised(dpy
, m
->barwin
);
1986 XSetClassHint(dpy
, m
->barwin
, &ch
);
1991 updatebarpos(Monitor
*m
)
1997 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1998 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
2009 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
2010 for (m
= mons
; m
; m
= m
->next
)
2011 for (c
= m
->clients
; c
; c
= c
->next
)
2012 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
2013 XA_WINDOW
, 32, PropModeAppend
,
2014 (unsigned char *) &(c
->win
), 1);
2023 if (XineramaIsActive(dpy
)) {
2027 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
2028 XineramaScreenInfo
*unique
= NULL
;
2030 for (n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
2031 /* only consider unique geometries as separate screens */
2032 unique
= ecalloc(nn
, sizeof(XineramaScreenInfo
));
2033 for (i
= 0, j
= 0; i
< nn
; i
++)
2034 if (isuniquegeom(unique
, j
, &info
[i
]))
2035 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
2038 if (n
<= nn
) { /* new monitors available */
2039 for (i
= 0; i
< (nn
- n
); i
++) {
2040 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
2042 m
->next
= createmon();
2046 for (i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
2048 || unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
2049 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
)
2053 m
->mx
= m
->wx
= unique
[i
].x_org
;
2054 m
->my
= m
->wy
= unique
[i
].y_org
;
2055 m
->mw
= m
->ww
= unique
[i
].width
;
2056 m
->mh
= m
->wh
= unique
[i
].height
;
2059 } else { /* less monitors available nn < n */
2060 for (i
= nn
; i
< n
; i
++) {
2061 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
2062 while ((c
= m
->clients
)) {
2064 m
->clients
= c
->next
;
2077 #endif /* XINERAMA */
2078 { /* default monitor setup */
2081 if (mons
->mw
!= sw
|| mons
->mh
!= sh
) {
2083 mons
->mw
= mons
->ww
= sw
;
2084 mons
->mh
= mons
->wh
= sh
;
2090 selmon
= wintomon(root
);
2096 updatenumlockmask(void)
2099 XModifierKeymap
*modmap
;
2102 modmap
= XGetModifierMapping(dpy
);
2103 for (i
= 0; i
< 8; i
++)
2104 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
2105 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
2106 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
2107 numlockmask
= (1 << i
);
2108 XFreeModifiermap(modmap
);
2112 updatesizehints(Client
*c
)
2117 if (!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
2118 /* size is uninitialized, ensure that size.flags aren't used */
2120 if (size
.flags
& PBaseSize
) {
2121 c
->basew
= size
.base_width
;
2122 c
->baseh
= size
.base_height
;
2123 } else if (size
.flags
& PMinSize
) {
2124 c
->basew
= size
.min_width
;
2125 c
->baseh
= size
.min_height
;
2127 c
->basew
= c
->baseh
= 0;
2128 if (size
.flags
& PResizeInc
) {
2129 c
->incw
= size
.width_inc
;
2130 c
->inch
= size
.height_inc
;
2132 c
->incw
= c
->inch
= 0;
2133 if (size
.flags
& PMaxSize
) {
2134 c
->maxw
= size
.max_width
;
2135 c
->maxh
= size
.max_height
;
2137 c
->maxw
= c
->maxh
= 0;
2138 if (size
.flags
& PMinSize
) {
2139 c
->minw
= size
.min_width
;
2140 c
->minh
= size
.min_height
;
2141 } else if (size
.flags
& PBaseSize
) {
2142 c
->minw
= size
.base_width
;
2143 c
->minh
= size
.base_height
;
2145 c
->minw
= c
->minh
= 0;
2146 if (size
.flags
& PAspect
) {
2147 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
2148 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
2150 c
->maxa
= c
->mina
= 0.0;
2151 c
->isfixed
= (c
->maxw
&& c
->maxh
&& c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
2157 if (!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
2158 strcpy(stext
, "dwm-"VERSION
);
2163 updatetitle(Client
*c
)
2165 if (!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
2166 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
2167 if (c
->name
[0] == '\0') /* hack to mark broken clients */
2168 strcpy(c
->name
, broken
);
2172 updatewindowtype(Client
*c
)
2174 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
2175 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
2177 if (state
== netatom
[NetWMFullscreen
])
2178 setfullscreen(c
, 1);
2179 if (wtype
== netatom
[NetWMWindowTypeDialog
])
2184 updatewmhints(Client
*c
)
2188 if ((wmh
= XGetWMHints(dpy
, c
->win
))) {
2189 if (c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2190 wmh
->flags
&= ~XUrgencyHint
;
2191 XSetWMHints(dpy
, c
->win
, wmh
);
2193 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? 1 : 0;
2194 if (wmh
->flags
& InputHint
)
2195 c
->neverfocus
= !wmh
->input
;
2203 view(const Arg
*arg
)
2205 if ((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2207 selmon
->seltags
^= 1; /* toggle sel tagset */
2208 if (arg
->ui
& TAGMASK
)
2209 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2221 xcb_res_client_id_spec_t spec
= {0};
2223 spec
.mask
= XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID
;
2225 xcb_generic_error_t
*e
= NULL
;
2226 xcb_res_query_client_ids_cookie_t c
= xcb_res_query_client_ids(xcon
, 1, &spec
);
2227 xcb_res_query_client_ids_reply_t
*r
= xcb_res_query_client_ids_reply(xcon
, c
, &e
);
2232 xcb_res_client_id_value_iterator_t i
= xcb_res_query_client_ids_ids_iterator(r
);
2233 for (; i
.rem
; xcb_res_client_id_value_next(&i
)) {
2234 spec
= i
.data
->spec
;
2235 if (spec
.mask
& XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID
) {
2236 uint32_t *t
= xcb_res_client_id_value_value(i
.data
);
2244 if (result
== (pid_t
)-1)
2247 #endif /* __linux__ */
2252 unsigned long len
, bytes
;
2253 unsigned char *prop
;
2256 if (XGetWindowProperty(dpy
, w
, XInternAtom(dpy
, "_NET_WM_PID", 0), 0, 1, False
, AnyPropertyType
, &type
, &format
, &len
, &bytes
, &prop
) != Success
|| !prop
)
2259 ret
= *(pid_t
*)prop
;
2263 #endif /* __OpenBSD__ */
2268 getparentprocess(pid_t p
)
2275 snprintf(buf
, sizeof(buf
) - 1, "/proc/%u/stat", (unsigned)p
);
2277 if (!(f
= fopen(buf
, "r")))
2280 fscanf(f
, "%*u %*s %*c %u", &v
);
2282 #endif /* __linux__*/
2287 struct kinfo_proc
*kp
;
2289 kd
= kvm_openfiles(NULL
, NULL
, NULL
, KVM_NO_FILES
, NULL
);
2293 kp
= kvm_getprocs(kd
, KERN_PROC_PID
, p
, sizeof(*kp
), &n
);
2295 #endif /* __OpenBSD__ */
2301 isdescprocess(pid_t p
, pid_t c
)
2303 while (p
!= c
&& c
!= 0)
2304 c
= getparentprocess(c
);
2310 termforwin(const Client
*w
)
2315 if (!w
->pid
|| w
->isterminal
)
2318 for (m
= mons
; m
; m
= m
->next
) {
2319 for (c
= m
->clients
; c
; c
= c
->next
) {
2320 if (c
->isterminal
&& !c
->swallowing
&& c
->pid
&& isdescprocess(c
->pid
, w
->pid
))
2329 swallowingclient(Window w
)
2334 for (m
= mons
; m
; m
= m
->next
) {
2335 for (c
= m
->clients
; c
; c
= c
->next
) {
2336 if (c
->swallowing
&& c
->swallowing
->win
== w
)
2345 wintoclient(Window w
)
2350 for (m
= mons
; m
; m
= m
->next
)
2351 for (c
= m
->clients
; c
; c
= c
->next
)
2364 if (w
== root
&& getrootptr(&x
, &y
))
2365 return recttomon(x
, y
, 1, 1);
2366 for (m
= mons
; m
; m
= m
->next
)
2369 if ((c
= wintoclient(w
)))
2374 /* There's no way to check accesses to destroyed windows, thus those cases are
2375 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2376 * default error handler, which may call exit. */
2378 xerror(Display
*dpy
, XErrorEvent
*ee
)
2380 if (ee
->error_code
== BadWindow
2381 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2382 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2383 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2384 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2385 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2386 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2387 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2388 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2390 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2391 ee
->request_code
, ee
->error_code
);
2392 return xerrorxlib(dpy
, ee
); /* may call exit */
2396 xerrordummy(Display
*dpy
, XErrorEvent
*ee
)
2401 /* Startup Error handler to check if another window manager
2402 * is already running. */
2404 xerrorstart(Display
*dpy
, XErrorEvent
*ee
)
2406 die("dwm: another window manager is already running");
2414 XRenderPictFormat
*fmt
;
2423 long masks
= VisualScreenMask
| VisualDepthMask
| VisualClassMask
;
2425 infos
= XGetVisualInfo(dpy
, masks
, &tpl
, &nitems
);
2427 for(i
= 0; i
< nitems
; i
++) {
2428 fmt
= XRenderFindVisualFormat(dpy
, infos
[i
].visual
);
2429 if (fmt
->type
== PictTypeDirect
&& fmt
->direct
.alphaMask
) {
2430 visual
= infos
[i
].visual
;
2431 depth
= infos
[i
].depth
;
2432 cmap
= XCreateColormap(dpy
, root
, visual
, AllocNone
);
2441 visual
= DefaultVisual(dpy
, screen
);
2442 depth
= DefaultDepth(dpy
, screen
);
2443 cmap
= DefaultColormap(dpy
, screen
);
2448 zoom(const Arg
*arg
)
2450 Client
*c
= selmon
->sel
;
2452 if (!selmon
->lt
[selmon
->sellt
]->arrange
2453 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2455 if (c
== nexttiled(selmon
->clients
))
2456 if (!c
|| !(c
= nexttiled(c
->next
)))
2462 resource_load(XrmDatabase db
, char *name
, enum resource_type rtype
, void *dst
)
2476 snprintf(fullname
, sizeof(fullname
), "%s.%s", "dwm", name
);
2477 fullname
[sizeof(fullname
) - 1] = '\0';
2479 XrmGetResource(db
, fullname
, "*", &type
, &ret
);
2480 if (!(ret
.addr
== NULL
|| strncmp("String", type
, 64)))
2484 strcpy(sdst
, ret
.addr
);
2487 *idst
= strtoul(ret
.addr
, NULL
, 10);
2490 *fdst
= strtof(ret
.addr
, NULL
);
2497 load_xresources(void)
2504 display
= XOpenDisplay(NULL
);
2505 resm
= XResourceManagerString(display
);
2509 db
= XrmGetStringDatabase(resm
);
2510 for (p
= resources
; p
< resources
+ LENGTH(resources
); p
++)
2511 resource_load(db
, p
->name
, p
->type
, p
->dst
);
2512 XCloseDisplay(display
);
2516 main(int argc
, char *argv
[])
2518 if (argc
== 2 && !strcmp("-v", argv
[1]))
2521 die("usage: dwm [-v]");
2522 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2523 fputs("warning: no locale support\n", stderr
);
2524 if (!(dpy
= XOpenDisplay(NULL
)))
2525 die("dwm: cannot open display");
2526 if (!(xcon
= XGetXCBConnection(dpy
)))
2527 die("dwm: cannot get xcb connection\n");
2533 if (pledge("stdio rpath proc exec ps", NULL
) == -1)
2535 #endif /* __OpenBSD__ */
2538 if(restart
) execvp(argv
[0], argv
);
2541 return EXIT_SUCCESS
;