Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details.
3 * dynamic window manager is designed like any other X client as well. It is
4 * driven through handling X events. In contrast to other X clients, a window
5 * manager selects for SubstructureRedirectMask on the root window, to receive
6 * events about window (dis-)appearance. Only one X connection at a time is
7 * allowed to select for this event mask.
9 * The event handlers of dwm are organized in an array which is accessed
10 * whenever a new event has been fetched. This allows event dispatching
13 * Each child of the root window is called a client, except windows which have
14 * set the override_redirect flag. Clients are organized in a linked client
15 * list on each monitor, the focus history is remembered through a stack list
16 * on each monitor. Each client contains a bit array to indicate the tags of a
19 * Keys and tagging rules are organized as arrays and defined in config.h.
21 * To understand everything else, start reading main().
31 #include <sys/types.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
40 #include <X11/extensions/Xinerama.h>
44 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
45 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
46 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
47 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
48 #define LENGTH(X) (sizeof X / sizeof X[0])
49 #define MAX(A, B) ((A) > (B) ? (A) : (B))
50 #define MIN(A, B) ((A) < (B) ? (A) : (B))
51 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
52 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
53 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
54 #define TAGMASK ((1 << LENGTH(tags)) - 1)
55 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
58 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
59 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
60 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
61 enum { WMProtocols
, WMDelete
, WMState
, WMLast
}; /* default atoms */
62 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
63 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
76 void (*func
)(const Arg
*arg
);
80 typedef struct Monitor Monitor
;
81 typedef struct Client Client
;
86 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
89 Bool isfixed
, isfloating
, isurgent
;
98 unsigned long norm
[ColLast
];
99 unsigned long sel
[ColLast
];
109 } DC
; /* draw context */
114 void (*func
)(const Arg
*);
120 void (*arrange
)(Monitor
*);
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 const char broken
[] = "broken";
241 static char stext
[256], ntext
[8];
243 static int sw
, sh
; /* X display screen geometry 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 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
[LENGTH(tags
) > 31 ? -1 : 1]; };
276 /* function implementations */
278 applyrules(Client
*c
) {
279 const char *class, *instance
;
283 XClassHint ch
= { 0 };
286 c
->isfloating
= c
->tags
= 0;
287 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
288 class = ch
.res_class
? ch
.res_class
: broken
;
289 instance
= ch
.res_name
? ch
.res_name
: broken
;
290 for(i
= 0; i
< LENGTH(rules
); i
++) {
292 if((!r
->title
|| strstr(c
->name
, r
->title
))
293 && (!r
->class || strstr(class, r
->class))
294 && (!r
->instance
|| strstr(instance
, r
->instance
)))
296 c
->isfloating
= r
->isfloating
;
298 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
308 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
312 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
316 /* set minimum possible */
324 if(*x
+ *w
+ 2 * c
->bw
< 0)
326 if(*y
+ *h
+ 2 * c
->bw
< 0)
330 if(*x
> m
->mx
+ m
->mw
)
331 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
332 if(*y
> m
->my
+ m
->mh
)
333 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
334 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
336 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
343 if(resizehints
|| c
->isfloating
) {
344 /* see last two sentences in ICCCM 4.1.2.3 */
345 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
346 if(!baseismin
) { /* temporarily remove base dimensions */
350 /* adjust for aspect limits */
351 if(c
->mina
> 0 && c
->maxa
> 0) {
352 if(c
->maxa
< (float)*w
/ *h
)
354 else if(c
->mina
< (float)*h
/ *w
)
357 if(baseismin
) { /* increment calculation requires this */
361 /* adjust for increment value */
366 /* restore base dimensions */
369 *w
= MAX(*w
, c
->minw
);
370 *h
= MAX(*h
, c
->minh
);
372 *w
= MIN(*w
, c
->maxw
);
374 *h
= MIN(*h
, c
->maxh
);
376 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
383 for(m
= mons
; m
; m
= m
->next
)
386 for(m
= mons
; m
; m
= m
->next
) {
387 if(m
->lt
[m
->sellt
]->arrange
)
388 m
->lt
[m
->sellt
]->arrange(m
);
395 c
->next
= c
->mon
->clients
;
400 attachstack(Client
*c
) {
401 c
->snext
= c
->mon
->stack
;
406 buttonpress(XEvent
*e
) {
407 unsigned int i
, x
, click
;
411 XButtonPressedEvent
*ev
= &e
->xbutton
;
414 /* focus monitor if necessary */
415 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
416 unfocus(selmon
->sel
);
420 if(ev
->window
== selmon
->barwin
) {
424 } while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
425 if(i
< LENGTH(tags
)) {
429 else if(ev
->x
< x
+ blw
)
431 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
432 click
= ClkStatusText
;
436 else if((c
= wintoclient(ev
->window
))) {
438 click
= ClkClientWin
;
440 for(i
= 0; i
< LENGTH(buttons
); i
++)
441 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
442 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
443 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
449 xerrorxlib
= XSetErrorHandler(xerrorstart
);
450 /* this causes an error if some other window manager is running */
451 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
454 die("dwm: another window manager is already running\n");
455 XSetErrorHandler(xerror
);
462 Layout foo
= { "", NULL
};
466 selmon
->lt
[selmon
->sellt
] = &foo
;
467 for(m
= mons
; m
; m
= m
->next
)
471 XFreeFontSet(dpy
, dc
.font
.set
);
473 XFreeFont(dpy
, dc
.font
.xfont
);
474 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
475 XFreePixmap(dpy
, dc
.drawable
);
477 XFreeCursor(dpy
, cursor
[CurNormal
]);
478 XFreeCursor(dpy
, cursor
[CurResize
]);
479 XFreeCursor(dpy
, cursor
[CurMove
]);
482 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
491 XUnmapWindow(dpy
, mons
->barwin
);
492 XDestroyWindow(dpy
, mons
->barwin
);
499 clearurgent(Client
*c
) {
503 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
505 wmh
->flags
&= ~XUrgencyHint
;
506 XSetWMHints(dpy
, c
->win
, wmh
);
511 configure(Client
*c
) {
514 ce
.type
= ConfigureNotify
;
522 ce
.border_width
= c
->bw
;
524 ce
.override_redirect
= False
;
525 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
529 configurenotify(XEvent
*e
) {
531 XConfigureEvent
*ev
= &e
->xconfigure
;
533 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
538 XFreePixmap(dpy
, dc
.drawable
);
539 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
541 for(m
= mons
; m
; m
= m
->next
)
542 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
548 configurerequest(XEvent
*e
) {
551 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
554 if((c
= wintoclient(ev
->window
))) {
555 if(ev
->value_mask
& CWBorderWidth
)
556 c
->bw
= ev
->border_width
;
557 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
559 if(ev
->value_mask
& CWX
)
560 c
->x
= m
->mx
+ ev
->x
;
561 if(ev
->value_mask
& CWY
)
562 c
->y
= m
->my
+ ev
->y
;
563 if(ev
->value_mask
& CWWidth
)
565 if(ev
->value_mask
& CWHeight
)
567 if((c
->x
- m
->mx
+ c
->w
) > m
->mw
&& c
->isfloating
)
568 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
569 if((c
->y
- m
->my
+ c
->h
) > m
->mh
&& c
->isfloating
)
570 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
571 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
574 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
582 wc
.width
= ev
->width
;
583 wc
.height
= ev
->height
;
584 wc
.border_width
= ev
->border_width
;
585 wc
.sibling
= ev
->above
;
586 wc
.stack_mode
= ev
->detail
;
587 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
593 destroynotify(XEvent
*e
) {
595 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
597 if((c
= wintoclient(ev
->window
)))
605 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
610 detachstack(Client
*c
) {
613 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
616 if(c
== c
->mon
->sel
) {
617 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
623 die(const char *errstr
, ...) {
626 va_start(ap
, errstr
);
627 vfprintf(stderr
, errstr
, ap
);
637 if(!(m
= selmon
->next
))
642 for(m
= mons
; m
->next
; m
= m
->next
);
644 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
650 drawbar(Monitor
*m
) {
652 unsigned int i
, n
= 0, occ
= 0, urg
= 0;
656 for(c
= m
->clients
; c
; c
= c
->next
) {
664 for(i
= 0; i
< LENGTH(tags
); i
++) {
665 dc
.w
= TEXTW(tags
[i
]);
666 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
667 drawtext(tags
[i
], col
, urg
& 1 << i
);
668 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
669 occ
& 1 << i
, urg
& 1 << i
, col
);
674 drawtext(m
->lt
[m
->sellt
]->symbol
, dc
.norm
, False
);
677 snprintf(ntext
, sizeof ntext
, "%u", n
);
679 drawtext(ntext
, dc
.norm
, False
);
681 if(m
== selmon
) { /* status is only drawn on selected monitor */
688 drawtext(stext
, dc
.norm
, False
);
692 if((dc
.w
= dc
.x
- x
) > bh
) {
695 col
= m
== selmon
? dc
.sel
: dc
.norm
;
696 drawtext(m
->sel
->name
, col
, False
);
697 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
700 drawtext(NULL
, dc
.norm
, False
);
702 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
710 for(m
= mons
; m
; m
= m
->next
)
715 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
718 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
720 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
721 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
722 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
726 r
.width
= r
.height
= x
+ 1;
727 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
730 r
.width
= r
.height
= x
;
731 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
736 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
738 int i
, x
, y
, h
, len
, olen
;
739 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
741 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
742 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
746 h
= dc
.font
.ascent
+ dc
.font
.descent
;
747 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
749 /* shorten text if necessary */
750 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
753 memcpy(buf
, text
, len
);
755 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
756 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
758 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
760 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
764 enternotify(XEvent
*e
) {
767 XCrossingEvent
*ev
= &e
->xcrossing
;
769 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
771 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
772 unfocus(selmon
->sel
);
775 if((c
= wintoclient(ev
->window
)))
784 XExposeEvent
*ev
= &e
->xexpose
;
786 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
792 if(!c
|| !ISVISIBLE(c
))
793 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
795 unfocus(selmon
->sel
);
803 grabbuttons(c
, True
);
804 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
805 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
808 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
814 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
815 XFocusChangeEvent
*ev
= &e
->xfocus
;
817 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
818 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
822 focusmon(const Arg
*arg
) {
827 m
= dirtomon(arg
->i
);
828 unfocus(selmon
->sel
);
834 focusstack(const Arg
*arg
) {
835 Client
*c
= NULL
, *i
;
840 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
842 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
845 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
849 for(; i
; i
= i
->next
)
860 getcolor(const char *colstr
) {
861 Colormap cmap
= DefaultColormap(dpy
, screen
);
864 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
865 die("error, cannot allocate color '%s'\n", colstr
);
870 getrootptr(int *x
, int *y
) {
875 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
882 unsigned char *p
= NULL
;
883 unsigned long n
, extra
;
886 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
887 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
888 if(status
!= Success
)
897 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
902 if(!text
|| size
== 0)
905 XGetTextProperty(dpy
, w
, &name
, atom
);
908 if(name
.encoding
== XA_STRING
)
909 strncpy(text
, (char *)name
.value
, size
- 1);
911 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
912 strncpy(text
, *list
, size
- 1);
913 XFreeStringList(list
);
916 text
[size
- 1] = '\0';
922 grabbuttons(Client
*c
, Bool focused
) {
926 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
927 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
929 for(i
= 0; i
< LENGTH(buttons
); i
++)
930 if(buttons
[i
].click
== ClkClientWin
)
931 for(j
= 0; j
< LENGTH(modifiers
); j
++)
932 XGrabButton(dpy
, buttons
[i
].button
,
933 buttons
[i
].mask
| modifiers
[j
],
934 c
->win
, False
, BUTTONMASK
,
935 GrabModeAsync
, GrabModeSync
, None
, None
);
938 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
939 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
948 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
951 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
952 for(i
= 0; i
< LENGTH(keys
); i
++) {
953 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
954 for(j
= 0; j
< LENGTH(modifiers
); j
++)
955 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
956 True
, GrabModeAsync
, GrabModeAsync
);
962 initfont(const char *fontstr
) {
963 char *def
, **missing
;
967 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
970 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
971 XFreeStringList(missing
);
974 XFontSetExtents
*font_extents
;
975 XFontStruct
**xfonts
;
978 dc
.font
.ascent
= dc
.font
.descent
= 0;
979 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
980 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
981 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
982 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
983 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
988 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
989 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
990 die("error, cannot load font: '%s'\n", fontstr
);
991 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
992 dc
.font
.descent
= dc
.font
.xfont
->descent
;
994 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
998 isprotodel(Client
*c
) {
1003 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1004 for(i
= 0; !ret
&& i
< n
; i
++)
1005 if(protocols
[i
] == wmatom
[WMDelete
])
1013 keypress(XEvent
*e
) {
1019 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1020 for(i
= 0; i
< LENGTH(keys
); i
++)
1021 if(keysym
== keys
[i
].keysym
1022 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1024 keys
[i
].func(&(keys
[i
].arg
));
1028 killclient(const Arg
*arg
) {
1033 if(isprotodel(selmon
->sel
)) {
1034 ev
.type
= ClientMessage
;
1035 ev
.xclient
.window
= selmon
->sel
->win
;
1036 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1037 ev
.xclient
.format
= 32;
1038 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1039 ev
.xclient
.data
.l
[1] = CurrentTime
;
1040 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1043 XKillClient(dpy
, selmon
->sel
->win
);
1047 manage(Window w
, XWindowAttributes
*wa
) {
1049 Client
*c
, *t
= NULL
;
1050 Window trans
= None
;
1053 if(!(c
= malloc(sizeof(Client
))))
1054 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1058 if(XGetTransientForHint(dpy
, w
, &trans
))
1059 t
= wintoclient(trans
);
1069 c
->x
= wa
->x
+ c
->mon
->wx
;
1070 c
->y
= wa
->y
+ c
->mon
->wy
;
1073 c
->oldbw
= wa
->border_width
;
1074 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1080 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1081 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1082 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1083 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1084 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1085 /* only fix client y-offset, if the client center might cover the bar */
1086 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1087 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1090 wc
.border_width
= c
->bw
;
1091 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1092 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1093 configure(c
); /* propagates border_width, if size doesn't change */
1095 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1096 grabbuttons(c
, False
);
1098 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1100 XRaiseWindow(dpy
, c
->win
);
1103 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1104 XMapWindow(dpy
, c
->win
);
1105 setclientstate(c
, NormalState
);
1110 mappingnotify(XEvent
*e
) {
1111 XMappingEvent
*ev
= &e
->xmapping
;
1113 XRefreshKeyboardMapping(ev
);
1114 if(ev
->request
== MappingKeyboard
)
1119 maprequest(XEvent
*e
) {
1120 static XWindowAttributes wa
;
1121 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1123 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1125 if(wa
.override_redirect
)
1127 if(!wintoclient(ev
->window
))
1128 manage(ev
->window
, &wa
);
1132 monocle(Monitor
*m
) {
1135 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1136 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1140 movemouse(const Arg
*arg
) {
1141 int x
, y
, ocx
, ocy
, nx
, ny
;
1146 if(!(c
= selmon
->sel
))
1151 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1152 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1154 if(!getrootptr(&x
, &y
))
1157 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1159 case ConfigureRequest
:
1162 handler
[ev
.type
](&ev
);
1165 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1166 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1167 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1168 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1169 if(abs(selmon
->wx
- nx
) < snap
)
1171 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1172 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1173 if(abs(selmon
->wy
- ny
) < snap
)
1175 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1176 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1177 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1178 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1179 togglefloating(NULL
);
1181 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1182 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1185 } while(ev
.type
!= ButtonRelease
);
1186 XUngrabPointer(dpy
, CurrentTime
);
1187 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1195 nexttiled(Client
*c
) {
1196 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1201 ptrtomon(int x
, int y
) {
1204 for(m
= mons
; m
; m
= m
->next
)
1205 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1211 propertynotify(XEvent
*e
) {
1214 XPropertyEvent
*ev
= &e
->xproperty
;
1216 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1218 else if(ev
->state
== PropertyDelete
)
1219 return; /* ignore */
1220 else if((c
= wintoclient(ev
->window
))) {
1223 case XA_WM_TRANSIENT_FOR
:
1224 XGetTransientForHint(dpy
, c
->win
, &trans
);
1225 if(!c
->isfloating
&& (c
->isfloating
= (wintoclient(trans
) != NULL
)))
1228 case XA_WM_NORMAL_HINTS
:
1236 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1238 if(c
== selmon
->sel
)
1245 quit(const Arg
*arg
) {
1250 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1253 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
)) {
1256 c
->w
= wc
.width
= w
;
1257 c
->h
= wc
.height
= h
;
1258 wc
.border_width
= c
->bw
;
1259 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1266 resizemouse(const Arg
*arg
) {
1273 if(!(c
= selmon
->sel
))
1278 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1279 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1281 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1283 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1285 case ConfigureRequest
:
1288 handler
[ev
.type
](&ev
);
1291 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1292 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1293 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1294 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
)
1296 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1297 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1298 togglefloating(NULL
);
1300 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1301 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1304 } while(ev
.type
!= ButtonRelease
);
1305 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1306 XUngrabPointer(dpy
, CurrentTime
);
1307 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1308 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1316 restack(Monitor
*m
) {
1324 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1325 XRaiseWindow(dpy
, m
->sel
->win
);
1326 if(m
->lt
[m
->sellt
]->arrange
) {
1327 wc
.stack_mode
= Below
;
1328 wc
.sibling
= m
->barwin
;
1329 for(c
= m
->stack
; c
; c
= c
->snext
)
1330 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1331 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1332 wc
.sibling
= c
->win
;
1336 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1343 /* main event loop */
1345 while(running
&& !XNextEvent(dpy
, &ev
))
1346 if(handler
[ev
.type
])
1347 handler
[ev
.type
](&ev
); /* call handler */
1352 unsigned int i
, num
;
1353 Window d1
, d2
, *wins
= NULL
;
1354 XWindowAttributes wa
;
1356 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1357 for(i
= 0; i
< num
; i
++) {
1358 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1359 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1361 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1362 manage(wins
[i
], &wa
);
1364 for(i
= 0; i
< num
; i
++) { /* now the transients */
1365 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1367 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1368 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1369 manage(wins
[i
], &wa
);
1377 sendmon(Client
*c
, Monitor
*m
) {
1384 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1392 setclientstate(Client
*c
, long state
) {
1393 long data
[] = { state
, None
};
1395 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1396 PropModeReplace
, (unsigned char *)data
, 2);
1400 setlayout(const Arg
*arg
) {
1401 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1404 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1411 /* arg > 1.0 will set mfact absolutly */
1413 setmfact(const Arg
*arg
) {
1416 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1418 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1419 if(f
< 0.1 || f
> 0.9)
1429 XSetWindowAttributes wa
;
1432 screen
= DefaultScreen(dpy
);
1433 root
= RootWindow(dpy
, screen
);
1435 sw
= DisplayWidth(dpy
, screen
);
1436 sh
= DisplayHeight(dpy
, screen
);
1437 bh
= dc
.h
= dc
.font
.height
+ 2;
1440 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1441 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1442 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1443 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1444 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1446 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1447 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1448 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1449 /* init appearance */
1450 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1451 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1452 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1453 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1454 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1455 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1456 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1457 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1458 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1460 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1462 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1463 w
= TEXTW(layouts
[i
].symbol
);
1468 /* EWMH support per view */
1469 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1470 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1471 /* select for events */
1472 wa
.cursor
= cursor
[CurNormal
];
1473 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1474 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1475 |PropertyChangeMask
;
1476 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1477 XSelectInput(dpy
, root
, wa
.event_mask
);
1482 showhide(Client
*c
) {
1485 if(ISVISIBLE(c
)) { /* show clients top down */
1486 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1487 if(!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1488 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1491 else { /* hide clients bottom up */
1493 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1499 sigchld(int signal
) {
1500 while(0 < waitpid(-1, NULL
, WNOHANG
));
1504 spawn(const Arg
*arg
) {
1505 signal(SIGCHLD
, sigchld
);
1508 close(ConnectionNumber(dpy
));
1510 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1511 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1518 tag(const Arg
*arg
) {
1519 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1520 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1526 tagmon(const Arg
*arg
) {
1527 if(!selmon
->sel
|| !mons
->next
)
1529 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1533 textnw(const char *text
, unsigned int len
) {
1537 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1540 return XTextWidth(dc
.font
.xfont
, text
, len
);
1549 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1553 c
= nexttiled(m
->clients
);
1554 mw
= m
->mfact
* m
->ww
;
1555 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1559 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1561 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1565 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1566 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1567 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1569 y
= c
->y
+ HEIGHT(c
);
1574 togglebar(const Arg
*arg
) {
1575 selmon
->showbar
= !selmon
->showbar
;
1576 updatebarpos(selmon
);
1577 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1582 togglefloating(const Arg
*arg
) {
1585 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1586 if(selmon
->sel
->isfloating
)
1587 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1588 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1593 toggletag(const Arg
*arg
) {
1598 mask
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1600 selmon
->sel
->tags
= mask
;
1606 toggleview(const Arg
*arg
) {
1607 unsigned int mask
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1610 selmon
->tagset
[selmon
->seltags
] = mask
;
1616 unfocus(Client
*c
) {
1619 grabbuttons(c
, False
);
1620 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1621 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1625 unmanage(Client
*c
) {
1628 wc
.border_width
= c
->oldbw
;
1629 /* The server grab construct avoids race conditions. */
1631 XSetErrorHandler(xerrordummy
);
1632 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1635 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1636 setclientstate(c
, WithdrawnState
);
1639 XSetErrorHandler(xerror
);
1646 unmapnotify(XEvent
*e
) {
1648 XUnmapEvent
*ev
= &e
->xunmap
;
1650 if((c
= wintoclient(ev
->window
)))
1657 XSetWindowAttributes wa
;
1659 wa
.override_redirect
= True
;
1660 wa
.background_pixmap
= ParentRelative
;
1661 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1662 for(m
= mons
; m
; m
= m
->next
) {
1663 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1664 CopyFromParent
, DefaultVisual(dpy
, screen
),
1665 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1666 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1667 XMapRaised(dpy
, m
->barwin
);
1672 updatebarpos(Monitor
*m
) {
1677 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1678 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1688 Monitor
*newmons
= NULL
, *m
= NULL
, *tm
;
1691 XineramaScreenInfo
*info
= NULL
;
1693 if(XineramaIsActive(dpy
))
1694 info
= XineramaQueryScreens(dpy
, &n
);
1695 #endif /* XINERAMA */
1696 /* allocate monitor(s) for the new geometry setup */
1697 for(i
= 0; i
< n
; i
++) {
1698 if(!(m
= (Monitor
*)malloc(sizeof(Monitor
))))
1699 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
1703 /* initialise monitor(s) */
1705 if(XineramaIsActive(dpy
)) {
1706 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1707 m
->num
= info
[i
].screen_number
;
1708 m
->mx
= m
->wx
= info
[i
].x_org
;
1709 m
->my
= m
->wy
= info
[i
].y_org
;
1710 m
->mw
= m
->ww
= info
[i
].width
;
1711 m
->mh
= m
->wh
= info
[i
].height
;
1716 #endif /* XINERAMA */
1717 /* default monitor setup */
1725 /* bar geometry setup */
1726 for(m
= newmons
; m
; m
= m
->next
) {
1727 m
->sel
= m
->stack
= m
->clients
= NULL
;
1730 m
->tagset
[0] = m
->tagset
[1] = 1;
1732 m
->showbar
= showbar
;
1734 m
->lt
[0] = &layouts
[0];
1735 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1738 /* reassign left over clients of disappeared monitors */
1739 for(tm
= mons
; tm
; tm
= tm
->next
)
1740 while(tm
->clients
) {
1742 tm
->clients
= c
->next
;
1748 /* select focused monitor */
1750 selmon
= mons
= newmons
;
1751 selmon
= wintomon(root
);
1755 updatenumlockmask(void) {
1757 XModifierKeymap
*modmap
;
1760 modmap
= XGetModifierMapping(dpy
);
1761 for(i
= 0; i
< 8; i
++)
1762 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1763 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1764 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1765 numlockmask
= (1 << i
);
1766 XFreeModifiermap(modmap
);
1770 updatesizehints(Client
*c
) {
1774 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1775 /* size is uninitialized, ensure that size.flags aren't used */
1777 if(size
.flags
& PBaseSize
) {
1778 c
->basew
= size
.base_width
;
1779 c
->baseh
= size
.base_height
;
1781 else if(size
.flags
& PMinSize
) {
1782 c
->basew
= size
.min_width
;
1783 c
->baseh
= size
.min_height
;
1786 c
->basew
= c
->baseh
= 0;
1787 if(size
.flags
& PResizeInc
) {
1788 c
->incw
= size
.width_inc
;
1789 c
->inch
= size
.height_inc
;
1792 c
->incw
= c
->inch
= 0;
1793 if(size
.flags
& PMaxSize
) {
1794 c
->maxw
= size
.max_width
;
1795 c
->maxh
= size
.max_height
;
1798 c
->maxw
= c
->maxh
= 0;
1799 if(size
.flags
& PMinSize
) {
1800 c
->minw
= size
.min_width
;
1801 c
->minh
= size
.min_height
;
1803 else if(size
.flags
& PBaseSize
) {
1804 c
->minw
= size
.base_width
;
1805 c
->minh
= size
.base_height
;
1808 c
->minw
= c
->minh
= 0;
1809 if(size
.flags
& PAspect
) {
1810 c
->mina
= (float)size
.min_aspect
.y
/ (float)size
.min_aspect
.x
;
1811 c
->maxa
= (float)size
.max_aspect
.x
/ (float)size
.max_aspect
.y
;
1814 c
->maxa
= c
->mina
= 0.0;
1815 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1816 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1820 updatetitle(Client
*c
) {
1821 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1822 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1823 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1824 strcpy(c
->name
, broken
);
1828 updatestatus(void) {
1829 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1830 strcpy(stext
, "dwm-"VERSION
);
1835 updatewmhints(Client
*c
) {
1838 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1839 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1840 wmh
->flags
&= ~XUrgencyHint
;
1841 XSetWMHints(dpy
, c
->win
, wmh
);
1844 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1850 view(const Arg
*arg
) {
1851 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1853 selmon
->seltags
^= 1; /* toggle sel tagset */
1854 if(arg
->ui
& TAGMASK
)
1855 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1860 wintoclient(Window w
) {
1864 for(m
= mons
; m
; m
= m
->next
)
1865 for(c
= m
->clients
; c
; c
= c
->next
)
1872 wintomon(Window w
) {
1877 if(w
== root
&& getrootptr(&x
, &y
))
1878 return ptrtomon(x
, y
);
1879 for(m
= mons
; m
; m
= m
->next
)
1882 if((c
= wintoclient(w
)))
1887 /* There's no way to check accesses to destroyed windows, thus those cases are
1888 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1889 * default error handler, which may call exit. */
1891 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1892 if(ee
->error_code
== BadWindow
1893 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1894 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1895 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1896 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1897 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1898 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1899 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1900 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1902 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1903 ee
->request_code
, ee
->error_code
);
1904 return xerrorxlib(dpy
, ee
); /* may call exit */
1908 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1912 /* Startup Error handler to check if another window manager
1913 * is already running. */
1915 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1921 zoom(const Arg
*arg
) {
1922 Client
*c
= selmon
->sel
;
1924 if(!selmon
->lt
[selmon
->sellt
]->arrange
1925 || selmon
->lt
[selmon
->sellt
]->arrange
== monocle
1926 || (selmon
->sel
&& selmon
->sel
->isfloating
))
1928 if(c
== nexttiled(selmon
->clients
))
1929 if(!c
|| !(c
= nexttiled(c
->next
)))
1938 main(int argc
, char *argv
[]) {
1939 if(argc
== 2 && !strcmp("-v", argv
[1]))
1940 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1942 die("usage: dwm [-v]\n");
1943 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1944 fputs("warning: no locale support\n", stderr
);
1945 if(!(dpy
= XOpenDisplay(NULL
)))
1946 die("dwm: cannot open display\n");