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().
23 #include <X11/Xatom.h>
25 #include <X11/Xproto.h>
26 #include <X11/Xutil.h>
27 #include <X11/cursorfont.h>
28 #include <X11/keysym.h>
36 #include <sys/types.h>
40 #include <X11/extensions/Xinerama.h>
42 #include <X11/Xft/Xft.h>
47 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
48 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
49 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
50 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
51 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
52 #define LENGTH(X) (sizeof X / sizeof X[0])
53 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
54 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
55 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
56 #define TAGMASK ((1 << LENGTH(tags)) - 1)
57 #define TEXTW(X) (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
62 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
63 enum { SchemeNorm
, SchemeSel
, SchemeLast
}; /* color schemes */
64 enum { NetSupported
, NetWMName
, NetWMState
,
65 NetWMFullscreen
, NetActiveWindow
, NetWMWindowType
,
66 NetWMWindowTypeDialog
, NetClientList
, NetLast
}; /* EWMH atoms */
67 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, WMLast
}; /* default atoms */
68 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
69 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
82 void (*func
)(const Arg
*arg
);
86 typedef struct Monitor Monitor
;
87 typedef struct Client Client
;
92 int oldx
, oldy
, oldw
, oldh
;
93 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
96 int isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
;
106 void (*func
)(const Arg
*);
112 void (*arrange
)(Monitor
*);
120 int by
; /* bar geometry */
121 int mx
, my
, mw
, mh
; /* screen size */
122 int wx
, wy
, ww
, wh
; /* window area */
123 unsigned int seltags
;
125 unsigned int tagset
[2];
138 const char *instance
;
145 /* function declarations */
146 static void applyrules(Client
*c
);
147 static int applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
);
148 static void arrange(Monitor
*m
);
149 static void arrangemon(Monitor
*m
);
150 static void attach(Client
*c
);
151 static void attachstack(Client
*c
);
152 static void buttonpress(XEvent
*e
);
153 static void checkotherwm(void);
154 static void cleanup(void);
155 static void cleanupmon(Monitor
*mon
);
156 static void clearurgent(Client
*c
);
157 static void clientmessage(XEvent
*e
);
158 static void configure(Client
*c
);
159 static void configurenotify(XEvent
*e
);
160 static void configurerequest(XEvent
*e
);
161 static Monitor
*createmon(void);
162 static void destroynotify(XEvent
*e
);
163 static void detach(Client
*c
);
164 static void detachstack(Client
*c
);
165 static Monitor
*dirtomon(int dir
);
166 static void drawbar(Monitor
*m
);
167 static void drawbars(void);
168 static void enternotify(XEvent
*e
);
169 static void expose(XEvent
*e
);
170 static void focus(Client
*c
);
171 static void focusin(XEvent
*e
);
172 static void focusmon(const Arg
*arg
);
173 static void focusstack(const Arg
*arg
);
174 static int getrootptr(int *x
, int *y
);
175 static long getstate(Window w
);
176 static int gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
177 static void grabbuttons(Client
*c
, int focused
);
178 static void grabkeys(void);
179 static void incnmaster(const Arg
*arg
);
180 static void keypress(XEvent
*e
);
181 static void killclient(const Arg
*arg
);
182 static void manage(Window w
, XWindowAttributes
*wa
);
183 static void mappingnotify(XEvent
*e
);
184 static void maprequest(XEvent
*e
);
185 static void monocle(Monitor
*m
);
186 static void motionnotify(XEvent
*e
);
187 static void movemouse(const Arg
*arg
);
188 static Client
*nexttiled(Client
*c
);
189 static void pop(Client
*);
190 static void propertynotify(XEvent
*e
);
191 static void quit(const Arg
*arg
);
192 static Monitor
*recttomon(int x
, int y
, int w
, int h
);
193 static void resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
);
194 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
195 static void resizemouse(const Arg
*arg
);
196 static void restack(Monitor
*m
);
197 static void run(void);
198 static void scan(void);
199 static int sendevent(Client
*c
, Atom proto
);
200 static void sendmon(Client
*c
, Monitor
*m
);
201 static void setclientstate(Client
*c
, long state
);
202 static void setfocus(Client
*c
);
203 static void setfullscreen(Client
*c
, int fullscreen
);
204 static void setlayout(const Arg
*arg
);
205 static void setmfact(const Arg
*arg
);
206 static void setup(void);
207 static void showhide(Client
*c
);
208 static void sigchld(int unused
);
209 static void spawn(const Arg
*arg
);
210 static void tag(const Arg
*arg
);
211 static void tagmon(const Arg
*arg
);
212 static void tile(Monitor
*);
213 static void togglebar(const Arg
*arg
);
214 static void togglefloating(const Arg
*arg
);
215 static void toggletag(const Arg
*arg
);
216 static void toggleview(const Arg
*arg
);
217 static void unfocus(Client
*c
, int setfocus
);
218 static void unmanage(Client
*c
, int destroyed
);
219 static void unmapnotify(XEvent
*e
);
220 static int updategeom(void);
221 static void updatebarpos(Monitor
*m
);
222 static void updatebars(void);
223 static void updateclientlist(void);
224 static void updatenumlockmask(void);
225 static void updatesizehints(Client
*c
);
226 static void updatestatus(void);
227 static void updatewindowtype(Client
*c
);
228 static void updatetitle(Client
*c
);
229 static void updatewmhints(Client
*c
);
230 static void view(const Arg
*arg
);
231 static Client
*wintoclient(Window w
);
232 static Monitor
*wintomon(Window w
);
233 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
234 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
235 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
236 static void xinitvisual();
237 static void zoom(const Arg
*arg
);
240 static const char broken
[] = "broken";
241 static char stext
[256];
243 static int sw
, sh
; /* X display screen geometry width, height */
244 static int bh
, blw
= 0; /* bar geometry */
245 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
246 static unsigned int numlockmask
= 0;
247 static void (*handler
[LASTEvent
]) (XEvent
*) = {
248 [ButtonPress
] = buttonpress
,
249 [ClientMessage
] = clientmessage
,
250 [ConfigureRequest
] = configurerequest
,
251 [ConfigureNotify
] = configurenotify
,
252 [DestroyNotify
] = destroynotify
,
253 [EnterNotify
] = enternotify
,
256 [KeyPress
] = keypress
,
257 [MappingNotify
] = mappingnotify
,
258 [MapRequest
] = maprequest
,
259 [MotionNotify
] = motionnotify
,
260 [PropertyNotify
] = propertynotify
,
261 [UnmapNotify
] = unmapnotify
263 static Atom wmatom
[WMLast
], netatom
[NetLast
];
264 static int running
= 1;
265 static Cur
*cursor
[CurLast
];
266 static ClrScheme scheme
[SchemeLast
];
269 static Monitor
*mons
, *selmon
;
272 static int useargb
= 0;
273 static Visual
*visual
;
275 static Colormap cmap
;
277 /* configuration, allows nested code to access above variables */
280 /* compile-time check if all tags fit into an unsigned int bit array. */
281 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
283 /* function implementations */
285 applyrules(Client
*c
)
287 const char *class, *instance
;
291 XClassHint ch
= { NULL
, NULL
};
296 XGetClassHint(dpy
, c
->win
, &ch
);
297 class = ch
.res_class
? ch
.res_class
: broken
;
298 instance
= ch
.res_name
? ch
.res_name
: broken
;
300 for (i
= 0; i
< LENGTH(rules
); i
++) {
302 if ((!r
->title
|| strstr(c
->name
, r
->title
))
303 && (!r
->class || strstr(class, r
->class))
304 && (!r
->instance
|| strstr(instance
, r
->instance
)))
306 c
->isfloating
= r
->isfloating
;
308 for (m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
317 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
321 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
)
326 /* set minimum possible */
334 if (*x
+ *w
+ 2 * c
->bw
< 0)
336 if (*y
+ *h
+ 2 * c
->bw
< 0)
339 if (*x
>= m
->wx
+ m
->ww
)
340 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
341 if (*y
>= m
->wy
+ m
->wh
)
342 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
343 if (*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
345 if (*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
352 if (resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
353 /* see last two sentences in ICCCM 4.1.2.3 */
354 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
355 if (!baseismin
) { /* temporarily remove base dimensions */
359 /* adjust for aspect limits */
360 if (c
->mina
> 0 && c
->maxa
> 0) {
361 if (c
->maxa
< (float)*w
/ *h
)
362 *w
= *h
* c
->maxa
+ 0.5;
363 else if (c
->mina
< (float)*h
/ *w
)
364 *h
= *w
* c
->mina
+ 0.5;
366 if (baseismin
) { /* increment calculation requires this */
370 /* adjust for increment value */
375 /* restore base dimensions */
376 *w
= MAX(*w
+ c
->basew
, c
->minw
);
377 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
379 *w
= MIN(*w
, c
->maxw
);
381 *h
= MIN(*h
, c
->maxh
);
383 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
391 else for (m
= mons
; m
; m
= m
->next
)
396 } else for (m
= mons
; m
; m
= m
->next
)
401 arrangemon(Monitor
*m
)
403 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
404 if (m
->lt
[m
->sellt
]->arrange
)
405 m
->lt
[m
->sellt
]->arrange(m
);
411 c
->next
= c
->mon
->clients
;
416 attachstack(Client
*c
)
418 c
->snext
= c
->mon
->stack
;
423 buttonpress(XEvent
*e
)
425 unsigned int i
, x
, click
;
429 XButtonPressedEvent
*ev
= &e
->xbutton
;
432 /* focus monitor if necessary */
433 if ((m
= wintomon(ev
->window
)) && m
!= selmon
) {
434 unfocus(selmon
->sel
, 1);
438 if (ev
->window
== selmon
->barwin
) {
440 unsigned int occ
= 0;
441 for(c
= m
->clients
; c
; c
= c
->next
)
444 /* do not reserve space for vacant tags */
445 if(!(occ
& 1 << i
|| m
->tagset
[m
->seltags
] & 1 << i
))
448 } while (ev
->x
>= x
&& ++i
< LENGTH(tags
));
449 if (i
< LENGTH(tags
)) {
452 } else if (ev
->x
< x
+ blw
)
454 else if (ev
->x
> selmon
->ww
- TEXTW(stext
))
455 click
= ClkStatusText
;
458 } else if ((c
= wintoclient(ev
->window
))) {
460 click
= ClkClientWin
;
462 for (i
= 0; i
< LENGTH(buttons
); i
++)
463 if (click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
464 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
465 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
471 xerrorxlib
= XSetErrorHandler(xerrorstart
);
472 /* this causes an error if some other window manager is running */
473 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
475 XSetErrorHandler(xerror
);
483 Layout foo
= { "", NULL
};
488 selmon
->lt
[selmon
->sellt
] = &foo
;
489 for (m
= mons
; m
; m
= m
->next
)
491 unmanage(m
->stack
, 0);
492 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
495 for (i
= 0; i
< CurLast
; i
++)
496 drw_cur_free(drw
, cursor
[i
]);
497 for (i
= 0; i
< SchemeLast
; i
++) {
498 drw_clr_free(scheme
[i
].border
);
499 drw_clr_free(scheme
[i
].bg
);
500 drw_clr_free(scheme
[i
].fg
);
504 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
505 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
509 cleanupmon(Monitor
*mon
)
516 for (m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
519 XUnmapWindow(dpy
, mon
->barwin
);
520 XDestroyWindow(dpy
, mon
->barwin
);
525 clearurgent(Client
*c
)
530 if (!(wmh
= XGetWMHints(dpy
, c
->win
)))
532 wmh
->flags
&= ~XUrgencyHint
;
533 XSetWMHints(dpy
, c
->win
, wmh
);
538 clientmessage(XEvent
*e
)
540 XClientMessageEvent
*cme
= &e
->xclient
;
541 Client
*c
= wintoclient(cme
->window
);
545 if (cme
->message_type
== netatom
[NetWMState
]) {
546 if (cme
->data
.l
[1] == netatom
[NetWMFullscreen
] || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
547 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
548 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
549 } else if (cme
->message_type
== netatom
[NetActiveWindow
]) {
551 c
->mon
->seltags
^= 1;
552 c
->mon
->tagset
[c
->mon
->seltags
] = c
->tags
;
563 ce
.type
= ConfigureNotify
;
571 ce
.border_width
= c
->bw
;
573 ce
.override_redirect
= False
;
574 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
578 configurenotify(XEvent
*e
)
581 XConfigureEvent
*ev
= &e
->xconfigure
;
584 /* TODO: updategeom handling sucks, needs to be simplified */
585 if (ev
->window
== root
) {
586 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
589 if (updategeom() || dirty
) {
590 drw_resize(drw
, sw
, bh
);
592 for (m
= mons
; m
; m
= m
->next
)
593 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
601 configurerequest(XEvent
*e
)
605 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
608 if ((c
= wintoclient(ev
->window
))) {
609 if (ev
->value_mask
& CWBorderWidth
)
610 c
->bw
= ev
->border_width
;
611 else if (c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
613 if (ev
->value_mask
& CWX
) {
615 c
->x
= m
->mx
+ ev
->x
;
617 if (ev
->value_mask
& CWY
) {
619 c
->y
= m
->my
+ ev
->y
;
621 if (ev
->value_mask
& CWWidth
) {
625 if (ev
->value_mask
& CWHeight
) {
629 if ((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
630 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
631 if ((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
632 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
633 if ((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
636 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
642 wc
.width
= ev
->width
;
643 wc
.height
= ev
->height
;
644 wc
.border_width
= ev
->border_width
;
645 wc
.sibling
= ev
->above
;
646 wc
.stack_mode
= ev
->detail
;
647 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
657 m
= ecalloc(1, sizeof(Monitor
));
658 m
->tagset
[0] = m
->tagset
[1] = 1;
660 m
->nmaster
= nmaster
;
661 m
->showbar
= showbar
;
663 m
->lt
[0] = &layouts
[0];
664 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
665 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
670 destroynotify(XEvent
*e
)
673 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
675 if ((c
= wintoclient(ev
->window
)))
684 for (tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
689 detachstack(Client
*c
)
693 for (tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
696 if (c
== c
->mon
->sel
) {
697 for (t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
708 if (!(m
= selmon
->next
))
710 } else if (selmon
== mons
)
711 for (m
= mons
; m
->next
; m
= m
->next
);
713 for (m
= mons
; m
->next
!= selmon
; m
= m
->next
);
721 unsigned int i
, occ
= 0, urg
= 0;
724 dx
= (drw
->fonts
[0]->ascent
+ drw
->fonts
[0]->descent
+ 2) / 4;
726 for (c
= m
->clients
; c
; c
= c
->next
) {
732 for (i
= 0; i
< LENGTH(tags
); i
++) {
733 /* do not draw vacant tags */
734 if(!(occ
& 1 << i
|| m
->tagset
[m
->seltags
] & 1 << i
))
737 drw_setscheme(drw
, m
->tagset
[m
->seltags
] & 1 << i
? &scheme
[SchemeSel
] : &scheme
[SchemeNorm
]);
738 drw_text(drw
, x
, 0, w
, bh
, tags
[i
], urg
& 1 << i
);
739 drw_rect(drw
, x
+ 1, 1, dx
, dx
, m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
743 w
= blw
= TEXTW(m
->ltsymbol
);
744 drw_setscheme(drw
, &scheme
[SchemeNorm
]);
745 drw_text(drw
, x
, 0, w
, bh
, m
->ltsymbol
, 0);
748 if (m
== selmon
) { /* status is only drawn on selected monitor */
755 drw_text(drw
, x
, 0, w
, bh
, stext
, 0);
758 if ((w
= x
- xx
) > bh
) {
761 drw_setscheme(drw
, m
== selmon
? &scheme
[SchemeSel
] : &scheme
[SchemeNorm
]);
762 drw_text(drw
, x
, 0, w
, bh
, m
->sel
->name
, 0);
763 drw_rect(drw
, x
+ 1, 1, dx
, dx
, m
->sel
->isfixed
, m
->sel
->isfloating
, 0);
765 drw_setscheme(drw
, &scheme
[SchemeNorm
]);
766 drw_rect(drw
, x
, 0, w
, bh
, 1, 0, 1);
769 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
);
777 for (m
= mons
; m
; m
= m
->next
)
782 enternotify(XEvent
*e
)
786 XCrossingEvent
*ev
= &e
->xcrossing
;
788 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
790 c
= wintoclient(ev
->window
);
791 m
= c
? c
->mon
: wintomon(ev
->window
);
793 unfocus(selmon
->sel
, 1);
795 } else if (!c
|| c
== selmon
->sel
)
804 XExposeEvent
*ev
= &e
->xexpose
;
806 if (ev
->count
== 0 && (m
= wintomon(ev
->window
)))
813 if (!c
|| !ISVISIBLE(c
))
814 for (c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
815 /* was if (selmon->sel) */
816 if (selmon
->sel
&& selmon
->sel
!= c
)
817 unfocus(selmon
->sel
, 0);
819 if (c
->mon
!= selmon
)
826 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
].border
->pix
);
829 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
830 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
836 /* there are some broken focus acquiring clients */
840 XFocusChangeEvent
*ev
= &e
->xfocus
;
842 if (selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
843 setfocus(selmon
->sel
);
847 focusmon(const Arg
*arg
)
853 if ((m
= dirtomon(arg
->i
)) == selmon
)
855 unfocus(selmon
->sel
, 0); /* s/1/0/ fixes input focus issues
856 in gedit and anjuta */
862 focusstack(const Arg
*arg
)
864 Client
*c
= NULL
, *i
;
869 for (c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
871 for (c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
873 for (i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
877 for (; i
; i
= i
->next
)
888 getatomprop(Client
*c
, Atom prop
)
892 unsigned char *p
= NULL
;
893 Atom da
, atom
= None
;
895 if (XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
896 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
904 getrootptr(int *x
, int *y
)
910 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
918 unsigned char *p
= NULL
;
919 unsigned long n
, extra
;
922 if (XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
923 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
932 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
938 if (!text
|| size
== 0)
941 XGetTextProperty(dpy
, w
, &name
, atom
);
944 if (name
.encoding
== XA_STRING
)
945 strncpy(text
, (char *)name
.value
, size
- 1);
947 if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
948 strncpy(text
, *list
, size
- 1);
949 XFreeStringList(list
);
952 text
[size
- 1] = '\0';
958 grabbuttons(Client
*c
, int focused
)
963 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
964 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
966 for (i
= 0; i
< LENGTH(buttons
); i
++)
967 if (buttons
[i
].click
== ClkClientWin
)
968 for (j
= 0; j
< LENGTH(modifiers
); j
++)
969 XGrabButton(dpy
, buttons
[i
].button
,
970 buttons
[i
].mask
| modifiers
[j
],
971 c
->win
, False
, BUTTONMASK
,
972 GrabModeAsync
, GrabModeSync
, None
, None
);
974 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
975 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
985 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
988 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
989 for (i
= 0; i
< LENGTH(keys
); i
++)
990 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
991 for (j
= 0; j
< LENGTH(modifiers
); j
++)
992 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
993 True
, GrabModeAsync
, GrabModeAsync
);
998 incnmaster(const Arg
*arg
)
1000 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
1006 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
)
1009 if (unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
1010 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
1014 #endif /* XINERAMA */
1024 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1025 for (i
= 0; i
< LENGTH(keys
); i
++)
1026 if (keysym
== keys
[i
].keysym
1027 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1029 keys
[i
].func(&(keys
[i
].arg
));
1033 killclient(const Arg
*arg
)
1037 if (!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1039 XSetErrorHandler(xerrordummy
);
1040 XSetCloseDownMode(dpy
, DestroyAll
);
1041 XKillClient(dpy
, selmon
->sel
->win
);
1043 XSetErrorHandler(xerror
);
1049 manage(Window w
, XWindowAttributes
*wa
)
1051 Client
*c
, *t
= NULL
;
1052 Window trans
= None
;
1055 c
= ecalloc(1, sizeof(Client
));
1058 if (XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1066 c
->x
= c
->oldx
= wa
->x
;
1067 c
->y
= c
->oldy
= wa
->y
;
1068 c
->w
= c
->oldw
= wa
->width
;
1069 c
->h
= c
->oldh
= wa
->height
;
1070 c
->oldbw
= wa
->border_width
;
1072 if (c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1073 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1074 if (c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1075 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1076 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1077 /* only fix client y-offset, if the client center might cover the bar */
1078 c
->y
= MAX(c
->y
, ((c
->mon
->by
== c
->mon
->my
) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1079 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1082 wc
.border_width
= c
->bw
;
1083 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1084 XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
].border
->pix
);
1085 configure(c
); /* propagates border_width, if size doesn't change */
1086 updatewindowtype(c
);
1089 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1092 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1094 XRaiseWindow(dpy
, c
->win
);
1097 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1098 (unsigned char *) &(c
->win
), 1);
1099 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1100 setclientstate(c
, NormalState
);
1101 if (c
->mon
== selmon
)
1102 unfocus(selmon
->sel
, 0);
1105 XMapWindow(dpy
, c
->win
);
1110 mappingnotify(XEvent
*e
)
1112 XMappingEvent
*ev
= &e
->xmapping
;
1114 XRefreshKeyboardMapping(ev
);
1115 if (ev
->request
== MappingKeyboard
)
1120 maprequest(XEvent
*e
)
1122 static XWindowAttributes wa
;
1123 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1125 if (!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1127 if (wa
.override_redirect
)
1129 if (!wintoclient(ev
->window
))
1130 manage(ev
->window
, &wa
);
1139 for (c
= m
->clients
; c
; c
= c
->next
)
1142 if (n
> 0) /* override layout symbol */
1143 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1144 for (c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1145 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, 0);
1149 motionnotify(XEvent
*e
)
1151 static Monitor
*mon
= NULL
;
1153 XMotionEvent
*ev
= &e
->xmotion
;
1155 if (ev
->window
!= root
)
1157 if ((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1158 unfocus(selmon
->sel
, 1);
1166 movemouse(const Arg
*arg
)
1168 int x
, y
, ocx
, ocy
, nx
, ny
;
1174 if (!(c
= selmon
->sel
))
1176 if (c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1181 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1182 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1184 if (!getrootptr(&x
, &y
))
1187 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1189 case ConfigureRequest
:
1192 handler
[ev
.type
](&ev
);
1195 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1197 lasttime
= ev
.xmotion
.time
;
1199 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1200 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1201 if (nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1202 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1203 if (abs(selmon
->wx
- nx
) < snap
)
1205 else if (abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1206 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1207 if (abs(selmon
->wy
- ny
) < snap
)
1209 else if (abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1210 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1211 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1212 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1213 togglefloating(NULL
);
1215 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1216 resize(c
, nx
, ny
, c
->w
, c
->h
, 1);
1219 } while (ev
.type
!= ButtonRelease
);
1220 XUngrabPointer(dpy
, CurrentTime
);
1221 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1229 nexttiled(Client
*c
)
1231 for (; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1245 propertynotify(XEvent
*e
)
1249 XPropertyEvent
*ev
= &e
->xproperty
;
1251 if ((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1253 else if (ev
->state
== PropertyDelete
)
1254 return; /* ignore */
1255 else if ((c
= wintoclient(ev
->window
))) {
1258 case XA_WM_TRANSIENT_FOR
:
1259 if (!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1260 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1263 case XA_WM_NORMAL_HINTS
:
1271 if (ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1273 if (c
== c
->mon
->sel
)
1276 if (ev
->atom
== netatom
[NetWMWindowType
])
1277 updatewindowtype(c
);
1282 quit(const Arg
*arg
)
1288 recttomon(int x
, int y
, int w
, int h
)
1290 Monitor
*m
, *r
= selmon
;
1293 for (m
= mons
; m
; m
= m
->next
)
1294 if ((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1302 resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
)
1304 if (applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1305 resizeclient(c
, x
, y
, w
, h
);
1309 resizeclient(Client
*c
, int x
, int y
, int w
, int h
)
1313 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1314 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1315 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1316 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1317 wc
.border_width
= c
->bw
;
1318 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1324 resizemouse(const Arg
*arg
)
1326 int ocx
, ocy
, nw
, nh
;
1332 if (!(c
= selmon
->sel
))
1334 if (c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1339 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1340 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1342 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1344 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1346 case ConfigureRequest
:
1349 handler
[ev
.type
](&ev
);
1352 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1354 lasttime
= ev
.xmotion
.time
;
1356 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1357 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1358 if (c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1359 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1361 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1362 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1363 togglefloating(NULL
);
1365 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1366 resize(c
, c
->x
, c
->y
, nw
, nh
, 1);
1369 } while (ev
.type
!= ButtonRelease
);
1370 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1371 XUngrabPointer(dpy
, CurrentTime
);
1372 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1373 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1390 if (m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1391 XRaiseWindow(dpy
, m
->sel
->win
);
1392 if (m
->lt
[m
->sellt
]->arrange
) {
1393 wc
.stack_mode
= Below
;
1394 wc
.sibling
= m
->barwin
;
1395 for (c
= m
->stack
; c
; c
= c
->snext
)
1396 if (!c
->isfloating
&& ISVISIBLE(c
)) {
1397 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1398 wc
.sibling
= c
->win
;
1402 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1409 /* main event loop */
1411 while (running
&& !XNextEvent(dpy
, &ev
))
1412 if (handler
[ev
.type
])
1413 handler
[ev
.type
](&ev
); /* call handler */
1419 unsigned int i
, num
;
1420 Window d1
, d2
, *wins
= NULL
;
1421 XWindowAttributes wa
;
1423 if (XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1424 for (i
= 0; i
< num
; i
++) {
1425 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1426 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1428 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1429 manage(wins
[i
], &wa
);
1431 for (i
= 0; i
< num
; i
++) { /* now the transients */
1432 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1434 if (XGetTransientForHint(dpy
, wins
[i
], &d1
)
1435 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1436 manage(wins
[i
], &wa
);
1444 sendmon(Client
*c
, Monitor
*m
)
1452 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1460 setclientstate(Client
*c
, long state
)
1462 long data
[] = { state
, None
};
1464 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1465 PropModeReplace
, (unsigned char *)data
, 2);
1469 sendevent(Client
*c
, Atom proto
)
1476 if (XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1477 while (!exists
&& n
--)
1478 exists
= protocols
[n
] == proto
;
1482 ev
.type
= ClientMessage
;
1483 ev
.xclient
.window
= c
->win
;
1484 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1485 ev
.xclient
.format
= 32;
1486 ev
.xclient
.data
.l
[0] = proto
;
1487 ev
.xclient
.data
.l
[1] = CurrentTime
;
1488 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1496 if (!c
->neverfocus
) {
1497 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1498 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1499 XA_WINDOW
, 32, PropModeReplace
,
1500 (unsigned char *) &(c
->win
), 1);
1502 sendevent(c
, wmatom
[WMTakeFocus
]);
1506 setfullscreen(Client
*c
, int fullscreen
)
1508 if (fullscreen
&& !c
->isfullscreen
) {
1509 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1510 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1511 c
->isfullscreen
= 1;
1512 c
->oldstate
= c
->isfloating
;
1516 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1517 XRaiseWindow(dpy
, c
->win
);
1518 } else if (!fullscreen
&& c
->isfullscreen
){
1519 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1520 PropModeReplace
, (unsigned char*)0, 0);
1521 c
->isfullscreen
= 0;
1522 c
->isfloating
= c
->oldstate
;
1528 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1534 setlayout(const Arg
*arg
)
1536 if (!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1539 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1540 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1547 /* arg > 1.0 will set mfact absolutly */
1549 setmfact(const Arg
*arg
)
1553 if (!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1555 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1556 if (f
< 0.1 || f
> 0.9)
1565 XSetWindowAttributes wa
;
1567 /* clean up any zombies immediately */
1571 screen
= DefaultScreen(dpy
);
1572 sw
= DisplayWidth(dpy
, screen
);
1573 sh
= DisplayHeight(dpy
, screen
);
1574 root
= RootWindow(dpy
, screen
);
1576 drw
= drw_create(dpy
, screen
, root
, sw
, sh
, visual
, depth
, cmap
);
1577 drw_load_fonts(drw
, fonts
, LENGTH(fonts
));
1578 if (!drw
->fontcount
)
1579 die("no fonts could be loaded.\n");
1580 bh
= drw
->fonts
[0]->h
+ 2;
1583 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1584 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1585 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1586 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1587 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1588 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1589 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1590 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1591 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1592 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1593 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1594 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1596 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1597 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1598 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1599 /* init appearance */
1600 scheme
[SchemeNorm
].border
= drw_clr_create(drw
, normbordercolor
, borderalpha
);
1601 scheme
[SchemeNorm
].bg
= drw_clr_create(drw
, normbgcolor
, baralpha
);
1602 scheme
[SchemeNorm
].fg
= drw_clr_create(drw
, normfgcolor
, OPAQUE
);
1603 scheme
[SchemeSel
].border
= drw_clr_create(drw
, selbordercolor
, borderalpha
);
1604 scheme
[SchemeSel
].bg
= drw_clr_create(drw
, selbgcolor
, baralpha
);
1605 scheme
[SchemeSel
].fg
= drw_clr_create(drw
, selfgcolor
, OPAQUE
);
1609 /* EWMH support per view */
1610 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1611 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1612 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1613 /* select for events */
1614 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1615 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
|PointerMotionMask
1616 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1617 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1618 XSelectInput(dpy
, root
, wa
.event_mask
);
1629 /* show clients top down */
1630 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1631 if ((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1632 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, 0);
1635 /* hide clients bottom up */
1637 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1644 if (signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1645 die("can't install SIGCHLD handler:");
1646 while (0 < waitpid(-1, NULL
, WNOHANG
));
1650 spawn(const Arg
*arg
)
1652 if (arg
->v
== dmenucmd
)
1653 dmenumon
[0] = '0' + selmon
->num
;
1656 close(ConnectionNumber(dpy
));
1658 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1659 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1668 if (selmon
->sel
&& arg
->ui
& TAGMASK
) {
1669 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1676 tagmon(const Arg
*arg
)
1678 if (!selmon
->sel
|| !mons
->next
)
1680 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1686 unsigned int i
, n
, h
, mw
, my
, ty
;
1689 for (n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1694 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1697 for (i
= my
= ty
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1698 if (i
< m
->nmaster
) {
1699 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
);
1700 resize(c
, m
->wx
, m
->wy
+ my
, mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1703 h
= (m
->wh
- ty
) / (n
- i
);
1704 resize(c
, m
->wx
+ mw
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1710 togglebar(const Arg
*arg
)
1712 selmon
->showbar
= !selmon
->showbar
;
1713 updatebarpos(selmon
);
1714 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1719 togglefloating(const Arg
*arg
)
1723 if (selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1725 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1726 if (selmon
->sel
->isfloating
)
1727 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1728 selmon
->sel
->w
, selmon
->sel
->h
, 0);
1733 toggletag(const Arg
*arg
)
1735 unsigned int newtags
;
1739 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1741 selmon
->sel
->tags
= newtags
;
1748 toggleview(const Arg
*arg
)
1750 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1753 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1760 unfocus(Client
*c
, int setfocus
)
1765 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
].border
->pix
);
1767 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1768 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1773 unmanage(Client
*c
, int destroyed
)
1775 Monitor
*m
= c
->mon
;
1778 /* The server grab construct avoids race conditions. */
1782 wc
.border_width
= c
->oldbw
;
1784 XSetErrorHandler(xerrordummy
);
1785 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1786 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1787 setclientstate(c
, WithdrawnState
);
1789 XSetErrorHandler(xerror
);
1799 unmapnotify(XEvent
*e
)
1802 XUnmapEvent
*ev
= &e
->xunmap
;
1804 if ((c
= wintoclient(ev
->window
))) {
1806 setclientstate(c
, WithdrawnState
);
1816 XSetWindowAttributes wa
= {
1817 .override_redirect
= True
,
1818 .background_pixel
= 0,
1821 .event_mask
= ButtonPressMask
|ExposureMask
1823 for (m
= mons
; m
; m
= m
->next
) {
1826 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, depth
,
1827 InputOutput
, visual
,
1828 CWOverrideRedirect
|CWBackPixel
|CWBorderPixel
|CWColormap
|CWEventMask
, &wa
);
1829 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
1830 XMapRaised(dpy
, m
->barwin
);
1835 updatebarpos(Monitor
*m
)
1841 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1842 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1853 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1854 for (m
= mons
; m
; m
= m
->next
)
1855 for (c
= m
->clients
; c
; c
= c
->next
)
1856 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
1857 XA_WINDOW
, 32, PropModeAppend
,
1858 (unsigned char *) &(c
->win
), 1);
1867 if (XineramaIsActive(dpy
)) {
1871 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1872 XineramaScreenInfo
*unique
= NULL
;
1874 for (n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1875 /* only consider unique geometries as separate screens */
1876 unique
= ecalloc(nn
, sizeof(XineramaScreenInfo
));
1877 for (i
= 0, j
= 0; i
< nn
; i
++)
1878 if (isuniquegeom(unique
, j
, &info
[i
]))
1879 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1883 for (i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1884 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1886 m
->next
= createmon();
1890 for (i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1892 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1893 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1897 m
->mx
= m
->wx
= unique
[i
].x_org
;
1898 m
->my
= m
->wy
= unique
[i
].y_org
;
1899 m
->mw
= m
->ww
= unique
[i
].width
;
1900 m
->mh
= m
->wh
= unique
[i
].height
;
1904 /* less monitors available nn < n */
1905 for (i
= nn
; i
< n
; i
++) {
1906 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1907 while (m
->clients
) {
1910 m
->clients
= c
->next
;
1923 #endif /* XINERAMA */
1924 /* default monitor setup */
1928 if (mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1930 mons
->mw
= mons
->ww
= sw
;
1931 mons
->mh
= mons
->wh
= sh
;
1937 selmon
= wintomon(root
);
1943 updatenumlockmask(void)
1946 XModifierKeymap
*modmap
;
1949 modmap
= XGetModifierMapping(dpy
);
1950 for (i
= 0; i
< 8; i
++)
1951 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1952 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1953 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1954 numlockmask
= (1 << i
);
1955 XFreeModifiermap(modmap
);
1959 updatesizehints(Client
*c
)
1964 if (!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1965 /* size is uninitialized, ensure that size.flags aren't used */
1967 if (size
.flags
& PBaseSize
) {
1968 c
->basew
= size
.base_width
;
1969 c
->baseh
= size
.base_height
;
1970 } else if (size
.flags
& PMinSize
) {
1971 c
->basew
= size
.min_width
;
1972 c
->baseh
= size
.min_height
;
1974 c
->basew
= c
->baseh
= 0;
1975 if (size
.flags
& PResizeInc
) {
1976 c
->incw
= size
.width_inc
;
1977 c
->inch
= size
.height_inc
;
1979 c
->incw
= c
->inch
= 0;
1980 if (size
.flags
& PMaxSize
) {
1981 c
->maxw
= size
.max_width
;
1982 c
->maxh
= size
.max_height
;
1984 c
->maxw
= c
->maxh
= 0;
1985 if (size
.flags
& PMinSize
) {
1986 c
->minw
= size
.min_width
;
1987 c
->minh
= size
.min_height
;
1988 } else if (size
.flags
& PBaseSize
) {
1989 c
->minw
= size
.base_width
;
1990 c
->minh
= size
.base_height
;
1992 c
->minw
= c
->minh
= 0;
1993 if (size
.flags
& PAspect
) {
1994 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1995 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1997 c
->maxa
= c
->mina
= 0.0;
1998 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1999 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
2003 updatetitle(Client
*c
)
2005 if (!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
2006 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
2007 if (c
->name
[0] == '\0') /* hack to mark broken clients */
2008 strcpy(c
->name
, broken
);
2014 if (!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
2015 strcpy(stext
, "dwm-"VERSION
);
2020 updatewindowtype(Client
*c
)
2022 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
2023 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
2025 if (state
== netatom
[NetWMFullscreen
])
2026 setfullscreen(c
, 1);
2027 if (wtype
== netatom
[NetWMWindowTypeDialog
])
2032 updatewmhints(Client
*c
)
2036 if ((wmh
= XGetWMHints(dpy
, c
->win
))) {
2037 if (c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2038 wmh
->flags
&= ~XUrgencyHint
;
2039 XSetWMHints(dpy
, c
->win
, wmh
);
2041 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? 1 : 0;
2042 if (wmh
->flags
& InputHint
)
2043 c
->neverfocus
= !wmh
->input
;
2051 view(const Arg
*arg
)
2053 if ((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2055 selmon
->seltags
^= 1; /* toggle sel tagset */
2056 if (arg
->ui
& TAGMASK
)
2057 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2063 wintoclient(Window w
)
2068 for (m
= mons
; m
; m
= m
->next
)
2069 for (c
= m
->clients
; c
; c
= c
->next
)
2082 if (w
== root
&& getrootptr(&x
, &y
))
2083 return recttomon(x
, y
, 1, 1);
2084 for (m
= mons
; m
; m
= m
->next
)
2087 if ((c
= wintoclient(w
)))
2092 /* There's no way to check accesses to destroyed windows, thus those cases are
2093 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2094 * default error handler, which may call exit. */
2096 xerror(Display
*dpy
, XErrorEvent
*ee
)
2098 if (ee
->error_code
== BadWindow
2099 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2100 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2101 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2102 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2103 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2104 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2105 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2106 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2108 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2109 ee
->request_code
, ee
->error_code
);
2110 return xerrorxlib(dpy
, ee
); /* may call exit */
2114 xerrordummy(Display
*dpy
, XErrorEvent
*ee
)
2119 /* Startup Error handler to check if another window manager
2120 * is already running. */
2122 xerrorstart(Display
*dpy
, XErrorEvent
*ee
)
2124 die("dwm: another window manager is already running\n");
2132 XRenderPictFormat
*fmt
;
2141 long masks
= VisualScreenMask
| VisualDepthMask
| VisualClassMask
;
2143 infos
= XGetVisualInfo(dpy
, masks
, &tpl
, &nitems
);
2145 for(i
= 0; i
< nitems
; i
++) {
2146 fmt
= XRenderFindVisualFormat(dpy
, infos
[i
].visual
);
2147 if (fmt
->type
== PictTypeDirect
&& fmt
->direct
.alphaMask
) {
2148 visual
= infos
[i
].visual
;
2149 depth
= infos
[i
].depth
;
2150 cmap
= XCreateColormap(dpy
, root
, visual
, AllocNone
);
2159 visual
= DefaultVisual(dpy
, screen
);
2160 depth
= DefaultDepth(dpy
, screen
);
2161 cmap
= DefaultColormap(dpy
, screen
);
2166 zoom(const Arg
*arg
)
2168 Client
*c
= selmon
->sel
;
2170 if (!selmon
->lt
[selmon
->sellt
]->arrange
2171 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2173 if (c
== nexttiled(selmon
->clients
))
2174 if (!c
|| !(c
= nexttiled(c
->next
)))
2180 main(int argc
, char *argv
[])
2182 if (argc
== 2 && !strcmp("-v", argv
[1]))
2183 die("dwm-"VERSION
"\n");
2185 die("usage: dwm [-v]\n");
2186 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2187 fputs("warning: no locale support\n", stderr
);
2188 if (!(dpy
= XOpenDisplay(NULL
)))
2189 die("dwm: cannot open display\n");
2196 return EXIT_SUCCESS
;