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>
44 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
45 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
46 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
47 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
48 #define LENGTH(X) (sizeof X / sizeof X[0])
49 #define MAX(A, B) ((A) > (B) ? (A) : (B))
50 #define MIN(A, B) ((A) < (B) ? (A) : (B))
51 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
52 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
53 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
54 #define TAGMASK ((1 << LENGTH(tags)) - 1)
55 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
58 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
59 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
60 enum { NetSupported
, NetWMName
, NetWMState
,
61 NetWMFullscreen
, NetLast
}; /* EWMH atoms */
62 enum { WMProtocols
, WMDelete
, WMState
, WMLast
}; /* default atoms */
63 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
64 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
77 void (*func
)(const Arg
*arg
);
81 typedef struct Monitor Monitor
;
82 typedef struct Client Client
;
87 int oldx
, oldy
, oldw
, oldh
;
88 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
91 Bool isfixed
, isfloating
, isurgent
, oldstate
;
100 unsigned long norm
[ColLast
];
101 unsigned long sel
[ColLast
];
111 } DC
; /* draw context */
116 void (*func
)(const Arg
*);
122 void (*arrange
)(Monitor
*);
129 int by
; /* bar geometry */
130 int mx
, my
, mw
, mh
; /* screen size */
131 int wx
, wy
, ww
, wh
; /* window area */
132 unsigned int seltags
;
134 unsigned int tagset
[2];
147 const char *instance
;
154 /* function declarations */
155 static void applyrules(Client
*c
);
156 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
157 static void arrange(Monitor
*m
);
158 static void arrangemon(Monitor
*m
);
159 static void attach(Client
*c
);
160 static void attachstack(Client
*c
);
161 static void buttonpress(XEvent
*e
);
162 static void checkotherwm(void);
163 static void cleanup(void);
164 static void cleanupmon(Monitor
*mon
);
165 static void clearurgent(Client
*c
);
166 static void clientmessage(XEvent
*e
);
167 static void configure(Client
*c
);
168 static void configurenotify(XEvent
*e
);
169 static void configurerequest(XEvent
*e
);
170 static Monitor
*createmon(void);
171 static void destroynotify(XEvent
*e
);
172 static void detach(Client
*c
);
173 static void detachstack(Client
*c
);
174 static void die(const char *errstr
, ...);
175 static Monitor
*dirtomon(int dir
);
176 static void drawbar(Monitor
*m
);
177 static void drawbars(void);
178 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
179 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
180 static void enternotify(XEvent
*e
);
181 static void expose(XEvent
*e
);
182 static void focus(Client
*c
);
183 static void focusin(XEvent
*e
);
184 static void focusmon(const Arg
*arg
);
185 static void focusstack(const Arg
*arg
);
186 static unsigned long getcolor(const char *colstr
);
187 static Bool
getrootptr(int *x
, int *y
);
188 static long getstate(Window w
);
189 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
190 static void grabbuttons(Client
*c
, Bool focused
);
191 static void grabkeys(void);
192 static void initfont(const char *fontstr
);
193 static Bool
isprotodel(Client
*c
);
194 static void keypress(XEvent
*e
);
195 static void killclient(const Arg
*arg
);
196 static void manage(Window w
, XWindowAttributes
*wa
);
197 static void mappingnotify(XEvent
*e
);
198 static void maprequest(XEvent
*e
);
199 static void monocle(Monitor
*m
);
200 static void movemouse(const Arg
*arg
);
201 static Client
*nexttiled(Client
*c
);
202 static Monitor
*ptrtomon(int x
, int y
);
203 static void propertynotify(XEvent
*e
);
204 static void quit(const Arg
*arg
);
205 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
206 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
207 static void resizemouse(const Arg
*arg
);
208 static void restack(Monitor
*m
);
209 static void run(void);
210 static void scan(void);
211 static void sendmon(Client
*c
, Monitor
*m
);
212 static void setclientstate(Client
*c
, long state
);
213 static void setlayout(const Arg
*arg
);
214 static void setmfact(const Arg
*arg
);
215 static void setup(void);
216 static void showhide(Client
*c
);
217 static void sigchld(int unused
);
218 static void spawn(const Arg
*arg
);
219 static void tag(const Arg
*arg
);
220 static void tagmon(const Arg
*arg
);
221 static int textnw(const char *text
, unsigned int len
);
222 static void tile(Monitor
*);
223 static void togglebar(const Arg
*arg
);
224 static void togglefloating(const Arg
*arg
);
225 static void toggletag(const Arg
*arg
);
226 static void toggleview(const Arg
*arg
);
227 static void unfocus(Client
*c
, Bool setfocus
);
228 static void unmanage(Client
*c
, Bool destroyed
);
229 static void unmapnotify(XEvent
*e
);
230 static Bool
updategeom(void);
231 static void updatebarpos(Monitor
*m
);
232 static void updatebars(void);
233 static void updatenumlockmask(void);
234 static void updatesizehints(Client
*c
);
235 static void updatestatus(void);
236 static void updatetitle(Client
*c
);
237 static void updatewmhints(Client
*c
);
238 static void view(const Arg
*arg
);
239 static Client
*wintoclient(Window w
);
240 static Monitor
*wintomon(Window w
);
241 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
242 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
243 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
244 static void zoom(const Arg
*arg
);
247 static const char broken
[] = "broken";
248 static char stext
[256];
250 static int sw
, sh
; /* X display screen geometry width, height */
251 static int bh
, blw
= 0; /* bar geometry */
252 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
253 static unsigned int numlockmask
= 0;
254 static void (*handler
[LASTEvent
]) (XEvent
*) = {
255 [ButtonPress
] = buttonpress
,
256 [ClientMessage
] = clientmessage
,
257 [ConfigureRequest
] = configurerequest
,
258 [ConfigureNotify
] = configurenotify
,
259 [DestroyNotify
] = destroynotify
,
260 [EnterNotify
] = enternotify
,
263 [KeyPress
] = keypress
,
264 [MappingNotify
] = mappingnotify
,
265 [MapRequest
] = maprequest
,
266 [PropertyNotify
] = propertynotify
,
267 [UnmapNotify
] = unmapnotify
269 static Atom wmatom
[WMLast
], netatom
[NetLast
];
270 static Bool running
= True
;
271 static Cursor cursor
[CurLast
];
274 static Monitor
*mons
= NULL
, *selmon
= NULL
;
277 /* configuration, allows nested code to access above variables */
280 /* compile-time check if all tags fit into an unsigned int bit array. */
281 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
283 /* function implementations */
285 applyrules(Client
*c
) {
286 const char *class, *instance
;
290 XClassHint ch
= { 0 };
293 c
->isfloating
= c
->tags
= 0;
294 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
295 class = ch
.res_class
? ch
.res_class
: broken
;
296 instance
= ch
.res_name
? ch
.res_name
: broken
;
297 for(i
= 0; i
< LENGTH(rules
); i
++) {
299 if((!r
->title
|| strstr(c
->name
, r
->title
))
300 && (!r
->class || strstr(class, r
->class))
301 && (!r
->instance
|| strstr(instance
, r
->instance
)))
303 c
->isfloating
= r
->isfloating
;
305 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
315 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
319 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
323 /* set minimum possible */
331 if(*x
+ *w
+ 2 * c
->bw
< 0)
333 if(*y
+ *h
+ 2 * c
->bw
< 0)
337 if(*x
> m
->mx
+ m
->mw
)
338 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
339 if(*y
> m
->my
+ m
->mh
)
340 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
341 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
343 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
350 if(resizehints
|| c
->isfloating
) {
351 /* see last two sentences in ICCCM 4.1.2.3 */
352 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
353 if(!baseismin
) { /* temporarily remove base dimensions */
357 /* adjust for aspect limits */
358 if(c
->mina
> 0 && c
->maxa
> 0) {
359 if(c
->maxa
< (float)*w
/ *h
)
360 *w
= *h
* c
->maxa
+ 0.5;
361 else if(c
->mina
< (float)*h
/ *w
)
362 *h
= *w
* c
->mina
+ 0.5;
364 if(baseismin
) { /* increment calculation requires this */
368 /* adjust for increment value */
373 /* restore base dimensions */
374 *w
= MAX(*w
+ c
->basew
, c
->minw
);
375 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
377 *w
= MIN(*w
, c
->maxw
);
379 *h
= MIN(*h
, c
->maxh
);
381 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
385 arrange(Monitor
*m
) {
388 else for(m
= mons
; m
; m
= m
->next
)
393 else for(m
= mons
; m
; m
= m
->next
)
398 arrangemon(Monitor
*m
) {
399 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
400 if(m
->lt
[m
->sellt
]->arrange
)
401 m
->lt
[m
->sellt
]->arrange(m
);
407 c
->next
= c
->mon
->clients
;
412 attachstack(Client
*c
) {
413 c
->snext
= c
->mon
->stack
;
418 buttonpress(XEvent
*e
) {
419 unsigned int i
, x
, click
;
423 XButtonPressedEvent
*ev
= &e
->xbutton
;
426 /* focus monitor if necessary */
427 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
428 unfocus(selmon
->sel
, True
);
432 if(ev
->window
== selmon
->barwin
) {
436 } while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
437 if(i
< LENGTH(tags
)) {
441 else if(ev
->x
< x
+ blw
)
443 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
444 click
= ClkStatusText
;
448 else if((c
= wintoclient(ev
->window
))) {
450 click
= ClkClientWin
;
452 for(i
= 0; i
< LENGTH(buttons
); i
++)
453 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
454 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
455 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
460 xerrorxlib
= XSetErrorHandler(xerrorstart
);
461 /* this causes an error if some other window manager is running */
462 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
464 XSetErrorHandler(xerror
);
471 Layout foo
= { "", NULL
};
475 selmon
->lt
[selmon
->sellt
] = &foo
;
476 for(m
= mons
; m
; m
= m
->next
)
478 unmanage(m
->stack
, False
);
480 XFreeFontSet(dpy
, dc
.font
.set
);
482 XFreeFont(dpy
, dc
.font
.xfont
);
483 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
484 XFreePixmap(dpy
, dc
.drawable
);
486 XFreeCursor(dpy
, cursor
[CurNormal
]);
487 XFreeCursor(dpy
, cursor
[CurResize
]);
488 XFreeCursor(dpy
, cursor
[CurMove
]);
492 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
496 cleanupmon(Monitor
*mon
) {
502 for(m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
505 XUnmapWindow(dpy
, mon
->barwin
);
506 XDestroyWindow(dpy
, mon
->barwin
);
511 clearurgent(Client
*c
) {
515 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
517 wmh
->flags
&= ~XUrgencyHint
;
518 XSetWMHints(dpy
, c
->win
, wmh
);
523 configure(Client
*c
) {
526 ce
.type
= ConfigureNotify
;
534 ce
.border_width
= c
->bw
;
536 ce
.override_redirect
= False
;
537 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
541 configurenotify(XEvent
*e
) {
543 XConfigureEvent
*ev
= &e
->xconfigure
;
545 if(ev
->window
== root
) {
550 XFreePixmap(dpy
, dc
.drawable
);
551 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
553 for(m
= mons
; m
; m
= m
->next
)
554 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
561 configurerequest(XEvent
*e
) {
564 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
567 if((c
= wintoclient(ev
->window
))) {
568 if(ev
->value_mask
& CWBorderWidth
)
569 c
->bw
= ev
->border_width
;
570 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
572 if(ev
->value_mask
& CWX
)
573 c
->x
= m
->mx
+ ev
->x
;
574 if(ev
->value_mask
& CWY
)
575 c
->y
= m
->my
+ ev
->y
;
576 if(ev
->value_mask
& CWWidth
)
578 if(ev
->value_mask
& CWHeight
)
580 if((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
581 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
582 if((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
583 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
584 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
587 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
595 wc
.width
= ev
->width
;
596 wc
.height
= ev
->height
;
597 wc
.border_width
= ev
->border_width
;
598 wc
.sibling
= ev
->above
;
599 wc
.stack_mode
= ev
->detail
;
600 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
609 if(!(m
= (Monitor
*)calloc(1, sizeof(Monitor
))))
610 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
611 m
->tagset
[0] = m
->tagset
[1] = 1;
613 m
->showbar
= showbar
;
615 m
->lt
[0] = &layouts
[0];
616 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
617 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
622 destroynotify(XEvent
*e
) {
624 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
626 if((c
= wintoclient(ev
->window
)))
634 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
639 detachstack(Client
*c
) {
642 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
645 if(c
== c
->mon
->sel
) {
646 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
652 die(const char *errstr
, ...) {
655 va_start(ap
, errstr
);
656 vfprintf(stderr
, errstr
, ap
);
666 if(!(m
= selmon
->next
))
671 for(m
= mons
; m
->next
; m
= m
->next
);
673 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
679 drawbar(Monitor
*m
) {
681 unsigned int i
, occ
= 0, urg
= 0;
685 for(c
= m
->clients
; c
; c
= c
->next
) {
691 for(i
= 0; i
< LENGTH(tags
); i
++) {
692 dc
.w
= TEXTW(tags
[i
]);
693 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
694 drawtext(tags
[i
], col
, urg
& 1 << i
);
695 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
696 occ
& 1 << i
, urg
& 1 << i
, col
);
699 dc
.w
= blw
= TEXTW(m
->ltsymbol
);
700 drawtext(m
->ltsymbol
, dc
.norm
, False
);
703 if(m
== selmon
) { /* status is only drawn on selected monitor */
710 drawtext(stext
, dc
.norm
, False
);
714 if((dc
.w
= dc
.x
- x
) > bh
) {
717 col
= m
== selmon
? dc
.sel
: dc
.norm
;
718 drawtext(m
->sel
->name
, col
, False
);
719 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
722 drawtext(NULL
, dc
.norm
, False
);
724 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
732 for(m
= mons
; m
; m
= m
->next
)
737 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
740 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
742 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
743 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
744 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
748 r
.width
= r
.height
= x
+ 1;
749 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
752 r
.width
= r
.height
= x
;
753 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
758 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
760 int i
, x
, y
, h
, len
, olen
;
761 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
763 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
764 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
768 h
= dc
.font
.ascent
+ dc
.font
.descent
;
769 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
771 /* shorten text if necessary */
772 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
775 memcpy(buf
, text
, len
);
777 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
778 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
780 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
782 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
786 enternotify(XEvent
*e
) {
788 XCrossingEvent
*ev
= &e
->xcrossing
;
790 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
792 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
793 unfocus(selmon
->sel
, True
);
796 focus((wintoclient(ev
->window
)));
802 XExposeEvent
*ev
= &e
->xexpose
;
804 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
810 if(!c
|| !ISVISIBLE(c
))
811 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
812 /* was if(selmon->sel) */
813 if(selmon
->sel
&& selmon
->sel
!= c
)
814 unfocus(selmon
->sel
, False
);
822 grabbuttons(c
, True
);
823 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
824 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
827 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
833 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
834 XFocusChangeEvent
*ev
= &e
->xfocus
;
836 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
837 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
841 focusmon(const Arg
*arg
) {
846 if((m
= dirtomon(arg
->i
)) == selmon
)
848 unfocus(selmon
->sel
, True
);
854 focusstack(const Arg
*arg
) {
855 Client
*c
= NULL
, *i
;
860 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
862 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 getcolor(const char *colstr
) {
881 Colormap cmap
= DefaultColormap(dpy
, screen
);
884 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
885 die("error, cannot allocate color '%s'\n", colstr
);
890 getrootptr(int *x
, int *y
) {
895 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
) {
921 if(!text
|| size
== 0)
924 XGetTextProperty(dpy
, w
, &name
, atom
);
927 if(name
.encoding
== XA_STRING
)
928 strncpy(text
, (char *)name
.value
, size
- 1);
930 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
931 strncpy(text
, *list
, size
- 1);
932 XFreeStringList(list
);
935 text
[size
- 1] = '\0';
941 grabbuttons(Client
*c
, Bool focused
) {
945 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
946 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
948 for(i
= 0; i
< LENGTH(buttons
); i
++)
949 if(buttons
[i
].click
== ClkClientWin
)
950 for(j
= 0; j
< LENGTH(modifiers
); j
++)
951 XGrabButton(dpy
, buttons
[i
].button
,
952 buttons
[i
].mask
| modifiers
[j
],
953 c
->win
, False
, BUTTONMASK
,
954 GrabModeAsync
, GrabModeSync
, None
, None
);
957 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
958 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
967 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
970 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
971 for(i
= 0; i
< LENGTH(keys
); i
++) {
972 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
973 for(j
= 0; j
< LENGTH(modifiers
); j
++)
974 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
975 True
, GrabModeAsync
, GrabModeAsync
);
981 initfont(const char *fontstr
) {
982 char *def
, **missing
;
986 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
989 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
990 XFreeStringList(missing
);
993 XFontStruct
**xfonts
;
996 dc
.font
.ascent
= dc
.font
.descent
= 0;
997 XExtentsOfFontSet(dc
.font
.set
);
998 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
999 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
1000 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
1001 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1006 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1007 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1008 die("error, cannot load font: '%s'\n", fontstr
);
1009 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1010 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1012 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1016 isprotodel(Client
*c
) {
1021 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1022 for(i
= 0; !ret
&& i
< n
; i
++)
1023 if(protocols
[i
] == wmatom
[WMDelete
])
1032 isuniquegeom(XineramaScreenInfo
*unique
, size_t len
, XineramaScreenInfo
*info
) {
1035 for(i
= 0; i
< len
; i
++)
1036 if(unique
[i
].x_org
== info
->x_org
&& unique
[i
].y_org
== info
->y_org
1037 && unique
[i
].width
== info
->width
&& unique
[i
].height
== info
->height
)
1041 #endif /* XINERAMA */
1044 keypress(XEvent
*e
) {
1050 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1051 for(i
= 0; i
< LENGTH(keys
); i
++)
1052 if(keysym
== keys
[i
].keysym
1053 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1055 keys
[i
].func(&(keys
[i
].arg
));
1059 killclient(const Arg
*arg
) {
1064 if(isprotodel(selmon
->sel
)) {
1065 ev
.type
= ClientMessage
;
1066 ev
.xclient
.window
= selmon
->sel
->win
;
1067 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1068 ev
.xclient
.format
= 32;
1069 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1070 ev
.xclient
.data
.l
[1] = CurrentTime
;
1071 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1075 XSetErrorHandler(xerrordummy
);
1076 XSetCloseDownMode(dpy
, DestroyAll
);
1077 XKillClient(dpy
, selmon
->sel
->win
);
1079 XSetErrorHandler(xerror
);
1085 manage(Window w
, XWindowAttributes
*wa
) {
1086 Client
*c
, *t
= NULL
;
1087 Window trans
= None
;
1090 if(!(c
= calloc(1, sizeof(Client
))))
1091 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1094 if(XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1103 c
->x
= c
->oldx
= wa
->x
+ c
->mon
->wx
;
1104 c
->y
= c
->oldy
= wa
->y
+ c
->mon
->wy
;
1105 c
->w
= c
->oldw
= wa
->width
;
1106 c
->h
= c
->oldh
= wa
->height
;
1107 c
->oldbw
= wa
->border_width
;
1108 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1109 c
->isfloating
= True
;
1115 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1116 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1117 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1118 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1119 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1120 /* only fix client y-offset, if the client center might cover the bar */
1121 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1122 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1125 wc
.border_width
= c
->bw
;
1126 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1127 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1128 configure(c
); /* propagates border_width, if size doesn't change */
1130 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1131 grabbuttons(c
, False
);
1133 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1135 XRaiseWindow(dpy
, c
->win
);
1138 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1139 XMapWindow(dpy
, c
->win
);
1140 setclientstate(c
, NormalState
);
1145 mappingnotify(XEvent
*e
) {
1146 XMappingEvent
*ev
= &e
->xmapping
;
1148 XRefreshKeyboardMapping(ev
);
1149 if(ev
->request
== MappingKeyboard
)
1154 maprequest(XEvent
*e
) {
1155 static XWindowAttributes wa
;
1156 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1158 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1160 if(wa
.override_redirect
)
1162 if(!wintoclient(ev
->window
))
1163 manage(ev
->window
, &wa
);
1167 monocle(Monitor
*m
) {
1171 for(c
= m
->clients
; c
; c
= c
->next
)
1174 if(n
> 0) /* override layout symbol */
1175 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1176 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1177 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1181 movemouse(const Arg
*arg
) {
1182 int x
, y
, ocx
, ocy
, nx
, ny
;
1187 if(!(c
= selmon
->sel
))
1192 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1193 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1195 if(!getrootptr(&x
, &y
))
1198 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1200 case ConfigureRequest
:
1203 handler
[ev
.type
](&ev
);
1206 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1207 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1208 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1209 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1210 if(abs(selmon
->wx
- nx
) < snap
)
1212 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1213 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1214 if(abs(selmon
->wy
- ny
) < snap
)
1216 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1217 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1218 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1219 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1220 togglefloating(NULL
);
1222 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1223 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1226 } while(ev
.type
!= ButtonRelease
);
1227 XUngrabPointer(dpy
, CurrentTime
);
1228 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1236 nexttiled(Client
*c
) {
1237 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1242 ptrtomon(int x
, int y
) {
1245 for(m
= mons
; m
; m
= m
->next
)
1246 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1252 propertynotify(XEvent
*e
) {
1255 XPropertyEvent
*ev
= &e
->xproperty
;
1257 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1259 else if(ev
->state
== PropertyDelete
)
1260 return; /* ignore */
1261 else if((c
= wintoclient(ev
->window
))) {
1264 case XA_WM_TRANSIENT_FOR
:
1265 if(!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1266 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1269 case XA_WM_NORMAL_HINTS
:
1277 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1279 if(c
== c
->mon
->sel
)
1286 clientmessage(XEvent
*e
) {
1287 XClientMessageEvent
*cme
= &e
->xclient
;
1290 if((c
= wintoclient(cme
->window
))
1291 && (cme
->message_type
== netatom
[NetWMState
] && cme
->data
.l
[1] == netatom
[NetWMFullscreen
]))
1293 if(cme
->data
.l
[0]) {
1294 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
1295 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1296 c
->oldstate
= c
->isfloating
;
1299 c
->isfloating
= True
;
1300 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1301 XRaiseWindow(dpy
, c
->win
);
1304 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
1305 PropModeReplace
, (unsigned char*)0, 0);
1306 c
->isfloating
= c
->oldstate
;
1312 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1319 quit(const Arg
*arg
) {
1324 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1325 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1326 resizeclient(c
, x
, y
, w
, h
);
1330 resizeclient(Client
*c
, int x
, int y
, int w
, int h
) {
1333 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1334 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1335 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1336 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1337 wc
.border_width
= c
->bw
;
1338 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1344 resizemouse(const Arg
*arg
) {
1351 if(!(c
= selmon
->sel
))
1356 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1357 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1359 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1361 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1363 case ConfigureRequest
:
1366 handler
[ev
.type
](&ev
);
1369 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1370 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1371 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1372 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
)
1374 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1375 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1376 togglefloating(NULL
);
1378 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1379 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1382 } while(ev
.type
!= ButtonRelease
);
1383 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1384 XUngrabPointer(dpy
, CurrentTime
);
1385 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1386 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1394 restack(Monitor
*m
) {
1402 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1403 XRaiseWindow(dpy
, m
->sel
->win
);
1404 if(m
->lt
[m
->sellt
]->arrange
) {
1405 wc
.stack_mode
= Below
;
1406 wc
.sibling
= m
->barwin
;
1407 for(c
= m
->stack
; c
; c
= c
->snext
)
1408 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1409 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1410 wc
.sibling
= c
->win
;
1414 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1420 /* main event loop */
1422 while(running
&& !XNextEvent(dpy
, &ev
)) {
1423 if(handler
[ev
.type
])
1424 handler
[ev
.type
](&ev
); /* call handler */
1430 unsigned int i
, num
;
1431 Window d1
, d2
, *wins
= NULL
;
1432 XWindowAttributes wa
;
1434 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1435 for(i
= 0; i
< num
; i
++) {
1436 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1437 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1439 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1440 manage(wins
[i
], &wa
);
1442 for(i
= 0; i
< num
; i
++) { /* now the transients */
1443 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1445 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1446 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1447 manage(wins
[i
], &wa
);
1455 sendmon(Client
*c
, Monitor
*m
) {
1462 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1470 setclientstate(Client
*c
, long state
) {
1471 long data
[] = { state
, None
};
1473 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1474 PropModeReplace
, (unsigned char *)data
, 2);
1478 setlayout(const Arg
*arg
) {
1479 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1482 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1483 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1490 /* arg > 1.0 will set mfact absolutly */
1492 setmfact(const Arg
*arg
) {
1495 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1497 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1498 if(f
< 0.1 || f
> 0.9)
1506 XSetWindowAttributes wa
;
1508 /* clean up any zombies immediately */
1512 screen
= DefaultScreen(dpy
);
1513 root
= RootWindow(dpy
, screen
);
1515 sw
= DisplayWidth(dpy
, screen
);
1516 sh
= DisplayHeight(dpy
, screen
);
1517 bh
= dc
.h
= dc
.font
.height
+ 2;
1520 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1521 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1522 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1523 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1524 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1525 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1526 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1528 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1529 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1530 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1531 /* init appearance */
1532 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1533 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1534 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1535 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1536 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1537 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1538 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1539 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1540 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1542 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1546 /* EWMH support per view */
1547 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1548 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1549 /* select for events */
1550 wa
.cursor
= cursor
[CurNormal
];
1551 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1552 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1553 |PropertyChangeMask
;
1554 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1555 XSelectInput(dpy
, root
, wa
.event_mask
);
1560 showhide(Client
*c
) {
1563 if(ISVISIBLE(c
)) { /* show clients top down */
1564 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1565 if(!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1566 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1569 else { /* hide clients bottom up */
1571 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1576 sigchld(int unused
) {
1577 if(signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1578 die("Can't install SIGCHLD handler");
1579 while(0 < waitpid(-1, NULL
, WNOHANG
));
1583 spawn(const Arg
*arg
) {
1586 close(ConnectionNumber(dpy
));
1588 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1589 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1596 tag(const Arg
*arg
) {
1597 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1598 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1604 tagmon(const Arg
*arg
) {
1605 if(!selmon
->sel
|| !mons
->next
)
1607 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1611 textnw(const char *text
, unsigned int len
) {
1615 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1618 return XTextWidth(dc
.font
.xfont
, text
, len
);
1627 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1631 c
= nexttiled(m
->clients
);
1632 mw
= m
->mfact
* m
->ww
;
1633 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1637 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1639 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1643 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1644 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1645 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1647 y
= c
->y
+ HEIGHT(c
);
1652 togglebar(const Arg
*arg
) {
1653 selmon
->showbar
= !selmon
->showbar
;
1654 updatebarpos(selmon
);
1655 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1660 togglefloating(const Arg
*arg
) {
1663 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1664 if(selmon
->sel
->isfloating
)
1665 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1666 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1671 toggletag(const Arg
*arg
) {
1672 unsigned int newtags
;
1676 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1678 selmon
->sel
->tags
= newtags
;
1684 toggleview(const Arg
*arg
) {
1685 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1688 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1694 unfocus(Client
*c
, Bool setfocus
) {
1697 grabbuttons(c
, False
);
1698 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1700 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1704 unmanage(Client
*c
, Bool destroyed
) {
1705 Monitor
*m
= c
->mon
;
1708 /* The server grab construct avoids race conditions. */
1712 wc
.border_width
= c
->oldbw
;
1714 XSetErrorHandler(xerrordummy
);
1715 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1716 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1717 setclientstate(c
, WithdrawnState
);
1719 XSetErrorHandler(xerror
);
1728 unmapnotify(XEvent
*e
) {
1730 XUnmapEvent
*ev
= &e
->xunmap
;
1732 if((c
= wintoclient(ev
->window
)))
1739 XSetWindowAttributes wa
;
1741 wa
.override_redirect
= True
;
1742 wa
.background_pixmap
= ParentRelative
;
1743 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1744 for(m
= mons
; m
; m
= m
->next
) {
1745 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1746 CopyFromParent
, DefaultVisual(dpy
, screen
),
1747 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1748 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1749 XMapRaised(dpy
, m
->barwin
);
1754 updatebarpos(Monitor
*m
) {
1759 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1760 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1771 if(XineramaIsActive(dpy
)) {
1775 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1776 XineramaScreenInfo
*unique
= NULL
;
1778 for(n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1779 /* only consider unique geometries as separate screens */
1780 if(!(unique
= (XineramaScreenInfo
*)malloc(sizeof(XineramaScreenInfo
) * nn
)))
1781 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo
) * nn
);
1782 for(i
= 0, j
= 0; i
< nn
; i
++)
1783 if(isuniquegeom(unique
, j
, &info
[i
]))
1784 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1788 for(i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1789 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1791 m
->next
= createmon();
1795 for(i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1797 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1798 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1802 m
->mx
= m
->wx
= unique
[i
].x_org
;
1803 m
->my
= m
->wy
= unique
[i
].y_org
;
1804 m
->mw
= m
->ww
= unique
[i
].width
;
1805 m
->mh
= m
->wh
= unique
[i
].height
;
1809 else { /* less monitors available nn < n */
1810 for(i
= nn
; i
< n
; i
++) {
1811 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1815 m
->clients
= c
->next
;
1829 #endif /* XINERAMA */
1830 /* default monitor setup */
1834 if(mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1836 mons
->mw
= mons
->ww
= sw
;
1837 mons
->mh
= mons
->wh
= sh
;
1843 selmon
= wintomon(root
);
1849 updatenumlockmask(void) {
1851 XModifierKeymap
*modmap
;
1854 modmap
= XGetModifierMapping(dpy
);
1855 for(i
= 0; i
< 8; i
++)
1856 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1857 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1858 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1859 numlockmask
= (1 << i
);
1860 XFreeModifiermap(modmap
);
1864 updatesizehints(Client
*c
) {
1868 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1869 /* size is uninitialized, ensure that size.flags aren't used */
1871 if(size
.flags
& PBaseSize
) {
1872 c
->basew
= size
.base_width
;
1873 c
->baseh
= size
.base_height
;
1875 else if(size
.flags
& PMinSize
) {
1876 c
->basew
= size
.min_width
;
1877 c
->baseh
= size
.min_height
;
1880 c
->basew
= c
->baseh
= 0;
1881 if(size
.flags
& PResizeInc
) {
1882 c
->incw
= size
.width_inc
;
1883 c
->inch
= size
.height_inc
;
1886 c
->incw
= c
->inch
= 0;
1887 if(size
.flags
& PMaxSize
) {
1888 c
->maxw
= size
.max_width
;
1889 c
->maxh
= size
.max_height
;
1892 c
->maxw
= c
->maxh
= 0;
1893 if(size
.flags
& PMinSize
) {
1894 c
->minw
= size
.min_width
;
1895 c
->minh
= size
.min_height
;
1897 else if(size
.flags
& PBaseSize
) {
1898 c
->minw
= size
.base_width
;
1899 c
->minh
= size
.base_height
;
1902 c
->minw
= c
->minh
= 0;
1903 if(size
.flags
& PAspect
) {
1904 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1905 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1908 c
->maxa
= c
->mina
= 0.0;
1909 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1910 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1914 updatetitle(Client
*c
) {
1915 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1916 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1917 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1918 strcpy(c
->name
, broken
);
1922 updatestatus(void) {
1923 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1924 strcpy(stext
, "dwm-"VERSION
);
1929 updatewmhints(Client
*c
) {
1932 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1933 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1934 wmh
->flags
&= ~XUrgencyHint
;
1935 XSetWMHints(dpy
, c
->win
, wmh
);
1938 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1944 view(const Arg
*arg
) {
1945 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1947 selmon
->seltags
^= 1; /* toggle sel tagset */
1948 if(arg
->ui
& TAGMASK
)
1949 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1954 wintoclient(Window w
) {
1958 for(m
= mons
; m
; m
= m
->next
)
1959 for(c
= m
->clients
; c
; c
= c
->next
)
1966 wintomon(Window w
) {
1971 if(w
== root
&& getrootptr(&x
, &y
))
1972 return ptrtomon(x
, y
);
1973 for(m
= mons
; m
; m
= m
->next
)
1976 if((c
= wintoclient(w
)))
1981 /* There's no way to check accesses to destroyed windows, thus those cases are
1982 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1983 * default error handler, which may call exit. */
1985 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1986 if(ee
->error_code
== BadWindow
1987 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1988 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1989 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1990 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1991 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1992 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1993 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1994 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1996 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1997 ee
->request_code
, ee
->error_code
);
1998 return xerrorxlib(dpy
, ee
); /* may call exit */
2002 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
2006 /* Startup Error handler to check if another window manager
2007 * is already running. */
2009 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
2010 die("dwm: another window manager is already running\n");
2015 zoom(const Arg
*arg
) {
2016 Client
*c
= selmon
->sel
;
2018 if(!selmon
->lt
[selmon
->sellt
]->arrange
2019 || selmon
->lt
[selmon
->sellt
]->arrange
== monocle
2020 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2022 if(c
== nexttiled(selmon
->clients
))
2023 if(!c
|| !(c
= nexttiled(c
->next
)))
2032 main(int argc
, char *argv
[]) {
2033 if(argc
== 2 && !strcmp("-v", argv
[1]))
2034 die("dwm-"VERSION
", © 2006-2010 dwm engineers, see LICENSE for details\n");
2036 die("usage: dwm [-v]\n");
2037 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2038 fputs("warning: no locale support\n", stderr
);
2039 if(!(dpy
= XOpenDisplay(NULL
)))
2040 die("dwm: cannot open display\n");