Xinqi Bao's Git
1 //#define XINULATOR /* debug, simulates dual head */
2 /* See LICENSE file for copyright and license details.
4 * dynamic window manager is designed like any other X client as well. It is
5 * driven through handling X events. In contrast to other X clients, a window
6 * manager selects for SubstructureRedirectMask on the root window, to receive
7 * events about window (dis-)appearance. Only one X connection at a time is
8 * allowed to select for this event mask.
10 * The event handlers of dwm are organized in an array which is accessed
11 * whenever a new event has been fetched. This allows event dispatching
14 * Each child of the root window is called a client, except windows which have
15 * set the override_redirect flag. Clients are organized in a global
16 * linked client list, the focus history is remembered through a global
17 * stack list. Each client contains a bit array to indicate the tags of a
20 * Keys and tagging rules are organized as arrays and defined in config.h.
22 * To understand everything else, start reading main().
32 #include <sys/types.h>
34 #include <X11/cursorfont.h>
35 #include <X11/keysym.h>
36 #include <X11/Xatom.h>
38 #include <X11/Xproto.h>
39 #include <X11/Xutil.h>
41 #include <X11/extensions/Xinerama.h>
45 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
46 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
47 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
48 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
49 #define LENGTH(X) (sizeof X / sizeof X[0])
50 #define MAX(A, B) ((A) > (B) ? (A) : (B))
51 #define MIN(A, B) ((A) < (B) ? (A) : (B))
52 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
53 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
54 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
55 #define TAGMASK ((int)((1LL << LENGTH(tags)) - 1))
56 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
59 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
60 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
61 enum { NetSupported
, NetWMName
, 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
*);
127 int by
, btx
; /* 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];
144 const char *instance
;
150 /* function declarations */
151 static void applyrules(Client
*c
);
152 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
);
153 static void arrange(void);
154 static void attach(Client
*c
);
155 static void attachstack(Client
*c
);
156 static void buttonpress(XEvent
*e
);
157 static void checkotherwm(void);
158 static void cleanup(void);
159 static void cleanupmons(void);
160 static void clearurgent(Client
*c
);
161 static void configure(Client
*c
);
162 static void configurenotify(XEvent
*e
);
163 static void configurerequest(XEvent
*e
);
164 static void destroynotify(XEvent
*e
);
165 static void detach(Client
*c
);
166 static void detachstack(Client
*c
);
167 static void die(const char *errstr
, ...);
168 static void drawbar(Monitor
*m
);
169 static void drawbars(void);
170 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
171 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
172 static void enternotify(XEvent
*e
);
173 static void expose(XEvent
*e
);
174 static void focus(Client
*c
);
175 static void focusin(XEvent
*e
);
176 static void focusstack(const Arg
*arg
);
177 static Client
*getclient(Window w
);
178 static unsigned long getcolor(const char *colstr
);
179 static Monitor
*getmon(Window w
);
180 static Monitor
*getmonn(unsigned int n
);
181 static Monitor
*getmonxy(int x
, int y
);
182 static Bool
getrootpointer(int *x
, int *y
);
183 static long getstate(Window w
);
184 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
185 static void grabbuttons(Client
*c
, Bool focused
);
186 static void grabkeys(void);
187 static void initfont(const char *fontstr
);
188 static Bool
isprotodel(Client
*c
);
189 static void keypress(XEvent
*e
);
190 static void killclient(const Arg
*arg
);
191 static void manage(Window w
, XWindowAttributes
*wa
);
192 static void mappingnotify(XEvent
*e
);
193 static void maprequest(XEvent
*e
);
194 static void monocle(Monitor
*m
);
195 static void movemouse(const Arg
*arg
);
196 static Client
*nexttiled(Client
*c
);
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
);
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 int textnw(const char *text
, unsigned int len
);
214 static void tile(Monitor
*);
215 static void togglebar(const Arg
*arg
);
216 static void togglefloating(const Arg
*arg
);
217 static void toggletag(const Arg
*arg
);
218 static void toggleview(const Arg
*arg
);
219 static void unfocus(Client
*c
);
220 static void unmanage(Client
*c
);
221 static void unmapnotify(XEvent
*e
);
222 static void updategeom(void);
223 static void updatebarpos(Monitor
*m
);
224 static void updatebars(void);
225 static void updatenumlockmask(void);
226 static void updatesizehints(Client
*c
);
227 static void updatestatus(void);
228 static void updatetitle(Client
*c
);
229 static void updatewmhints(Client
*c
);
230 static void view(const Arg
*arg
);
231 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
232 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
233 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
234 static void zoom(const Arg
*arg
);
236 static void focusmon(const Arg
*arg
);
237 static void tagmon(const Arg
*arg
);
238 #endif /* XINERAMA */
241 static char stext
[256];
243 static int sx
, sy
, sw
, sh
; /* X display screen geometry x, y, width, height */
244 static int bh
, blw
= 0; /* bar geometry */
245 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
246 static unsigned int numlockmask
= 0;
247 static void (*handler
[LASTEvent
]) (XEvent
*) = {
248 [ButtonPress
] = buttonpress
,
249 [ConfigureRequest
] = configurerequest
,
250 [ConfigureNotify
] = configurenotify
,
251 [DestroyNotify
] = destroynotify
,
252 [EnterNotify
] = enternotify
,
255 [KeyPress
] = keypress
,
256 [MappingNotify
] = mappingnotify
,
257 [MapRequest
] = maprequest
,
258 [PropertyNotify
] = propertynotify
,
259 [UnmapNotify
] = unmapnotify
261 static Atom wmatom
[WMLast
], netatom
[NetLast
];
263 static Bool running
= True
;
264 static Cursor cursor
[CurLast
];
267 static Layout
*lt
[] = { NULL
, NULL
};
268 static Monitor
*mons
= NULL
, *selmon
= NULL
;
270 /* configuration, allows nested code to access above variables */
273 /* compile-time check if all tags fit into an unsigned int bit array. */
274 struct NumTags
{ char limitexceeded
[sizeof(unsigned int) * 8 < LENGTH(tags
) ? -1 : 1]; };
276 /* function implementations */
278 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
)))) {
291 c
->isfloating
= r
->isfloating
;
300 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
304 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
) {
307 /* set minimum possible */
315 if(*x
+ *w
+ 2 * c
->bw
< sx
)
317 if(*y
+ *h
+ 2 * c
->bw
< sy
)
324 if(resizehints
|| c
->isfloating
) {
325 /* see last two sentences in ICCCM 4.1.2.3 */
326 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
328 if(!baseismin
) { /* temporarily remove base dimensions */
333 /* adjust for aspect limits */
334 if(c
->mina
> 0 && c
->maxa
> 0) {
335 if(c
->maxa
< (float)*w
/ *h
)
337 else if(c
->mina
< (float)*h
/ *w
)
341 if(baseismin
) { /* increment calculation requires this */
346 /* adjust for increment value */
352 /* restore base dimensions */
356 *w
= MAX(*w
, c
->minw
);
357 *h
= MAX(*h
, c
->minh
);
360 *w
= MIN(*w
, c
->maxw
);
363 *h
= MIN(*h
, c
->maxh
);
365 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
372 /* optimise two loops into one, check focus(NULL) */
373 for(m
= mons
; m
; m
= m
->next
)
376 for(m
= mons
; m
; m
= m
->next
) {
377 if(lt
[m
->sellt
]->arrange
)
378 lt
[m
->sellt
]->arrange(m
);
385 c
->next
= c
->mon
->clients
;
390 attachstack(Client
*c
) {
391 c
->snext
= c
->mon
->stack
;
396 buttonpress(XEvent
*e
) {
397 unsigned int i
, x
, click
;
401 XButtonPressedEvent
*ev
= &e
->xbutton
;
404 /* focus monitor if necessary */
405 if((m
= getmon(ev
->window
)) && m
!= selmon
) {
406 unfocus(selmon
->sel
);
410 if(ev
->window
== selmon
->barwin
&& ev
->x
>= selmon
->btx
) {
415 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
416 if(i
< LENGTH(tags
)) {
420 else if(ev
->x
< x
+ blw
)
422 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
423 click
= ClkStatusText
;
427 else if((c
= getclient(ev
->window
))) {
429 click
= ClkClientWin
;
432 for(i
= 0; i
< LENGTH(buttons
); i
++)
433 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
434 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
435 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
441 xerrorxlib
= XSetErrorHandler(xerrorstart
);
443 /* this causes an error if some other window manager is running */
444 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
447 die("dwm: another window manager is already running\n");
448 XSetErrorHandler(xerror
);
455 Layout foo
= { "", NULL
};
459 lt
[selmon
->sellt
] = &foo
;
461 /* TODO: consider simplifying cleanup code of the stack, perhaps do that in cleanmons() ? */
462 for(m
= mons
; m
; m
= m
->next
)
466 XFreeFontSet(dpy
, dc
.font
.set
);
468 XFreeFont(dpy
, dc
.font
.xfont
);
469 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
470 XFreePixmap(dpy
, dc
.drawable
);
472 XFreeCursor(dpy
, cursor
[CurNormal
]);
473 XFreeCursor(dpy
, cursor
[CurResize
]);
474 XFreeCursor(dpy
, cursor
[CurMove
]);
477 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
486 XUnmapWindow(dpy
, mons
->barwin
);
487 XDestroyWindow(dpy
, mons
->barwin
);
494 clearurgent(Client
*c
) {
498 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
500 wmh
->flags
&= ~XUrgencyHint
;
501 XSetWMHints(dpy
, c
->win
, wmh
);
506 configure(Client
*c
) {
509 ce
.type
= ConfigureNotify
;
517 ce
.border_width
= c
->bw
;
519 ce
.override_redirect
= False
;
520 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
524 configurenotify(XEvent
*e
) {
526 XConfigureEvent
*ev
= &e
->xconfigure
;
528 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
533 XFreePixmap(dpy
, dc
.drawable
);
534 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
536 for(m
= mons
; m
; m
= m
->next
)
537 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
543 configurerequest(XEvent
*e
) {
546 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
549 if((c
= getclient(ev
->window
))) {
550 if(ev
->value_mask
& CWBorderWidth
)
551 c
->bw
= ev
->border_width
;
552 else if(c
->isfloating
|| !lt
[selmon
->sellt
]->arrange
) {
554 if(ev
->value_mask
& CWX
)
555 c
->x
= m
->mx
+ ev
->x
;
556 if(ev
->value_mask
& CWY
)
557 c
->y
= m
->my
+ ev
->y
;
558 if(ev
->value_mask
& CWWidth
)
560 if(ev
->value_mask
& CWHeight
)
562 if((c
->x
- m
->mx
+ c
->w
) > m
->mw
&& c
->isfloating
)
563 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
564 if((c
->y
- m
->my
+ c
->h
) > m
->mh
&& c
->isfloating
)
565 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
566 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
569 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
577 wc
.width
= ev
->width
;
578 wc
.height
= ev
->height
;
579 wc
.border_width
= ev
->border_width
;
580 wc
.sibling
= ev
->above
;
581 wc
.stack_mode
= ev
->detail
;
582 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
588 destroynotify(XEvent
*e
) {
590 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
592 if((c
= getclient(ev
->window
)))
600 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
605 detachstack(Client
*c
) {
608 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
613 die(const char *errstr
, ...) {
616 va_start(ap
, errstr
);
617 vfprintf(stderr
, errstr
, ap
);
623 drawbar(Monitor
*m
) {
625 unsigned int i
, occ
= 0, urg
= 0;
629 for(c
= m
->clients
; c
; c
= c
->next
) {
639 buf
[0] = m
->screen_number
+ '0';
642 drawtext(buf
, selmon
== m
? dc
.sel
: dc
.norm
, True
);
645 #endif /* XINERAMA */
647 for(i
= 0; i
< LENGTH(tags
); i
++) {
648 dc
.w
= TEXTW(tags
[i
]);
649 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
650 drawtext(tags
[i
], col
, urg
& 1 << i
);
651 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
652 occ
& 1 << i
, urg
& 1 << i
, col
);
657 drawtext(lt
[m
->sellt
]->symbol
, dc
.norm
, False
);
662 if(m
== selmon
) { /* status is only drawn on selected monitor */
669 drawtext(stext
, dc
.norm
, False
);
674 if((dc
.w
= dc
.x
- x
) > bh
) {
677 col
= m
== selmon
? dc
.sel
: dc
.norm
;
678 drawtext(m
->sel
->name
, col
, False
);
679 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
682 drawtext(NULL
, dc
.norm
, False
);
684 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
692 for(m
= mons
; m
; m
= m
->next
)
697 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
700 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
702 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
703 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
704 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
708 r
.width
= r
.height
= x
+ 1;
709 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
712 r
.width
= r
.height
= x
;
713 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
718 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
720 int i
, x
, y
, h
, len
, olen
;
721 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
723 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
724 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
728 h
= dc
.font
.ascent
+ dc
.font
.descent
;
729 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
731 /* shorten text if necessary */
732 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
735 memcpy(buf
, text
, len
);
737 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
738 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
740 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
742 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
746 enternotify(XEvent
*e
) {
749 XCrossingEvent
*ev
= &e
->xcrossing
;
751 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
753 if((m
= getmon(ev
->window
)) && m
!= selmon
) {
754 unfocus(selmon
->sel
);
757 if((c
= getclient(ev
->window
)))
766 XExposeEvent
*ev
= &e
->xexpose
;
768 if(ev
->count
== 0 && (m
= getmon(ev
->window
)))
774 if(!c
|| !ISVISIBLE(c
))
775 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
777 unfocus(selmon
->sel
);
785 grabbuttons(c
, True
);
786 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
787 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
790 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
796 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
797 XFocusChangeEvent
*ev
= &e
->xfocus
;
799 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
800 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
805 focusmon(const Arg
*arg
) {
808 if(!(m
= getmonn(arg
->ui
)) || m
== selmon
)
810 unfocus(selmon
->sel
);
814 #endif /* XINERAMA */
817 focusstack(const Arg
*arg
) {
818 Client
*c
= NULL
, *i
;
823 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
825 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
828 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
832 for(; i
; i
= i
->next
)
843 getclient(Window w
) {
847 for(m
= mons
; m
; m
= m
->next
)
848 for(c
= m
->clients
; c
; c
= c
->next
)
855 getcolor(const char *colstr
) {
856 Colormap cmap
= DefaultColormap(dpy
, screen
);
859 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
860 die("error, cannot allocate color '%s'\n", colstr
);
870 if(w
== root
&& getrootpointer(&x
, &y
))
871 return getmonxy(x
, y
);
872 for(m
= mons
; m
; m
= m
->next
)
875 if((c
= getclient(w
)))
881 getmonn(unsigned int n
) {
885 for(m
= mons
, i
= 0; m
&& i
!= n
; m
= m
->next
, i
++);
890 getmonxy(int x
, int y
) {
893 for(m
= mons
; m
; m
= m
->next
)
894 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
900 getrootpointer(int *x
, int *y
) {
904 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
911 unsigned char *p
= NULL
;
912 unsigned long n
, extra
;
915 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
916 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
917 if(status
!= Success
)
926 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
931 if(!text
|| size
== 0)
934 XGetTextProperty(dpy
, w
, &name
, atom
);
937 if(name
.encoding
== XA_STRING
)
938 strncpy(text
, (char *)name
.value
, size
- 1);
940 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
942 strncpy(text
, *list
, size
- 1);
943 XFreeStringList(list
);
946 text
[size
- 1] = '\0';
952 grabbuttons(Client
*c
, Bool focused
) {
956 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
957 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
959 for(i
= 0; i
< LENGTH(buttons
); i
++)
960 if(buttons
[i
].click
== ClkClientWin
)
961 for(j
= 0; j
< LENGTH(modifiers
); j
++)
962 XGrabButton(dpy
, buttons
[i
].button
,
963 buttons
[i
].mask
| modifiers
[j
],
964 c
->win
, False
, BUTTONMASK
,
965 GrabModeAsync
, GrabModeSync
, None
, None
);
967 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
968 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
977 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
980 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
981 for(i
= 0; i
< LENGTH(keys
); i
++) {
982 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
983 for(j
= 0; j
< LENGTH(modifiers
); j
++)
984 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
985 True
, GrabModeAsync
, GrabModeAsync
);
991 initfont(const char *fontstr
) {
992 char *def
, **missing
;
996 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
999 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
1000 XFreeStringList(missing
);
1003 XFontSetExtents
*font_extents
;
1004 XFontStruct
**xfonts
;
1006 dc
.font
.ascent
= dc
.font
.descent
= 0;
1007 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
1008 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
1009 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
1010 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
1011 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1016 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1017 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1018 die("error, cannot load font: '%s'\n", fontstr
);
1019 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1020 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1022 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1026 isprotodel(Client
*c
) {
1031 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1032 for(i
= 0; !ret
&& i
< n
; i
++)
1033 if(protocols
[i
] == wmatom
[WMDelete
])
1041 keypress(XEvent
*e
) {
1047 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1048 for(i
= 0; i
< LENGTH(keys
); i
++)
1049 if(keysym
== keys
[i
].keysym
1050 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1052 keys
[i
].func(&(keys
[i
].arg
));
1056 killclient(const Arg
*arg
) {
1061 if(isprotodel(selmon
->sel
)) {
1062 ev
.type
= ClientMessage
;
1063 ev
.xclient
.window
= selmon
->sel
->win
;
1064 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1065 ev
.xclient
.format
= 32;
1066 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1067 ev
.xclient
.data
.l
[1] = CurrentTime
;
1068 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1071 XKillClient(dpy
, selmon
->sel
->win
);
1075 manage(Window w
, XWindowAttributes
*wa
) {
1077 Client
*c
, *t
= NULL
;
1078 Window trans
= None
;
1081 if(!(c
= malloc(sizeof(Client
))))
1082 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1088 c
->x
= wa
->x
+ selmon
->wx
;
1089 c
->y
= wa
->y
+ selmon
->wy
;
1092 c
->oldbw
= wa
->border_width
;
1093 if(c
->w
== selmon
->mw
&& c
->h
== selmon
->mh
) {
1099 if(c
->x
+ WIDTH(c
) > selmon
->mx
+ selmon
->mw
)
1100 c
->x
= selmon
->mx
+ selmon
->mw
- WIDTH(c
);
1101 if(c
->y
+ HEIGHT(c
) > selmon
->my
+ selmon
->mh
)
1102 c
->y
= selmon
->my
+ selmon
->mh
- HEIGHT(c
);
1103 c
->x
= MAX(c
->x
, selmon
->mx
);
1104 /* only fix client y-offset, if the client center might cover the bar */
1105 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1106 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: selmon
->my
);
1110 wc
.border_width
= c
->bw
;
1111 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1112 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1113 configure(c
); /* propagates border_width, if size doesn't change */
1115 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1116 grabbuttons(c
, False
);
1118 if(XGetTransientForHint(dpy
, w
, &trans
))
1119 t
= getclient(trans
);
1125 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1127 XRaiseWindow(dpy
, c
->win
);
1130 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1131 XMapWindow(dpy
, c
->win
);
1132 setclientstate(c
, NormalState
);
1137 mappingnotify(XEvent
*e
) {
1138 XMappingEvent
*ev
= &e
->xmapping
;
1140 XRefreshKeyboardMapping(ev
);
1141 if(ev
->request
== MappingKeyboard
)
1146 maprequest(XEvent
*e
) {
1147 static XWindowAttributes wa
;
1148 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1150 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1152 if(wa
.override_redirect
)
1154 if(!getclient(ev
->window
))
1155 manage(ev
->window
, &wa
);
1159 monocle(Monitor
*m
) {
1162 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1163 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1167 movemouse(const Arg
*arg
) {
1168 int x
, y
, ocx
, ocy
, nx
, ny
;
1173 if(!(c
= selmon
->sel
))
1178 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1179 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1181 if(!getrootpointer(&x
, &y
))
1184 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1186 case ConfigureRequest
:
1189 handler
[ev
.type
](&ev
);
1192 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1193 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1194 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1195 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1196 if(abs(selmon
->wx
- nx
) < snap
)
1198 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1199 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1200 if(abs(selmon
->wy
- ny
) < snap
)
1202 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1203 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1204 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1205 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1206 togglefloating(NULL
);
1208 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1209 resize(c
, nx
, ny
, c
->w
, c
->h
);
1213 while(ev
.type
!= ButtonRelease
);
1214 XUngrabPointer(dpy
, CurrentTime
);
1215 if((m
= getmonxy(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1223 nexttiled(Client
*c
) {
1224 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1229 propertynotify(XEvent
*e
) {
1232 XPropertyEvent
*ev
= &e
->xproperty
;
1234 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1236 else if(ev
->state
== PropertyDelete
)
1237 return; /* ignore */
1238 else if((c
= getclient(ev
->window
))) {
1241 case XA_WM_TRANSIENT_FOR
:
1242 XGetTransientForHint(dpy
, c
->win
, &trans
);
1243 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1246 case XA_WM_NORMAL_HINTS
:
1254 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1256 if(c
== selmon
->sel
)
1263 quit(const Arg
*arg
) {
1268 resize(Client
*c
, int x
, int y
, int w
, int h
) {
1271 if(applysizehints(c
, &x
, &y
, &w
, &h
)) {
1274 c
->w
= wc
.width
= w
;
1275 c
->h
= wc
.height
= h
;
1276 wc
.border_width
= c
->bw
;
1277 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1284 resizemouse(const Arg
*arg
) {
1291 if(!(c
= selmon
->sel
))
1296 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1297 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1299 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1301 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1303 case ConfigureRequest
:
1306 handler
[ev
.type
](&ev
);
1309 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1310 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1312 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1313 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
) {
1314 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1315 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1316 togglefloating(NULL
);
1318 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1319 resize(c
, c
->x
, c
->y
, nw
, nh
);
1323 while(ev
.type
!= ButtonRelease
);
1324 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1325 XUngrabPointer(dpy
, CurrentTime
);
1326 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1327 if((m
= getmonxy(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1335 restack(Monitor
*m
) {
1343 if(m
->sel
->isfloating
|| !lt
[m
->sellt
]->arrange
)
1344 XRaiseWindow(dpy
, m
->sel
->win
);
1345 if(lt
[m
->sellt
]->arrange
) {
1346 wc
.stack_mode
= Below
;
1347 wc
.sibling
= m
->barwin
;
1348 for(c
= m
->stack
; c
; c
= c
->snext
)
1349 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1350 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1351 wc
.sibling
= c
->win
;
1355 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1362 /* main event loop */
1364 while(running
&& !XNextEvent(dpy
, &ev
)) {
1365 if(handler
[ev
.type
])
1366 (handler
[ev
.type
])(&ev
); /* call handler */
1372 unsigned int i
, num
;
1373 Window d1
, d2
, *wins
= NULL
;
1374 XWindowAttributes wa
;
1376 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1377 for(i
= 0; i
< num
; i
++) {
1378 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1379 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1381 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1382 manage(wins
[i
], &wa
);
1384 for(i
= 0; i
< num
; i
++) { /* now the transients */
1385 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1387 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1388 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1389 manage(wins
[i
], &wa
);
1397 sendmon(Client
*c
, Monitor
*m
) {
1403 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1411 setclientstate(Client
*c
, long state
) {
1412 long data
[] = {state
, None
};
1414 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1415 PropModeReplace
, (unsigned char *)data
, 2);
1419 setlayout(const Arg
*arg
) {
1420 if(!arg
|| !arg
->v
|| arg
->v
!= lt
[selmon
->sellt
])
1423 lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1430 /* arg > 1.0 will set mfact absolutly */
1432 setmfact(const Arg
*arg
) {
1435 if(!arg
|| !lt
[selmon
->sellt
]->arrange
)
1437 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1438 if(f
< 0.1 || f
> 0.9)
1448 XSetWindowAttributes wa
;
1451 screen
= DefaultScreen(dpy
);
1452 root
= RootWindow(dpy
, screen
);
1456 sw
= DisplayWidth(dpy
, screen
);
1457 sh
= DisplayHeight(dpy
, screen
);
1458 bh
= dc
.h
= dc
.font
.height
+ 2;
1459 lt
[0] = &layouts
[0];
1460 lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1464 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1465 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1466 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1467 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1468 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1471 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1472 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1473 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1475 /* init appearance */
1476 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1477 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1478 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1479 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1480 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1481 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1482 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1483 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1484 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1486 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1489 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1490 w
= TEXTW(layouts
[i
].symbol
);
1496 /* EWMH support per view */
1497 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1498 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1500 /* select for events */
1501 wa
.cursor
= cursor
[CurNormal
];
1502 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1503 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1504 |PropertyChangeMask
;
1505 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1506 XSelectInput(dpy
, root
, wa
.event_mask
);
1512 showhide(Client
*c
) {
1515 if(ISVISIBLE(c
)) { /* show clients top down */
1516 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1517 if(!lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1518 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1521 else { /* hide clients bottom up */
1523 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1529 sigchld(int signal
) {
1530 while(0 < waitpid(-1, NULL
, WNOHANG
));
1534 spawn(const Arg
*arg
) {
1535 signal(SIGCHLD
, sigchld
);
1538 close(ConnectionNumber(dpy
));
1540 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1541 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1548 tag(const Arg
*arg
) {
1549 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1550 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1557 tagmon(const Arg
*arg
) {
1560 if(!selmon
->sel
|| !(m
= getmonn(arg
->ui
)))
1562 sendmon(selmon
->sel
, m
);
1564 #endif /* XINERAMA */
1567 textnw(const char *text
, unsigned int len
) {
1571 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1574 return XTextWidth(dc
.font
.xfont
, text
, len
);
1583 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1588 c
= nexttiled(m
->clients
);
1589 mw
= m
->mfact
* m
->ww
;
1590 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1596 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1598 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1603 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1604 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1605 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
));
1607 y
= c
->y
+ HEIGHT(c
);
1612 togglebar(const Arg
*arg
) {
1613 selmon
->showbar
= !selmon
->showbar
;
1614 updatebarpos(selmon
);
1615 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1620 togglefloating(const Arg
*arg
) {
1623 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1624 if(selmon
->sel
->isfloating
)
1625 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
, selmon
->sel
->w
, selmon
->sel
->h
);
1630 toggletag(const Arg
*arg
) {
1636 mask
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1638 selmon
->sel
->tags
= mask
;
1644 toggleview(const Arg
*arg
) {
1645 unsigned int mask
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1648 selmon
->tagset
[selmon
->seltags
] = mask
;
1654 unfocus(Client
*c
) {
1657 grabbuttons(c
, False
);
1658 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1659 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1663 unmanage(Client
*c
) {
1666 wc
.border_width
= c
->oldbw
;
1667 /* The server grab construct avoids race conditions. */
1669 XSetErrorHandler(xerrordummy
);
1670 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1673 if(c
->mon
->sel
== c
) {
1674 /* TODO: consider separate the next code into a function or into detachstack? */
1676 for(tc
= c
->mon
->stack
; tc
&& !ISVISIBLE(tc
); tc
= tc
->snext
);
1680 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1681 setclientstate(c
, WithdrawnState
);
1684 XSetErrorHandler(xerror
);
1690 unmapnotify(XEvent
*e
) {
1692 XUnmapEvent
*ev
= &e
->xunmap
;
1694 if((c
= getclient(ev
->window
)))
1701 XSetWindowAttributes wa
;
1703 wa
.override_redirect
= True
;
1704 wa
.background_pixmap
= ParentRelative
;
1705 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1707 for(m
= mons
; m
; m
= m
->next
) {
1708 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1710 CopyFromParent
, DefaultVisual(dpy
, screen
),
1711 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1712 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1713 XMapRaised(dpy
, m
->barwin
);
1718 updatebarpos(Monitor
*m
) {
1723 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1724 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1734 Monitor
*newmons
= NULL
, *m
, *tm
;
1738 #elif defined(XINERAMA)
1739 XineramaScreenInfo
*info
= NULL
;
1741 if(XineramaIsActive(dpy
))
1742 info
= XineramaQueryScreens(dpy
, &n
);
1744 /* allocate monitor(s) for the new geometry setup */
1745 for(i
= 0; i
< n
; i
++) {
1746 m
= (Monitor
*)malloc(sizeof(Monitor
));
1751 /* initialise monitor(s) */
1755 m
->screen_number
= 0;
1759 m
->mh
= m
->wh
= sh
/ 2;
1761 m
->screen_number
= 1;
1763 m
->my
= m
->wy
= sy
+ sh
/ 2;
1765 m
->mh
= m
->wh
= sh
/ 2;
1768 #elif defined(XINERAMA)
1769 if(XineramaIsActive(dpy
)) {
1770 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1771 m
->screen_number
= info
[i
].screen_number
;
1772 m
->mx
= m
->wx
= info
[i
].x_org
;
1773 m
->my
= m
->wy
= info
[i
].y_org
;
1774 m
->mw
= m
->ww
= info
[i
].width
;
1775 m
->mh
= m
->wh
= info
[i
].height
;
1781 /* default monitor setup */
1783 m
->screen_number
= 0;
1790 /* bar geometry setup */
1791 for(m
= newmons
; m
; m
= m
->next
) {
1792 /* TODO: consider removing the following values from config.h */
1798 m
->tagset
[0] = m
->tagset
[1] = 1;
1800 m
->showbar
= showbar
;
1805 /* reassign left over clients of disappeared monitors */
1806 for(tm
= mons
; tm
; tm
= tm
->next
)
1807 while(tm
->clients
) {
1809 tm
->clients
= c
->next
;
1816 /* select focused monitor */
1819 selmon
= getmon(root
);
1823 updatenumlockmask(void) {
1825 XModifierKeymap
*modmap
;
1828 modmap
= XGetModifierMapping(dpy
);
1829 for(i
= 0; i
< 8; i
++)
1830 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1831 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1832 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1833 numlockmask
= (1 << i
);
1834 XFreeModifiermap(modmap
);
1838 updatesizehints(Client
*c
) {
1842 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1843 /* size is uninitialized, ensure that size.flags aren't used */
1845 if(size
.flags
& PBaseSize
) {
1846 c
->basew
= size
.base_width
;
1847 c
->baseh
= size
.base_height
;
1849 else if(size
.flags
& PMinSize
) {
1850 c
->basew
= size
.min_width
;
1851 c
->baseh
= size
.min_height
;
1854 c
->basew
= c
->baseh
= 0;
1855 if(size
.flags
& PResizeInc
) {
1856 c
->incw
= size
.width_inc
;
1857 c
->inch
= size
.height_inc
;
1860 c
->incw
= c
->inch
= 0;
1861 if(size
.flags
& PMaxSize
) {
1862 c
->maxw
= size
.max_width
;
1863 c
->maxh
= size
.max_height
;
1866 c
->maxw
= c
->maxh
= 0;
1867 if(size
.flags
& PMinSize
) {
1868 c
->minw
= size
.min_width
;
1869 c
->minh
= size
.min_height
;
1871 else if(size
.flags
& PBaseSize
) {
1872 c
->minw
= size
.base_width
;
1873 c
->minh
= size
.base_height
;
1876 c
->minw
= c
->minh
= 0;
1877 if(size
.flags
& PAspect
) {
1878 c
->mina
= (float)size
.min_aspect
.y
/ (float)size
.min_aspect
.x
;
1879 c
->maxa
= (float)size
.max_aspect
.x
/ (float)size
.max_aspect
.y
;
1882 c
->maxa
= c
->mina
= 0.0;
1883 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1884 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1888 updatetitle(Client
*c
) {
1889 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1890 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1894 updatestatus(void) {
1895 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1896 strcpy(stext
, "dwm-"VERSION
);
1901 updatewmhints(Client
*c
) {
1904 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1905 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1906 wmh
->flags
&= ~XUrgencyHint
;
1907 XSetWMHints(dpy
, c
->win
, wmh
);
1910 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1917 view(const Arg
*arg
) {
1918 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1920 selmon
->seltags
^= 1; /* toggle sel tagset */
1921 if(arg
->ui
& TAGMASK
)
1922 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1926 /* There's no way to check accesses to destroyed windows, thus those cases are
1927 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1928 * default error handler, which may call exit. */
1930 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1931 if(ee
->error_code
== BadWindow
1932 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1933 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1934 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1935 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1936 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1937 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1938 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1939 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1941 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1942 ee
->request_code
, ee
->error_code
);
1943 return xerrorxlib(dpy
, ee
); /* may call exit */
1947 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1951 /* Startup Error handler to check if another window manager
1952 * is already running. */
1954 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1960 zoom(const Arg
*arg
) {
1961 Client
*c
= selmon
->sel
;
1963 if(!lt
[selmon
->sellt
]->arrange
|| lt
[selmon
->sellt
]->arrange
== monocle
|| (selmon
->sel
&& selmon
->sel
->isfloating
))
1965 if(c
== nexttiled(selmon
->clients
))
1966 if(!c
|| !(c
= nexttiled(c
->next
)))
1975 main(int argc
, char *argv
[]) {
1976 if(argc
== 2 && !strcmp("-v", argv
[1]))
1977 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1979 die("usage: dwm [-v]\n");
1981 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1982 fputs("warning: no locale support\n", stderr
);
1984 if(!(dpy
= XOpenDisplay(NULL
)))
1985 die("dwm: cannot open display\n");