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
, NetLast
}; /* EWMH atoms */
62 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, 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 oldx
, oldy
, oldw
, oldh
;
88 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
91 Bool isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
;
100 unsigned long norm
[ColLast
];
101 unsigned long sel
[ColLast
];
111 } DC
; /* draw context */
116 void (*func
)(const Arg
*);
122 void (*arrange
)(Monitor
*);
129 int by
; /* bar geometry */
130 int mx
, my
, mw
, mh
; /* screen size */
131 int wx
, wy
, ww
, wh
; /* window area */
132 unsigned int seltags
;
134 unsigned int tagset
[2];
147 const char *instance
;
154 /* function declarations */
155 static void applyrules(Client
*c
);
156 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
157 static void arrange(Monitor
*m
);
158 static void arrangemon(Monitor
*m
);
159 static void attach(Client
*c
);
160 static void attachstack(Client
*c
);
161 static void buttonpress(XEvent
*e
);
162 static void checkotherwm(void);
163 static void cleanup(void);
164 static void cleanupmon(Monitor
*mon
);
165 static void clearurgent(Client
*c
);
166 static void clientmessage(XEvent
*e
);
167 static void configure(Client
*c
);
168 static void configurenotify(XEvent
*e
);
169 static void configurerequest(XEvent
*e
);
170 static Monitor
*createmon(void);
171 static void destroynotify(XEvent
*e
);
172 static void detach(Client
*c
);
173 static void detachstack(Client
*c
);
174 static void die(const char *errstr
, ...);
175 static Monitor
*dirtomon(int dir
);
176 static void drawbar(Monitor
*m
);
177 static void drawbars(void);
178 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
179 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
180 static void enternotify(XEvent
*e
);
181 static void expose(XEvent
*e
);
182 static void focus(Client
*c
);
183 static void focusin(XEvent
*e
);
184 static void focusmon(const Arg
*arg
);
185 static void focusstack(const Arg
*arg
);
186 static unsigned long getcolor(const char *colstr
);
187 static Bool
getrootptr(int *x
, int *y
);
188 static long getstate(Window w
);
189 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
190 static void grabbuttons(Client
*c
, Bool focused
);
191 static void grabkeys(void);
192 static void initfont(const char *fontstr
);
193 static void keypress(XEvent
*e
);
194 static void killclient(const Arg
*arg
);
195 static void manage(Window w
, XWindowAttributes
*wa
);
196 static void mappingnotify(XEvent
*e
);
197 static void maprequest(XEvent
*e
);
198 static void monocle(Monitor
*m
);
199 static void movemouse(const Arg
*arg
);
200 static Client
*nexttiled(Client
*c
);
201 static void pop(Client
*);
202 static void propertynotify(XEvent
*e
);
203 static Monitor
*ptrtomon(int x
, int y
);
204 static void quit(const Arg
*arg
);
205 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
206 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
207 static void resizemouse(const Arg
*arg
);
208 static void restack(Monitor
*m
);
209 static void run(void);
210 static void scan(void);
211 static Bool
sendevent(Client
*c
, Atom proto
);
212 static void sendmon(Client
*c
, Monitor
*m
);
213 static void setclientstate(Client
*c
, long state
);
214 static void setfocus(Client
*c
);
215 static void setlayout(const Arg
*arg
);
216 static void setmfact(const Arg
*arg
);
217 static void setup(void);
218 static void showhide(Client
*c
);
219 static void sigchld(int unused
);
220 static void spawn(const Arg
*arg
);
221 static void tag(const Arg
*arg
);
222 static void tagmon(const Arg
*arg
);
223 static int textnw(const char *text
, unsigned int len
);
224 static void tile(Monitor
*);
225 static void togglebar(const Arg
*arg
);
226 static void togglefloating(const Arg
*arg
);
227 static void toggletag(const Arg
*arg
);
228 static void toggleview(const Arg
*arg
);
229 static void unfocus(Client
*c
, Bool setfocus
);
230 static void unmanage(Client
*c
, Bool destroyed
);
231 static void unmapnotify(XEvent
*e
);
232 static Bool
updategeom(void);
233 static void updatebarpos(Monitor
*m
);
234 static void updatebars(void);
235 static void updatenumlockmask(void);
236 static void updatesizehints(Client
*c
);
237 static void updatestatus(void);
238 static void updatetitle(Client
*c
);
239 static void updatewmhints(Client
*c
);
240 static void view(const Arg
*arg
);
241 static Client
*wintoclient(Window w
);
242 static Monitor
*wintomon(Window w
);
243 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
244 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
245 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
246 static void zoom(const Arg
*arg
);
249 static const char broken
[] = "broken";
250 static char stext
[256];
252 static int sw
, sh
; /* X display screen geometry width, height */
253 static int bh
, blw
= 0; /* bar geometry */
254 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
255 static unsigned int numlockmask
= 0;
256 static void (*handler
[LASTEvent
]) (XEvent
*) = {
257 [ButtonPress
] = buttonpress
,
258 [ClientMessage
] = clientmessage
,
259 [ConfigureRequest
] = configurerequest
,
260 [ConfigureNotify
] = configurenotify
,
261 [DestroyNotify
] = destroynotify
,
262 [EnterNotify
] = enternotify
,
265 [KeyPress
] = keypress
,
266 [MappingNotify
] = mappingnotify
,
267 [MapRequest
] = maprequest
,
268 [PropertyNotify
] = propertynotify
,
269 [UnmapNotify
] = unmapnotify
271 static Atom wmatom
[WMLast
], netatom
[NetLast
];
272 static Bool running
= True
;
273 static Cursor cursor
[CurLast
];
276 static Monitor
*mons
= NULL
, *selmon
= NULL
;
279 /* configuration, allows nested code to access above variables */
282 /* compile-time check if all tags fit into an unsigned int bit array. */
283 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
285 /* function implementations */
287 applyrules(Client
*c
) {
288 const char *class, *instance
;
292 XClassHint ch
= { 0 };
295 c
->isfloating
= c
->tags
= 0;
296 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
297 class = ch
.res_class
? ch
.res_class
: broken
;
298 instance
= ch
.res_name
? ch
.res_name
: broken
;
299 for(i
= 0; i
< LENGTH(rules
); i
++) {
301 if((!r
->title
|| strstr(c
->name
, r
->title
))
302 && (!r
->class || strstr(class, r
->class))
303 && (!r
->instance
|| strstr(instance
, r
->instance
)))
305 c
->isfloating
= r
->isfloating
;
307 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
317 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
321 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
325 /* set minimum possible */
333 if(*x
+ *w
+ 2 * c
->bw
< 0)
335 if(*y
+ *h
+ 2 * c
->bw
< 0)
339 if(*x
> m
->mx
+ m
->mw
)
340 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
341 if(*y
> m
->my
+ m
->mh
)
342 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
343 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
345 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
352 if(resizehints
|| c
->isfloating
) {
353 /* see last two sentences in ICCCM 4.1.2.3 */
354 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
355 if(!baseismin
) { /* temporarily remove base dimensions */
359 /* adjust for aspect limits */
360 if(c
->mina
> 0 && c
->maxa
> 0) {
361 if(c
->maxa
< (float)*w
/ *h
)
362 *w
= *h
* c
->maxa
+ 0.5;
363 else if(c
->mina
< (float)*h
/ *w
)
364 *h
= *w
* c
->mina
+ 0.5;
366 if(baseismin
) { /* increment calculation requires this */
370 /* adjust for increment value */
375 /* restore base dimensions */
376 *w
= MAX(*w
+ c
->basew
, c
->minw
);
377 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
379 *w
= MIN(*w
, c
->maxw
);
381 *h
= MIN(*h
, c
->maxh
);
383 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
387 arrange(Monitor
*m
) {
390 else for(m
= mons
; m
; m
= m
->next
)
394 else for(m
= mons
; m
; m
= m
->next
)
399 arrangemon(Monitor
*m
) {
400 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
401 if(m
->lt
[m
->sellt
]->arrange
)
402 m
->lt
[m
->sellt
]->arrange(m
);
408 c
->next
= c
->mon
->clients
;
413 attachstack(Client
*c
) {
414 c
->snext
= c
->mon
->stack
;
419 buttonpress(XEvent
*e
) {
420 unsigned int i
, x
, click
;
424 XButtonPressedEvent
*ev
= &e
->xbutton
;
427 /* focus monitor if necessary */
428 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
429 unfocus(selmon
->sel
, True
);
433 if(ev
->window
== selmon
->barwin
) {
437 } while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
438 if(i
< LENGTH(tags
)) {
442 else if(ev
->x
< x
+ blw
)
444 else if(ev
->x
> selmon
->ww
- TEXTW(stext
))
445 click
= ClkStatusText
;
449 else if((c
= wintoclient(ev
->window
))) {
451 click
= ClkClientWin
;
453 for(i
= 0; i
< LENGTH(buttons
); i
++)
454 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
455 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
456 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
461 xerrorxlib
= XSetErrorHandler(xerrorstart
);
462 /* this causes an error if some other window manager is running */
463 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
465 XSetErrorHandler(xerror
);
472 Layout foo
= { "", NULL
};
476 selmon
->lt
[selmon
->sellt
] = &foo
;
477 for(m
= mons
; m
; m
= m
->next
)
479 unmanage(m
->stack
, False
);
481 XFreeFontSet(dpy
, dc
.font
.set
);
483 XFreeFont(dpy
, dc
.font
.xfont
);
484 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
485 XFreePixmap(dpy
, dc
.drawable
);
487 XFreeCursor(dpy
, cursor
[CurNormal
]);
488 XFreeCursor(dpy
, cursor
[CurResize
]);
489 XFreeCursor(dpy
, cursor
[CurMove
]);
493 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
497 cleanupmon(Monitor
*mon
) {
503 for(m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
506 XUnmapWindow(dpy
, mon
->barwin
);
507 XDestroyWindow(dpy
, mon
->barwin
);
512 clearurgent(Client
*c
) {
516 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
518 wmh
->flags
&= ~XUrgencyHint
;
519 XSetWMHints(dpy
, c
->win
, wmh
);
524 clientmessage(XEvent
*e
) {
525 XClientMessageEvent
*cme
= &e
->xclient
;
526 Client
*c
= wintoclient(cme
->window
);
530 if(cme
->message_type
== netatom
[NetWMState
] && cme
->data
.l
[1] == netatom
[NetWMFullscreen
]) {
531 if(cme
->data
.l
[0] && !c
->isfullscreen
) {
532 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
533 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
534 c
->isfullscreen
= True
;
535 c
->oldstate
= c
->isfloating
;
538 c
->isfloating
= True
;
539 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
540 XRaiseWindow(dpy
, c
->win
);
543 XChangeProperty(dpy
, cme
->window
, netatom
[NetWMState
], XA_ATOM
, 32,
544 PropModeReplace
, (unsigned char*)0, 0);
545 c
->isfullscreen
= False
;
546 c
->isfloating
= c
->oldstate
;
552 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
556 else if(cme
->message_type
== netatom
[NetActiveWindow
]) {
558 c
->mon
->seltags
^= 1;
559 c
->mon
->tagset
[c
->mon
->seltags
] = c
->tags
;
566 configure(Client
*c
) {
569 ce
.type
= ConfigureNotify
;
577 ce
.border_width
= c
->bw
;
579 ce
.override_redirect
= False
;
580 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
584 configurenotify(XEvent
*e
) {
586 XConfigureEvent
*ev
= &e
->xconfigure
;
589 if(ev
->window
== root
) {
590 dirty
= (sw
!= ev
->width
);
593 if(updategeom() || dirty
) {
595 XFreePixmap(dpy
, dc
.drawable
);
596 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
598 for(m
= mons
; m
; m
= m
->next
)
599 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
607 configurerequest(XEvent
*e
) {
610 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
613 if((c
= wintoclient(ev
->window
))) {
614 if(ev
->value_mask
& CWBorderWidth
)
615 c
->bw
= ev
->border_width
;
616 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
618 if(ev
->value_mask
& CWX
)
619 c
->x
= m
->mx
+ ev
->x
;
620 if(ev
->value_mask
& CWY
)
621 c
->y
= m
->my
+ ev
->y
;
622 if(ev
->value_mask
& CWWidth
)
624 if(ev
->value_mask
& CWHeight
)
626 if((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
627 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
628 if((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
629 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
630 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
633 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
641 wc
.width
= ev
->width
;
642 wc
.height
= ev
->height
;
643 wc
.border_width
= ev
->border_width
;
644 wc
.sibling
= ev
->above
;
645 wc
.stack_mode
= ev
->detail
;
646 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
655 if(!(m
= (Monitor
*)calloc(1, sizeof(Monitor
))))
656 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
657 m
->tagset
[0] = m
->tagset
[1] = 1;
659 m
->showbar
= showbar
;
661 m
->lt
[0] = &layouts
[0];
662 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
663 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
668 destroynotify(XEvent
*e
) {
670 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
672 if((c
= wintoclient(ev
->window
)))
680 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
685 detachstack(Client
*c
) {
688 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
691 if(c
== c
->mon
->sel
) {
692 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
698 die(const char *errstr
, ...) {
701 va_start(ap
, errstr
);
702 vfprintf(stderr
, errstr
, ap
);
712 if(!(m
= selmon
->next
))
717 for(m
= mons
; m
->next
; m
= m
->next
);
719 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
725 drawbar(Monitor
*m
) {
727 unsigned int i
, occ
= 0, urg
= 0;
731 for(c
= m
->clients
; c
; c
= c
->next
) {
737 for(i
= 0; i
< LENGTH(tags
); i
++) {
738 dc
.w
= TEXTW(tags
[i
]);
739 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
740 drawtext(tags
[i
], col
, urg
& 1 << i
);
741 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
742 occ
& 1 << i
, urg
& 1 << i
, col
);
745 dc
.w
= blw
= TEXTW(m
->ltsymbol
);
746 drawtext(m
->ltsymbol
, dc
.norm
, False
);
749 if(m
== selmon
) { /* status is only drawn on selected monitor */
756 drawtext(stext
, dc
.norm
, False
);
760 if((dc
.w
= dc
.x
- x
) > bh
) {
763 col
= m
== selmon
? dc
.sel
: dc
.norm
;
764 drawtext(m
->sel
->name
, col
, False
);
765 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
768 drawtext(NULL
, dc
.norm
, False
);
770 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
778 for(m
= mons
; m
; m
= m
->next
)
783 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
786 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
787 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
789 XFillRectangle(dpy
, dc
.drawable
, dc
.gc
, dc
.x
+1, dc
.y
+1, x
+1, x
+1);
791 XDrawRectangle(dpy
, dc
.drawable
, dc
.gc
, dc
.x
+1, dc
.y
+1, x
, x
);
795 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
797 int i
, x
, y
, h
, len
, olen
;
799 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
800 XFillRectangle(dpy
, dc
.drawable
, dc
.gc
, dc
.x
, dc
.y
, dc
.w
, dc
.h
);
804 h
= dc
.font
.ascent
+ dc
.font
.descent
;
805 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
807 /* shorten text if necessary */
808 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
811 memcpy(buf
, text
, len
);
813 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
814 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
816 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
818 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
822 enternotify(XEvent
*e
) {
825 XCrossingEvent
*ev
= &e
->xcrossing
;
827 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
829 c
= wintoclient(ev
->window
);
830 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
831 unfocus(selmon
->sel
, True
);
834 else if(c
== selmon
->sel
|| c
== NULL
)
836 focus((wintoclient(ev
->window
)));
842 XExposeEvent
*ev
= &e
->xexpose
;
844 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
850 if(!c
|| !ISVISIBLE(c
))
851 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
852 /* was if(selmon->sel) */
853 if(selmon
->sel
&& selmon
->sel
!= c
)
854 unfocus(selmon
->sel
, False
);
862 grabbuttons(c
, True
);
863 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
867 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
873 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
874 XFocusChangeEvent
*ev
= &e
->xfocus
;
876 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
877 setfocus(selmon
->sel
);
881 focusmon(const Arg
*arg
) {
886 if((m
= dirtomon(arg
->i
)) == selmon
)
888 unfocus(selmon
->sel
, True
);
894 focusstack(const Arg
*arg
) {
895 Client
*c
= NULL
, *i
;
900 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
902 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
905 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
909 for(; i
; i
= i
->next
)
920 getcolor(const char *colstr
) {
921 Colormap cmap
= DefaultColormap(dpy
, screen
);
924 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
925 die("error, cannot allocate color '%s'\n", colstr
);
930 getrootptr(int *x
, int *y
) {
935 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
942 unsigned char *p
= NULL
;
943 unsigned long n
, extra
;
946 if(XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
947 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
956 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
961 if(!text
|| size
== 0)
964 XGetTextProperty(dpy
, w
, &name
, atom
);
967 if(name
.encoding
== XA_STRING
)
968 strncpy(text
, (char *)name
.value
, size
- 1);
970 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
971 strncpy(text
, *list
, size
- 1);
972 XFreeStringList(list
);
975 text
[size
- 1] = '\0';
981 grabbuttons(Client
*c
, Bool focused
) {
985 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
986 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
988 for(i
= 0; i
< LENGTH(buttons
); i
++)
989 if(buttons
[i
].click
== ClkClientWin
)
990 for(j
= 0; j
< LENGTH(modifiers
); j
++)
991 XGrabButton(dpy
, buttons
[i
].button
,
992 buttons
[i
].mask
| modifiers
[j
],
993 c
->win
, False
, BUTTONMASK
,
994 GrabModeAsync
, GrabModeSync
, None
, None
);
997 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
998 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
1004 updatenumlockmask();
1007 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1010 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
1011 for(i
= 0; i
< LENGTH(keys
); i
++) {
1012 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
1013 for(j
= 0; j
< LENGTH(modifiers
); j
++)
1014 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
1015 True
, GrabModeAsync
, GrabModeAsync
);
1021 initfont(const char *fontstr
) {
1022 char *def
, **missing
;
1026 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
1029 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
1030 XFreeStringList(missing
);
1033 XFontStruct
**xfonts
;
1036 dc
.font
.ascent
= dc
.font
.descent
= 0;
1037 XExtentsOfFontSet(dc
.font
.set
);
1038 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
1040 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
1041 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1046 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1047 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1048 die("error, cannot load font: '%s'\n", fontstr
);
1049 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1050 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1052 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1057 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
) {
1059 if(unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
1060 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
1064 #endif /* XINERAMA */
1067 keypress(XEvent
*e
) {
1073 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1074 for(i
= 0; i
< LENGTH(keys
); i
++)
1075 if(keysym
== keys
[i
].keysym
1076 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1078 keys
[i
].func(&(keys
[i
].arg
));
1082 killclient(const Arg
*arg
) {
1085 if(!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1087 XSetErrorHandler(xerrordummy
);
1088 XSetCloseDownMode(dpy
, DestroyAll
);
1089 XKillClient(dpy
, selmon
->sel
->win
);
1091 XSetErrorHandler(xerror
);
1097 manage(Window w
, XWindowAttributes
*wa
) {
1098 Client
*c
, *t
= NULL
;
1099 Window trans
= None
;
1102 if(!(c
= calloc(1, sizeof(Client
))))
1103 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1106 if(XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1115 c
->x
= c
->oldx
= wa
->x
+ c
->mon
->wx
;
1116 c
->y
= c
->oldy
= wa
->y
+ c
->mon
->wy
;
1117 c
->w
= c
->oldw
= wa
->width
;
1118 c
->h
= c
->oldh
= wa
->height
;
1119 c
->oldbw
= wa
->border_width
;
1120 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1121 c
->isfloating
= True
;
1127 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1128 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1129 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1130 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1131 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1132 /* only fix client y-offset, if the client center might cover the bar */
1133 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1134 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1137 wc
.border_width
= c
->bw
;
1138 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1139 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1140 configure(c
); /* propagates border_width, if size doesn't change */
1143 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1144 grabbuttons(c
, False
);
1146 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1148 XRaiseWindow(dpy
, c
->win
);
1151 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1152 setclientstate(c
, NormalState
);
1154 XMapWindow(dpy
, c
->win
);
1159 mappingnotify(XEvent
*e
) {
1160 XMappingEvent
*ev
= &e
->xmapping
;
1162 XRefreshKeyboardMapping(ev
);
1163 if(ev
->request
== MappingKeyboard
)
1168 maprequest(XEvent
*e
) {
1169 static XWindowAttributes wa
;
1170 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1172 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1174 if(wa
.override_redirect
)
1176 if(!wintoclient(ev
->window
))
1177 manage(ev
->window
, &wa
);
1181 monocle(Monitor
*m
) {
1185 for(c
= m
->clients
; c
; c
= c
->next
)
1188 if(n
> 0) /* override layout symbol */
1189 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1190 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1191 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1195 movemouse(const Arg
*arg
) {
1196 int x
, y
, ocx
, ocy
, nx
, ny
;
1201 if(!(c
= selmon
->sel
))
1206 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1207 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1209 if(!getrootptr(&x
, &y
))
1212 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1214 case ConfigureRequest
:
1217 handler
[ev
.type
](&ev
);
1220 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1221 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1222 if(nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1223 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1224 if(abs(selmon
->wx
- nx
) < snap
)
1226 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1227 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1228 if(abs(selmon
->wy
- ny
) < snap
)
1230 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1231 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1232 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1233 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1234 togglefloating(NULL
);
1236 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1237 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1240 } while(ev
.type
!= ButtonRelease
);
1241 XUngrabPointer(dpy
, CurrentTime
);
1242 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1250 nexttiled(Client
*c
) {
1251 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1264 propertynotify(XEvent
*e
) {
1267 XPropertyEvent
*ev
= &e
->xproperty
;
1269 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1271 else if(ev
->state
== PropertyDelete
)
1272 return; /* ignore */
1273 else if((c
= wintoclient(ev
->window
))) {
1276 case XA_WM_TRANSIENT_FOR
:
1277 if(!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1278 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1281 case XA_WM_NORMAL_HINTS
:
1289 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1291 if(c
== c
->mon
->sel
)
1298 ptrtomon(int x
, int y
) {
1301 for(m
= mons
; m
; m
= m
->next
)
1302 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1307 quit(const Arg
*arg
) {
1312 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1313 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1314 resizeclient(c
, x
, y
, w
, h
);
1318 resizeclient(Client
*c
, int x
, int y
, int w
, int h
) {
1321 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1322 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1323 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1324 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1325 wc
.border_width
= c
->bw
;
1326 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1332 resizemouse(const Arg
*arg
) {
1339 if(!(c
= selmon
->sel
))
1344 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1345 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1347 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1349 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1351 case ConfigureRequest
:
1354 handler
[ev
.type
](&ev
);
1357 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1358 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1359 if(c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1360 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1362 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1363 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1364 togglefloating(NULL
);
1366 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1367 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1370 } while(ev
.type
!= ButtonRelease
);
1371 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1372 XUngrabPointer(dpy
, CurrentTime
);
1373 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1374 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1382 restack(Monitor
*m
) {
1390 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1391 XRaiseWindow(dpy
, m
->sel
->win
);
1392 if(m
->lt
[m
->sellt
]->arrange
) {
1393 wc
.stack_mode
= Below
;
1394 wc
.sibling
= m
->barwin
;
1395 for(c
= m
->stack
; c
; c
= c
->snext
)
1396 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1397 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1398 wc
.sibling
= c
->win
;
1402 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1408 /* main event loop */
1410 while(running
&& !XNextEvent(dpy
, &ev
)) {
1411 if(handler
[ev
.type
])
1412 handler
[ev
.type
](&ev
); /* call handler */
1418 unsigned int i
, num
;
1419 Window d1
, d2
, *wins
= NULL
;
1420 XWindowAttributes wa
;
1422 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1423 for(i
= 0; i
< num
; i
++) {
1424 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1425 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1427 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1428 manage(wins
[i
], &wa
);
1430 for(i
= 0; i
< num
; i
++) { /* now the transients */
1431 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1433 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1434 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1435 manage(wins
[i
], &wa
);
1443 sendmon(Client
*c
, Monitor
*m
) {
1450 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1458 setclientstate(Client
*c
, long state
) {
1459 long data
[] = { state
, None
};
1461 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1462 PropModeReplace
, (unsigned char *)data
, 2);
1466 sendevent(Client
*c
, Atom proto
) {
1469 Bool exists
= False
;
1472 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1473 while(!exists
&& n
--)
1474 exists
= protocols
[n
] == proto
;
1478 ev
.type
= ClientMessage
;
1479 ev
.xclient
.window
= c
->win
;
1480 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1481 ev
.xclient
.format
= 32;
1482 ev
.xclient
.data
.l
[0] = proto
;
1483 ev
.xclient
.data
.l
[1] = CurrentTime
;
1484 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1490 setfocus(Client
*c
) {
1492 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1493 sendevent(c
, wmatom
[WMTakeFocus
]);
1497 setlayout(const Arg
*arg
) {
1498 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1501 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1502 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1509 /* arg > 1.0 will set mfact absolutly */
1511 setmfact(const Arg
*arg
) {
1514 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1516 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1517 if(f
< 0.1 || f
> 0.9)
1525 XSetWindowAttributes wa
;
1527 /* clean up any zombies immediately */
1531 screen
= DefaultScreen(dpy
);
1532 root
= RootWindow(dpy
, screen
);
1534 sw
= DisplayWidth(dpy
, screen
);
1535 sh
= DisplayHeight(dpy
, screen
);
1536 bh
= dc
.h
= dc
.font
.height
+ 2;
1539 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1540 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1541 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1542 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1543 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1544 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1545 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1546 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1547 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1549 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1550 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1551 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1552 /* init appearance */
1553 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1554 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1555 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1556 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1557 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1558 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1559 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1560 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1561 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1563 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1567 /* EWMH support per view */
1568 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1569 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1570 /* select for events */
1571 wa
.cursor
= cursor
[CurNormal
];
1572 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1573 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1574 |PropertyChangeMask
;
1575 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1576 XSelectInput(dpy
, root
, wa
.event_mask
);
1581 showhide(Client
*c
) {
1584 if(ISVISIBLE(c
)) { /* show clients top down */
1585 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1586 if((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1587 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1590 else { /* hide clients bottom up */
1592 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1597 sigchld(int unused
) {
1598 if(signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1599 die("Can't install SIGCHLD handler");
1600 while(0 < waitpid(-1, NULL
, WNOHANG
));
1604 spawn(const Arg
*arg
) {
1607 close(ConnectionNumber(dpy
));
1609 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1610 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1617 tag(const Arg
*arg
) {
1618 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1619 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1626 tagmon(const Arg
*arg
) {
1627 if(!selmon
->sel
|| !mons
->next
)
1629 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1633 textnw(const char *text
, unsigned int len
) {
1637 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1640 return XTextWidth(dc
.font
.xfont
, text
, len
);
1649 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1653 c
= nexttiled(m
->clients
);
1654 mw
= m
->mfact
* m
->ww
;
1655 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1659 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1661 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1665 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1666 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1667 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1669 y
= c
->y
+ HEIGHT(c
);
1674 togglebar(const Arg
*arg
) {
1675 selmon
->showbar
= !selmon
->showbar
;
1676 updatebarpos(selmon
);
1677 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1682 togglefloating(const Arg
*arg
) {
1685 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1686 if(selmon
->sel
->isfloating
)
1687 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1688 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1693 toggletag(const Arg
*arg
) {
1694 unsigned int newtags
;
1698 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1700 selmon
->sel
->tags
= newtags
;
1707 toggleview(const Arg
*arg
) {
1708 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1711 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1718 unfocus(Client
*c
, Bool setfocus
) {
1721 grabbuttons(c
, False
);
1722 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1724 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1728 unmanage(Client
*c
, Bool destroyed
) {
1729 Monitor
*m
= c
->mon
;
1732 /* The server grab construct avoids race conditions. */
1736 wc
.border_width
= c
->oldbw
;
1738 XSetErrorHandler(xerrordummy
);
1739 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1740 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1741 setclientstate(c
, WithdrawnState
);
1743 XSetErrorHandler(xerror
);
1752 unmapnotify(XEvent
*e
) {
1754 XUnmapEvent
*ev
= &e
->xunmap
;
1756 if((c
= wintoclient(ev
->window
)))
1763 XSetWindowAttributes wa
= {
1764 .override_redirect
= True
,
1765 .background_pixmap
= ParentRelative
,
1766 .event_mask
= ButtonPressMask
|ExposureMask
1768 for(m
= mons
; m
; m
= m
->next
) {
1769 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1770 CopyFromParent
, DefaultVisual(dpy
, screen
),
1771 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1772 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1773 XMapRaised(dpy
, m
->barwin
);
1778 updatebarpos(Monitor
*m
) {
1783 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1784 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1795 if(XineramaIsActive(dpy
)) {
1799 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1800 XineramaScreenInfo
*unique
= NULL
;
1802 for(n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1803 /* only consider unique geometries as separate screens */
1804 if(!(unique
= (XineramaScreenInfo
*)malloc(sizeof(XineramaScreenInfo
) * nn
)))
1805 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo
) * nn
);
1806 for(i
= 0, j
= 0; i
< nn
; i
++)
1807 if(isuniquegeom(unique
, j
, &info
[i
]))
1808 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1812 for(i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1813 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1815 m
->next
= createmon();
1819 for(i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1821 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1822 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1826 m
->mx
= m
->wx
= unique
[i
].x_org
;
1827 m
->my
= m
->wy
= unique
[i
].y_org
;
1828 m
->mw
= m
->ww
= unique
[i
].width
;
1829 m
->mh
= m
->wh
= unique
[i
].height
;
1833 else { /* less monitors available nn < n */
1834 for(i
= nn
; i
< n
; i
++) {
1835 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1839 m
->clients
= c
->next
;
1853 #endif /* XINERAMA */
1854 /* default monitor setup */
1858 if(mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1860 mons
->mw
= mons
->ww
= sw
;
1861 mons
->mh
= mons
->wh
= sh
;
1867 selmon
= wintomon(root
);
1873 updatenumlockmask(void) {
1875 XModifierKeymap
*modmap
;
1878 modmap
= XGetModifierMapping(dpy
);
1879 for(i
= 0; i
< 8; i
++)
1880 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1881 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1882 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1883 numlockmask
= (1 << i
);
1884 XFreeModifiermap(modmap
);
1888 updatesizehints(Client
*c
) {
1892 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1893 /* size is uninitialized, ensure that size.flags aren't used */
1895 if(size
.flags
& PBaseSize
) {
1896 c
->basew
= size
.base_width
;
1897 c
->baseh
= size
.base_height
;
1899 else if(size
.flags
& PMinSize
) {
1900 c
->basew
= size
.min_width
;
1901 c
->baseh
= size
.min_height
;
1904 c
->basew
= c
->baseh
= 0;
1905 if(size
.flags
& PResizeInc
) {
1906 c
->incw
= size
.width_inc
;
1907 c
->inch
= size
.height_inc
;
1910 c
->incw
= c
->inch
= 0;
1911 if(size
.flags
& PMaxSize
) {
1912 c
->maxw
= size
.max_width
;
1913 c
->maxh
= size
.max_height
;
1916 c
->maxw
= c
->maxh
= 0;
1917 if(size
.flags
& PMinSize
) {
1918 c
->minw
= size
.min_width
;
1919 c
->minh
= size
.min_height
;
1921 else if(size
.flags
& PBaseSize
) {
1922 c
->minw
= size
.base_width
;
1923 c
->minh
= size
.base_height
;
1926 c
->minw
= c
->minh
= 0;
1927 if(size
.flags
& PAspect
) {
1928 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1929 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1932 c
->maxa
= c
->mina
= 0.0;
1933 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1934 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1938 updatetitle(Client
*c
) {
1939 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1940 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1941 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1942 strcpy(c
->name
, broken
);
1946 updatestatus(void) {
1947 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1948 strcpy(stext
, "dwm-"VERSION
);
1953 updatewmhints(Client
*c
) {
1956 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1957 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1958 wmh
->flags
&= ~XUrgencyHint
;
1959 XSetWMHints(dpy
, c
->win
, wmh
);
1962 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1963 if(wmh
->flags
& InputHint
)
1964 c
->neverfocus
= !wmh
->input
;
1966 c
->neverfocus
= False
;
1972 view(const Arg
*arg
) {
1973 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1975 selmon
->seltags
^= 1; /* toggle sel tagset */
1976 if(arg
->ui
& TAGMASK
)
1977 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1983 wintoclient(Window w
) {
1987 for(m
= mons
; m
; m
= m
->next
)
1988 for(c
= m
->clients
; c
; c
= c
->next
)
1995 wintomon(Window w
) {
2000 if(w
== root
&& getrootptr(&x
, &y
))
2001 return ptrtomon(x
, y
);
2002 for(m
= mons
; m
; m
= m
->next
)
2005 if((c
= wintoclient(w
)))
2010 /* There's no way to check accesses to destroyed windows, thus those cases are
2011 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2012 * default error handler, which may call exit. */
2014 xerror(Display
*dpy
, XErrorEvent
*ee
) {
2015 if(ee
->error_code
== BadWindow
2016 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2017 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2018 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2019 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2020 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2021 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2022 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2023 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2025 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2026 ee
->request_code
, ee
->error_code
);
2027 return xerrorxlib(dpy
, ee
); /* may call exit */
2031 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
2035 /* Startup Error handler to check if another window manager
2036 * is already running. */
2038 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
2039 die("dwm: another window manager is already running\n");
2044 zoom(const Arg
*arg
) {
2045 Client
*c
= selmon
->sel
;
2047 if(!selmon
->lt
[selmon
->sellt
]->arrange
2048 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2050 if(c
== nexttiled(selmon
->clients
))
2051 if(!c
|| !(c
= nexttiled(c
->next
)))
2057 main(int argc
, char *argv
[]) {
2058 if(argc
== 2 && !strcmp("-v", argv
[1]))
2059 die("dwm-"VERSION
", © 2006-2011 dwm engineers, see LICENSE for details\n");
2061 die("usage: dwm [-v]\n");
2062 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2063 fputs("warning: no locale support\n", stderr
);
2064 if(!(dpy
= XOpenDisplay(NULL
)))
2065 die("dwm: cannot open display\n");
2072 return EXIT_SUCCESS
;