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)
63 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
64 enum { SchemeNorm
, SchemeSel
, SchemeLast
}; /* color schemes */
65 enum { NetSupported
, NetWMName
, NetWMState
,
66 NetWMFullscreen
, NetActiveWindow
, NetWMWindowType
,
67 NetWMWindowTypeDialog
, NetClientList
, NetLast
}; /* EWMH atoms */
68 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, WMLast
}; /* default atoms */
69 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
70 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
83 void (*func
)(const Arg
*arg
);
87 typedef struct Monitor Monitor
;
88 typedef struct Client Client
;
93 int oldx
, oldy
, oldw
, oldh
;
94 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
97 int isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
;
107 void (*func
)(const Arg
*);
113 void (*arrange
)(Monitor
*);
121 int by
; /* bar geometry */
122 int mx
, my
, mw
, mh
; /* screen size */
123 int wx
, wy
, ww
, wh
; /* window area */
124 unsigned int seltags
;
126 unsigned int tagset
[2];
139 const char *instance
;
146 /* function declarations */
147 static void applyrules(Client
*c
);
148 static int applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
);
149 static void arrange(Monitor
*m
);
150 static void arrangemon(Monitor
*m
);
151 static void attach(Client
*c
);
152 static void attachstack(Client
*c
);
153 static void buttonpress(XEvent
*e
);
154 static void checkotherwm(void);
155 static void cleanup(void);
156 static void cleanupmon(Monitor
*mon
);
157 static void clearurgent(Client
*c
);
158 static void clientmessage(XEvent
*e
);
159 static void configure(Client
*c
);
160 static void configurenotify(XEvent
*e
);
161 static void configurerequest(XEvent
*e
);
162 static Monitor
*createmon(void);
163 static void destroynotify(XEvent
*e
);
164 static void detach(Client
*c
);
165 static void detachstack(Client
*c
);
166 static Monitor
*dirtomon(int dir
);
167 static void drawbar(Monitor
*m
);
168 static void drawbars(void);
169 static void enternotify(XEvent
*e
);
170 static void expose(XEvent
*e
);
171 static void focus(Client
*c
);
172 static void focusin(XEvent
*e
);
173 static void focusmon(const Arg
*arg
);
174 static void focusstack(const Arg
*arg
);
175 static int getrootptr(int *x
, int *y
);
176 static long getstate(Window w
);
177 static int gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
178 static void grabbuttons(Client
*c
, int focused
);
179 static void grabkeys(void);
180 static void incnmaster(const Arg
*arg
);
181 static void keypress(XEvent
*e
);
182 static void killclient(const Arg
*arg
);
183 static void manage(Window w
, XWindowAttributes
*wa
);
184 static void mappingnotify(XEvent
*e
);
185 static void maprequest(XEvent
*e
);
186 static void monocle(Monitor
*m
);
187 static void motionnotify(XEvent
*e
);
188 static void movemouse(const Arg
*arg
);
189 static Client
*nexttiled(Client
*c
);
190 static void pop(Client
*);
191 static void propertynotify(XEvent
*e
);
192 static void quit(const Arg
*arg
);
193 static Monitor
*recttomon(int x
, int y
, int w
, int h
);
194 static void resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
);
195 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
196 static void resizemouse(const Arg
*arg
);
197 static void restack(Monitor
*m
);
198 static void run(void);
199 static void scan(void);
200 static int sendevent(Client
*c
, Atom proto
);
201 static void sendmon(Client
*c
, Monitor
*m
);
202 static void setclientstate(Client
*c
, long state
);
203 static void setfocus(Client
*c
);
204 static void setfullscreen(Client
*c
, int fullscreen
);
205 static void setlayout(const Arg
*arg
);
206 static void setmfact(const Arg
*arg
);
207 static void setup(void);
208 static void showhide(Client
*c
);
209 static void sigchld(int unused
);
210 static void spawn(const Arg
*arg
);
211 static void tag(const Arg
*arg
);
212 static void tagmon(const Arg
*arg
);
213 static void tile(Monitor
*);
214 static void togglebar(const Arg
*arg
);
215 static void togglefloating(const Arg
*arg
);
216 static void toggletag(const Arg
*arg
);
217 static void toggleview(const Arg
*arg
);
218 static void unfocus(Client
*c
, int setfocus
);
219 static void unmanage(Client
*c
, int destroyed
);
220 static void unmapnotify(XEvent
*e
);
221 static int updategeom(void);
222 static void updatebarpos(Monitor
*m
);
223 static void updatebars(void);
224 static void updateclientlist(void);
225 static void updatenumlockmask(void);
226 static void updatesizehints(Client
*c
);
227 static void updatestatus(void);
228 static void updatewindowtype(Client
*c
);
229 static void updatetitle(Client
*c
);
230 static void updatewmhints(Client
*c
);
231 static void view(const Arg
*arg
);
232 static Client
*wintoclient(Window w
);
233 static Monitor
*wintomon(Window w
);
234 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
235 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
236 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
237 static void xinitvisual();
238 static void zoom(const Arg
*arg
);
241 static const char broken
[] = "broken";
242 static char stext
[256];
244 static int sw
, sh
; /* X display screen geometry width, height */
245 static int bh
, blw
= 0; /* bar geometry */
246 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
247 static unsigned int numlockmask
= 0;
248 static void (*handler
[LASTEvent
]) (XEvent
*) = {
249 [ButtonPress
] = buttonpress
,
250 [ClientMessage
] = clientmessage
,
251 [ConfigureRequest
] = configurerequest
,
252 [ConfigureNotify
] = configurenotify
,
253 [DestroyNotify
] = destroynotify
,
254 [EnterNotify
] = enternotify
,
257 [KeyPress
] = keypress
,
258 [MappingNotify
] = mappingnotify
,
259 [MapRequest
] = maprequest
,
260 [MotionNotify
] = motionnotify
,
261 [PropertyNotify
] = propertynotify
,
262 [UnmapNotify
] = unmapnotify
264 static Atom wmatom
[WMLast
], netatom
[NetLast
];
265 static int running
= 1;
266 static Cur
*cursor
[CurLast
];
267 static ClrScheme scheme
[SchemeLast
];
270 static Monitor
*mons
, *selmon
;
273 static int useargb
= 0;
274 static Visual
*visual
;
276 static Colormap cmap
;
278 /* configuration, allows nested code to access above variables */
281 /* compile-time check if all tags fit into an unsigned int bit array. */
282 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
284 /* function implementations */
286 applyrules(Client
*c
)
288 const char *class, *instance
;
292 XClassHint ch
= { NULL
, NULL
};
297 XGetClassHint(dpy
, c
->win
, &ch
);
298 class = ch
.res_class
? ch
.res_class
: broken
;
299 instance
= ch
.res_name
? ch
.res_name
: broken
;
301 for (i
= 0; i
< LENGTH(rules
); i
++) {
303 if ((!r
->title
|| strstr(c
->name
, r
->title
))
304 && (!r
->class || strstr(class, r
->class))
305 && (!r
->instance
|| strstr(instance
, r
->instance
)))
307 c
->isfloating
= r
->isfloating
;
309 for (m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
318 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
322 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, int interact
)
327 /* set minimum possible */
335 if (*x
+ *w
+ 2 * c
->bw
< 0)
337 if (*y
+ *h
+ 2 * c
->bw
< 0)
340 if (*x
>= m
->wx
+ m
->ww
)
341 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
342 if (*y
>= m
->wy
+ m
->wh
)
343 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
344 if (*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
346 if (*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
353 if (resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
354 /* see last two sentences in ICCCM 4.1.2.3 */
355 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
356 if (!baseismin
) { /* temporarily remove base dimensions */
360 /* adjust for aspect limits */
361 if (c
->mina
> 0 && c
->maxa
> 0) {
362 if (c
->maxa
< (float)*w
/ *h
)
363 *w
= *h
* c
->maxa
+ 0.5;
364 else if (c
->mina
< (float)*h
/ *w
)
365 *h
= *w
* c
->mina
+ 0.5;
367 if (baseismin
) { /* increment calculation requires this */
371 /* adjust for increment value */
376 /* restore base dimensions */
377 *w
= MAX(*w
+ c
->basew
, c
->minw
);
378 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
380 *w
= MIN(*w
, c
->maxw
);
382 *h
= MIN(*h
, c
->maxh
);
384 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
392 else for (m
= mons
; m
; m
= m
->next
)
397 } else for (m
= mons
; m
; m
= m
->next
)
402 arrangemon(Monitor
*m
)
404 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
405 if (m
->lt
[m
->sellt
]->arrange
)
406 m
->lt
[m
->sellt
]->arrange(m
);
412 c
->next
= c
->mon
->clients
;
417 attachstack(Client
*c
)
419 c
->snext
= c
->mon
->stack
;
424 buttonpress(XEvent
*e
)
426 unsigned int i
, x
, click
;
430 XButtonPressedEvent
*ev
= &e
->xbutton
;
433 /* focus monitor if necessary */
434 if ((m
= wintomon(ev
->window
)) && m
!= selmon
) {
435 unfocus(selmon
->sel
, 1);
439 if (ev
->window
== selmon
->barwin
) {
443 while (ev
->x
>= x
&& ++i
< LENGTH(tags
));
444 if (i
< LENGTH(tags
)) {
447 } else if (ev
->x
< x
+ blw
)
449 else if (ev
->x
> selmon
->ww
- TEXTW(stext
))
450 click
= ClkStatusText
;
453 } else if ((c
= wintoclient(ev
->window
))) {
455 click
= ClkClientWin
;
457 for (i
= 0; i
< LENGTH(buttons
); i
++)
458 if (click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
459 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
460 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
466 xerrorxlib
= XSetErrorHandler(xerrorstart
);
467 /* this causes an error if some other window manager is running */
468 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
470 XSetErrorHandler(xerror
);
478 Layout foo
= { "", NULL
};
483 selmon
->lt
[selmon
->sellt
] = &foo
;
484 for (m
= mons
; m
; m
= m
->next
)
486 unmanage(m
->stack
, 0);
487 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
490 for (i
= 0; i
< CurLast
; i
++)
491 drw_cur_free(drw
, cursor
[i
]);
492 for (i
= 0; i
< SchemeLast
; i
++) {
493 drw_clr_free(scheme
[i
].border
);
494 drw_clr_free(scheme
[i
].bg
);
495 drw_clr_free(scheme
[i
].fg
);
499 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
500 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
504 cleanupmon(Monitor
*mon
)
511 for (m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
514 XUnmapWindow(dpy
, mon
->barwin
);
515 XDestroyWindow(dpy
, mon
->barwin
);
520 clearurgent(Client
*c
)
525 if (!(wmh
= XGetWMHints(dpy
, c
->win
)))
527 wmh
->flags
&= ~XUrgencyHint
;
528 XSetWMHints(dpy
, c
->win
, wmh
);
533 clientmessage(XEvent
*e
)
535 XClientMessageEvent
*cme
= &e
->xclient
;
536 Client
*c
= wintoclient(cme
->window
);
540 if (cme
->message_type
== netatom
[NetWMState
]) {
541 if (cme
->data
.l
[1] == netatom
[NetWMFullscreen
] || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
542 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
543 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
544 } else if (cme
->message_type
== netatom
[NetActiveWindow
]) {
546 c
->mon
->seltags
^= 1;
547 c
->mon
->tagset
[c
->mon
->seltags
] = c
->tags
;
558 ce
.type
= ConfigureNotify
;
566 ce
.border_width
= c
->bw
;
568 ce
.override_redirect
= False
;
569 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
573 configurenotify(XEvent
*e
)
576 XConfigureEvent
*ev
= &e
->xconfigure
;
579 /* TODO: updategeom handling sucks, needs to be simplified */
580 if (ev
->window
== root
) {
581 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
584 if (updategeom() || dirty
) {
585 drw_resize(drw
, sw
, bh
);
587 for (m
= mons
; m
; m
= m
->next
)
588 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
596 configurerequest(XEvent
*e
)
600 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
603 if ((c
= wintoclient(ev
->window
))) {
604 if (ev
->value_mask
& CWBorderWidth
)
605 c
->bw
= ev
->border_width
;
606 else if (c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
608 if (ev
->value_mask
& CWX
) {
610 c
->x
= m
->mx
+ ev
->x
;
612 if (ev
->value_mask
& CWY
) {
614 c
->y
= m
->my
+ ev
->y
;
616 if (ev
->value_mask
& CWWidth
) {
620 if (ev
->value_mask
& CWHeight
) {
624 if ((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
625 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
626 if ((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
627 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
628 if ((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
631 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
637 wc
.width
= ev
->width
;
638 wc
.height
= ev
->height
;
639 wc
.border_width
= ev
->border_width
;
640 wc
.sibling
= ev
->above
;
641 wc
.stack_mode
= ev
->detail
;
642 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
652 m
= ecalloc(1, sizeof(Monitor
));
653 m
->tagset
[0] = m
->tagset
[1] = 1;
655 m
->nmaster
= nmaster
;
656 m
->showbar
= showbar
;
658 m
->lt
[0] = &layouts
[0];
659 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
660 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
665 destroynotify(XEvent
*e
)
668 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
670 if ((c
= wintoclient(ev
->window
)))
679 for (tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
684 detachstack(Client
*c
)
688 for (tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
691 if (c
== c
->mon
->sel
) {
692 for (t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
703 if (!(m
= selmon
->next
))
705 } else if (selmon
== mons
)
706 for (m
= mons
; m
->next
; m
= m
->next
);
708 for (m
= mons
; m
->next
!= selmon
; m
= m
->next
);
716 unsigned int i
, occ
= 0, urg
= 0;
719 dx
= (drw
->fonts
[0]->ascent
+ drw
->fonts
[0]->descent
+ 2) / 4;
721 for (c
= m
->clients
; c
; c
= c
->next
) {
727 for (i
= 0; i
< LENGTH(tags
); i
++) {
729 drw_setscheme(drw
, m
->tagset
[m
->seltags
] & 1 << i
? &scheme
[SchemeSel
] : &scheme
[SchemeNorm
]);
730 drw_text(drw
, x
, 0, w
, bh
, tags
[i
], urg
& 1 << i
);
731 drw_rect(drw
, x
+ 1, 1, dx
, dx
, m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
732 occ
& 1 << i
, urg
& 1 << i
);
735 w
= blw
= TEXTW(m
->ltsymbol
);
736 drw_setscheme(drw
, &scheme
[SchemeNorm
]);
737 drw_text(drw
, x
, 0, w
, bh
, m
->ltsymbol
, 0);
740 if (m
== selmon
) { /* status is only drawn on selected monitor */
747 drw_text(drw
, x
, 0, w
, bh
, stext
, 0);
750 if ((w
= x
- xx
) > bh
) {
753 drw_setscheme(drw
, m
== selmon
? &scheme
[SchemeSel
] : &scheme
[SchemeNorm
]);
754 drw_text(drw
, x
, 0, w
, bh
, m
->sel
->name
, 0);
755 drw_rect(drw
, x
+ 1, 1, dx
, dx
, m
->sel
->isfixed
, m
->sel
->isfloating
, 0);
757 drw_setscheme(drw
, &scheme
[SchemeNorm
]);
758 drw_rect(drw
, x
, 0, w
, bh
, 1, 0, 1);
761 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
);
769 for (m
= mons
; m
; m
= m
->next
)
774 enternotify(XEvent
*e
)
778 XCrossingEvent
*ev
= &e
->xcrossing
;
780 if ((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
782 c
= wintoclient(ev
->window
);
783 m
= c
? c
->mon
: wintomon(ev
->window
);
785 unfocus(selmon
->sel
, 1);
787 } else if (!c
|| c
== selmon
->sel
)
796 XExposeEvent
*ev
= &e
->xexpose
;
798 if (ev
->count
== 0 && (m
= wintomon(ev
->window
)))
805 if (!c
|| !ISVISIBLE(c
))
806 for (c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
807 /* was if (selmon->sel) */
808 if (selmon
->sel
&& selmon
->sel
!= c
)
809 unfocus(selmon
->sel
, 0);
811 if (c
->mon
!= selmon
)
818 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeSel
].border
->pix
);
821 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
822 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
828 /* there are some broken focus acquiring clients */
832 XFocusChangeEvent
*ev
= &e
->xfocus
;
834 if (selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
835 setfocus(selmon
->sel
);
839 focusmon(const Arg
*arg
)
845 if ((m
= dirtomon(arg
->i
)) == selmon
)
847 unfocus(selmon
->sel
, 0); /* s/1/0/ fixes input focus issues
848 in gedit and anjuta */
854 focusstack(const Arg
*arg
)
856 Client
*c
= NULL
, *i
;
861 for (c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
863 for (c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
865 for (i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
869 for (; i
; i
= i
->next
)
880 getatomprop(Client
*c
, Atom prop
)
884 unsigned char *p
= NULL
;
885 Atom da
, atom
= None
;
887 if (XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
888 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
896 getrootptr(int *x
, int *y
)
902 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
910 unsigned char *p
= NULL
;
911 unsigned long n
, extra
;
914 if (XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
915 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
924 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
)
930 if (!text
|| size
== 0)
933 XGetTextProperty(dpy
, w
, &name
, atom
);
936 if (name
.encoding
== XA_STRING
)
937 strncpy(text
, (char *)name
.value
, size
- 1);
939 if (XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
940 strncpy(text
, *list
, size
- 1);
941 XFreeStringList(list
);
944 text
[size
- 1] = '\0';
950 grabbuttons(Client
*c
, int focused
)
955 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
956 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
958 for (i
= 0; i
< LENGTH(buttons
); i
++)
959 if (buttons
[i
].click
== ClkClientWin
)
960 for (j
= 0; j
< LENGTH(modifiers
); j
++)
961 XGrabButton(dpy
, buttons
[i
].button
,
962 buttons
[i
].mask
| modifiers
[j
],
963 c
->win
, False
, BUTTONMASK
,
964 GrabModeAsync
, GrabModeSync
, None
, None
);
966 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
967 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
977 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
980 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
981 for (i
= 0; i
< LENGTH(keys
); i
++)
982 if ((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
983 for (j
= 0; j
< LENGTH(modifiers
); j
++)
984 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
985 True
, GrabModeAsync
, GrabModeAsync
);
990 incnmaster(const Arg
*arg
)
992 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
998 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
)
1001 if (unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
1002 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
1006 #endif /* XINERAMA */
1016 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1017 for (i
= 0; i
< LENGTH(keys
); i
++)
1018 if (keysym
== keys
[i
].keysym
1019 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1021 keys
[i
].func(&(keys
[i
].arg
));
1025 killclient(const Arg
*arg
)
1029 if (!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1031 XSetErrorHandler(xerrordummy
);
1032 XSetCloseDownMode(dpy
, DestroyAll
);
1033 XKillClient(dpy
, selmon
->sel
->win
);
1035 XSetErrorHandler(xerror
);
1041 manage(Window w
, XWindowAttributes
*wa
)
1043 Client
*c
, *t
= NULL
;
1044 Window trans
= None
;
1047 c
= ecalloc(1, sizeof(Client
));
1050 if (XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1058 c
->x
= c
->oldx
= wa
->x
;
1059 c
->y
= c
->oldy
= wa
->y
;
1060 c
->w
= c
->oldw
= wa
->width
;
1061 c
->h
= c
->oldh
= wa
->height
;
1062 c
->oldbw
= wa
->border_width
;
1064 if (c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1065 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1066 if (c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1067 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1068 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1069 /* only fix client y-offset, if the client center might cover the bar */
1070 c
->y
= MAX(c
->y
, ((c
->mon
->by
== c
->mon
->my
) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1071 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1074 wc
.border_width
= c
->bw
;
1075 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1076 XSetWindowBorder(dpy
, w
, scheme
[SchemeNorm
].border
->pix
);
1077 configure(c
); /* propagates border_width, if size doesn't change */
1078 updatewindowtype(c
);
1081 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1084 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1086 XRaiseWindow(dpy
, c
->win
);
1089 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1090 (unsigned char *) &(c
->win
), 1);
1091 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1092 setclientstate(c
, NormalState
);
1093 if (c
->mon
== selmon
)
1094 unfocus(selmon
->sel
, 0);
1097 XMapWindow(dpy
, c
->win
);
1102 mappingnotify(XEvent
*e
)
1104 XMappingEvent
*ev
= &e
->xmapping
;
1106 XRefreshKeyboardMapping(ev
);
1107 if (ev
->request
== MappingKeyboard
)
1112 maprequest(XEvent
*e
)
1114 static XWindowAttributes wa
;
1115 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1117 if (!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1119 if (wa
.override_redirect
)
1121 if (!wintoclient(ev
->window
))
1122 manage(ev
->window
, &wa
);
1131 for (c
= m
->clients
; c
; c
= c
->next
)
1134 if (n
> 0) /* override layout symbol */
1135 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1136 for (c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1137 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, 0);
1141 motionnotify(XEvent
*e
)
1143 static Monitor
*mon
= NULL
;
1145 XMotionEvent
*ev
= &e
->xmotion
;
1147 if (ev
->window
!= root
)
1149 if ((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1150 unfocus(selmon
->sel
, 1);
1158 movemouse(const Arg
*arg
)
1160 int x
, y
, ocx
, ocy
, nx
, ny
;
1166 if (!(c
= selmon
->sel
))
1168 if (c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1173 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1174 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1176 if (!getrootptr(&x
, &y
))
1179 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1181 case ConfigureRequest
:
1184 handler
[ev
.type
](&ev
);
1187 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1189 lasttime
= ev
.xmotion
.time
;
1191 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1192 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1193 if (nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1194 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1195 if (abs(selmon
->wx
- nx
) < snap
)
1197 else if (abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1198 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1199 if (abs(selmon
->wy
- ny
) < snap
)
1201 else if (abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1202 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1203 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1204 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1205 togglefloating(NULL
);
1207 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1208 resize(c
, nx
, ny
, c
->w
, c
->h
, 1);
1211 } while (ev
.type
!= ButtonRelease
);
1212 XUngrabPointer(dpy
, CurrentTime
);
1213 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1221 nexttiled(Client
*c
)
1223 for (; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1237 propertynotify(XEvent
*e
)
1241 XPropertyEvent
*ev
= &e
->xproperty
;
1243 if ((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1245 else if (ev
->state
== PropertyDelete
)
1246 return; /* ignore */
1247 else if ((c
= wintoclient(ev
->window
))) {
1250 case XA_WM_TRANSIENT_FOR
:
1251 if (!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1252 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1255 case XA_WM_NORMAL_HINTS
:
1263 if (ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1265 if (c
== c
->mon
->sel
)
1268 if (ev
->atom
== netatom
[NetWMWindowType
])
1269 updatewindowtype(c
);
1274 quit(const Arg
*arg
)
1280 recttomon(int x
, int y
, int w
, int h
)
1282 Monitor
*m
, *r
= selmon
;
1285 for (m
= mons
; m
; m
= m
->next
)
1286 if ((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1294 resize(Client
*c
, int x
, int y
, int w
, int h
, int interact
)
1296 if (applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1297 resizeclient(c
, x
, y
, w
, h
);
1301 resizeclient(Client
*c
, int x
, int y
, int w
, int h
)
1305 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1306 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1307 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1308 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1309 wc
.border_width
= c
->bw
;
1310 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1316 resizemouse(const Arg
*arg
)
1318 int ocx
, ocy
, nw
, nh
;
1324 if (!(c
= selmon
->sel
))
1326 if (c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1331 if (XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1332 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1334 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1336 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1338 case ConfigureRequest
:
1341 handler
[ev
.type
](&ev
);
1344 if ((ev
.xmotion
.time
- lasttime
) <= (1000 / 60))
1346 lasttime
= ev
.xmotion
.time
;
1348 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1349 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1350 if (c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1351 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1353 if (!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1354 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1355 togglefloating(NULL
);
1357 if (!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1358 resize(c
, c
->x
, c
->y
, nw
, nh
, 1);
1361 } while (ev
.type
!= ButtonRelease
);
1362 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1363 XUngrabPointer(dpy
, CurrentTime
);
1364 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1365 if ((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1382 if (m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1383 XRaiseWindow(dpy
, m
->sel
->win
);
1384 if (m
->lt
[m
->sellt
]->arrange
) {
1385 wc
.stack_mode
= Below
;
1386 wc
.sibling
= m
->barwin
;
1387 for (c
= m
->stack
; c
; c
= c
->snext
)
1388 if (!c
->isfloating
&& ISVISIBLE(c
)) {
1389 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1390 wc
.sibling
= c
->win
;
1394 while (XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1401 /* main event loop */
1403 while (running
&& !XNextEvent(dpy
, &ev
))
1404 if (handler
[ev
.type
])
1405 handler
[ev
.type
](&ev
); /* call handler */
1411 unsigned int i
, num
;
1412 Window d1
, d2
, *wins
= NULL
;
1413 XWindowAttributes wa
;
1415 if (XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1416 for (i
= 0; i
< num
; i
++) {
1417 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1418 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1420 if (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1421 manage(wins
[i
], &wa
);
1423 for (i
= 0; i
< num
; i
++) { /* now the transients */
1424 if (!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1426 if (XGetTransientForHint(dpy
, wins
[i
], &d1
)
1427 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1428 manage(wins
[i
], &wa
);
1436 sendmon(Client
*c
, Monitor
*m
)
1444 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1452 setclientstate(Client
*c
, long state
)
1454 long data
[] = { state
, None
};
1456 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1457 PropModeReplace
, (unsigned char *)data
, 2);
1461 sendevent(Client
*c
, Atom proto
)
1468 if (XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1469 while (!exists
&& n
--)
1470 exists
= protocols
[n
] == proto
;
1474 ev
.type
= ClientMessage
;
1475 ev
.xclient
.window
= c
->win
;
1476 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1477 ev
.xclient
.format
= 32;
1478 ev
.xclient
.data
.l
[0] = proto
;
1479 ev
.xclient
.data
.l
[1] = CurrentTime
;
1480 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1488 if (!c
->neverfocus
) {
1489 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1490 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1491 XA_WINDOW
, 32, PropModeReplace
,
1492 (unsigned char *) &(c
->win
), 1);
1494 sendevent(c
, wmatom
[WMTakeFocus
]);
1498 setfullscreen(Client
*c
, int fullscreen
)
1500 if (fullscreen
&& !c
->isfullscreen
) {
1501 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1502 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1503 c
->isfullscreen
= 1;
1504 c
->oldstate
= c
->isfloating
;
1508 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1509 XRaiseWindow(dpy
, c
->win
);
1510 } else if (!fullscreen
&& c
->isfullscreen
){
1511 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1512 PropModeReplace
, (unsigned char*)0, 0);
1513 c
->isfullscreen
= 0;
1514 c
->isfloating
= c
->oldstate
;
1520 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1526 setlayout(const Arg
*arg
)
1528 if (!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1531 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1532 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1539 /* arg > 1.0 will set mfact absolutly */
1541 setmfact(const Arg
*arg
)
1545 if (!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1547 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1548 if (f
< 0.1 || f
> 0.9)
1557 XSetWindowAttributes wa
;
1559 /* clean up any zombies immediately */
1563 screen
= DefaultScreen(dpy
);
1564 sw
= DisplayWidth(dpy
, screen
);
1565 sh
= DisplayHeight(dpy
, screen
);
1566 root
= RootWindow(dpy
, screen
);
1568 drw
= drw_create(dpy
, screen
, root
, sw
, sh
, visual
, depth
, cmap
);
1569 drw_load_fonts(drw
, fonts
, LENGTH(fonts
));
1570 if (!drw
->fontcount
)
1571 die("no fonts could be loaded.\n");
1572 bh
= drw
->fonts
[0]->h
+ 2;
1575 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1576 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1577 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1578 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1579 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1580 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1581 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1582 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1583 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1584 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1585 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1586 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1588 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1589 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1590 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1591 /* init appearance */
1592 scheme
[SchemeNorm
].border
= drw_clr_create(drw
, normbordercolor
, borderalpha
);
1593 scheme
[SchemeNorm
].bg
= drw_clr_create(drw
, normbgcolor
, baralpha
);
1594 scheme
[SchemeNorm
].fg
= drw_clr_create(drw
, normfgcolor
, OPAQUE
);
1595 scheme
[SchemeSel
].border
= drw_clr_create(drw
, selbordercolor
, borderalpha
);
1596 scheme
[SchemeSel
].bg
= drw_clr_create(drw
, selbgcolor
, baralpha
);
1597 scheme
[SchemeSel
].fg
= drw_clr_create(drw
, selfgcolor
, OPAQUE
);
1601 /* EWMH support per view */
1602 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1603 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1604 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1605 /* select for events */
1606 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1607 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
|PointerMotionMask
1608 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1609 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1610 XSelectInput(dpy
, root
, wa
.event_mask
);
1621 /* show clients top down */
1622 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1623 if ((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1624 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, 0);
1627 /* hide clients bottom up */
1629 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1636 if (signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1637 die("can't install SIGCHLD handler:");
1638 while (0 < waitpid(-1, NULL
, WNOHANG
));
1642 spawn(const Arg
*arg
)
1644 if (arg
->v
== dmenucmd
)
1645 dmenumon
[0] = '0' + selmon
->num
;
1648 close(ConnectionNumber(dpy
));
1650 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1651 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1660 if (selmon
->sel
&& arg
->ui
& TAGMASK
) {
1661 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1668 tagmon(const Arg
*arg
)
1670 if (!selmon
->sel
|| !mons
->next
)
1672 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1678 unsigned int i
, n
, h
, mw
, my
, ty
;
1681 for (n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1686 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1689 for (i
= my
= ty
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1690 if (i
< m
->nmaster
) {
1691 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
);
1692 resize(c
, m
->wx
, m
->wy
+ my
, mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1695 h
= (m
->wh
- ty
) / (n
- i
);
1696 resize(c
, m
->wx
+ mw
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
), h
- (2*c
->bw
), 0);
1702 togglebar(const Arg
*arg
)
1704 selmon
->showbar
= !selmon
->showbar
;
1705 updatebarpos(selmon
);
1706 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1711 togglefloating(const Arg
*arg
)
1715 if (selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1717 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1718 if (selmon
->sel
->isfloating
)
1719 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1720 selmon
->sel
->w
, selmon
->sel
->h
, 0);
1725 toggletag(const Arg
*arg
)
1727 unsigned int newtags
;
1731 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1733 selmon
->sel
->tags
= newtags
;
1740 toggleview(const Arg
*arg
)
1742 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1745 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1752 unfocus(Client
*c
, int setfocus
)
1757 XSetWindowBorder(dpy
, c
->win
, scheme
[SchemeNorm
].border
->pix
);
1759 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1760 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1765 unmanage(Client
*c
, int destroyed
)
1767 Monitor
*m
= c
->mon
;
1770 /* The server grab construct avoids race conditions. */
1774 wc
.border_width
= c
->oldbw
;
1776 XSetErrorHandler(xerrordummy
);
1777 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1778 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1779 setclientstate(c
, WithdrawnState
);
1781 XSetErrorHandler(xerror
);
1791 unmapnotify(XEvent
*e
)
1794 XUnmapEvent
*ev
= &e
->xunmap
;
1796 if ((c
= wintoclient(ev
->window
))) {
1798 setclientstate(c
, WithdrawnState
);
1808 XSetWindowAttributes wa
= {
1809 .override_redirect
= True
,
1810 .background_pixel
= 0,
1813 .event_mask
= ButtonPressMask
|ExposureMask
1815 for (m
= mons
; m
; m
= m
->next
) {
1818 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, depth
,
1819 InputOutput
, visual
,
1820 CWOverrideRedirect
|CWBackPixel
|CWBorderPixel
|CWColormap
|CWEventMask
, &wa
);
1821 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
1822 XMapRaised(dpy
, m
->barwin
);
1827 updatebarpos(Monitor
*m
)
1833 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1834 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1845 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1846 for (m
= mons
; m
; m
= m
->next
)
1847 for (c
= m
->clients
; c
; c
= c
->next
)
1848 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
1849 XA_WINDOW
, 32, PropModeAppend
,
1850 (unsigned char *) &(c
->win
), 1);
1859 if (XineramaIsActive(dpy
)) {
1863 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1864 XineramaScreenInfo
*unique
= NULL
;
1866 for (n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1867 /* only consider unique geometries as separate screens */
1868 unique
= ecalloc(nn
, sizeof(XineramaScreenInfo
));
1869 for (i
= 0, j
= 0; i
< nn
; i
++)
1870 if (isuniquegeom(unique
, j
, &info
[i
]))
1871 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1875 for (i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1876 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1878 m
->next
= createmon();
1882 for (i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1884 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1885 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1889 m
->mx
= m
->wx
= unique
[i
].x_org
;
1890 m
->my
= m
->wy
= unique
[i
].y_org
;
1891 m
->mw
= m
->ww
= unique
[i
].width
;
1892 m
->mh
= m
->wh
= unique
[i
].height
;
1896 /* less monitors available nn < n */
1897 for (i
= nn
; i
< n
; i
++) {
1898 for (m
= mons
; m
&& m
->next
; m
= m
->next
);
1899 while (m
->clients
) {
1902 m
->clients
= c
->next
;
1915 #endif /* XINERAMA */
1916 /* default monitor setup */
1920 if (mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1922 mons
->mw
= mons
->ww
= sw
;
1923 mons
->mh
= mons
->wh
= sh
;
1929 selmon
= wintomon(root
);
1935 updatenumlockmask(void)
1938 XModifierKeymap
*modmap
;
1941 modmap
= XGetModifierMapping(dpy
);
1942 for (i
= 0; i
< 8; i
++)
1943 for (j
= 0; j
< modmap
->max_keypermod
; j
++)
1944 if (modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1945 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1946 numlockmask
= (1 << i
);
1947 XFreeModifiermap(modmap
);
1951 updatesizehints(Client
*c
)
1956 if (!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1957 /* size is uninitialized, ensure that size.flags aren't used */
1959 if (size
.flags
& PBaseSize
) {
1960 c
->basew
= size
.base_width
;
1961 c
->baseh
= size
.base_height
;
1962 } else if (size
.flags
& PMinSize
) {
1963 c
->basew
= size
.min_width
;
1964 c
->baseh
= size
.min_height
;
1966 c
->basew
= c
->baseh
= 0;
1967 if (size
.flags
& PResizeInc
) {
1968 c
->incw
= size
.width_inc
;
1969 c
->inch
= size
.height_inc
;
1971 c
->incw
= c
->inch
= 0;
1972 if (size
.flags
& PMaxSize
) {
1973 c
->maxw
= size
.max_width
;
1974 c
->maxh
= size
.max_height
;
1976 c
->maxw
= c
->maxh
= 0;
1977 if (size
.flags
& PMinSize
) {
1978 c
->minw
= size
.min_width
;
1979 c
->minh
= size
.min_height
;
1980 } else if (size
.flags
& PBaseSize
) {
1981 c
->minw
= size
.base_width
;
1982 c
->minh
= size
.base_height
;
1984 c
->minw
= c
->minh
= 0;
1985 if (size
.flags
& PAspect
) {
1986 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1987 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1989 c
->maxa
= c
->mina
= 0.0;
1990 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1991 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1995 updatetitle(Client
*c
)
1997 if (!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1998 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1999 if (c
->name
[0] == '\0') /* hack to mark broken clients */
2000 strcpy(c
->name
, broken
);
2006 if (!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
2007 strcpy(stext
, "dwm-"VERSION
);
2012 updatewindowtype(Client
*c
)
2014 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
2015 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
2017 if (state
== netatom
[NetWMFullscreen
])
2018 setfullscreen(c
, 1);
2019 if (wtype
== netatom
[NetWMWindowTypeDialog
])
2024 updatewmhints(Client
*c
)
2028 if ((wmh
= XGetWMHints(dpy
, c
->win
))) {
2029 if (c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2030 wmh
->flags
&= ~XUrgencyHint
;
2031 XSetWMHints(dpy
, c
->win
, wmh
);
2033 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? 1 : 0;
2034 if (wmh
->flags
& InputHint
)
2035 c
->neverfocus
= !wmh
->input
;
2043 view(const Arg
*arg
)
2045 if ((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2047 selmon
->seltags
^= 1; /* toggle sel tagset */
2048 if (arg
->ui
& TAGMASK
)
2049 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2055 wintoclient(Window w
)
2060 for (m
= mons
; m
; m
= m
->next
)
2061 for (c
= m
->clients
; c
; c
= c
->next
)
2074 if (w
== root
&& getrootptr(&x
, &y
))
2075 return recttomon(x
, y
, 1, 1);
2076 for (m
= mons
; m
; m
= m
->next
)
2079 if ((c
= wintoclient(w
)))
2084 /* There's no way to check accesses to destroyed windows, thus those cases are
2085 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2086 * default error handler, which may call exit. */
2088 xerror(Display
*dpy
, XErrorEvent
*ee
)
2090 if (ee
->error_code
== BadWindow
2091 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2092 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2093 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2094 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2095 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2096 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2097 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2098 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2100 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2101 ee
->request_code
, ee
->error_code
);
2102 return xerrorxlib(dpy
, ee
); /* may call exit */
2106 xerrordummy(Display
*dpy
, XErrorEvent
*ee
)
2111 /* Startup Error handler to check if another window manager
2112 * is already running. */
2114 xerrorstart(Display
*dpy
, XErrorEvent
*ee
)
2116 die("dwm: another window manager is already running\n");
2124 XRenderPictFormat
*fmt
;
2133 long masks
= VisualScreenMask
| VisualDepthMask
| VisualClassMask
;
2135 infos
= XGetVisualInfo(dpy
, masks
, &tpl
, &nitems
);
2137 for(i
= 0; i
< nitems
; i
++) {
2138 fmt
= XRenderFindVisualFormat(dpy
, infos
[i
].visual
);
2139 if (fmt
->type
== PictTypeDirect
&& fmt
->direct
.alphaMask
) {
2140 visual
= infos
[i
].visual
;
2141 depth
= infos
[i
].depth
;
2142 cmap
= XCreateColormap(dpy
, root
, visual
, AllocNone
);
2151 visual
= DefaultVisual(dpy
, screen
);
2152 depth
= DefaultDepth(dpy
, screen
);
2153 cmap
= DefaultColormap(dpy
, screen
);
2158 zoom(const Arg
*arg
)
2160 Client
*c
= selmon
->sel
;
2162 if (!selmon
->lt
[selmon
->sellt
]->arrange
2163 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2165 if (c
== nexttiled(selmon
->clients
))
2166 if (!c
|| !(c
= nexttiled(c
->next
)))
2172 main(int argc
, char *argv
[])
2174 if (argc
== 2 && !strcmp("-v", argv
[1]))
2175 die("dwm-"VERSION
"\n");
2177 die("usage: dwm [-v]\n");
2178 if (!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2179 fputs("warning: no locale support\n", stderr
);
2180 if (!(dpy
= XOpenDisplay(NULL
)))
2181 die("dwm: cannot open display\n");
2188 return EXIT_SUCCESS
;