Xinqi Bao's Git
9779654881e815bc7493d9f70b79f3755e22d18e
1 //#define XINULATOR /* debug, simulates dual head */
2 /* See LICENSE file for copyright and license details.
4 * dynamic window manager is designed like any other X client as well. It is
5 * driven through handling X events. In contrast to other X clients, a window
6 * manager selects for SubstructureRedirectMask on the root window, to receive
7 * events about window (dis-)appearance. Only one X connection at a time is
8 * allowed to select for this event mask.
10 * The event handlers of dwm are organized in an array which is accessed
11 * whenever a new event has been fetched. This allows event dispatching
14 * Each child of the root window is called a client, except windows which have
15 * set the override_redirect flag. Clients are organized in a global
16 * linked client list, the focus history is remembered through a global
17 * stack list. Each client contains a bit array to indicate the tags of a
20 * Keys and tagging rules are organized as arrays and defined in config.h.
22 * To understand everything else, start reading main().
32 #include <sys/types.h>
34 #include <X11/cursorfont.h>
35 #include <X11/keysym.h>
36 #include <X11/Xatom.h>
38 #include <X11/Xproto.h>
39 #include <X11/Xutil.h>
41 #include <X11/extensions/Xinerama.h>
45 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
46 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
47 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
48 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
49 #define LENGTH(X) (sizeof X / sizeof X[0])
50 #define MAX(A, B) ((A) > (B) ? (A) : (B))
51 #define MIN(A, B) ((A) < (B) ? (A) : (B))
52 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
53 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
54 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
55 #define TAGMASK ((int)((1LL << LENGTH(tags)) - 1))
56 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
59 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
60 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
61 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
62 enum { WMProtocols
, WMDelete
, WMState
, WMLast
}; /* default atoms */
63 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
64 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
77 void (*func
)(const Arg
*arg
);
81 typedef struct Monitor Monitor
;
82 typedef struct Client Client
;
87 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
90 Bool isfixed
, isfloating
, isurgent
;
99 unsigned long norm
[ColLast
];
100 unsigned long sel
[ColLast
];
110 } DC
; /* draw context */
115 void (*func
)(const Arg
*);
121 void (*arrange
)(Monitor
*);
127 int by
, btx
; /* bar geometry */
128 int my
, mh
; /* vertical screen size*/
129 int wx
, wy
, ww
, wh
; /* window area */
130 unsigned int seltags
;
132 unsigned int tagset
[2];
144 const char *instance
;
150 /* function declarations */
151 static void applyrules(Client
*c
);
152 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
);
153 static void arrange(void);
154 static void attach(Client
*c
);
155 static void attachstack(Client
*c
);
156 static void buttonpress(XEvent
*e
);
157 static void checkotherwm(void);
158 static void cleanup(void);
159 static void cleanupmons(void);
160 static void clearurgent(Client
*c
);
161 static void configure(Client
*c
);
162 static void configurenotify(XEvent
*e
);
163 static void configurerequest(XEvent
*e
);
164 static void destroynotify(XEvent
*e
);
165 static void detach(Client
*c
);
166 static void detachstack(Client
*c
);
167 static void die(const char *errstr
, ...);
168 static void drawbar(Monitor
*m
);
169 static void drawbars(void);
170 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
171 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
172 static void enternotify(XEvent
*e
);
173 static void expose(XEvent
*e
);
174 static void focus(Client
*c
);
175 static void focusin(XEvent
*e
);
176 static void focusstack(const Arg
*arg
);
177 static Client
*getclient(Window w
);
178 static unsigned long getcolor(const char *colstr
);
179 static Monitor
*getmonitor(Window w
);
180 static Monitor
*getmonitorxy(int x
, int y
);
181 static Bool
getrootpointer(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 void propertynotify(XEvent
*e
);
197 static void quit(const Arg
*arg
);
198 static void resize(Client
*c
, int x
, int y
, int w
, int h
);
199 static void resizemouse(const Arg
*arg
);
200 static void restack(Monitor
*m
);
201 static void run(void);
202 static void scan(void);
203 static void sendmon(Client
*c
, Monitor
*m
);
204 static void setclientstate(Client
*c
, long state
);
205 static void setlayout(const Arg
*arg
);
206 static void setmfact(const Arg
*arg
);
207 static void setup(void);
208 static void showhide(Client
*c
);
209 static void sigchld(int signal
);
210 static void spawn(const Arg
*arg
);
211 static void tag(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 int xerror(Display
*dpy
, XErrorEvent
*ee
);
231 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
232 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
233 static void zoom(const Arg
*arg
);
235 static void focusmon(const Arg
*arg
);
236 static void tagmon(const Arg
*arg
);
237 #endif /* XINERAMA */
240 static char stext
[256];
242 static int sx
, sy
, sw
, sh
; /* X display screen geometry x, y, width, height */
243 static int bh
, blw
= 0; /* bar geometry */
244 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
245 static unsigned int numlockmask
= 0;
246 static void (*handler
[LASTEvent
]) (XEvent
*) = {
247 [ButtonPress
] = buttonpress
,
248 [ConfigureRequest
] = configurerequest
,
249 [ConfigureNotify
] = configurenotify
,
250 [DestroyNotify
] = destroynotify
,
251 [EnterNotify
] = enternotify
,
254 [KeyPress
] = keypress
,
255 [MappingNotify
] = mappingnotify
,
256 [MapRequest
] = maprequest
,
257 [PropertyNotify
] = propertynotify
,
258 [UnmapNotify
] = unmapnotify
260 static Atom wmatom
[WMLast
], netatom
[NetLast
];
262 static Bool running
= True
;
263 static Cursor cursor
[CurLast
];
266 static Layout
*lt
[] = { NULL
, NULL
};
267 static Monitor
*mons
= NULL
, *selmon
= NULL
;
269 /* configuration, allows nested code to access above variables */
272 /* compile-time check if all tags fit into an unsigned int bit array. */
273 struct NumTags
{ char limitexceeded
[sizeof(unsigned int) * 8 < LENGTH(tags
) ? -1 : 1]; };
275 /* function implementations */
277 applyrules(Client
*c
) {
280 XClassHint ch
= { 0 };
283 c
->isfloating
= c
->tags
= 0;
284 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
285 for(i
= 0; i
< LENGTH(rules
); i
++) {
287 if((!r
->title
|| strstr(c
->name
, r
->title
))
288 && (!r
->class || (ch
.res_class
&& strstr(ch
.res_class
, r
->class)))
289 && (!r
->instance
|| (ch
.res_name
&& strstr(ch
.res_name
, r
->instance
)))) {
290 c
->isfloating
= r
->isfloating
;
299 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
303 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
) {
306 /* set minimum possible */
314 if(*x
+ *w
+ 2 * c
->bw
< sx
)
316 if(*y
+ *h
+ 2 * c
->bw
< sy
)
323 if(resizehints
|| c
->isfloating
) {
324 /* see last two sentences in ICCCM 4.1.2.3 */
325 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
327 if(!baseismin
) { /* temporarily remove base dimensions */
332 /* adjust for aspect limits */
333 if(c
->mina
> 0 && c
->maxa
> 0) {
334 if(c
->maxa
< (float)*w
/ *h
)
336 else if(c
->mina
< (float)*h
/ *w
)
340 if(baseismin
) { /* increment calculation requires this */
345 /* adjust for increment value */
351 /* restore base dimensions */
355 *w
= MAX(*w
, c
->minw
);
356 *h
= MAX(*h
, c
->minh
);
359 *w
= MIN(*w
, c
->maxw
);
362 *h
= MIN(*h
, c
->maxh
);
364 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
371 /* optimise two loops into one, check focus(NULL) */
372 for(m
= mons
; m
; m
= m
->next
)
375 for(m
= mons
; m
; m
= m
->next
) {
376 if(lt
[m
->sellt
]->arrange
)
377 lt
[m
->sellt
]->arrange(m
);
384 c
->next
= c
->mon
->clients
;
389 attachstack(Client
*c
) {
390 c
->snext
= c
->mon
->stack
;
395 buttonpress(XEvent
*e
) {
396 unsigned int i
, x
, click
;
400 XButtonPressedEvent
*ev
= &e
->xbutton
;
403 /* focus monitor if necessary */
404 if((m
= getmonitor(ev
->window
)) && m
!= selmon
) {
405 unfocus(selmon
->sel
);
409 if(ev
->window
== selmon
->barwin
&& ev
->x
>= selmon
->btx
) {
414 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
415 if(i
< LENGTH(tags
)) {
419 else if(ev
->x
< x
+ blw
)
421 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
422 click
= ClkStatusText
;
426 else if((c
= getclient(ev
->window
))) {
428 click
= ClkClientWin
;
431 for(i
= 0; i
< LENGTH(buttons
); i
++)
432 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
433 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
434 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
440 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
;
460 /* TODO: consider simplifying cleanup code of the stack, perhaps do that in cleanmons() ? */
461 for(m
= mons
; m
; m
= m
->next
)
465 XFreeFontSet(dpy
, dc
.font
.set
);
467 XFreeFont(dpy
, dc
.font
.xfont
);
468 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
469 XFreePixmap(dpy
, dc
.drawable
);
471 XFreeCursor(dpy
, cursor
[CurNormal
]);
472 XFreeCursor(dpy
, cursor
[CurResize
]);
473 XFreeCursor(dpy
, cursor
[CurMove
]);
476 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
485 XUnmapWindow(dpy
, mons
->barwin
);
486 XDestroyWindow(dpy
, mons
->barwin
);
493 clearurgent(Client
*c
) {
497 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
499 wmh
->flags
&= ~XUrgencyHint
;
500 XSetWMHints(dpy
, c
->win
, wmh
);
505 configure(Client
*c
) {
508 ce
.type
= ConfigureNotify
;
516 ce
.border_width
= c
->bw
;
518 ce
.override_redirect
= False
;
519 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
523 configurenotify(XEvent
*e
) {
525 XConfigureEvent
*ev
= &e
->xconfigure
;
527 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
532 XFreePixmap(dpy
, dc
.drawable
);
533 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
535 for(m
= mons
; m
; m
= m
->next
)
536 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
542 configurerequest(XEvent
*e
) {
544 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
547 if((c
= getclient(ev
->window
))) {
548 if(ev
->value_mask
& CWBorderWidth
)
549 c
->bw
= ev
->border_width
;
550 else if(c
->isfloating
|| !lt
[selmon
->sellt
]->arrange
) {
551 if(ev
->value_mask
& CWX
)
553 if(ev
->value_mask
& CWY
)
555 if(ev
->value_mask
& CWWidth
)
557 if(ev
->value_mask
& CWHeight
)
559 if((c
->x
- sx
+ c
->w
) > sw
&& c
->isfloating
)
560 c
->x
= sx
+ (sw
/ 2 - c
->w
/ 2); /* center in x direction */
561 if((c
->y
- sy
+ c
->h
) > sh
&& c
->isfloating
)
562 c
->y
= sy
+ (sh
/ 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
= getclient(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
);
610 die(const char *errstr
, ...) {
613 va_start(ap
, errstr
);
614 vfprintf(stderr
, errstr
, ap
);
620 drawbar(Monitor
*m
) {
622 unsigned int i
, occ
= 0, urg
= 0;
626 for(c
= m
->clients
; c
; c
= c
->next
) {
636 buf
[0] = m
->screen_number
+ '0';
639 drawtext(buf
, selmon
== m
? dc
.sel
: dc
.norm
, True
);
642 #endif /* XINERAMA */
644 for(i
= 0; i
< LENGTH(tags
); i
++) {
645 dc
.w
= TEXTW(tags
[i
]);
646 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
647 drawtext(tags
[i
], col
, urg
& 1 << i
);
648 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
649 occ
& 1 << i
, urg
& 1 << i
, col
);
654 drawtext(lt
[m
->sellt
]->symbol
, dc
.norm
, False
);
659 if(m
== selmon
) { /* status is only drawn on selected monitor */
666 drawtext(stext
, dc
.norm
, False
);
671 if((dc
.w
= dc
.x
- x
) > bh
) {
674 col
= m
== selmon
? dc
.sel
: dc
.norm
;
675 drawtext(m
->sel
->name
, col
, False
);
676 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
679 drawtext(NULL
, dc
.norm
, False
);
681 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
689 for(m
= mons
; m
; m
= m
->next
)
694 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
697 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
699 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
700 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
701 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
705 r
.width
= r
.height
= x
+ 1;
706 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
709 r
.width
= r
.height
= x
;
710 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
715 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
717 int i
, x
, y
, h
, len
, olen
;
718 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
720 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
721 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
725 h
= dc
.font
.ascent
+ dc
.font
.descent
;
726 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
728 /* shorten text if necessary */
729 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
732 memcpy(buf
, text
, len
);
734 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
735 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
737 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
739 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
743 enternotify(XEvent
*e
) {
746 XCrossingEvent
*ev
= &e
->xcrossing
;
748 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
750 if((m
= getmonitor(ev
->window
)) && m
!= selmon
) {
751 unfocus(selmon
->sel
);
754 if((c
= getclient(ev
->window
)))
763 XExposeEvent
*ev
= &e
->xexpose
;
765 if(ev
->count
== 0 && (m
= getmonitor(ev
->window
)))
771 if(!c
|| !ISVISIBLE(c
))
772 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
774 unfocus(selmon
->sel
);
782 grabbuttons(c
, True
);
783 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
784 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
787 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
793 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
794 XFocusChangeEvent
*ev
= &e
->xfocus
;
796 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
797 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
802 focusmon(const Arg
*arg
) {
806 for(i
= 0, m
= mons
; m
; m
= m
->next
, i
++)
810 unfocus(selmon
->sel
);
816 #endif /* XINERAMA */
819 focusstack(const Arg
*arg
) {
820 Client
*c
= NULL
, *i
;
825 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
827 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
830 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
834 for(; i
; i
= i
->next
)
845 getclient(Window w
) {
849 for(m
= mons
; m
; m
= m
->next
)
850 for(c
= m
->clients
; c
; c
= c
->next
)
857 getcolor(const char *colstr
) {
858 Colormap cmap
= DefaultColormap(dpy
, screen
);
861 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
862 die("error, cannot allocate color '%s'\n", colstr
);
867 getmonitor(Window w
) {
872 if(w
== root
&& getrootpointer(&x
, &y
))
873 return getmonitorxy(x
, y
);
874 for(m
= mons
; m
; m
= m
->next
)
877 if((c
= getclient(w
)))
883 getmonitorxy(int x
, int y
) {
886 for(m
= mons
; m
; m
= m
->next
)
887 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
893 getrootpointer(int *x
, int *y
) {
897 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
904 unsigned char *p
= NULL
;
905 unsigned long n
, extra
;
908 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
909 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
910 if(status
!= Success
)
919 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
924 if(!text
|| size
== 0)
927 XGetTextProperty(dpy
, w
, &name
, atom
);
930 if(name
.encoding
== XA_STRING
)
931 strncpy(text
, (char *)name
.value
, size
- 1);
933 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
935 strncpy(text
, *list
, size
- 1);
936 XFreeStringList(list
);
939 text
[size
- 1] = '\0';
945 grabbuttons(Client
*c
, Bool focused
) {
949 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
950 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
952 for(i
= 0; i
< LENGTH(buttons
); i
++)
953 if(buttons
[i
].click
== ClkClientWin
)
954 for(j
= 0; j
< LENGTH(modifiers
); j
++)
955 XGrabButton(dpy
, buttons
[i
].button
,
956 buttons
[i
].mask
| modifiers
[j
],
957 c
->win
, False
, BUTTONMASK
,
958 GrabModeAsync
, GrabModeSync
, None
, None
);
960 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
961 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
970 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
973 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
974 for(i
= 0; i
< LENGTH(keys
); i
++) {
975 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
976 for(j
= 0; j
< LENGTH(modifiers
); j
++)
977 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
978 True
, GrabModeAsync
, GrabModeAsync
);
984 initfont(const char *fontstr
) {
985 char *def
, **missing
;
989 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
992 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
993 XFreeStringList(missing
);
996 XFontSetExtents
*font_extents
;
997 XFontStruct
**xfonts
;
999 dc
.font
.ascent
= dc
.font
.descent
= 0;
1000 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
1001 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
1002 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
1003 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
1004 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1009 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1010 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1011 die("error, cannot load font: '%s'\n", fontstr
);
1012 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1013 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1015 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1019 isprotodel(Client
*c
) {
1024 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1025 for(i
= 0; !ret
&& i
< n
; i
++)
1026 if(protocols
[i
] == wmatom
[WMDelete
])
1034 keypress(XEvent
*e
) {
1040 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1041 for(i
= 0; i
< LENGTH(keys
); i
++)
1042 if(keysym
== keys
[i
].keysym
1043 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1045 keys
[i
].func(&(keys
[i
].arg
));
1049 killclient(const Arg
*arg
) {
1054 if(isprotodel(selmon
->sel
)) {
1055 ev
.type
= ClientMessage
;
1056 ev
.xclient
.window
= selmon
->sel
->win
;
1057 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1058 ev
.xclient
.format
= 32;
1059 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1060 ev
.xclient
.data
.l
[1] = CurrentTime
;
1061 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1064 XKillClient(dpy
, selmon
->sel
->win
);
1068 manage(Window w
, XWindowAttributes
*wa
) {
1070 Client
*c
, *t
= NULL
;
1071 Window trans
= None
;
1074 if(!(c
= malloc(sizeof(Client
))))
1075 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1085 c
->oldbw
= wa
->border_width
;
1086 if(c
->w
== sw
&& c
->h
== sh
) {
1092 if(c
->x
+ WIDTH(c
) > sx
+ sw
)
1093 c
->x
= sx
+ sw
- WIDTH(c
);
1094 if(c
->y
+ HEIGHT(c
) > sy
+ sh
)
1095 c
->y
= sy
+ sh
- HEIGHT(c
);
1096 c
->x
= MAX(c
->x
, sx
);
1097 /* only fix client y-offset, if the client center might cover the bar */
1098 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1099 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: sy
);
1103 wc
.border_width
= c
->bw
;
1104 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1105 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1106 configure(c
); /* propagates border_width, if size doesn't change */
1108 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1109 grabbuttons(c
, False
);
1111 if(XGetTransientForHint(dpy
, w
, &trans
))
1112 t
= getclient(trans
);
1118 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1120 XRaiseWindow(dpy
, c
->win
);
1123 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1124 XMapWindow(dpy
, c
->win
);
1125 setclientstate(c
, NormalState
);
1130 mappingnotify(XEvent
*e
) {
1131 XMappingEvent
*ev
= &e
->xmapping
;
1133 XRefreshKeyboardMapping(ev
);
1134 if(ev
->request
== MappingKeyboard
)
1139 maprequest(XEvent
*e
) {
1140 static XWindowAttributes wa
;
1141 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1143 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1145 if(wa
.override_redirect
)
1147 if(!getclient(ev
->window
))
1148 manage(ev
->window
, &wa
);
1152 monocle(Monitor
*m
) {
1155 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1156 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1160 movemouse(const Arg
*arg
) {
1161 int x
, y
, ocx
, ocy
, nx
, ny
;
1166 if(!(c
= selmon
->sel
))
1171 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1172 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1174 if(!getrootpointer(&x
, &y
))
1177 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1179 case ConfigureRequest
:
1182 handler
[ev
.type
](&ev
);
1185 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1186 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1187 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1188 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1189 if(abs(selmon
->wx
- nx
) < snap
)
1191 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1192 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1193 if(abs(selmon
->wy
- ny
) < snap
)
1195 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1196 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1197 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1198 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1199 togglefloating(NULL
);
1201 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1202 resize(c
, nx
, ny
, c
->w
, c
->h
);
1206 while(ev
.type
!= ButtonRelease
);
1207 XUngrabPointer(dpy
, CurrentTime
);
1208 if((m
= getmonitorxy(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
)
1213 nexttiled(Client
*c
) {
1214 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1219 propertynotify(XEvent
*e
) {
1222 XPropertyEvent
*ev
= &e
->xproperty
;
1224 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1226 else if(ev
->state
== PropertyDelete
)
1227 return; /* ignore */
1228 else if((c
= getclient(ev
->window
))) {
1231 case XA_WM_TRANSIENT_FOR
:
1232 XGetTransientForHint(dpy
, c
->win
, &trans
);
1233 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1236 case XA_WM_NORMAL_HINTS
:
1244 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1246 if(c
== selmon
->sel
)
1253 quit(const Arg
*arg
) {
1258 resize(Client
*c
, int x
, int y
, int w
, int h
) {
1261 if(applysizehints(c
, &x
, &y
, &w
, &h
)) {
1264 c
->w
= wc
.width
= w
;
1265 c
->h
= wc
.height
= h
;
1266 wc
.border_width
= c
->bw
;
1267 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1274 resizemouse(const Arg
*arg
) {
1281 if(!(c
= selmon
->sel
))
1286 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1287 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1289 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1291 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1293 case ConfigureRequest
:
1296 handler
[ev
.type
](&ev
);
1299 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1300 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1302 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1303 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
) {
1304 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1305 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1306 togglefloating(NULL
);
1308 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1309 resize(c
, c
->x
, c
->y
, nw
, nh
);
1313 while(ev
.type
!= ButtonRelease
);
1314 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1315 XUngrabPointer(dpy
, CurrentTime
);
1316 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1317 if((m
= getmonitorxy(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
)
1322 restack(Monitor
*m
) {
1330 if(m
->sel
->isfloating
|| !lt
[m
->sellt
]->arrange
)
1331 XRaiseWindow(dpy
, m
->sel
->win
);
1332 if(lt
[m
->sellt
]->arrange
) {
1333 wc
.stack_mode
= Below
;
1334 wc
.sibling
= m
->barwin
;
1335 for(c
= m
->stack
; c
; c
= c
->snext
)
1336 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1337 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1338 wc
.sibling
= c
->win
;
1342 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1349 /* main event loop */
1351 while(running
&& !XNextEvent(dpy
, &ev
)) {
1352 if(handler
[ev
.type
])
1353 (handler
[ev
.type
])(&ev
); /* call handler */
1359 unsigned int i
, num
;
1360 Window d1
, d2
, *wins
= NULL
;
1361 XWindowAttributes wa
;
1363 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1364 for(i
= 0; i
< num
; i
++) {
1365 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1366 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1368 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1369 manage(wins
[i
], &wa
);
1371 for(i
= 0; i
< num
; i
++) { /* now the transients */
1372 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1374 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1375 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1376 manage(wins
[i
], &wa
);
1384 sendmon(Client
*c
, Monitor
*m
) {
1390 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1398 setclientstate(Client
*c
, long state
) {
1399 long data
[] = {state
, None
};
1401 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1402 PropModeReplace
, (unsigned char *)data
, 2);
1406 setlayout(const Arg
*arg
) {
1407 if(!arg
|| !arg
->v
|| arg
->v
!= lt
[selmon
->sellt
])
1410 lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1417 /* arg > 1.0 will set mfact absolutly */
1419 setmfact(const Arg
*arg
) {
1422 if(!arg
|| !lt
[selmon
->sellt
]->arrange
)
1424 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1425 if(f
< 0.1 || f
> 0.9)
1435 XSetWindowAttributes wa
;
1438 screen
= DefaultScreen(dpy
);
1439 root
= RootWindow(dpy
, screen
);
1443 sw
= DisplayWidth(dpy
, screen
);
1444 sh
= DisplayHeight(dpy
, screen
);
1445 bh
= dc
.h
= dc
.font
.height
+ 2;
1446 lt
[0] = &layouts
[0];
1447 lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1451 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1452 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1453 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1454 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1455 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1458 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1459 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1460 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1462 /* init appearance */
1463 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1464 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1465 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1466 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1467 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1468 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1469 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1470 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1471 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1473 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1476 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1477 w
= TEXTW(layouts
[i
].symbol
);
1483 /* EWMH support per view */
1484 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1485 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1487 /* select for events */
1488 wa
.cursor
= cursor
[CurNormal
];
1489 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1490 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1491 |PropertyChangeMask
;
1492 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1493 XSelectInput(dpy
, root
, wa
.event_mask
);
1499 showhide(Client
*c
) {
1502 if(ISVISIBLE(c
)) { /* show clients top down */
1503 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1504 if(!lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1505 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1508 else { /* hide clients bottom up */
1510 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1516 sigchld(int signal
) {
1517 while(0 < waitpid(-1, NULL
, WNOHANG
));
1521 spawn(const Arg
*arg
) {
1522 signal(SIGCHLD
, sigchld
);
1525 close(ConnectionNumber(dpy
));
1527 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1528 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1535 tag(const Arg
*arg
) {
1536 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1537 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1544 tagmon(const Arg
*arg
) {
1549 if(!(c
= selmon
->sel
))
1551 for(i
= 0, m
= mons
; m
; m
= m
->next
, i
++)
1557 #endif /* XINERAMA */
1560 textnw(const char *text
, unsigned int len
) {
1564 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1567 return XTextWidth(dc
.font
.xfont
, text
, len
);
1576 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1581 c
= nexttiled(m
->clients
);
1582 mw
= m
->mfact
* m
->ww
;
1583 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1589 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1591 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1596 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1597 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1598 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
));
1600 y
= c
->y
+ HEIGHT(c
);
1605 togglebar(const Arg
*arg
) {
1606 selmon
->showbar
= !selmon
->showbar
;
1607 updatebarpos(selmon
);
1608 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1613 togglefloating(const Arg
*arg
) {
1616 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1617 if(selmon
->sel
->isfloating
)
1618 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
, selmon
->sel
->w
, selmon
->sel
->h
);
1623 toggletag(const Arg
*arg
) {
1629 mask
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1631 selmon
->sel
->tags
= mask
;
1637 toggleview(const Arg
*arg
) {
1638 unsigned int mask
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1641 selmon
->tagset
[selmon
->seltags
] = mask
;
1647 unfocus(Client
*c
) {
1650 grabbuttons(c
, False
);
1651 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1652 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1656 unmanage(Client
*c
) {
1659 wc
.border_width
= c
->oldbw
;
1660 /* The server grab construct avoids race conditions. */
1662 XSetErrorHandler(xerrordummy
);
1663 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1666 if(c
->mon
->sel
== c
) {
1667 /* TODO: consider separate the next code into a function or into detachstack? */
1669 for(tc
= c
->mon
->stack
; tc
&& !ISVISIBLE(tc
); tc
= tc
->snext
);
1673 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1674 setclientstate(c
, WithdrawnState
);
1677 XSetErrorHandler(xerror
);
1683 unmapnotify(XEvent
*e
) {
1685 XUnmapEvent
*ev
= &e
->xunmap
;
1687 if((c
= getclient(ev
->window
)))
1694 XSetWindowAttributes wa
;
1696 wa
.override_redirect
= True
;
1697 wa
.background_pixmap
= ParentRelative
;
1698 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1700 for(m
= mons
; m
; m
= m
->next
) {
1701 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1703 CopyFromParent
, DefaultVisual(dpy
, screen
),
1704 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1705 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1706 XMapRaised(dpy
, m
->barwin
);
1711 updatebarpos(Monitor
*m
) {
1716 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1717 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1727 Monitor
*newmons
= NULL
, *m
, *tm
;
1731 #elif defined(XINERAMA)
1732 XineramaScreenInfo
*info
= NULL
;
1734 if(XineramaIsActive(dpy
))
1735 info
= XineramaQueryScreens(dpy
, &n
);
1737 /* allocate monitor(s) for the new geometry setup */
1738 for(i
= 0; i
< n
; i
++) {
1739 m
= (Monitor
*)malloc(sizeof(Monitor
));
1744 /* initialise monitor(s) */
1748 m
->screen_number
= 0;
1752 m
->mh
= m
->wh
= sh
/ 2;
1754 m
->screen_number
= 1;
1756 m
->my
= m
->wy
= sy
+ sh
/ 2;
1758 m
->mh
= m
->wh
= sh
/ 2;
1761 #elif defined(XINERAMA)
1762 if(XineramaIsActive(dpy
)) {
1763 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1764 m
->screen_number
= info
[i
].screen_number
;
1765 m
->wx
= info
[i
].x_org
;
1766 m
->my
= m
->wy
= info
[i
].y_org
;
1767 m
->ww
= info
[i
].width
;
1768 m
->mh
= m
->wh
= info
[i
].height
;
1774 /* default monitor setup */
1776 m
->screen_number
= 0;
1783 /* bar geometry setup */
1784 for(m
= newmons
; m
; m
= m
->next
) {
1785 /* TODO: consider removing the following values from config.h */
1791 m
->tagset
[0] = m
->tagset
[1] = 1;
1793 m
->showbar
= showbar
;
1798 /* reassign left over clients of disappeared monitors */
1799 for(tm
= mons
; tm
; tm
= tm
->next
)
1800 while(tm
->clients
) {
1802 tm
->clients
= c
->next
;
1809 /* select focused monitor */
1812 selmon
= getmonitor(root
);
1816 updatenumlockmask(void) {
1818 XModifierKeymap
*modmap
;
1821 modmap
= XGetModifierMapping(dpy
);
1822 for(i
= 0; i
< 8; i
++)
1823 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1824 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1825 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1826 numlockmask
= (1 << i
);
1827 XFreeModifiermap(modmap
);
1831 updatesizehints(Client
*c
) {
1835 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1836 /* size is uninitialized, ensure that size.flags aren't used */
1838 if(size
.flags
& PBaseSize
) {
1839 c
->basew
= size
.base_width
;
1840 c
->baseh
= size
.base_height
;
1842 else if(size
.flags
& PMinSize
) {
1843 c
->basew
= size
.min_width
;
1844 c
->baseh
= size
.min_height
;
1847 c
->basew
= c
->baseh
= 0;
1848 if(size
.flags
& PResizeInc
) {
1849 c
->incw
= size
.width_inc
;
1850 c
->inch
= size
.height_inc
;
1853 c
->incw
= c
->inch
= 0;
1854 if(size
.flags
& PMaxSize
) {
1855 c
->maxw
= size
.max_width
;
1856 c
->maxh
= size
.max_height
;
1859 c
->maxw
= c
->maxh
= 0;
1860 if(size
.flags
& PMinSize
) {
1861 c
->minw
= size
.min_width
;
1862 c
->minh
= size
.min_height
;
1864 else if(size
.flags
& PBaseSize
) {
1865 c
->minw
= size
.base_width
;
1866 c
->minh
= size
.base_height
;
1869 c
->minw
= c
->minh
= 0;
1870 if(size
.flags
& PAspect
) {
1871 c
->mina
= (float)size
.min_aspect
.y
/ (float)size
.min_aspect
.x
;
1872 c
->maxa
= (float)size
.max_aspect
.x
/ (float)size
.max_aspect
.y
;
1875 c
->maxa
= c
->mina
= 0.0;
1876 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1877 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1881 updatetitle(Client
*c
) {
1882 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1883 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1887 updatestatus(void) {
1888 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1889 strcpy(stext
, "dwm-"VERSION
);
1894 updatewmhints(Client
*c
) {
1897 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1898 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1899 wmh
->flags
&= ~XUrgencyHint
;
1900 XSetWMHints(dpy
, c
->win
, wmh
);
1903 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1910 view(const Arg
*arg
) {
1911 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1913 selmon
->seltags
^= 1; /* toggle sel tagset */
1914 if(arg
->ui
& TAGMASK
)
1915 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1919 /* There's no way to check accesses to destroyed windows, thus those cases are
1920 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1921 * default error handler, which may call exit. */
1923 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1924 if(ee
->error_code
== BadWindow
1925 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1926 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1927 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1928 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1929 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1930 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1931 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1932 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1934 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1935 ee
->request_code
, ee
->error_code
);
1936 return xerrorxlib(dpy
, ee
); /* may call exit */
1940 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1944 /* Startup Error handler to check if another window manager
1945 * is already running. */
1947 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1953 zoom(const Arg
*arg
) {
1954 Client
*c
= selmon
->sel
;
1956 if(!lt
[selmon
->sellt
]->arrange
|| lt
[selmon
->sellt
]->arrange
== monocle
|| (selmon
->sel
&& selmon
->sel
->isfloating
))
1958 if(c
== nexttiled(selmon
->clients
))
1959 if(!c
|| !(c
= nexttiled(c
->next
)))
1968 main(int argc
, char *argv
[]) {
1969 if(argc
== 2 && !strcmp("-v", argv
[1]))
1970 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1972 die("usage: dwm [-v]\n");
1974 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1975 fputs("warning: no locale support\n", stderr
);
1977 if(!(dpy
= XOpenDisplay(NULL
)))
1978 die("dwm: cannot open display\n");