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 unsigned int seltags
;
134 unsigned int tagset
[2];
147 const char *instance
;
156 /* function declarations */
157 static void applyrules(Client
*c
);
158 static int applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
);
159 static void arrange(Monitor
*m
);
160 static void arrangemon(Monitor
*m
);
161 static void attach(Client
*c
);
162 static void attachstack(Client
*c
);
163 static void buttonpress(XEvent
*e
);
164 static void checkotherwm(void);
165 static void cleanup(void);
166 static void cleanupmon(Monitor
*mon
);
167 static void clientmessage(XEvent
*e
);
168 static void configure(Client
*c
);
169 static void configurenotify(XEvent
*e
);
170 static void configurerequest(XEvent
*e
);
171 static Monitor
*createmon(void);
172 static void destroynotify(XEvent
*e
);
173 static void detach(Client
*c
);
174 static void detachstack(Client
*c
);
175 static Monitor
*dirtomon(int dir
);
176 static void drawbar(Monitor
*m
);
177 static void drawbars(void);
178 static void enternotify(XEvent
*e
);
179 static void expose(XEvent
*e
);
180 static void focus(Client
*c
);
181 static void focusin(XEvent
*e
);
182 static void focusmon(const Arg
*arg
);
183 static void focusstack(const Arg
*arg
);
184 static Atom
getatomprop(Client
*c
, Atom prop
);
185 static int getrootptr(int *x
, int *y
);
186 static long getstate(Window w
);
187 static int gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
188 static void grabbuttons(Client
*c
, int focused
);
189 static void grabkeys(void);
190 static void incnmaster(const Arg
*arg
);
191 static void keypress(XEvent
*e
);
192 static void killclient(const Arg
*arg
);
193 static void manage(Window w
, XWindowAttributes
*wa
);
194 static void mappingnotify(XEvent
*e
);
195 static void maprequest(XEvent
*e
);
196 static void monocle(Monitor
*m
);
197 static void motionnotify(XEvent
*e
);
198 static void movemouse(const Arg
*arg
);
199 static Client
*nexttiled(Client
*c
);
200 static void pop(Client
*);
201 static void propertynotify(XEvent
*e
);
202 static void quit(const Arg
*arg
);
203 static Monitor
*recttomon(int x
, int y
, int w
, int h
);
204 static void resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
);
205 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
206 static void resizemouse(const Arg
*arg
);
207 static void restack(Monitor
*m
);
208 static void run(void);
209 static void scan(void);
210 static int sendevent(Client
*c
, Atom proto
);
211 static void sendmon(Client
*c
, Monitor
*m
);
212 static void setclientstate(Client
*c
, long state
);
213 static void setfocus(Client
*c
);
214 static void setfullscreen(Client
*c
, int fullscreen
);
215 static void setlayout(const Arg
*arg
);
216 static void setmfact(const Arg
*arg
);
217 static void setup(void);
218 static void seturgent(Client
*c
, int urg
);
219 static void showhide(Client
*c
);
220 static void sigchld(int unused
);
221 static void spawn(const Arg
*arg
);
222 static void tag(const Arg
*arg
);
223 static void tagmon(const Arg
*arg
);
224 static void tile(Monitor
*);
225 static void togglebar(const Arg
*arg
);
226 static void togglefloating(const Arg
*arg
);
227 static void toggletag(const Arg
*arg
);
228 static void toggleview(const Arg
*arg
);
229 static void unfocus(Client
*c
, int setfocus
);
230 static void unmanage(Client
*c
, int destroyed
);
231 static void unmapnotify(XEvent
*e
);
232 static void updatebarpos(Monitor
*m
);
233 static void updatebars(void);
234 static void updateclientlist(void);
235 static int updategeom(void);
236 static void updatenumlockmask(void);
237 static void updatesizehints(Client
*c
);
238 static void updatestatus(void);
239 static void updatetitle(Client
*c
);
240 static void updatewindowtype(Client
*c
);
241 static void updatewmhints(Client
*c
);
242 static void view(const Arg
*arg
);
243 static Client
*wintoclient(Window w
);
244 static Monitor
*wintomon(Window w
);
245 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
246 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
247 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
248 static void xinitvisual();
249 static void zoom(const Arg
*arg
);
251 static pid_t
getparentprocess(pid_t p
);
252 static int isdescprocess(pid_t p
, pid_t c
);
253 static Client
*swallowingclient(Window w
);
254 static Client
*termforwin(const Client
*c
);
255 static pid_t
winpid(Window w
);
258 static const char broken
[] = "broken";
259 static char stext
[256];
261 static int sw
, sh
; /* X display screen geometry width, height */
262 static int bh
, blw
= 0; /* bar geometry */
263 static int lrpad
; /* sum of left and right padding for text */
264 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
265 static unsigned int numlockmask
= 0;
266 static void (*handler
[LASTEvent
]) (XEvent
*) = {
267 [ButtonPress
] = buttonpress
,
268 [ClientMessage
] = clientmessage
,
269 [ConfigureRequest
] = configurerequest
,
270 [ConfigureNotify
] = configurenotify
,
271 [DestroyNotify
] = destroynotify
,
272 [EnterNotify
] = enternotify
,
275 [KeyPress
] = keypress
,
276 [MappingNotify
] = mappingnotify
,
277 [MapRequest
] = maprequest
,
278 [MotionNotify
] = motionnotify
,
279 [PropertyNotify
] = propertynotify
,
280 [UnmapNotify
] = unmapnotify
282 static Atom wmatom
[WMLast
], netatom
[NetLast
];
283 static int running
= 1;
284 static Cur
*cursor
[CurLast
];
288 static Monitor
*mons
, *selmon
;
289 static Window root
, wmcheckwin
;
291 static xcb_connection_t
*xcon
;
293 static int useargb
= 0;
294 static Visual
*visual
;
296 static Colormap cmap
;
298 /* configuration, allows nested code to access above variables */
301 /* compile-time check if all tags fit into an unsigned int bit array. */
302 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
304 /* function implementations */
306 applyrules(Client
*c
)
308 const char *class, *instance
;
312 XClassHint ch
= { NULL
, NULL
};
317 XGetClassHint(dpy
, c
->win
, &ch
);
318 class = ch
.res_class
? ch
.res_class
: broken
;
319 instance
= ch
.res_name
? ch
.res_name
: broken
;
321 for (i
= 0; i
< LENGTH(rules
); i
++) {
323 if ((!r
->title
|| strstr(c
->name
, r
->title
))
324 && (!r
->class || strstr(class, r
->class))
325 && (!r
->instance
|| strstr(instance
, r
->instance
)))
327 c
->isterminal
= r
->isterminal
;
328 c
->noswallow
= r
->noswallow
;
329 c
->isfloating
= r
->isfloating
;
331 for (m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
340 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
344 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
)
349 /* set minimum possible */
357 if (*x
+ *w
+ 2 * c
->bw
< 0)
359 if (*y
+ *h
+ 2 * c
->bw
< 0)
362 if (*x
>= m
->wx
+ m
->ww
)
363 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
364 if (*y
>= m
->wy
+ m
->wh
)
365 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
366 if (*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
368 if (*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
375 if (resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
376 /* see last two sentences in ICCCM 4.1.2.3 */
377 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
378 if (!baseismin
) { /* temporarily remove base dimensions */
382 /* adjust for aspect limits */
383 if (c
->mina
> 0 && c
->maxa
> 0) {
384 if (c
->maxa
< (float)*w
/ *h
)
385 *w
= *h
* c
->maxa
+ 0.5;
386 else if (c
->mina
< (float)*h
/ *w
)
387 *h
= *w
* c
->mina
+ 0.5;
389 if (baseismin
) { /* increment calculation requires this */
393 /* adjust for increment value */
398 /* restore base dimensions */
399 *w
= MAX(*w
+ c
->basew
, c
->minw
);
400 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
402 *w
= MIN(*w
, c
->maxw
);
404 *h
= MIN(*h
, c
->maxh
);
406 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
414 else for (m
= mons
; m
; m
= m
->next
)
419 } else for (m
= mons
; m
; m
= m
->next
)
424 arrangemon(Monitor
*m
)
426 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
427 if (m
->lt
[m
->sellt
]->arrange
)
428 m
->lt
[m
->sellt
]->arrange(m
);
434 c
->next
= c
->mon
->clients
;
439 attachstack(Client
*c
)
441 c
->snext
= c
->mon
->stack
;
446 swallow(Client
*p
, Client
*c
)
449 if (c
->noswallow
|| c
->isterminal
)
451 if (c
->noswallow
&& !swallowfloating
&& c
->isfloating
)
457 setclientstate(c
, WithdrawnState
);
458 XUnmapWindow(dpy
, p
->win
);
467 XMoveResizeWindow(dpy
, p
->win
, p
->x
, p
->y
, p
->w
, p
->h
);
476 c
->win
= c
->swallowing
->win
;
479 c
->swallowing
= NULL
;
481 /* unfullscreen the client */
485 XMapWindow(dpy
, c
->win
);
486 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
487 setclientstate(c
, NormalState
);
493 buttonpress(XEvent
*e
)
495 unsigned int i
, x
, click
;
499 XButtonPressedEvent
*ev
= &e
->xbutton
;
502 /* focus monitor if necessary */
503 if ((m
= wintomon(ev
->window
)) && m
!= selmon
) {
504 unfocus(selmon
->sel
, 1);
508 if (ev
->window
== selmon
->barwin
) {
512 while (ev
->x
>= x
&& ++i
< LENGTH(tags
));
513 if (i
< LENGTH(tags
)) {
516 } else if (ev
->x
< x
+ blw
)
518 else if (ev
->x
> selmon
->ww
- (int)TEXTW(stext
))
519 click
= ClkStatusText
;
522 } else if ((c
= wintoclient(ev
->window
))) {
525 XAllowEvents(dpy
, ReplayPointer
, CurrentTime
);
526 click
= ClkClientWin
;
528 for (i
= 0; i
< LENGTH(buttons
); i
++)
529 if (click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
530 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
531 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
537 xerrorxlib
= XSetErrorHandler(xerrorstart
);
538 /* this causes an error if some other window manager is running */
539 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
541 XSetErrorHandler(xerror
);
549 Layout foo
= { "", NULL
};
554 selmon
->lt
[selmon
->sellt
] = &foo
;
555 for (m
= mons
; m
; m
= m
->next
)
557 unmanage(m
->stack
, 0);
558 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
561 for (i
= 0; i
< CurLast
; i
++)
562 drw_cur_free(drw
, cursor
[i
]);
563 for (i
= 0; i
< LENGTH(colors
); i
++)
566 XDestroyWindow(dpy
, wmcheckwin
);
569 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
570 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
574 cleanupmon(Monitor
*mon
)
581 for (m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
584 XUnmapWindow(dpy
, mon
->barwin
);
585 XDestroyWindow(dpy
, mon
->barwin
);
590 clientmessage(XEvent
*e
)
592 XClientMessageEvent
*cme
= &e
->xclient
;
593 Client
*c
= wintoclient(cme
->window
);
597 if (cme
->message_type
== netatom
[NetWMState
]) {
598 if (cme
->data
.l
[1] == netatom
[NetWMFullscreen
]
599 || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
600 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
601 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
602 } else if (cme
->message_type
== netatom
[NetActiveWindow
]) {
603 if (c
!= selmon
->sel
&& !c
->isurgent
)
613 ce
.type
= ConfigureNotify
;
621 ce
.border_width
= c
->bw
;
623 ce
.override_redirect
= False
;
624 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
628 configurenotify(XEvent
*e
)
632 XConfigureEvent
*ev
= &e
->xconfigure
;
635 /* TODO: updategeom handling sucks, needs to be simplified */
636 if (ev
->window
== root
) {
637 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
640 if (updategeom() || dirty
) {
641 drw_resize(drw
, sw
, bh
);
643 for (m
= mons
; m
; m
= m
->next
) {
644 for (c
= m
->clients
; c
; c
= c
->next
)
646 resizeclient(c
, m
->mx
, m
->my
, m
->mw
, m
->mh
);
647 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
656 configurerequest(XEvent
*e
)
660 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
663 if ((c
= wintoclient(ev
->window
))) {
664 if (ev
->value_mask
& CWBorderWidth
)
665 c
->bw
= ev
->border_width
;
666 else if (c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
668 if (ev
->value_mask
& CWX
) {
670 c
->x
= m
->mx
+ ev
->x
;
672 if (ev
->value_mask
& CWY
) {
674 c
->y
= m
->my
+ ev
->y
;
676 if (ev
->value_mask
& CWWidth
) {
680 if (ev
->value_mask
& CWHeight
) {
684 if ((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
685 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
686 if ((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
687 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
688 if ((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
691 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
697 wc
.width
= ev
->width
;
698 wc
.height
= ev
->height
;
699 wc
.border_width
= ev
->border_width
;
700 wc
.sibling
= ev
->above
;
701 wc
.stack_mode
= ev
->detail
;
702 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
712 m
= ecalloc(1, sizeof(Monitor
));
713 m
->tagset
[0] = m
->tagset
[1] = 1;
715 m
->nmaster
= nmaster
;
716 m
->showbar
= showbar
;
718 m
->lt
[0] = &layouts
[0];
719 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
720 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
725 destroynotify(XEvent
*e
)
728 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
730 if ((c
= wintoclient(ev
->window
)))
733 else if ((c
= swallowingclient(ev
->window
)))
734 unmanage(c
->swallowing
, 1);
742 for (tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
747 detachstack(Client
*c
)
751 for (tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
754 if (c
== c
->mon
->sel
) {
755 for (t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
766 if (!(m
= selmon
->next
))
768 } else if (selmon
== mons
)
769 for (m
= mons
; m
->next
; m
= m
->next
);
771 for (m
= mons
; m
->next
!= selmon
; m
= m
->next
);
779 int boxs
= drw
->fonts
->h
/ 9;
780 int boxw
= drw
->fonts
->h
/ 6 + 2;
781 unsigned int i
, occ
= 0, urg
= 0;
787 /* draw status first so it can be overdrawn by tags later */
788 if (m
== selmon
) { /* status is only drawn on selected monitor */
789 drw_setscheme(drw
, scheme
[SchemeNorm
]);
790 tw
= TEXTW(stext
) - lrpad
+ 2; /* 2px right padding */
791 drw_text(drw
, m
->ww
- tw
, 0, tw
, bh
, 0, stext
, 0);
794 for (c
= m
->clients
; c
; c
= c
->next
) {
800 for (i
= 0; i
< LENGTH(tags
); i
++) {
802 drw_setscheme(drw
, scheme
[m
->tagset
[m
->seltags
] & 1 << i
? SchemeSel
: SchemeNorm
]);
803 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, tags
[i
], urg
& 1 << i
);
805 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
,
806 m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
810 w
= blw
= TEXTW(m
->ltsymbol
);
811 drw_setscheme(drw
, scheme
[SchemeNorm
]);
812 x
= drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->ltsymbol
, 0);
814 if ((w
= m
->ww
- tw
- x
) > bh
) {
816 drw_setscheme(drw
, scheme
[m
== selmon
? SchemeSel
: SchemeNorm
]);
817 drw_text(drw
, x
, 0, w
, bh
, lrpad
/ 2, m
->sel
->name
, 0);
818 if (m
->sel
->isfloating
)
819 drw_rect(drw
, x
+ boxs
, boxs
, boxw
, boxw
, m
->sel
->isfixed
, 0);
821 drw_setscheme(drw
, scheme
[SchemeNorm
]);
822 drw_rect(drw
, x
, 0, w
, bh
, 1, 1);
825 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
);
833 for (m
= mons
; m
; m
= m
->next
)
838 enternotify(XEvent
*e
)
842 XCrossingEvent
*ev
= &e
->xcrossing
;
844 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
846 c
= wintoclient(ev
->window
);
847 m
= c
? c
->mon
: wintomon(ev
->window
);
849 unfocus(selmon
->sel
, 1);
851 } else if (!c
|| c
== selmon
->sel
)
860 XExposeEvent
*ev
= &e
->xexpose
;
862 if (ev
->count
== 0 && (m
= wintomon(ev
->window
)))
869 if (!c
|| !ISVISIBLE(c
))
870 for (c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
871 if (selmon
->sel
&& selmon
->sel
!= c
)
872 unfocus(selmon
->sel
, 0);
874 if (c
->mon
!= selmon
)
881 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
][ColBorder
].pixel
);
884 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
885 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
891 /* there are some broken focus acquiring clients needing extra handling */
895 XFocusChangeEvent
*ev
= &e
->xfocus
;
897 if (selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
898 setfocus(selmon
->sel
);
902 focusmon(const Arg
*arg
)
908 if ((m
= dirtomon(arg
->i
)) == selmon
)
910 unfocus(selmon
->sel
, 0);
916 focusstack(const Arg
*arg
)
918 Client
*c
= NULL
, *i
;
920 if (!selmon
->sel
|| (selmon
->sel
->isfullscreen
&& lockfullscreen
))
923 for (c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
925 for (c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
927 for (i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
931 for (; i
; i
= i
->next
)
942 getatomprop(Client
*c
, Atom prop
)
946 unsigned char *p
= NULL
;
947 Atom da
, atom
= None
;
949 if (XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
950 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
958 getrootptr(int *x
, int *y
)
964 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
972 unsigned char *p
= NULL
;
973 unsigned long n
, extra
;
976 if (XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
977 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
986 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
992 if (!text
|| size
== 0)
995 if (!XGetTextProperty(dpy
, w
, &name
, atom
) || !name
.nitems
)
997 if (name
.encoding
== XA_STRING
)
998 strncpy(text
, (char *)name
.value
, size
- 1);
1000 if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
1001 strncpy(text
, *list
, size
- 1);
1002 XFreeStringList(list
);
1005 text
[size
- 1] = '\0';
1011 grabbuttons(Client
*c
, int focused
)
1013 updatenumlockmask();
1016 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1017 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1019 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
1020 BUTTONMASK
, GrabModeSync
, GrabModeSync
, None
, None
);
1021 for (i
= 0; i
< LENGTH(buttons
); i
++)
1022 if (buttons
[i
].click
== ClkClientWin
)
1023 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1024 XGrabButton(dpy
, buttons
[i
].button
,
1025 buttons
[i
].mask
| modifiers
[j
],
1026 c
->win
, False
, BUTTONMASK
,
1027 GrabModeAsync
, GrabModeSync
, None
, None
);
1034 updatenumlockmask();
1037 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1040 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
1041 for (i
= 0; i
< LENGTH(keys
); i
++)
1042 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
1043 for (j
= 0; j
< LENGTH(modifiers
); j
++)
1044 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
1045 True
, GrabModeAsync
, GrabModeAsync
);
1050 incnmaster(const Arg
*arg
)
1052 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
1058 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
)
1061 if (unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
1062 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
1066 #endif /* XINERAMA */
1076 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1077 for (i
= 0; i
< LENGTH(keys
); i
++)
1078 if (keysym
== keys
[i
].keysym
1079 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1081 keys
[i
].func(&(keys
[i
].arg
));
1085 killclient(const Arg
*arg
)
1089 if (!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1091 XSetErrorHandler(xerrordummy
);
1092 XSetCloseDownMode(dpy
, DestroyAll
);
1093 XKillClient(dpy
, selmon
->sel
->win
);
1095 XSetErrorHandler(xerror
);
1101 manage(Window w
, XWindowAttributes
*wa
)
1103 Client
*c
, *t
= NULL
, *term
= NULL
;
1104 Window trans
= None
;
1107 c
= ecalloc(1, sizeof(Client
));
1111 c
->x
= c
->oldx
= wa
->x
;
1112 c
->y
= c
->oldy
= wa
->y
;
1113 c
->w
= c
->oldw
= wa
->width
;
1114 c
->h
= c
->oldh
= wa
->height
;
1115 c
->oldbw
= wa
->border_width
;
1118 if (XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1124 term
= termforwin(c
);
1127 if (c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1128 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1129 if (c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1130 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1131 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1132 /* only fix client y-offset, if the client center might cover the bar */
1133 c
->y
= MAX(c
->y
, ((c
->mon
->by
== c
->mon
->my
) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1134 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1137 wc
.border_width
= c
->bw
;
1138 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1139 XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1140 configure(c
); /* propagates border_width, if size doesn't change */
1141 updatewindowtype(c
);
1144 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1147 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1149 XRaiseWindow(dpy
, c
->win
);
1152 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1153 (unsigned char *) &(c
->win
), 1);
1154 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1155 setclientstate(c
, NormalState
);
1156 if (c
->mon
== selmon
)
1157 unfocus(selmon
->sel
, 0);
1160 XMapWindow(dpy
, c
->win
);
1167 mappingnotify(XEvent
*e
)
1169 XMappingEvent
*ev
= &e
->xmapping
;
1171 XRefreshKeyboardMapping(ev
);
1172 if (ev
->request
== MappingKeyboard
)
1177 maprequest(XEvent
*e
)
1179 static XWindowAttributes wa
;
1180 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1182 if (!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1184 if (wa
.override_redirect
)
1186 if (!wintoclient(ev
->window
))
1187 manage(ev
->window
, &wa
);
1196 for (c
= m
->clients
; c
; c
= c
->next
)
1199 if (n
> 0) /* override layout symbol */
1200 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1201 for (c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1202 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, 0);
1206 motionnotify(XEvent
*e
)
1208 static Monitor
*mon
= NULL
;
1210 XMotionEvent
*ev
= &e
->xmotion
;
1212 if (ev
->window
!= root
)
1214 if ((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1215 unfocus(selmon
->sel
, 1);
1223 movemouse(const Arg
*arg
)
1225 int x
, y
, ocx
, ocy
, nx
, ny
;
1231 if (!(c
= selmon
->sel
))
1233 if (c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1238 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1239 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1241 if (!getrootptr(&x
, &y
))
1244 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1246 case ConfigureRequest
:
1249 handler
[ev
.type
](&ev
);
1252 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1254 lasttime
= ev
.xmotion
.time
;
1256 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1257 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1258 if (abs(selmon
->wx
- nx
) < snap
)
1260 else if (abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1261 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1262 if (abs(selmon
->wy
- ny
) < snap
)
1264 else if (abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1265 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1266 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1267 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1268 togglefloating(NULL
);
1269 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1270 resize(c
, nx
, ny
, c
->w
, c
->h
, 1);
1273 } while (ev
.type
!= ButtonRelease
);
1274 XUngrabPointer(dpy
, CurrentTime
);
1275 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1283 nexttiled(Client
*c
)
1285 for (; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1299 propertynotify(XEvent
*e
)
1303 XPropertyEvent
*ev
= &e
->xproperty
;
1305 if ((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1307 else if (ev
->state
== PropertyDelete
)
1308 return; /* ignore */
1309 else if ((c
= wintoclient(ev
->window
))) {
1312 case XA_WM_TRANSIENT_FOR
:
1313 if (!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1314 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1317 case XA_WM_NORMAL_HINTS
:
1325 if (ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1327 if (c
== c
->mon
->sel
)
1330 if (ev
->atom
== netatom
[NetWMWindowType
])
1331 updatewindowtype(c
);
1336 quit(const Arg
*arg
)
1342 recttomon(int x
, int y
, int w
, int h
)
1344 Monitor
*m
, *r
= selmon
;
1347 for (m
= mons
; m
; m
= m
->next
)
1348 if ((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1356 resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
)
1358 if (applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1359 resizeclient(c
, x
, y
, w
, h
);
1363 resizeclient(Client
*c
, int x
, int y
, int w
, int h
)
1367 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1368 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1369 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1370 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1371 wc
.border_width
= c
->bw
;
1372 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1378 resizemouse(const Arg
*arg
)
1380 int ocx
, ocy
, nw
, nh
;
1386 if (!(c
= selmon
->sel
))
1388 if (c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1393 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1394 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1396 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1398 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1400 case ConfigureRequest
:
1403 handler
[ev
.type
](&ev
);
1406 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1408 lasttime
= ev
.xmotion
.time
;
1410 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1411 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1412 if (c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1413 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1415 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1416 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1417 togglefloating(NULL
);
1419 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1420 resize(c
, c
->x
, c
->y
, nw
, nh
, 1);
1423 } while (ev
.type
!= ButtonRelease
);
1424 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1425 XUngrabPointer(dpy
, CurrentTime
);
1426 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1427 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1444 if (m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1445 XRaiseWindow(dpy
, m
->sel
->win
);
1446 if (m
->lt
[m
->sellt
]->arrange
) {
1447 wc
.stack_mode
= Below
;
1448 wc
.sibling
= m
->barwin
;
1449 for (c
= m
->stack
; c
; c
= c
->snext
)
1450 if (!c
->isfloating
&& ISVISIBLE(c
)) {
1451 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1452 wc
.sibling
= c
->win
;
1456 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1463 /* main event loop */
1465 while (running
&& !XNextEvent(dpy
, &ev
))
1466 if (handler
[ev
.type
])
1467 handler
[ev
.type
](&ev
); /* call handler */
1473 unsigned int i
, num
;
1474 Window d1
, d2
, *wins
= NULL
;
1475 XWindowAttributes wa
;
1477 if (XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1478 for (i
= 0; i
< num
; i
++) {
1479 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1480 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1482 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1483 manage(wins
[i
], &wa
);
1485 for (i
= 0; i
< num
; i
++) { /* now the transients */
1486 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1488 if (XGetTransientForHint(dpy
, wins
[i
], &d1
)
1489 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1490 manage(wins
[i
], &wa
);
1498 sendmon(Client
*c
, Monitor
*m
)
1506 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1514 setclientstate(Client
*c
, long state
)
1516 long data
[] = { state
, None
};
1518 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1519 PropModeReplace
, (unsigned char *)data
, 2);
1523 sendevent(Client
*c
, Atom proto
)
1530 if (XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1531 while (!exists
&& n
--)
1532 exists
= protocols
[n
] == proto
;
1536 ev
.type
= ClientMessage
;
1537 ev
.xclient
.window
= c
->win
;
1538 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1539 ev
.xclient
.format
= 32;
1540 ev
.xclient
.data
.l
[0] = proto
;
1541 ev
.xclient
.data
.l
[1] = CurrentTime
;
1542 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1550 if (!c
->neverfocus
) {
1551 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1552 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1553 XA_WINDOW
, 32, PropModeReplace
,
1554 (unsigned char *) &(c
->win
), 1);
1556 sendevent(c
, wmatom
[WMTakeFocus
]);
1560 setfullscreen(Client
*c
, int fullscreen
)
1562 if (fullscreen
&& !c
->isfullscreen
) {
1563 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1564 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1565 c
->isfullscreen
= 1;
1566 c
->oldstate
= c
->isfloating
;
1570 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1571 XRaiseWindow(dpy
, c
->win
);
1572 } else if (!fullscreen
&& c
->isfullscreen
){
1573 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1574 PropModeReplace
, (unsigned char*)0, 0);
1575 c
->isfullscreen
= 0;
1576 c
->isfloating
= c
->oldstate
;
1582 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1588 setlayout(const Arg
*arg
)
1590 if (!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1593 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1594 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1601 /* arg > 1.0 will set mfact absolutely */
1603 setmfact(const Arg
*arg
)
1607 if (!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1609 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1610 if (f
< 0.05 || f
> 0.95)
1620 XSetWindowAttributes wa
;
1623 /* clean up any zombies immediately */
1627 screen
= DefaultScreen(dpy
);
1628 sw
= DisplayWidth(dpy
, screen
);
1629 sh
= DisplayHeight(dpy
, screen
);
1630 root
= RootWindow(dpy
, screen
);
1632 drw
= drw_create(dpy
, screen
, root
, sw
, sh
, visual
, depth
, cmap
);
1633 if (!drw_fontset_create(drw
, fonts
, LENGTH(fonts
)))
1634 die("no fonts could be loaded.");
1635 lrpad
= drw
->fonts
->h
;
1636 bh
= drw
->fonts
->h
+ 2;
1639 utf8string
= XInternAtom(dpy
, "UTF8_STRING", False
);
1640 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1641 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1642 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1643 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1644 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1645 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1646 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1647 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1648 netatom
[NetWMCheck
] = XInternAtom(dpy
, "_NET_SUPPORTING_WM_CHECK", False
);
1649 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1650 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1651 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1652 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1654 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1655 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1656 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1657 /* init appearance */
1658 scheme
= ecalloc(LENGTH(colors
), sizeof(Clr
*));
1659 for (i
= 0; i
< LENGTH(colors
); i
++)
1660 scheme
[i
] = drw_scm_create(drw
, colors
[i
], alphas
[i
], 3);
1664 /* supporting window for NetWMCheck */
1665 wmcheckwin
= XCreateSimpleWindow(dpy
, root
, 0, 0, 1, 1, 0, 0, 0);
1666 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1667 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1668 XChangeProperty(dpy
, wmcheckwin
, netatom
[NetWMName
], utf8string
, 8,
1669 PropModeReplace
, (unsigned char *) "dwm", 3);
1670 XChangeProperty(dpy
, root
, netatom
[NetWMCheck
], XA_WINDOW
, 32,
1671 PropModeReplace
, (unsigned char *) &wmcheckwin
, 1);
1672 /* EWMH support per view */
1673 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1674 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1675 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1677 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1678 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
1679 |ButtonPressMask
|PointerMotionMask
|EnterWindowMask
1680 |LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1681 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1682 XSelectInput(dpy
, root
, wa
.event_mask
);
1689 seturgent(Client
*c
, int urg
)
1694 if (!(wmh
= XGetWMHints(dpy
, c
->win
)))
1696 wmh
->flags
= urg
? (wmh
->flags
| XUrgencyHint
) : (wmh
->flags
& ~XUrgencyHint
);
1697 XSetWMHints(dpy
, c
->win
, wmh
);
1707 /* show clients top down */
1708 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1709 if ((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1710 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, 0);
1713 /* hide clients bottom up */
1715 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1722 if (signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1723 die("can't install SIGCHLD handler:");
1724 while (0 < waitpid(-1, NULL
, WNOHANG
));
1728 spawn(const Arg
*arg
)
1730 if (arg
->v
== dmenucmd
)
1731 dmenumon
[0] = '0' + selmon
->num
;
1734 close(ConnectionNumber(dpy
));
1736 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1737 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1746 if (selmon
->sel
&& arg
->ui
& TAGMASK
) {
1747 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1754 tagmon(const Arg
*arg
)
1756 if (!selmon
->sel
|| !mons
->next
)
1758 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1764 unsigned int i
, n
, h
, mw
, my
, ty
;
1767 for (n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1772 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1775 for (i
= my
= ty
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1776 if (i
< m
->nmaster
) {
1777 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
);
1778 resize(c
, m
->wx
, m
->wy
+ my
, mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1779 if (my
+ HEIGHT(c
) < m
->wh
)
1782 h
= (m
->wh
- ty
) / (n
- i
);
1783 resize(c
, m
->wx
+ mw
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1784 if (ty
+ HEIGHT(c
) < m
->wh
)
1790 togglebar(const Arg
*arg
)
1792 selmon
->showbar
= !selmon
->showbar
;
1793 updatebarpos(selmon
);
1794 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1799 togglefloating(const Arg
*arg
)
1803 if (selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1805 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1806 if (selmon
->sel
->isfloating
)
1807 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1808 selmon
->sel
->w
, selmon
->sel
->h
, 0);
1813 toggletag(const Arg
*arg
)
1815 unsigned int newtags
;
1819 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1821 selmon
->sel
->tags
= newtags
;
1828 toggleview(const Arg
*arg
)
1830 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1833 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1840 unfocus(Client
*c
, int setfocus
)
1845 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
][ColBorder
].pixel
);
1847 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1848 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1853 unmanage(Client
*c
, int destroyed
)
1855 Monitor
*m
= c
->mon
;
1858 if (c
->swallowing
) {
1863 Client
*s
= swallowingclient(c
->win
);
1865 free(s
->swallowing
);
1866 s
->swallowing
= NULL
;
1875 wc
.border_width
= c
->oldbw
;
1876 XGrabServer(dpy
); /* avoid race conditions */
1877 XSetErrorHandler(xerrordummy
);
1878 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1879 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1880 setclientstate(c
, WithdrawnState
);
1882 XSetErrorHandler(xerror
);
1895 unmapnotify(XEvent
*e
)
1898 XUnmapEvent
*ev
= &e
->xunmap
;
1900 if ((c
= wintoclient(ev
->window
))) {
1902 setclientstate(c
, WithdrawnState
);
1912 XSetWindowAttributes wa
= {
1913 .override_redirect
= True
,
1914 .background_pixel
= 0,
1917 .event_mask
= ButtonPressMask
|ExposureMask
1919 XClassHint ch
= {"dwm", "dwm"};
1920 for (m
= mons
; m
; m
= m
->next
) {
1923 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, depth
,
1924 InputOutput
, visual
,
1925 CWOverrideRedirect
|CWBackPixel
|CWBorderPixel
|CWColormap
|CWEventMask
, &wa
);
1926 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
1927 XMapRaised(dpy
, m
->barwin
);
1928 XSetClassHint(dpy
, m
->barwin
, &ch
);
1933 updatebarpos(Monitor
*m
)
1939 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1940 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1951 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1952 for (m
= mons
; m
; m
= m
->next
)
1953 for (c
= m
->clients
; c
; c
= c
->next
)
1954 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
1955 XA_WINDOW
, 32, PropModeAppend
,
1956 (unsigned char *) &(c
->win
), 1);
1965 if (XineramaIsActive(dpy
)) {
1969 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1970 XineramaScreenInfo
*unique
= NULL
;
1972 for (n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1973 /* only consider unique geometries as separate screens */
1974 unique
= ecalloc(nn
, sizeof(XineramaScreenInfo
));
1975 for (i
= 0, j
= 0; i
< nn
; i
++)
1976 if (isuniquegeom(unique
, j
, &info
[i
]))
1977 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1980 if (n
<= nn
) { /* new monitors available */
1981 for (i
= 0; i
< (nn
- n
); i
++) {
1982 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1984 m
->next
= createmon();
1988 for (i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1990 || unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1991 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
)
1995 m
->mx
= m
->wx
= unique
[i
].x_org
;
1996 m
->my
= m
->wy
= unique
[i
].y_org
;
1997 m
->mw
= m
->ww
= unique
[i
].width
;
1998 m
->mh
= m
->wh
= unique
[i
].height
;
2001 } else { /* less monitors available nn < n */
2002 for (i
= nn
; i
< n
; i
++) {
2003 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
2004 while ((c
= m
->clients
)) {
2006 m
->clients
= c
->next
;
2019 #endif /* XINERAMA */
2020 { /* default monitor setup */
2023 if (mons
->mw
!= sw
|| mons
->mh
!= sh
) {
2025 mons
->mw
= mons
->ww
= sw
;
2026 mons
->mh
= mons
->wh
= sh
;
2032 selmon
= wintomon(root
);
2038 updatenumlockmask(void)
2041 XModifierKeymap
*modmap
;
2044 modmap
= XGetModifierMapping(dpy
);
2045 for (i
= 0; i
< 8; i
++)
2046 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
2047 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
2048 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
2049 numlockmask
= (1 << i
);
2050 XFreeModifiermap(modmap
);
2054 updatesizehints(Client
*c
)
2059 if (!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
2060 /* size is uninitialized, ensure that size.flags aren't used */
2062 if (size
.flags
& PBaseSize
) {
2063 c
->basew
= size
.base_width
;
2064 c
->baseh
= size
.base_height
;
2065 } else if (size
.flags
& PMinSize
) {
2066 c
->basew
= size
.min_width
;
2067 c
->baseh
= size
.min_height
;
2069 c
->basew
= c
->baseh
= 0;
2070 if (size
.flags
& PResizeInc
) {
2071 c
->incw
= size
.width_inc
;
2072 c
->inch
= size
.height_inc
;
2074 c
->incw
= c
->inch
= 0;
2075 if (size
.flags
& PMaxSize
) {
2076 c
->maxw
= size
.max_width
;
2077 c
->maxh
= size
.max_height
;
2079 c
->maxw
= c
->maxh
= 0;
2080 if (size
.flags
& PMinSize
) {
2081 c
->minw
= size
.min_width
;
2082 c
->minh
= size
.min_height
;
2083 } else if (size
.flags
& PBaseSize
) {
2084 c
->minw
= size
.base_width
;
2085 c
->minh
= size
.base_height
;
2087 c
->minw
= c
->minh
= 0;
2088 if (size
.flags
& PAspect
) {
2089 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
2090 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
2092 c
->maxa
= c
->mina
= 0.0;
2093 c
->isfixed
= (c
->maxw
&& c
->maxh
&& c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
2099 if (!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
2100 strcpy(stext
, "dwm-"VERSION
);
2105 updatetitle(Client
*c
)
2107 if (!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
2108 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
2109 if (c
->name
[0] == '\0') /* hack to mark broken clients */
2110 strcpy(c
->name
, broken
);
2114 updatewindowtype(Client
*c
)
2116 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
2117 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
2119 if (state
== netatom
[NetWMFullscreen
])
2120 setfullscreen(c
, 1);
2121 if (wtype
== netatom
[NetWMWindowTypeDialog
])
2126 updatewmhints(Client
*c
)
2130 if ((wmh
= XGetWMHints(dpy
, c
->win
))) {
2131 if (c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2132 wmh
->flags
&= ~XUrgencyHint
;
2133 XSetWMHints(dpy
, c
->win
, wmh
);
2135 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? 1 : 0;
2136 if (wmh
->flags
& InputHint
)
2137 c
->neverfocus
= !wmh
->input
;
2145 view(const Arg
*arg
)
2147 if ((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2149 selmon
->seltags
^= 1; /* toggle sel tagset */
2150 if (arg
->ui
& TAGMASK
)
2151 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2163 xcb_res_client_id_spec_t spec
= {0};
2165 spec
.mask
= XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID
;
2167 xcb_generic_error_t
*e
= NULL
;
2168 xcb_res_query_client_ids_cookie_t c
= xcb_res_query_client_ids(xcon
, 1, &spec
);
2169 xcb_res_query_client_ids_reply_t
*r
= xcb_res_query_client_ids_reply(xcon
, c
, &e
);
2174 xcb_res_client_id_value_iterator_t i
= xcb_res_query_client_ids_ids_iterator(r
);
2175 for (; i
.rem
; xcb_res_client_id_value_next(&i
)) {
2176 spec
= i
.data
->spec
;
2177 if (spec
.mask
& XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID
) {
2178 uint32_t *t
= xcb_res_client_id_value_value(i
.data
);
2186 if (result
== (pid_t
)-1)
2189 #endif /* __linux__ */
2194 unsigned long len
, bytes
;
2195 unsigned char *prop
;
2198 if (XGetWindowProperty(dpy
, w
, XInternAtom(dpy
, "_NET_WM_PID", 0), 0, 1, False
, AnyPropertyType
, &type
, &format
, &len
, &bytes
, &prop
) != Success
|| !prop
)
2201 ret
= *(pid_t
*)prop
;
2205 #endif /* __OpenBSD__ */
2210 getparentprocess(pid_t p
)
2217 snprintf(buf
, sizeof(buf
) - 1, "/proc/%u/stat", (unsigned)p
);
2219 if (!(f
= fopen(buf
, "r")))
2222 fscanf(f
, "%*u %*s %*c %u", &v
);
2224 #endif /* __linux__*/
2229 struct kinfo_proc
*kp
;
2231 kd
= kvm_openfiles(NULL
, NULL
, NULL
, KVM_NO_FILES
, NULL
);
2235 kp
= kvm_getprocs(kd
, KERN_PROC_PID
, p
, sizeof(*kp
), &n
);
2237 #endif /* __OpenBSD__ */
2243 isdescprocess(pid_t p
, pid_t c
)
2245 while (p
!= c
&& c
!= 0)
2246 c
= getparentprocess(c
);
2252 termforwin(const Client
*w
)
2257 if (!w
->pid
|| w
->isterminal
)
2260 for (m
= mons
; m
; m
= m
->next
) {
2261 for (c
= m
->clients
; c
; c
= c
->next
) {
2262 if (c
->isterminal
&& !c
->swallowing
&& c
->pid
&& isdescprocess(c
->pid
, w
->pid
))
2271 swallowingclient(Window w
)
2276 for (m
= mons
; m
; m
= m
->next
) {
2277 for (c
= m
->clients
; c
; c
= c
->next
) {
2278 if (c
->swallowing
&& c
->swallowing
->win
== w
)
2287 wintoclient(Window w
)
2292 for (m
= mons
; m
; m
= m
->next
)
2293 for (c
= m
->clients
; c
; c
= c
->next
)
2306 if (w
== root
&& getrootptr(&x
, &y
))
2307 return recttomon(x
, y
, 1, 1);
2308 for (m
= mons
; m
; m
= m
->next
)
2311 if ((c
= wintoclient(w
)))
2316 /* There's no way to check accesses to destroyed windows, thus those cases are
2317 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2318 * default error handler, which may call exit. */
2320 xerror(Display
*dpy
, XErrorEvent
*ee
)
2322 if (ee
->error_code
== BadWindow
2323 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2324 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2325 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2326 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2327 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2328 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2329 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2330 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2332 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2333 ee
->request_code
, ee
->error_code
);
2334 return xerrorxlib(dpy
, ee
); /* may call exit */
2338 xerrordummy(Display
*dpy
, XErrorEvent
*ee
)
2343 /* Startup Error handler to check if another window manager
2344 * is already running. */
2346 xerrorstart(Display
*dpy
, XErrorEvent
*ee
)
2348 die("dwm: another window manager is already running");
2356 XRenderPictFormat
*fmt
;
2365 long masks
= VisualScreenMask
| VisualDepthMask
| VisualClassMask
;
2367 infos
= XGetVisualInfo(dpy
, masks
, &tpl
, &nitems
);
2369 for(i
= 0; i
< nitems
; i
++) {
2370 fmt
= XRenderFindVisualFormat(dpy
, infos
[i
].visual
);
2371 if (fmt
->type
== PictTypeDirect
&& fmt
->direct
.alphaMask
) {
2372 visual
= infos
[i
].visual
;
2373 depth
= infos
[i
].depth
;
2374 cmap
= XCreateColormap(dpy
, root
, visual
, AllocNone
);
2383 visual
= DefaultVisual(dpy
, screen
);
2384 depth
= DefaultDepth(dpy
, screen
);
2385 cmap
= DefaultColormap(dpy
, screen
);
2390 zoom(const Arg
*arg
)
2392 Client
*c
= selmon
->sel
;
2394 if (!selmon
->lt
[selmon
->sellt
]->arrange
2395 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2397 if (c
== nexttiled(selmon
->clients
))
2398 if (!c
|| !(c
= nexttiled(c
->next
)))
2404 main(int argc
, char *argv
[])
2406 if (argc
== 2 && !strcmp("-v", argv
[1]))
2409 die("usage: dwm [-v]");
2410 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2411 fputs("warning: no locale support\n", stderr
);
2412 if (!(dpy
= XOpenDisplay(NULL
)))
2413 die("dwm: cannot open display");
2414 if (!(xcon
= XGetXCBConnection(dpy
)))
2415 die("dwm: cannot get xcb connection\n");
2419 if (pledge("stdio rpath proc exec ps", NULL
) == -1)
2421 #endif /* __OpenBSD__ */
2426 return EXIT_SUCCESS
;