Xinqi Bao's Git
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>
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_font_getexts_width(drw->font, X, strlen(X)) + drw->font->h)
60 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
61 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
62 enum { NetSupported
, NetWMName
, NetWMState
,
63 NetWMFullscreen
, NetActiveWindow
, NetWMWindowType
,
64 NetWMWindowTypeDialog
, NetClientList
, NetLast
}; /* EWMH atoms */
65 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, WMLast
}; /* default atoms */
66 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
67 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
80 void (*func
)(const Arg
*arg
);
84 typedef struct Monitor Monitor
;
85 typedef struct Client Client
;
90 int oldx
, oldy
, oldw
, oldh
;
91 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
94 Bool isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
;
104 void (*func
)(const Arg
*);
110 void (*arrange
)(Monitor
*);
118 int by
; /* bar geometry */
119 int mx
, my
, mw
, mh
; /* screen size */
120 int wx
, wy
, ww
, wh
; /* window area */
121 unsigned int seltags
;
123 unsigned int tagset
[2];
136 const char *instance
;
143 /* function declarations */
144 static void applyrules(Client
*c
);
145 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
146 static void arrange(Monitor
*m
);
147 static void arrangemon(Monitor
*m
);
148 static void attach(Client
*c
);
149 static void attachstack(Client
*c
);
150 static void buttonpress(XEvent
*e
);
151 static void checkotherwm(void);
152 static void cleanup(void);
153 static void cleanupmon(Monitor
*mon
);
154 static void clearurgent(Client
*c
);
155 static void clientmessage(XEvent
*e
);
156 static void configure(Client
*c
);
157 static void configurenotify(XEvent
*e
);
158 static void configurerequest(XEvent
*e
);
159 static Monitor
*createmon(void);
160 static void destroynotify(XEvent
*e
);
161 static void detach(Client
*c
);
162 static void detachstack(Client
*c
);
163 static Monitor
*dirtomon(int dir
);
164 static void drawbar(Monitor
*m
);
165 static void drawbars(void);
166 static void enternotify(XEvent
*e
);
167 static void expose(XEvent
*e
);
168 static void focus(Client
*c
);
169 static void focusin(XEvent
*e
);
170 static void focusmon(const Arg
*arg
);
171 static void focusstack(const Arg
*arg
);
172 static Bool
getrootptr(int *x
, int *y
);
173 static long getstate(Window w
);
174 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
175 static void grabbuttons(Client
*c
, Bool focused
);
176 static void grabkeys(void);
177 static void incnmaster(const Arg
*arg
);
178 static void keypress(XEvent
*e
);
179 static void killclient(const Arg
*arg
);
180 static void manage(Window w
, XWindowAttributes
*wa
);
181 static void mappingnotify(XEvent
*e
);
182 static void maprequest(XEvent
*e
);
183 static void monocle(Monitor
*m
);
184 static void motionnotify(XEvent
*e
);
185 static void movemouse(const Arg
*arg
);
186 static Client
*nexttiled(Client
*c
);
187 static void pop(Client
*);
188 static void propertynotify(XEvent
*e
);
189 static void quit(const Arg
*arg
);
190 static Monitor
*recttomon(int x
, int y
, int w
, int h
);
191 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
192 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
193 static void resizemouse(const Arg
*arg
);
194 static void restack(Monitor
*m
);
195 static void run(void);
196 static void scan(void);
197 static Bool
sendevent(Client
*c
, Atom proto
);
198 static void sendmon(Client
*c
, Monitor
*m
);
199 static void setclientstate(Client
*c
, long state
);
200 static void setfocus(Client
*c
);
201 static void setfullscreen(Client
*c
, Bool fullscreen
);
202 static void setlayout(const Arg
*arg
);
203 static void setmfact(const Arg
*arg
);
204 static void setup(void);
205 static void showhide(Client
*c
);
206 static void sigchld(int unused
);
207 static void spawn(const Arg
*arg
);
208 static void tag(const Arg
*arg
);
209 static void tagmon(const Arg
*arg
);
210 static void tile(Monitor
*);
211 static void togglebar(const Arg
*arg
);
212 static void togglefloating(const Arg
*arg
);
213 static void toggletag(const Arg
*arg
);
214 static void toggleview(const Arg
*arg
);
215 static void unfocus(Client
*c
, Bool setfocus
);
216 static void unmanage(Client
*c
, Bool destroyed
);
217 static void unmapnotify(XEvent
*e
);
218 static Bool
updategeom(void);
219 static void updatebarpos(Monitor
*m
);
220 static void updatebars(void);
221 static void updateclientlist(void);
222 static void updatenumlockmask(void);
223 static void updatesizehints(Client
*c
);
224 static void updatestatus(void);
225 static void updatewindowtype(Client
*c
);
226 static void updatetitle(Client
*c
);
227 static void updatewmhints(Client
*c
);
228 static void view(const Arg
*arg
);
229 static Client
*wintoclient(Window w
);
230 static Monitor
*wintomon(Window w
);
231 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
232 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
233 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
234 static void zoom(const Arg
*arg
);
237 static const char broken
[] = "broken";
238 static char stext
[256];
240 static int sw
, sh
; /* X display screen geometry width, height */
241 static int bh
, blw
= 0; /* bar geometry */
242 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
243 static unsigned int numlockmask
= 0;
244 static void (*handler
[LASTEvent
]) (XEvent
*) = {
245 [ButtonPress
] = buttonpress
,
246 [ClientMessage
] = clientmessage
,
247 [ConfigureRequest
] = configurerequest
,
248 [ConfigureNotify
] = configurenotify
,
249 [DestroyNotify
] = destroynotify
,
250 [EnterNotify
] = enternotify
,
253 [KeyPress
] = keypress
,
254 [MappingNotify
] = mappingnotify
,
255 [MapRequest
] = maprequest
,
256 [MotionNotify
] = motionnotify
,
257 [PropertyNotify
] = propertynotify
,
258 [UnmapNotify
] = unmapnotify
260 static Atom wmatom
[WMLast
], netatom
[NetLast
];
261 static Bool running
= True
;
262 static Cur
*cursor
[CurLast
];
263 static Theme thmnorm
[ColLast
];
264 static Theme thmsel
[ColLast
];
268 static Monitor
*mons
, *selmon
;
271 /* configuration, allows nested code to access above variables */
274 /* compile-time check if all tags fit into an unsigned int bit array. */
275 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
277 /* function implementations */
279 applyrules(Client
*c
) {
280 const char *class, *instance
;
284 XClassHint ch
= { NULL
, NULL
};
287 c
->isfloating
= c
->tags
= 0;
288 XGetClassHint(dpy
, c
->win
, &ch
);
289 class = ch
.res_class
? ch
.res_class
: broken
;
290 instance
= ch
.res_name
? ch
.res_name
: broken
;
292 for(i
= 0; i
< LENGTH(rules
); i
++) {
294 if((!r
->title
|| strstr(c
->name
, r
->title
))
295 && (!r
->class || strstr(class, r
->class))
296 && (!r
->instance
|| strstr(instance
, r
->instance
)))
298 c
->isfloating
= r
->isfloating
;
300 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
309 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
313 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
317 /* set minimum possible */
325 if(*x
+ *w
+ 2 * c
->bw
< 0)
327 if(*y
+ *h
+ 2 * c
->bw
< 0)
331 if(*x
>= m
->wx
+ m
->ww
)
332 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
333 if(*y
>= m
->wy
+ m
->wh
)
334 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
335 if(*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
337 if(*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
344 if(resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
345 /* see last two sentences in ICCCM 4.1.2.3 */
346 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
347 if(!baseismin
) { /* temporarily remove base dimensions */
351 /* adjust for aspect limits */
352 if(c
->mina
> 0 && c
->maxa
> 0) {
353 if(c
->maxa
< (float)*w
/ *h
)
354 *w
= *h
* c
->maxa
+ 0.5;
355 else if(c
->mina
< (float)*h
/ *w
)
356 *h
= *w
* c
->mina
+ 0.5;
358 if(baseismin
) { /* increment calculation requires this */
362 /* adjust for increment value */
367 /* restore base dimensions */
368 *w
= MAX(*w
+ c
->basew
, c
->minw
);
369 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
371 *w
= MIN(*w
, c
->maxw
);
373 *h
= MIN(*h
, c
->maxh
);
375 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
379 arrange(Monitor
*m
) {
382 else for(m
= mons
; m
; m
= m
->next
)
387 } else for(m
= mons
; m
; m
= m
->next
)
392 arrangemon(Monitor
*m
) {
393 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
394 if(m
->lt
[m
->sellt
]->arrange
)
395 m
->lt
[m
->sellt
]->arrange(m
);
400 c
->next
= c
->mon
->clients
;
405 attachstack(Client
*c
) {
406 c
->snext
= c
->mon
->stack
;
411 buttonpress(XEvent
*e
) {
412 unsigned int i
, x
, click
;
416 XButtonPressedEvent
*ev
= &e
->xbutton
;
419 /* focus monitor if necessary */
420 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
421 unfocus(selmon
->sel
, True
);
425 if(ev
->window
== selmon
->barwin
) {
429 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
430 if(i
< LENGTH(tags
)) {
434 else if(ev
->x
< x
+ blw
)
436 else if(ev
->x
> selmon
->ww
- TEXTW(stext
))
437 click
= ClkStatusText
;
441 else if((c
= wintoclient(ev
->window
))) {
443 click
= ClkClientWin
;
445 for(i
= 0; i
< LENGTH(buttons
); i
++)
446 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
447 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
448 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
453 xerrorxlib
= XSetErrorHandler(xerrorstart
);
454 /* this causes an error if some other window manager is running */
455 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
457 XSetErrorHandler(xerror
);
464 Layout foo
= { "", NULL
};
468 selmon
->lt
[selmon
->sellt
] = &foo
;
469 for(m
= mons
; m
; m
= m
->next
)
471 unmanage(m
->stack
, False
);
472 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
475 drw_cur_free(drw
, cursor
[CurNormal
]);
476 drw_cur_free(drw
, cursor
[CurResize
]);
477 drw_cur_free(drw
, cursor
[CurMove
]);
478 drw_font_free(dpy
, fnt
);
479 drw_clr_free(thmnorm
->border
);
480 drw_clr_free(thmnorm
->bg
);
481 drw_clr_free(thmnorm
->fg
);
482 drw_clr_free(thmsel
->border
);
483 drw_clr_free(thmsel
->bg
);
484 drw_clr_free(thmsel
->fg
);
487 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
488 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
492 cleanupmon(Monitor
*mon
) {
498 for(m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
501 XUnmapWindow(dpy
, mon
->barwin
);
502 XDestroyWindow(dpy
, mon
->barwin
);
507 clearurgent(Client
*c
) {
511 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
513 wmh
->flags
&= ~XUrgencyHint
;
514 XSetWMHints(dpy
, c
->win
, wmh
);
519 clientmessage(XEvent
*e
) {
520 XClientMessageEvent
*cme
= &e
->xclient
;
521 Client
*c
= wintoclient(cme
->window
);
525 if(cme
->message_type
== netatom
[NetWMState
]) {
526 if(cme
->data
.l
[1] == netatom
[NetWMFullscreen
] || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
527 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
528 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
530 else if(cme
->message_type
== netatom
[NetActiveWindow
]) {
532 c
->mon
->seltags
^= 1;
533 c
->mon
->tagset
[c
->mon
->seltags
] = c
->tags
;
540 configure(Client
*c
) {
543 ce
.type
= ConfigureNotify
;
551 ce
.border_width
= c
->bw
;
553 ce
.override_redirect
= False
;
554 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
558 configurenotify(XEvent
*e
) {
560 XConfigureEvent
*ev
= &e
->xconfigure
;
563 // TODO: updategeom handling sucks, needs to be simplified
564 if(ev
->window
== root
) {
565 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
568 if(updategeom() || dirty
) {
569 drw_resize(drw
, sw
, bh
);
571 for(m
= mons
; m
; m
= m
->next
)
572 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
580 configurerequest(XEvent
*e
) {
583 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
586 if((c
= wintoclient(ev
->window
))) {
587 if(ev
->value_mask
& CWBorderWidth
)
588 c
->bw
= ev
->border_width
;
589 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
591 if(ev
->value_mask
& CWX
) {
593 c
->x
= m
->mx
+ ev
->x
;
595 if(ev
->value_mask
& CWY
) {
597 c
->y
= m
->my
+ ev
->y
;
599 if(ev
->value_mask
& CWWidth
) {
603 if(ev
->value_mask
& CWHeight
) {
607 if((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
608 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
609 if((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
610 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
611 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
614 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
622 wc
.width
= ev
->width
;
623 wc
.height
= ev
->height
;
624 wc
.border_width
= ev
->border_width
;
625 wc
.sibling
= ev
->above
;
626 wc
.stack_mode
= ev
->detail
;
627 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
636 if(!(m
= (Monitor
*)calloc(1, sizeof(Monitor
))))
637 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
638 m
->tagset
[0] = m
->tagset
[1] = 1;
640 m
->nmaster
= nmaster
;
641 m
->showbar
= showbar
;
643 m
->lt
[0] = &layouts
[0];
644 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
645 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
650 destroynotify(XEvent
*e
) {
652 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
654 if((c
= wintoclient(ev
->window
)))
662 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
667 detachstack(Client
*c
) {
670 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
673 if(c
== c
->mon
->sel
) {
674 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
684 if(!(m
= selmon
->next
))
687 else if(selmon
== mons
)
688 for(m
= mons
; m
->next
; m
= m
->next
);
690 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
695 drawbar(Monitor
*m
) {
697 unsigned int i
, occ
= 0, urg
= 0;
700 for(c
= m
->clients
; c
; c
= c
->next
) {
706 for(i
= 0; i
< LENGTH(tags
); i
++) {
708 drw_settheme(drw
, m
->tagset
[m
->seltags
] & 1 << i
? thmsel
: thmnorm
);
709 drw_text(drw
, x
, 0, w
, bh
, tags
[i
], urg
& 1 << i
);
710 drw_rect(drw
, x
, 0, w
, bh
, m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
711 occ
& 1 << i
, urg
& 1 << i
);
714 w
= blw
= TEXTW(m
->ltsymbol
);
715 drw_settheme(drw
, thmnorm
);
716 drw_text(drw
, x
, 0, w
, bh
, m
->ltsymbol
, 0);
719 if(m
== selmon
) { /* status is only drawn on selected monitor */
726 drw_text(drw
, x
, 0, w
, bh
, stext
, 0);
730 if((w
= x
- xx
) > bh
) {
733 drw_settheme(drw
, m
== selmon
? thmsel
: thmnorm
);
734 drw_text(drw
, x
, 0, w
, bh
, m
->sel
->name
, 0);
735 drw_rect(drw
, x
, 0, w
, bh
, m
->sel
->isfixed
, m
->sel
->isfloating
, 0);
738 drw_settheme(drw
, thmnorm
);
739 drw_text(drw
, x
, 0, w
, bh
, NULL
, 0);
742 drw_map(drw
, m
->barwin
, 0, 0, m
->ww
, bh
);
749 for(m
= mons
; m
; m
= m
->next
)
754 enternotify(XEvent
*e
) {
757 XCrossingEvent
*ev
= &e
->xcrossing
;
759 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
761 c
= wintoclient(ev
->window
);
762 m
= c
? c
->mon
: wintomon(ev
->window
);
764 unfocus(selmon
->sel
, True
);
767 else if(!c
|| c
== selmon
->sel
)
775 XExposeEvent
*ev
= &e
->xexpose
;
777 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
783 if(!c
|| !ISVISIBLE(c
))
784 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
785 /* was if(selmon->sel) */
786 if(selmon
->sel
&& selmon
->sel
!= c
)
787 unfocus(selmon
->sel
, False
);
795 grabbuttons(c
, True
);
796 XSetWindowBorder(dpy
, c
->win
, thmsel
->border
->rgb
);
800 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
801 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
808 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
809 XFocusChangeEvent
*ev
= &e
->xfocus
;
811 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
812 setfocus(selmon
->sel
);
816 focusmon(const Arg
*arg
) {
821 if((m
= dirtomon(arg
->i
)) == selmon
)
823 unfocus(selmon
->sel
, False
); /* s/True/False/ fixes input focus issues
824 in gedit and anjuta */
830 focusstack(const Arg
*arg
) {
831 Client
*c
= NULL
, *i
;
836 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
838 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
841 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
845 for(; i
; i
= i
->next
)
856 getatomprop(Client
*c
, Atom prop
) {
859 unsigned char *p
= NULL
;
860 Atom da
, atom
= None
;
862 if(XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
863 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
871 getrootptr(int *x
, int *y
) {
876 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
883 unsigned char *p
= NULL
;
884 unsigned long n
, extra
;
887 if(XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
888 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
897 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
902 if(!text
|| size
== 0)
905 XGetTextProperty(dpy
, w
, &name
, atom
);
908 if(name
.encoding
== XA_STRING
)
909 strncpy(text
, (char *)name
.value
, size
- 1);
911 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
912 strncpy(text
, *list
, size
- 1);
913 XFreeStringList(list
);
916 text
[size
- 1] = '\0';
922 grabbuttons(Client
*c
, Bool focused
) {
926 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
927 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
929 for(i
= 0; i
< LENGTH(buttons
); i
++)
930 if(buttons
[i
].click
== ClkClientWin
)
931 for(j
= 0; j
< LENGTH(modifiers
); j
++)
932 XGrabButton(dpy
, buttons
[i
].button
,
933 buttons
[i
].mask
| modifiers
[j
],
934 c
->win
, False
, BUTTONMASK
,
935 GrabModeAsync
, GrabModeSync
, None
, None
);
938 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
939 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
948 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
951 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
952 for(i
= 0; i
< LENGTH(keys
); i
++)
953 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
954 for(j
= 0; j
< LENGTH(modifiers
); j
++)
955 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
956 True
, GrabModeAsync
, GrabModeAsync
);
961 incnmaster(const Arg
*arg
) {
962 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
968 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
) {
970 if(unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
971 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
975 #endif /* XINERAMA */
978 keypress(XEvent
*e
) {
984 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
985 for(i
= 0; i
< LENGTH(keys
); i
++)
986 if(keysym
== keys
[i
].keysym
987 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
989 keys
[i
].func(&(keys
[i
].arg
));
993 killclient(const Arg
*arg
) {
996 if(!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
998 XSetErrorHandler(xerrordummy
);
999 XSetCloseDownMode(dpy
, DestroyAll
);
1000 XKillClient(dpy
, selmon
->sel
->win
);
1002 XSetErrorHandler(xerror
);
1008 manage(Window w
, XWindowAttributes
*wa
) {
1009 Client
*c
, *t
= NULL
;
1010 Window trans
= None
;
1013 if(!(c
= calloc(1, sizeof(Client
))))
1014 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1017 if(XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1026 c
->x
= c
->oldx
= wa
->x
;
1027 c
->y
= c
->oldy
= wa
->y
;
1028 c
->w
= c
->oldw
= wa
->width
;
1029 c
->h
= c
->oldh
= wa
->height
;
1030 c
->oldbw
= wa
->border_width
;
1032 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1033 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1034 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1035 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1036 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1037 /* only fix client y-offset, if the client center might cover the bar */
1038 c
->y
= MAX(c
->y
, ((c
->mon
->by
== c
->mon
->my
) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1039 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1042 wc
.border_width
= c
->bw
;
1043 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1044 XSetWindowBorder(dpy
, w
, thmnorm
->border
->rgb
);
1045 configure(c
); /* propagates border_width, if size doesn't change */
1046 updatewindowtype(c
);
1049 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1050 grabbuttons(c
, False
);
1052 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1054 XRaiseWindow(dpy
, c
->win
);
1057 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1058 (unsigned char *) &(c
->win
), 1);
1059 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1060 setclientstate(c
, NormalState
);
1061 if (c
->mon
== selmon
)
1062 unfocus(selmon
->sel
, False
);
1065 XMapWindow(dpy
, c
->win
);
1070 mappingnotify(XEvent
*e
) {
1071 XMappingEvent
*ev
= &e
->xmapping
;
1073 XRefreshKeyboardMapping(ev
);
1074 if(ev
->request
== MappingKeyboard
)
1079 maprequest(XEvent
*e
) {
1080 static XWindowAttributes wa
;
1081 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1083 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1085 if(wa
.override_redirect
)
1087 if(!wintoclient(ev
->window
))
1088 manage(ev
->window
, &wa
);
1092 monocle(Monitor
*m
) {
1096 for(c
= m
->clients
; c
; c
= c
->next
)
1099 if(n
> 0) /* override layout symbol */
1100 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1101 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1102 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1106 motionnotify(XEvent
*e
) {
1107 static Monitor
*mon
= NULL
;
1109 XMotionEvent
*ev
= &e
->xmotion
;
1111 if(ev
->window
!= root
)
1113 if((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1114 unfocus(selmon
->sel
, True
);
1122 movemouse(const Arg
*arg
) {
1123 int x
, y
, ocx
, ocy
, nx
, ny
;
1128 if(!(c
= selmon
->sel
))
1130 if(c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1135 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1136 None
, cursor
[CurMove
]->cursor
, CurrentTime
) != GrabSuccess
)
1138 if(!getrootptr(&x
, &y
))
1141 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1143 case ConfigureRequest
:
1146 handler
[ev
.type
](&ev
);
1149 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1150 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1151 if(nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1152 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1153 if(abs(selmon
->wx
- nx
) < snap
)
1155 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1156 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1157 if(abs(selmon
->wy
- ny
) < snap
)
1159 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1160 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1161 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1162 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1163 togglefloating(NULL
);
1165 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1166 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1169 } while(ev
.type
!= ButtonRelease
);
1170 XUngrabPointer(dpy
, CurrentTime
);
1171 if((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1179 nexttiled(Client
*c
) {
1180 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1193 propertynotify(XEvent
*e
) {
1196 XPropertyEvent
*ev
= &e
->xproperty
;
1198 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1200 else if(ev
->state
== PropertyDelete
)
1201 return; /* ignore */
1202 else if((c
= wintoclient(ev
->window
))) {
1205 case XA_WM_TRANSIENT_FOR
:
1206 if(!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1207 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1210 case XA_WM_NORMAL_HINTS
:
1218 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1220 if(c
== c
->mon
->sel
)
1223 if(ev
->atom
== netatom
[NetWMWindowType
])
1224 updatewindowtype(c
);
1229 quit(const Arg
*arg
) {
1234 recttomon(int x
, int y
, int w
, int h
) {
1235 Monitor
*m
, *r
= selmon
;
1238 for(m
= mons
; m
; m
= m
->next
)
1239 if((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1247 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1248 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1249 resizeclient(c
, x
, y
, w
, h
);
1253 resizeclient(Client
*c
, int x
, int y
, int w
, int h
) {
1256 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1257 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1258 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1259 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1260 wc
.border_width
= c
->bw
;
1261 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1267 resizemouse(const Arg
*arg
) {
1274 if(!(c
= selmon
->sel
))
1276 if(c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1281 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1282 None
, cursor
[CurResize
]->cursor
, CurrentTime
) != GrabSuccess
)
1284 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1286 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1288 case ConfigureRequest
:
1291 handler
[ev
.type
](&ev
);
1294 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1295 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1296 if(c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1297 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1299 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1300 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1301 togglefloating(NULL
);
1303 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1304 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1307 } while(ev
.type
!= ButtonRelease
);
1308 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1309 XUngrabPointer(dpy
, CurrentTime
);
1310 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1311 if((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1319 restack(Monitor
*m
) {
1327 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1328 XRaiseWindow(dpy
, m
->sel
->win
);
1329 if(m
->lt
[m
->sellt
]->arrange
) {
1330 wc
.stack_mode
= Below
;
1331 wc
.sibling
= m
->barwin
;
1332 for(c
= m
->stack
; c
; c
= c
->snext
)
1333 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1334 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1335 wc
.sibling
= c
->win
;
1339 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1345 /* main event loop */
1347 while(running
&& !XNextEvent(dpy
, &ev
))
1348 if(handler
[ev
.type
])
1349 handler
[ev
.type
](&ev
); /* call handler */
1354 unsigned int i
, num
;
1355 Window d1
, d2
, *wins
= NULL
;
1356 XWindowAttributes wa
;
1358 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1359 for(i
= 0; i
< num
; i
++) {
1360 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1361 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1363 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1364 manage(wins
[i
], &wa
);
1366 for(i
= 0; i
< num
; i
++) { /* now the transients */
1367 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1369 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1370 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1371 manage(wins
[i
], &wa
);
1379 sendmon(Client
*c
, Monitor
*m
) {
1386 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1394 setclientstate(Client
*c
, long state
) {
1395 long data
[] = { state
, None
};
1397 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1398 PropModeReplace
, (unsigned char *)data
, 2);
1402 sendevent(Client
*c
, Atom proto
) {
1405 Bool exists
= False
;
1408 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1409 while(!exists
&& n
--)
1410 exists
= protocols
[n
] == proto
;
1414 ev
.type
= ClientMessage
;
1415 ev
.xclient
.window
= c
->win
;
1416 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1417 ev
.xclient
.format
= 32;
1418 ev
.xclient
.data
.l
[0] = proto
;
1419 ev
.xclient
.data
.l
[1] = CurrentTime
;
1420 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1426 setfocus(Client
*c
) {
1427 if(!c
->neverfocus
) {
1428 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1429 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1430 XA_WINDOW
, 32, PropModeReplace
,
1431 (unsigned char *) &(c
->win
), 1);
1433 sendevent(c
, wmatom
[WMTakeFocus
]);
1437 setfullscreen(Client
*c
, Bool fullscreen
) {
1439 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1440 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1441 c
->isfullscreen
= True
;
1442 c
->oldstate
= c
->isfloating
;
1445 c
->isfloating
= True
;
1446 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1447 XRaiseWindow(dpy
, c
->win
);
1450 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1451 PropModeReplace
, (unsigned char*)0, 0);
1452 c
->isfullscreen
= False
;
1453 c
->isfloating
= c
->oldstate
;
1459 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1465 setlayout(const Arg
*arg
) {
1466 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1469 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1470 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1477 /* arg > 1.0 will set mfact absolutly */
1479 setmfact(const Arg
*arg
) {
1482 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1484 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1485 if(f
< 0.1 || f
> 0.9)
1493 XSetWindowAttributes wa
;
1495 /* clean up any zombies immediately */
1499 screen
= DefaultScreen(dpy
);
1500 root
= RootWindow(dpy
, screen
);
1501 fnt
= drw_font_create(dpy
, font
);
1502 sw
= DisplayWidth(dpy
, screen
);
1503 sh
= DisplayHeight(dpy
, screen
);
1505 drw
= drw_create(dpy
, screen
, root
, sw
, sh
);
1506 drw_setfont(drw
, fnt
);
1509 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1510 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1511 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1512 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1513 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1514 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1515 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1516 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1517 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1518 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1519 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1520 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1522 cursor
[CurNormal
] = drw_cur_create(drw
, XC_left_ptr
);
1523 cursor
[CurResize
] = drw_cur_create(drw
, XC_sizing
);
1524 cursor
[CurMove
] = drw_cur_create(drw
, XC_fleur
);
1525 /* init appearance */
1526 thmnorm
->border
= drw_clr_create(drw
, normbordercolor
);
1527 thmnorm
->bg
= drw_clr_create(drw
, normbgcolor
);
1528 thmnorm
->fg
= drw_clr_create(drw
, normfgcolor
);
1529 thmsel
->border
= drw_clr_create(drw
, selbordercolor
);
1530 thmsel
->bg
= drw_clr_create(drw
, selbgcolor
);
1531 thmsel
->fg
= drw_clr_create(drw
, selfgcolor
);
1535 /* EWMH support per view */
1536 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1537 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1538 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1539 /* select for events */
1540 wa
.cursor
= cursor
[CurNormal
]->cursor
;
1541 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
|PointerMotionMask
1542 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1543 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1544 XSelectInput(dpy
, root
, wa
.event_mask
);
1549 showhide(Client
*c
) {
1552 if(ISVISIBLE(c
)) { /* show clients top down */
1553 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1554 if((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1555 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1558 else { /* hide clients bottom up */
1560 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1565 sigchld(int unused
) {
1566 if(signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1567 die("Can't install SIGCHLD handler");
1568 while(0 < waitpid(-1, NULL
, WNOHANG
));
1572 spawn(const Arg
*arg
) {
1575 close(ConnectionNumber(dpy
));
1577 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1578 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1585 tag(const Arg
*arg
) {
1586 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1587 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1594 tagmon(const Arg
*arg
) {
1595 if(!selmon
->sel
|| !mons
->next
)
1597 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1602 unsigned int i
, n
, h
, mw
, my
, ty
;
1605 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1610 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1613 for(i
= my
= ty
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1614 if(i
< m
->nmaster
) {
1615 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
);
1616 resize(c
, m
->wx
, m
->wy
+ my
, mw
- (2*c
->bw
), h
- (2*c
->bw
), False
);
1620 h
= (m
->wh
- ty
) / (n
- i
);
1621 resize(c
, m
->wx
+ mw
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
), h
- (2*c
->bw
), False
);
1627 togglebar(const Arg
*arg
) {
1628 selmon
->showbar
= !selmon
->showbar
;
1629 updatebarpos(selmon
);
1630 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1635 togglefloating(const Arg
*arg
) {
1638 if(selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1640 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1641 if(selmon
->sel
->isfloating
)
1642 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1643 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1648 toggletag(const Arg
*arg
) {
1649 unsigned int newtags
;
1653 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1655 selmon
->sel
->tags
= newtags
;
1662 toggleview(const Arg
*arg
) {
1663 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1666 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1673 unfocus(Client
*c
, Bool setfocus
) {
1676 grabbuttons(c
, False
);
1677 XSetWindowBorder(dpy
, c
->win
, thmnorm
->border
->rgb
);
1679 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1680 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1685 unmanage(Client
*c
, Bool destroyed
) {
1686 Monitor
*m
= c
->mon
;
1689 /* The server grab construct avoids race conditions. */
1693 wc
.border_width
= c
->oldbw
;
1695 XSetErrorHandler(xerrordummy
);
1696 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1697 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1698 setclientstate(c
, WithdrawnState
);
1700 XSetErrorHandler(xerror
);
1710 unmapnotify(XEvent
*e
) {
1712 XUnmapEvent
*ev
= &e
->xunmap
;
1714 if((c
= wintoclient(ev
->window
))) {
1716 setclientstate(c
, WithdrawnState
);
1725 XSetWindowAttributes wa
= {
1726 .override_redirect
= True
,
1727 .background_pixmap
= ParentRelative
,
1728 .event_mask
= ButtonPressMask
|ExposureMask
1730 for(m
= mons
; m
; m
= m
->next
) {
1733 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1734 CopyFromParent
, DefaultVisual(dpy
, screen
),
1735 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1736 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]->cursor
);
1737 XMapRaised(dpy
, m
->barwin
);
1742 updatebarpos(Monitor
*m
) {
1747 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1748 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1755 updateclientlist() {
1759 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1760 for(m
= mons
; m
; m
= m
->next
)
1761 for(c
= m
->clients
; c
; c
= c
->next
)
1762 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
1763 XA_WINDOW
, 32, PropModeAppend
,
1764 (unsigned char *) &(c
->win
), 1);
1772 if(XineramaIsActive(dpy
)) {
1776 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1777 XineramaScreenInfo
*unique
= NULL
;
1779 for(n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1780 /* only consider unique geometries as separate screens */
1781 if(!(unique
= (XineramaScreenInfo
*)malloc(sizeof(XineramaScreenInfo
) * nn
)))
1782 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo
) * nn
);
1783 for(i
= 0, j
= 0; i
< nn
; i
++)
1784 if(isuniquegeom(unique
, j
, &info
[i
]))
1785 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1789 for(i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1790 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1792 m
->next
= createmon();
1796 for(i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1798 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1799 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1803 m
->mx
= m
->wx
= unique
[i
].x_org
;
1804 m
->my
= m
->wy
= unique
[i
].y_org
;
1805 m
->mw
= m
->ww
= unique
[i
].width
;
1806 m
->mh
= m
->wh
= unique
[i
].height
;
1810 else { /* less monitors available nn < n */
1811 for(i
= nn
; i
< n
; i
++) {
1812 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1816 m
->clients
= c
->next
;
1830 #endif /* XINERAMA */
1831 /* default monitor setup */
1835 if(mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1837 mons
->mw
= mons
->ww
= sw
;
1838 mons
->mh
= mons
->wh
= sh
;
1844 selmon
= wintomon(root
);
1850 updatenumlockmask(void) {
1852 XModifierKeymap
*modmap
;
1855 modmap
= XGetModifierMapping(dpy
);
1856 for(i
= 0; i
< 8; i
++)
1857 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1858 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1859 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1860 numlockmask
= (1 << i
);
1861 XFreeModifiermap(modmap
);
1865 updatesizehints(Client
*c
) {
1869 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1870 /* size is uninitialized, ensure that size.flags aren't used */
1872 if(size
.flags
& PBaseSize
) {
1873 c
->basew
= size
.base_width
;
1874 c
->baseh
= size
.base_height
;
1876 else if(size
.flags
& PMinSize
) {
1877 c
->basew
= size
.min_width
;
1878 c
->baseh
= size
.min_height
;
1881 c
->basew
= c
->baseh
= 0;
1882 if(size
.flags
& PResizeInc
) {
1883 c
->incw
= size
.width_inc
;
1884 c
->inch
= size
.height_inc
;
1887 c
->incw
= c
->inch
= 0;
1888 if(size
.flags
& PMaxSize
) {
1889 c
->maxw
= size
.max_width
;
1890 c
->maxh
= size
.max_height
;
1893 c
->maxw
= c
->maxh
= 0;
1894 if(size
.flags
& PMinSize
) {
1895 c
->minw
= size
.min_width
;
1896 c
->minh
= size
.min_height
;
1898 else if(size
.flags
& PBaseSize
) {
1899 c
->minw
= size
.base_width
;
1900 c
->minh
= size
.base_height
;
1903 c
->minw
= c
->minh
= 0;
1904 if(size
.flags
& PAspect
) {
1905 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1906 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1909 c
->maxa
= c
->mina
= 0.0;
1910 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1911 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1915 updatetitle(Client
*c
) {
1916 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1917 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1918 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1919 strcpy(c
->name
, broken
);
1923 updatestatus(void) {
1924 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1925 strcpy(stext
, "dwm-"VERSION
);
1930 updatewindowtype(Client
*c
) {
1931 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
1932 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
1934 if(state
== netatom
[NetWMFullscreen
])
1935 setfullscreen(c
, True
);
1936 if(wtype
== netatom
[NetWMWindowTypeDialog
])
1937 c
->isfloating
= True
;
1941 updatewmhints(Client
*c
) {
1944 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1945 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1946 wmh
->flags
&= ~XUrgencyHint
;
1947 XSetWMHints(dpy
, c
->win
, wmh
);
1950 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1951 if(wmh
->flags
& InputHint
)
1952 c
->neverfocus
= !wmh
->input
;
1954 c
->neverfocus
= False
;
1960 view(const Arg
*arg
) {
1961 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1963 selmon
->seltags
^= 1; /* toggle sel tagset */
1964 if(arg
->ui
& TAGMASK
)
1965 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1971 wintoclient(Window w
) {
1975 for(m
= mons
; m
; m
= m
->next
)
1976 for(c
= m
->clients
; c
; c
= c
->next
)
1983 wintomon(Window w
) {
1988 if(w
== root
&& getrootptr(&x
, &y
))
1989 return recttomon(x
, y
, 1, 1);
1990 for(m
= mons
; m
; m
= m
->next
)
1993 if((c
= wintoclient(w
)))
1998 /* There's no way to check accesses to destroyed windows, thus those cases are
1999 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2000 * default error handler, which may call exit. */
2002 xerror(Display
*dpy
, XErrorEvent
*ee
) {
2003 if(ee
->error_code
== BadWindow
2004 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2005 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2006 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2007 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2008 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2009 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2010 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2011 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2013 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2014 ee
->request_code
, ee
->error_code
);
2015 return xerrorxlib(dpy
, ee
); /* may call exit */
2019 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
2023 /* Startup Error handler to check if another window manager
2024 * is already running. */
2026 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
2027 die("dwm: another window manager is already running\n");
2032 zoom(const Arg
*arg
) {
2033 Client
*c
= selmon
->sel
;
2035 if(!selmon
->lt
[selmon
->sellt
]->arrange
2036 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2038 if(c
== nexttiled(selmon
->clients
))
2039 if(!c
|| !(c
= nexttiled(c
->next
)))
2045 main(int argc
, char *argv
[]) {
2046 if(argc
== 2 && !strcmp("-v", argv
[1]))
2047 die("dwm-"VERSION
", © 2006-2012 dwm engineers, see LICENSE for details\n");
2049 die("usage: dwm [-v]\n");
2050 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2051 fputs("warning: no locale support\n", stderr
);
2052 if(!(dpy
= XOpenDisplay(NULL
)))
2053 die("dwm: cannot open display\n");
2060 return EXIT_SUCCESS
;