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>
48 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
49 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
50 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
51 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
52 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
53 #define LENGTH(X) (sizeof X / sizeof X[0])
54 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
55 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
56 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
57 #define TAGMASK ((1 << LENGTH(tags)) - 1)
58 #define TEXTW(X) (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
61 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
62 enum { SchemeNorm
, SchemeSel
, SchemeLast
}; /* color schemes */
63 enum { NetSupported
, NetWMName
, NetWMState
,
64 NetWMFullscreen
, NetActiveWindow
, NetWMWindowType
,
65 NetWMWindowTypeDialog
, NetClientList
, NetLast
}; /* EWMH atoms */
66 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, WMLast
}; /* default atoms */
67 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
68 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
81 void (*func
)(const Arg
*arg
);
85 typedef struct Monitor Monitor
;
86 typedef struct Client Client
;
91 int oldx
, oldy
, oldw
, oldh
;
92 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
95 int isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
;
105 void (*func
)(const Arg
*);
111 void (*arrange
)(Monitor
*);
119 int by
; /* bar geometry */
120 int mx
, my
, mw
, mh
; /* screen size */
121 int wx
, wy
, ww
, wh
; /* window area */
122 unsigned int seltags
;
124 unsigned int tagset
[2];
137 const char *instance
;
144 /* function declarations */
145 static void applyrules(Client
*c
);
146 static int applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
);
147 static void arrange(Monitor
*m
);
148 static void arrangemon(Monitor
*m
);
149 static void attach(Client
*c
);
150 static void attachstack(Client
*c
);
151 static void buttonpress(XEvent
*e
);
152 static void checkotherwm(void);
153 static void cleanup(void);
154 static void cleanupmon(Monitor
*mon
);
155 static void clearurgent(Client
*c
);
156 static void clientmessage(XEvent
*e
);
157 static void configure(Client
*c
);
158 static void configurenotify(XEvent
*e
);
159 static void configurerequest(XEvent
*e
);
160 static Monitor
*createmon(void);
161 static void destroynotify(XEvent
*e
);
162 static void detach(Client
*c
);
163 static void detachstack(Client
*c
);
164 static Monitor
*dirtomon(int dir
);
165 static void drawbar(Monitor
*m
);
166 static void drawbars(void);
167 static void enternotify(XEvent
*e
);
168 static void expose(XEvent
*e
);
169 static void focus(Client
*c
);
170 static void focusin(XEvent
*e
);
171 static void focusmon(const Arg
*arg
);
172 static void focusstack(const Arg
*arg
);
173 static int getrootptr(int *x
, int *y
);
174 static long getstate(Window w
);
175 static int gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
176 static void grabbuttons(Client
*c
, int focused
);
177 static void grabkeys(void);
178 static void incnmaster(const Arg
*arg
);
179 static void keypress(XEvent
*e
);
180 static void killclient(const Arg
*arg
);
181 static void manage(Window w
, XWindowAttributes
*wa
);
182 static void mappingnotify(XEvent
*e
);
183 static void maprequest(XEvent
*e
);
184 static void monocle(Monitor
*m
);
185 static void motionnotify(XEvent
*e
);
186 static void movemouse(const Arg
*arg
);
187 static Client
*nexttiled(Client
*c
);
188 static void pop(Client
*);
189 static void propertynotify(XEvent
*e
);
190 static void quit(const Arg
*arg
);
191 static Monitor
*recttomon(int x
, int y
, int w
, int h
);
192 static void resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
);
193 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
194 static void resizemouse(const Arg
*arg
);
195 static void restack(Monitor
*m
);
196 static void run(void);
197 static void scan(void);
198 static int sendevent(Client
*c
, Atom proto
);
199 static void sendmon(Client
*c
, Monitor
*m
);
200 static void setclientstate(Client
*c
, long state
);
201 static void setfocus(Client
*c
);
202 static void setfullscreen(Client
*c
, int fullscreen
);
203 static void setlayout(const Arg
*arg
);
204 static void setmfact(const Arg
*arg
);
205 static void setup(void);
206 static void showhide(Client
*c
);
207 static void sigchld(int unused
);
208 static void spawn(const Arg
*arg
);
209 static void tag(const Arg
*arg
);
210 static void tagmon(const Arg
*arg
);
211 static void tile(Monitor
*);
212 static void togglebar(const Arg
*arg
);
213 static void togglefloating(const Arg
*arg
);
214 static void toggletag(const Arg
*arg
);
215 static void toggleview(const Arg
*arg
);
216 static void unfocus(Client
*c
, int setfocus
);
217 static void unmanage(Client
*c
, int destroyed
);
218 static void unmapnotify(XEvent
*e
);
219 static int updategeom(void);
220 static void updatebarpos(Monitor
*m
);
221 static void updatebars(void);
222 static void updateclientlist(void);
223 static void updatenumlockmask(void);
224 static void updatesizehints(Client
*c
);
225 static void updatestatus(void);
226 static void updatewindowtype(Client
*c
);
227 static void updatetitle(Client
*c
);
228 static void updatewmhints(Client
*c
);
229 static void view(const Arg
*arg
);
230 static Client
*wintoclient(Window w
);
231 static Monitor
*wintomon(Window w
);
232 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
233 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
234 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
235 static void zoom(const Arg
*arg
);
238 static const char broken
[] = "broken";
239 static char stext
[256];
241 static int sw
, sh
; /* X display screen geometry width, height */
242 static int bh
, blw
= 0; /* bar geometry */
243 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
244 static unsigned int numlockmask
= 0;
245 static void (*handler
[LASTEvent
]) (XEvent
*) = {
246 [ButtonPress
] = buttonpress
,
247 [ClientMessage
] = clientmessage
,
248 [ConfigureRequest
] = configurerequest
,
249 [ConfigureNotify
] = configurenotify
,
250 [DestroyNotify
] = destroynotify
,
251 [EnterNotify
] = enternotify
,
254 [KeyPress
] = keypress
,
255 [MappingNotify
] = mappingnotify
,
256 [MapRequest
] = maprequest
,
257 [MotionNotify
] = motionnotify
,
258 [PropertyNotify
] = propertynotify
,
259 [UnmapNotify
] = unmapnotify
261 static Atom wmatom
[WMLast
], netatom
[NetLast
];
262 static int running
= 1;
263 static Cur
*cursor
[CurLast
];
264 static ClrScheme scheme
[SchemeLast
];
267 static Monitor
*mons
, *selmon
;
270 /* configuration, allows nested code to access above variables */
273 /* compile-time check if all tags fit into an unsigned int bit array. */
274 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
276 /* function implementations */
278 applyrules(Client
*c
)
280 const char *class, *instance
;
284 XClassHint ch
= { NULL
, NULL
};
289 XGetClassHint(dpy
, c
->win
, &ch
);
290 class = ch
.res_class
? ch
.res_class
: broken
;
291 instance
= ch
.res_name
? ch
.res_name
: broken
;
293 for (i
= 0; i
< LENGTH(rules
); i
++) {
295 if ((!r
->title
|| strstr(c
->name
, r
->title
))
296 && (!r
->class || strstr(class, r
->class))
297 && (!r
->instance
|| strstr(instance
, r
->instance
)))
299 c
->isfloating
= r
->isfloating
;
301 for (m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
310 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
314 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
)
319 /* set minimum possible */
327 if (*x
+ *w
+ 2 * c
->bw
< 0)
329 if (*y
+ *h
+ 2 * c
->bw
< 0)
332 if (*x
>= m
->wx
+ m
->ww
)
333 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
334 if (*y
>= m
->wy
+ m
->wh
)
335 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
336 if (*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
338 if (*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
345 if (resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
346 /* see last two sentences in ICCCM 4.1.2.3 */
347 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
348 if (!baseismin
) { /* temporarily remove base dimensions */
352 /* adjust for aspect limits */
353 if (c
->mina
> 0 && c
->maxa
> 0) {
354 if (c
->maxa
< (float)*w
/ *h
)
355 *w
= *h
* c
->maxa
+ 0.5;
356 else if (c
->mina
< (float)*h
/ *w
)
357 *h
= *w
* c
->mina
+ 0.5;
359 if (baseismin
) { /* increment calculation requires this */
363 /* adjust for increment value */
368 /* restore base dimensions */
369 *w
= MAX(*w
+ c
->basew
, c
->minw
);
370 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
372 *w
= MIN(*w
, c
->maxw
);
374 *h
= MIN(*h
, c
->maxh
);
376 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
384 else for (m
= mons
; m
; m
= m
->next
)
389 } else for (m
= mons
; m
; m
= m
->next
)
394 arrangemon(Monitor
*m
)
396 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
397 if (m
->lt
[m
->sellt
]->arrange
)
398 m
->lt
[m
->sellt
]->arrange(m
);
404 c
->next
= c
->mon
->clients
;
409 attachstack(Client
*c
)
411 c
->snext
= c
->mon
->stack
;
416 buttonpress(XEvent
*e
)
418 unsigned int i
, x
, click
;
422 XButtonPressedEvent
*ev
= &e
->xbutton
;
425 /* focus monitor if necessary */
426 if ((m
= wintomon(ev
->window
)) && m
!= selmon
) {
427 unfocus(selmon
->sel
, 1);
431 if (ev
->window
== selmon
->barwin
) {
435 while (ev
->x
>= x
&& ++i
< LENGTH(tags
));
436 if (i
< LENGTH(tags
)) {
439 } else if (ev
->x
< x
+ blw
)
441 else if (ev
->x
> selmon
->ww
- TEXTW(stext
))
442 click
= ClkStatusText
;
445 } else if ((c
= wintoclient(ev
->window
))) {
447 click
= ClkClientWin
;
449 for (i
= 0; i
< LENGTH(buttons
); i
++)
450 if (click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
451 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
452 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
458 xerrorxlib
= XSetErrorHandler(xerrorstart
);
459 /* this causes an error if some other window manager is running */
460 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
462 XSetErrorHandler(xerror
);
470 Layout foo
= { "", NULL
};
475 selmon
->lt
[selmon
->sellt
] = &foo
;
476 for (m
= mons
; m
; m
= m
->next
)
478 unmanage(m
->stack
, 0);
479 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
482 for (i
= 0; i
< CurLast
; i
++)
483 drw_cur_free(drw
, cursor
[i
]);
484 for (i
= 0; i
< SchemeLast
; i
++) {
485 drw_clr_free(scheme
[i
].border
);
486 drw_clr_free(scheme
[i
].bg
);
487 drw_clr_free(scheme
[i
].fg
);
491 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
492 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
496 cleanupmon(Monitor
*mon
)
503 for (m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
506 XUnmapWindow(dpy
, mon
->barwin
);
507 XDestroyWindow(dpy
, mon
->barwin
);
512 clearurgent(Client
*c
)
517 if (!(wmh
= XGetWMHints(dpy
, c
->win
)))
519 wmh
->flags
&= ~XUrgencyHint
;
520 XSetWMHints(dpy
, c
->win
, wmh
);
525 clientmessage(XEvent
*e
)
527 XClientMessageEvent
*cme
= &e
->xclient
;
528 Client
*c
= wintoclient(cme
->window
);
532 if (cme
->message_type
== netatom
[NetWMState
]) {
533 if (cme
->data
.l
[1] == netatom
[NetWMFullscreen
] || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
534 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
535 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
536 } else if (cme
->message_type
== netatom
[NetActiveWindow
]) {
538 c
->mon
->seltags
^= 1;
539 c
->mon
->tagset
[c
->mon
->seltags
] = c
->tags
;
550 ce
.type
= ConfigureNotify
;
558 ce
.border_width
= c
->bw
;
560 ce
.override_redirect
= False
;
561 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
565 configurenotify(XEvent
*e
)
568 XConfigureEvent
*ev
= &e
->xconfigure
;
571 /* TODO: updategeom handling sucks, needs to be simplified */
572 if (ev
->window
== root
) {
573 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
576 if (updategeom() || dirty
) {
577 drw_resize(drw
, sw
, bh
);
579 for (m
= mons
; m
; m
= m
->next
)
580 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
588 configurerequest(XEvent
*e
)
592 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
595 if ((c
= wintoclient(ev
->window
))) {
596 if (ev
->value_mask
& CWBorderWidth
)
597 c
->bw
= ev
->border_width
;
598 else if (c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
600 if (ev
->value_mask
& CWX
) {
602 c
->x
= m
->mx
+ ev
->x
;
604 if (ev
->value_mask
& CWY
) {
606 c
->y
= m
->my
+ ev
->y
;
608 if (ev
->value_mask
& CWWidth
) {
612 if (ev
->value_mask
& CWHeight
) {
616 if ((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
617 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
618 if ((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
619 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
620 if ((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
623 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
629 wc
.width
= ev
->width
;
630 wc
.height
= ev
->height
;
631 wc
.border_width
= ev
->border_width
;
632 wc
.sibling
= ev
->above
;
633 wc
.stack_mode
= ev
->detail
;
634 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
644 m
= ecalloc(1, sizeof(Monitor
));
645 m
->tagset
[0] = m
->tagset
[1] = 1;
647 m
->nmaster
= nmaster
;
648 m
->showbar
= showbar
;
650 m
->lt
[0] = &layouts
[0];
651 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
652 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
657 destroynotify(XEvent
*e
)
660 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
662 if ((c
= wintoclient(ev
->window
)))
671 for (tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
676 detachstack(Client
*c
)
680 for (tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
683 if (c
== c
->mon
->sel
) {
684 for (t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
695 if (!(m
= selmon
->next
))
697 } else if (selmon
== mons
)
698 for (m
= mons
; m
->next
; m
= m
->next
);
700 for (m
= mons
; m
->next
!= selmon
; m
= m
->next
);
708 unsigned int i
, occ
= 0, urg
= 0;
711 dx
= (drw
->fonts
[0]->ascent
+ drw
->fonts
[0]->descent
+ 2) / 4;
713 for (c
= m
->clients
; c
; c
= c
->next
) {
719 for (i
= 0; i
< LENGTH(tags
); i
++) {
721 drw_setscheme(drw
, m
->tagset
[m
->seltags
] & 1 << i
? &scheme
[SchemeSel
] : &scheme
[SchemeNorm
]);
722 drw_text(drw
, x
, 0, w
, bh
, tags
[i
], urg
& 1 << i
);
723 drw_rect(drw
, x
+ 1, 1, dx
, dx
, m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
724 occ
& 1 << i
, urg
& 1 << i
);
727 w
= blw
= TEXTW(m
->ltsymbol
);
728 drw_setscheme(drw
, &scheme
[SchemeNorm
]);
729 drw_text(drw
, x
, 0, w
, bh
, m
->ltsymbol
, 0);
732 if (m
== selmon
) { /* status is only drawn on selected monitor */
739 drw_text(drw
, x
, 0, w
, bh
, stext
, 0);
742 if ((w
= x
- xx
) > bh
) {
745 drw_setscheme(drw
, m
== selmon
? &scheme
[SchemeSel
] : &scheme
[SchemeNorm
]);
746 drw_text(drw
, x
, 0, w
, bh
, m
->sel
->name
, 0);
747 drw_rect(drw
, x
+ 1, 1, dx
, dx
, m
->sel
->isfixed
, m
->sel
->isfloating
, 0);
749 drw_setscheme(drw
, &scheme
[SchemeNorm
]);
750 drw_rect(drw
, x
, 0, w
, bh
, 1, 0, 1);
753 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
);
761 for (m
= mons
; m
; m
= m
->next
)
766 enternotify(XEvent
*e
)
770 XCrossingEvent
*ev
= &e
->xcrossing
;
772 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
774 c
= wintoclient(ev
->window
);
775 m
= c
? c
->mon
: wintomon(ev
->window
);
777 unfocus(selmon
->sel
, 1);
779 } else if (!c
|| c
== selmon
->sel
)
788 XExposeEvent
*ev
= &e
->xexpose
;
790 if (ev
->count
== 0 && (m
= wintomon(ev
->window
)))
797 if (!c
|| !ISVISIBLE(c
))
798 for (c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
799 /* was if (selmon->sel) */
800 if (selmon
->sel
&& selmon
->sel
!= c
)
801 unfocus(selmon
->sel
, 0);
803 if (c
->mon
!= selmon
)
810 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
].border
->pix
);
813 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
814 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
820 /* there are some broken focus acquiring clients */
824 XFocusChangeEvent
*ev
= &e
->xfocus
;
826 if (selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
827 setfocus(selmon
->sel
);
831 focusmon(const Arg
*arg
)
837 if ((m
= dirtomon(arg
->i
)) == selmon
)
839 unfocus(selmon
->sel
, 0); /* s/1/0/ fixes input focus issues
840 in gedit and anjuta */
846 focusstack(const Arg
*arg
)
848 Client
*c
= NULL
, *i
;
853 for (c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
855 for (c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
857 for (i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
861 for (; i
; i
= i
->next
)
872 getatomprop(Client
*c
, Atom prop
)
876 unsigned char *p
= NULL
;
877 Atom da
, atom
= None
;
879 if (XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
880 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
888 getrootptr(int *x
, int *y
)
894 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
902 unsigned char *p
= NULL
;
903 unsigned long n
, extra
;
906 if (XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
907 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
916 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
922 if (!text
|| size
== 0)
925 XGetTextProperty(dpy
, w
, &name
, atom
);
928 if (name
.encoding
== XA_STRING
)
929 strncpy(text
, (char *)name
.value
, size
- 1);
931 if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
932 strncpy(text
, *list
, size
- 1);
933 XFreeStringList(list
);
936 text
[size
- 1] = '\0';
942 grabbuttons(Client
*c
, int focused
)
947 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
948 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
950 for (i
= 0; i
< LENGTH(buttons
); i
++)
951 if (buttons
[i
].click
== ClkClientWin
)
952 for (j
= 0; j
< LENGTH(modifiers
); j
++)
953 XGrabButton(dpy
, buttons
[i
].button
,
954 buttons
[i
].mask
| modifiers
[j
],
955 c
->win
, False
, BUTTONMASK
,
956 GrabModeAsync
, GrabModeSync
, None
, None
);
958 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
959 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
969 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
972 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
973 for (i
= 0; i
< LENGTH(keys
); i
++)
974 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
975 for (j
= 0; j
< LENGTH(modifiers
); j
++)
976 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
977 True
, GrabModeAsync
, GrabModeAsync
);
982 incnmaster(const Arg
*arg
)
984 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
990 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
)
993 if (unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
994 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
998 #endif /* XINERAMA */
1008 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1009 for (i
= 0; i
< LENGTH(keys
); i
++)
1010 if (keysym
== keys
[i
].keysym
1011 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1013 keys
[i
].func(&(keys
[i
].arg
));
1017 killclient(const Arg
*arg
)
1021 if (!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1023 XSetErrorHandler(xerrordummy
);
1024 XSetCloseDownMode(dpy
, DestroyAll
);
1025 XKillClient(dpy
, selmon
->sel
->win
);
1027 XSetErrorHandler(xerror
);
1033 manage(Window w
, XWindowAttributes
*wa
)
1035 Client
*c
, *t
= NULL
;
1036 Window trans
= None
;
1039 c
= ecalloc(1, sizeof(Client
));
1042 if (XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1050 c
->x
= c
->oldx
= wa
->x
;
1051 c
->y
= c
->oldy
= wa
->y
;
1052 c
->w
= c
->oldw
= wa
->width
;
1053 c
->h
= c
->oldh
= wa
->height
;
1054 c
->oldbw
= wa
->border_width
;
1056 if (c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1057 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1058 if (c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1059 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1060 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1061 /* only fix client y-offset, if the client center might cover the bar */
1062 c
->y
= MAX(c
->y
, ((c
->mon
->by
== c
->mon
->my
) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1063 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1066 wc
.border_width
= c
->bw
;
1067 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1068 XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
].border
->pix
);
1069 configure(c
); /* propagates border_width, if size doesn't change */
1070 updatewindowtype(c
);
1073 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1076 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1078 XRaiseWindow(dpy
, c
->win
);
1081 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1082 (unsigned char *) &(c
->win
), 1);
1083 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1084 setclientstate(c
, NormalState
);
1085 if (c
->mon
== selmon
)
1086 unfocus(selmon
->sel
, 0);
1089 XMapWindow(dpy
, c
->win
);
1094 mappingnotify(XEvent
*e
)
1096 XMappingEvent
*ev
= &e
->xmapping
;
1098 XRefreshKeyboardMapping(ev
);
1099 if (ev
->request
== MappingKeyboard
)
1104 maprequest(XEvent
*e
)
1106 static XWindowAttributes wa
;
1107 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1109 if (!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1111 if (wa
.override_redirect
)
1113 if (!wintoclient(ev
->window
))
1114 manage(ev
->window
, &wa
);
1123 for (c
= m
->clients
; c
; c
= c
->next
)
1126 if (n
> 0) /* override layout symbol */
1127 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1128 for (c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1129 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, 0);
1133 motionnotify(XEvent
*e
)
1135 static Monitor
*mon
= NULL
;
1137 XMotionEvent
*ev
= &e
->xmotion
;
1139 if (ev
->window
!= root
)
1141 if ((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1142 unfocus(selmon
->sel
, 1);
1150 movemouse(const Arg
*arg
)
1152 int x
, y
, ocx
, ocy
, nx
, ny
;
1158 if (!(c
= selmon
->sel
))
1160 if (c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1165 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1166 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1168 if (!getrootptr(&x
, &y
))
1171 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1173 case ConfigureRequest
:
1176 handler
[ev
.type
](&ev
);
1179 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1181 lasttime
= ev
.xmotion
.time
;
1183 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1184 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1185 if (nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1186 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1187 if (abs(selmon
->wx
- nx
) < snap
)
1189 else if (abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1190 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1191 if (abs(selmon
->wy
- ny
) < snap
)
1193 else if (abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1194 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1195 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1196 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1197 togglefloating(NULL
);
1199 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1200 resize(c
, nx
, ny
, c
->w
, c
->h
, 1);
1203 } while (ev
.type
!= ButtonRelease
);
1204 XUngrabPointer(dpy
, CurrentTime
);
1205 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1213 nexttiled(Client
*c
)
1215 for (; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1229 propertynotify(XEvent
*e
)
1233 XPropertyEvent
*ev
= &e
->xproperty
;
1235 if ((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1237 else if (ev
->state
== PropertyDelete
)
1238 return; /* ignore */
1239 else if ((c
= wintoclient(ev
->window
))) {
1242 case XA_WM_TRANSIENT_FOR
:
1243 if (!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1244 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1247 case XA_WM_NORMAL_HINTS
:
1255 if (ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1257 if (c
== c
->mon
->sel
)
1260 if (ev
->atom
== netatom
[NetWMWindowType
])
1261 updatewindowtype(c
);
1266 quit(const Arg
*arg
)
1272 recttomon(int x
, int y
, int w
, int h
)
1274 Monitor
*m
, *r
= selmon
;
1277 for (m
= mons
; m
; m
= m
->next
)
1278 if ((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1286 resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
)
1288 if (applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1289 resizeclient(c
, x
, y
, w
, h
);
1293 resizeclient(Client
*c
, int x
, int y
, int w
, int h
)
1297 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1298 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1299 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1300 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1301 wc
.border_width
= c
->bw
;
1302 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1308 resizemouse(const Arg
*arg
)
1310 int ocx
, ocy
, nw
, nh
;
1316 if (!(c
= selmon
->sel
))
1318 if (c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1323 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1324 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1326 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1328 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1330 case ConfigureRequest
:
1333 handler
[ev
.type
](&ev
);
1336 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1338 lasttime
= ev
.xmotion
.time
;
1340 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1341 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1342 if (c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1343 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1345 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1346 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1347 togglefloating(NULL
);
1349 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1350 resize(c
, c
->x
, c
->y
, nw
, nh
, 1);
1353 } while (ev
.type
!= ButtonRelease
);
1354 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1355 XUngrabPointer(dpy
, CurrentTime
);
1356 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1357 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1374 if (m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1375 XRaiseWindow(dpy
, m
->sel
->win
);
1376 if (m
->lt
[m
->sellt
]->arrange
) {
1377 wc
.stack_mode
= Below
;
1378 wc
.sibling
= m
->barwin
;
1379 for (c
= m
->stack
; c
; c
= c
->snext
)
1380 if (!c
->isfloating
&& ISVISIBLE(c
)) {
1381 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1382 wc
.sibling
= c
->win
;
1386 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1393 /* main event loop */
1395 while (running
&& !XNextEvent(dpy
, &ev
))
1396 if (handler
[ev
.type
])
1397 handler
[ev
.type
](&ev
); /* call handler */
1403 unsigned int i
, num
;
1404 Window d1
, d2
, *wins
= NULL
;
1405 XWindowAttributes wa
;
1407 if (XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1408 for (i
= 0; i
< num
; i
++) {
1409 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1410 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1412 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1413 manage(wins
[i
], &wa
);
1415 for (i
= 0; i
< num
; i
++) { /* now the transients */
1416 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1418 if (XGetTransientForHint(dpy
, wins
[i
], &d1
)
1419 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1420 manage(wins
[i
], &wa
);
1428 sendmon(Client
*c
, Monitor
*m
)
1436 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1444 setclientstate(Client
*c
, long state
)
1446 long data
[] = { state
, None
};
1448 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1449 PropModeReplace
, (unsigned char *)data
, 2);
1453 sendevent(Client
*c
, Atom proto
)
1460 if (XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1461 while (!exists
&& n
--)
1462 exists
= protocols
[n
] == proto
;
1466 ev
.type
= ClientMessage
;
1467 ev
.xclient
.window
= c
->win
;
1468 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1469 ev
.xclient
.format
= 32;
1470 ev
.xclient
.data
.l
[0] = proto
;
1471 ev
.xclient
.data
.l
[1] = CurrentTime
;
1472 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1480 if (!c
->neverfocus
) {
1481 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1482 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1483 XA_WINDOW
, 32, PropModeReplace
,
1484 (unsigned char *) &(c
->win
), 1);
1486 sendevent(c
, wmatom
[WMTakeFocus
]);
1490 setfullscreen(Client
*c
, int fullscreen
)
1492 if (fullscreen
&& !c
->isfullscreen
) {
1493 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1494 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1495 c
->isfullscreen
= 1;
1496 c
->oldstate
= c
->isfloating
;
1500 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1501 XRaiseWindow(dpy
, c
->win
);
1502 } else if (!fullscreen
&& c
->isfullscreen
){
1503 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1504 PropModeReplace
, (unsigned char*)0, 0);
1505 c
->isfullscreen
= 0;
1506 c
->isfloating
= c
->oldstate
;
1512 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1518 setlayout(const Arg
*arg
)
1520 if (!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1523 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1524 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1531 /* arg > 1.0 will set mfact absolutly */
1533 setmfact(const Arg
*arg
)
1537 if (!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1539 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1540 if (f
< 0.1 || f
> 0.9)
1549 XSetWindowAttributes wa
;
1551 /* clean up any zombies immediately */
1555 screen
= DefaultScreen(dpy
);
1556 sw
= DisplayWidth(dpy
, screen
);
1557 sh
= DisplayHeight(dpy
, screen
);
1558 root
= RootWindow(dpy
, screen
);
1559 drw
= drw_create(dpy
, screen
, root
, sw
, sh
);
1560 drw_load_fonts(drw
, fonts
, LENGTH(fonts
));
1561 if (!drw
->fontcount
)
1562 die("no fonts could be loaded.\n");
1563 bh
= drw
->fonts
[0]->h
+ 2;
1566 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1567 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1568 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1569 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1570 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1571 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1572 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1573 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1574 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1575 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1576 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1577 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1579 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1580 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1581 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1582 /* init appearance */
1583 scheme
[SchemeNorm
].border
= drw_clr_create(drw
, normbordercolor
);
1584 scheme
[SchemeNorm
].bg
= drw_clr_create(drw
, normbgcolor
);
1585 scheme
[SchemeNorm
].fg
= drw_clr_create(drw
, normfgcolor
);
1586 scheme
[SchemeSel
].border
= drw_clr_create(drw
, selbordercolor
);
1587 scheme
[SchemeSel
].bg
= drw_clr_create(drw
, selbgcolor
);
1588 scheme
[SchemeSel
].fg
= drw_clr_create(drw
, selfgcolor
);
1592 /* EWMH support per view */
1593 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1594 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1595 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1596 /* select for events */
1597 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1598 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
|PointerMotionMask
1599 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1600 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1601 XSelectInput(dpy
, root
, wa
.event_mask
);
1612 /* show clients top down */
1613 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1614 if ((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1615 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, 0);
1618 /* hide clients bottom up */
1620 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1627 if (signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1628 die("can't install SIGCHLD handler:");
1629 while (0 < waitpid(-1, NULL
, WNOHANG
));
1633 spawn(const Arg
*arg
)
1635 if (arg
->v
== dmenucmd
)
1636 dmenumon
[0] = '0' + selmon
->num
;
1639 close(ConnectionNumber(dpy
));
1641 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1642 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1651 if (selmon
->sel
&& arg
->ui
& TAGMASK
) {
1652 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1659 tagmon(const Arg
*arg
)
1661 if (!selmon
->sel
|| !mons
->next
)
1663 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1669 unsigned int i
, n
, h
, mw
, my
, ty
;
1672 for (n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1677 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1680 for (i
= my
= ty
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1681 if (i
< m
->nmaster
) {
1682 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
);
1683 resize(c
, m
->wx
, m
->wy
+ my
, mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1686 h
= (m
->wh
- ty
) / (n
- i
);
1687 resize(c
, m
->wx
+ mw
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1693 togglebar(const Arg
*arg
)
1695 selmon
->showbar
= !selmon
->showbar
;
1696 updatebarpos(selmon
);
1697 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1702 togglefloating(const Arg
*arg
)
1706 if (selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1708 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1709 if (selmon
->sel
->isfloating
)
1710 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1711 selmon
->sel
->w
, selmon
->sel
->h
, 0);
1716 toggletag(const Arg
*arg
)
1718 unsigned int newtags
;
1722 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1724 selmon
->sel
->tags
= newtags
;
1731 toggleview(const Arg
*arg
)
1733 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1736 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1743 unfocus(Client
*c
, int setfocus
)
1748 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
].border
->pix
);
1750 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1751 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1756 unmanage(Client
*c
, int destroyed
)
1758 Monitor
*m
= c
->mon
;
1761 /* The server grab construct avoids race conditions. */
1765 wc
.border_width
= c
->oldbw
;
1767 XSetErrorHandler(xerrordummy
);
1768 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1769 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1770 setclientstate(c
, WithdrawnState
);
1772 XSetErrorHandler(xerror
);
1782 unmapnotify(XEvent
*e
)
1785 XUnmapEvent
*ev
= &e
->xunmap
;
1787 if ((c
= wintoclient(ev
->window
))) {
1789 setclientstate(c
, WithdrawnState
);
1799 XSetWindowAttributes wa
= {
1800 .override_redirect
= True
,
1801 .background_pixmap
= ParentRelative
,
1802 .event_mask
= ButtonPressMask
|ExposureMask
1804 for (m
= mons
; m
; m
= m
->next
) {
1807 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1808 CopyFromParent
, DefaultVisual(dpy
, screen
),
1809 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1810 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
1811 XMapRaised(dpy
, m
->barwin
);
1816 updatebarpos(Monitor
*m
)
1822 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1823 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1834 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1835 for (m
= mons
; m
; m
= m
->next
)
1836 for (c
= m
->clients
; c
; c
= c
->next
)
1837 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
1838 XA_WINDOW
, 32, PropModeAppend
,
1839 (unsigned char *) &(c
->win
), 1);
1848 if (XineramaIsActive(dpy
)) {
1852 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1853 XineramaScreenInfo
*unique
= NULL
;
1855 for (n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1856 /* only consider unique geometries as separate screens */
1857 unique
= ecalloc(nn
, sizeof(XineramaScreenInfo
));
1858 for (i
= 0, j
= 0; i
< nn
; i
++)
1859 if (isuniquegeom(unique
, j
, &info
[i
]))
1860 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1864 for (i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1865 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1867 m
->next
= createmon();
1871 for (i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1873 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1874 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1878 m
->mx
= m
->wx
= unique
[i
].x_org
;
1879 m
->my
= m
->wy
= unique
[i
].y_org
;
1880 m
->mw
= m
->ww
= unique
[i
].width
;
1881 m
->mh
= m
->wh
= unique
[i
].height
;
1885 /* less monitors available nn < n */
1886 for (i
= nn
; i
< n
; i
++) {
1887 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1888 while (m
->clients
) {
1891 m
->clients
= c
->next
;
1904 #endif /* XINERAMA */
1905 /* default monitor setup */
1909 if (mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1911 mons
->mw
= mons
->ww
= sw
;
1912 mons
->mh
= mons
->wh
= sh
;
1918 selmon
= wintomon(root
);
1924 updatenumlockmask(void)
1927 XModifierKeymap
*modmap
;
1930 modmap
= XGetModifierMapping(dpy
);
1931 for (i
= 0; i
< 8; i
++)
1932 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1933 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1934 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1935 numlockmask
= (1 << i
);
1936 XFreeModifiermap(modmap
);
1940 updatesizehints(Client
*c
)
1945 if (!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1946 /* size is uninitialized, ensure that size.flags aren't used */
1948 if (size
.flags
& PBaseSize
) {
1949 c
->basew
= size
.base_width
;
1950 c
->baseh
= size
.base_height
;
1951 } else if (size
.flags
& PMinSize
) {
1952 c
->basew
= size
.min_width
;
1953 c
->baseh
= size
.min_height
;
1955 c
->basew
= c
->baseh
= 0;
1956 if (size
.flags
& PResizeInc
) {
1957 c
->incw
= size
.width_inc
;
1958 c
->inch
= size
.height_inc
;
1960 c
->incw
= c
->inch
= 0;
1961 if (size
.flags
& PMaxSize
) {
1962 c
->maxw
= size
.max_width
;
1963 c
->maxh
= size
.max_height
;
1965 c
->maxw
= c
->maxh
= 0;
1966 if (size
.flags
& PMinSize
) {
1967 c
->minw
= size
.min_width
;
1968 c
->minh
= size
.min_height
;
1969 } else if (size
.flags
& PBaseSize
) {
1970 c
->minw
= size
.base_width
;
1971 c
->minh
= size
.base_height
;
1973 c
->minw
= c
->minh
= 0;
1974 if (size
.flags
& PAspect
) {
1975 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1976 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1978 c
->maxa
= c
->mina
= 0.0;
1979 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1980 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1984 updatetitle(Client
*c
)
1986 if (!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1987 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1988 if (c
->name
[0] == '\0') /* hack to mark broken clients */
1989 strcpy(c
->name
, broken
);
1995 if (!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1996 strcpy(stext
, "dwm-"VERSION
);
2001 updatewindowtype(Client
*c
)
2003 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
2004 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
2006 if (state
== netatom
[NetWMFullscreen
])
2007 setfullscreen(c
, 1);
2008 if (wtype
== netatom
[NetWMWindowTypeDialog
])
2013 updatewmhints(Client
*c
)
2017 if ((wmh
= XGetWMHints(dpy
, c
->win
))) {
2018 if (c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2019 wmh
->flags
&= ~XUrgencyHint
;
2020 XSetWMHints(dpy
, c
->win
, wmh
);
2022 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? 1 : 0;
2023 if (wmh
->flags
& InputHint
)
2024 c
->neverfocus
= !wmh
->input
;
2032 view(const Arg
*arg
)
2034 if ((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2036 selmon
->seltags
^= 1; /* toggle sel tagset */
2037 if (arg
->ui
& TAGMASK
)
2038 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2044 wintoclient(Window w
)
2049 for (m
= mons
; m
; m
= m
->next
)
2050 for (c
= m
->clients
; c
; c
= c
->next
)
2063 if (w
== root
&& getrootptr(&x
, &y
))
2064 return recttomon(x
, y
, 1, 1);
2065 for (m
= mons
; m
; m
= m
->next
)
2068 if ((c
= wintoclient(w
)))
2073 /* There's no way to check accesses to destroyed windows, thus those cases are
2074 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2075 * default error handler, which may call exit. */
2077 xerror(Display
*dpy
, XErrorEvent
*ee
)
2079 if (ee
->error_code
== BadWindow
2080 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2081 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2082 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2083 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2084 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2085 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2086 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2087 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2089 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2090 ee
->request_code
, ee
->error_code
);
2091 return xerrorxlib(dpy
, ee
); /* may call exit */
2095 xerrordummy(Display
*dpy
, XErrorEvent
*ee
)
2100 /* Startup Error handler to check if another window manager
2101 * is already running. */
2103 xerrorstart(Display
*dpy
, XErrorEvent
*ee
)
2105 die("dwm: another window manager is already running\n");
2110 zoom(const Arg
*arg
)
2112 Client
*c
= selmon
->sel
;
2114 if (!selmon
->lt
[selmon
->sellt
]->arrange
2115 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2117 if (c
== nexttiled(selmon
->clients
))
2118 if (!c
|| !(c
= nexttiled(c
->next
)))
2124 main(int argc
, char *argv
[])
2126 if (argc
== 2 && !strcmp("-v", argv
[1]))
2127 die("dwm-"VERSION
"\n");
2129 die("usage: dwm [-v]\n");
2130 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2131 fputs("warning: no locale support\n", stderr
);
2132 if (!(dpy
= XOpenDisplay(NULL
)))
2133 die("dwm: cannot open display\n");
2140 return EXIT_SUCCESS
;