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 basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
90 Bool isfixed
, isfloating
, isurgent
;
99 unsigned long norm
[ColLast
];
100 unsigned long sel
[ColLast
];
110 } DC
; /* draw context */
115 void (*func
)(const Arg
*);
121 void (*arrange
)(Monitor
*);
128 int by
; /* bar geometry */
129 int mx
, my
, mw
, mh
; /* screen size */
130 int wx
, wy
, ww
, wh
; /* window area */
131 unsigned int seltags
;
133 unsigned int tagset
[2];
146 const char *instance
;
153 /* function declarations */
154 static void applyrules(Client
*c
);
155 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
156 static void arrange(Monitor
*m
);
157 static void arrangemon(Monitor
*m
);
158 static void attach(Client
*c
);
159 static void attachstack(Client
*c
);
160 static void buttonpress(XEvent
*e
);
161 static void checkotherwm(void);
162 static void cleanup(void);
163 static void cleanupmon(Monitor
*mon
);
164 static void clearurgent(Client
*c
);
165 static void clientmessage(XEvent
*e
);
166 static void configure(Client
*c
);
167 static void configurenotify(XEvent
*e
);
168 static void configurerequest(XEvent
*e
);
169 static Monitor
*createmon(void);
170 static void destroynotify(XEvent
*e
);
171 static void detach(Client
*c
);
172 static void detachstack(Client
*c
);
173 static void die(const char *errstr
, ...);
174 static Monitor
*dirtomon(int dir
);
175 static void drawbar(Monitor
*m
);
176 static void drawbars(void);
177 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
178 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
179 static void enternotify(XEvent
*e
);
180 static void expose(XEvent
*e
);
181 static void focus(Client
*c
);
182 static void focusin(XEvent
*e
);
183 static void focusmon(const Arg
*arg
);
184 static void focusstack(const Arg
*arg
);
185 static unsigned long getcolor(const char *colstr
);
186 static Bool
getrootptr(int *x
, int *y
);
187 static long getstate(Window w
);
188 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
189 static void grabbuttons(Client
*c
, Bool focused
);
190 static void grabkeys(void);
191 static void initfont(const char *fontstr
);
192 static Bool
isprotodel(Client
*c
);
193 static void keypress(XEvent
*e
);
194 static void killclient(const Arg
*arg
);
195 static void manage(Window w
, XWindowAttributes
*wa
);
196 static void mappingnotify(XEvent
*e
);
197 static void maprequest(XEvent
*e
);
198 static void monocle(Monitor
*m
);
199 static void movemouse(const Arg
*arg
);
200 static Client
*nexttiled(Client
*c
);
201 static Monitor
*ptrtomon(int x
, int y
);
202 static void propertynotify(XEvent
*e
);
203 static void quit(const Arg
*arg
);
204 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
205 static void resizemouse(const Arg
*arg
);
206 static void restack(Monitor
*m
);
207 static void run(void);
208 static void scan(void);
209 static void sendmon(Client
*c
, Monitor
*m
);
210 static void setclientstate(Client
*c
, long state
);
211 static void setlayout(const Arg
*arg
);
212 static void setmfact(const Arg
*arg
);
213 static void setup(void);
214 static void showhide(Client
*c
);
215 static void sigchld(int unused
);
216 static void spawn(const Arg
*arg
);
217 static void tag(const Arg
*arg
);
218 static void tagmon(const Arg
*arg
);
219 static int textnw(const char *text
, unsigned int len
);
220 static void tile(Monitor
*);
221 static void togglebar(const Arg
*arg
);
222 static void togglefloating(const Arg
*arg
);
223 static void toggletag(const Arg
*arg
);
224 static void toggleview(const Arg
*arg
);
225 static void unfocus(Client
*c
);
226 static void unmanage(Client
*c
, Bool destroyed
);
227 static void unmapnotify(XEvent
*e
);
228 static Bool
updategeom(void);
229 static void updatebarpos(Monitor
*m
);
230 static void updatebars(void);
231 static void updatenumlockmask(void);
232 static void updatesizehints(Client
*c
);
233 static void updatestatus(void);
234 static void updatetitle(Client
*c
);
235 static void updatewmhints(Client
*c
);
236 static void view(const Arg
*arg
);
237 static Client
*wintoclient(Window w
);
238 static Monitor
*wintomon(Window w
);
239 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
240 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
241 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
242 static void zoom(const Arg
*arg
);
245 static const char broken
[] = "broken";
246 static char stext
[256];
248 static int sw
, sh
; /* X display screen geometry width, height */
249 static int bh
, blw
= 0; /* bar geometry */
250 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
251 static unsigned int numlockmask
= 0;
252 static void (*handler
[LASTEvent
]) (XEvent
*) = {
253 [ButtonPress
] = buttonpress
,
254 [ClientMessage
] = clientmessage
,
255 [ConfigureRequest
] = configurerequest
,
256 [ConfigureNotify
] = configurenotify
,
257 [DestroyNotify
] = destroynotify
,
258 [EnterNotify
] = enternotify
,
261 [KeyPress
] = keypress
,
262 [MappingNotify
] = mappingnotify
,
263 [MapRequest
] = maprequest
,
264 [PropertyNotify
] = propertynotify
,
265 [UnmapNotify
] = unmapnotify
267 static Atom wmatom
[WMLast
], netatom
[NetLast
];
269 static Bool running
= True
;
270 static Cursor cursor
[CurLast
];
273 static Monitor
*mons
= NULL
, *selmon
= NULL
;
276 /* configuration, allows nested code to access above variables */
279 /* compile-time check if all tags fit into an unsigned int bit array. */
280 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
282 /* function implementations */
284 applyrules(Client
*c
) {
285 const char *class, *instance
;
289 XClassHint ch
= { 0 };
292 c
->isfloating
= c
->tags
= 0;
293 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
294 class = ch
.res_class
? ch
.res_class
: broken
;
295 instance
= ch
.res_name
? ch
.res_name
: broken
;
296 for(i
= 0; i
< LENGTH(rules
); i
++) {
298 if((!r
->title
|| strstr(c
->name
, r
->title
))
299 && (!r
->class || strstr(class, r
->class))
300 && (!r
->instance
|| strstr(instance
, r
->instance
)))
302 c
->isfloating
= r
->isfloating
;
304 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
314 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
318 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
322 /* set minimum possible */
330 if(*x
+ *w
+ 2 * c
->bw
< 0)
332 if(*y
+ *h
+ 2 * c
->bw
< 0)
336 if(*x
> m
->mx
+ m
->mw
)
337 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
338 if(*y
> m
->my
+ m
->mh
)
339 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
340 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
342 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
349 if(resizehints
|| c
->isfloating
) {
350 /* see last two sentences in ICCCM 4.1.2.3 */
351 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
352 if(!baseismin
) { /* temporarily remove base dimensions */
356 /* adjust for aspect limits */
357 if(c
->mina
> 0 && c
->maxa
> 0) {
358 if(c
->maxa
< (float)*w
/ *h
)
359 *w
= *h
* c
->maxa
+ 0.5;
360 else if(c
->mina
< (float)*h
/ *w
)
361 *h
= *w
* c
->mina
+ 0.5;
363 if(baseismin
) { /* increment calculation requires this */
367 /* adjust for increment value */
372 /* restore base dimensions */
375 *w
= MAX(*w
, c
->minw
);
376 *h
= MAX(*h
, c
->minh
);
378 *w
= MIN(*w
, c
->maxw
);
380 *h
= MIN(*h
, c
->maxh
);
382 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
386 arrange(Monitor
*m
) {
389 else for(m
= mons
; m
; m
= m
->next
)
394 else for(m
= mons
; m
; m
= m
->next
)
399 arrangemon(Monitor
*m
) {
400 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
401 if(m
->lt
[m
->sellt
]->arrange
)
402 m
->lt
[m
->sellt
]->arrange(m
);
408 c
->next
= c
->mon
->clients
;
413 attachstack(Client
*c
) {
414 c
->snext
= c
->mon
->stack
;
419 buttonpress(XEvent
*e
) {
420 unsigned int i
, x
, click
;
424 XButtonPressedEvent
*ev
= &e
->xbutton
;
427 /* focus monitor if necessary */
428 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
429 unfocus(selmon
->sel
);
433 if(ev
->window
== selmon
->barwin
) {
437 } while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
438 if(i
< LENGTH(tags
)) {
442 else if(ev
->x
< x
+ blw
)
444 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
445 click
= ClkStatusText
;
449 else if((c
= wintoclient(ev
->window
))) {
451 click
= ClkClientWin
;
453 for(i
= 0; i
< LENGTH(buttons
); i
++)
454 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
455 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
456 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
462 xerrorxlib
= XSetErrorHandler(xerrorstart
);
463 /* this causes an error if some other window manager is running */
464 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
467 die("dwm: another window manager is already running\n");
468 XSetErrorHandler(xerror
);
475 Layout foo
= { "", NULL
};
479 selmon
->lt
[selmon
->sellt
] = &foo
;
480 for(m
= mons
; m
; m
= m
->next
)
482 unmanage(m
->stack
, False
);
484 XFreeFontSet(dpy
, dc
.font
.set
);
486 XFreeFont(dpy
, dc
.font
.xfont
);
487 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
488 XFreePixmap(dpy
, dc
.drawable
);
490 XFreeCursor(dpy
, cursor
[CurNormal
]);
491 XFreeCursor(dpy
, cursor
[CurResize
]);
492 XFreeCursor(dpy
, cursor
[CurMove
]);
496 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
500 cleanupmon(Monitor
*mon
) {
506 for(m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
509 XUnmapWindow(dpy
, mon
->barwin
);
510 XDestroyWindow(dpy
, mon
->barwin
);
515 clearurgent(Client
*c
) {
519 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
521 wmh
->flags
&= ~XUrgencyHint
;
522 XSetWMHints(dpy
, c
->win
, wmh
);
527 configure(Client
*c
) {
530 ce
.type
= ConfigureNotify
;
538 ce
.border_width
= c
->bw
;
540 ce
.override_redirect
= False
;
541 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
545 configurenotify(XEvent
*e
) {
547 XConfigureEvent
*ev
= &e
->xconfigure
;
549 if(ev
->window
== root
) {
554 XFreePixmap(dpy
, dc
.drawable
);
555 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
557 for(m
= mons
; m
; m
= m
->next
)
558 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
565 configurerequest(XEvent
*e
) {
568 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
571 if((c
= wintoclient(ev
->window
))) {
572 if(ev
->value_mask
& CWBorderWidth
)
573 c
->bw
= ev
->border_width
;
574 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
576 if(ev
->value_mask
& CWX
)
577 c
->x
= m
->mx
+ ev
->x
;
578 if(ev
->value_mask
& CWY
)
579 c
->y
= m
->my
+ ev
->y
;
580 if(ev
->value_mask
& CWWidth
)
582 if(ev
->value_mask
& CWHeight
)
584 if((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
585 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
586 if((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
587 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
588 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
591 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
599 wc
.width
= ev
->width
;
600 wc
.height
= ev
->height
;
601 wc
.border_width
= ev
->border_width
;
602 wc
.sibling
= ev
->above
;
603 wc
.stack_mode
= ev
->detail
;
604 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
613 if(!(m
= (Monitor
*)calloc(1, sizeof(Monitor
))))
614 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
615 m
->tagset
[0] = m
->tagset
[1] = 1;
617 m
->showbar
= showbar
;
619 m
->lt
[0] = &layouts
[0];
620 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
621 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
626 destroynotify(XEvent
*e
) {
628 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
630 if((c
= wintoclient(ev
->window
)))
638 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
643 detachstack(Client
*c
) {
646 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
649 if(c
== c
->mon
->sel
) {
650 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
656 die(const char *errstr
, ...) {
659 va_start(ap
, errstr
);
660 vfprintf(stderr
, errstr
, ap
);
670 if(!(m
= selmon
->next
))
675 for(m
= mons
; m
->next
; m
= m
->next
);
677 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
683 drawbar(Monitor
*m
) {
685 unsigned int i
, occ
= 0, urg
= 0;
689 for(c
= m
->clients
; c
; c
= c
->next
) {
695 for(i
= 0; i
< LENGTH(tags
); i
++) {
696 dc
.w
= TEXTW(tags
[i
]);
697 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
698 drawtext(tags
[i
], col
, urg
& 1 << i
);
699 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
700 occ
& 1 << i
, urg
& 1 << i
, col
);
703 dc
.w
= blw
= TEXTW(m
->ltsymbol
);
704 drawtext(m
->ltsymbol
, dc
.norm
, False
);
707 if(m
== selmon
) { /* status is only drawn on selected monitor */
714 drawtext(stext
, dc
.norm
, False
);
718 if((dc
.w
= dc
.x
- x
) > bh
) {
721 col
= m
== selmon
? dc
.sel
: dc
.norm
;
722 drawtext(m
->sel
->name
, col
, False
);
723 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
726 drawtext(NULL
, dc
.norm
, False
);
728 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
736 for(m
= mons
; m
; m
= m
->next
)
741 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
744 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
746 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
747 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
748 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
752 r
.width
= r
.height
= x
+ 1;
753 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
756 r
.width
= r
.height
= x
;
757 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
762 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
764 int i
, x
, y
, h
, len
, olen
;
765 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
767 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
768 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
772 h
= dc
.font
.ascent
+ dc
.font
.descent
;
773 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
775 /* shorten text if necessary */
776 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
779 memcpy(buf
, text
, len
);
781 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
782 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
784 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
786 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
790 enternotify(XEvent
*e
) {
793 XCrossingEvent
*ev
= &e
->xcrossing
;
795 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
797 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
798 unfocus(selmon
->sel
);
801 if((c
= wintoclient(ev
->window
)))
810 XExposeEvent
*ev
= &e
->xexpose
;
812 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
818 if(!c
|| !ISVISIBLE(c
))
819 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
821 unfocus(selmon
->sel
);
829 grabbuttons(c
, True
);
830 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
831 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
834 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
840 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
841 XFocusChangeEvent
*ev
= &e
->xfocus
;
843 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
844 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
848 focusmon(const Arg
*arg
) {
853 if((m
= dirtomon(arg
->i
)) == selmon
)
855 unfocus(selmon
->sel
);
861 focusstack(const Arg
*arg
) {
862 Client
*c
= NULL
, *i
;
867 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
869 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
872 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
876 for(; i
; i
= i
->next
)
887 getcolor(const char *colstr
) {
888 Colormap cmap
= DefaultColormap(dpy
, screen
);
891 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
892 die("error, cannot allocate color '%s'\n", colstr
);
897 getrootptr(int *x
, int *y
) {
902 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
909 unsigned char *p
= NULL
;
910 unsigned long n
, extra
;
913 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
914 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
915 if(status
!= Success
)
924 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
929 if(!text
|| size
== 0)
932 XGetTextProperty(dpy
, w
, &name
, atom
);
935 if(name
.encoding
== XA_STRING
)
936 strncpy(text
, (char *)name
.value
, size
- 1);
938 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
939 strncpy(text
, *list
, size
- 1);
940 XFreeStringList(list
);
943 text
[size
- 1] = '\0';
949 grabbuttons(Client
*c
, Bool focused
) {
953 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
954 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
956 for(i
= 0; i
< LENGTH(buttons
); i
++)
957 if(buttons
[i
].click
== ClkClientWin
)
958 for(j
= 0; j
< LENGTH(modifiers
); j
++)
959 XGrabButton(dpy
, buttons
[i
].button
,
960 buttons
[i
].mask
| modifiers
[j
],
961 c
->win
, False
, BUTTONMASK
,
962 GrabModeAsync
, GrabModeSync
, None
, None
);
965 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
966 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
975 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
978 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
979 for(i
= 0; i
< LENGTH(keys
); i
++) {
980 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
981 for(j
= 0; j
< LENGTH(modifiers
); j
++)
982 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
983 True
, GrabModeAsync
, GrabModeAsync
);
989 initfont(const char *fontstr
) {
990 char *def
, **missing
;
994 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
997 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
998 XFreeStringList(missing
);
1001 XFontSetExtents
*font_extents
;
1002 XFontStruct
**xfonts
;
1005 dc
.font
.ascent
= dc
.font
.descent
= 0;
1006 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
1007 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
1008 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
1009 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
1010 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1015 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1016 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1017 die("error, cannot load font: '%s'\n", fontstr
);
1018 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1019 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1021 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1025 isprotodel(Client
*c
) {
1030 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1031 for(i
= 0; !ret
&& i
< n
; i
++)
1032 if(protocols
[i
] == wmatom
[WMDelete
])
1041 isuniquegeom(XineramaScreenInfo
*unique
, size_t len
, XineramaScreenInfo
*info
) {
1044 for(i
= 0; i
< len
; i
++)
1045 if(unique
[i
].x_org
== info
->x_org
&& unique
[i
].y_org
== info
->y_org
1046 && unique
[i
].width
== info
->width
&& unique
[i
].height
== info
->height
)
1050 #endif /* XINERAMA */
1053 keypress(XEvent
*e
) {
1059 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1060 for(i
= 0; i
< LENGTH(keys
); i
++)
1061 if(keysym
== keys
[i
].keysym
1062 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1064 keys
[i
].func(&(keys
[i
].arg
));
1068 killclient(const Arg
*arg
) {
1073 if(isprotodel(selmon
->sel
)) {
1074 ev
.type
= ClientMessage
;
1075 ev
.xclient
.window
= selmon
->sel
->win
;
1076 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1077 ev
.xclient
.format
= 32;
1078 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1079 ev
.xclient
.data
.l
[1] = CurrentTime
;
1080 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1084 XSetErrorHandler(xerrordummy
);
1085 XSetCloseDownMode(dpy
, DestroyAll
);
1086 XKillClient(dpy
, selmon
->sel
->win
);
1088 XSetErrorHandler(xerror
);
1094 manage(Window w
, XWindowAttributes
*wa
) {
1096 Client
*c
, *t
= NULL
;
1097 Window trans
= None
;
1100 if(!(c
= malloc(sizeof(Client
))))
1101 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1105 if(XGetTransientForHint(dpy
, w
, &trans
))
1106 t
= wintoclient(trans
);
1116 c
->x
= wa
->x
+ c
->mon
->wx
;
1117 c
->y
= wa
->y
+ c
->mon
->wy
;
1120 c
->oldbw
= wa
->border_width
;
1121 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1127 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1128 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1129 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1130 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1131 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1132 /* only fix client y-offset, if the client center might cover the bar */
1133 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1134 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1137 wc
.border_width
= c
->bw
;
1138 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1139 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1140 configure(c
); /* propagates border_width, if size doesn't change */
1142 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1143 grabbuttons(c
, False
);
1145 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1147 XRaiseWindow(dpy
, c
->win
);
1150 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1151 XMapWindow(dpy
, c
->win
);
1152 setclientstate(c
, NormalState
);
1157 mappingnotify(XEvent
*e
) {
1158 XMappingEvent
*ev
= &e
->xmapping
;
1160 XRefreshKeyboardMapping(ev
);
1161 if(ev
->request
== MappingKeyboard
)
1166 maprequest(XEvent
*e
) {
1167 static XWindowAttributes wa
;
1168 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1170 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1172 if(wa
.override_redirect
)
1174 if(!wintoclient(ev
->window
))
1175 manage(ev
->window
, &wa
);
1179 monocle(Monitor
*m
) {
1183 for(c
= m
->clients
; c
; c
= c
->next
)
1186 if(n
> 0) /* override layout symbol */
1187 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1188 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1189 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1193 movemouse(const Arg
*arg
) {
1194 int x
, y
, ocx
, ocy
, nx
, ny
;
1199 if(!(c
= selmon
->sel
))
1204 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1205 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1207 if(!getrootptr(&x
, &y
))
1210 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1212 case ConfigureRequest
:
1215 handler
[ev
.type
](&ev
);
1218 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1219 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1220 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1221 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1222 if(abs(selmon
->wx
- nx
) < snap
)
1224 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1225 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1226 if(abs(selmon
->wy
- ny
) < snap
)
1228 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1229 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1230 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1231 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1232 togglefloating(NULL
);
1234 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1235 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1238 } while(ev
.type
!= ButtonRelease
);
1239 XUngrabPointer(dpy
, CurrentTime
);
1240 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1248 nexttiled(Client
*c
) {
1249 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1254 ptrtomon(int x
, int y
) {
1257 for(m
= mons
; m
; m
= m
->next
)
1258 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1264 propertynotify(XEvent
*e
) {
1267 XPropertyEvent
*ev
= &e
->xproperty
;
1269 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1271 else if(ev
->state
== PropertyDelete
)
1272 return; /* ignore */
1273 else if((c
= wintoclient(ev
->window
))) {
1276 case XA_WM_TRANSIENT_FOR
:
1277 XGetTransientForHint(dpy
, c
->win
, &trans
);
1278 if(!c
->isfloating
&& (c
->isfloating
= (wintoclient(trans
) != NULL
)))
1281 case XA_WM_NORMAL_HINTS
:
1289 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1291 if(c
== c
->mon
->sel
)
1298 clientmessage(XEvent
*e
) {
1299 XClientMessageEvent
*cme
= &e
->xclient
;
1301 if(cme
->message_type
== netatom
[NetWMState
] && cme
->data
.l
[1] == netatom
[NetWMFullscreen
]) {
1303 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
1304 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1306 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
1307 PropModeReplace
, (unsigned char*)0, 0);
1312 quit(const Arg
*arg
) {
1317 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1320 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
)) {
1323 c
->w
= wc
.width
= w
;
1324 c
->h
= wc
.height
= h
;
1325 wc
.border_width
= c
->bw
;
1326 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1333 resizemouse(const Arg
*arg
) {
1340 if(!(c
= selmon
->sel
))
1345 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1346 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1348 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1350 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1352 case ConfigureRequest
:
1355 handler
[ev
.type
](&ev
);
1358 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1359 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1360 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1361 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
)
1363 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1364 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1365 togglefloating(NULL
);
1367 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1368 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1371 } while(ev
.type
!= ButtonRelease
);
1372 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1373 XUngrabPointer(dpy
, CurrentTime
);
1374 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1375 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1383 restack(Monitor
*m
) {
1391 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1392 XRaiseWindow(dpy
, m
->sel
->win
);
1393 if(m
->lt
[m
->sellt
]->arrange
) {
1394 wc
.stack_mode
= Below
;
1395 wc
.sibling
= m
->barwin
;
1396 for(c
= m
->stack
; c
; c
= c
->snext
)
1397 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1398 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1399 wc
.sibling
= c
->win
;
1403 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1409 /* main event loop */
1411 while(running
&& !XNextEvent(dpy
, &ev
)) {
1412 if(handler
[ev
.type
])
1413 handler
[ev
.type
](&ev
); /* call handler */
1419 unsigned int i
, num
;
1420 Window d1
, d2
, *wins
= NULL
;
1421 XWindowAttributes wa
;
1423 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1424 for(i
= 0; i
< num
; i
++) {
1425 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1426 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1428 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1429 manage(wins
[i
], &wa
);
1431 for(i
= 0; i
< num
; i
++) { /* now the transients */
1432 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1434 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1435 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1436 manage(wins
[i
], &wa
);
1444 sendmon(Client
*c
, Monitor
*m
) {
1451 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1459 setclientstate(Client
*c
, long state
) {
1460 long data
[] = { state
, None
};
1462 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1463 PropModeReplace
, (unsigned char *)data
, 2);
1467 setlayout(const Arg
*arg
) {
1468 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1471 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1472 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1479 /* arg > 1.0 will set mfact absolutly */
1481 setmfact(const Arg
*arg
) {
1484 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1486 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1487 if(f
< 0.1 || f
> 0.9)
1495 XSetWindowAttributes wa
;
1497 /* clean up any zombies immediately */
1501 screen
= DefaultScreen(dpy
);
1502 root
= RootWindow(dpy
, screen
);
1504 sw
= DisplayWidth(dpy
, screen
);
1505 sh
= DisplayHeight(dpy
, screen
);
1506 bh
= dc
.h
= dc
.font
.height
+ 2;
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 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1513 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1514 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1515 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1517 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1518 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1519 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1520 /* init appearance */
1521 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1522 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1523 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1524 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1525 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1526 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1527 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1528 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1529 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1531 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1535 /* EWMH support per view */
1536 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1537 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1538 /* select for events */
1539 wa
.cursor
= cursor
[CurNormal
];
1540 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1541 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1542 |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
)
1555 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1558 else { /* hide clients bottom up */
1560 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1566 sigchld(int unused
) {
1567 if(signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1568 die("Can't install SIGCHLD handler");
1569 while(0 < waitpid(-1, NULL
, WNOHANG
));
1573 spawn(const Arg
*arg
) {
1576 close(ConnectionNumber(dpy
));
1578 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1579 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1586 tag(const Arg
*arg
) {
1587 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1588 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1594 tagmon(const Arg
*arg
) {
1595 if(!selmon
->sel
|| !mons
->next
)
1597 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1601 textnw(const char *text
, unsigned int len
) {
1605 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1608 return XTextWidth(dc
.font
.xfont
, text
, len
);
1617 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1621 c
= nexttiled(m
->clients
);
1622 mw
= m
->mfact
* m
->ww
;
1623 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1627 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1629 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1633 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1634 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1635 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1637 y
= c
->y
+ HEIGHT(c
);
1642 togglebar(const Arg
*arg
) {
1643 selmon
->showbar
= !selmon
->showbar
;
1644 updatebarpos(selmon
);
1645 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1650 togglefloating(const Arg
*arg
) {
1653 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1654 if(selmon
->sel
->isfloating
)
1655 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1656 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1661 toggletag(const Arg
*arg
) {
1662 unsigned int newtags
;
1666 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1668 selmon
->sel
->tags
= newtags
;
1674 toggleview(const Arg
*arg
) {
1675 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1678 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1684 unfocus(Client
*c
) {
1687 grabbuttons(c
, False
);
1688 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1689 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1693 unmanage(Client
*c
, Bool destroyed
) {
1694 Monitor
*m
= c
->mon
;
1697 /* The server grab construct avoids race conditions. */
1701 wc
.border_width
= c
->oldbw
;
1703 XSetErrorHandler(xerrordummy
);
1704 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1705 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1706 setclientstate(c
, WithdrawnState
);
1708 XSetErrorHandler(xerror
);
1717 unmapnotify(XEvent
*e
) {
1719 XUnmapEvent
*ev
= &e
->xunmap
;
1721 if((c
= wintoclient(ev
->window
)))
1728 XSetWindowAttributes wa
;
1730 wa
.override_redirect
= True
;
1731 wa
.background_pixmap
= ParentRelative
;
1732 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1733 for(m
= mons
; m
; m
= m
->next
) {
1734 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1735 CopyFromParent
, DefaultVisual(dpy
, screen
),
1736 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1737 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1738 XMapRaised(dpy
, m
->barwin
);
1743 updatebarpos(Monitor
*m
) {
1748 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1749 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1760 if(XineramaIsActive(dpy
)) {
1764 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1765 XineramaScreenInfo
*unique
= NULL
;
1767 info
= XineramaQueryScreens(dpy
, &nn
);
1768 for(n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1769 /* only consider unique geometries as separate screens */
1770 if(!(unique
= (XineramaScreenInfo
*)malloc(sizeof(XineramaScreenInfo
) * nn
)))
1771 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo
) * nn
);
1772 for(i
= 0, j
= 0; i
< nn
; i
++)
1773 if(isuniquegeom(unique
, j
, &info
[i
]))
1774 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1778 for(i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1779 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1781 m
->next
= createmon();
1785 for(i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1787 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1788 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1792 m
->mx
= m
->wx
= unique
[i
].x_org
;
1793 m
->my
= m
->wy
= unique
[i
].y_org
;
1794 m
->mw
= m
->ww
= unique
[i
].width
;
1795 m
->mh
= m
->wh
= unique
[i
].height
;
1799 else { /* less monitors available nn < n */
1800 for(i
= nn
; i
< n
; i
++) {
1801 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1805 m
->clients
= c
->next
;
1819 #endif /* XINERAMA */
1820 /* default monitor setup */
1824 if(mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1826 mons
->mw
= mons
->ww
= sw
;
1827 mons
->mh
= mons
->wh
= sh
;
1833 selmon
= wintomon(root
);
1839 updatenumlockmask(void) {
1841 XModifierKeymap
*modmap
;
1844 modmap
= XGetModifierMapping(dpy
);
1845 for(i
= 0; i
< 8; i
++)
1846 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1847 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1848 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1849 numlockmask
= (1 << i
);
1850 XFreeModifiermap(modmap
);
1854 updatesizehints(Client
*c
) {
1858 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1859 /* size is uninitialized, ensure that size.flags aren't used */
1861 if(size
.flags
& PBaseSize
) {
1862 c
->basew
= size
.base_width
;
1863 c
->baseh
= size
.base_height
;
1865 else if(size
.flags
& PMinSize
) {
1866 c
->basew
= size
.min_width
;
1867 c
->baseh
= size
.min_height
;
1870 c
->basew
= c
->baseh
= 0;
1871 if(size
.flags
& PResizeInc
) {
1872 c
->incw
= size
.width_inc
;
1873 c
->inch
= size
.height_inc
;
1876 c
->incw
= c
->inch
= 0;
1877 if(size
.flags
& PMaxSize
) {
1878 c
->maxw
= size
.max_width
;
1879 c
->maxh
= size
.max_height
;
1882 c
->maxw
= c
->maxh
= 0;
1883 if(size
.flags
& PMinSize
) {
1884 c
->minw
= size
.min_width
;
1885 c
->minh
= size
.min_height
;
1887 else if(size
.flags
& PBaseSize
) {
1888 c
->minw
= size
.base_width
;
1889 c
->minh
= size
.base_height
;
1892 c
->minw
= c
->minh
= 0;
1893 if(size
.flags
& PAspect
) {
1894 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1895 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1898 c
->maxa
= c
->mina
= 0.0;
1899 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1900 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1904 updatetitle(Client
*c
) {
1905 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1906 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1907 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1908 strcpy(c
->name
, broken
);
1912 updatestatus(void) {
1913 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1914 strcpy(stext
, "dwm-"VERSION
);
1919 updatewmhints(Client
*c
) {
1922 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1923 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1924 wmh
->flags
&= ~XUrgencyHint
;
1925 XSetWMHints(dpy
, c
->win
, wmh
);
1928 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1934 view(const Arg
*arg
) {
1935 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1937 selmon
->seltags
^= 1; /* toggle sel tagset */
1938 if(arg
->ui
& TAGMASK
)
1939 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1944 wintoclient(Window w
) {
1948 for(m
= mons
; m
; m
= m
->next
)
1949 for(c
= m
->clients
; c
; c
= c
->next
)
1956 wintomon(Window w
) {
1961 if(w
== root
&& getrootptr(&x
, &y
))
1962 return ptrtomon(x
, y
);
1963 for(m
= mons
; m
; m
= m
->next
)
1966 if((c
= wintoclient(w
)))
1971 /* There's no way to check accesses to destroyed windows, thus those cases are
1972 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1973 * default error handler, which may call exit. */
1975 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1976 if(ee
->error_code
== BadWindow
1977 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1978 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1979 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1980 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1981 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1982 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1983 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1984 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1986 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1987 ee
->request_code
, ee
->error_code
);
1988 return xerrorxlib(dpy
, ee
); /* may call exit */
1992 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1996 /* Startup Error handler to check if another window manager
1997 * is already running. */
1999 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
2005 zoom(const Arg
*arg
) {
2006 Client
*c
= selmon
->sel
;
2008 if(!selmon
->lt
[selmon
->sellt
]->arrange
2009 || selmon
->lt
[selmon
->sellt
]->arrange
== monocle
2010 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2012 if(c
== nexttiled(selmon
->clients
))
2013 if(!c
|| !(c
= nexttiled(c
->next
)))
2022 main(int argc
, char *argv
[]) {
2023 if(argc
== 2 && !strcmp("-v", argv
[1]))
2024 die("dwm-"VERSION
", © 2006-2010 dwm engineers, see LICENSE for details\n");
2026 die("usage: dwm [-v]\n");
2027 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2028 fputs("warning: no locale support\n", stderr
);
2029 if(!(dpy
= XOpenDisplay(NULL
)))
2030 die("dwm: cannot open display\n");