Xinqi Bao's Git
48f50daf438284b6faf5cf4a5ffdede217120393
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))
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
, NetLast
}; /* EWMH atoms */
61 enum { WMProtocols
, WMDelete
, WMState
, WMLast
}; /* default atoms */
62 enum { ClkTagBar
, ClkLtSymbol
, ClkStatusText
, ClkWinTitle
,
63 ClkClientWin
, ClkRootWin
, ClkLast
}; /* clicks */
76 void (*func
)(const Arg
*arg
);
80 typedef struct Monitor Monitor
;
81 typedef struct Client Client
;
86 int basew
, baseh
, incw
, inch
, maxw
, maxh
, minw
, minh
;
89 Bool isfixed
, isfloating
, isurgent
;
98 unsigned long norm
[ColLast
];
99 unsigned long sel
[ColLast
];
109 } DC
; /* draw context */
114 void (*func
)(const Arg
*);
120 void (*arrange
)(Monitor
*);
127 int by
; /* bar geometry */
128 int mx
, my
, mw
, mh
; /* screen size */
129 int wx
, wy
, ww
, wh
; /* window area */
130 unsigned int seltags
;
132 unsigned int tagset
[2];
145 const char *instance
;
152 /* function declarations */
153 static void applyrules(Client
*c
);
154 static Bool
applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
);
155 static void arrange(void);
156 static void attach(Client
*c
);
157 static void attachstack(Client
*c
);
158 static void buttonpress(XEvent
*e
);
159 static void checkotherwm(void);
160 static void cleanup(void);
161 static void cleanupmons(void);
162 static void clearurgent(Client
*c
);
163 static void configure(Client
*c
);
164 static void configurenotify(XEvent
*e
);
165 static void configurerequest(XEvent
*e
);
166 static Monitor
*createmon(void);
167 static void destroynotify(XEvent
*e
);
168 static void detach(Client
*c
);
169 static void detachstack(Client
*c
);
170 static void die(const char *errstr
, ...);
171 static Monitor
*dirtomon(int dir
);
172 static void drawbar(Monitor
*m
);
173 static void drawbars(void);
174 static void drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]);
175 static void drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
);
176 static void enternotify(XEvent
*e
);
177 static void expose(XEvent
*e
);
178 static void focus(Client
*c
);
179 static void focusin(XEvent
*e
);
180 static void focusmon(const Arg
*arg
);
181 static void focusstack(const Arg
*arg
);
182 static unsigned long getcolor(const char *colstr
);
183 static Bool
getrootptr(int *x
, int *y
);
184 static long getstate(Window w
);
185 static Bool
gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
);
186 static void grabbuttons(Client
*c
, Bool focused
);
187 static void grabkeys(void);
188 static void initfont(const char *fontstr
);
189 static Bool
isprotodel(Client
*c
);
190 static void keypress(XEvent
*e
);
191 static void killclient(const Arg
*arg
);
192 static void manage(Window w
, XWindowAttributes
*wa
);
193 static void mappingnotify(XEvent
*e
);
194 static void maprequest(XEvent
*e
);
195 static void monocle(Monitor
*m
);
196 static void movemouse(const Arg
*arg
);
197 static Client
*nexttiled(Client
*c
);
198 static Monitor
*ptrtomon(int x
, int y
);
199 static void propertynotify(XEvent
*e
);
200 static void quit(const Arg
*arg
);
201 static void resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
);
202 static void resizemouse(const Arg
*arg
);
203 static void restack(Monitor
*m
);
204 static void run(void);
205 static void scan(void);
206 static void sendmon(Client
*c
, Monitor
*m
);
207 static void setclientstate(Client
*c
, long state
);
208 static void setlayout(const Arg
*arg
);
209 static void setmfact(const Arg
*arg
);
210 static void setup(void);
211 static void showhide(Client
*c
);
212 static void sigchld(int unused
);
213 static void spawn(const Arg
*arg
);
214 static void tag(const Arg
*arg
);
215 static void tagmon(const Arg
*arg
);
216 static int textnw(const char *text
, unsigned int len
);
217 static void tile(Monitor
*);
218 static void togglebar(const Arg
*arg
);
219 static void togglefloating(const Arg
*arg
);
220 static void toggletag(const Arg
*arg
);
221 static void toggleview(const Arg
*arg
);
222 static void unfocus(Client
*c
);
223 static void unmanage(Client
*c
, Bool destroyed
);
224 static void unmapnotify(XEvent
*e
);
225 static Bool
updategeom(void);
226 static void updatebarpos(Monitor
*m
);
227 static void updatebars(void);
228 static void updatenumlockmask(void);
229 static void updatesizehints(Client
*c
);
230 static void updatestatus(void);
231 static void updatetitle(Client
*c
);
232 static void updatewmhints(Client
*c
);
233 static void view(const Arg
*arg
);
234 static Client
*wintoclient(Window w
);
235 static Monitor
*wintomon(Window w
);
236 static int xerror(Display
*dpy
, XErrorEvent
*ee
);
237 static int xerrordummy(Display
*dpy
, XErrorEvent
*ee
);
238 static int xerrorstart(Display
*dpy
, XErrorEvent
*ee
);
239 static void zoom(const Arg
*arg
);
242 static const char broken
[] = "broken";
243 static char stext
[256];
245 static int sw
, sh
; /* X display screen geometry width, height */
246 static int bh
, blw
= 0; /* bar geometry */
247 static int (*xerrorxlib
)(Display
*, XErrorEvent
*);
248 static unsigned int numlockmask
= 0;
249 static void (*handler
[LASTEvent
]) (XEvent
*) = {
250 [ButtonPress
] = buttonpress
,
251 [ConfigureRequest
] = configurerequest
,
252 [ConfigureNotify
] = configurenotify
,
253 [DestroyNotify
] = destroynotify
,
254 [EnterNotify
] = enternotify
,
257 [KeyPress
] = keypress
,
258 [MappingNotify
] = mappingnotify
,
259 [MapRequest
] = maprequest
,
260 [PropertyNotify
] = propertynotify
,
261 [UnmapNotify
] = unmapnotify
263 static Atom wmatom
[WMLast
], netatom
[NetLast
];
265 static Bool running
= True
;
266 static Cursor cursor
[CurLast
];
269 static Monitor
*mons
= NULL
, *selmon
= NULL
;
272 /* configuration, allows nested code to access above variables */
275 /* compile-time check if all tags fit into an unsigned int bit array. */
276 struct NumTags
{ char limitexceeded
[LENGTH(tags
) > 31 ? -1 : 1]; };
278 /* function implementations */
280 applyrules(Client
*c
) {
281 const char *class, *instance
;
285 XClassHint ch
= { 0 };
288 c
->isfloating
= c
->tags
= 0;
289 if(XGetClassHint(dpy
, c
->win
, &ch
)) {
290 class = ch
.res_class
? ch
.res_class
: broken
;
291 instance
= ch
.res_name
? ch
.res_name
: broken
;
292 for(i
= 0; i
< LENGTH(rules
); i
++) {
294 if((!r
->title
|| strstr(c
->name
, r
->title
))
295 && (!r
->class || strstr(class, r
->class))
296 && (!r
->instance
|| strstr(instance
, r
->instance
)))
298 c
->isfloating
= r
->isfloating
;
300 for(m
= mons
; m
&& m
->num
!= r
->monitor
; m
= m
->next
);
310 c
->tags
= c
->tags
& TAGMASK
? c
->tags
& TAGMASK
: c
->mon
->tagset
[c
->mon
->seltags
];
314 applysizehints(Client
*c
, int *x
, int *y
, int *w
, int *h
, Bool interact
) {
318 /* set minimum possible */
326 if(*x
+ *w
+ 2 * c
->bw
< 0)
328 if(*y
+ *h
+ 2 * c
->bw
< 0)
332 if(*x
> m
->mx
+ m
->mw
)
333 *x
= m
->mx
+ m
->mw
- WIDTH(c
);
334 if(*y
> m
->my
+ m
->mh
)
335 *y
= m
->my
+ m
->mh
- HEIGHT(c
);
336 if(*x
+ *w
+ 2 * c
->bw
< m
->mx
)
338 if(*y
+ *h
+ 2 * c
->bw
< m
->my
)
345 if(resizehints
|| c
->isfloating
) {
346 /* see last two sentences in ICCCM 4.1.2.3 */
347 baseismin
= c
->basew
== c
->minw
&& c
->baseh
== c
->minh
;
348 if(!baseismin
) { /* temporarily remove base dimensions */
352 /* adjust for aspect limits */
353 if(c
->mina
> 0 && c
->maxa
> 0) {
354 if(c
->maxa
< (float)*w
/ *h
)
355 *w
= *h
* c
->maxa
+ 0.5;
356 else if(c
->mina
< (float)*h
/ *w
)
357 *h
= *w
* c
->mina
+ 0.5;
359 if(baseismin
) { /* increment calculation requires this */
363 /* adjust for increment value */
368 /* restore base dimensions */
371 *w
= MAX(*w
, c
->minw
);
372 *h
= MAX(*h
, c
->minh
);
374 *w
= MIN(*w
, c
->maxw
);
376 *h
= MIN(*h
, c
->maxh
);
378 return *x
!= c
->x
|| *y
!= c
->y
|| *w
!= c
->w
|| *h
!= c
->h
;
385 for(m
= mons
; m
; m
= m
->next
)
388 for(m
= mons
; m
; m
= m
->next
) {
389 strncpy(m
->ltsymbol
, m
->lt
[m
->sellt
]->symbol
, sizeof m
->ltsymbol
);
390 if(m
->lt
[m
->sellt
]->arrange
)
391 m
->lt
[m
->sellt
]->arrange(m
);
398 c
->next
= c
->mon
->clients
;
403 attachstack(Client
*c
) {
404 c
->snext
= c
->mon
->stack
;
409 buttonpress(XEvent
*e
) {
410 unsigned int i
, x
, click
;
414 XButtonPressedEvent
*ev
= &e
->xbutton
;
417 /* focus monitor if necessary */
418 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
419 unfocus(selmon
->sel
);
423 if(ev
->window
== selmon
->barwin
) {
427 } while(ev
->x
>= x
&& ++i
< LENGTH(tags
));
428 if(i
< LENGTH(tags
)) {
432 else if(ev
->x
< x
+ blw
)
434 else if(ev
->x
> selmon
->wx
+ selmon
->ww
- TEXTW(stext
))
435 click
= ClkStatusText
;
439 else if((c
= wintoclient(ev
->window
))) {
441 click
= ClkClientWin
;
443 for(i
= 0; i
< LENGTH(buttons
); i
++)
444 if(click
== buttons
[i
].click
&& buttons
[i
].func
&& buttons
[i
].button
== ev
->button
445 && CLEANMASK(buttons
[i
].mask
) == CLEANMASK(ev
->state
))
446 buttons
[i
].func(click
== ClkTagBar
&& buttons
[i
].arg
.i
== 0 ? &arg
: &buttons
[i
].arg
);
452 xerrorxlib
= XSetErrorHandler(xerrorstart
);
453 /* this causes an error if some other window manager is running */
454 XSelectInput(dpy
, DefaultRootWindow(dpy
), SubstructureRedirectMask
);
457 die("dwm: another window manager is already running\n");
458 XSetErrorHandler(xerror
);
465 Layout foo
= { "", NULL
};
469 selmon
->lt
[selmon
->sellt
] = &foo
;
470 for(m
= mons
; m
; m
= m
->next
)
472 unmanage(m
->stack
, False
);
474 XFreeFontSet(dpy
, dc
.font
.set
);
476 XFreeFont(dpy
, dc
.font
.xfont
);
477 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
478 XFreePixmap(dpy
, dc
.drawable
);
480 XFreeCursor(dpy
, cursor
[CurNormal
]);
481 XFreeCursor(dpy
, cursor
[CurResize
]);
482 XFreeCursor(dpy
, cursor
[CurMove
]);
485 XSetInputFocus(dpy
, PointerRoot
, RevertToPointerRoot
, CurrentTime
);
494 XUnmapWindow(dpy
, mons
->barwin
);
495 XDestroyWindow(dpy
, mons
->barwin
);
502 clearurgent(Client
*c
) {
506 if(!(wmh
= XGetWMHints(dpy
, c
->win
)))
508 wmh
->flags
&= ~XUrgencyHint
;
509 XSetWMHints(dpy
, c
->win
, wmh
);
514 configure(Client
*c
) {
517 ce
.type
= ConfigureNotify
;
525 ce
.border_width
= c
->bw
;
527 ce
.override_redirect
= False
;
528 XSendEvent(dpy
, c
->win
, False
, StructureNotifyMask
, (XEvent
*)&ce
);
532 configurenotify(XEvent
*e
) {
534 XConfigureEvent
*ev
= &e
->xconfigure
;
536 if(ev
->window
== root
) {
541 XFreePixmap(dpy
, dc
.drawable
);
542 dc
.drawable
= XCreatePixmap(dpy
, root
, sw
, bh
, DefaultDepth(dpy
, screen
));
544 for(m
= mons
; m
; m
= m
->next
)
545 XMoveResizeWindow(dpy
, m
->barwin
, m
->wx
, m
->by
, m
->ww
, bh
);
552 configurerequest(XEvent
*e
) {
555 XConfigureRequestEvent
*ev
= &e
->xconfigurerequest
;
558 if((c
= wintoclient(ev
->window
))) {
559 if(ev
->value_mask
& CWBorderWidth
)
560 c
->bw
= ev
->border_width
;
561 else if(c
->isfloating
|| !selmon
->lt
[selmon
->sellt
]->arrange
) {
563 if(ev
->value_mask
& CWX
)
564 c
->x
= m
->mx
+ ev
->x
;
565 if(ev
->value_mask
& CWY
)
566 c
->y
= m
->my
+ ev
->y
;
567 if(ev
->value_mask
& CWWidth
)
569 if(ev
->value_mask
& CWHeight
)
571 if((c
->x
+ c
->w
) > m
->mx
+ m
->mw
&& c
->isfloating
)
572 c
->x
= m
->mx
+ (m
->mw
/ 2 - c
->w
/ 2); /* center in x direction */
573 if((c
->y
+ c
->h
) > m
->my
+ m
->mh
&& c
->isfloating
)
574 c
->y
= m
->my
+ (m
->mh
/ 2 - c
->h
/ 2); /* center in y direction */
575 if((ev
->value_mask
& (CWX
|CWY
)) && !(ev
->value_mask
& (CWWidth
|CWHeight
)))
578 XMoveResizeWindow(dpy
, c
->win
, c
->x
, c
->y
, c
->w
, c
->h
);
586 wc
.width
= ev
->width
;
587 wc
.height
= ev
->height
;
588 wc
.border_width
= ev
->border_width
;
589 wc
.sibling
= ev
->above
;
590 wc
.stack_mode
= ev
->detail
;
591 XConfigureWindow(dpy
, ev
->window
, ev
->value_mask
, &wc
);
600 if(!(m
= (Monitor
*)calloc(1, sizeof(Monitor
))))
601 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor
));
602 m
->tagset
[0] = m
->tagset
[1] = 1;
604 m
->showbar
= showbar
;
606 m
->lt
[0] = &layouts
[0];
607 m
->lt
[1] = &layouts
[1 % LENGTH(layouts
)];
608 strncpy(m
->ltsymbol
, layouts
[0].symbol
, sizeof m
->ltsymbol
);
613 destroynotify(XEvent
*e
) {
615 XDestroyWindowEvent
*ev
= &e
->xdestroywindow
;
617 if((c
= wintoclient(ev
->window
)))
625 for(tc
= &c
->mon
->clients
; *tc
&& *tc
!= c
; tc
= &(*tc
)->next
);
630 detachstack(Client
*c
) {
633 for(tc
= &c
->mon
->stack
; *tc
&& *tc
!= c
; tc
= &(*tc
)->snext
);
636 if(c
== c
->mon
->sel
) {
637 for(t
= c
->mon
->stack
; t
&& !ISVISIBLE(t
); t
= t
->snext
);
643 die(const char *errstr
, ...) {
646 va_start(ap
, errstr
);
647 vfprintf(stderr
, errstr
, ap
);
657 if(!(m
= selmon
->next
))
662 for(m
= mons
; m
->next
; m
= m
->next
);
664 for(m
= mons
; m
->next
!= selmon
; m
= m
->next
);
670 drawbar(Monitor
*m
) {
672 unsigned int i
, occ
= 0, urg
= 0;
676 for(c
= m
->clients
; c
; c
= c
->next
) {
682 for(i
= 0; i
< LENGTH(tags
); i
++) {
683 dc
.w
= TEXTW(tags
[i
]);
684 col
= m
->tagset
[m
->seltags
] & 1 << i
? dc
.sel
: dc
.norm
;
685 drawtext(tags
[i
], col
, urg
& 1 << i
);
686 drawsquare(m
== selmon
&& selmon
->sel
&& selmon
->sel
->tags
& 1 << i
,
687 occ
& 1 << i
, urg
& 1 << i
, col
);
690 dc
.w
= blw
= TEXTW(m
->ltsymbol
);
691 drawtext(m
->ltsymbol
, dc
.norm
, False
);
694 if(m
== selmon
) { /* status is only drawn on selected monitor */
701 drawtext(stext
, dc
.norm
, False
);
705 if((dc
.w
= dc
.x
- x
) > bh
) {
708 col
= m
== selmon
? dc
.sel
: dc
.norm
;
709 drawtext(m
->sel
->name
, col
, False
);
710 drawsquare(m
->sel
->isfixed
, m
->sel
->isfloating
, False
, col
);
713 drawtext(NULL
, dc
.norm
, False
);
715 XCopyArea(dpy
, dc
.drawable
, m
->barwin
, dc
.gc
, 0, 0, m
->ww
, bh
, 0, 0);
723 for(m
= mons
; m
; m
= m
->next
)
728 drawsquare(Bool filled
, Bool empty
, Bool invert
, unsigned long col
[ColLast
]) {
731 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
733 gcv
.foreground
= col
[invert
? ColBG
: ColFG
];
734 XChangeGC(dpy
, dc
.gc
, GCForeground
, &gcv
);
735 x
= (dc
.font
.ascent
+ dc
.font
.descent
+ 2) / 4;
739 r
.width
= r
.height
= x
+ 1;
740 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
743 r
.width
= r
.height
= x
;
744 XDrawRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
749 drawtext(const char *text
, unsigned long col
[ColLast
], Bool invert
) {
751 int i
, x
, y
, h
, len
, olen
;
752 XRectangle r
= { dc
.x
, dc
.y
, dc
.w
, dc
.h
};
754 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColFG
: ColBG
]);
755 XFillRectangles(dpy
, dc
.drawable
, dc
.gc
, &r
, 1);
759 h
= dc
.font
.ascent
+ dc
.font
.descent
;
760 y
= dc
.y
+ (dc
.h
/ 2) - (h
/ 2) + dc
.font
.ascent
;
762 /* shorten text if necessary */
763 for(len
= MIN(olen
, sizeof buf
); len
&& textnw(text
, len
) > dc
.w
- h
; len
--);
766 memcpy(buf
, text
, len
);
768 for(i
= len
; i
&& i
> len
- 3; buf
[--i
] = '.');
769 XSetForeground(dpy
, dc
.gc
, col
[invert
? ColBG
: ColFG
]);
771 XmbDrawString(dpy
, dc
.drawable
, dc
.font
.set
, dc
.gc
, x
, y
, buf
, len
);
773 XDrawString(dpy
, dc
.drawable
, dc
.gc
, x
, y
, buf
, len
);
777 enternotify(XEvent
*e
) {
780 XCrossingEvent
*ev
= &e
->xcrossing
;
782 if((ev
->mode
!= NotifyNormal
|| ev
->detail
== NotifyInferior
) && ev
->window
!= root
)
784 if((m
= wintomon(ev
->window
)) && m
!= selmon
) {
785 unfocus(selmon
->sel
);
788 if((c
= wintoclient(ev
->window
)))
797 XExposeEvent
*ev
= &e
->xexpose
;
799 if(ev
->count
== 0 && (m
= wintomon(ev
->window
)))
805 if(!c
|| !ISVISIBLE(c
))
806 for(c
= selmon
->stack
; c
&& !ISVISIBLE(c
); c
= c
->snext
);
808 unfocus(selmon
->sel
);
816 grabbuttons(c
, True
);
817 XSetWindowBorder(dpy
, c
->win
, dc
.sel
[ColBorder
]);
818 XSetInputFocus(dpy
, c
->win
, RevertToPointerRoot
, CurrentTime
);
821 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
827 focusin(XEvent
*e
) { /* there are some broken focus acquiring clients */
828 XFocusChangeEvent
*ev
= &e
->xfocus
;
830 if(selmon
->sel
&& ev
->window
!= selmon
->sel
->win
)
831 XSetInputFocus(dpy
, selmon
->sel
->win
, RevertToPointerRoot
, CurrentTime
);
835 focusmon(const Arg
*arg
) {
840 m
= dirtomon(arg
->i
);
841 unfocus(selmon
->sel
);
847 focusstack(const Arg
*arg
) {
848 Client
*c
= NULL
, *i
;
853 for(c
= selmon
->sel
->next
; c
&& !ISVISIBLE(c
); c
= c
->next
);
855 for(c
= selmon
->clients
; c
&& !ISVISIBLE(c
); c
= c
->next
);
858 for(i
= selmon
->clients
; i
!= selmon
->sel
; i
= i
->next
)
862 for(; i
; i
= i
->next
)
873 getcolor(const char *colstr
) {
874 Colormap cmap
= DefaultColormap(dpy
, screen
);
877 if(!XAllocNamedColor(dpy
, cmap
, colstr
, &color
, &color
))
878 die("error, cannot allocate color '%s'\n", colstr
);
883 getrootptr(int *x
, int *y
) {
888 return XQueryPointer(dpy
, root
, &dummy
, &dummy
, x
, y
, &di
, &di
, &dui
);
895 unsigned char *p
= NULL
;
896 unsigned long n
, extra
;
899 status
= XGetWindowProperty(dpy
, w
, wmatom
[WMState
], 0L, 2L, False
, wmatom
[WMState
],
900 &real
, &format
, &n
, &extra
, (unsigned char **)&p
);
901 if(status
!= Success
)
910 gettextprop(Window w
, Atom atom
, char *text
, unsigned int size
) {
915 if(!text
|| size
== 0)
918 XGetTextProperty(dpy
, w
, &name
, atom
);
921 if(name
.encoding
== XA_STRING
)
922 strncpy(text
, (char *)name
.value
, size
- 1);
924 if(XmbTextPropertyToTextList(dpy
, &name
, &list
, &n
) >= Success
&& n
> 0 && *list
) {
925 strncpy(text
, *list
, size
- 1);
926 XFreeStringList(list
);
929 text
[size
- 1] = '\0';
935 grabbuttons(Client
*c
, Bool focused
) {
939 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
940 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
942 for(i
= 0; i
< LENGTH(buttons
); i
++)
943 if(buttons
[i
].click
== ClkClientWin
)
944 for(j
= 0; j
< LENGTH(modifiers
); j
++)
945 XGrabButton(dpy
, buttons
[i
].button
,
946 buttons
[i
].mask
| modifiers
[j
],
947 c
->win
, False
, BUTTONMASK
,
948 GrabModeAsync
, GrabModeSync
, None
, None
);
951 XGrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
, False
,
952 BUTTONMASK
, GrabModeAsync
, GrabModeSync
, None
, None
);
961 unsigned int modifiers
[] = { 0, LockMask
, numlockmask
, numlockmask
|LockMask
};
964 XUngrabKey(dpy
, AnyKey
, AnyModifier
, root
);
965 for(i
= 0; i
< LENGTH(keys
); i
++) {
966 if((code
= XKeysymToKeycode(dpy
, keys
[i
].keysym
)))
967 for(j
= 0; j
< LENGTH(modifiers
); j
++)
968 XGrabKey(dpy
, code
, keys
[i
].mod
| modifiers
[j
], root
,
969 True
, GrabModeAsync
, GrabModeAsync
);
975 initfont(const char *fontstr
) {
976 char *def
, **missing
;
980 dc
.font
.set
= XCreateFontSet(dpy
, fontstr
, &missing
, &n
, &def
);
983 fprintf(stderr
, "dwm: missing fontset: %s\n", missing
[n
]);
984 XFreeStringList(missing
);
987 XFontSetExtents
*font_extents
;
988 XFontStruct
**xfonts
;
991 dc
.font
.ascent
= dc
.font
.descent
= 0;
992 font_extents
= XExtentsOfFontSet(dc
.font
.set
);
993 n
= XFontsOfFontSet(dc
.font
.set
, &xfonts
, &font_names
);
994 for(i
= 0, dc
.font
.ascent
= 0, dc
.font
.descent
= 0; i
< n
; i
++) {
995 dc
.font
.ascent
= MAX(dc
.font
.ascent
, (*xfonts
)->ascent
);
996 dc
.font
.descent
= MAX(dc
.font
.descent
,(*xfonts
)->descent
);
1001 if(!(dc
.font
.xfont
= XLoadQueryFont(dpy
, fontstr
))
1002 && !(dc
.font
.xfont
= XLoadQueryFont(dpy
, "fixed")))
1003 die("error, cannot load font: '%s'\n", fontstr
);
1004 dc
.font
.ascent
= dc
.font
.xfont
->ascent
;
1005 dc
.font
.descent
= dc
.font
.xfont
->descent
;
1007 dc
.font
.height
= dc
.font
.ascent
+ dc
.font
.descent
;
1011 isprotodel(Client
*c
) {
1016 if(XGetWMProtocols(dpy
, c
->win
, &protocols
, &n
)) {
1017 for(i
= 0; !ret
&& i
< n
; i
++)
1018 if(protocols
[i
] == wmatom
[WMDelete
])
1027 isuniquegeom(XineramaScreenInfo
*unique
, size_t len
, XineramaScreenInfo
*info
) {
1030 for(i
= 0; i
< len
; i
++)
1031 if(unique
[i
].x_org
== info
->x_org
&& unique
[i
].y_org
== info
->y_org
1032 && unique
[i
].width
== info
->width
&& unique
[i
].height
== info
->height
)
1036 #endif /* XINERAMA */
1039 keypress(XEvent
*e
) {
1045 keysym
= XKeycodeToKeysym(dpy
, (KeyCode
)ev
->keycode
, 0);
1046 for(i
= 0; i
< LENGTH(keys
); i
++)
1047 if(keysym
== keys
[i
].keysym
1048 && CLEANMASK(keys
[i
].mod
) == CLEANMASK(ev
->state
)
1050 keys
[i
].func(&(keys
[i
].arg
));
1054 killclient(const Arg
*arg
) {
1059 if(isprotodel(selmon
->sel
)) {
1060 ev
.type
= ClientMessage
;
1061 ev
.xclient
.window
= selmon
->sel
->win
;
1062 ev
.xclient
.message_type
= wmatom
[WMProtocols
];
1063 ev
.xclient
.format
= 32;
1064 ev
.xclient
.data
.l
[0] = wmatom
[WMDelete
];
1065 ev
.xclient
.data
.l
[1] = CurrentTime
;
1066 XSendEvent(dpy
, selmon
->sel
->win
, False
, NoEventMask
, &ev
);
1070 XSetErrorHandler(xerrordummy
);
1071 XSetCloseDownMode(dpy
, DestroyAll
);
1072 XKillClient(dpy
, selmon
->sel
->win
);
1074 XSetErrorHandler(xerror
);
1080 manage(Window w
, XWindowAttributes
*wa
) {
1082 Client
*c
, *t
= NULL
;
1083 Window trans
= None
;
1086 if(!(c
= malloc(sizeof(Client
))))
1087 die("fatal: could not malloc() %u bytes\n", sizeof(Client
));
1091 if(XGetTransientForHint(dpy
, w
, &trans
))
1092 t
= wintoclient(trans
);
1102 c
->x
= wa
->x
+ c
->mon
->wx
;
1103 c
->y
= wa
->y
+ c
->mon
->wy
;
1106 c
->oldbw
= wa
->border_width
;
1107 if(c
->w
== c
->mon
->mw
&& c
->h
== c
->mon
->mh
) {
1113 if(c
->x
+ WIDTH(c
) > c
->mon
->mx
+ c
->mon
->mw
)
1114 c
->x
= c
->mon
->mx
+ c
->mon
->mw
- WIDTH(c
);
1115 if(c
->y
+ HEIGHT(c
) > c
->mon
->my
+ c
->mon
->mh
)
1116 c
->y
= c
->mon
->my
+ c
->mon
->mh
- HEIGHT(c
);
1117 c
->x
= MAX(c
->x
, c
->mon
->mx
);
1118 /* only fix client y-offset, if the client center might cover the bar */
1119 c
->y
= MAX(c
->y
, ((c
->mon
->by
== 0) && (c
->x
+ (c
->w
/ 2) >= c
->mon
->wx
)
1120 && (c
->x
+ (c
->w
/ 2) < c
->mon
->wx
+ c
->mon
->ww
)) ? bh
: c
->mon
->my
);
1123 wc
.border_width
= c
->bw
;
1124 XConfigureWindow(dpy
, w
, CWBorderWidth
, &wc
);
1125 XSetWindowBorder(dpy
, w
, dc
.norm
[ColBorder
]);
1126 configure(c
); /* propagates border_width, if size doesn't change */
1128 XSelectInput(dpy
, w
, EnterWindowMask
|FocusChangeMask
|PropertyChangeMask
|StructureNotifyMask
);
1129 grabbuttons(c
, False
);
1131 c
->isfloating
= trans
!= None
|| c
->isfixed
;
1133 XRaiseWindow(dpy
, c
->win
);
1136 XMoveResizeWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
, c
->w
, c
->h
); /* some windows require this */
1137 XMapWindow(dpy
, c
->win
);
1138 setclientstate(c
, NormalState
);
1143 mappingnotify(XEvent
*e
) {
1144 XMappingEvent
*ev
= &e
->xmapping
;
1146 XRefreshKeyboardMapping(ev
);
1147 if(ev
->request
== MappingKeyboard
)
1152 maprequest(XEvent
*e
) {
1153 static XWindowAttributes wa
;
1154 XMapRequestEvent
*ev
= &e
->xmaprequest
;
1156 if(!XGetWindowAttributes(dpy
, ev
->window
, &wa
))
1158 if(wa
.override_redirect
)
1160 if(!wintoclient(ev
->window
))
1161 manage(ev
->window
, &wa
);
1165 monocle(Monitor
*m
) {
1169 for(c
= m
->clients
; c
; c
= c
->next
)
1172 if(n
> 0) /* override layout symbol */
1173 snprintf(m
->ltsymbol
, sizeof m
->ltsymbol
, "[%d]", n
);
1174 for(c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
))
1175 resize(c
, m
->wx
, m
->wy
, m
->ww
- 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1179 movemouse(const Arg
*arg
) {
1180 int x
, y
, ocx
, ocy
, nx
, ny
;
1185 if(!(c
= selmon
->sel
))
1190 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1191 None
, cursor
[CurMove
], CurrentTime
) != GrabSuccess
)
1193 if(!getrootptr(&x
, &y
))
1196 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1198 case ConfigureRequest
:
1201 handler
[ev
.type
](&ev
);
1204 nx
= ocx
+ (ev
.xmotion
.x
- x
);
1205 ny
= ocy
+ (ev
.xmotion
.y
- y
);
1206 if(snap
&& nx
>= selmon
->wx
&& nx
<= selmon
->wx
+ selmon
->ww
1207 && ny
>= selmon
->wy
&& ny
<= selmon
->wy
+ selmon
->wh
) {
1208 if(abs(selmon
->wx
- nx
) < snap
)
1210 else if(abs((selmon
->wx
+ selmon
->ww
) - (nx
+ WIDTH(c
))) < snap
)
1211 nx
= selmon
->wx
+ selmon
->ww
- WIDTH(c
);
1212 if(abs(selmon
->wy
- ny
) < snap
)
1214 else if(abs((selmon
->wy
+ selmon
->wh
) - (ny
+ HEIGHT(c
))) < snap
)
1215 ny
= selmon
->wy
+ selmon
->wh
- HEIGHT(c
);
1216 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1217 && (abs(nx
- c
->x
) > snap
|| abs(ny
- c
->y
) > snap
))
1218 togglefloating(NULL
);
1220 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1221 resize(c
, nx
, ny
, c
->w
, c
->h
, True
);
1224 } while(ev
.type
!= ButtonRelease
);
1225 XUngrabPointer(dpy
, CurrentTime
);
1226 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1234 nexttiled(Client
*c
) {
1235 for(; c
&& (c
->isfloating
|| !ISVISIBLE(c
)); c
= c
->next
);
1240 ptrtomon(int x
, int y
) {
1243 for(m
= mons
; m
; m
= m
->next
)
1244 if(INRECT(x
, y
, m
->wx
, m
->wy
, m
->ww
, m
->wh
))
1250 propertynotify(XEvent
*e
) {
1253 XPropertyEvent
*ev
= &e
->xproperty
;
1255 if((ev
->window
== root
) && (ev
->atom
== XA_WM_NAME
))
1257 else if(ev
->state
== PropertyDelete
)
1258 return; /* ignore */
1259 else if((c
= wintoclient(ev
->window
))) {
1262 case XA_WM_TRANSIENT_FOR
:
1263 XGetTransientForHint(dpy
, c
->win
, &trans
);
1264 if(!c
->isfloating
&& (c
->isfloating
= (wintoclient(trans
) != NULL
)))
1267 case XA_WM_NORMAL_HINTS
:
1275 if(ev
->atom
== XA_WM_NAME
|| ev
->atom
== netatom
[NetWMName
]) {
1277 if(c
== c
->mon
->sel
)
1284 quit(const Arg
*arg
) {
1289 resize(Client
*c
, int x
, int y
, int w
, int h
, Bool interact
) {
1292 if(applysizehints(c
, &x
, &y
, &w
, &h
, interact
)) {
1295 c
->w
= wc
.width
= w
;
1296 c
->h
= wc
.height
= h
;
1297 wc
.border_width
= c
->bw
;
1298 XConfigureWindow(dpy
, c
->win
, CWX
|CWY
|CWWidth
|CWHeight
|CWBorderWidth
, &wc
);
1305 resizemouse(const Arg
*arg
) {
1312 if(!(c
= selmon
->sel
))
1317 if(XGrabPointer(dpy
, root
, False
, MOUSEMASK
, GrabModeAsync
, GrabModeAsync
,
1318 None
, cursor
[CurResize
], CurrentTime
) != GrabSuccess
)
1320 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1322 XMaskEvent(dpy
, MOUSEMASK
|ExposureMask
|SubstructureRedirectMask
, &ev
);
1324 case ConfigureRequest
:
1327 handler
[ev
.type
](&ev
);
1330 nw
= MAX(ev
.xmotion
.x
- ocx
- 2 * c
->bw
+ 1, 1);
1331 nh
= MAX(ev
.xmotion
.y
- ocy
- 2 * c
->bw
+ 1, 1);
1332 if(snap
&& nw
>= selmon
->wx
&& nw
<= selmon
->wx
+ selmon
->ww
1333 && nh
>= selmon
->wy
&& nh
<= selmon
->wy
+ selmon
->wh
)
1335 if(!c
->isfloating
&& selmon
->lt
[selmon
->sellt
]->arrange
1336 && (abs(nw
- c
->w
) > snap
|| abs(nh
- c
->h
) > snap
))
1337 togglefloating(NULL
);
1339 if(!selmon
->lt
[selmon
->sellt
]->arrange
|| c
->isfloating
)
1340 resize(c
, c
->x
, c
->y
, nw
, nh
, True
);
1343 } while(ev
.type
!= ButtonRelease
);
1344 XWarpPointer(dpy
, None
, c
->win
, 0, 0, 0, 0, c
->w
+ c
->bw
- 1, c
->h
+ c
->bw
- 1);
1345 XUngrabPointer(dpy
, CurrentTime
);
1346 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1347 if((m
= ptrtomon(c
->x
+ c
->w
/ 2, c
->y
+ c
->h
/ 2)) != selmon
) {
1355 restack(Monitor
*m
) {
1363 if(m
->sel
->isfloating
|| !m
->lt
[m
->sellt
]->arrange
)
1364 XRaiseWindow(dpy
, m
->sel
->win
);
1365 if(m
->lt
[m
->sellt
]->arrange
) {
1366 wc
.stack_mode
= Below
;
1367 wc
.sibling
= m
->barwin
;
1368 for(c
= m
->stack
; c
; c
= c
->snext
)
1369 if(!c
->isfloating
&& ISVISIBLE(c
)) {
1370 XConfigureWindow(dpy
, c
->win
, CWSibling
|CWStackMode
, &wc
);
1371 wc
.sibling
= c
->win
;
1375 while(XCheckMaskEvent(dpy
, EnterWindowMask
, &ev
));
1382 /* main event loop */
1384 while(running
&& !XNextEvent(dpy
, &ev
))
1385 if(handler
[ev
.type
])
1386 handler
[ev
.type
](&ev
); /* call handler */
1391 unsigned int i
, num
;
1392 Window d1
, d2
, *wins
= NULL
;
1393 XWindowAttributes wa
;
1395 if(XQueryTree(dpy
, root
, &d1
, &d2
, &wins
, &num
)) {
1396 for(i
= 0; i
< num
; i
++) {
1397 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
)
1398 || wa
.override_redirect
|| XGetTransientForHint(dpy
, wins
[i
], &d1
))
1400 if(wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
)
1401 manage(wins
[i
], &wa
);
1403 for(i
= 0; i
< num
; i
++) { /* now the transients */
1404 if(!XGetWindowAttributes(dpy
, wins
[i
], &wa
))
1406 if(XGetTransientForHint(dpy
, wins
[i
], &d1
)
1407 && (wa
.map_state
== IsViewable
|| getstate(wins
[i
]) == IconicState
))
1408 manage(wins
[i
], &wa
);
1416 sendmon(Client
*c
, Monitor
*m
) {
1423 c
->tags
= m
->tagset
[m
->seltags
]; /* assign tags of target monitor */
1431 setclientstate(Client
*c
, long state
) {
1432 long data
[] = { state
, None
};
1434 XChangeProperty(dpy
, c
->win
, wmatom
[WMState
], wmatom
[WMState
], 32,
1435 PropModeReplace
, (unsigned char *)data
, 2);
1439 setlayout(const Arg
*arg
) {
1440 if(!arg
|| !arg
->v
|| arg
->v
!= selmon
->lt
[selmon
->sellt
])
1443 selmon
->lt
[selmon
->sellt
] = (Layout
*)arg
->v
;
1450 /* arg > 1.0 will set mfact absolutly */
1452 setmfact(const Arg
*arg
) {
1455 if(!arg
|| !selmon
->lt
[selmon
->sellt
]->arrange
)
1457 f
= arg
->f
< 1.0 ? arg
->f
+ selmon
->mfact
: arg
->f
- 1.0;
1458 if(f
< 0.1 || f
> 0.9)
1466 XSetWindowAttributes wa
;
1468 /* clean up any zombies immediately */
1472 screen
= DefaultScreen(dpy
);
1473 root
= RootWindow(dpy
, screen
);
1475 sw
= DisplayWidth(dpy
, screen
);
1476 sh
= DisplayHeight(dpy
, screen
);
1477 bh
= dc
.h
= dc
.font
.height
+ 2;
1480 wmatom
[WMProtocols
] = XInternAtom(dpy
, "WM_PROTOCOLS", False
);
1481 wmatom
[WMDelete
] = XInternAtom(dpy
, "WM_DELETE_WINDOW", False
);
1482 wmatom
[WMState
] = XInternAtom(dpy
, "WM_STATE", False
);
1483 netatom
[NetSupported
] = XInternAtom(dpy
, "_NET_SUPPORTED", False
);
1484 netatom
[NetWMName
] = XInternAtom(dpy
, "_NET_WM_NAME", False
);
1486 cursor
[CurNormal
] = XCreateFontCursor(dpy
, XC_left_ptr
);
1487 cursor
[CurResize
] = XCreateFontCursor(dpy
, XC_sizing
);
1488 cursor
[CurMove
] = XCreateFontCursor(dpy
, XC_fleur
);
1489 /* init appearance */
1490 dc
.norm
[ColBorder
] = getcolor(normbordercolor
);
1491 dc
.norm
[ColBG
] = getcolor(normbgcolor
);
1492 dc
.norm
[ColFG
] = getcolor(normfgcolor
);
1493 dc
.sel
[ColBorder
] = getcolor(selbordercolor
);
1494 dc
.sel
[ColBG
] = getcolor(selbgcolor
);
1495 dc
.sel
[ColFG
] = getcolor(selfgcolor
);
1496 dc
.drawable
= XCreatePixmap(dpy
, root
, DisplayWidth(dpy
, screen
), bh
, DefaultDepth(dpy
, screen
));
1497 dc
.gc
= XCreateGC(dpy
, root
, 0, NULL
);
1498 XSetLineAttributes(dpy
, dc
.gc
, 1, LineSolid
, CapButt
, JoinMiter
);
1500 XSetFont(dpy
, dc
.gc
, dc
.font
.xfont
->fid
);
1504 /* EWMH support per view */
1505 XChangeProperty(dpy
, root
, netatom
[NetSupported
], XA_ATOM
, 32,
1506 PropModeReplace
, (unsigned char *) netatom
, NetLast
);
1507 /* select for events */
1508 wa
.cursor
= cursor
[CurNormal
];
1509 wa
.event_mask
= SubstructureRedirectMask
|SubstructureNotifyMask
|ButtonPressMask
1510 |EnterWindowMask
|LeaveWindowMask
|StructureNotifyMask
1511 |PropertyChangeMask
;
1512 XChangeWindowAttributes(dpy
, root
, CWEventMask
|CWCursor
, &wa
);
1513 XSelectInput(dpy
, root
, wa
.event_mask
);
1518 showhide(Client
*c
) {
1521 if(ISVISIBLE(c
)) { /* show clients top down */
1522 XMoveWindow(dpy
, c
->win
, c
->x
, c
->y
);
1523 if(!c
->mon
->lt
[c
->mon
->sellt
]->arrange
|| c
->isfloating
)
1524 resize(c
, c
->x
, c
->y
, c
->w
, c
->h
, False
);
1527 else { /* hide clients bottom up */
1529 XMoveWindow(dpy
, c
->win
, c
->x
+ 2 * sw
, c
->y
);
1535 sigchld(int unused
) {
1536 if(signal(SIGCHLD
, sigchld
) == SIG_ERR
)
1537 die("Can't install SIGCHLD handler");
1538 while(0 < waitpid(-1, NULL
, WNOHANG
));
1542 spawn(const Arg
*arg
) {
1545 close(ConnectionNumber(dpy
));
1547 execvp(((char **)arg
->v
)[0], (char **)arg
->v
);
1548 fprintf(stderr
, "dwm: execvp %s", ((char **)arg
->v
)[0]);
1555 tag(const Arg
*arg
) {
1556 if(selmon
->sel
&& arg
->ui
& TAGMASK
) {
1557 selmon
->sel
->tags
= arg
->ui
& TAGMASK
;
1563 tagmon(const Arg
*arg
) {
1564 if(!selmon
->sel
|| !mons
->next
)
1566 sendmon(selmon
->sel
, dirtomon(arg
->i
));
1570 textnw(const char *text
, unsigned int len
) {
1574 XmbTextExtents(dc
.font
.set
, text
, len
, NULL
, &r
);
1577 return XTextWidth(dc
.font
.xfont
, text
, len
);
1586 for(n
= 0, c
= nexttiled(m
->clients
); c
; c
= nexttiled(c
->next
), n
++);
1590 c
= nexttiled(m
->clients
);
1591 mw
= m
->mfact
* m
->ww
;
1592 resize(c
, m
->wx
, m
->wy
, (n
== 1 ? m
->ww
: mw
) - 2 * c
->bw
, m
->wh
- 2 * c
->bw
, False
);
1596 x
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? c
->x
+ c
->w
+ 2 * c
->bw
: m
->wx
+ mw
;
1598 w
= (m
->wx
+ mw
> c
->x
+ c
->w
) ? m
->wx
+ m
->ww
- x
: m
->ww
- mw
;
1602 for(i
= 0, c
= nexttiled(c
->next
); c
; c
= nexttiled(c
->next
), i
++) {
1603 resize(c
, x
, y
, w
- 2 * c
->bw
, /* remainder */ ((i
+ 1 == n
)
1604 ? m
->wy
+ m
->wh
- y
- 2 * c
->bw
: h
- 2 * c
->bw
), False
);
1606 y
= c
->y
+ HEIGHT(c
);
1611 togglebar(const Arg
*arg
) {
1612 selmon
->showbar
= !selmon
->showbar
;
1613 updatebarpos(selmon
);
1614 XMoveResizeWindow(dpy
, selmon
->barwin
, selmon
->wx
, selmon
->by
, selmon
->ww
, bh
);
1619 togglefloating(const Arg
*arg
) {
1622 selmon
->sel
->isfloating
= !selmon
->sel
->isfloating
|| selmon
->sel
->isfixed
;
1623 if(selmon
->sel
->isfloating
)
1624 resize(selmon
->sel
, selmon
->sel
->x
, selmon
->sel
->y
,
1625 selmon
->sel
->w
, selmon
->sel
->h
, False
);
1630 toggletag(const Arg
*arg
) {
1631 unsigned int newtags
;
1635 newtags
= selmon
->sel
->tags
^ (arg
->ui
& TAGMASK
);
1637 selmon
->sel
->tags
= newtags
;
1643 toggleview(const Arg
*arg
) {
1644 unsigned int newtagset
= selmon
->tagset
[selmon
->seltags
] ^ (arg
->ui
& TAGMASK
);
1647 selmon
->tagset
[selmon
->seltags
] = newtagset
;
1653 unfocus(Client
*c
) {
1656 grabbuttons(c
, False
);
1657 XSetWindowBorder(dpy
, c
->win
, dc
.norm
[ColBorder
]);
1658 XSetInputFocus(dpy
, root
, RevertToPointerRoot
, CurrentTime
);
1662 unmanage(Client
*c
, Bool destroyed
) {
1665 /* The server grab construct avoids race conditions. */
1669 wc
.border_width
= c
->oldbw
;
1671 XSetErrorHandler(xerrordummy
);
1672 XConfigureWindow(dpy
, c
->win
, CWBorderWidth
, &wc
); /* restore border */
1673 XUngrabButton(dpy
, AnyButton
, AnyModifier
, c
->win
);
1674 setclientstate(c
, WithdrawnState
);
1676 XSetErrorHandler(xerror
);
1685 unmapnotify(XEvent
*e
) {
1687 XUnmapEvent
*ev
= &e
->xunmap
;
1689 if((c
= wintoclient(ev
->window
)))
1696 XSetWindowAttributes wa
;
1698 wa
.override_redirect
= True
;
1699 wa
.background_pixmap
= ParentRelative
;
1700 wa
.event_mask
= ButtonPressMask
|ExposureMask
;
1701 for(m
= mons
; m
; m
= m
->next
) {
1702 m
->barwin
= XCreateWindow(dpy
, root
, m
->wx
, m
->by
, m
->ww
, bh
, 0, DefaultDepth(dpy
, screen
),
1703 CopyFromParent
, DefaultVisual(dpy
, screen
),
1704 CWOverrideRedirect
|CWBackPixmap
|CWEventMask
, &wa
);
1705 XDefineCursor(dpy
, m
->barwin
, cursor
[CurNormal
]);
1706 XMapRaised(dpy
, m
->barwin
);
1711 updatebarpos(Monitor
*m
) {
1716 m
->by
= m
->topbar
? m
->wy
: m
->wy
+ m
->wh
;
1717 m
->wy
= m
->topbar
? m
->wy
+ bh
: m
->wy
;
1728 if(XineramaIsActive(dpy
)) {
1731 XineramaScreenInfo
*info
= XineramaQueryScreens(dpy
, &nn
);
1732 XineramaScreenInfo
*unique
= NULL
;
1734 info
= XineramaQueryScreens(dpy
, &nn
);
1735 for(n
= 0, m
= mons
; m
; m
= m
->next
, n
++);
1736 /* only consider unique geometries as separate screens */
1737 if(!(unique
= (XineramaScreenInfo
*)malloc(sizeof(XineramaScreenInfo
) * nn
)))
1738 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo
) * nn
);
1739 for(i
= 0, j
= 0; i
< nn
; i
++)
1740 if(isuniquegeom(unique
, j
, &info
[i
]))
1741 memcpy(&unique
[j
++], &info
[i
], sizeof(XineramaScreenInfo
));
1745 for(i
= 0; i
< (nn
- n
); i
++) { /* new monitors available */
1746 for(m
= mons
; m
&& m
->next
; m
= m
->next
);
1748 m
->next
= createmon();
1752 for(i
= 0, m
= mons
; i
< nn
&& m
; m
= m
->next
, i
++)
1754 || (unique
[i
].x_org
!= m
->mx
|| unique
[i
].y_org
!= m
->my
1755 || unique
[i
].width
!= m
->mw
|| unique
[i
].height
!= m
->mh
))
1758 m
->num
= unique
[i
].screen_number
;
1759 m
->mx
= m
->wx
= unique
[i
].x_org
;
1760 m
->my
= m
->wy
= unique
[i
].y_org
;
1761 m
->mw
= m
->ww
= unique
[i
].width
;
1762 m
->mh
= m
->wh
= unique
[i
].height
;
1766 else { /* less monitors available */
1774 #endif /* XINERAMA */
1775 /* default monitor setup */
1779 if(mons
->mw
!= sw
|| mons
->mh
!= sh
) {
1781 mons
->mw
= mons
->ww
= sw
;
1782 mons
->mh
= mons
->wh
= sh
;
1788 selmon
= wintomon(root
);
1794 updatenumlockmask(void) {
1796 XModifierKeymap
*modmap
;
1799 modmap
= XGetModifierMapping(dpy
);
1800 for(i
= 0; i
< 8; i
++)
1801 for(j
= 0; j
< modmap
->max_keypermod
; j
++)
1802 if(modmap
->modifiermap
[i
* modmap
->max_keypermod
+ j
]
1803 == XKeysymToKeycode(dpy
, XK_Num_Lock
))
1804 numlockmask
= (1 << i
);
1805 XFreeModifiermap(modmap
);
1809 updatesizehints(Client
*c
) {
1813 if(!XGetWMNormalHints(dpy
, c
->win
, &size
, &msize
))
1814 /* size is uninitialized, ensure that size.flags aren't used */
1816 if(size
.flags
& PBaseSize
) {
1817 c
->basew
= size
.base_width
;
1818 c
->baseh
= size
.base_height
;
1820 else if(size
.flags
& PMinSize
) {
1821 c
->basew
= size
.min_width
;
1822 c
->baseh
= size
.min_height
;
1825 c
->basew
= c
->baseh
= 0;
1826 if(size
.flags
& PResizeInc
) {
1827 c
->incw
= size
.width_inc
;
1828 c
->inch
= size
.height_inc
;
1831 c
->incw
= c
->inch
= 0;
1832 if(size
.flags
& PMaxSize
) {
1833 c
->maxw
= size
.max_width
;
1834 c
->maxh
= size
.max_height
;
1837 c
->maxw
= c
->maxh
= 0;
1838 if(size
.flags
& PMinSize
) {
1839 c
->minw
= size
.min_width
;
1840 c
->minh
= size
.min_height
;
1842 else if(size
.flags
& PBaseSize
) {
1843 c
->minw
= size
.base_width
;
1844 c
->minh
= size
.base_height
;
1847 c
->minw
= c
->minh
= 0;
1848 if(size
.flags
& PAspect
) {
1849 c
->mina
= (float)size
.min_aspect
.y
/ size
.min_aspect
.x
;
1850 c
->maxa
= (float)size
.max_aspect
.x
/ size
.max_aspect
.y
;
1853 c
->maxa
= c
->mina
= 0.0;
1854 c
->isfixed
= (c
->maxw
&& c
->minw
&& c
->maxh
&& c
->minh
1855 && c
->maxw
== c
->minw
&& c
->maxh
== c
->minh
);
1859 updatetitle(Client
*c
) {
1860 if(!gettextprop(c
->win
, netatom
[NetWMName
], c
->name
, sizeof c
->name
))
1861 gettextprop(c
->win
, XA_WM_NAME
, c
->name
, sizeof c
->name
);
1862 if(c
->name
[0] == '\0') /* hack to mark broken clients */
1863 strcpy(c
->name
, broken
);
1867 updatestatus(void) {
1868 if(!gettextprop(root
, XA_WM_NAME
, stext
, sizeof(stext
)))
1869 strcpy(stext
, "dwm-"VERSION
);
1874 updatewmhints(Client
*c
) {
1877 if((wmh
= XGetWMHints(dpy
, c
->win
))) {
1878 if(c
== selmon
->sel
&& wmh
->flags
& XUrgencyHint
) {
1879 wmh
->flags
&= ~XUrgencyHint
;
1880 XSetWMHints(dpy
, c
->win
, wmh
);
1883 c
->isurgent
= (wmh
->flags
& XUrgencyHint
) ? True
: False
;
1889 view(const Arg
*arg
) {
1890 if((arg
->ui
& TAGMASK
) == selmon
->tagset
[selmon
->seltags
])
1892 selmon
->seltags
^= 1; /* toggle sel tagset */
1893 if(arg
->ui
& TAGMASK
)
1894 selmon
->tagset
[selmon
->seltags
] = arg
->ui
& TAGMASK
;
1899 wintoclient(Window w
) {
1903 for(m
= mons
; m
; m
= m
->next
)
1904 for(c
= m
->clients
; c
; c
= c
->next
)
1911 wintomon(Window w
) {
1916 if(w
== root
&& getrootptr(&x
, &y
))
1917 return ptrtomon(x
, y
);
1918 for(m
= mons
; m
; m
= m
->next
)
1921 if((c
= wintoclient(w
)))
1926 /* There's no way to check accesses to destroyed windows, thus those cases are
1927 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
1928 * default error handler, which may call exit. */
1930 xerror(Display
*dpy
, XErrorEvent
*ee
) {
1931 if(ee
->error_code
== BadWindow
1932 || (ee
->request_code
== X_SetInputFocus
&& ee
->error_code
== BadMatch
)
1933 || (ee
->request_code
== X_PolyText8
&& ee
->error_code
== BadDrawable
)
1934 || (ee
->request_code
== X_PolyFillRectangle
&& ee
->error_code
== BadDrawable
)
1935 || (ee
->request_code
== X_PolySegment
&& ee
->error_code
== BadDrawable
)
1936 || (ee
->request_code
== X_ConfigureWindow
&& ee
->error_code
== BadMatch
)
1937 || (ee
->request_code
== X_GrabButton
&& ee
->error_code
== BadAccess
)
1938 || (ee
->request_code
== X_GrabKey
&& ee
->error_code
== BadAccess
)
1939 || (ee
->request_code
== X_CopyArea
&& ee
->error_code
== BadDrawable
))
1941 fprintf(stderr
, "dwm: fatal error: request code=%d, error code=%d\n",
1942 ee
->request_code
, ee
->error_code
);
1943 return xerrorxlib(dpy
, ee
); /* may call exit */
1947 xerrordummy(Display
*dpy
, XErrorEvent
*ee
) {
1951 /* Startup Error handler to check if another window manager
1952 * is already running. */
1954 xerrorstart(Display
*dpy
, XErrorEvent
*ee
) {
1960 zoom(const Arg
*arg
) {
1961 Client
*c
= selmon
->sel
;
1963 if(!selmon
->lt
[selmon
->sellt
]->arrange
1964 || selmon
->lt
[selmon
->sellt
]->arrange
== monocle
1965 || (selmon
->sel
&& selmon
->sel
->isfloating
))
1967 if(c
== nexttiled(selmon
->clients
))
1968 if(!c
|| !(c
= nexttiled(c
->next
)))
1977 main(int argc
, char *argv
[]) {
1978 if(argc
== 2 && !strcmp("-v", argv
[1]))
1979 die("dwm-"VERSION
", © 2006-2009 dwm engineers, see LICENSE for details\n");
1981 die("usage: dwm [-v]\n");
1982 if(!setlocale(LC_CTYPE
, "") || !XSupportsLocale())
1983 fputs("warning: no locale support\n", stderr
);
1984 if(!(dpy
= XOpenDisplay(NULL
)))
1985 die("dwm: cannot open display\n");