Xinqi Bao's Git
1 //#define XINULATOR /* debug, simulates dual head */
2 /* See LICENSE file for copyright and license details.
4 * dynamic window manager is designed like any other X client as well. It is
5 * driven through handling X events. In contrast to other X clients, a window
6 * manager selects for SubstructureRedirectMask on the root window, to receive
7 * events about window (dis-)appearance. Only one X connection at a time is
8 * allowed to select for this event mask.
10 * The event handlers of dwm are organized in an array which is accessed
11 * whenever a new event has been fetched. This allows event dispatching
14 * Each child of the root window is called a client, except windows which have
15 * set the override_redirect flag. Clients are organized in a global
16 * linked client list, the focus history is remembered through a global
17 * stack list. Each client contains a bit array to indicate the tags of a
20 * Keys and tagging rules are organized as arrays and defined in config.h.
22 * To understand everything else, start reading main().
32 #include <sys/types.h>
34 #include <X11/cursorfont.h>
35 #include <X11/keysym.h>
36 #include <X11/Xatom.h>
38 #include <X11/Xproto.h>
39 #include <X11/Xutil.h>
41 #include <X11/extensions/Xinerama.h>
45 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
46 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
47 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
48 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
49 #define LENGTH(X) (sizeof X / sizeof X[0])
50 #define MAX(A, B) ((A) > (B) ? (A) : (B))
51 #define MIN(A, B) ((A) < (B) ? (A) : (B))
52 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
53 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
54 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
55 #define TAGMASK ((int)((1LL << LENGTH(tags)) - 1))
56 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
59 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
60 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
61 enum { NetSupported
, NetWMName
, NetLast
}; /* EWMH atoms */
62 enum { WMProtocols
, WMDelete
, WMState
, WMLast
}; /* default atoms */
63 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
64 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
77 void (*func
)(const Arg
*arg
);
81 typedef struct Monitor Monitor
;
82 typedef struct Client Client
;
87 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
90 Bool isfixed
, isfloating
, isurgent
;
99 unsigned long norm
[ColLast
];
100 unsigned long sel
[ColLast
];
110 } DC
; /* draw context */
115 void (*func
)(const Arg
*);
121 void (*arrange
)(Monitor
*);
127 int by
, btx
; /* bar geometry */
128 int 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
*getmon(Window w
);
180 static Monitor
*getmonn(unsigned int n
);
181 static Monitor
*getmonxy(int x
, int y
);
182 static Bool
getrootpointer(int *x
, int *y
);
183 static long getstate(Window w
);
184 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
185 static void grabbuttons(Client
*c
, Bool focused
);
186 static void grabkeys(void);
187 static void initfont(const char *fontstr
);
188 static Bool
isprotodel(Client
*c
);
189 static void keypress(XEvent
*e
);
190 static void killclient(const Arg
*arg
);
191 static void manage(Window w
, XWindowAttributes
*wa
);
192 static void mappingnotify(XEvent
*e
);
193 static void maprequest(XEvent
*e
);
194 static void monocle(Monitor
*m
);
195 static void movemouse(const Arg
*arg
);
196 static Client
*nexttiled(Client
*c
);
197 static void propertynotify(XEvent
*e
);
198 static void quit(const Arg
*arg
);
199 static void resize(Client
*c
, int x
, int y
, int w
, int h
);
200 static void resizemouse(const Arg
*arg
);
201 static void restack(Monitor
*m
);
202 static void run(void);
203 static void scan(void);
204 static void sendmon(Client
*c
, Monitor
*m
);
205 static void setclientstate(Client
*c
, long state
);
206 static void setlayout(const Arg
*arg
);
207 static void setmfact(const Arg
*arg
);
208 static void setup(void);
209 static void showhide(Client
*c
);
210 static void sigchld(int signal
);
211 static void spawn(const Arg
*arg
);
212 static void tag(const Arg
*arg
);
213 static int textnw(const char *text
, unsigned int len
);
214 static void tile(Monitor
*);
215 static void togglebar(const Arg
*arg
);
216 static void togglefloating(const Arg
*arg
);
217 static void toggletag(const Arg
*arg
);
218 static void toggleview(const Arg
*arg
);
219 static void unfocus(Client
*c
);
220 static void unmanage(Client
*c
);
221 static void unmapnotify(XEvent
*e
);
222 static void updategeom(void);
223 static void updatebarpos(Monitor
*m
);
224 static void updatebars(void);
225 static void updatenumlockmask(void);
226 static void updatesizehints(Client
*c
);
227 static void updatestatus(void);
228 static void updatetitle(Client
*c
);
229 static void updatewmhints(Client
*c
);
230 static void view(const Arg
*arg
);
231 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
232 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
233 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
234 static void zoom(const Arg
*arg
);
236 static void focusmon(const Arg
*arg
);
237 static void tagmon(const Arg
*arg
);
238 #endif /* XINERAMA */
241 static char stext
[256];
243 static int sx
, sy
, sw
, sh
; /* X display screen geometry x, y, width, height */
244 static int bh
, blw
= 0; /* bar geometry */
245 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
246 static unsigned int numlockmask
= 0;
247 static void (*handler
[LASTEvent
]) (XEvent
*) = {
248 [ButtonPress
] = buttonpress
,
249 [ConfigureRequest
] = configurerequest
,
250 [ConfigureNotify
] = configurenotify
,
251 [DestroyNotify
] = destroynotify
,
252 [EnterNotify
] = enternotify
,
255 [KeyPress
] = keypress
,
256 [MappingNotify
] = mappingnotify
,
257 [MapRequest
] = maprequest
,
258 [PropertyNotify
] = propertynotify
,
259 [UnmapNotify
] = unmapnotify
261 static Atom wmatom
[WMLast
], netatom
[NetLast
];
263 static Bool running
= True
;
264 static Cursor cursor
[CurLast
];
267 static Layout
*lt
[] = { NULL
, NULL
};
268 static Monitor
*mons
= NULL
, *selmon
= NULL
;
270 /* configuration, allows nested code to access above variables */
273 /* compile-time check if all tags fit into an unsigned int bit array. */
274 struct NumTags
{ char limitexceeded
[sizeof(unsigned int) * 8 < LENGTH(tags
) ? -1 : 1]; };
276 /* function implementations */
278 applyrules(Client
*c
) {
281 XClassHint ch
= { 0 };
284 c
->isfloating
= c
->tags
= 0;
285 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
286 for(i
= 0; i
< LENGTH(rules
); i
++) {
288 if((!r
->title
|| strstr(c
->name
, r
->title
))
289 && (!r
->class || (ch
.res_class
&& strstr(ch
.res_class
, r
->class)))
290 && (!r
->instance
|| (ch
.res_name
&& strstr(ch
.res_name
, r
->instance
)))) {
291 c
->isfloating
= r
->isfloating
;
300 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
304 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
) {
307 /* set minimum possible */
315 if(*x
+ *w
+ 2 * c
->bw
< sx
)
317 if(*y
+ *h
+ 2 * c
->bw
< sy
)
324 if(resizehints
|| c
->isfloating
) {
325 /* see last two sentences in ICCCM 4.1.2.3 */
326 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
328 if(!baseismin
) { /* temporarily remove base dimensions */
333 /* adjust for aspect limits */
334 if(c
->mina
> 0 && c
->maxa
> 0) {
335 if(c
->maxa
< (float)*w
/ *h
)
337 else if(c
->mina
< (float)*h
/ *w
)
341 if(baseismin
) { /* increment calculation requires this */
346 /* adjust for increment value */
352 /* restore base dimensions */
356 *w
= MAX(*w
, c
->minw
);
357 *h
= MAX(*h
, c
->minh
);
360 *w
= MIN(*w
, c
->maxw
);
363 *h
= MIN(*h
, c
->maxh
);
365 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
372 /* optimise two loops into one, check focus(NULL) */
373 for(m
= mons
; m
; m
= m
->next
)
376 for(m
= mons
; m
; m
= m
->next
) {
377 if(lt
[m
->sellt
]->arrange
)
378 lt
[m
->sellt
]->arrange(m
);
385 c
->next
= c
->mon
->clients
;
390 attachstack(Client
*c
) {
391 c
->snext
= c
->mon
->stack
;
396 buttonpress(XEvent
*e
) {
397 unsigned int i
, x
, click
;
401 XButtonPressedEvent
*ev
= &e
->xbutton
;
404 /* focus monitor if necessary */
405 if((m
= getmon(ev
->window
)) && m
!= selmon
) {
406 unfocus(selmon
->sel
);
410 if(ev
->window
== selmon
->barwin
&& ev
->x
>= selmon
->btx
) {
415 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
416 if(i
< LENGTH(tags
)) {
420 else if(ev
->x
< x
+ blw
)
422 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
423 click
= ClkStatusText
;
427 else if((c
= getclient(ev
->window
))) {
429 click
= ClkClientWin
;
432 for(i
= 0; i
< LENGTH(buttons
); i
++)
433 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
434 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
435 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
441 xerrorxlib
= XSetErrorHandler(xerrorstart
);
443 /* this causes an error if some other window manager is running */
444 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
447 die("dwm: another window manager is already running\n");
448 XSetErrorHandler(xerror
);
455 Layout foo
= { "", NULL
};
459 lt
[selmon
->sellt
] = &foo
;
461 /* TODO: consider simplifying cleanup code of the stack, perhaps do that in cleanmons() ? */
462 for(m
= mons
; m
; m
= m
->next
)
466 XFreeFontSet(dpy
, dc
.font
.set
);
468 XFreeFont(dpy
, dc
.font
.xfont
);
469 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
470 XFreePixmap(dpy
, dc
.drawable
);
472 XFreeCursor(dpy
, cursor
[CurNormal
]);
473 XFreeCursor(dpy
, cursor
[CurResize
]);
474 XFreeCursor(dpy
, cursor
[CurMove
]);
477 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
486 XUnmapWindow(dpy
, mons
->barwin
);
487 XDestroyWindow(dpy
, mons
->barwin
);
494 clearurgent(Client
*c
) {
498 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
500 wmh
->flags
&= ~XUrgencyHint
;
501 XSetWMHints(dpy
, c
->win
, wmh
);
506 configure(Client
*c
) {
509 ce
.type
= ConfigureNotify
;
517 ce
.border_width
= c
->bw
;
519 ce
.override_redirect
= False
;
520 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
524 configurenotify(XEvent
*e
) {
526 XConfigureEvent
*ev
= &e
->xconfigure
;
528 if(ev
->window
== root
&& (ev
->width
!= sw
|| ev
->height
!= sh
)) {
533 XFreePixmap(dpy
, dc
.drawable
);
534 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
536 for(m
= mons
; m
; m
= m
->next
)
537 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
543 configurerequest(XEvent
*e
) {
545 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
548 if((c
= getclient(ev
->window
))) {
549 if(ev
->value_mask
& CWBorderWidth
)
550 c
->bw
= ev
->border_width
;
551 else if(c
->isfloating
|| !lt
[selmon
->sellt
]->arrange
) {
552 if(ev
->value_mask
& CWX
)
554 if(ev
->value_mask
& CWY
)
556 if(ev
->value_mask
& CWWidth
)
558 if(ev
->value_mask
& CWHeight
)
560 if((c
->x
- sx
+ c
->w
) > sw
&& c
->isfloating
)
561 c
->x
= sx
+ (sw
/ 2 - c
->w
/ 2); /* center in x direction */
562 if((c
->y
- sy
+ c
->h
) > sh
&& c
->isfloating
)
563 c
->y
= sy
+ (sh
/ 2 - c
->h
/ 2); /* center in y direction */
564 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
567 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
575 wc
.width
= ev
->width
;
576 wc
.height
= ev
->height
;
577 wc
.border_width
= ev
->border_width
;
578 wc
.sibling
= ev
->above
;
579 wc
.stack_mode
= ev
->detail
;
580 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
586 destroynotify(XEvent
*e
) {
588 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
590 if((c
= getclient(ev
->window
)))
598 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
603 detachstack(Client
*c
) {
606 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
611 die(const char *errstr
, ...) {
614 va_start(ap
, errstr
);
615 vfprintf(stderr
, errstr
, ap
);
621 drawbar(Monitor
*m
) {
623 unsigned int i
, occ
= 0, urg
= 0;
627 for(c
= m
->clients
; c
; c
= c
->next
) {
637 buf
[0] = m
->screen_number
+ '0';
640 drawtext(buf
, selmon
== m
? dc
.sel
: dc
.norm
, True
);
643 #endif /* XINERAMA */
645 for(i
= 0; i
< LENGTH(tags
); i
++) {
646 dc
.w
= TEXTW(tags
[i
]);
647 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
648 drawtext(tags
[i
], col
, urg
& 1 << i
);
649 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
650 occ
& 1 << i
, urg
& 1 << i
, col
);
655 drawtext(lt
[m
->sellt
]->symbol
, dc
.norm
, False
);
660 if(m
== selmon
) { /* status is only drawn on selected monitor */
667 drawtext(stext
, dc
.norm
, False
);
672 if((dc
.w
= dc
.x
- x
) > bh
) {
675 col
= m
== selmon
? dc
.sel
: dc
.norm
;
676 drawtext(m
->sel
->name
, col
, False
);
677 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
680 drawtext(NULL
, dc
.norm
, False
);
682 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
690 for(m
= mons
; m
; m
= m
->next
)
695 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
698 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
700 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
701 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
702 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
706 r
.width
= r
.height
= x
+ 1;
707 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
710 r
.width
= r
.height
= x
;
711 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
716 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
718 int i
, x
, y
, h
, len
, olen
;
719 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
721 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
722 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
726 h
= dc
.font
.ascent
+ dc
.font
.descent
;
727 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
729 /* shorten text if necessary */
730 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
733 memcpy(buf
, text
, len
);
735 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
736 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
738 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
740 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
744 enternotify(XEvent
*e
) {
747 XCrossingEvent
*ev
= &e
->xcrossing
;
749 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
751 if((m
= getmon(ev
->window
)) && m
!= selmon
) {
752 unfocus(selmon
->sel
);
755 if((c
= getclient(ev
->window
)))
764 XExposeEvent
*ev
= &e
->xexpose
;
766 if(ev
->count
== 0 && (m
= getmon(ev
->window
)))
772 if(!c
|| !ISVISIBLE(c
))
773 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
775 unfocus(selmon
->sel
);
783 grabbuttons(c
, True
);
784 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
785 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
788 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
794 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
795 XFocusChangeEvent
*ev
= &e
->xfocus
;
797 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
798 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
803 focusmon(const Arg
*arg
) {
806 if(!(m
= getmonn(arg
->ui
)) || m
== selmon
)
808 unfocus(selmon
->sel
);
812 #endif /* XINERAMA */
815 focusstack(const Arg
*arg
) {
816 Client
*c
= NULL
, *i
;
821 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
823 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
826 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
830 for(; i
; i
= i
->next
)
841 getclient(Window w
) {
845 for(m
= mons
; m
; m
= m
->next
)
846 for(c
= m
->clients
; c
; c
= c
->next
)
853 getcolor(const char *colstr
) {
854 Colormap cmap
= DefaultColormap(dpy
, screen
);
857 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
858 die("error, cannot allocate color '%s'\n", colstr
);
868 if(w
== root
&& getrootpointer(&x
, &y
))
869 return getmonxy(x
, y
);
870 for(m
= mons
; m
; m
= m
->next
)
873 if((c
= getclient(w
)))
879 getmonn(unsigned int n
) {
883 for(m
= mons
, i
= 0; m
&& i
!= n
; m
= m
->next
, i
++);
888 getmonxy(int x
, int y
) {
891 for(m
= mons
; m
; m
= m
->next
)
892 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
898 getrootpointer(int *x
, int *y
) {
902 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
909 unsigned char *p
= NULL
;
910 unsigned long n
, extra
;
913 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
914 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
915 if(status
!= Success
)
924 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
929 if(!text
|| size
== 0)
932 XGetTextProperty(dpy
, w
, &name
, atom
);
935 if(name
.encoding
== XA_STRING
)
936 strncpy(text
, (char *)name
.value
, size
- 1);
938 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
940 strncpy(text
, *list
, size
- 1);
941 XFreeStringList(list
);
944 text
[size
- 1] = '\0';
950 grabbuttons(Client
*c
, Bool focused
) {
954 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
955 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
957 for(i
= 0; i
< LENGTH(buttons
); i
++)
958 if(buttons
[i
].click
== ClkClientWin
)
959 for(j
= 0; j
< LENGTH(modifiers
); j
++)
960 XGrabButton(dpy
, buttons
[i
].button
,
961 buttons
[i
].mask
| modifiers
[j
],
962 c
->win
, False
, BUTTONMASK
,
963 GrabModeAsync
, GrabModeSync
, None
, None
);
965 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
966 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
975 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
978 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
979 for(i
= 0; i
< LENGTH(keys
); i
++) {
980 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
981 for(j
= 0; j
< LENGTH(modifiers
); j
++)
982 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
983 True
, GrabModeAsync
, GrabModeAsync
);
989 initfont(const char *fontstr
) {
990 char *def
, **missing
;
994 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
997 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
998 XFreeStringList(missing
);
1001 XFontSetExtents
*font_extents
;
1002 XFontStruct
**xfonts
;
1004 dc
.font
.ascent
= dc
.font
.descent
= 0;
1005 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
1006 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
1007 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
1008 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
1009 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1014 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1015 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1016 die("error, cannot load font: '%s'\n", fontstr
);
1017 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1018 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1020 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1024 isprotodel(Client
*c
) {
1029 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1030 for(i
= 0; !ret
&& i
< n
; i
++)
1031 if(protocols
[i
] == wmatom
[WMDelete
])
1039 keypress(XEvent
*e
) {
1045 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1046 for(i
= 0; i
< LENGTH(keys
); i
++)
1047 if(keysym
== keys
[i
].keysym
1048 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1050 keys
[i
].func(&(keys
[i
].arg
));
1054 killclient(const Arg
*arg
) {
1059 if(isprotodel(selmon
->sel
)) {
1060 ev
.type
= ClientMessage
;
1061 ev
.xclient
.window
= selmon
->sel
->win
;
1062 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1063 ev
.xclient
.format
= 32;
1064 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1065 ev
.xclient
.data
.l
[1] = CurrentTime
;
1066 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1069 XKillClient(dpy
, selmon
->sel
->win
);
1073 manage(Window w
, XWindowAttributes
*wa
) {
1075 Client
*c
, *t
= NULL
;
1076 Window trans
= None
;
1079 if(!(c
= malloc(sizeof(Client
))))
1080 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1090 c
->oldbw
= wa
->border_width
;
1091 if(c
->w
== sw
&& c
->h
== sh
) {
1097 if(c
->x
+ WIDTH(c
) > sx
+ sw
)
1098 c
->x
= sx
+ sw
- WIDTH(c
);
1099 if(c
->y
+ HEIGHT(c
) > sy
+ sh
)
1100 c
->y
= sy
+ sh
- HEIGHT(c
);
1101 c
->x
= MAX(c
->x
, sx
);
1102 /* only fix client y-offset, if the client center might cover the bar */
1103 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1104 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: sy
);
1108 wc
.border_width
= c
->bw
;
1109 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1110 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1111 configure(c
); /* propagates border_width, if size doesn't change */
1113 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1114 grabbuttons(c
, False
);
1116 if(XGetTransientForHint(dpy
, w
, &trans
))
1117 t
= getclient(trans
);
1123 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1125 XRaiseWindow(dpy
, c
->win
);
1128 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1129 XMapWindow(dpy
, c
->win
);
1130 setclientstate(c
, NormalState
);
1135 mappingnotify(XEvent
*e
) {
1136 XMappingEvent
*ev
= &e
->xmapping
;
1138 XRefreshKeyboardMapping(ev
);
1139 if(ev
->request
== MappingKeyboard
)
1144 maprequest(XEvent
*e
) {
1145 static XWindowAttributes wa
;
1146 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1148 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1150 if(wa
.override_redirect
)
1152 if(!getclient(ev
->window
))
1153 manage(ev
->window
, &wa
);
1157 monocle(Monitor
*m
) {
1160 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1161 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1165 movemouse(const Arg
*arg
) {
1166 int x
, y
, ocx
, ocy
, nx
, ny
;
1171 if(!(c
= selmon
->sel
))
1176 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1177 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1179 if(!getrootpointer(&x
, &y
))
1182 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1184 case ConfigureRequest
:
1187 handler
[ev
.type
](&ev
);
1190 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1191 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1192 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1193 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1194 if(abs(selmon
->wx
- nx
) < snap
)
1196 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1197 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1198 if(abs(selmon
->wy
- ny
) < snap
)
1200 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1201 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1202 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1203 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1204 togglefloating(NULL
);
1206 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1207 resize(c
, nx
, ny
, c
->w
, c
->h
);
1211 while(ev
.type
!= ButtonRelease
);
1212 XUngrabPointer(dpy
, CurrentTime
);
1213 if((m
= getmonxy(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
)
1218 nexttiled(Client
*c
) {
1219 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1224 propertynotify(XEvent
*e
) {
1227 XPropertyEvent
*ev
= &e
->xproperty
;
1229 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1231 else if(ev
->state
== PropertyDelete
)
1232 return; /* ignore */
1233 else if((c
= getclient(ev
->window
))) {
1236 case XA_WM_TRANSIENT_FOR
:
1237 XGetTransientForHint(dpy
, c
->win
, &trans
);
1238 if(!c
->isfloating
&& (c
->isfloating
= (getclient(trans
) != NULL
)))
1241 case XA_WM_NORMAL_HINTS
:
1249 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1251 if(c
== selmon
->sel
)
1258 quit(const Arg
*arg
) {
1263 resize(Client
*c
, int x
, int y
, int w
, int h
) {
1266 if(applysizehints(c
, &x
, &y
, &w
, &h
)) {
1269 c
->w
= wc
.width
= w
;
1270 c
->h
= wc
.height
= h
;
1271 wc
.border_width
= c
->bw
;
1272 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1279 resizemouse(const Arg
*arg
) {
1286 if(!(c
= selmon
->sel
))
1291 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1292 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1294 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1296 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1298 case ConfigureRequest
:
1301 handler
[ev
.type
](&ev
);
1304 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1305 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1307 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1308 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
) {
1309 if(!c
->isfloating
&& lt
[selmon
->sellt
]->arrange
1310 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1311 togglefloating(NULL
);
1313 if(!lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1314 resize(c
, c
->x
, c
->y
, nw
, nh
);
1318 while(ev
.type
!= ButtonRelease
);
1319 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1320 XUngrabPointer(dpy
, CurrentTime
);
1321 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1322 if((m
= getmonxy(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
)
1327 restack(Monitor
*m
) {
1335 if(m
->sel
->isfloating
|| !lt
[m
->sellt
]->arrange
)
1336 XRaiseWindow(dpy
, m
->sel
->win
);
1337 if(lt
[m
->sellt
]->arrange
) {
1338 wc
.stack_mode
= Below
;
1339 wc
.sibling
= m
->barwin
;
1340 for(c
= m
->stack
; c
; c
= c
->snext
)
1341 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1342 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1343 wc
.sibling
= c
->win
;
1347 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1354 /* main event loop */
1356 while(running
&& !XNextEvent(dpy
, &ev
)) {
1357 if(handler
[ev
.type
])
1358 (handler
[ev
.type
])(&ev
); /* call handler */
1364 unsigned int i
, num
;
1365 Window d1
, d2
, *wins
= NULL
;
1366 XWindowAttributes wa
;
1368 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1369 for(i
= 0; i
< num
; i
++) {
1370 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1371 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1373 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1374 manage(wins
[i
], &wa
);
1376 for(i
= 0; i
< num
; i
++) { /* now the transients */
1377 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1379 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1380 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1381 manage(wins
[i
], &wa
);
1389 sendmon(Client
*c
, Monitor
*m
) {
1395 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1403 setclientstate(Client
*c
, long state
) {
1404 long data
[] = {state
, None
};
1406 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1407 PropModeReplace
, (unsigned char *)data
, 2);
1411 setlayout(const Arg
*arg
) {
1412 if(!arg
|| !arg
->v
|| arg
->v
!= lt
[selmon
->sellt
])
1415 lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1422 /* arg > 1.0 will set mfact absolutly */
1424 setmfact(const Arg
*arg
) {
1427 if(!arg
|| !lt
[selmon
->sellt
]->arrange
)
1429 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1430 if(f
< 0.1 || f
> 0.9)
1440 XSetWindowAttributes wa
;
1443 screen
= DefaultScreen(dpy
);
1444 root
= RootWindow(dpy
, screen
);
1448 sw
= DisplayWidth(dpy
, screen
);
1449 sh
= DisplayHeight(dpy
, screen
);
1450 bh
= dc
.h
= dc
.font
.height
+ 2;
1451 lt
[0] = &layouts
[0];
1452 lt
[1] = &layouts
[1 % LENGTH(layouts
)];
1456 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1457 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1458 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1459 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1460 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1463 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1464 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1465 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1467 /* init appearance */
1468 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1469 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1470 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1471 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1472 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1473 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1474 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1475 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1476 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1478 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1481 for(blw
= i
= 0; LENGTH(layouts
) > 1 && i
< LENGTH(layouts
); i
++) {
1482 w
= TEXTW(layouts
[i
].symbol
);
1488 /* EWMH support per view */
1489 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1490 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1492 /* select for events */
1493 wa
.cursor
= cursor
[CurNormal
];
1494 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1495 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1496 |PropertyChangeMask
;
1497 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1498 XSelectInput(dpy
, root
, wa
.event_mask
);
1504 showhide(Client
*c
) {
1507 if(ISVISIBLE(c
)) { /* show clients top down */
1508 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1509 if(!lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1510 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1513 else { /* hide clients bottom up */
1515 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1521 sigchld(int signal
) {
1522 while(0 < waitpid(-1, NULL
, WNOHANG
));
1526 spawn(const Arg
*arg
) {
1527 signal(SIGCHLD
, sigchld
);
1530 close(ConnectionNumber(dpy
));
1532 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1533 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1540 tag(const Arg
*arg
) {
1541 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1542 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1549 tagmon(const Arg
*arg
) {
1552 if(!selmon
->sel
|| !(m
= getmonn(arg
->ui
)))
1554 sendmon(selmon
->sel
, m
);
1556 #endif /* XINERAMA */
1559 textnw(const char *text
, unsigned int len
) {
1563 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1566 return XTextWidth(dc
.font
.xfont
, text
, len
);
1575 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1580 c
= nexttiled(m
->clients
);
1581 mw
= m
->mfact
* m
->ww
;
1582 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
);
1588 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1590 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1595 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1596 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1597 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
));
1599 y
= c
->y
+ HEIGHT(c
);
1604 togglebar(const Arg
*arg
) {
1605 selmon
->showbar
= !selmon
->showbar
;
1606 updatebarpos(selmon
);
1607 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1612 togglefloating(const Arg
*arg
) {
1615 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1616 if(selmon
->sel
->isfloating
)
1617 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
, selmon
->sel
->w
, selmon
->sel
->h
);
1622 toggletag(const Arg
*arg
) {
1628 mask
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1630 selmon
->sel
->tags
= mask
;
1636 toggleview(const Arg
*arg
) {
1637 unsigned int mask
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1640 selmon
->tagset
[selmon
->seltags
] = mask
;
1646 unfocus(Client
*c
) {
1649 grabbuttons(c
, False
);
1650 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1651 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1655 unmanage(Client
*c
) {
1658 wc
.border_width
= c
->oldbw
;
1659 /* The server grab construct avoids race conditions. */
1661 XSetErrorHandler(xerrordummy
);
1662 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1665 if(c
->mon
->sel
== c
) {
1666 /* TODO: consider separate the next code into a function or into detachstack? */
1668 for(tc
= c
->mon
->stack
; tc
&& !ISVISIBLE(tc
); tc
= tc
->snext
);
1672 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1673 setclientstate(c
, WithdrawnState
);
1676 XSetErrorHandler(xerror
);
1682 unmapnotify(XEvent
*e
) {
1684 XUnmapEvent
*ev
= &e
->xunmap
;
1686 if((c
= getclient(ev
->window
)))
1693 XSetWindowAttributes wa
;
1695 wa
.override_redirect
= True
;
1696 wa
.background_pixmap
= ParentRelative
;
1697 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1699 for(m
= mons
; m
; m
= m
->next
) {
1700 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1702 CopyFromParent
, DefaultVisual(dpy
, screen
),
1703 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1704 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1705 XMapRaised(dpy
, m
->barwin
);
1710 updatebarpos(Monitor
*m
) {
1715 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1716 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1726 Monitor
*newmons
= NULL
, *m
, *tm
;
1730 #elif defined(XINERAMA)
1731 XineramaScreenInfo
*info
= NULL
;
1733 if(XineramaIsActive(dpy
))
1734 info
= XineramaQueryScreens(dpy
, &n
);
1736 /* allocate monitor(s) for the new geometry setup */
1737 for(i
= 0; i
< n
; i
++) {
1738 m
= (Monitor
*)malloc(sizeof(Monitor
));
1743 /* initialise monitor(s) */
1747 m
->screen_number
= 0;
1751 m
->mh
= m
->wh
= sh
/ 2;
1753 m
->screen_number
= 1;
1755 m
->my
= m
->wy
= sy
+ sh
/ 2;
1757 m
->mh
= m
->wh
= sh
/ 2;
1760 #elif defined(XINERAMA)
1761 if(XineramaIsActive(dpy
)) {
1762 for(i
= 0, m
= newmons
; m
; m
= m
->next
, i
++) {
1763 m
->screen_number
= info
[i
].screen_number
;
1764 m
->wx
= info
[i
].x_org
;
1765 m
->my
= m
->wy
= info
[i
].y_org
;
1766 m
->ww
= info
[i
].width
;
1767 m
->mh
= m
->wh
= info
[i
].height
;
1773 /* default monitor setup */
1775 m
->screen_number
= 0;
1782 /* bar geometry setup */
1783 for(m
= newmons
; m
; m
= m
->next
) {
1784 /* TODO: consider removing the following values from config.h */
1790 m
->tagset
[0] = m
->tagset
[1] = 1;
1792 m
->showbar
= showbar
;
1797 /* reassign left over clients of disappeared monitors */
1798 for(tm
= mons
; tm
; tm
= tm
->next
)
1799 while(tm
->clients
) {
1801 tm
->clients
= c
->next
;
1808 /* select focused monitor */
1811 selmon
= getmon(root
);
1815 updatenumlockmask(void) {
1817 XModifierKeymap
*modmap
;
1820 modmap
= XGetModifierMapping(dpy
);
1821 for(i
= 0; i
< 8; i
++)
1822 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1823 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1824 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1825 numlockmask
= (1 << i
);
1826 XFreeModifiermap(modmap
);
1830 updatesizehints(Client
*c
) {
1834 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1835 /* size is uninitialized, ensure that size.flags aren't used */
1837 if(size
.flags
& PBaseSize
) {
1838 c
->basew
= size
.base_width
;
1839 c
->baseh
= size
.base_height
;
1841 else if(size
.flags
& PMinSize
) {
1842 c
->basew
= size
.min_width
;
1843 c
->baseh
= size
.min_height
;
1846 c
->basew
= c
->baseh
= 0;
1847 if(size
.flags
& PResizeInc
) {
1848 c
->incw
= size
.width_inc
;
1849 c
->inch
= size
.height_inc
;
1852 c
->incw
= c
->inch
= 0;
1853 if(size
.flags
& PMaxSize
) {
1854 c
->maxw
= size
.max_width
;
1855 c
->maxh
= size
.max_height
;
1858 c
->maxw
= c
->maxh
= 0;
1859 if(size
.flags
& PMinSize
) {
1860 c
->minw
= size
.min_width
;
1861 c
->minh
= size
.min_height
;
1863 else if(size
.flags
& PBaseSize
) {
1864 c
->minw
= size
.base_width
;
1865 c
->minh
= size
.base_height
;
1868 c
->minw
= c
->minh
= 0;
1869 if(size
.flags
& PAspect
) {
1870 c
->mina
= (float)size
.min_aspect
.y
/ (float)size
.min_aspect
.x
;
1871 c
->maxa
= (float)size
.max_aspect
.x
/ (float)size
.max_aspect
.y
;
1874 c
->maxa
= c
->mina
= 0.0;
1875 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1876 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1880 updatetitle(Client
*c
) {
1881 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1882 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1886 updatestatus(void) {
1887 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1888 strcpy(stext
, "dwm-"VERSION
);
1893 updatewmhints(Client
*c
) {
1896 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1897 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1898 wmh
->flags
&= ~XUrgencyHint
;
1899 XSetWMHints(dpy
, c
->win
, wmh
);
1902 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1909 view(const Arg
*arg
) {
1910 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1912 selmon
->seltags
^= 1; /* toggle sel tagset */
1913 if(arg
->ui
& TAGMASK
)
1914 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1918 /* There's no way to check accesses to destroyed windows, thus those cases are
1919 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1920 * default error handler, which may call exit. */
1922 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1923 if(ee
->error_code
== BadWindow
1924 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1925 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1926 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1927 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1928 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1929 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1930 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1931 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1933 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1934 ee
->request_code
, ee
->error_code
);
1935 return xerrorxlib(dpy
, ee
); /* may call exit */
1939 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1943 /* Startup Error handler to check if another window manager
1944 * is already running. */
1946 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1952 zoom(const Arg
*arg
) {
1953 Client
*c
= selmon
->sel
;
1955 if(!lt
[selmon
->sellt
]->arrange
|| lt
[selmon
->sellt
]->arrange
== monocle
|| (selmon
->sel
&& selmon
->sel
->isfloating
))
1957 if(c
== nexttiled(selmon
->clients
))
1958 if(!c
|| !(c
= nexttiled(c
->next
)))
1967 main(int argc
, char *argv
[]) {
1968 if(argc
== 2 && !strcmp("-v", argv
[1]))
1969 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1971 die("usage: dwm [-v]\n");
1973 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1974 fputs("warning: no locale support\n", stderr
);
1976 if(!(dpy
= XOpenDisplay(NULL
)))
1977 die("dwm: cannot open display\n");