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 global
15 * linked client list, the focus history is remembered through a global
16 * stack list. 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 void drawbar(Monitor
*m
);
168 static void drawbars(void);
169 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
170 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
171 static void enternotify(XEvent
*e
);
172 static void expose(XEvent
*e
);
173 static void focus(Client
*c
);
174 static void focusin(XEvent
*e
);
175 static void focusmon(const Arg
*arg
);
176 static void focusstack(const Arg
*arg
);
177 static unsigned long getcolor(const char *colstr
);
178 static Bool
getrootpointer(int *x
, int *y
);
179 static long getstate(Window w
);
180 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
181 static void grabbuttons(Client
*c
, Bool focused
);
182 static void grabkeys(void);
183 static Monitor
*idxtomon(unsigned int n
);
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
)
334 if(resizehints
|| c
->isfloating
) {
335 /* see last two sentences in ICCCM 4.1.2.3 */
336 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
338 if(!baseismin
) { /* temporarily remove base dimensions */
343 /* adjust for aspect limits */
344 if(c
->mina
> 0 && c
->maxa
> 0) {
345 if(c
->maxa
< (float)*w
/ *h
)
347 else if(c
->mina
< (float)*h
/ *w
)
351 if(baseismin
) { /* increment calculation requires this */
356 /* adjust for increment value */
362 /* restore base dimensions */
366 *w
= MAX(*w
, c
->minw
);
367 *h
= MAX(*h
, c
->minh
);
370 *w
= MIN(*w
, c
->maxw
);
373 *h
= MIN(*h
, c
->maxh
);
375 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
382 /* optimise two loops into one, check focus(NULL) */
383 for(m
= mons
; m
; m
= m
->next
)
386 for(m
= mons
; m
; m
= m
->next
) {
387 if(lt
[m
->sellt
]->arrange
)
388 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
&& ev
->x
>= selmon
->btx
) {
425 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
426 if(i
< LENGTH(tags
)) {
430 else if(ev
->x
< x
+ blw
)
432 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
433 click
= ClkStatusText
;
437 else if((c
= wintoclient(ev
->window
))) {
439 click
= ClkClientWin
;
442 for(i
= 0; i
< LENGTH(buttons
); i
++)
443 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
444 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
445 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
451 xerrorxlib
= XSetErrorHandler(xerrorstart
);
453 /* this causes an error if some other window manager is running */
454 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
457 die("dwm: another window manager is already running\n");
458 XSetErrorHandler(xerror
);
465 Layout foo
= { "", NULL
};
469 lt
[selmon
->sellt
] = &foo
;
470 for(m
= mons
; m
; m
= m
->next
)
474 XFreeFontSet(dpy
, dc
.font
.set
);
476 XFreeFont(dpy
, dc
.font
.xfont
);
477 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
478 XFreePixmap(dpy
, dc
.drawable
);
480 XFreeCursor(dpy
, cursor
[CurNormal
]);
481 XFreeCursor(dpy
, cursor
[CurResize
]);
482 XFreeCursor(dpy
, cursor
[CurMove
]);
485 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
494 XUnmapWindow(dpy
, mons
->barwin
);
495 XDestroyWindow(dpy
, mons
->barwin
);
502 clearurgent(Client
*c
) {
506 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
508 wmh
->flags
&= ~XUrgencyHint
;
509 XSetWMHints(dpy
, c
->win
, wmh
);
514 configure(Client
*c
) {
517 ce
.type
= ConfigureNotify
;
525 ce
.border_width
= c
->bw
;
527 ce
.override_redirect
= False
;
528 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
532 configurenotify(XEvent
*e
) {
534 XConfigureEvent
*ev
= &e
->xconfigure
;
536 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
541 XFreePixmap(dpy
, dc
.drawable
);
542 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
544 for(m
= mons
; m
; m
= m
->next
)
545 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
551 configurerequest(XEvent
*e
) {
554 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
557 if((c
= wintoclient(ev
->window
))) {
558 if(ev
->value_mask
& CWBorderWidth
)
559 c
->bw
= ev
->border_width
;
560 else if(c
->isfloating
|| !lt
[selmon
->sellt
]->arrange
) {
562 if(ev
->value_mask
& CWX
)
563 c
->x
= m
->mx
+ ev
->x
;
564 if(ev
->value_mask
& CWY
)
565 c
->y
= m
->my
+ ev
->y
;
566 if(ev
->value_mask
& CWWidth
)
568 if(ev
->value_mask
& CWHeight
)
570 if((c
->x
- m
->mx
+ c
->w
) > m
->mw
&& c
->isfloating
)
571 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
572 if((c
->y
- m
->my
+ c
->h
) > m
->mh
&& c
->isfloating
)
573 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
574 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
577 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
585 wc
.width
= ev
->width
;
586 wc
.height
= ev
->height
;
587 wc
.border_width
= ev
->border_width
;
588 wc
.sibling
= ev
->above
;
589 wc
.stack_mode
= ev
->detail
;
590 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
596 destroynotify(XEvent
*e
) {
598 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
600 if((c
= wintoclient(ev
->window
)))
608 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
613 detachstack(Client
*c
) {
616 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
619 if(c
== c
->mon
->sel
) {
620 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
626 die(const char *errstr
, ...) {
629 va_start(ap
, errstr
);
630 vfprintf(stderr
, errstr
, ap
);
636 drawbar(Monitor
*m
) {
638 unsigned int i
, occ
= 0, urg
= 0;
642 for(c
= m
->clients
; c
; c
= c
->next
) {
649 if(mons
->next
) { /* more than a single monitor */
650 dc
.w
= TEXTW(monsyms
[m
->screen_number
]);
651 drawtext(monsyms
[m
->screen_number
], selmon
== m
? dc
.sel
: dc
.norm
, False
);
655 for(i
= 0; i
< LENGTH(tags
); i
++) {
656 dc
.w
= TEXTW(tags
[i
]);
657 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
658 drawtext(tags
[i
], col
, urg
& 1 << i
);
659 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
660 occ
& 1 << i
, urg
& 1 << i
, col
);
665 drawtext(lt
[m
->sellt
]->symbol
, dc
.norm
, False
);
670 if(m
== selmon
) { /* status is only drawn on selected monitor */
677 drawtext(stext
, dc
.norm
, False
);
682 if((dc
.w
= dc
.x
- x
) > bh
) {
685 col
= m
== selmon
? dc
.sel
: dc
.norm
;
686 drawtext(m
->sel
->name
, col
, False
);
687 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
690 drawtext(NULL
, dc
.norm
, False
);
692 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
700 for(m
= mons
; m
; m
= m
->next
)
705 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
708 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
710 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
711 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
712 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
716 r
.width
= r
.height
= x
+ 1;
717 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
720 r
.width
= r
.height
= x
;
721 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
726 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
728 int i
, x
, y
, h
, len
, olen
;
729 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
731 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
732 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
736 h
= dc
.font
.ascent
+ dc
.font
.descent
;
737 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
739 /* shorten text if necessary */
740 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
743 memcpy(buf
, text
, len
);
745 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
746 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
748 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
750 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
754 enternotify(XEvent
*e
) {
757 XCrossingEvent
*ev
= &e
->xcrossing
;
759 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
761 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
762 unfocus(selmon
->sel
);
765 if((c
= wintoclient(ev
->window
)))
774 XExposeEvent
*ev
= &e
->xexpose
;
776 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
782 if(!c
|| !ISVISIBLE(c
))
783 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
785 unfocus(selmon
->sel
);
793 grabbuttons(c
, True
);
794 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
795 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
798 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
804 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
805 XFocusChangeEvent
*ev
= &e
->xfocus
;
807 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
808 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
812 focusmon(const Arg
*arg
) {
815 if(!(m
= idxtomon(arg
->ui
)) || m
== selmon
)
817 unfocus(selmon
->sel
);
823 focusstack(const Arg
*arg
) {
824 Client
*c
= NULL
, *i
;
829 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
831 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
834 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
838 for(; i
; i
= i
->next
)
849 getcolor(const char *colstr
) {
850 Colormap cmap
= DefaultColormap(dpy
, screen
);
853 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
854 die("error, cannot allocate color '%s'\n", colstr
);
859 getrootpointer(int *x
, int *y
) {
863 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
870 unsigned char *p
= NULL
;
871 unsigned long n
, extra
;
874 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
875 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
876 if(status
!= Success
)
885 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
890 if(!text
|| size
== 0)
893 XGetTextProperty(dpy
, w
, &name
, atom
);
896 if(name
.encoding
== XA_STRING
)
897 strncpy(text
, (char *)name
.value
, size
- 1);
899 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
901 strncpy(text
, *list
, size
- 1);
902 XFreeStringList(list
);
905 text
[size
- 1] = '\0';
911 grabbuttons(Client
*c
, Bool focused
) {
915 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
916 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
918 for(i
= 0; i
< LENGTH(buttons
); i
++)
919 if(buttons
[i
].click
== ClkClientWin
)
920 for(j
= 0; j
< LENGTH(modifiers
); j
++)
921 XGrabButton(dpy
, buttons
[i
].button
,
922 buttons
[i
].mask
| modifiers
[j
],
923 c
->win
, False
, BUTTONMASK
,
924 GrabModeAsync
, GrabModeSync
, None
, None
);
926 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
927 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
936 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
939 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
940 for(i
= 0; i
< LENGTH(keys
); i
++) {
941 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
942 for(j
= 0; j
< LENGTH(modifiers
); j
++)
943 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
944 True
, GrabModeAsync
, GrabModeAsync
);
950 idxtomon(unsigned int n
) {
954 for(m
= mons
, i
= 0; m
&& i
!= n
; m
= m
->next
, i
++);
959 initfont(const char *fontstr
) {
960 char *def
, **missing
;
964 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
967 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
968 XFreeStringList(missing
);
971 XFontSetExtents
*font_extents
;
972 XFontStruct
**xfonts
;
974 dc
.font
.ascent
= dc
.font
.descent
= 0;
975 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
976 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
977 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
978 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
979 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
984 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
985 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
986 die("error, cannot load font: '%s'\n", fontstr
);
987 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
988 dc
.font
.descent
= dc
.font
.xfont
->descent
;
990 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
994 isprotodel(Client
*c
) {
999 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1000 for(i
= 0; !ret
&& i
< n
; i
++)
1001 if(protocols
[i
] == wmatom
[WMDelete
])
1009 keypress(XEvent
*e
) {
1015 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1016 for(i
= 0; i
< LENGTH(keys
); i
++)
1017 if(keysym
== keys
[i
].keysym
1018 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1020 keys
[i
].func(&(keys
[i
].arg
));
1024 killclient(const Arg
*arg
) {
1029 if(isprotodel(selmon
->sel
)) {
1030 ev
.type
= ClientMessage
;
1031 ev
.xclient
.window
= selmon
->sel
->win
;
1032 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1033 ev
.xclient
.format
= 32;
1034 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1035 ev
.xclient
.data
.l
[1] = CurrentTime
;
1036 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1039 XKillClient(dpy
, selmon
->sel
->win
);
1043 manage(Window w
, XWindowAttributes
*wa
) {
1045 Client
*c
, *t
= NULL
;
1046 Window trans
= None
;
1049 if(!(c
= malloc(sizeof(Client
))))
1050 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1054 if(XGetTransientForHint(dpy
, w
, &trans
))
1055 t
= wintoclient(trans
);
1066 c
->x
= wa
->x
+ c
->mon
->wx
;
1067 c
->y
= wa
->y
+ c
->mon
->wy
;
1070 c
->oldbw
= wa
->border_width
;
1071 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1077 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1078 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1079 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1080 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1081 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1082 /* only fix client y-offset, if the client center might cover the bar */
1083 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1084 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1088 wc
.border_width
= c
->bw
;
1089 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1090 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1091 configure(c
); /* propagates border_width, if size doesn't change */
1093 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1094 grabbuttons(c
, False
);
1097 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1099 XRaiseWindow(dpy
, c
->win
);
1102 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1103 XMapWindow(dpy
, c
->win
);
1104 setclientstate(c
, NormalState
);
1109 mappingnotify(XEvent
*e
) {
1110 XMappingEvent
*ev
= &e
->xmapping
;
1112 XRefreshKeyboardMapping(ev
);
1113 if(ev
->request
== MappingKeyboard
)
1118 maprequest(XEvent
*e
) {
1119 static XWindowAttributes wa
;
1120 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1122 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1124 if(wa
.override_redirect
)
1126 if(!wintoclient(ev
->window
))
1127 manage(ev
->window
, &wa
);
1131 monocle(Monitor
*m
) {
1134 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1135 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1139 movemouse(const Arg
*arg
) {
1140 int x
, y
, ocx
, ocy
, nx
, ny
;
1145 if(!(c
= selmon
->sel
))
1150 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1151 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1153 if(!getrootpointer(&x
, &y
))
1156 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1158 case ConfigureRequest
:
1161 handler
[ev
.type
](&ev
);
1164 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1165 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1166 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1167 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1168 if(abs(selmon
->wx
- nx
) < snap
)
1170 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1171 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1172 if(abs(selmon
->wy
- ny
) < snap
)
1174 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1175 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1176 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1177 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1178 togglefloating(NULL
);
1180 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1181 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1185 while(ev
.type
!= ButtonRelease
);
1186 XUngrabPointer(dpy
, CurrentTime
);
1187 if((m
= pointertomon(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 pointertomon(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);
1294 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1295 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
) {
1296 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1297 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1298 togglefloating(NULL
);
1300 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1301 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1305 while(ev
.type
!= ButtonRelease
);
1306 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1307 XUngrabPointer(dpy
, CurrentTime
);
1308 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1309 if((m
= pointertomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1317 restack(Monitor
*m
) {
1325 if(m
->sel
->isfloating
|| !lt
[m
->sellt
]->arrange
)
1326 XRaiseWindow(dpy
, m
->sel
->win
);
1327 if(lt
[m
->sellt
]->arrange
) {
1328 wc
.stack_mode
= Below
;
1329 wc
.sibling
= m
->barwin
;
1330 for(c
= m
->stack
; c
; c
= c
->snext
)
1331 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1332 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1333 wc
.sibling
= c
->win
;
1337 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1344 /* main event loop */
1346 while(running
&& !XNextEvent(dpy
, &ev
)) {
1347 if(handler
[ev
.type
])
1348 (handler
[ev
.type
])(&ev
); /* call handler */
1354 unsigned int i
, num
;
1355 Window d1
, d2
, *wins
= NULL
;
1356 XWindowAttributes wa
;
1358 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1359 for(i
= 0; i
< num
; i
++) {
1360 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1361 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1363 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1364 manage(wins
[i
], &wa
);
1366 for(i
= 0; i
< num
; i
++) { /* now the transients */
1367 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1369 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1370 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1371 manage(wins
[i
], &wa
);
1379 sendmon(Client
*c
, Monitor
*m
) {
1386 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1394 setclientstate(Client
*c
, long state
) {
1395 long data
[] = {state
, None
};
1397 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1398 PropModeReplace
, (unsigned char *)data
, 2);
1402 setlayout(const Arg
*arg
) {
1403 if(!arg
|| !arg
->v
|| arg
->v
!= lt
[selmon
->sellt
])
1406 lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1413 /* arg > 1.0 will set mfact absolutly */
1415 setmfact(const Arg
*arg
) {
1418 if(!arg
|| !lt
[selmon
->sellt
]->arrange
)
1420 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1421 if(f
< 0.1 || f
> 0.9)
1431 XSetWindowAttributes wa
;
1434 screen
= DefaultScreen(dpy
);
1435 root
= RootWindow(dpy
, screen
);
1437 sw
= DisplayWidth(dpy
, screen
);
1438 sh
= DisplayHeight(dpy
, screen
);
1439 bh
= dc
.h
= dc
.font
.height
+ 2;
1440 lt
[0] = &layouts
[0];
1441 lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1445 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1446 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1447 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1448 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1449 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1452 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1453 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1454 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1456 /* init appearance */
1457 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1458 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1459 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1460 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1461 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1462 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1463 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1464 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1465 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1467 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1470 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1471 w
= TEXTW(layouts
[i
].symbol
);
1477 /* EWMH support per view */
1478 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1479 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1481 /* select for events */
1482 wa
.cursor
= cursor
[CurNormal
];
1483 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1484 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1485 |PropertyChangeMask
;
1486 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1487 XSelectInput(dpy
, root
, wa
.event_mask
);
1493 showhide(Client
*c
) {
1496 if(ISVISIBLE(c
)) { /* show clients top down */
1497 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1498 if(!lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1499 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1502 else { /* hide clients bottom up */
1504 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1510 sigchld(int signal
) {
1511 while(0 < waitpid(-1, NULL
, WNOHANG
));
1515 spawn(const Arg
*arg
) {
1516 signal(SIGCHLD
, sigchld
);
1519 close(ConnectionNumber(dpy
));
1521 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1522 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1529 tag(const Arg
*arg
) {
1530 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1531 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1537 tagmon(const Arg
*arg
) {
1540 if(!selmon
->sel
|| !(m
= idxtomon(arg
->ui
)))
1542 sendmon(selmon
->sel
, m
);
1546 textnw(const char *text
, unsigned int len
) {
1550 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1553 return XTextWidth(dc
.font
.xfont
, text
, len
);
1562 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1567 c
= nexttiled(m
->clients
);
1568 mw
= m
->mfact
* m
->ww
;
1569 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1575 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1577 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1582 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1583 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1584 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1586 y
= c
->y
+ HEIGHT(c
);
1591 togglebar(const Arg
*arg
) {
1592 selmon
->showbar
= !selmon
->showbar
;
1593 updatebarpos(selmon
);
1594 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1599 togglefloating(const Arg
*arg
) {
1602 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1603 if(selmon
->sel
->isfloating
)
1604 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1605 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1610 toggletag(const Arg
*arg
) {
1616 mask
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1618 selmon
->sel
->tags
= mask
;
1624 toggleview(const Arg
*arg
) {
1625 unsigned int mask
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1628 selmon
->tagset
[selmon
->seltags
] = mask
;
1634 unfocus(Client
*c
) {
1637 grabbuttons(c
, False
);
1638 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1639 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1643 unmanage(Client
*c
) {
1646 wc
.border_width
= c
->oldbw
;
1647 /* The server grab construct avoids race conditions. */
1649 XSetErrorHandler(xerrordummy
);
1650 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1653 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1654 setclientstate(c
, WithdrawnState
);
1657 XSetErrorHandler(xerror
);
1664 unmapnotify(XEvent
*e
) {
1666 XUnmapEvent
*ev
= &e
->xunmap
;
1668 if((c
= wintoclient(ev
->window
)))
1675 XSetWindowAttributes wa
;
1677 wa
.override_redirect
= True
;
1678 wa
.background_pixmap
= ParentRelative
;
1679 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1681 for(m
= mons
; m
; m
= m
->next
) {
1682 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1684 CopyFromParent
, DefaultVisual(dpy
, screen
),
1685 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1686 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1687 XMapRaised(dpy
, m
->barwin
);
1692 updatebarpos(Monitor
*m
) {
1697 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1698 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1708 Monitor
*newmons
= NULL
, *m
, *tm
;
1711 XineramaScreenInfo
*info
= NULL
;
1713 if(XineramaIsActive(dpy
))
1714 info
= XineramaQueryScreens(dpy
, &n
);
1715 #endif /* XINERAMA */
1716 /* allocate monitor(s) for the new geometry setup */
1717 for(i
= 0; i
< n
; i
++) {
1718 m
= (Monitor
*)malloc(sizeof(Monitor
));
1723 /* initialise monitor(s) */
1725 if(XineramaIsActive(dpy
)) {
1726 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1727 m
->screen_number
= info
[i
].screen_number
;
1728 m
->mx
= m
->wx
= info
[i
].x_org
;
1729 m
->my
= m
->wy
= info
[i
].y_org
;
1730 m
->mw
= m
->ww
= info
[i
].width
;
1731 m
->mh
= m
->wh
= info
[i
].height
;
1736 #endif /* XINERAMA */
1737 /* default monitor setup */
1739 m
->screen_number
= 0;
1746 /* bar geometry setup */
1747 for(m
= newmons
; m
; m
= m
->next
) {
1748 m
->sel
= m
->stack
= m
->clients
= NULL
;
1751 m
->tagset
[0] = m
->tagset
[1] = 1;
1753 m
->showbar
= SHOWBAR
;
1758 /* reassign left over clients of disappeared monitors */
1759 for(tm
= mons
; tm
; tm
= tm
->next
)
1760 while(tm
->clients
) {
1762 tm
->clients
= c
->next
;
1769 /* select focused monitor */
1771 selmon
= mons
= newmons
;
1772 selmon
= wintomon(root
);
1776 updatenumlockmask(void) {
1778 XModifierKeymap
*modmap
;
1781 modmap
= XGetModifierMapping(dpy
);
1782 for(i
= 0; i
< 8; i
++)
1783 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1784 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1785 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1786 numlockmask
= (1 << i
);
1787 XFreeModifiermap(modmap
);
1791 updatesizehints(Client
*c
) {
1795 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1796 /* size is uninitialized, ensure that size.flags aren't used */
1798 if(size
.flags
& PBaseSize
) {
1799 c
->basew
= size
.base_width
;
1800 c
->baseh
= size
.base_height
;
1802 else if(size
.flags
& PMinSize
) {
1803 c
->basew
= size
.min_width
;
1804 c
->baseh
= size
.min_height
;
1807 c
->basew
= c
->baseh
= 0;
1808 if(size
.flags
& PResizeInc
) {
1809 c
->incw
= size
.width_inc
;
1810 c
->inch
= size
.height_inc
;
1813 c
->incw
= c
->inch
= 0;
1814 if(size
.flags
& PMaxSize
) {
1815 c
->maxw
= size
.max_width
;
1816 c
->maxh
= size
.max_height
;
1819 c
->maxw
= c
->maxh
= 0;
1820 if(size
.flags
& PMinSize
) {
1821 c
->minw
= size
.min_width
;
1822 c
->minh
= size
.min_height
;
1824 else if(size
.flags
& PBaseSize
) {
1825 c
->minw
= size
.base_width
;
1826 c
->minh
= size
.base_height
;
1829 c
->minw
= c
->minh
= 0;
1830 if(size
.flags
& PAspect
) {
1831 c
->mina
= (float)size
.min_aspect
.y
/ (float)size
.min_aspect
.x
;
1832 c
->maxa
= (float)size
.max_aspect
.x
/ (float)size
.max_aspect
.y
;
1835 c
->maxa
= c
->mina
= 0.0;
1836 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1837 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1841 updatetitle(Client
*c
) {
1842 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1843 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1847 updatestatus(void) {
1848 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1849 strcpy(stext
, "dwm-"VERSION
);
1854 updatewmhints(Client
*c
) {
1857 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1858 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1859 wmh
->flags
&= ~XUrgencyHint
;
1860 XSetWMHints(dpy
, c
->win
, wmh
);
1863 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1870 view(const Arg
*arg
) {
1871 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1873 selmon
->seltags
^= 1; /* toggle sel tagset */
1874 if(arg
->ui
& TAGMASK
)
1875 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1880 wintoclient(Window w
) {
1884 for(m
= mons
; m
; m
= m
->next
)
1885 for(c
= m
->clients
; c
; c
= c
->next
)
1892 wintomon(Window w
) {
1897 if(w
== root
&& getrootpointer(&x
, &y
))
1898 return pointertomon(x
, y
);
1899 for(m
= mons
; m
; m
= m
->next
)
1902 if((c
= wintoclient(w
)))
1907 /* There's no way to check accesses to destroyed windows, thus those cases are
1908 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1909 * default error handler, which may call exit. */
1911 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1912 if(ee
->error_code
== BadWindow
1913 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1914 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1915 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1916 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1917 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1918 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1919 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1920 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1922 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1923 ee
->request_code
, ee
->error_code
);
1924 return xerrorxlib(dpy
, ee
); /* may call exit */
1928 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1932 /* Startup Error handler to check if another window manager
1933 * is already running. */
1935 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1941 zoom(const Arg
*arg
) {
1942 Client
*c
= selmon
->sel
;
1944 if(!lt
[selmon
->sellt
]->arrange
|| lt
[selmon
->sellt
]->arrange
== monocle
|| (selmon
->sel
&& selmon
->sel
->isfloating
))
1946 if(c
== nexttiled(selmon
->clients
))
1947 if(!c
|| !(c
= nexttiled(c
->next
)))
1956 main(int argc
, char *argv
[]) {
1957 if(argc
== 2 && !strcmp("-v", argv
[1]))
1958 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1960 die("usage: dwm [-v]\n");
1962 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1963 fputs("warning: no locale support\n", stderr
);
1965 if(!(dpy
= XOpenDisplay(NULL
)))
1966 die("dwm: cannot open display\n");