Xinqi Bao's Git
36304c0f97e3b6877e7869f9f94495cdac6622a9
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
, btx
; /* 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];
143 const char *instance
;
149 /* function declarations */
150 static void applyrules(Client
*c
);
151 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
152 static void arrange(void);
153 static void attach(Client
*c
);
154 static void attachstack(Client
*c
);
155 static void buttonpress(XEvent
*e
);
156 static void checkotherwm(void);
157 static void cleanup(void);
158 static void cleanupmons(void);
159 static void clearurgent(Client
*c
);
160 static void configure(Client
*c
);
161 static void configurenotify(XEvent
*e
);
162 static void configurerequest(XEvent
*e
);
163 static void destroynotify(XEvent
*e
);
164 static void detach(Client
*c
);
165 static void detachstack(Client
*c
);
166 static void die(const char *errstr
, ...);
167 static Monitor
*dirtomon(int dir
);
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 focusmon(const Arg
*arg
);
177 static void focusstack(const Arg
*arg
);
178 static unsigned long getcolor(const char *colstr
);
179 static Bool
getrootpointer(int *x
, int *y
);
180 static long getstate(Window w
);
181 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
182 static void grabbuttons(Client
*c
, Bool focused
);
183 static void grabkeys(void);
184 static void initfont(const char *fontstr
);
185 static Bool
isprotodel(Client
*c
);
186 static void keypress(XEvent
*e
);
187 static void killclient(const Arg
*arg
);
188 static void manage(Window w
, XWindowAttributes
*wa
);
189 static void mappingnotify(XEvent
*e
);
190 static void maprequest(XEvent
*e
);
191 static void monocle(Monitor
*m
);
192 static void movemouse(const Arg
*arg
);
193 static Client
*nexttiled(Client
*c
);
194 static Monitor
*pointertomon(int x
, int y
);
195 static void propertynotify(XEvent
*e
);
196 static void quit(const Arg
*arg
);
197 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
198 static void resizemouse(const Arg
*arg
);
199 static void restack(Monitor
*m
);
200 static void run(void);
201 static void scan(void);
202 static void sendmon(Client
*c
, Monitor
*m
);
203 static void setclientstate(Client
*c
, long state
);
204 static void setlayout(const Arg
*arg
);
205 static void setmfact(const Arg
*arg
);
206 static void setup(void);
207 static void showhide(Client
*c
);
208 static void sigchld(int signal
);
209 static void spawn(const Arg
*arg
);
210 static void tag(const Arg
*arg
);
211 static void tagmon(const Arg
*arg
);
212 static int textnw(const char *text
, unsigned int len
);
213 static void tile(Monitor
*);
214 static void togglebar(const Arg
*arg
);
215 static void togglefloating(const Arg
*arg
);
216 static void toggletag(const Arg
*arg
);
217 static void toggleview(const Arg
*arg
);
218 static void unfocus(Client
*c
);
219 static void unmanage(Client
*c
);
220 static void unmapnotify(XEvent
*e
);
221 static void updategeom(void);
222 static void updatebarpos(Monitor
*m
);
223 static void updatebars(void);
224 static void updatenumlockmask(void);
225 static void updatesizehints(Client
*c
);
226 static void updatestatus(void);
227 static void updatetitle(Client
*c
);
228 static void updatewmhints(Client
*c
);
229 static void view(const Arg
*arg
);
230 static Client
*wintoclient(Window w
);
231 static Monitor
*wintomon(Window w
);
232 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
233 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
234 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
235 static void zoom(const Arg
*arg
);
238 static char stext
[256];
240 static int sw
, sh
; /* X display screen geometry x, y, width, height */
241 static int bh
, blw
= 0; /* bar geometry */
242 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
243 static unsigned int numlockmask
= 0;
244 static void (*handler
[LASTEvent
]) (XEvent
*) = {
245 [ButtonPress
] = buttonpress
,
246 [ConfigureRequest
] = configurerequest
,
247 [ConfigureNotify
] = configurenotify
,
248 [DestroyNotify
] = destroynotify
,
249 [EnterNotify
] = enternotify
,
252 [KeyPress
] = keypress
,
253 [MappingNotify
] = mappingnotify
,
254 [MapRequest
] = maprequest
,
255 [PropertyNotify
] = propertynotify
,
256 [UnmapNotify
] = unmapnotify
258 static Atom wmatom
[WMLast
], netatom
[NetLast
];
260 static Bool running
= True
;
261 static Cursor cursor
[CurLast
];
264 static Layout
*lt
[] = { NULL
, NULL
};
265 static Monitor
*mons
= NULL
, *selmon
= NULL
;
267 /* configuration, allows nested code to access above variables */
270 /* compile-time check if all tags fit into an unsigned int bit array. */
271 struct NumTags
{ char limitexceeded
[sizeof(unsigned int) * 8 < LENGTH(tags
) ? -1 : 1]; };
273 /* function implementations */
275 applyrules(Client
*c
) {
278 XClassHint ch
= { 0 };
281 c
->isfloating
= c
->tags
= 0;
282 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
283 for(i
= 0; i
< LENGTH(rules
); i
++) {
285 if((!r
->title
|| strstr(c
->name
, r
->title
))
286 && (!r
->class || (ch
.res_class
&& strstr(ch
.res_class
, r
->class)))
287 && (!r
->instance
|| (ch
.res_name
&& strstr(ch
.res_name
, r
->instance
)))) {
288 c
->isfloating
= r
->isfloating
;
297 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
301 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
305 /* set minimum possible */
314 if(*x
+ *w
+ 2 * c
->bw
< 0)
316 if(*y
+ *h
+ 2 * c
->bw
< 0)
320 if(*x
> m
->mx
+ m
->mw
)
321 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
322 if(*y
> m
->my
+ m
->mh
)
323 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
324 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
326 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
333 if(resizehints
|| c
->isfloating
) {
334 /* see last two sentences in ICCCM 4.1.2.3 */
335 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
336 if(!baseismin
) { /* temporarily remove base dimensions */
340 /* adjust for aspect limits */
341 if(c
->mina
> 0 && c
->maxa
> 0) {
342 if(c
->maxa
< (float)*w
/ *h
)
344 else if(c
->mina
< (float)*h
/ *w
)
347 if(baseismin
) { /* increment calculation requires this */
351 /* adjust for increment value */
356 /* restore base dimensions */
359 *w
= MAX(*w
, c
->minw
);
360 *h
= MAX(*h
, c
->minh
);
362 *w
= MIN(*w
, c
->maxw
);
364 *h
= MIN(*h
, c
->maxh
);
366 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
373 /* optimise two loops into one, check focus(NULL) */
374 for(m
= mons
; m
; m
= m
->next
)
377 for(m
= mons
; m
; m
= m
->next
) {
378 if(lt
[m
->sellt
]->arrange
)
379 lt
[m
->sellt
]->arrange(m
);
386 c
->next
= c
->mon
->clients
;
391 attachstack(Client
*c
) {
392 c
->snext
= c
->mon
->stack
;
397 buttonpress(XEvent
*e
) {
398 unsigned int i
, x
, click
;
402 XButtonPressedEvent
*ev
= &e
->xbutton
;
405 /* focus monitor if necessary */
406 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
407 unfocus(selmon
->sel
);
411 if(ev
->window
== selmon
->barwin
&& ev
->x
>= selmon
->btx
) {
416 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
417 if(i
< LENGTH(tags
)) {
421 else if(ev
->x
< x
+ blw
)
423 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
424 click
= ClkStatusText
;
428 else if((c
= wintoclient(ev
->window
))) {
430 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
);
442 /* this causes an error if some other window manager is running */
443 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
446 die("dwm: another window manager is already running\n");
447 XSetErrorHandler(xerror
);
454 Layout foo
= { "", NULL
};
458 lt
[selmon
->sellt
] = &foo
;
459 for(m
= mons
; m
; m
= m
->next
)
463 XFreeFontSet(dpy
, dc
.font
.set
);
465 XFreeFont(dpy
, dc
.font
.xfont
);
466 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
467 XFreePixmap(dpy
, dc
.drawable
);
469 XFreeCursor(dpy
, cursor
[CurNormal
]);
470 XFreeCursor(dpy
, cursor
[CurResize
]);
471 XFreeCursor(dpy
, cursor
[CurMove
]);
474 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
483 XUnmapWindow(dpy
, mons
->barwin
);
484 XDestroyWindow(dpy
, mons
->barwin
);
491 clearurgent(Client
*c
) {
495 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
497 wmh
->flags
&= ~XUrgencyHint
;
498 XSetWMHints(dpy
, c
->win
, wmh
);
503 configure(Client
*c
) {
506 ce
.type
= ConfigureNotify
;
514 ce
.border_width
= c
->bw
;
516 ce
.override_redirect
= False
;
517 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
521 configurenotify(XEvent
*e
) {
523 XConfigureEvent
*ev
= &e
->xconfigure
;
525 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
530 XFreePixmap(dpy
, dc
.drawable
);
531 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
533 for(m
= mons
; m
; m
= m
->next
)
534 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
540 configurerequest(XEvent
*e
) {
543 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
546 if((c
= wintoclient(ev
->window
))) {
547 if(ev
->value_mask
& CWBorderWidth
)
548 c
->bw
= ev
->border_width
;
549 else if(c
->isfloating
|| !lt
[selmon
->sellt
]->arrange
) {
551 if(ev
->value_mask
& CWX
)
552 c
->x
= m
->mx
+ ev
->x
;
553 if(ev
->value_mask
& CWY
)
554 c
->y
= m
->my
+ ev
->y
;
555 if(ev
->value_mask
& CWWidth
)
557 if(ev
->value_mask
& CWHeight
)
559 if((c
->x
- m
->mx
+ c
->w
) > m
->mw
&& c
->isfloating
)
560 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
561 if((c
->y
- m
->my
+ c
->h
) > m
->mh
&& c
->isfloating
)
562 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
563 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
566 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
574 wc
.width
= ev
->width
;
575 wc
.height
= ev
->height
;
576 wc
.border_width
= ev
->border_width
;
577 wc
.sibling
= ev
->above
;
578 wc
.stack_mode
= ev
->detail
;
579 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
585 destroynotify(XEvent
*e
) {
587 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
589 if((c
= wintoclient(ev
->window
)))
597 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
602 detachstack(Client
*c
) {
605 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
608 if(c
== c
->mon
->sel
) {
609 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
615 die(const char *errstr
, ...) {
618 va_start(ap
, errstr
);
619 vfprintf(stderr
, errstr
, ap
);
629 if(!(m
= selmon
->next
))
634 for(m
= mons
; m
->next
; m
= m
->next
);
636 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
642 drawbar(Monitor
*m
) {
644 unsigned int i
, occ
= 0, urg
= 0;
648 for(c
= m
->clients
; c
; c
= c
->next
) {
654 if(mons
->next
) { /* more than a single monitor */
655 dc
.w
= TEXTW(monsyms
[m
->screen_number
]);
656 drawtext(monsyms
[m
->screen_number
], selmon
== m
? dc
.sel
: dc
.norm
, False
);
660 for(i
= 0; i
< LENGTH(tags
); i
++) {
661 dc
.w
= TEXTW(tags
[i
]);
662 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
663 drawtext(tags
[i
], col
, urg
& 1 << i
);
664 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
665 occ
& 1 << i
, urg
& 1 << i
, col
);
670 drawtext(lt
[m
->sellt
]->symbol
, dc
.norm
, False
);
675 if(m
== selmon
) { /* status is only drawn on selected monitor */
682 drawtext(stext
, dc
.norm
, False
);
686 if((dc
.w
= dc
.x
- x
) > bh
) {
689 col
= m
== selmon
? dc
.sel
: dc
.norm
;
690 drawtext(m
->sel
->name
, col
, False
);
691 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
694 drawtext(NULL
, dc
.norm
, False
);
696 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
704 for(m
= mons
; m
; m
= m
->next
)
709 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
712 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
714 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
715 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
716 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
720 r
.width
= r
.height
= x
+ 1;
721 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
724 r
.width
= r
.height
= x
;
725 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
730 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
732 int i
, x
, y
, h
, len
, olen
;
733 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
735 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
736 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
740 h
= dc
.font
.ascent
+ dc
.font
.descent
;
741 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
743 /* shorten text if necessary */
744 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
747 memcpy(buf
, text
, len
);
749 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
750 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
752 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
754 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
758 enternotify(XEvent
*e
) {
761 XCrossingEvent
*ev
= &e
->xcrossing
;
763 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
765 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
766 unfocus(selmon
->sel
);
769 if((c
= wintoclient(ev
->window
)))
778 XExposeEvent
*ev
= &e
->xexpose
;
780 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
786 if(!c
|| !ISVISIBLE(c
))
787 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
789 unfocus(selmon
->sel
);
797 grabbuttons(c
, True
);
798 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
799 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
802 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
808 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
809 XFocusChangeEvent
*ev
= &e
->xfocus
;
811 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
812 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
816 focusmon(const Arg
*arg
) {
821 m
= dirtomon(arg
->i
);
822 unfocus(selmon
->sel
);
828 focusstack(const Arg
*arg
) {
829 Client
*c
= NULL
, *i
;
834 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
836 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
839 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
843 for(; i
; i
= i
->next
)
854 getcolor(const char *colstr
) {
855 Colormap cmap
= DefaultColormap(dpy
, screen
);
858 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
859 die("error, cannot allocate color '%s'\n", colstr
);
864 getrootpointer(int *x
, int *y
) {
869 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
876 unsigned char *p
= NULL
;
877 unsigned long n
, extra
;
880 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
881 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
882 if(status
!= Success
)
891 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
896 if(!text
|| size
== 0)
899 XGetTextProperty(dpy
, w
, &name
, atom
);
902 if(name
.encoding
== XA_STRING
)
903 strncpy(text
, (char *)name
.value
, size
- 1);
905 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
907 strncpy(text
, *list
, size
- 1);
908 XFreeStringList(list
);
911 text
[size
- 1] = '\0';
917 grabbuttons(Client
*c
, Bool focused
) {
921 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
922 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
924 for(i
= 0; i
< LENGTH(buttons
); i
++)
925 if(buttons
[i
].click
== ClkClientWin
)
926 for(j
= 0; j
< LENGTH(modifiers
); j
++)
927 XGrabButton(dpy
, buttons
[i
].button
,
928 buttons
[i
].mask
| modifiers
[j
],
929 c
->win
, False
, BUTTONMASK
,
930 GrabModeAsync
, GrabModeSync
, None
, None
);
932 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
933 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
942 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
945 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
946 for(i
= 0; i
< LENGTH(keys
); i
++) {
947 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
948 for(j
= 0; j
< LENGTH(modifiers
); j
++)
949 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
950 True
, GrabModeAsync
, GrabModeAsync
);
956 initfont(const char *fontstr
) {
957 char *def
, **missing
;
961 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
964 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
965 XFreeStringList(missing
);
968 XFontSetExtents
*font_extents
;
969 XFontStruct
**xfonts
;
971 dc
.font
.ascent
= dc
.font
.descent
= 0;
972 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
973 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
974 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
975 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
976 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
981 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
982 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
983 die("error, cannot load font: '%s'\n", fontstr
);
984 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
985 dc
.font
.descent
= dc
.font
.xfont
->descent
;
987 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
991 isprotodel(Client
*c
) {
996 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
997 for(i
= 0; !ret
&& i
< n
; i
++)
998 if(protocols
[i
] == wmatom
[WMDelete
])
1006 keypress(XEvent
*e
) {
1012 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1013 for(i
= 0; i
< LENGTH(keys
); i
++)
1014 if(keysym
== keys
[i
].keysym
1015 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1017 keys
[i
].func(&(keys
[i
].arg
));
1021 killclient(const Arg
*arg
) {
1026 if(isprotodel(selmon
->sel
)) {
1027 ev
.type
= ClientMessage
;
1028 ev
.xclient
.window
= selmon
->sel
->win
;
1029 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1030 ev
.xclient
.format
= 32;
1031 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1032 ev
.xclient
.data
.l
[1] = CurrentTime
;
1033 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1036 XKillClient(dpy
, selmon
->sel
->win
);
1040 manage(Window w
, XWindowAttributes
*wa
) {
1042 Client
*c
, *t
= NULL
;
1043 Window trans
= None
;
1046 if(!(c
= malloc(sizeof(Client
))))
1047 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1050 if(XGetTransientForHint(dpy
, w
, &trans
))
1051 t
= wintoclient(trans
);
1061 c
->x
= wa
->x
+ c
->mon
->wx
;
1062 c
->y
= wa
->y
+ c
->mon
->wy
;
1065 c
->oldbw
= wa
->border_width
;
1066 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1072 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1073 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1074 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1075 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1076 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1077 /* only fix client y-offset, if the client center might cover the bar */
1078 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1079 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1082 wc
.border_width
= c
->bw
;
1083 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1084 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1085 configure(c
); /* propagates border_width, if size doesn't change */
1087 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1088 grabbuttons(c
, False
);
1091 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1093 XRaiseWindow(dpy
, c
->win
);
1096 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1097 XMapWindow(dpy
, c
->win
);
1098 setclientstate(c
, NormalState
);
1103 mappingnotify(XEvent
*e
) {
1104 XMappingEvent
*ev
= &e
->xmapping
;
1106 XRefreshKeyboardMapping(ev
);
1107 if(ev
->request
== MappingKeyboard
)
1112 maprequest(XEvent
*e
) {
1113 static XWindowAttributes wa
;
1114 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1116 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1118 if(wa
.override_redirect
)
1120 if(!wintoclient(ev
->window
))
1121 manage(ev
->window
, &wa
);
1125 monocle(Monitor
*m
) {
1128 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1129 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1133 movemouse(const Arg
*arg
) {
1134 int x
, y
, ocx
, ocy
, nx
, ny
;
1139 if(!(c
= selmon
->sel
))
1144 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1145 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1147 if(!getrootpointer(&x
, &y
))
1150 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1152 case ConfigureRequest
:
1155 handler
[ev
.type
](&ev
);
1158 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1159 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1160 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1161 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1162 if(abs(selmon
->wx
- nx
) < snap
)
1164 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1165 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1166 if(abs(selmon
->wy
- ny
) < snap
)
1168 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1169 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1170 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1171 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1172 togglefloating(NULL
);
1174 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1175 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1179 while(ev
.type
!= ButtonRelease
);
1180 XUngrabPointer(dpy
, CurrentTime
);
1181 if((m
= pointertomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1189 nexttiled(Client
*c
) {
1190 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1195 pointertomon(int x
, int y
) {
1198 for(m
= mons
; m
; m
= m
->next
)
1199 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1205 propertynotify(XEvent
*e
) {
1208 XPropertyEvent
*ev
= &e
->xproperty
;
1210 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1212 else if(ev
->state
== PropertyDelete
)
1213 return; /* ignore */
1214 else if((c
= wintoclient(ev
->window
))) {
1217 case XA_WM_TRANSIENT_FOR
:
1218 XGetTransientForHint(dpy
, c
->win
, &trans
);
1219 if(!c
->isfloating
&& (c
->isfloating
= (wintoclient(trans
) != NULL
)))
1222 case XA_WM_NORMAL_HINTS
:
1230 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1232 if(c
== selmon
->sel
)
1239 quit(const Arg
*arg
) {
1244 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1247 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
)) {
1250 c
->w
= wc
.width
= w
;
1251 c
->h
= wc
.height
= h
;
1252 wc
.border_width
= c
->bw
;
1253 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1260 resizemouse(const Arg
*arg
) {
1267 if(!(c
= selmon
->sel
))
1272 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1273 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1275 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1277 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1279 case ConfigureRequest
:
1282 handler
[ev
.type
](&ev
);
1285 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1286 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1287 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1288 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
) {
1289 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1290 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1291 togglefloating(NULL
);
1293 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1294 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1298 while(ev
.type
!= ButtonRelease
);
1299 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1300 XUngrabPointer(dpy
, CurrentTime
);
1301 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1302 if((m
= pointertomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1310 restack(Monitor
*m
) {
1318 if(m
->sel
->isfloating
|| !lt
[m
->sellt
]->arrange
)
1319 XRaiseWindow(dpy
, m
->sel
->win
);
1320 if(lt
[m
->sellt
]->arrange
) {
1321 wc
.stack_mode
= Below
;
1322 wc
.sibling
= m
->barwin
;
1323 for(c
= m
->stack
; c
; c
= c
->snext
)
1324 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1325 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1326 wc
.sibling
= c
->win
;
1330 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1337 /* main event loop */
1339 while(running
&& !XNextEvent(dpy
, &ev
)) {
1340 if(handler
[ev
.type
])
1341 (handler
[ev
.type
])(&ev
); /* call handler */
1347 unsigned int i
, num
;
1348 Window d1
, d2
, *wins
= NULL
;
1349 XWindowAttributes wa
;
1351 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1352 for(i
= 0; i
< num
; i
++) {
1353 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1354 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1356 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1357 manage(wins
[i
], &wa
);
1359 for(i
= 0; i
< num
; i
++) { /* now the transients */
1360 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1362 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1363 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1364 manage(wins
[i
], &wa
);
1372 sendmon(Client
*c
, Monitor
*m
) {
1379 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1387 setclientstate(Client
*c
, long state
) {
1388 long data
[] = {state
, None
};
1390 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1391 PropModeReplace
, (unsigned char *)data
, 2);
1395 setlayout(const Arg
*arg
) {
1396 if(!arg
|| !arg
->v
|| arg
->v
!= lt
[selmon
->sellt
])
1399 lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1406 /* arg > 1.0 will set mfact absolutly */
1408 setmfact(const Arg
*arg
) {
1411 if(!arg
|| !lt
[selmon
->sellt
]->arrange
)
1413 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1414 if(f
< 0.1 || f
> 0.9)
1424 XSetWindowAttributes wa
;
1427 screen
= DefaultScreen(dpy
);
1428 root
= RootWindow(dpy
, screen
);
1430 sw
= DisplayWidth(dpy
, screen
);
1431 sh
= DisplayHeight(dpy
, screen
);
1432 bh
= dc
.h
= dc
.font
.height
+ 2;
1433 lt
[0] = &layouts
[0];
1434 lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1437 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1438 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1439 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1440 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1441 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1443 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1444 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1445 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1446 /* init appearance */
1447 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1448 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1449 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1450 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1451 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1452 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1453 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1454 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1455 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1457 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1459 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1460 w
= TEXTW(layouts
[i
].symbol
);
1465 /* EWMH support per view */
1466 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1467 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1468 /* select for events */
1469 wa
.cursor
= cursor
[CurNormal
];
1470 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1471 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1472 |PropertyChangeMask
;
1473 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1474 XSelectInput(dpy
, root
, wa
.event_mask
);
1479 showhide(Client
*c
) {
1482 if(ISVISIBLE(c
)) { /* show clients top down */
1483 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1484 if(!lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1485 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1488 else { /* hide clients bottom up */
1490 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1496 sigchld(int signal
) {
1497 while(0 < waitpid(-1, NULL
, WNOHANG
));
1501 spawn(const Arg
*arg
) {
1502 signal(SIGCHLD
, sigchld
);
1505 close(ConnectionNumber(dpy
));
1507 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1508 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1515 tag(const Arg
*arg
) {
1516 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1517 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1523 tagmon(const Arg
*arg
) {
1524 if(!selmon
->sel
|| !mons
->next
)
1526 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1530 textnw(const char *text
, unsigned int len
) {
1534 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1537 return XTextWidth(dc
.font
.xfont
, text
, len
);
1546 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1550 c
= nexttiled(m
->clients
);
1551 mw
= m
->mfact
* m
->ww
;
1552 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1556 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1558 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1562 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1563 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1564 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1566 y
= c
->y
+ HEIGHT(c
);
1571 togglebar(const Arg
*arg
) {
1572 selmon
->showbar
= !selmon
->showbar
;
1573 updatebarpos(selmon
);
1574 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1579 togglefloating(const Arg
*arg
) {
1582 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1583 if(selmon
->sel
->isfloating
)
1584 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1585 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1590 toggletag(const Arg
*arg
) {
1595 mask
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1597 selmon
->sel
->tags
= mask
;
1603 toggleview(const Arg
*arg
) {
1604 unsigned int mask
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1607 selmon
->tagset
[selmon
->seltags
] = mask
;
1613 unfocus(Client
*c
) {
1616 grabbuttons(c
, False
);
1617 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1618 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1622 unmanage(Client
*c
) {
1625 wc
.border_width
= c
->oldbw
;
1626 /* The server grab construct avoids race conditions. */
1628 XSetErrorHandler(xerrordummy
);
1629 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1632 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1633 setclientstate(c
, WithdrawnState
);
1636 XSetErrorHandler(xerror
);
1643 unmapnotify(XEvent
*e
) {
1645 XUnmapEvent
*ev
= &e
->xunmap
;
1647 if((c
= wintoclient(ev
->window
)))
1654 XSetWindowAttributes wa
;
1656 wa
.override_redirect
= True
;
1657 wa
.background_pixmap
= ParentRelative
;
1658 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1659 for(m
= mons
; m
; m
= m
->next
) {
1660 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1661 CopyFromParent
, DefaultVisual(dpy
, screen
),
1662 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1663 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1664 XMapRaised(dpy
, m
->barwin
);
1669 updatebarpos(Monitor
*m
) {
1674 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1675 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1685 Monitor
*newmons
= NULL
, *m
, *tm
;
1688 XineramaScreenInfo
*info
= NULL
;
1690 if(XineramaIsActive(dpy
))
1691 info
= XineramaQueryScreens(dpy
, &n
);
1692 #endif /* XINERAMA */
1693 /* allocate monitor(s) for the new geometry setup */
1694 for(i
= 0; i
< n
; i
++) {
1695 m
= (Monitor
*)malloc(sizeof(Monitor
));
1699 /* initialise monitor(s) */
1701 if(XineramaIsActive(dpy
)) {
1702 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1703 m
->screen_number
= info
[i
].screen_number
;
1704 m
->mx
= m
->wx
= info
[i
].x_org
;
1705 m
->my
= m
->wy
= info
[i
].y_org
;
1706 m
->mw
= m
->ww
= info
[i
].width
;
1707 m
->mh
= m
->wh
= info
[i
].height
;
1712 #endif /* XINERAMA */
1713 /* default monitor setup */
1715 m
->screen_number
= 0;
1721 /* bar geometry setup */
1722 for(m
= newmons
; m
; m
= m
->next
) {
1723 m
->sel
= m
->stack
= m
->clients
= NULL
;
1726 m
->tagset
[0] = m
->tagset
[1] = 1;
1728 m
->showbar
= SHOWBAR
;
1732 /* reassign left over clients of disappeared monitors */
1733 for(tm
= mons
; tm
; tm
= tm
->next
)
1734 while(tm
->clients
) {
1736 tm
->clients
= c
->next
;
1742 /* select focused monitor */
1744 selmon
= mons
= newmons
;
1745 selmon
= wintomon(root
);
1749 updatenumlockmask(void) {
1751 XModifierKeymap
*modmap
;
1754 modmap
= XGetModifierMapping(dpy
);
1755 for(i
= 0; i
< 8; i
++)
1756 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1757 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1758 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1759 numlockmask
= (1 << i
);
1760 XFreeModifiermap(modmap
);
1764 updatesizehints(Client
*c
) {
1768 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1769 /* size is uninitialized, ensure that size.flags aren't used */
1771 if(size
.flags
& PBaseSize
) {
1772 c
->basew
= size
.base_width
;
1773 c
->baseh
= size
.base_height
;
1775 else if(size
.flags
& PMinSize
) {
1776 c
->basew
= size
.min_width
;
1777 c
->baseh
= size
.min_height
;
1780 c
->basew
= c
->baseh
= 0;
1781 if(size
.flags
& PResizeInc
) {
1782 c
->incw
= size
.width_inc
;
1783 c
->inch
= size
.height_inc
;
1786 c
->incw
= c
->inch
= 0;
1787 if(size
.flags
& PMaxSize
) {
1788 c
->maxw
= size
.max_width
;
1789 c
->maxh
= size
.max_height
;
1792 c
->maxw
= c
->maxh
= 0;
1793 if(size
.flags
& PMinSize
) {
1794 c
->minw
= size
.min_width
;
1795 c
->minh
= size
.min_height
;
1797 else if(size
.flags
& PBaseSize
) {
1798 c
->minw
= size
.base_width
;
1799 c
->minh
= size
.base_height
;
1802 c
->minw
= c
->minh
= 0;
1803 if(size
.flags
& PAspect
) {
1804 c
->mina
= (float)size
.min_aspect
.y
/ (float)size
.min_aspect
.x
;
1805 c
->maxa
= (float)size
.max_aspect
.x
/ (float)size
.max_aspect
.y
;
1808 c
->maxa
= c
->mina
= 0.0;
1809 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1810 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1814 updatetitle(Client
*c
) {
1815 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1816 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1820 updatestatus(void) {
1821 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1822 strcpy(stext
, "dwm-"VERSION
);
1827 updatewmhints(Client
*c
) {
1830 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1831 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1832 wmh
->flags
&= ~XUrgencyHint
;
1833 XSetWMHints(dpy
, c
->win
, wmh
);
1836 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1842 view(const Arg
*arg
) {
1843 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1845 selmon
->seltags
^= 1; /* toggle sel tagset */
1846 if(arg
->ui
& TAGMASK
)
1847 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1852 wintoclient(Window w
) {
1856 for(m
= mons
; m
; m
= m
->next
)
1857 for(c
= m
->clients
; c
; c
= c
->next
)
1864 wintomon(Window w
) {
1869 if(w
== root
&& getrootpointer(&x
, &y
))
1870 return pointertomon(x
, y
);
1871 for(m
= mons
; m
; m
= m
->next
)
1874 if((c
= wintoclient(w
)))
1879 /* There's no way to check accesses to destroyed windows, thus those cases are
1880 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1881 * default error handler, which may call exit. */
1883 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1884 if(ee
->error_code
== BadWindow
1885 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1886 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1887 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1888 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1889 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1890 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1891 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1892 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1894 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1895 ee
->request_code
, ee
->error_code
);
1896 return xerrorxlib(dpy
, ee
); /* may call exit */
1900 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1904 /* Startup Error handler to check if another window manager
1905 * is already running. */
1907 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1913 zoom(const Arg
*arg
) {
1914 Client
*c
= selmon
->sel
;
1916 if(!lt
[selmon
->sellt
]->arrange
1917 || lt
[selmon
->sellt
]->arrange
== monocle
1918 || (selmon
->sel
&& selmon
->sel
->isfloating
))
1920 if(c
== nexttiled(selmon
->clients
))
1921 if(!c
|| !(c
= nexttiled(c
->next
)))
1930 main(int argc
, char *argv
[]) {
1931 if(argc
== 2 && !strcmp("-v", argv
[1]))
1932 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1934 die("usage: dwm [-v]\n");
1935 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1936 fputs("warning: no locale support\n", stderr
);
1937 if(!(dpy
= XOpenDisplay(NULL
)))
1938 die("dwm: cannot open display\n");