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
, NetLast
}; /* EWMH atoms */
61 enum { WMProtocols
, WMDelete
, WMState
, WMLast
}; /* default atoms */
62 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
63 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
76 void (*func
)(const Arg
*arg
);
80 typedef struct Monitor Monitor
;
81 typedef struct Client Client
;
86 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
89 Bool isfixed
, isfloating
, isurgent
;
98 unsigned long norm
[ColLast
];
99 unsigned long sel
[ColLast
];
109 } DC
; /* draw context */
114 void (*func
)(const Arg
*);
120 void (*arrange
)(Monitor
*);
127 int by
; /* bar geometry */
128 int mx
, my
, mw
, mh
; /* screen size */
129 int wx
, wy
, ww
, wh
; /* window area */
130 unsigned int seltags
;
132 unsigned int tagset
[2];
145 const char *instance
;
152 /* function declarations */
153 static void applyrules(Client
*c
);
154 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
155 static void arrange(void);
156 static void attach(Client
*c
);
157 static void attachstack(Client
*c
);
158 static void buttonpress(XEvent
*e
);
159 static void checkotherwm(void);
160 static void cleanup(void);
161 static void cleanupmon(Monitor
*mon
);
162 static void clearurgent(Client
*c
);
163 static void configure(Client
*c
);
164 static void configurenotify(XEvent
*e
);
165 static void configurerequest(XEvent
*e
);
166 static Monitor
*createmon(void);
167 static void destroynotify(XEvent
*e
);
168 static void detach(Client
*c
);
169 static void detachstack(Client
*c
);
170 static void die(const char *errstr
, ...);
171 static Monitor
*dirtomon(int dir
);
172 static void drawbar(Monitor
*m
);
173 static void drawbars(void);
174 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
175 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
176 static void enternotify(XEvent
*e
);
177 static void expose(XEvent
*e
);
178 static void focus(Client
*c
);
179 static void focusin(XEvent
*e
);
180 static void focusmon(const Arg
*arg
);
181 static void focusstack(const Arg
*arg
);
182 static unsigned long getcolor(const char *colstr
);
183 static Bool
getrootptr(int *x
, int *y
);
184 static long getstate(Window w
);
185 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
186 static void grabbuttons(Client
*c
, Bool focused
);
187 static void grabkeys(void);
188 static void initfont(const char *fontstr
);
189 static Bool
isprotodel(Client
*c
);
190 static void keypress(XEvent
*e
);
191 static void killclient(const Arg
*arg
);
192 static void manage(Window w
, XWindowAttributes
*wa
);
193 static void mappingnotify(XEvent
*e
);
194 static void maprequest(XEvent
*e
);
195 static void monocle(Monitor
*m
);
196 static void movemouse(const Arg
*arg
);
197 static Client
*nexttiled(Client
*c
);
198 static Monitor
*ptrtomon(int x
, int y
);
199 static void propertynotify(XEvent
*e
);
200 static void quit(const Arg
*arg
);
201 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
202 static void resizemouse(const Arg
*arg
);
203 static void restack(Monitor
*m
);
204 static void run(void);
205 static void scan(void);
206 static void sendmon(Client
*c
, Monitor
*m
);
207 static void setclientstate(Client
*c
, long state
);
208 static void setlayout(const Arg
*arg
);
209 static void setmfact(const Arg
*arg
);
210 static void setup(void);
211 static void showhide(Client
*c
);
212 static void sigchld(int unused
);
213 static void spawn(const Arg
*arg
);
214 static void tag(const Arg
*arg
);
215 static void tagmon(const Arg
*arg
);
216 static int textnw(const char *text
, unsigned int len
);
217 static void tile(Monitor
*);
218 static void togglebar(const Arg
*arg
);
219 static void togglefloating(const Arg
*arg
);
220 static void toggletag(const Arg
*arg
);
221 static void toggleview(const Arg
*arg
);
222 static void unfocus(Client
*c
);
223 static void unmanage(Client
*c
, Bool destroyed
);
224 static void unmapnotify(XEvent
*e
);
225 static Bool
updategeom(void);
226 static void updatebarpos(Monitor
*m
);
227 static void updatebars(void);
228 static void updatenumlockmask(void);
229 static void updatesizehints(Client
*c
);
230 static void updatestatus(void);
231 static void updatetitle(Client
*c
);
232 static void updatewmhints(Client
*c
);
233 static void view(const Arg
*arg
);
234 static Client
*wintoclient(Window w
);
235 static Monitor
*wintomon(Window w
);
236 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
237 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
238 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
239 static void zoom(const Arg
*arg
);
242 static const char broken
[] = "broken";
243 static char stext
[256];
245 static int sw
, sh
; /* X display screen geometry width, height */
246 static int bh
, blw
= 0; /* bar geometry */
247 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
248 static unsigned int numlockmask
= 0;
249 static void (*handler
[LASTEvent
]) (XEvent
*) = {
250 [ButtonPress
] = buttonpress
,
251 [ConfigureRequest
] = configurerequest
,
252 [ConfigureNotify
] = configurenotify
,
253 [DestroyNotify
] = destroynotify
,
254 [EnterNotify
] = enternotify
,
257 [KeyPress
] = keypress
,
258 [MappingNotify
] = mappingnotify
,
259 [MapRequest
] = maprequest
,
260 [PropertyNotify
] = propertynotify
,
261 [UnmapNotify
] = unmapnotify
263 static Atom wmatom
[WMLast
], netatom
[NetLast
];
265 static Bool running
= True
;
266 static Cursor cursor
[CurLast
];
269 static Monitor
*mons
= NULL
, *selmon
= NULL
;
272 /* configuration, allows nested code to access above variables */
275 /* compile-time check if all tags fit into an unsigned int bit array. */
276 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
278 /* function implementations */
280 applyrules(Client
*c
) {
281 const char *class, *instance
;
285 XClassHint ch
= { 0 };
288 c
->isfloating
= c
->tags
= 0;
289 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
290 class = ch
.res_class
? ch
.res_class
: broken
;
291 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
);
310 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
314 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
318 /* set minimum possible */
326 if(*x
+ *w
+ 2 * c
->bw
< 0)
328 if(*y
+ *h
+ 2 * c
->bw
< 0)
332 if(*x
> m
->mx
+ m
->mw
)
333 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
334 if(*y
> m
->my
+ m
->mh
)
335 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
336 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
338 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
345 if(resizehints
|| c
->isfloating
) {
346 /* see last two sentences in ICCCM 4.1.2.3 */
347 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
348 if(!baseismin
) { /* temporarily remove base dimensions */
352 /* adjust for aspect limits */
353 if(c
->mina
> 0 && c
->maxa
> 0) {
354 if(c
->maxa
< (float)*w
/ *h
)
355 *w
= *h
* c
->maxa
+ 0.5;
356 else if(c
->mina
< (float)*h
/ *w
)
357 *h
= *w
* c
->mina
+ 0.5;
359 if(baseismin
) { /* increment calculation requires this */
363 /* adjust for increment value */
368 /* restore base dimensions */
371 *w
= MAX(*w
, c
->minw
);
372 *h
= MAX(*h
, c
->minh
);
374 *w
= MIN(*w
, c
->maxw
);
376 *h
= MIN(*h
, c
->maxh
);
378 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
385 for(m
= mons
; m
; m
= m
->next
)
388 for(m
= mons
; m
; m
= m
->next
) {
389 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
390 if(m
->lt
[m
->sellt
]->arrange
)
391 m
->lt
[m
->sellt
]->arrange(m
);
398 c
->next
= c
->mon
->clients
;
403 attachstack(Client
*c
) {
404 c
->snext
= c
->mon
->stack
;
409 buttonpress(XEvent
*e
) {
410 unsigned int i
, x
, click
;
414 XButtonPressedEvent
*ev
= &e
->xbutton
;
417 /* focus monitor if necessary */
418 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
419 unfocus(selmon
->sel
);
423 if(ev
->window
== selmon
->barwin
) {
427 } while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
428 if(i
< LENGTH(tags
)) {
432 else if(ev
->x
< x
+ blw
)
434 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
435 click
= ClkStatusText
;
439 else if((c
= wintoclient(ev
->window
))) {
441 click
= ClkClientWin
;
443 for(i
= 0; i
< LENGTH(buttons
); i
++)
444 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
445 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
446 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
452 xerrorxlib
= XSetErrorHandler(xerrorstart
);
453 /* this causes an error if some other window manager is running */
454 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
457 die("dwm: another window manager is already running\n");
458 XSetErrorHandler(xerror
);
465 Layout foo
= { "", NULL
};
469 selmon
->lt
[selmon
->sellt
] = &foo
;
470 for(m
= mons
; m
; m
= m
->next
)
472 unmanage(m
->stack
, False
);
474 XFreeFontSet(dpy
, dc
.font
.set
);
476 XFreeFont(dpy
, dc
.font
.xfont
);
477 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
478 XFreePixmap(dpy
, dc
.drawable
);
480 XFreeCursor(dpy
, cursor
[CurNormal
]);
481 XFreeCursor(dpy
, cursor
[CurResize
]);
482 XFreeCursor(dpy
, cursor
[CurMove
]);
486 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
490 cleanupmon(Monitor
*mon
) {
496 for(m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
499 XUnmapWindow(dpy
, mon
->barwin
);
500 XDestroyWindow(dpy
, mon
->barwin
);
505 clearurgent(Client
*c
) {
509 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
511 wmh
->flags
&= ~XUrgencyHint
;
512 XSetWMHints(dpy
, c
->win
, wmh
);
517 configure(Client
*c
) {
520 ce
.type
= ConfigureNotify
;
528 ce
.border_width
= c
->bw
;
530 ce
.override_redirect
= False
;
531 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
535 configurenotify(XEvent
*e
) {
537 XConfigureEvent
*ev
= &e
->xconfigure
;
539 if(ev
->window
== root
) {
544 XFreePixmap(dpy
, dc
.drawable
);
545 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
547 for(m
= mons
; m
; m
= m
->next
)
548 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
555 configurerequest(XEvent
*e
) {
558 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
561 if((c
= wintoclient(ev
->window
))) {
562 if(ev
->value_mask
& CWBorderWidth
)
563 c
->bw
= ev
->border_width
;
564 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
566 if(ev
->value_mask
& CWX
)
567 c
->x
= m
->mx
+ ev
->x
;
568 if(ev
->value_mask
& CWY
)
569 c
->y
= m
->my
+ ev
->y
;
570 if(ev
->value_mask
& CWWidth
)
572 if(ev
->value_mask
& CWHeight
)
574 if((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
575 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
576 if((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
577 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
578 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
581 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
589 wc
.width
= ev
->width
;
590 wc
.height
= ev
->height
;
591 wc
.border_width
= ev
->border_width
;
592 wc
.sibling
= ev
->above
;
593 wc
.stack_mode
= ev
->detail
;
594 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
603 if(!(m
= (Monitor
*)calloc(1, sizeof(Monitor
))))
604 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
605 m
->tagset
[0] = m
->tagset
[1] = 1;
607 m
->showbar
= showbar
;
609 m
->lt
[0] = &layouts
[0];
610 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
611 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
616 destroynotify(XEvent
*e
) {
618 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
620 if((c
= wintoclient(ev
->window
)))
628 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
633 detachstack(Client
*c
) {
636 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
639 if(c
== c
->mon
->sel
) {
640 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
646 die(const char *errstr
, ...) {
649 va_start(ap
, errstr
);
650 vfprintf(stderr
, errstr
, ap
);
660 if(!(m
= selmon
->next
))
665 for(m
= mons
; m
->next
; m
= m
->next
);
667 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
673 drawbar(Monitor
*m
) {
675 unsigned int i
, occ
= 0, urg
= 0;
679 for(c
= m
->clients
; c
; c
= c
->next
) {
685 for(i
= 0; i
< LENGTH(tags
); i
++) {
686 dc
.w
= TEXTW(tags
[i
]);
687 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
688 drawtext(tags
[i
], col
, urg
& 1 << i
);
689 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
690 occ
& 1 << i
, urg
& 1 << i
, col
);
693 dc
.w
= blw
= TEXTW(m
->ltsymbol
);
694 drawtext(m
->ltsymbol
, dc
.norm
, False
);
697 if(m
== selmon
) { /* status is only drawn on selected monitor */
704 drawtext(stext
, dc
.norm
, False
);
708 if((dc
.w
= dc
.x
- x
) > bh
) {
711 col
= m
== selmon
? dc
.sel
: dc
.norm
;
712 drawtext(m
->sel
->name
, col
, False
);
713 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
716 drawtext(NULL
, dc
.norm
, False
);
718 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
726 for(m
= mons
; m
; m
= m
->next
)
731 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
734 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
736 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
737 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
738 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
742 r
.width
= r
.height
= x
+ 1;
743 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
746 r
.width
= r
.height
= x
;
747 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
752 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
754 int i
, x
, y
, h
, len
, olen
;
755 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
757 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
758 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
762 h
= dc
.font
.ascent
+ dc
.font
.descent
;
763 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
765 /* shorten text if necessary */
766 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
769 memcpy(buf
, text
, len
);
771 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
772 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
774 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
776 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
780 enternotify(XEvent
*e
) {
783 XCrossingEvent
*ev
= &e
->xcrossing
;
785 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
787 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
788 unfocus(selmon
->sel
);
791 if((c
= wintoclient(ev
->window
)))
800 XExposeEvent
*ev
= &e
->xexpose
;
802 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
808 if(!c
|| !ISVISIBLE(c
))
809 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
811 unfocus(selmon
->sel
);
819 grabbuttons(c
, True
);
820 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
821 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
824 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
830 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
831 XFocusChangeEvent
*ev
= &e
->xfocus
;
833 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
834 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
838 focusmon(const Arg
*arg
) {
843 m
= dirtomon(arg
->i
);
844 unfocus(selmon
->sel
);
850 focusstack(const Arg
*arg
) {
851 Client
*c
= NULL
, *i
;
856 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
858 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
861 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
865 for(; i
; i
= i
->next
)
876 getcolor(const char *colstr
) {
877 Colormap cmap
= DefaultColormap(dpy
, screen
);
880 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
881 die("error, cannot allocate color '%s'\n", colstr
);
886 getrootptr(int *x
, int *y
) {
891 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
898 unsigned char *p
= NULL
;
899 unsigned long n
, extra
;
902 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
903 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
904 if(status
!= Success
)
913 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
918 if(!text
|| size
== 0)
921 XGetTextProperty(dpy
, w
, &name
, atom
);
924 if(name
.encoding
== XA_STRING
)
925 strncpy(text
, (char *)name
.value
, size
- 1);
927 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
928 strncpy(text
, *list
, size
- 1);
929 XFreeStringList(list
);
932 text
[size
- 1] = '\0';
938 grabbuttons(Client
*c
, Bool focused
) {
942 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
943 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
945 for(i
= 0; i
< LENGTH(buttons
); i
++)
946 if(buttons
[i
].click
== ClkClientWin
)
947 for(j
= 0; j
< LENGTH(modifiers
); j
++)
948 XGrabButton(dpy
, buttons
[i
].button
,
949 buttons
[i
].mask
| modifiers
[j
],
950 c
->win
, False
, BUTTONMASK
,
951 GrabModeAsync
, GrabModeSync
, None
, None
);
954 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
955 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
964 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
967 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
968 for(i
= 0; i
< LENGTH(keys
); i
++) {
969 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
970 for(j
= 0; j
< LENGTH(modifiers
); j
++)
971 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
972 True
, GrabModeAsync
, GrabModeAsync
);
978 initfont(const char *fontstr
) {
979 char *def
, **missing
;
983 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
986 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
987 XFreeStringList(missing
);
990 XFontSetExtents
*font_extents
;
991 XFontStruct
**xfonts
;
994 dc
.font
.ascent
= dc
.font
.descent
= 0;
995 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
996 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
997 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
998 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
999 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1004 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1005 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1006 die("error, cannot load font: '%s'\n", fontstr
);
1007 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1008 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1010 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1014 isprotodel(Client
*c
) {
1019 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1020 for(i
= 0; !ret
&& i
< n
; i
++)
1021 if(protocols
[i
] == wmatom
[WMDelete
])
1030 isuniquegeom(XineramaScreenInfo
*unique
, size_t len
, XineramaScreenInfo
*info
) {
1033 for(i
= 0; i
< len
; i
++)
1034 if(unique
[i
].x_org
== info
->x_org
&& unique
[i
].y_org
== info
->y_org
1035 && unique
[i
].width
== info
->width
&& unique
[i
].height
== info
->height
)
1039 #endif /* XINERAMA */
1042 keypress(XEvent
*e
) {
1048 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1049 for(i
= 0; i
< LENGTH(keys
); i
++)
1050 if(keysym
== keys
[i
].keysym
1051 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1053 keys
[i
].func(&(keys
[i
].arg
));
1057 killclient(const Arg
*arg
) {
1062 if(isprotodel(selmon
->sel
)) {
1063 ev
.type
= ClientMessage
;
1064 ev
.xclient
.window
= selmon
->sel
->win
;
1065 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1066 ev
.xclient
.format
= 32;
1067 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1068 ev
.xclient
.data
.l
[1] = CurrentTime
;
1069 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1073 XSetErrorHandler(xerrordummy
);
1074 XSetCloseDownMode(dpy
, DestroyAll
);
1075 XKillClient(dpy
, selmon
->sel
->win
);
1077 XSetErrorHandler(xerror
);
1083 manage(Window w
, XWindowAttributes
*wa
) {
1085 Client
*c
, *t
= NULL
;
1086 Window trans
= None
;
1089 if(!(c
= malloc(sizeof(Client
))))
1090 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1094 if(XGetTransientForHint(dpy
, w
, &trans
))
1095 t
= wintoclient(trans
);
1105 c
->x
= wa
->x
+ c
->mon
->wx
;
1106 c
->y
= wa
->y
+ c
->mon
->wy
;
1109 c
->oldbw
= wa
->border_width
;
1110 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1116 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1117 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1118 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1119 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1120 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1121 /* only fix client y-offset, if the client center might cover the bar */
1122 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1123 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1126 wc
.border_width
= c
->bw
;
1127 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1128 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1129 configure(c
); /* propagates border_width, if size doesn't change */
1131 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1132 grabbuttons(c
, False
);
1134 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1136 XRaiseWindow(dpy
, c
->win
);
1139 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1140 XMapWindow(dpy
, c
->win
);
1141 setclientstate(c
, NormalState
);
1146 mappingnotify(XEvent
*e
) {
1147 XMappingEvent
*ev
= &e
->xmapping
;
1149 XRefreshKeyboardMapping(ev
);
1150 if(ev
->request
== MappingKeyboard
)
1155 maprequest(XEvent
*e
) {
1156 static XWindowAttributes wa
;
1157 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1159 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1161 if(wa
.override_redirect
)
1163 if(!wintoclient(ev
->window
))
1164 manage(ev
->window
, &wa
);
1168 monocle(Monitor
*m
) {
1172 for(c
= m
->clients
; c
; c
= c
->next
)
1175 if(n
> 0) /* override layout symbol */
1176 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1177 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1178 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1182 movemouse(const Arg
*arg
) {
1183 int x
, y
, ocx
, ocy
, nx
, ny
;
1188 if(!(c
= selmon
->sel
))
1193 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1194 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1196 if(!getrootptr(&x
, &y
))
1199 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1201 case ConfigureRequest
:
1204 handler
[ev
.type
](&ev
);
1207 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1208 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1209 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1210 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1211 if(abs(selmon
->wx
- nx
) < snap
)
1213 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1214 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1215 if(abs(selmon
->wy
- ny
) < snap
)
1217 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1218 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1219 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1220 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1221 togglefloating(NULL
);
1223 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1224 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1227 } while(ev
.type
!= ButtonRelease
);
1228 XUngrabPointer(dpy
, CurrentTime
);
1229 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1237 nexttiled(Client
*c
) {
1238 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1243 ptrtomon(int x
, int y
) {
1246 for(m
= mons
; m
; m
= m
->next
)
1247 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1253 propertynotify(XEvent
*e
) {
1256 XPropertyEvent
*ev
= &e
->xproperty
;
1258 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1260 else if(ev
->state
== PropertyDelete
)
1261 return; /* ignore */
1262 else if((c
= wintoclient(ev
->window
))) {
1265 case XA_WM_TRANSIENT_FOR
:
1266 XGetTransientForHint(dpy
, c
->win
, &trans
);
1267 if(!c
->isfloating
&& (c
->isfloating
= (wintoclient(trans
) != NULL
)))
1270 case XA_WM_NORMAL_HINTS
:
1278 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1280 if(c
== c
->mon
->sel
)
1287 quit(const Arg
*arg
) {
1292 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1295 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
)) {
1298 c
->w
= wc
.width
= w
;
1299 c
->h
= wc
.height
= h
;
1300 wc
.border_width
= c
->bw
;
1301 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1308 resizemouse(const Arg
*arg
) {
1315 if(!(c
= selmon
->sel
))
1320 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1321 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1323 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1325 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1327 case ConfigureRequest
:
1330 handler
[ev
.type
](&ev
);
1333 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1334 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1335 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1336 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
)
1338 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1339 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1340 togglefloating(NULL
);
1342 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1343 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1346 } while(ev
.type
!= ButtonRelease
);
1347 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1348 XUngrabPointer(dpy
, CurrentTime
);
1349 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1350 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1358 restack(Monitor
*m
) {
1366 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1367 XRaiseWindow(dpy
, m
->sel
->win
);
1368 if(m
->lt
[m
->sellt
]->arrange
) {
1369 wc
.stack_mode
= Below
;
1370 wc
.sibling
= m
->barwin
;
1371 for(c
= m
->stack
; c
; c
= c
->snext
)
1372 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1373 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1374 wc
.sibling
= c
->win
;
1378 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1385 /* main event loop */
1387 while(running
&& !XNextEvent(dpy
, &ev
))
1388 if(handler
[ev
.type
])
1389 handler
[ev
.type
](&ev
); /* call handler */
1394 unsigned int i
, num
;
1395 Window d1
, d2
, *wins
= NULL
;
1396 XWindowAttributes wa
;
1398 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1399 for(i
= 0; i
< num
; i
++) {
1400 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1401 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1403 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1404 manage(wins
[i
], &wa
);
1406 for(i
= 0; i
< num
; i
++) { /* now the transients */
1407 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1409 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1410 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1411 manage(wins
[i
], &wa
);
1419 sendmon(Client
*c
, Monitor
*m
) {
1426 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1434 setclientstate(Client
*c
, long state
) {
1435 long data
[] = { state
, None
};
1437 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1438 PropModeReplace
, (unsigned char *)data
, 2);
1442 setlayout(const Arg
*arg
) {
1443 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1446 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1453 /* arg > 1.0 will set mfact absolutly */
1455 setmfact(const Arg
*arg
) {
1458 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1460 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1461 if(f
< 0.1 || f
> 0.9)
1469 XSetWindowAttributes wa
;
1471 /* clean up any zombies immediately */
1475 screen
= DefaultScreen(dpy
);
1476 root
= RootWindow(dpy
, screen
);
1478 sw
= DisplayWidth(dpy
, screen
);
1479 sh
= DisplayHeight(dpy
, screen
);
1480 bh
= dc
.h
= dc
.font
.height
+ 2;
1483 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1484 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1485 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1486 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1487 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1489 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1490 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1491 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1492 /* init appearance */
1493 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1494 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1495 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1496 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1497 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1498 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1499 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1500 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1501 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1503 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1507 /* EWMH support per view */
1508 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1509 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1510 /* select for events */
1511 wa
.cursor
= cursor
[CurNormal
];
1512 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1513 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1514 |PropertyChangeMask
;
1515 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1516 XSelectInput(dpy
, root
, wa
.event_mask
);
1521 showhide(Client
*c
) {
1524 if(ISVISIBLE(c
)) { /* show clients top down */
1525 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1526 if(!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1527 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1530 else { /* hide clients bottom up */
1532 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1538 sigchld(int unused
) {
1539 if(signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1540 die("Can't install SIGCHLD handler");
1541 while(0 < waitpid(-1, NULL
, WNOHANG
));
1545 spawn(const Arg
*arg
) {
1548 close(ConnectionNumber(dpy
));
1550 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1551 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1558 tag(const Arg
*arg
) {
1559 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1560 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1566 tagmon(const Arg
*arg
) {
1567 if(!selmon
->sel
|| !mons
->next
)
1569 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1573 textnw(const char *text
, unsigned int len
) {
1577 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1580 return XTextWidth(dc
.font
.xfont
, text
, len
);
1589 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1593 c
= nexttiled(m
->clients
);
1594 mw
= m
->mfact
* m
->ww
;
1595 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1599 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1601 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1605 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1606 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1607 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1609 y
= c
->y
+ HEIGHT(c
);
1614 togglebar(const Arg
*arg
) {
1615 selmon
->showbar
= !selmon
->showbar
;
1616 updatebarpos(selmon
);
1617 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1622 togglefloating(const Arg
*arg
) {
1625 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1626 if(selmon
->sel
->isfloating
)
1627 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1628 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1633 toggletag(const Arg
*arg
) {
1634 unsigned int newtags
;
1638 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1640 selmon
->sel
->tags
= newtags
;
1646 toggleview(const Arg
*arg
) {
1647 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1650 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1656 unfocus(Client
*c
) {
1659 grabbuttons(c
, False
);
1660 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1661 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1665 unmanage(Client
*c
, Bool destroyed
) {
1668 /* The server grab construct avoids race conditions. */
1672 wc
.border_width
= c
->oldbw
;
1674 XSetErrorHandler(xerrordummy
);
1675 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1676 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1677 setclientstate(c
, WithdrawnState
);
1679 XSetErrorHandler(xerror
);
1688 unmapnotify(XEvent
*e
) {
1690 XUnmapEvent
*ev
= &e
->xunmap
;
1692 if((c
= wintoclient(ev
->window
)))
1699 XSetWindowAttributes wa
;
1701 wa
.override_redirect
= True
;
1702 wa
.background_pixmap
= ParentRelative
;
1703 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1704 for(m
= mons
; m
; m
= m
->next
) {
1705 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1706 CopyFromParent
, DefaultVisual(dpy
, screen
),
1707 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1708 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1709 XMapRaised(dpy
, m
->barwin
);
1714 updatebarpos(Monitor
*m
) {
1719 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1720 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1731 if(XineramaIsActive(dpy
)) {
1735 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1736 XineramaScreenInfo
*unique
= NULL
;
1738 info
= XineramaQueryScreens(dpy
, &nn
);
1739 for(n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1740 /* only consider unique geometries as separate screens */
1741 if(!(unique
= (XineramaScreenInfo
*)malloc(sizeof(XineramaScreenInfo
) * nn
)))
1742 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo
) * nn
);
1743 for(i
= 0, j
= 0; i
< nn
; i
++)
1744 if(isuniquegeom(unique
, j
, &info
[i
]))
1745 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1749 for(i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1750 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1752 m
->next
= createmon();
1756 for(i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1758 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1759 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1763 m
->mx
= m
->wx
= unique
[i
].x_org
;
1764 m
->my
= m
->wy
= unique
[i
].y_org
;
1765 m
->mw
= m
->ww
= unique
[i
].width
;
1766 m
->mh
= m
->wh
= unique
[i
].height
;
1770 else { /* less monitors available nn < n */
1771 for(i
= nn
; i
< n
; i
++) {
1772 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1776 m
->clients
= c
->next
;
1790 #endif /* XINERAMA */
1791 /* default monitor setup */
1795 if(mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1797 mons
->mw
= mons
->ww
= sw
;
1798 mons
->mh
= mons
->wh
= sh
;
1804 selmon
= wintomon(root
);
1810 updatenumlockmask(void) {
1812 XModifierKeymap
*modmap
;
1815 modmap
= XGetModifierMapping(dpy
);
1816 for(i
= 0; i
< 8; i
++)
1817 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1818 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1819 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1820 numlockmask
= (1 << i
);
1821 XFreeModifiermap(modmap
);
1825 updatesizehints(Client
*c
) {
1829 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1830 /* size is uninitialized, ensure that size.flags aren't used */
1832 if(size
.flags
& PBaseSize
) {
1833 c
->basew
= size
.base_width
;
1834 c
->baseh
= size
.base_height
;
1836 else if(size
.flags
& PMinSize
) {
1837 c
->basew
= size
.min_width
;
1838 c
->baseh
= size
.min_height
;
1841 c
->basew
= c
->baseh
= 0;
1842 if(size
.flags
& PResizeInc
) {
1843 c
->incw
= size
.width_inc
;
1844 c
->inch
= size
.height_inc
;
1847 c
->incw
= c
->inch
= 0;
1848 if(size
.flags
& PMaxSize
) {
1849 c
->maxw
= size
.max_width
;
1850 c
->maxh
= size
.max_height
;
1853 c
->maxw
= c
->maxh
= 0;
1854 if(size
.flags
& PMinSize
) {
1855 c
->minw
= size
.min_width
;
1856 c
->minh
= size
.min_height
;
1858 else if(size
.flags
& PBaseSize
) {
1859 c
->minw
= size
.base_width
;
1860 c
->minh
= size
.base_height
;
1863 c
->minw
= c
->minh
= 0;
1864 if(size
.flags
& PAspect
) {
1865 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1866 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1869 c
->maxa
= c
->mina
= 0.0;
1870 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1871 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1875 updatetitle(Client
*c
) {
1876 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1877 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1878 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1879 strcpy(c
->name
, broken
);
1883 updatestatus(void) {
1884 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1885 strcpy(stext
, "dwm-"VERSION
);
1890 updatewmhints(Client
*c
) {
1893 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1894 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1895 wmh
->flags
&= ~XUrgencyHint
;
1896 XSetWMHints(dpy
, c
->win
, wmh
);
1899 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1905 view(const Arg
*arg
) {
1906 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1908 selmon
->seltags
^= 1; /* toggle sel tagset */
1909 if(arg
->ui
& TAGMASK
)
1910 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1915 wintoclient(Window w
) {
1919 for(m
= mons
; m
; m
= m
->next
)
1920 for(c
= m
->clients
; c
; c
= c
->next
)
1927 wintomon(Window w
) {
1932 if(w
== root
&& getrootptr(&x
, &y
))
1933 return ptrtomon(x
, y
);
1934 for(m
= mons
; m
; m
= m
->next
)
1937 if((c
= wintoclient(w
)))
1942 /* There's no way to check accesses to destroyed windows, thus those cases are
1943 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1944 * default error handler, which may call exit. */
1946 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1947 if(ee
->error_code
== BadWindow
1948 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1949 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1950 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1951 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1952 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1953 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1954 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1955 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1957 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1958 ee
->request_code
, ee
->error_code
);
1959 return xerrorxlib(dpy
, ee
); /* may call exit */
1963 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1967 /* Startup Error handler to check if another window manager
1968 * is already running. */
1970 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1976 zoom(const Arg
*arg
) {
1977 Client
*c
= selmon
->sel
;
1979 if(!selmon
->lt
[selmon
->sellt
]->arrange
1980 || selmon
->lt
[selmon
->sellt
]->arrange
== monocle
1981 || (selmon
->sel
&& selmon
->sel
->isfloating
))
1983 if(c
== nexttiled(selmon
->clients
))
1984 if(!c
|| !(c
= nexttiled(c
->next
)))
1993 main(int argc
, char *argv
[]) {
1994 if(argc
== 2 && !strcmp("-v", argv
[1]))
1995 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1997 die("usage: dwm [-v]\n");
1998 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1999 fputs("warning: no locale support\n", stderr
);
2000 if(!(dpy
= XOpenDisplay(NULL
)))
2001 die("dwm: cannot open display\n");