Xinqi Bao's Git
bfeae15a3232c624c378cda06caa3c96ff6d7dde
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 ((int)((1LL << 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
*);
126 int by
; /* bar geometry */
127 int mx
, my
, mw
, mh
; /* screen size */
128 int wx
, wy
, ww
, wh
; /* window area */
129 unsigned int seltags
;
131 unsigned int tagset
[2];
144 const char *instance
;
151 /* function declarations */
152 static void applyrules(Client
*c
);
153 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
154 static void arrange(void);
155 static void attach(Client
*c
);
156 static void attachstack(Client
*c
);
157 static void buttonpress(XEvent
*e
);
158 static void checkotherwm(void);
159 static void cleanup(void);
160 static void cleanupmons(void);
161 static void clearurgent(Client
*c
);
162 static void configure(Client
*c
);
163 static void configurenotify(XEvent
*e
);
164 static void configurerequest(XEvent
*e
);
165 static void destroynotify(XEvent
*e
);
166 static void detach(Client
*c
);
167 static void detachstack(Client
*c
);
168 static void die(const char *errstr
, ...);
169 static Monitor
*dirtomon(int dir
);
170 static void drawbar(Monitor
*m
);
171 static void drawbars(void);
172 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
173 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
174 static void enternotify(XEvent
*e
);
175 static void expose(XEvent
*e
);
176 static void focus(Client
*c
);
177 static void focusin(XEvent
*e
);
178 static void focusmon(const Arg
*arg
);
179 static void focusstack(const Arg
*arg
);
180 static unsigned long getcolor(const char *colstr
);
181 static Bool
getrootptr(int *x
, int *y
);
182 static long getstate(Window w
);
183 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
184 static void grabbuttons(Client
*c
, Bool focused
);
185 static void grabkeys(void);
186 static void initfont(const char *fontstr
);
187 static Bool
isprotodel(Client
*c
);
188 static void keypress(XEvent
*e
);
189 static void killclient(const Arg
*arg
);
190 static void manage(Window w
, XWindowAttributes
*wa
);
191 static void mappingnotify(XEvent
*e
);
192 static void maprequest(XEvent
*e
);
193 static void monocle(Monitor
*m
);
194 static void movemouse(const Arg
*arg
);
195 static Client
*nexttiled(Client
*c
);
196 static Monitor
*ptrtomon(int x
, int y
);
197 static void propertynotify(XEvent
*e
);
198 static void quit(const Arg
*arg
);
199 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
200 static void resizemouse(const Arg
*arg
);
201 static void restack(Monitor
*m
);
202 static void run(void);
203 static void scan(void);
204 static void sendmon(Client
*c
, Monitor
*m
);
205 static void setclientstate(Client
*c
, long state
);
206 static void setlayout(const Arg
*arg
);
207 static void setmfact(const Arg
*arg
);
208 static void setup(void);
209 static void showhide(Client
*c
);
210 static void sigchld(int signal
);
211 static void spawn(const Arg
*arg
);
212 static void tag(const Arg
*arg
);
213 static void tagmon(const Arg
*arg
);
214 static int textnw(const char *text
, unsigned int len
);
215 static void tile(Monitor
*);
216 static void togglebar(const Arg
*arg
);
217 static void togglefloating(const Arg
*arg
);
218 static void toggletag(const Arg
*arg
);
219 static void toggleview(const Arg
*arg
);
220 static void unfocus(Client
*c
);
221 static void unmanage(Client
*c
);
222 static void unmapnotify(XEvent
*e
);
223 static void updategeom(void);
224 static void updatebarpos(Monitor
*m
);
225 static void updatebars(void);
226 static void updatenumlockmask(void);
227 static void updatesizehints(Client
*c
);
228 static void updatestatus(void);
229 static void updatetitle(Client
*c
);
230 static void updatewmhints(Client
*c
);
231 static void view(const Arg
*arg
);
232 static Client
*wintoclient(Window w
);
233 static Monitor
*wintomon(Window w
);
234 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
235 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
236 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
237 static void zoom(const Arg
*arg
);
240 static char stext
[256], ntext
[8];
242 static int sw
, sh
; /* X display screen geometry width, height */
243 static int bh
, blw
= 0; /* bar geometry */
244 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
245 static unsigned int numlockmask
= 0;
246 static void (*handler
[LASTEvent
]) (XEvent
*) = {
247 [ButtonPress
] = buttonpress
,
248 [ConfigureRequest
] = configurerequest
,
249 [ConfigureNotify
] = configurenotify
,
250 [DestroyNotify
] = destroynotify
,
251 [EnterNotify
] = enternotify
,
254 [KeyPress
] = keypress
,
255 [MappingNotify
] = mappingnotify
,
256 [MapRequest
] = maprequest
,
257 [PropertyNotify
] = propertynotify
,
258 [UnmapNotify
] = unmapnotify
260 static Atom wmatom
[WMLast
], netatom
[NetLast
];
262 static Bool running
= True
;
263 static Cursor cursor
[CurLast
];
266 static Monitor
*mons
= NULL
, *selmon
= NULL
;
269 /* configuration, allows nested code to access above variables */
272 /* compile-time check if all tags fit into an unsigned int bit array. */
273 struct NumTags
{ char limitexceeded
[sizeof(unsigned int) * 8 < LENGTH(tags
) ? -1 : 1]; };
275 /* function implementations */
277 applyrules(Client
*c
) {
281 XClassHint ch
= { 0 };
284 c
->isfloating
= c
->tags
= 0;
285 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
286 for(i
= 0; i
< LENGTH(rules
); i
++) {
288 if((!r
->title
|| strstr(c
->name
, r
->title
))
289 && (!r
->class || (ch
.res_class
&& strstr(ch
.res_class
, r
->class)))
290 && (!r
->instance
|| (ch
.res_name
&& strstr(ch
.res_name
, r
->instance
))))
292 c
->isfloating
= r
->isfloating
;
294 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
304 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
308 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
312 /* set minimum possible */
320 if(*x
+ *w
+ 2 * c
->bw
< 0)
322 if(*y
+ *h
+ 2 * c
->bw
< 0)
326 if(*x
> m
->mx
+ m
->mw
)
327 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
328 if(*y
> m
->my
+ m
->mh
)
329 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
330 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
332 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
339 if(resizehints
|| c
->isfloating
) {
340 /* see last two sentences in ICCCM 4.1.2.3 */
341 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
342 if(!baseismin
) { /* temporarily remove base dimensions */
346 /* adjust for aspect limits */
347 if(c
->mina
> 0 && c
->maxa
> 0) {
348 if(c
->maxa
< (float)*w
/ *h
)
350 else if(c
->mina
< (float)*h
/ *w
)
353 if(baseismin
) { /* increment calculation requires this */
357 /* adjust for increment value */
362 /* restore base dimensions */
365 *w
= MAX(*w
, c
->minw
);
366 *h
= MAX(*h
, c
->minh
);
368 *w
= MIN(*w
, c
->maxw
);
370 *h
= MIN(*h
, c
->maxh
);
372 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
379 /* optimise two loops into one, check focus(NULL) */
380 for(m
= mons
; m
; m
= m
->next
)
383 for(m
= mons
; m
; m
= m
->next
) {
384 if(m
->lt
[m
->sellt
]->arrange
)
385 m
->lt
[m
->sellt
]->arrange(m
);
392 c
->next
= c
->mon
->clients
;
397 attachstack(Client
*c
) {
398 c
->snext
= c
->mon
->stack
;
403 buttonpress(XEvent
*e
) {
404 unsigned int i
, x
, click
;
408 XButtonPressedEvent
*ev
= &e
->xbutton
;
411 /* focus monitor if necessary */
412 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
413 unfocus(selmon
->sel
);
417 if(ev
->window
== selmon
->barwin
) {
421 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
422 if(i
< LENGTH(tags
)) {
426 else if(ev
->x
< x
+ blw
)
428 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
429 click
= ClkStatusText
;
433 else if((c
= wintoclient(ev
->window
))) {
435 click
= ClkClientWin
;
437 for(i
= 0; i
< LENGTH(buttons
); i
++)
438 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
439 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
440 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
446 xerrorxlib
= XSetErrorHandler(xerrorstart
);
447 /* this causes an error if some other window manager is running */
448 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
451 die("dwm: another window manager is already running\n");
452 XSetErrorHandler(xerror
);
459 Layout foo
= { "", NULL
};
463 selmon
->lt
[selmon
->sellt
] = &foo
;
464 for(m
= mons
; m
; m
= m
->next
)
468 XFreeFontSet(dpy
, dc
.font
.set
);
470 XFreeFont(dpy
, dc
.font
.xfont
);
471 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
472 XFreePixmap(dpy
, dc
.drawable
);
474 XFreeCursor(dpy
, cursor
[CurNormal
]);
475 XFreeCursor(dpy
, cursor
[CurResize
]);
476 XFreeCursor(dpy
, cursor
[CurMove
]);
479 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
488 XUnmapWindow(dpy
, mons
->barwin
);
489 XDestroyWindow(dpy
, mons
->barwin
);
496 clearurgent(Client
*c
) {
500 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
502 wmh
->flags
&= ~XUrgencyHint
;
503 XSetWMHints(dpy
, c
->win
, wmh
);
508 configure(Client
*c
) {
511 ce
.type
= ConfigureNotify
;
519 ce
.border_width
= c
->bw
;
521 ce
.override_redirect
= False
;
522 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
526 configurenotify(XEvent
*e
) {
528 XConfigureEvent
*ev
= &e
->xconfigure
;
530 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
535 XFreePixmap(dpy
, dc
.drawable
);
536 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
538 for(m
= mons
; m
; m
= m
->next
)
539 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
545 configurerequest(XEvent
*e
) {
548 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
551 if((c
= wintoclient(ev
->window
))) {
552 if(ev
->value_mask
& CWBorderWidth
)
553 c
->bw
= ev
->border_width
;
554 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
556 if(ev
->value_mask
& CWX
)
557 c
->x
= m
->mx
+ ev
->x
;
558 if(ev
->value_mask
& CWY
)
559 c
->y
= m
->my
+ ev
->y
;
560 if(ev
->value_mask
& CWWidth
)
562 if(ev
->value_mask
& CWHeight
)
564 if((c
->x
- m
->mx
+ c
->w
) > m
->mw
&& c
->isfloating
)
565 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
566 if((c
->y
- m
->my
+ c
->h
) > m
->mh
&& c
->isfloating
)
567 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
568 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
571 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
579 wc
.width
= ev
->width
;
580 wc
.height
= ev
->height
;
581 wc
.border_width
= ev
->border_width
;
582 wc
.sibling
= ev
->above
;
583 wc
.stack_mode
= ev
->detail
;
584 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
590 destroynotify(XEvent
*e
) {
592 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
594 if((c
= wintoclient(ev
->window
)))
602 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
607 detachstack(Client
*c
) {
610 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
613 if(c
== c
->mon
->sel
) {
614 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
620 die(const char *errstr
, ...) {
623 va_start(ap
, errstr
);
624 vfprintf(stderr
, errstr
, ap
);
634 if(!(m
= selmon
->next
))
639 for(m
= mons
; m
->next
; m
= m
->next
);
641 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
647 drawbar(Monitor
*m
) {
649 unsigned int i
, n
= 0, occ
= 0, urg
= 0;
653 for(c
= m
->clients
; c
; c
= c
->next
) {
661 for(i
= 0; i
< LENGTH(tags
); i
++) {
662 dc
.w
= TEXTW(tags
[i
]);
663 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
664 drawtext(tags
[i
], col
, urg
& 1 << i
);
665 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
666 occ
& 1 << i
, urg
& 1 << i
, col
);
671 drawtext(m
->lt
[m
->sellt
]->symbol
, dc
.norm
, False
);
674 snprintf(ntext
, sizeof ntext
, "%u", n
);
676 drawtext(ntext
, dc
.norm
, False
);
678 if(m
== selmon
) { /* status is only drawn on selected monitor */
685 drawtext(stext
, dc
.norm
, False
);
689 if((dc
.w
= dc
.x
- x
) > bh
) {
692 col
= m
== selmon
? dc
.sel
: dc
.norm
;
693 drawtext(m
->sel
->name
, col
, False
);
694 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
697 drawtext(NULL
, dc
.norm
, False
);
699 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
707 for(m
= mons
; m
; m
= m
->next
)
712 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
715 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
717 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
718 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
719 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
723 r
.width
= r
.height
= x
+ 1;
724 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
727 r
.width
= r
.height
= x
;
728 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
733 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
735 int i
, x
, y
, h
, len
, olen
;
736 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
738 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
739 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
743 h
= dc
.font
.ascent
+ dc
.font
.descent
;
744 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
746 /* shorten text if necessary */
747 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
750 memcpy(buf
, text
, len
);
752 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
753 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
755 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
757 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
761 enternotify(XEvent
*e
) {
764 XCrossingEvent
*ev
= &e
->xcrossing
;
766 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
768 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
769 unfocus(selmon
->sel
);
772 if((c
= wintoclient(ev
->window
)))
781 XExposeEvent
*ev
= &e
->xexpose
;
783 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
789 if(!c
|| !ISVISIBLE(c
))
790 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
792 unfocus(selmon
->sel
);
800 grabbuttons(c
, True
);
801 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
802 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
805 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
811 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
812 XFocusChangeEvent
*ev
= &e
->xfocus
;
814 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
815 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
819 focusmon(const Arg
*arg
) {
824 m
= dirtomon(arg
->i
);
825 unfocus(selmon
->sel
);
831 focusstack(const Arg
*arg
) {
832 Client
*c
= NULL
, *i
;
837 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
839 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
842 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
846 for(; i
; i
= i
->next
)
857 getcolor(const char *colstr
) {
858 Colormap cmap
= DefaultColormap(dpy
, screen
);
861 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
862 die("error, cannot allocate color '%s'\n", colstr
);
867 getrootptr(int *x
, int *y
) {
872 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
879 unsigned char *p
= NULL
;
880 unsigned long n
, extra
;
883 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
884 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
885 if(status
!= Success
)
894 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
899 if(!text
|| size
== 0)
902 XGetTextProperty(dpy
, w
, &name
, atom
);
905 if(name
.encoding
== XA_STRING
)
906 strncpy(text
, (char *)name
.value
, size
- 1);
908 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
911 strncpy(text
, *list
, size
- 1);
912 XFreeStringList(list
);
915 text
[size
- 1] = '\0';
921 grabbuttons(Client
*c
, Bool focused
) {
925 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
926 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
928 for(i
= 0; i
< LENGTH(buttons
); i
++)
929 if(buttons
[i
].click
== ClkClientWin
)
930 for(j
= 0; j
< LENGTH(modifiers
); j
++)
931 XGrabButton(dpy
, buttons
[i
].button
,
932 buttons
[i
].mask
| modifiers
[j
],
933 c
->win
, False
, BUTTONMASK
,
934 GrabModeAsync
, GrabModeSync
, None
, None
);
937 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
938 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
947 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
950 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
951 for(i
= 0; i
< LENGTH(keys
); i
++) {
952 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
953 for(j
= 0; j
< LENGTH(modifiers
); j
++)
954 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
955 True
, GrabModeAsync
, GrabModeAsync
);
961 initfont(const char *fontstr
) {
962 char *def
, **missing
;
966 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
969 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
970 XFreeStringList(missing
);
973 XFontSetExtents
*font_extents
;
974 XFontStruct
**xfonts
;
976 dc
.font
.ascent
= dc
.font
.descent
= 0;
977 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
978 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
979 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
980 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
981 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
986 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
987 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
988 die("error, cannot load font: '%s'\n", fontstr
);
989 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
990 dc
.font
.descent
= dc
.font
.xfont
->descent
;
992 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
996 isprotodel(Client
*c
) {
1001 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1002 for(i
= 0; !ret
&& i
< n
; i
++)
1003 if(protocols
[i
] == wmatom
[WMDelete
])
1011 keypress(XEvent
*e
) {
1017 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1018 for(i
= 0; i
< LENGTH(keys
); i
++)
1019 if(keysym
== keys
[i
].keysym
1020 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1022 keys
[i
].func(&(keys
[i
].arg
));
1026 killclient(const Arg
*arg
) {
1031 if(isprotodel(selmon
->sel
)) {
1032 ev
.type
= ClientMessage
;
1033 ev
.xclient
.window
= selmon
->sel
->win
;
1034 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1035 ev
.xclient
.format
= 32;
1036 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1037 ev
.xclient
.data
.l
[1] = CurrentTime
;
1038 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1041 XKillClient(dpy
, selmon
->sel
->win
);
1045 manage(Window w
, XWindowAttributes
*wa
) {
1047 Client
*c
, *t
= NULL
;
1048 Window trans
= None
;
1051 if(!(c
= malloc(sizeof(Client
))))
1052 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1056 if(XGetTransientForHint(dpy
, w
, &trans
))
1057 t
= wintoclient(trans
);
1067 c
->x
= wa
->x
+ c
->mon
->wx
;
1068 c
->y
= wa
->y
+ c
->mon
->wy
;
1071 c
->oldbw
= wa
->border_width
;
1072 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1078 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1079 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1080 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1081 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1082 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1083 /* only fix client y-offset, if the client center might cover the bar */
1084 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1085 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1088 wc
.border_width
= c
->bw
;
1089 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1090 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1091 configure(c
); /* propagates border_width, if size doesn't change */
1093 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1094 grabbuttons(c
, False
);
1096 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1098 XRaiseWindow(dpy
, c
->win
);
1101 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1102 XMapWindow(dpy
, c
->win
);
1103 setclientstate(c
, NormalState
);
1108 mappingnotify(XEvent
*e
) {
1109 XMappingEvent
*ev
= &e
->xmapping
;
1111 XRefreshKeyboardMapping(ev
);
1112 if(ev
->request
== MappingKeyboard
)
1117 maprequest(XEvent
*e
) {
1118 static XWindowAttributes wa
;
1119 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1121 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1123 if(wa
.override_redirect
)
1125 if(!wintoclient(ev
->window
))
1126 manage(ev
->window
, &wa
);
1130 monocle(Monitor
*m
) {
1133 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1134 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1138 movemouse(const Arg
*arg
) {
1139 int x
, y
, ocx
, ocy
, nx
, ny
;
1144 if(!(c
= selmon
->sel
))
1149 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1150 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1152 if(!getrootptr(&x
, &y
))
1155 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1157 case ConfigureRequest
:
1160 handler
[ev
.type
](&ev
);
1163 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1164 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1165 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1166 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1167 if(abs(selmon
->wx
- nx
) < snap
)
1169 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1170 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1171 if(abs(selmon
->wy
- ny
) < snap
)
1173 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1174 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1175 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1176 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1177 togglefloating(NULL
);
1179 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1180 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1184 while(ev
.type
!= ButtonRelease
);
1185 XUngrabPointer(dpy
, CurrentTime
);
1186 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1194 nexttiled(Client
*c
) {
1195 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1200 ptrtomon(int x
, int y
) {
1203 for(m
= mons
; m
; m
= m
->next
)
1204 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1210 propertynotify(XEvent
*e
) {
1213 XPropertyEvent
*ev
= &e
->xproperty
;
1215 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1217 else if(ev
->state
== PropertyDelete
)
1218 return; /* ignore */
1219 else if((c
= wintoclient(ev
->window
))) {
1222 case XA_WM_TRANSIENT_FOR
:
1223 XGetTransientForHint(dpy
, c
->win
, &trans
);
1224 if(!c
->isfloating
&& (c
->isfloating
= (wintoclient(trans
) != NULL
)))
1227 case XA_WM_NORMAL_HINTS
:
1235 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1237 if(c
== selmon
->sel
)
1244 quit(const Arg
*arg
) {
1249 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1252 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
)) {
1255 c
->w
= wc
.width
= w
;
1256 c
->h
= wc
.height
= h
;
1257 wc
.border_width
= c
->bw
;
1258 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1265 resizemouse(const Arg
*arg
) {
1272 if(!(c
= selmon
->sel
))
1277 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1278 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1280 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1282 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1284 case ConfigureRequest
:
1287 handler
[ev
.type
](&ev
);
1290 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1291 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1292 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1293 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
) {
1294 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1295 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1296 togglefloating(NULL
);
1298 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1299 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1303 while(ev
.type
!= ButtonRelease
);
1304 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1305 XUngrabPointer(dpy
, CurrentTime
);
1306 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1307 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1315 restack(Monitor
*m
) {
1323 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1324 XRaiseWindow(dpy
, m
->sel
->win
);
1325 if(m
->lt
[m
->sellt
]->arrange
) {
1326 wc
.stack_mode
= Below
;
1327 wc
.sibling
= m
->barwin
;
1328 for(c
= m
->stack
; c
; c
= c
->snext
)
1329 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1330 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1331 wc
.sibling
= c
->win
;
1335 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1342 /* main event loop */
1344 while(running
&& !XNextEvent(dpy
, &ev
))
1345 if(handler
[ev
.type
])
1346 (handler
[ev
.type
])(&ev
); /* call handler */
1351 unsigned int i
, num
;
1352 Window d1
, d2
, *wins
= NULL
;
1353 XWindowAttributes wa
;
1355 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1356 for(i
= 0; i
< num
; i
++) {
1357 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1358 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1360 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1361 manage(wins
[i
], &wa
);
1363 for(i
= 0; i
< num
; i
++) { /* now the transients */
1364 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1366 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1367 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1368 manage(wins
[i
], &wa
);
1376 sendmon(Client
*c
, Monitor
*m
) {
1383 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1391 setclientstate(Client
*c
, long state
) {
1392 long data
[] = { state
, None
};
1394 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1395 PropModeReplace
, (unsigned char *)data
, 2);
1399 setlayout(const Arg
*arg
) {
1400 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1403 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1410 /* arg > 1.0 will set mfact absolutly */
1412 setmfact(const Arg
*arg
) {
1415 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1417 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1418 if(f
< 0.1 || f
> 0.9)
1428 XSetWindowAttributes wa
;
1431 screen
= DefaultScreen(dpy
);
1432 root
= RootWindow(dpy
, screen
);
1434 sw
= DisplayWidth(dpy
, screen
);
1435 sh
= DisplayHeight(dpy
, screen
);
1436 bh
= dc
.h
= dc
.font
.height
+ 2;
1439 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1440 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1441 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1442 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1443 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1445 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1446 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1447 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1448 /* init appearance */
1449 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1450 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1451 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1452 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1453 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1454 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1455 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1456 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1457 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1459 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1461 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1462 w
= TEXTW(layouts
[i
].symbol
);
1467 /* EWMH support per view */
1468 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1469 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1470 /* select for events */
1471 wa
.cursor
= cursor
[CurNormal
];
1472 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1473 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1474 |PropertyChangeMask
;
1475 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1476 XSelectInput(dpy
, root
, wa
.event_mask
);
1481 showhide(Client
*c
) {
1484 if(ISVISIBLE(c
)) { /* show clients top down */
1485 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1486 if(!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1487 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1490 else { /* hide clients bottom up */
1492 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1498 sigchld(int signal
) {
1499 while(0 < waitpid(-1, NULL
, WNOHANG
));
1503 spawn(const Arg
*arg
) {
1504 signal(SIGCHLD
, sigchld
);
1507 close(ConnectionNumber(dpy
));
1509 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1510 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1517 tag(const Arg
*arg
) {
1518 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1519 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1525 tagmon(const Arg
*arg
) {
1526 if(!selmon
->sel
|| !mons
->next
)
1528 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1532 textnw(const char *text
, unsigned int len
) {
1536 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1539 return XTextWidth(dc
.font
.xfont
, text
, len
);
1548 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1552 c
= nexttiled(m
->clients
);
1553 mw
= m
->mfact
* m
->ww
;
1554 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1558 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1560 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1564 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1565 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1566 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1568 y
= c
->y
+ HEIGHT(c
);
1573 togglebar(const Arg
*arg
) {
1574 selmon
->showbar
= !selmon
->showbar
;
1575 updatebarpos(selmon
);
1576 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1581 togglefloating(const Arg
*arg
) {
1584 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1585 if(selmon
->sel
->isfloating
)
1586 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1587 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1592 toggletag(const Arg
*arg
) {
1597 mask
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1599 selmon
->sel
->tags
= mask
;
1605 toggleview(const Arg
*arg
) {
1606 unsigned int mask
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1609 selmon
->tagset
[selmon
->seltags
] = mask
;
1615 unfocus(Client
*c
) {
1618 grabbuttons(c
, False
);
1619 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1620 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1624 unmanage(Client
*c
) {
1627 wc
.border_width
= c
->oldbw
;
1628 /* The server grab construct avoids race conditions. */
1630 XSetErrorHandler(xerrordummy
);
1631 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1634 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1635 setclientstate(c
, WithdrawnState
);
1638 XSetErrorHandler(xerror
);
1645 unmapnotify(XEvent
*e
) {
1647 XUnmapEvent
*ev
= &e
->xunmap
;
1649 if((c
= wintoclient(ev
->window
)))
1656 XSetWindowAttributes wa
;
1658 wa
.override_redirect
= True
;
1659 wa
.background_pixmap
= ParentRelative
;
1660 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1661 for(m
= mons
; m
; m
= m
->next
) {
1662 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1663 CopyFromParent
, DefaultVisual(dpy
, screen
),
1664 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1665 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1666 XMapRaised(dpy
, m
->barwin
);
1671 updatebarpos(Monitor
*m
) {
1676 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1677 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1687 Monitor
*newmons
= NULL
, *m
= NULL
, *tm
;
1690 XineramaScreenInfo
*info
= NULL
;
1692 if(XineramaIsActive(dpy
))
1693 info
= XineramaQueryScreens(dpy
, &n
);
1694 #endif /* XINERAMA */
1695 /* allocate monitor(s) for the new geometry setup */
1696 for(i
= 0; i
< n
; i
++) {
1697 if(!(m
= (Monitor
*)malloc(sizeof(Monitor
))))
1698 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
1702 /* initialise monitor(s) */
1704 if(XineramaIsActive(dpy
)) {
1705 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1706 m
->num
= info
[i
].screen_number
;
1707 m
->mx
= m
->wx
= info
[i
].x_org
;
1708 m
->my
= m
->wy
= info
[i
].y_org
;
1709 m
->mw
= m
->ww
= info
[i
].width
;
1710 m
->mh
= m
->wh
= info
[i
].height
;
1715 #endif /* XINERAMA */
1716 /* default monitor setup */
1724 /* bar geometry setup */
1725 for(m
= newmons
; m
; m
= m
->next
) {
1726 m
->sel
= m
->stack
= m
->clients
= NULL
;
1729 m
->tagset
[0] = m
->tagset
[1] = 1;
1731 m
->showbar
= showbar
;
1733 m
->lt
[0] = &layouts
[0];
1734 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1737 /* reassign left over clients of disappeared monitors */
1738 for(tm
= mons
; tm
; tm
= tm
->next
)
1739 while(tm
->clients
) {
1741 tm
->clients
= c
->next
;
1747 /* select focused monitor */
1749 selmon
= mons
= newmons
;
1750 selmon
= wintomon(root
);
1754 updatenumlockmask(void) {
1756 XModifierKeymap
*modmap
;
1759 modmap
= XGetModifierMapping(dpy
);
1760 for(i
= 0; i
< 8; i
++)
1761 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1762 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1763 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1764 numlockmask
= (1 << i
);
1765 XFreeModifiermap(modmap
);
1769 updatesizehints(Client
*c
) {
1773 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1774 /* size is uninitialized, ensure that size.flags aren't used */
1776 if(size
.flags
& PBaseSize
) {
1777 c
->basew
= size
.base_width
;
1778 c
->baseh
= size
.base_height
;
1780 else if(size
.flags
& PMinSize
) {
1781 c
->basew
= size
.min_width
;
1782 c
->baseh
= size
.min_height
;
1785 c
->basew
= c
->baseh
= 0;
1786 if(size
.flags
& PResizeInc
) {
1787 c
->incw
= size
.width_inc
;
1788 c
->inch
= size
.height_inc
;
1791 c
->incw
= c
->inch
= 0;
1792 if(size
.flags
& PMaxSize
) {
1793 c
->maxw
= size
.max_width
;
1794 c
->maxh
= size
.max_height
;
1797 c
->maxw
= c
->maxh
= 0;
1798 if(size
.flags
& PMinSize
) {
1799 c
->minw
= size
.min_width
;
1800 c
->minh
= size
.min_height
;
1802 else if(size
.flags
& PBaseSize
) {
1803 c
->minw
= size
.base_width
;
1804 c
->minh
= size
.base_height
;
1807 c
->minw
= c
->minh
= 0;
1808 if(size
.flags
& PAspect
) {
1809 c
->mina
= (float)size
.min_aspect
.y
/ (float)size
.min_aspect
.x
;
1810 c
->maxa
= (float)size
.max_aspect
.x
/ (float)size
.max_aspect
.y
;
1813 c
->maxa
= c
->mina
= 0.0;
1814 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1815 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1819 updatetitle(Client
*c
) {
1820 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1821 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1825 updatestatus(void) {
1826 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1827 strcpy(stext
, "dwm-"VERSION
);
1832 updatewmhints(Client
*c
) {
1835 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1836 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1837 wmh
->flags
&= ~XUrgencyHint
;
1838 XSetWMHints(dpy
, c
->win
, wmh
);
1841 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1847 view(const Arg
*arg
) {
1848 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1850 selmon
->seltags
^= 1; /* toggle sel tagset */
1851 if(arg
->ui
& TAGMASK
)
1852 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1857 wintoclient(Window w
) {
1861 for(m
= mons
; m
; m
= m
->next
)
1862 for(c
= m
->clients
; c
; c
= c
->next
)
1869 wintomon(Window w
) {
1874 if(w
== root
&& getrootptr(&x
, &y
))
1875 return ptrtomon(x
, y
);
1876 for(m
= mons
; m
; m
= m
->next
)
1879 if((c
= wintoclient(w
)))
1884 /* There's no way to check accesses to destroyed windows, thus those cases are
1885 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1886 * default error handler, which may call exit. */
1888 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1889 if(ee
->error_code
== BadWindow
1890 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1891 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1892 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1893 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1894 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1895 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1896 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1897 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1899 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1900 ee
->request_code
, ee
->error_code
);
1901 return xerrorxlib(dpy
, ee
); /* may call exit */
1905 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1909 /* Startup Error handler to check if another window manager
1910 * is already running. */
1912 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1918 zoom(const Arg
*arg
) {
1919 Client
*c
= selmon
->sel
;
1921 if(!selmon
->lt
[selmon
->sellt
]->arrange
1922 || selmon
->lt
[selmon
->sellt
]->arrange
== monocle
1923 || (selmon
->sel
&& selmon
->sel
->isfloating
))
1925 if(c
== nexttiled(selmon
->clients
))
1926 if(!c
|| !(c
= nexttiled(c
->next
)))
1935 main(int argc
, char *argv
[]) {
1936 if(argc
== 2 && !strcmp("-v", argv
[1]))
1937 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1939 die("usage: dwm [-v]\n");
1940 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1941 fputs("warning: no locale support\n", stderr
);
1942 if(!(dpy
= XOpenDisplay(NULL
)))
1943 die("dwm: cannot open display\n");