Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details.
3 * dynamic window manager is designed like any other X client as well. It is
4 * driven through handling X events. In contrast to other X clients, a window
5 * manager selects for SubstructureRedirectMask on the root window, to receive
6 * events about window (dis-)appearance. Only one X connection at a time is
7 * allowed to select for this event mask.
9 * The event handlers of dwm are organized in an array which is accessed
10 * whenever a new event has been fetched. This allows event dispatching
13 * Each child of the root window is called a client, except windows which have
14 * set the override_redirect flag. Clients are organized in a linked client
15 * list on each monitor, the focus history is remembered through a stack list
16 * on each monitor. Each client contains a bit array to indicate the tags of a
19 * Keys and tagging rules are organized as arrays and defined in config.h.
21 * To understand everything else, start reading main().
31 #include <sys/types.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
40 #include <X11/extensions/Xinerama.h>
44 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
45 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
46 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
47 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
48 #define LENGTH(X) (sizeof X / sizeof X[0])
49 #define MAX(A, B) ((A) > (B) ? (A) : (B))
50 #define MIN(A, B) ((A) < (B) ? (A) : (B))
51 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
52 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
53 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
54 #define TAGMASK ((1 << LENGTH(tags)) - 1)
55 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
58 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
59 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
60 enum { NetSupported
, NetWMName
, NetWMState
,
61 NetWMFullscreen
, NetActiveWindow
, NetWMWindowType
,
62 NetWMWindowTypeDialog
, NetLast
}; /* EWMH atoms */
63 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, WMLast
}; /* default atoms */
64 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
65 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
78 void (*func
)(const Arg
*arg
);
82 typedef struct Monitor Monitor
;
83 typedef struct Client Client
;
88 int oldx
, oldy
, oldw
, oldh
;
89 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
92 Bool isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
;
101 unsigned long norm
[ColLast
];
102 unsigned long sel
[ColLast
];
112 } DC
; /* draw context */
117 void (*func
)(const Arg
*);
123 void (*arrange
)(Monitor
*);
131 int by
; /* bar geometry */
132 int mx
, my
, mw
, mh
; /* screen size */
133 int wx
, wy
, ww
, wh
; /* window area */
134 unsigned int seltags
;
136 unsigned int tagset
[2];
149 const char *instance
;
156 /* function declarations */
157 static void applyrules(Client
*c
);
158 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
159 static void arrange(Monitor
*m
);
160 static void arrangemon(Monitor
*m
);
161 static void attach(Client
*c
);
162 static void attachstack(Client
*c
);
163 static void buttonpress(XEvent
*e
);
164 static void checkotherwm(void);
165 static void cleanup(void);
166 static void cleanupmon(Monitor
*mon
);
167 static void clearurgent(Client
*c
);
168 static void clientmessage(XEvent
*e
);
169 static void configure(Client
*c
);
170 static void configurenotify(XEvent
*e
);
171 static void configurerequest(XEvent
*e
);
172 static Monitor
*createmon(void);
173 static void destroynotify(XEvent
*e
);
174 static void detach(Client
*c
);
175 static void detachstack(Client
*c
);
176 static void die(const char *errstr
, ...);
177 static Monitor
*dirtomon(int dir
);
178 static void drawbar(Monitor
*m
);
179 static void drawbars(void);
180 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
181 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
182 static void enternotify(XEvent
*e
);
183 static void expose(XEvent
*e
);
184 static void focus(Client
*c
);
185 static void focusin(XEvent
*e
);
186 static void focusmon(const Arg
*arg
);
187 static void focusstack(const Arg
*arg
);
188 static unsigned long getcolor(const char *colstr
);
189 static Bool
getrootptr(int *x
, int *y
);
190 static long getstate(Window w
);
191 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
192 static void grabbuttons(Client
*c
, Bool focused
);
193 static void grabkeys(void);
194 static void incnmaster(const Arg
*arg
);
195 static void initfont(const char *fontstr
);
196 static void keypress(XEvent
*e
);
197 static void killclient(const Arg
*arg
);
198 static void manage(Window w
, XWindowAttributes
*wa
);
199 static void mappingnotify(XEvent
*e
);
200 static void maprequest(XEvent
*e
);
201 static void monocle(Monitor
*m
);
202 static void movemouse(const Arg
*arg
);
203 static Client
*nexttiled(Client
*c
);
204 static void pop(Client
*);
205 static void propertynotify(XEvent
*e
);
206 static Monitor
*ptrtomon(int x
, int y
);
207 static void quit(const Arg
*arg
);
208 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
209 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
210 static void resizemouse(const Arg
*arg
);
211 static void restack(Monitor
*m
);
212 static void run(void);
213 static void scan(void);
214 static Bool
sendevent(Client
*c
, Atom proto
);
215 static void sendmon(Client
*c
, Monitor
*m
);
216 static void setclientstate(Client
*c
, long state
);
217 static void setfocus(Client
*c
);
218 static void setlayout(const Arg
*arg
);
219 static void setmfact(const Arg
*arg
);
220 static void setup(void);
221 static void showhide(Client
*c
);
222 static void sigchld(int unused
);
223 static void spawn(const Arg
*arg
);
224 static void tag(const Arg
*arg
);
225 static void tagmon(const Arg
*arg
);
226 static int textnw(const char *text
, unsigned int len
);
227 static void tile(Monitor
*);
228 static void togglebar(const Arg
*arg
);
229 static void togglefloating(const Arg
*arg
);
230 static void toggletag(const Arg
*arg
);
231 static void toggleview(const Arg
*arg
);
232 static void unfocus(Client
*c
, Bool setfocus
);
233 static void unmanage(Client
*c
, Bool destroyed
);
234 static void unmapnotify(XEvent
*e
);
235 static Bool
updategeom(void);
236 static void updatebarpos(Monitor
*m
);
237 static void updatebars(void);
238 static void updatenumlockmask(void);
239 static void updatesizehints(Client
*c
);
240 static void updatestatus(void);
241 static void updatewindowtype(Client
*c
);
242 static void updatetitle(Client
*c
);
243 static void updatewmhints(Client
*c
);
244 static void view(const Arg
*arg
);
245 static Client
*wintoclient(Window w
);
246 static Monitor
*wintomon(Window w
);
247 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
248 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
249 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
250 static void zoom(const Arg
*arg
);
253 static const char broken
[] = "broken";
254 static char stext
[256];
256 static int sw
, sh
; /* X display screen geometry width, height */
257 static int bh
, blw
= 0; /* bar geometry */
258 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
259 static unsigned int numlockmask
= 0;
260 static void (*handler
[LASTEvent
]) (XEvent
*) = {
261 [ButtonPress
] = buttonpress
,
262 [ClientMessage
] = clientmessage
,
263 [ConfigureRequest
] = configurerequest
,
264 [ConfigureNotify
] = configurenotify
,
265 [DestroyNotify
] = destroynotify
,
266 [EnterNotify
] = enternotify
,
269 [KeyPress
] = keypress
,
270 [MappingNotify
] = mappingnotify
,
271 [MapRequest
] = maprequest
,
272 [PropertyNotify
] = propertynotify
,
273 [UnmapNotify
] = unmapnotify
275 static Atom wmatom
[WMLast
], netatom
[NetLast
];
276 static Bool running
= True
;
277 static Cursor cursor
[CurLast
];
280 static Monitor
*mons
= NULL
, *selmon
= NULL
;
283 /* configuration, allows nested code to access above variables */
286 /* compile-time check if all tags fit into an unsigned int bit array. */
287 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
289 /* function implementations */
291 applyrules(Client
*c
) {
292 const char *class, *instance
;
296 XClassHint ch
= { NULL
, NULL
};
299 c
->isfloating
= c
->tags
= 0;
300 XGetClassHint(dpy
, c
->win
, &ch
);
301 class = ch
.res_class
? ch
.res_class
: broken
;
302 instance
= ch
.res_name
? ch
.res_name
: broken
;
304 for(i
= 0; i
< LENGTH(rules
); i
++) {
306 if((!r
->title
|| strstr(c
->name
, r
->title
))
307 && (!r
->class || strstr(class, r
->class))
308 && (!r
->instance
|| strstr(instance
, r
->instance
)))
310 c
->isfloating
= r
->isfloating
;
312 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
321 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
325 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
329 /* set minimum possible */
337 if(*x
+ *w
+ 2 * c
->bw
< 0)
339 if(*y
+ *h
+ 2 * c
->bw
< 0)
343 if(*x
> m
->mx
+ m
->mw
)
344 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
345 if(*y
> m
->my
+ m
->mh
)
346 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
347 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
349 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
356 if(resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
357 /* see last two sentences in ICCCM 4.1.2.3 */
358 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
359 if(!baseismin
) { /* temporarily remove base dimensions */
363 /* adjust for aspect limits */
364 if(c
->mina
> 0 && c
->maxa
> 0) {
365 if(c
->maxa
< (float)*w
/ *h
)
366 *w
= *h
* c
->maxa
+ 0.5;
367 else if(c
->mina
< (float)*h
/ *w
)
368 *h
= *w
* c
->mina
+ 0.5;
370 if(baseismin
) { /* increment calculation requires this */
374 /* adjust for increment value */
379 /* restore base dimensions */
380 *w
= MAX(*w
+ c
->basew
, c
->minw
);
381 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
383 *w
= MIN(*w
, c
->maxw
);
385 *h
= MIN(*h
, c
->maxh
);
387 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
391 arrange(Monitor
*m
) {
394 else for(m
= mons
; m
; m
= m
->next
)
398 else for(m
= mons
; m
; m
= m
->next
)
403 arrangemon(Monitor
*m
) {
404 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
405 if(m
->lt
[m
->sellt
]->arrange
)
406 m
->lt
[m
->sellt
]->arrange(m
);
412 c
->next
= c
->mon
->clients
;
417 attachstack(Client
*c
) {
418 c
->snext
= c
->mon
->stack
;
423 buttonpress(XEvent
*e
) {
424 unsigned int i
, x
, click
;
428 XButtonPressedEvent
*ev
= &e
->xbutton
;
431 /* focus monitor if necessary */
432 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
433 unfocus(selmon
->sel
, True
);
437 if(ev
->window
== selmon
->barwin
) {
441 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
442 if(i
< LENGTH(tags
)) {
446 else if(ev
->x
< x
+ blw
)
448 else if(ev
->x
> selmon
->ww
- TEXTW(stext
))
449 click
= ClkStatusText
;
453 else if((c
= wintoclient(ev
->window
))) {
455 click
= ClkClientWin
;
457 for(i
= 0; i
< LENGTH(buttons
); i
++)
458 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
459 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
460 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
465 xerrorxlib
= XSetErrorHandler(xerrorstart
);
466 /* this causes an error if some other window manager is running */
467 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
469 XSetErrorHandler(xerror
);
476 Layout foo
= { "", NULL
};
480 selmon
->lt
[selmon
->sellt
] = &foo
;
481 for(m
= mons
; m
; m
= m
->next
)
483 unmanage(m
->stack
, False
);
485 XFreeFontSet(dpy
, dc
.font
.set
);
487 XFreeFont(dpy
, dc
.font
.xfont
);
488 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
489 XFreePixmap(dpy
, dc
.drawable
);
491 XFreeCursor(dpy
, cursor
[CurNormal
]);
492 XFreeCursor(dpy
, cursor
[CurResize
]);
493 XFreeCursor(dpy
, cursor
[CurMove
]);
497 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
501 cleanupmon(Monitor
*mon
) {
507 for(m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
510 XUnmapWindow(dpy
, mon
->barwin
);
511 XDestroyWindow(dpy
, mon
->barwin
);
516 clearurgent(Client
*c
) {
520 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
522 wmh
->flags
&= ~XUrgencyHint
;
523 XSetWMHints(dpy
, c
->win
, wmh
);
528 clientmessage(XEvent
*e
) {
529 XClientMessageEvent
*cme
= &e
->xclient
;
530 Client
*c
= wintoclient(cme
->window
);
534 if(cme
->message_type
== netatom
[NetWMState
] && cme
->data
.l
[1] == netatom
[NetWMFullscreen
]) {
535 if(cme
->data
.l
[0] && !c
->isfullscreen
) {
536 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
537 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
538 c
->isfullscreen
= True
;
539 c
->oldstate
= c
->isfloating
;
542 c
->isfloating
= True
;
543 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
544 XRaiseWindow(dpy
, c
->win
);
547 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
548 PropModeReplace
, (unsigned char*)0, 0);
549 c
->isfullscreen
= False
;
550 c
->isfloating
= c
->oldstate
;
556 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
560 else if(cme
->message_type
== netatom
[NetActiveWindow
]) {
562 c
->mon
->seltags
^= 1;
563 c
->mon
->tagset
[c
->mon
->seltags
] = c
->tags
;
570 configure(Client
*c
) {
573 ce
.type
= ConfigureNotify
;
581 ce
.border_width
= c
->bw
;
583 ce
.override_redirect
= False
;
584 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
588 configurenotify(XEvent
*e
) {
590 XConfigureEvent
*ev
= &e
->xconfigure
;
593 if(ev
->window
== root
) {
594 dirty
= (sw
!= ev
->width
);
597 if(updategeom() || dirty
) {
599 XFreePixmap(dpy
, dc
.drawable
);
600 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
602 for(m
= mons
; m
; m
= m
->next
)
603 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
611 configurerequest(XEvent
*e
) {
614 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
617 if((c
= wintoclient(ev
->window
))) {
618 if(ev
->value_mask
& CWBorderWidth
)
619 c
->bw
= ev
->border_width
;
620 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
622 if(ev
->value_mask
& CWX
) {
624 c
->x
= m
->mx
+ ev
->x
;
626 if(ev
->value_mask
& CWY
) {
628 c
->y
= m
->my
+ ev
->y
;
630 if(ev
->value_mask
& CWWidth
) {
634 if(ev
->value_mask
& CWHeight
) {
638 if((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
639 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
640 if((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
641 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
642 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
645 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
653 wc
.width
= ev
->width
;
654 wc
.height
= ev
->height
;
655 wc
.border_width
= ev
->border_width
;
656 wc
.sibling
= ev
->above
;
657 wc
.stack_mode
= ev
->detail
;
658 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
667 if(!(m
= (Monitor
*)calloc(1, sizeof(Monitor
))))
668 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
669 m
->tagset
[0] = m
->tagset
[1] = 1;
671 m
->nmaster
= nmaster
;
672 m
->showbar
= showbar
;
674 m
->lt
[0] = &layouts
[0];
675 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
676 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
681 destroynotify(XEvent
*e
) {
683 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
685 if((c
= wintoclient(ev
->window
)))
693 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
698 detachstack(Client
*c
) {
701 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
704 if(c
== c
->mon
->sel
) {
705 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
711 die(const char *errstr
, ...) {
714 va_start(ap
, errstr
);
715 vfprintf(stderr
, errstr
, ap
);
725 if(!(m
= selmon
->next
))
728 else if(selmon
== mons
)
729 for(m
= mons
; m
->next
; m
= m
->next
);
731 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
736 drawbar(Monitor
*m
) {
738 unsigned int i
, occ
= 0, urg
= 0;
742 for(c
= m
->clients
; c
; c
= c
->next
) {
748 for(i
= 0; i
< LENGTH(tags
); i
++) {
749 dc
.w
= TEXTW(tags
[i
]);
750 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
751 drawtext(tags
[i
], col
, urg
& 1 << i
);
752 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
753 occ
& 1 << i
, urg
& 1 << i
, col
);
756 dc
.w
= blw
= TEXTW(m
->ltsymbol
);
757 drawtext(m
->ltsymbol
, dc
.norm
, False
);
760 if(m
== selmon
) { /* status is only drawn on selected monitor */
767 drawtext(stext
, dc
.norm
, False
);
771 if((dc
.w
= dc
.x
- x
) > bh
) {
774 col
= m
== selmon
? dc
.sel
: dc
.norm
;
775 drawtext(m
->sel
->name
, col
, False
);
776 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
779 drawtext(NULL
, dc
.norm
, False
);
781 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
789 for(m
= mons
; m
; m
= m
->next
)
794 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
797 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
798 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
800 XFillRectangle(dpy
, dc
.drawable
, dc
.gc
, dc
.x
+1, dc
.y
+1, x
+1, x
+1);
802 XDrawRectangle(dpy
, dc
.drawable
, dc
.gc
, dc
.x
+1, dc
.y
+1, x
, x
);
806 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
808 int i
, x
, y
, h
, len
, olen
;
810 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
811 XFillRectangle(dpy
, dc
.drawable
, dc
.gc
, dc
.x
, dc
.y
, dc
.w
, dc
.h
);
815 h
= dc
.font
.ascent
+ dc
.font
.descent
;
816 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
818 /* shorten text if necessary */
819 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
822 memcpy(buf
, text
, len
);
824 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
825 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
827 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
829 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
833 enternotify(XEvent
*e
) {
836 XCrossingEvent
*ev
= &e
->xcrossing
;
838 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
840 c
= wintoclient(ev
->window
);
841 m
= c
? c
->mon
: wintomon(ev
->window
);
843 unfocus(selmon
->sel
, True
);
846 else if(!c
|| c
== selmon
->sel
)
854 XExposeEvent
*ev
= &e
->xexpose
;
856 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
862 if(!c
|| !ISVISIBLE(c
))
863 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
864 /* was if(selmon->sel) */
865 if(selmon
->sel
&& selmon
->sel
!= c
)
866 unfocus(selmon
->sel
, False
);
874 grabbuttons(c
, True
);
875 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
879 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
885 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
886 XFocusChangeEvent
*ev
= &e
->xfocus
;
888 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
889 setfocus(selmon
->sel
);
893 focusmon(const Arg
*arg
) {
898 if((m
= dirtomon(arg
->i
)) == selmon
)
900 unfocus(selmon
->sel
, True
);
906 focusstack(const Arg
*arg
) {
907 Client
*c
= NULL
, *i
;
912 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
914 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
917 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
921 for(; i
; i
= i
->next
)
932 getcolor(const char *colstr
) {
933 Colormap cmap
= DefaultColormap(dpy
, screen
);
936 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
937 die("error, cannot allocate color '%s'\n", colstr
);
942 getrootptr(int *x
, int *y
) {
947 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
954 unsigned char *p
= NULL
;
955 unsigned long n
, extra
;
958 if(XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
959 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
968 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
973 if(!text
|| size
== 0)
976 XGetTextProperty(dpy
, w
, &name
, atom
);
979 if(name
.encoding
== XA_STRING
)
980 strncpy(text
, (char *)name
.value
, size
- 1);
982 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
983 strncpy(text
, *list
, size
- 1);
984 XFreeStringList(list
);
987 text
[size
- 1] = '\0';
993 grabbuttons(Client
*c
, Bool focused
) {
997 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
998 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1000 for(i
= 0; i
< LENGTH(buttons
); i
++)
1001 if(buttons
[i
].click
== ClkClientWin
)
1002 for(j
= 0; j
< LENGTH(modifiers
); j
++)
1003 XGrabButton(dpy
, buttons
[i
].button
,
1004 buttons
[i
].mask
| modifiers
[j
],
1005 c
->win
, False
, BUTTONMASK
,
1006 GrabModeAsync
, GrabModeSync
, None
, None
);
1009 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
1010 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
1016 updatenumlockmask();
1019 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1022 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
1023 for(i
= 0; i
< LENGTH(keys
); i
++)
1024 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
1025 for(j
= 0; j
< LENGTH(modifiers
); j
++)
1026 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
1027 True
, GrabModeAsync
, GrabModeAsync
);
1032 incnmaster(const Arg
*arg
) {
1033 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
1038 initfont(const char *fontstr
) {
1039 char *def
, **missing
;
1042 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
1045 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
1046 XFreeStringList(missing
);
1049 XFontStruct
**xfonts
;
1052 dc
.font
.ascent
= dc
.font
.descent
= 0;
1053 XExtentsOfFontSet(dc
.font
.set
);
1054 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
1056 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
1057 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1062 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1063 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1064 die("error, cannot load font: '%s'\n", fontstr
);
1065 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1066 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1068 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1073 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
) {
1075 if(unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
1076 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
1080 #endif /* XINERAMA */
1083 keypress(XEvent
*e
) {
1089 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1090 for(i
= 0; i
< LENGTH(keys
); i
++)
1091 if(keysym
== keys
[i
].keysym
1092 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1094 keys
[i
].func(&(keys
[i
].arg
));
1098 killclient(const Arg
*arg
) {
1101 if(!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1103 XSetErrorHandler(xerrordummy
);
1104 XSetCloseDownMode(dpy
, DestroyAll
);
1105 XKillClient(dpy
, selmon
->sel
->win
);
1107 XSetErrorHandler(xerror
);
1113 manage(Window w
, XWindowAttributes
*wa
) {
1114 Client
*c
, *t
= NULL
;
1115 Window trans
= None
;
1118 if(!(c
= calloc(1, sizeof(Client
))))
1119 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1122 if(XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1131 c
->x
= c
->oldx
= wa
->x
;
1132 c
->y
= c
->oldy
= wa
->y
;
1133 c
->w
= c
->oldw
= wa
->width
;
1134 c
->h
= c
->oldh
= wa
->height
;
1135 c
->oldbw
= wa
->border_width
;
1136 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1137 c
->isfloating
= True
; // regression with flash, XXXX
1143 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1144 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1145 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1146 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1147 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1148 /* only fix client y-offset, if the client center might cover the bar */
1149 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1150 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1153 wc
.border_width
= c
->bw
;
1154 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1155 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1156 configure(c
); /* propagates border_width, if size doesn't change */
1157 updatewindowtype(c
);
1160 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1161 grabbuttons(c
, False
);
1163 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1165 XRaiseWindow(dpy
, c
->win
);
1168 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1169 setclientstate(c
, NormalState
);
1170 if (c
->mon
== selmon
)
1171 unfocus(selmon
->sel
, False
);
1174 XMapWindow(dpy
, c
->win
);
1179 mappingnotify(XEvent
*e
) {
1180 XMappingEvent
*ev
= &e
->xmapping
;
1182 XRefreshKeyboardMapping(ev
);
1183 if(ev
->request
== MappingKeyboard
)
1188 maprequest(XEvent
*e
) {
1189 static XWindowAttributes wa
;
1190 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1192 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1194 if(wa
.override_redirect
)
1196 if(!wintoclient(ev
->window
))
1197 manage(ev
->window
, &wa
);
1201 monocle(Monitor
*m
) {
1205 for(c
= m
->clients
; c
; c
= c
->next
)
1208 if(n
> 0) /* override layout symbol */
1209 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1210 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1211 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1215 movemouse(const Arg
*arg
) {
1216 int x
, y
, ocx
, ocy
, nx
, ny
;
1221 if(!(c
= selmon
->sel
))
1226 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1227 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1229 if(!getrootptr(&x
, &y
))
1232 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1234 case ConfigureRequest
:
1237 handler
[ev
.type
](&ev
);
1240 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1241 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1242 if(nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1243 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1244 if(abs(selmon
->wx
- nx
) < snap
)
1246 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1247 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1248 if(abs(selmon
->wy
- ny
) < snap
)
1250 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1251 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1252 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1253 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1254 togglefloating(NULL
);
1256 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1257 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1260 } while(ev
.type
!= ButtonRelease
);
1261 XUngrabPointer(dpy
, CurrentTime
);
1262 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1270 nexttiled(Client
*c
) {
1271 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1284 propertynotify(XEvent
*e
) {
1287 XPropertyEvent
*ev
= &e
->xproperty
;
1289 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1291 else if(ev
->state
== PropertyDelete
)
1292 return; /* ignore */
1293 else if((c
= wintoclient(ev
->window
))) {
1296 case XA_WM_TRANSIENT_FOR
:
1297 if(!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1298 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1301 case XA_WM_NORMAL_HINTS
:
1309 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1311 if(c
== c
->mon
->sel
)
1314 if(ev
->atom
== netatom
[NetWMWindowType
])
1315 updatewindowtype(c
);
1320 ptrtomon(int x
, int y
) {
1323 for(m
= mons
; m
; m
= m
->next
)
1324 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1330 quit(const Arg
*arg
) {
1335 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1336 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1337 resizeclient(c
, x
, y
, w
, h
);
1341 resizeclient(Client
*c
, int x
, int y
, int w
, int h
) {
1344 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1345 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1346 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1347 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1348 wc
.border_width
= c
->bw
;
1349 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1355 resizemouse(const Arg
*arg
) {
1362 if(!(c
= selmon
->sel
))
1367 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1368 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1370 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1372 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1374 case ConfigureRequest
:
1377 handler
[ev
.type
](&ev
);
1380 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1381 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1382 if(c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1383 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1385 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1386 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1387 togglefloating(NULL
);
1389 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1390 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1393 } while(ev
.type
!= ButtonRelease
);
1394 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1395 XUngrabPointer(dpy
, CurrentTime
);
1396 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1397 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1405 restack(Monitor
*m
) {
1413 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1414 XRaiseWindow(dpy
, m
->sel
->win
);
1415 if(m
->lt
[m
->sellt
]->arrange
) {
1416 wc
.stack_mode
= Below
;
1417 wc
.sibling
= m
->barwin
;
1418 for(c
= m
->stack
; c
; c
= c
->snext
)
1419 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1420 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1421 wc
.sibling
= c
->win
;
1425 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1431 /* main event loop */
1433 while(running
&& !XNextEvent(dpy
, &ev
))
1434 if(handler
[ev
.type
])
1435 handler
[ev
.type
](&ev
); /* call handler */
1440 unsigned int i
, num
;
1441 Window d1
, d2
, *wins
= NULL
;
1442 XWindowAttributes wa
;
1444 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1445 for(i
= 0; i
< num
; i
++) {
1446 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1447 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1449 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1450 manage(wins
[i
], &wa
);
1452 for(i
= 0; i
< num
; i
++) { /* now the transients */
1453 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1455 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1456 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1457 manage(wins
[i
], &wa
);
1465 sendmon(Client
*c
, Monitor
*m
) {
1472 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1480 setclientstate(Client
*c
, long state
) {
1481 long data
[] = { state
, None
};
1483 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1484 PropModeReplace
, (unsigned char *)data
, 2);
1488 sendevent(Client
*c
, Atom proto
) {
1491 Bool exists
= False
;
1494 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1495 while(!exists
&& n
--)
1496 exists
= protocols
[n
] == proto
;
1500 ev
.type
= ClientMessage
;
1501 ev
.xclient
.window
= c
->win
;
1502 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1503 ev
.xclient
.format
= 32;
1504 ev
.xclient
.data
.l
[0] = proto
;
1505 ev
.xclient
.data
.l
[1] = CurrentTime
;
1506 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1512 setfocus(Client
*c
) {
1514 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1515 sendevent(c
, wmatom
[WMTakeFocus
]);
1519 setlayout(const Arg
*arg
) {
1520 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1523 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1524 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1531 /* arg > 1.0 will set mfact absolutly */
1533 setmfact(const Arg
*arg
) {
1536 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1538 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1539 if(f
< 0.1 || f
> 0.9)
1547 XSetWindowAttributes wa
;
1549 /* clean up any zombies immediately */
1553 screen
= DefaultScreen(dpy
);
1554 root
= RootWindow(dpy
, screen
);
1556 sw
= DisplayWidth(dpy
, screen
);
1557 sh
= DisplayHeight(dpy
, screen
);
1558 bh
= dc
.h
= dc
.font
.height
+ 2;
1561 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1562 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1563 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1564 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1565 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1566 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1567 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1568 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1569 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1570 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1571 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1573 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1574 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1575 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1576 /* init appearance */
1577 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1578 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1579 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1580 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1581 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1582 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1583 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1584 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1585 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1587 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1591 /* EWMH support per view */
1592 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1593 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1594 /* select for events */
1595 wa
.cursor
= cursor
[CurNormal
];
1596 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1597 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1598 |PropertyChangeMask
;
1599 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1600 XSelectInput(dpy
, root
, wa
.event_mask
);
1605 showhide(Client
*c
) {
1608 if(ISVISIBLE(c
)) { /* show clients top down */
1609 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1610 if((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1611 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1614 else { /* hide clients bottom up */
1616 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1621 sigchld(int unused
) {
1622 if(signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1623 die("Can't install SIGCHLD handler");
1624 while(0 < waitpid(-1, NULL
, WNOHANG
));
1628 spawn(const Arg
*arg
) {
1631 close(ConnectionNumber(dpy
));
1633 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1634 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1641 tag(const Arg
*arg
) {
1642 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1643 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1650 tagmon(const Arg
*arg
) {
1651 if(!selmon
->sel
|| !mons
->next
)
1653 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1657 textnw(const char *text
, unsigned int len
) {
1661 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1664 return XTextWidth(dc
.font
.xfont
, text
, len
);
1669 unsigned int i
, n
, h
, mw
, my
, ty
;
1672 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1677 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1680 for(i
= my
= ty
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1681 if(i
< m
->nmaster
) {
1682 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
);
1683 resize(c
, m
->wx
, m
->wy
+ my
, mw
- (2*c
->bw
), h
- (2*c
->bw
), False
);
1687 h
= (m
->wh
- ty
) / (n
- i
);
1688 resize(c
, m
->wx
+ mw
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
), h
- (2*c
->bw
), False
);
1694 togglebar(const Arg
*arg
) {
1695 selmon
->showbar
= !selmon
->showbar
;
1696 updatebarpos(selmon
);
1697 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1702 togglefloating(const Arg
*arg
) {
1705 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1706 if(selmon
->sel
->isfloating
)
1707 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1708 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1713 toggletag(const Arg
*arg
) {
1714 unsigned int newtags
;
1718 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1720 selmon
->sel
->tags
= newtags
;
1727 toggleview(const Arg
*arg
) {
1728 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1731 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1738 unfocus(Client
*c
, Bool setfocus
) {
1741 grabbuttons(c
, False
);
1742 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1744 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1748 unmanage(Client
*c
, Bool destroyed
) {
1749 Monitor
*m
= c
->mon
;
1752 /* The server grab construct avoids race conditions. */
1756 wc
.border_width
= c
->oldbw
;
1758 XSetErrorHandler(xerrordummy
);
1759 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1760 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1761 setclientstate(c
, WithdrawnState
);
1763 XSetErrorHandler(xerror
);
1772 unmapnotify(XEvent
*e
) {
1774 XUnmapEvent
*ev
= &e
->xunmap
;
1776 if((c
= wintoclient(ev
->window
))) {
1778 setclientstate(c
, WithdrawnState
);
1787 XSetWindowAttributes wa
= {
1788 .override_redirect
= True
,
1789 .background_pixmap
= ParentRelative
,
1790 .event_mask
= ButtonPressMask
|ExposureMask
1792 for(m
= mons
; m
; m
= m
->next
) {
1793 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1794 CopyFromParent
, DefaultVisual(dpy
, screen
),
1795 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1796 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1797 XMapRaised(dpy
, m
->barwin
);
1802 updatebarpos(Monitor
*m
) {
1807 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1808 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1819 if(XineramaIsActive(dpy
)) {
1823 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1824 XineramaScreenInfo
*unique
= NULL
;
1826 for(n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1827 /* only consider unique geometries as separate screens */
1828 if(!(unique
= (XineramaScreenInfo
*)malloc(sizeof(XineramaScreenInfo
) * nn
)))
1829 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo
) * nn
);
1830 for(i
= 0, j
= 0; i
< nn
; i
++)
1831 if(isuniquegeom(unique
, j
, &info
[i
]))
1832 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1836 for(i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1837 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1839 m
->next
= createmon();
1843 for(i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1845 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1846 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1850 m
->mx
= m
->wx
= unique
[i
].x_org
;
1851 m
->my
= m
->wy
= unique
[i
].y_org
;
1852 m
->mw
= m
->ww
= unique
[i
].width
;
1853 m
->mh
= m
->wh
= unique
[i
].height
;
1857 else { /* less monitors available nn < n */
1858 for(i
= nn
; i
< n
; i
++) {
1859 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1863 m
->clients
= c
->next
;
1877 #endif /* XINERAMA */
1878 /* default monitor setup */
1882 if(mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1884 mons
->mw
= mons
->ww
= sw
;
1885 mons
->mh
= mons
->wh
= sh
;
1891 selmon
= wintomon(root
);
1897 updatenumlockmask(void) {
1899 XModifierKeymap
*modmap
;
1902 modmap
= XGetModifierMapping(dpy
);
1903 for(i
= 0; i
< 8; i
++)
1904 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1905 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1906 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1907 numlockmask
= (1 << i
);
1908 XFreeModifiermap(modmap
);
1912 updatesizehints(Client
*c
) {
1916 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1917 /* size is uninitialized, ensure that size.flags aren't used */
1919 if(size
.flags
& PBaseSize
) {
1920 c
->basew
= size
.base_width
;
1921 c
->baseh
= size
.base_height
;
1923 else if(size
.flags
& PMinSize
) {
1924 c
->basew
= size
.min_width
;
1925 c
->baseh
= size
.min_height
;
1928 c
->basew
= c
->baseh
= 0;
1929 if(size
.flags
& PResizeInc
) {
1930 c
->incw
= size
.width_inc
;
1931 c
->inch
= size
.height_inc
;
1934 c
->incw
= c
->inch
= 0;
1935 if(size
.flags
& PMaxSize
) {
1936 c
->maxw
= size
.max_width
;
1937 c
->maxh
= size
.max_height
;
1940 c
->maxw
= c
->maxh
= 0;
1941 if(size
.flags
& PMinSize
) {
1942 c
->minw
= size
.min_width
;
1943 c
->minh
= size
.min_height
;
1945 else if(size
.flags
& PBaseSize
) {
1946 c
->minw
= size
.base_width
;
1947 c
->minh
= size
.base_height
;
1950 c
->minw
= c
->minh
= 0;
1951 if(size
.flags
& PAspect
) {
1952 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1953 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1956 c
->maxa
= c
->mina
= 0.0;
1957 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1958 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1962 updatetitle(Client
*c
) {
1963 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1964 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1965 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1966 strcpy(c
->name
, broken
);
1970 updatestatus(void) {
1971 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1972 strcpy(stext
, "dwm-"VERSION
);
1977 updatewindowtype(Client
*c
)
1981 unsigned long n
, extra
;
1982 unsigned char *p
= NULL
;
1984 if(XGetWindowProperty(dpy
, c
->win
, netatom
[NetWMWindowType
], 0L,
1985 sizeof(Atom
), False
, XA_ATOM
, &real
, &format
,
1986 &n
, &extra
, (unsigned char **)&p
) == Success
&& p
) {
1990 if(wtype
== netatom
[NetWMWindowTypeDialog
])
1991 c
->isfloating
= True
;
1996 updatewmhints(Client
*c
) {
1999 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
2000 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2001 wmh
->flags
&= ~XUrgencyHint
;
2002 XSetWMHints(dpy
, c
->win
, wmh
);
2005 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
2006 if(wmh
->flags
& InputHint
)
2007 c
->neverfocus
= !wmh
->input
;
2009 c
->neverfocus
= False
;
2015 view(const Arg
*arg
) {
2016 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2018 selmon
->seltags
^= 1; /* toggle sel tagset */
2019 if(arg
->ui
& TAGMASK
)
2020 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2026 wintoclient(Window w
) {
2030 for(m
= mons
; m
; m
= m
->next
)
2031 for(c
= m
->clients
; c
; c
= c
->next
)
2038 wintomon(Window w
) {
2043 if(w
== root
&& getrootptr(&x
, &y
))
2044 return ptrtomon(x
, y
);
2045 for(m
= mons
; m
; m
= m
->next
)
2048 if((c
= wintoclient(w
)))
2053 /* There's no way to check accesses to destroyed windows, thus those cases are
2054 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2055 * default error handler, which may call exit. */
2057 xerror(Display
*dpy
, XErrorEvent
*ee
) {
2058 if(ee
->error_code
== BadWindow
2059 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2060 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2061 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2062 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2063 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2064 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2065 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2066 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2068 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2069 ee
->request_code
, ee
->error_code
);
2070 return xerrorxlib(dpy
, ee
); /* may call exit */
2074 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
2078 /* Startup Error handler to check if another window manager
2079 * is already running. */
2081 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
2082 die("dwm: another window manager is already running\n");
2087 zoom(const Arg
*arg
) {
2088 Client
*c
= selmon
->sel
;
2090 if(!selmon
->lt
[selmon
->sellt
]->arrange
2091 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2093 if(c
== nexttiled(selmon
->clients
))
2094 if(!c
|| !(c
= nexttiled(c
->next
)))
2100 main(int argc
, char *argv
[]) {
2101 if(argc
== 2 && !strcmp("-v", argv
[1]))
2102 die("dwm-"VERSION
", © 2006-2011 dwm engineers, see LICENSE for details\n");
2104 die("usage: dwm [-v]\n");
2105 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2106 fputs("warning: no locale support\n", stderr
);
2107 if(!(dpy
= XOpenDisplay(NULL
)))
2108 die("dwm: cannot open display\n");
2115 return EXIT_SUCCESS
;