Xinqi Bao's Git
6695ddfb63e8f8be6dbee574f9cae0f4b4f522e0
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>
39 #include <X11/XKBlib.h>
41 #include <X11/extensions/Xinerama.h>
47 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
48 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
49 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
50 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
51 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
52 #define LENGTH(X) (sizeof X / sizeof X[0])
53 #define MAX(A, B) ((A) > (B) ? (A) : (B))
54 #define MIN(A, B) ((A) < (B) ? (A) : (B))
55 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
56 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
57 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
58 #define TAGMASK ((1 << LENGTH(tags)) - 1)
59 #define TEXTW(X) (textnw(X, strlen(X)) + dc.font.height)
62 enum { CurNormal
, CurResize
, CurMove
, CurLast
}; /* cursor */
63 enum { ColBorder
, ColFG
, ColBG
, ColLast
}; /* color */
64 enum { NetSupported
, NetWMName
, NetWMState
,
65 NetWMFullscreen
, NetActiveWindow
, NetWMWindowType
,
66 NetWMWindowTypeDialog
, NetClientList
, NetLast
}; /* EWMH atoms */
67 enum { WMProtocols
, WMDelete
, WMState
, WMTakeFocus
, WMLast
}; /* default atoms */
68 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
69 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
82 void (*func
)(const Arg
*arg
);
86 typedef struct Monitor Monitor
;
87 typedef struct Client Client
;
92 int oldx
, oldy
, oldw
, oldh
;
93 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
96 Bool isfixed
, isfloating
, isurgent
, neverfocus
, oldstate
, isfullscreen
;
105 unsigned long norm
[ColLast
];
106 unsigned long sel
[ColLast
];
116 } DC
; /* draw context */
121 void (*func
)(const Arg
*);
127 void (*arrange
)(Monitor
*);
135 int by
; /* bar geometry */
136 int mx
, my
, mw
, mh
; /* screen size */
137 int wx
, wy
, ww
, wh
; /* window area */
138 unsigned int seltags
;
140 unsigned int tagset
[2];
153 const char *instance
;
160 /* function declarations */
161 static void applyrules(Client
*c
);
162 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
163 static void arrange(Monitor
*m
);
164 static void arrangemon(Monitor
*m
);
165 static void attach(Client
*c
);
166 static void attachstack(Client
*c
);
167 static void buttonpress(XEvent
*e
);
168 static void checkotherwm(void);
169 static void cleanup(void);
170 static void cleanupmon(Monitor
*mon
);
171 static void clearurgent(Client
*c
);
172 static void clientmessage(XEvent
*e
);
173 static void configure(Client
*c
);
174 static void configurenotify(XEvent
*e
);
175 static void configurerequest(XEvent
*e
);
176 static Monitor
*createmon(void);
177 static void destroynotify(XEvent
*e
);
178 static void detach(Client
*c
);
179 static void detachstack(Client
*c
);
180 static void die(const char *errstr
, ...);
181 static Monitor
*dirtomon(int dir
);
182 static void drawbar(Monitor
*m
);
183 static void drawbars(void);
184 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
185 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
186 static void enternotify(XEvent
*e
);
187 static void expose(XEvent
*e
);
188 static void focus(Client
*c
);
189 static void focusin(XEvent
*e
);
190 static void focusmon(const Arg
*arg
);
191 static void focusstack(const Arg
*arg
);
192 static unsigned long getcolor(const char *colstr
);
193 static Bool
getrootptr(int *x
, int *y
);
194 static long getstate(Window w
);
195 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
196 static void grabbuttons(Client
*c
, Bool focused
);
197 static void grabkeys(void);
198 static void incnmaster(const Arg
*arg
);
199 static void initfont(const char *fontstr
);
200 static void keypress(XEvent
*e
);
201 static void killclient(const Arg
*arg
);
202 static void manage(Window w
, XWindowAttributes
*wa
);
203 static void mappingnotify(XEvent
*e
);
204 static void maprequest(XEvent
*e
);
205 static void monocle(Monitor
*m
);
206 static void motionnotify(XEvent
*e
);
207 static void movemouse(const Arg
*arg
);
208 static Client
*nexttiled(Client
*c
);
209 static void pop(Client
*);
210 static void propertynotify(XEvent
*e
);
211 static void quit(const Arg
*arg
);
212 static Monitor
*recttomon(int x
, int y
, int w
, int h
);
213 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
214 static void resizeclient(Client
*c
, int x
, int y
, int w
, int h
);
215 static void resizemouse(const Arg
*arg
);
216 static void restack(Monitor
*m
);
217 static void run(void);
218 static void scan(void);
219 static Bool
sendevent(Client
*c
, Atom proto
);
220 static void sendmon(Client
*c
, Monitor
*m
);
221 static void setclientstate(Client
*c
, long state
);
222 static void setfocus(Client
*c
);
223 static void setfullscreen(Client
*c
, Bool fullscreen
);
224 static void setlayout(const Arg
*arg
);
225 static void setmfact(const Arg
*arg
);
226 static void setup(void);
227 static void showhide(Client
*c
);
228 static void sigchld(int unused
);
229 static void spawn(const Arg
*arg
);
230 static void tag(const Arg
*arg
);
231 static void tagmon(const Arg
*arg
);
232 static int textnw(const char *text
, unsigned int len
);
233 static void tile(Monitor
*);
234 static void togglebar(const Arg
*arg
);
235 static void togglefloating(const Arg
*arg
);
236 static void toggletag(const Arg
*arg
);
237 static void toggleview(const Arg
*arg
);
238 static void unfocus(Client
*c
, Bool setfocus
);
239 static void unmanage(Client
*c
, Bool destroyed
);
240 static void unmapnotify(XEvent
*e
);
241 static Bool
updategeom(void);
242 static void updatebarpos(Monitor
*m
);
243 static void updatebars(void);
244 static void updateclientlist(void);
245 static void updatenumlockmask(void);
246 static void updatesizehints(Client
*c
);
247 static void updatestatus(void);
248 static void updatewindowtype(Client
*c
);
249 static void updatetitle(Client
*c
);
250 static void updatewmhints(Client
*c
);
251 static void view(const Arg
*arg
);
252 static Client
*wintoclient(Window w
);
253 static Monitor
*wintomon(Window w
);
254 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
255 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
256 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
257 static void zoom(const Arg
*arg
);
260 static const char broken
[] = "broken";
261 static char stext
[256];
263 static int sw
, sh
; /* X display screen geometry width, height */
264 static int bh
, blw
= 0; /* bar geometry */
265 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
266 static unsigned int numlockmask
= 0;
267 static void (*handler
[LASTEvent
]) (XEvent
*) = {
268 [ButtonPress
] = buttonpress
,
269 [ClientMessage
] = clientmessage
,
270 [ConfigureRequest
] = configurerequest
,
271 [ConfigureNotify
] = configurenotify
,
272 [DestroyNotify
] = destroynotify
,
273 [EnterNotify
] = enternotify
,
276 [KeyPress
] = keypress
,
277 [MappingNotify
] = mappingnotify
,
278 [MapRequest
] = maprequest
,
279 [MotionNotify
] = motionnotify
,
280 [PropertyNotify
] = propertynotify
,
281 [UnmapNotify
] = unmapnotify
283 static Atom wmatom
[WMLast
], netatom
[NetLast
];
284 static Bool running
= True
, usexkb
;
285 static Cursor cursor
[CurLast
];
288 static Monitor
*mons
= NULL
, *selmon
= NULL
;
291 /* configuration, allows nested code to access above variables */
294 /* compile-time check if all tags fit into an unsigned int bit array. */
295 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
297 /* function implementations */
299 applyrules(Client
*c
) {
300 const char *class, *instance
;
304 XClassHint ch
= { NULL
, NULL
};
307 c
->isfloating
= c
->tags
= 0;
308 XGetClassHint(dpy
, c
->win
, &ch
);
309 class = ch
.res_class
? ch
.res_class
: broken
;
310 instance
= ch
.res_name
? ch
.res_name
: broken
;
312 for(i
= 0; i
< LENGTH(rules
); i
++) {
314 if((!r
->title
|| strstr(c
->name
, r
->title
))
315 && (!r
->class || strstr(class, r
->class))
316 && (!r
->instance
|| strstr(instance
, r
->instance
)))
318 c
->isfloating
= r
->isfloating
;
320 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
329 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
333 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
337 /* set minimum possible */
345 if(*x
+ *w
+ 2 * c
->bw
< 0)
347 if(*y
+ *h
+ 2 * c
->bw
< 0)
351 if(*x
>= m
->wx
+ m
->ww
)
352 *x
= m
->wx
+ m
->ww
- WIDTH(c
);
353 if(*y
>= m
->wy
+ m
->wh
)
354 *y
= m
->wy
+ m
->wh
- HEIGHT(c
);
355 if(*x
+ *w
+ 2 * c
->bw
<= m
->wx
)
357 if(*y
+ *h
+ 2 * c
->bw
<= m
->wy
)
364 if(resizehints
|| c
->isfloating
|| !c
->mon
->lt
[c
->mon
->sellt
]->arrange
) {
365 /* see last two sentences in ICCCM 4.1.2.3 */
366 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
367 if(!baseismin
) { /* temporarily remove base dimensions */
371 /* adjust for aspect limits */
372 if(c
->mina
> 0 && c
->maxa
> 0) {
373 if(c
->maxa
< (float)*w
/ *h
)
374 *w
= *h
* c
->maxa
+ 0.5;
375 else if(c
->mina
< (float)*h
/ *w
)
376 *h
= *w
* c
->mina
+ 0.5;
378 if(baseismin
) { /* increment calculation requires this */
382 /* adjust for increment value */
387 /* restore base dimensions */
388 *w
= MAX(*w
+ c
->basew
, c
->minw
);
389 *h
= MAX(*h
+ c
->baseh
, c
->minh
);
391 *w
= MIN(*w
, c
->maxw
);
393 *h
= MIN(*h
, c
->maxh
);
395 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
399 arrange(Monitor
*m
) {
402 else for(m
= mons
; m
; m
= m
->next
)
407 } else for(m
= mons
; m
; m
= m
->next
)
412 arrangemon(Monitor
*m
) {
413 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
414 if(m
->lt
[m
->sellt
]->arrange
)
415 m
->lt
[m
->sellt
]->arrange(m
);
420 c
->next
= c
->mon
->clients
;
425 attachstack(Client
*c
) {
426 c
->snext
= c
->mon
->stack
;
431 buttonpress(XEvent
*e
) {
432 unsigned int i
, x
, click
;
436 XButtonPressedEvent
*ev
= &e
->xbutton
;
439 /* focus monitor if necessary */
440 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
441 unfocus(selmon
->sel
, True
);
445 if(ev
->window
== selmon
->barwin
) {
449 while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
450 if(i
< LENGTH(tags
)) {
454 else if(ev
->x
< x
+ blw
)
456 else if(ev
->x
> selmon
->ww
- TEXTW(stext
))
457 click
= ClkStatusText
;
461 else if((c
= wintoclient(ev
->window
))) {
463 click
= ClkClientWin
;
465 for(i
= 0; i
< LENGTH(buttons
); i
++)
466 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
467 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
468 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
473 xerrorxlib
= XSetErrorHandler(xerrorstart
);
474 /* this causes an error if some other window manager is running */
475 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
477 XSetErrorHandler(xerror
);
484 Layout foo
= { "", NULL
};
488 selmon
->lt
[selmon
->sellt
] = &foo
;
489 for(m
= mons
; m
; m
= m
->next
)
491 unmanage(m
->stack
, False
);
493 XFreeFontSet(dpy
, dc
.font
.set
);
495 XFreeFont(dpy
, dc
.font
.xfont
);
496 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
497 XFreePixmap(dpy
, dc
.drawable
);
499 XFreeCursor(dpy
, cursor
[CurNormal
]);
500 XFreeCursor(dpy
, cursor
[CurResize
]);
501 XFreeCursor(dpy
, cursor
[CurMove
]);
505 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
506 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
510 cleanupmon(Monitor
*mon
) {
516 for(m
= mons
; m
&& m
->next
!= mon
; m
= m
->next
);
519 XUnmapWindow(dpy
, mon
->barwin
);
520 XDestroyWindow(dpy
, mon
->barwin
);
525 clearurgent(Client
*c
) {
529 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
531 wmh
->flags
&= ~XUrgencyHint
;
532 XSetWMHints(dpy
, c
->win
, wmh
);
537 clientmessage(XEvent
*e
) {
538 XClientMessageEvent
*cme
= &e
->xclient
;
539 Client
*c
= wintoclient(cme
->window
);
543 if(cme
->message_type
== netatom
[NetWMState
]) {
544 if(cme
->data
.l
[1] == netatom
[NetWMFullscreen
] || cme
->data
.l
[2] == netatom
[NetWMFullscreen
])
545 setfullscreen(c
, (cme
->data
.l
[0] == 1 /* _NET_WM_STATE_ADD */
546 || (cme
->data
.l
[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c
->isfullscreen
)));
548 else if(cme
->message_type
== netatom
[NetActiveWindow
]) {
550 c
->mon
->seltags
^= 1;
551 c
->mon
->tagset
[c
->mon
->seltags
] = c
->tags
;
558 configure(Client
*c
) {
561 ce
.type
= ConfigureNotify
;
569 ce
.border_width
= c
->bw
;
571 ce
.override_redirect
= False
;
572 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
576 configurenotify(XEvent
*e
) {
578 XConfigureEvent
*ev
= &e
->xconfigure
;
581 // TODO: updategeom handling sucks, needs to be simplified
582 if(ev
->window
== root
) {
583 dirty
= (sw
!= ev
->width
|| sh
!= ev
->height
);
586 if(updategeom() || dirty
) {
588 XFreePixmap(dpy
, dc
.drawable
);
589 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
591 for(m
= mons
; m
; m
= m
->next
)
592 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
600 configurerequest(XEvent
*e
) {
603 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
606 if((c
= wintoclient(ev
->window
))) {
607 if(ev
->value_mask
& CWBorderWidth
)
608 c
->bw
= ev
->border_width
;
609 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
611 if(ev
->value_mask
& CWX
) {
613 c
->x
= m
->mx
+ ev
->x
;
615 if(ev
->value_mask
& CWY
) {
617 c
->y
= m
->my
+ ev
->y
;
619 if(ev
->value_mask
& CWWidth
) {
623 if(ev
->value_mask
& CWHeight
) {
627 if((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
628 c
->x
= m
->mx
+ (m
->mw
/ 2 - WIDTH(c
) / 2); /* center in x direction */
629 if((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
630 c
->y
= m
->my
+ (m
->mh
/ 2 - HEIGHT(c
) / 2); /* center in y direction */
631 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
634 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
642 wc
.width
= ev
->width
;
643 wc
.height
= ev
->height
;
644 wc
.border_width
= ev
->border_width
;
645 wc
.sibling
= ev
->above
;
646 wc
.stack_mode
= ev
->detail
;
647 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
656 if(!(m
= (Monitor
*)calloc(1, sizeof(Monitor
))))
657 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
658 m
->tagset
[0] = m
->tagset
[1] = 1;
660 m
->nmaster
= nmaster
;
661 m
->showbar
= showbar
;
663 m
->lt
[0] = &layouts
[0];
664 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
665 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
670 destroynotify(XEvent
*e
) {
672 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
674 if((c
= wintoclient(ev
->window
)))
682 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
687 detachstack(Client
*c
) {
690 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
693 if(c
== c
->mon
->sel
) {
694 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
700 die(const char *errstr
, ...) {
703 va_start(ap
, errstr
);
704 vfprintf(stderr
, errstr
, ap
);
714 if(!(m
= selmon
->next
))
717 else if(selmon
== mons
)
718 for(m
= mons
; m
->next
; m
= m
->next
);
720 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 m
= c
? c
->mon
: wintomon(ev
->window
);
832 unfocus(selmon
->sel
, True
);
835 else if(!c
|| c
== selmon
->sel
)
843 XExposeEvent
*ev
= &e
->xexpose
;
845 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
851 if(!c
|| !ISVISIBLE(c
))
852 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
853 /* was if(selmon->sel) */
854 if(selmon
->sel
&& selmon
->sel
!= c
)
855 unfocus(selmon
->sel
, False
);
863 grabbuttons(c
, True
);
864 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
868 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
869 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
876 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
877 XFocusChangeEvent
*ev
= &e
->xfocus
;
879 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
880 setfocus(selmon
->sel
);
884 focusmon(const Arg
*arg
) {
889 if((m
= dirtomon(arg
->i
)) == selmon
)
891 unfocus(selmon
->sel
, False
); /* s/True/False/ fixes input focus issues
892 in gedit and anjuta */
898 focusstack(const Arg
*arg
) {
899 Client
*c
= NULL
, *i
;
904 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
906 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
909 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
913 for(; i
; i
= i
->next
)
924 getatomprop(Client
*c
, Atom prop
) {
927 unsigned char *p
= NULL
;
928 Atom da
, atom
= None
;
930 if(XGetWindowProperty(dpy
, c
->win
, prop
, 0L, sizeof atom
, False
, XA_ATOM
,
931 &da
, &di
, &dl
, &dl
, &p
) == Success
&& p
) {
939 getcolor(const char *colstr
) {
940 Colormap cmap
= DefaultColormap(dpy
, screen
);
943 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
944 die("error, cannot allocate color '%s'\n", colstr
);
949 getrootptr(int *x
, int *y
) {
954 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
961 unsigned char *p
= NULL
;
962 unsigned long n
, extra
;
965 if(XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
966 &real
, &format
, &n
, &extra
, (unsigned char **)&p
) != Success
)
975 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
980 if(!text
|| size
== 0)
983 XGetTextProperty(dpy
, w
, &name
, atom
);
986 if(name
.encoding
== XA_STRING
)
987 strncpy(text
, (char *)name
.value
, size
- 1);
989 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
990 strncpy(text
, *list
, size
- 1);
991 XFreeStringList(list
);
994 text
[size
- 1] = '\0';
1000 grabbuttons(Client
*c
, Bool focused
) {
1001 updatenumlockmask();
1004 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1005 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1007 for(i
= 0; i
< LENGTH(buttons
); i
++)
1008 if(buttons
[i
].click
== ClkClientWin
)
1009 for(j
= 0; j
< LENGTH(modifiers
); j
++)
1010 XGrabButton(dpy
, buttons
[i
].button
,
1011 buttons
[i
].mask
| modifiers
[j
],
1012 c
->win
, False
, BUTTONMASK
,
1013 GrabModeAsync
, GrabModeSync
, None
, None
);
1016 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
1017 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
1023 updatenumlockmask();
1026 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
1029 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
1030 for(i
= 0; i
< LENGTH(keys
); i
++)
1031 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
1032 for(j
= 0; j
< LENGTH(modifiers
); j
++)
1033 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
1034 True
, GrabModeAsync
, GrabModeAsync
);
1039 incnmaster(const Arg
*arg
) {
1040 selmon
->nmaster
= MAX(selmon
->nmaster
+ arg
->i
, 0);
1045 initfont(const char *fontstr
) {
1046 char *def
, **missing
;
1049 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
1052 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
1053 XFreeStringList(missing
);
1056 XFontStruct
**xfonts
;
1059 dc
.font
.ascent
= dc
.font
.descent
= 0;
1060 XExtentsOfFontSet(dc
.font
.set
);
1061 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
1063 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
1064 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1069 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1070 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1071 die("error, cannot load font: '%s'\n", fontstr
);
1072 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1073 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1075 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1080 isuniquegeom(XineramaScreenInfo
*unique
, size_t n
, XineramaScreenInfo
*info
) {
1082 if(unique
[n
].x_org
== info
->x_org
&& unique
[n
].y_org
== info
->y_org
1083 && unique
[n
].width
== info
->width
&& unique
[n
].height
== info
->height
)
1087 #endif /* XINERAMA */
1090 keypress(XEvent
*e
) {
1097 keysym
= XkbKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0, 0);
1099 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1100 for(i
= 0; i
< LENGTH(keys
); i
++)
1101 if(keysym
== keys
[i
].keysym
1102 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1104 keys
[i
].func(&(keys
[i
].arg
));
1108 killclient(const Arg
*arg
) {
1111 if(!sendevent(selmon
->sel
, wmatom
[WMDelete
])) {
1113 XSetErrorHandler(xerrordummy
);
1114 XSetCloseDownMode(dpy
, DestroyAll
);
1115 XKillClient(dpy
, selmon
->sel
->win
);
1117 XSetErrorHandler(xerror
);
1123 manage(Window w
, XWindowAttributes
*wa
) {
1124 Client
*c
, *t
= NULL
;
1125 Window trans
= None
;
1128 if(!(c
= calloc(1, sizeof(Client
))))
1129 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1132 if(XGetTransientForHint(dpy
, w
, &trans
) && (t
= wintoclient(trans
))) {
1141 c
->x
= c
->oldx
= wa
->x
;
1142 c
->y
= c
->oldy
= wa
->y
;
1143 c
->w
= c
->oldw
= wa
->width
;
1144 c
->h
= c
->oldh
= wa
->height
;
1145 c
->oldbw
= wa
->border_width
;
1147 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1148 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1149 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1150 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1151 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1152 /* only fix client y-offset, if the client center might cover the bar */
1153 c
->y
= MAX(c
->y
, ((c
->mon
->by
== c
->mon
->my
) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1154 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1157 wc
.border_width
= c
->bw
;
1158 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1159 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1160 configure(c
); /* propagates border_width, if size doesn't change */
1161 updatewindowtype(c
);
1164 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1165 grabbuttons(c
, False
);
1167 c
->isfloating
= c
->oldstate
= trans
!= None
|| c
->isfixed
;
1169 XRaiseWindow(dpy
, c
->win
);
1172 XChangeProperty(dpy
, root
, netatom
[NetClientList
], XA_WINDOW
, 32, PropModeAppend
,
1173 (unsigned char *) &(c
->win
), 1);
1174 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1175 setclientstate(c
, NormalState
);
1176 if (c
->mon
== selmon
)
1177 unfocus(selmon
->sel
, False
);
1180 XMapWindow(dpy
, c
->win
);
1185 mappingnotify(XEvent
*e
) {
1186 XMappingEvent
*ev
= &e
->xmapping
;
1188 XRefreshKeyboardMapping(ev
);
1189 if(ev
->request
== MappingKeyboard
)
1194 maprequest(XEvent
*e
) {
1195 static XWindowAttributes wa
;
1196 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1198 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1200 if(wa
.override_redirect
)
1202 if(!wintoclient(ev
->window
))
1203 manage(ev
->window
, &wa
);
1207 monocle(Monitor
*m
) {
1211 for(c
= m
->clients
; c
; c
= c
->next
)
1214 if(n
> 0) /* override layout symbol */
1215 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1216 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1217 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1221 motionnotify(XEvent
*e
) {
1222 static Monitor
*mon
= NULL
;
1224 XMotionEvent
*ev
= &e
->xmotion
;
1226 if(ev
->window
!= root
)
1228 if((m
= recttomon(ev
->x_root
, ev
->y_root
, 1, 1)) != mon
&& mon
) {
1229 unfocus(selmon
->sel
, True
);
1237 movemouse(const Arg
*arg
) {
1238 int x
, y
, ocx
, ocy
, nx
, ny
;
1243 if(!(c
= selmon
->sel
))
1245 if(c
->isfullscreen
) /* no support moving fullscreen windows by mouse */
1250 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1251 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1253 if(!getrootptr(&x
, &y
))
1256 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1258 case ConfigureRequest
:
1261 handler
[ev
.type
](&ev
);
1264 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1265 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1266 if(nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1267 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1268 if(abs(selmon
->wx
- nx
) < snap
)
1270 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1271 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1272 if(abs(selmon
->wy
- ny
) < snap
)
1274 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1275 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1276 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1277 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1278 togglefloating(NULL
);
1280 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1281 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1284 } while(ev
.type
!= ButtonRelease
);
1285 XUngrabPointer(dpy
, CurrentTime
);
1286 if((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1294 nexttiled(Client
*c
) {
1295 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1308 propertynotify(XEvent
*e
) {
1311 XPropertyEvent
*ev
= &e
->xproperty
;
1313 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1315 else if(ev
->state
== PropertyDelete
)
1316 return; /* ignore */
1317 else if((c
= wintoclient(ev
->window
))) {
1320 case XA_WM_TRANSIENT_FOR
:
1321 if(!c
->isfloating
&& (XGetTransientForHint(dpy
, c
->win
, &trans
)) &&
1322 (c
->isfloating
= (wintoclient(trans
)) != NULL
))
1325 case XA_WM_NORMAL_HINTS
:
1333 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1335 if(c
== c
->mon
->sel
)
1338 if(ev
->atom
== netatom
[NetWMWindowType
])
1339 updatewindowtype(c
);
1344 quit(const Arg
*arg
) {
1349 recttomon(int x
, int y
, int w
, int h
) {
1350 Monitor
*m
, *r
= selmon
;
1353 for(m
= mons
; m
; m
= m
->next
)
1354 if((a
= INTERSECT(x
, y
, w
, h
, m
)) > area
) {
1362 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1363 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
))
1364 resizeclient(c
, x
, y
, w
, h
);
1368 resizeclient(Client
*c
, int x
, int y
, int w
, int h
) {
1371 c
->oldx
= c
->x
; c
->x
= wc
.x
= x
;
1372 c
->oldy
= c
->y
; c
->y
= wc
.y
= y
;
1373 c
->oldw
= c
->w
; c
->w
= wc
.width
= w
;
1374 c
->oldh
= c
->h
; c
->h
= wc
.height
= h
;
1375 wc
.border_width
= c
->bw
;
1376 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1382 resizemouse(const Arg
*arg
) {
1389 if(!(c
= selmon
->sel
))
1391 if(c
->isfullscreen
) /* no support resizing fullscreen windows by mouse */
1396 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1397 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1399 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1401 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1403 case ConfigureRequest
:
1406 handler
[ev
.type
](&ev
);
1409 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1410 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1411 if(c
->mon
->wx
+ nw
>= selmon
->wx
&& c
->mon
->wx
+ nw
<= selmon
->wx
+ selmon
->ww
1412 && c
->mon
->wy
+ nh
>= selmon
->wy
&& c
->mon
->wy
+ nh
<= selmon
->wy
+ selmon
->wh
)
1414 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1415 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1416 togglefloating(NULL
);
1418 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1419 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1422 } while(ev
.type
!= ButtonRelease
);
1423 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1424 XUngrabPointer(dpy
, CurrentTime
);
1425 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1426 if((m
= recttomon(c
->x
, c
->y
, c
->w
, c
->h
)) != selmon
) {
1434 restack(Monitor
*m
) {
1442 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1443 XRaiseWindow(dpy
, m
->sel
->win
);
1444 if(m
->lt
[m
->sellt
]->arrange
) {
1445 wc
.stack_mode
= Below
;
1446 wc
.sibling
= m
->barwin
;
1447 for(c
= m
->stack
; c
; c
= c
->snext
)
1448 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1449 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1450 wc
.sibling
= c
->win
;
1454 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1460 /* main event loop */
1462 while(running
&& !XNextEvent(dpy
, &ev
))
1463 if(handler
[ev
.type
])
1464 handler
[ev
.type
](&ev
); /* call handler */
1469 unsigned int i
, num
;
1470 Window d1
, d2
, *wins
= NULL
;
1471 XWindowAttributes wa
;
1473 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1474 for(i
= 0; i
< num
; i
++) {
1475 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1476 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1478 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1479 manage(wins
[i
], &wa
);
1481 for(i
= 0; i
< num
; i
++) { /* now the transients */
1482 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1484 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1485 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1486 manage(wins
[i
], &wa
);
1494 sendmon(Client
*c
, Monitor
*m
) {
1501 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1509 setclientstate(Client
*c
, long state
) {
1510 long data
[] = { state
, None
};
1512 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1513 PropModeReplace
, (unsigned char *)data
, 2);
1517 sendevent(Client
*c
, Atom proto
) {
1520 Bool exists
= False
;
1523 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1524 while(!exists
&& n
--)
1525 exists
= protocols
[n
] == proto
;
1529 ev
.type
= ClientMessage
;
1530 ev
.xclient
.window
= c
->win
;
1531 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1532 ev
.xclient
.format
= 32;
1533 ev
.xclient
.data
.l
[0] = proto
;
1534 ev
.xclient
.data
.l
[1] = CurrentTime
;
1535 XSendEvent(dpy
, c
->win
, False
, NoEventMask
, &ev
);
1541 setfocus(Client
*c
) {
1542 if(!c
->neverfocus
) {
1543 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
1544 XChangeProperty(dpy
, root
, netatom
[NetActiveWindow
],
1545 XA_WINDOW
, 32, PropModeReplace
,
1546 (unsigned char *) &(c
->win
), 1);
1548 sendevent(c
, wmatom
[WMTakeFocus
]);
1552 setfullscreen(Client
*c
, Bool fullscreen
) {
1554 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1555 PropModeReplace
, (unsigned char*)&netatom
[NetWMFullscreen
], 1);
1556 c
->isfullscreen
= True
;
1557 c
->oldstate
= c
->isfloating
;
1560 c
->isfloating
= True
;
1561 resizeclient(c
, c
->mon
->mx
, c
->mon
->my
, c
->mon
->mw
, c
->mon
->mh
);
1562 XRaiseWindow(dpy
, c
->win
);
1565 XChangeProperty(dpy
, c
->win
, netatom
[NetWMState
], XA_ATOM
, 32,
1566 PropModeReplace
, (unsigned char*)0, 0);
1567 c
->isfullscreen
= False
;
1568 c
->isfloating
= c
->oldstate
;
1574 resizeclient(c
, c
->x
, c
->y
, c
->w
, c
->h
);
1580 setlayout(const Arg
*arg
) {
1581 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1584 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1585 strncpy(selmon
->ltsymbol
, selmon
->lt
[selmon
->sellt
]->symbol
, sizeof selmon
->ltsymbol
);
1592 /* arg > 1.0 will set mfact absolutly */
1594 setmfact(const Arg
*arg
) {
1597 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1599 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1600 if(f
< 0.1 || f
> 0.9)
1608 XSetWindowAttributes wa
;
1609 int dummy
= 0, xkbmajor
= XkbMajorVersion
, xkbminor
= XkbMinorVersion
;
1611 /* clean up any zombies immediately */
1615 screen
= DefaultScreen(dpy
);
1616 root
= RootWindow(dpy
, screen
);
1618 sw
= DisplayWidth(dpy
, screen
);
1619 sh
= DisplayHeight(dpy
, screen
);
1620 bh
= dc
.h
= dc
.font
.height
+ 2;
1623 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1624 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1625 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1626 wmatom
[WMTakeFocus
] = XInternAtom(dpy
, "WM_TAKE_FOCUS", False
);
1627 netatom
[NetActiveWindow
] = XInternAtom(dpy
, "_NET_ACTIVE_WINDOW", False
);
1628 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1629 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1630 netatom
[NetWMState
] = XInternAtom(dpy
, "_NET_WM_STATE", False
);
1631 netatom
[NetWMFullscreen
] = XInternAtom(dpy
, "_NET_WM_STATE_FULLSCREEN", False
);
1632 netatom
[NetWMWindowType
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE", False
);
1633 netatom
[NetWMWindowTypeDialog
] = XInternAtom(dpy
, "_NET_WM_WINDOW_TYPE_DIALOG", False
);
1634 netatom
[NetClientList
] = XInternAtom(dpy
, "_NET_CLIENT_LIST", False
);
1636 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1637 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1638 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1639 /* init appearance */
1640 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1641 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1642 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1643 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1644 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1645 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1646 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1647 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1648 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1650 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1654 /* EWMH support per view */
1655 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1656 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1657 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1658 /* select for events */
1659 wa
.cursor
= cursor
[CurNormal
];
1660 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
|PointerMotionMask
1661 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
|PropertyChangeMask
;
1662 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1663 XSelectInput(dpy
, root
, wa
.event_mask
);
1665 usexkb
= XkbQueryExtension(dpy
, &dummy
, &dummy
, &dummy
, &xkbmajor
, &xkbminor
);
1670 showhide(Client
*c
) {
1673 if(ISVISIBLE(c
)) { /* show clients top down */
1674 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1675 if((!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
) && !c
->isfullscreen
)
1676 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1679 else { /* hide clients bottom up */
1681 XMoveWindow(dpy
, c
->win
, WIDTH(c
) * -2, c
->y
);
1686 sigchld(int unused
) {
1687 if(signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1688 die("Can't install SIGCHLD handler");
1689 while(0 < waitpid(-1, NULL
, WNOHANG
));
1693 spawn(const Arg
*arg
) {
1696 close(ConnectionNumber(dpy
));
1698 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1699 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1706 tag(const Arg
*arg
) {
1707 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1708 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1715 tagmon(const Arg
*arg
) {
1716 if(!selmon
->sel
|| !mons
->next
)
1718 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1722 textnw(const char *text
, unsigned int len
) {
1726 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1729 return XTextWidth(dc
.font
.xfont
, text
, len
);
1734 unsigned int i
, n
, h
, mw
, my
, ty
;
1737 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1742 mw
= m
->nmaster
? m
->ww
* m
->mfact
: 0;
1745 for(i
= my
= ty
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), i
++)
1746 if(i
< m
->nmaster
) {
1747 h
= (m
->wh
- my
) / (MIN(n
, m
->nmaster
) - i
);
1748 resize(c
, m
->wx
, m
->wy
+ my
, mw
- (2*c
->bw
), h
- (2*c
->bw
), False
);
1752 h
= (m
->wh
- ty
) / (n
- i
);
1753 resize(c
, m
->wx
+ mw
, m
->wy
+ ty
, m
->ww
- mw
- (2*c
->bw
), h
- (2*c
->bw
), False
);
1759 togglebar(const Arg
*arg
) {
1760 selmon
->showbar
= !selmon
->showbar
;
1761 updatebarpos(selmon
);
1762 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1767 togglefloating(const Arg
*arg
) {
1770 if(selmon
->sel
->isfullscreen
) /* no support for fullscreen windows */
1772 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1773 if(selmon
->sel
->isfloating
)
1774 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1775 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1780 toggletag(const Arg
*arg
) {
1781 unsigned int newtags
;
1785 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1787 selmon
->sel
->tags
= newtags
;
1794 toggleview(const Arg
*arg
) {
1795 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1798 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1805 unfocus(Client
*c
, Bool setfocus
) {
1808 grabbuttons(c
, False
);
1809 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1811 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1812 XDeleteProperty(dpy
, root
, netatom
[NetActiveWindow
]);
1817 unmanage(Client
*c
, Bool destroyed
) {
1818 Monitor
*m
= c
->mon
;
1821 /* The server grab construct avoids race conditions. */
1825 wc
.border_width
= c
->oldbw
;
1827 XSetErrorHandler(xerrordummy
);
1828 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1829 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1830 setclientstate(c
, WithdrawnState
);
1832 XSetErrorHandler(xerror
);
1842 unmapnotify(XEvent
*e
) {
1844 XUnmapEvent
*ev
= &e
->xunmap
;
1846 if((c
= wintoclient(ev
->window
))) {
1848 setclientstate(c
, WithdrawnState
);
1857 XSetWindowAttributes wa
= {
1858 .override_redirect
= True
,
1859 .background_pixmap
= ParentRelative
,
1860 .event_mask
= ButtonPressMask
|ExposureMask
1862 for(m
= mons
; m
; m
= m
->next
) {
1865 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1866 CopyFromParent
, DefaultVisual(dpy
, screen
),
1867 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1868 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1869 XMapRaised(dpy
, m
->barwin
);
1874 updatebarpos(Monitor
*m
) {
1879 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1880 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1887 updateclientlist() {
1891 XDeleteProperty(dpy
, root
, netatom
[NetClientList
]);
1892 for(m
= mons
; m
; m
= m
->next
)
1893 for(c
= m
->clients
; c
; c
= c
->next
)
1894 XChangeProperty(dpy
, root
, netatom
[NetClientList
],
1895 XA_WINDOW
, 32, PropModeAppend
,
1896 (unsigned char *) &(c
->win
), 1);
1904 if(XineramaIsActive(dpy
)) {
1908 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1909 XineramaScreenInfo
*unique
= NULL
;
1911 for(n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1912 /* only consider unique geometries as separate screens */
1913 if(!(unique
= (XineramaScreenInfo
*)malloc(sizeof(XineramaScreenInfo
) * nn
)))
1914 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo
) * nn
);
1915 for(i
= 0, j
= 0; i
< nn
; i
++)
1916 if(isuniquegeom(unique
, j
, &info
[i
]))
1917 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1921 for(i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1922 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1924 m
->next
= createmon();
1928 for(i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1930 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1931 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1935 m
->mx
= m
->wx
= unique
[i
].x_org
;
1936 m
->my
= m
->wy
= unique
[i
].y_org
;
1937 m
->mw
= m
->ww
= unique
[i
].width
;
1938 m
->mh
= m
->wh
= unique
[i
].height
;
1942 else { /* less monitors available nn < n */
1943 for(i
= nn
; i
< n
; i
++) {
1944 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1948 m
->clients
= c
->next
;
1962 #endif /* XINERAMA */
1963 /* default monitor setup */
1967 if(mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1969 mons
->mw
= mons
->ww
= sw
;
1970 mons
->mh
= mons
->wh
= sh
;
1976 selmon
= wintomon(root
);
1982 updatenumlockmask(void) {
1984 XModifierKeymap
*modmap
;
1987 modmap
= XGetModifierMapping(dpy
);
1988 for(i
= 0; i
< 8; i
++)
1989 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1990 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1991 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1992 numlockmask
= (1 << i
);
1993 XFreeModifiermap(modmap
);
1997 updatesizehints(Client
*c
) {
2001 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
2002 /* size is uninitialized, ensure that size.flags aren't used */
2004 if(size
.flags
& PBaseSize
) {
2005 c
->basew
= size
.base_width
;
2006 c
->baseh
= size
.base_height
;
2008 else if(size
.flags
& PMinSize
) {
2009 c
->basew
= size
.min_width
;
2010 c
->baseh
= size
.min_height
;
2013 c
->basew
= c
->baseh
= 0;
2014 if(size
.flags
& PResizeInc
) {
2015 c
->incw
= size
.width_inc
;
2016 c
->inch
= size
.height_inc
;
2019 c
->incw
= c
->inch
= 0;
2020 if(size
.flags
& PMaxSize
) {
2021 c
->maxw
= size
.max_width
;
2022 c
->maxh
= size
.max_height
;
2025 c
->maxw
= c
->maxh
= 0;
2026 if(size
.flags
& PMinSize
) {
2027 c
->minw
= size
.min_width
;
2028 c
->minh
= size
.min_height
;
2030 else if(size
.flags
& PBaseSize
) {
2031 c
->minw
= size
.base_width
;
2032 c
->minh
= size
.base_height
;
2035 c
->minw
= c
->minh
= 0;
2036 if(size
.flags
& PAspect
) {
2037 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
2038 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
2041 c
->maxa
= c
->mina
= 0.0;
2042 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
2043 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
2047 updatetitle(Client
*c
) {
2048 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
2049 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
2050 if(c
->name
[0] == '\0') /* hack to mark broken clients */
2051 strcpy(c
->name
, broken
);
2055 updatestatus(void) {
2056 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
2057 strcpy(stext
, "dwm-"VERSION
);
2062 updatewindowtype(Client
*c
) {
2063 Atom state
= getatomprop(c
, netatom
[NetWMState
]);
2064 Atom wtype
= getatomprop(c
, netatom
[NetWMWindowType
]);
2066 if(state
== netatom
[NetWMFullscreen
])
2067 setfullscreen(c
, True
);
2068 if(wtype
== netatom
[NetWMWindowTypeDialog
])
2069 c
->isfloating
= True
;
2073 updatewmhints(Client
*c
) {
2076 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
2077 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
2078 wmh
->flags
&= ~XUrgencyHint
;
2079 XSetWMHints(dpy
, c
->win
, wmh
);
2082 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
2083 if(wmh
->flags
& InputHint
)
2084 c
->neverfocus
= !wmh
->input
;
2086 c
->neverfocus
= False
;
2092 view(const Arg
*arg
) {
2093 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
2095 selmon
->seltags
^= 1; /* toggle sel tagset */
2096 if(arg
->ui
& TAGMASK
)
2097 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
2103 wintoclient(Window w
) {
2107 for(m
= mons
; m
; m
= m
->next
)
2108 for(c
= m
->clients
; c
; c
= c
->next
)
2115 wintomon(Window w
) {
2120 if(w
== root
&& getrootptr(&x
, &y
))
2121 return recttomon(x
, y
, 1, 1);
2122 for(m
= mons
; m
; m
= m
->next
)
2125 if((c
= wintoclient(w
)))
2130 /* There's no way to check accesses to destroyed windows, thus those cases are
2131 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2132 * default error handler, which may call exit. */
2134 xerror(Display
*dpy
, XErrorEvent
*ee
) {
2135 if(ee
->error_code
== BadWindow
2136 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
2137 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
2138 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
2139 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
2140 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
2141 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
2142 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
2143 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
2145 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
2146 ee
->request_code
, ee
->error_code
);
2147 return xerrorxlib(dpy
, ee
); /* may call exit */
2151 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
2155 /* Startup Error handler to check if another window manager
2156 * is already running. */
2158 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
2159 die("dwm: another window manager is already running\n");
2164 zoom(const Arg
*arg
) {
2165 Client
*c
= selmon
->sel
;
2167 if(!selmon
->lt
[selmon
->sellt
]->arrange
2168 || (selmon
->sel
&& selmon
->sel
->isfloating
))
2170 if(c
== nexttiled(selmon
->clients
))
2171 if(!c
|| !(c
= nexttiled(c
->next
)))
2177 main(int argc
, char *argv
[]) {
2178 if(argc
== 2 && !strcmp("-v", argv
[1]))
2179 die("dwm-"VERSION
", © 2006-2012 dwm engineers, see LICENSE for details\n");
2181 die("usage: dwm [-v]\n");
2182 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
2183 fputs("warning: no locale support\n", stderr
);
2184 if(!(dpy
= XOpenDisplay(NULL
)))
2185 die("dwm: cannot open display\n");
2192 return EXIT_SUCCESS
;